Skip to content

Commit

Permalink
Fix sorting of custom language texts (terminal42#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
bytehead authored Dec 3, 2023
1 parent 767d819 commit 2fd2ed6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/Helper/LanguageText.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public function orderNavigationItems(array &$items): void
usort(
$items,
static function (NavigationItem $a, NavigationItem $b) use ($languages) {
$key1 = array_search(strtolower($a->getLanguageTag()), $languages, true);
$key2 = array_search(strtolower($b->getLanguageTag()), $languages, true);
$key1 = array_search(strtolower($a->getLocaleId()), $languages, true);
$key2 = array_search(strtolower($b->getLocaleId()), $languages, true);

return $key1 <=> $key2;
},
Expand Down
20 changes: 10 additions & 10 deletions tests/Helper/LanguageTextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ public function testHasLanguageInMap(): void
$map = [
'en' => 'International',
'de' => 'Germany',
'de-CH' => 'Switzerland (German)',
'de_CH' => 'Switzerland (German)',
];

$languageText = new LanguageText($map);

$this->assertTrue($languageText->has('en'));
$this->assertTrue($languageText->has('de'));
$this->assertTrue($languageText->has('de-CH'));
$this->assertTrue($languageText->has('de_CH'));
$this->assertFalse($languageText->has('fr'));
}

Expand Down Expand Up @@ -78,28 +78,28 @@ public function testOrdersNavigationItemsAccordingToCustomMap(): void
{
$map = [
'en' => 'International',
'de-CH' => 'Switzerland (German)',
'de_CH' => 'Switzerland (German)',
'de' => 'Germany',
'fr-FR' => 'France',
'fr_FR' => 'France',
'pl' => 'Poland',
];

$languageText = new LanguageText($map);

// items do not get added in "correct" order on purpose to test the sorting
$items = [];
$items[] = new NavigationItem($this->createRootPage('bar.ch', 'de-CH'));
$items[] = new NavigationItem($this->createRootPage('bar.ch', 'de_CH'));
$items[] = new NavigationItem($this->createRootPage('world.pl', 'pl'));
$items[] = new NavigationItem($this->createRootPage('foo.com', 'en'));
$items[] = new NavigationItem($this->createRootPage('hello.fr', 'fr-FR'));
$items[] = new NavigationItem($this->createRootPage('hello.fr', 'fr_FR'));
$items[] = new NavigationItem($this->createRootPage('baz.de', 'de'));

$languageText->orderNavigationItems($items);
$keys = array_keys($map);

foreach ($items as $i => $item) {
// items order should be equal to the order in the map which was passed to LanguageText
$this->assertSame($keys[$i], $item->getLanguageTag());
$this->assertSame($keys[$i], $item->getLocaleId());
}
}

Expand All @@ -110,13 +110,13 @@ public function testIgnoresOrderIfMapIsEmpty(): void
/** @var array<NavigationItem> $items */
$items = [
new NavigationItem($this->createRootPage('foo.com', 'en')),
new NavigationItem($this->createRootPage('bar.ch', 'de-CH')),
new NavigationItem($this->createRootPage('bar.ch', 'de_CH')),
];

$languageText->orderNavigationItems($items);

$this->assertSame('en', $items[0]->getLanguageTag());
$this->assertSame('de-CH', $items[1]->getLanguageTag());
$this->assertSame('en', $items[0]->getLocaleId());
$this->assertSame('de_CH', $items[1]->getLocaleId());
}

public function testIsCreatedFromOptionWizard(): void
Expand Down

0 comments on commit 2fd2ed6

Please sign in to comment.