Commit 9bbfedec authored by alexpott's avatar alexpott

Issue #2304651 by YesCT, martin107: Fixed Core Language sort() should not...

Issue #2304651 by YesCT, martin107: Fixed Core Language sort() should not work, needs to use name instead of default title.
parent 54d5db55
......@@ -7,6 +7,8 @@
namespace Drupal\Core\Language;
use Drupal\Component\Utility\SortArray;
/**
* An object containing the information for an interface language.
*
......@@ -217,7 +219,9 @@ public function setNegotiationMethodId($method_id) {
* The array of language objects keyed by langcode.
*/
public static function sort(&$languages) {
uasort($languages, 'Drupal\Component\Utility\SortArray::sortByWeightAndTitleKey');
uasort($languages, function ($a, $b) {
return SortArray::sortByWeightAndTitleKey($a, $b, 'weight', 'name');
});
}
}
......@@ -91,4 +91,93 @@ public function testGetNegotiationMethodId() {
$this->assertSame($method_id, $this->language->getNegotiationMethodId());
}
/**
* Tests sorting an array of Language objects.
*
* @covers ::sort()
*
* @dataProvider providerTestSortArrayOfLanguages
*
* @param \Drupal\Core\Language\Language[] $languages
* An array of \Drupal\Core\Language\Language objects.
* @param array $expected
* The expected array of keys.
*/
public function testSortArrayOfLanguages(array $languages, array $expected) {
Language::sort($languages);
$this->assertSame($expected, array_keys($languages));
}
/**
* Provides data for testSortArrayOfLanguages.
*
* @return array
* An array of test data.
*/
public function providerTestSortArrayOfLanguages() {
$language9A = new Language();
$language9A->setName('A');
$language9A->setWeight(9);
$language9A->setId('dd');
$language10A = new Language();
$language10A->setName('A');
$language10A->setWeight(10);
$language10A->setId('ee');
$language10B = new Language();
$language10B->setName('B');
$language10B->setWeight(10);
$language10B->setId('ff');
return array(
// Set up data set #0, already ordered by weight.
array(
// Set the data.
array(
$language9A->getId() => $language9A,
$language10B->getId() => $language10B,
),
// Set the expected key order.
array(
$language9A->getId(),
$language10B->getId(),
),
),
// Set up data set #1, out of order by weight.
array(
array(
$language10B->getId() => $language10B,
$language9A->getId() => $language9A,
),
array(
$language9A->getId(),
$language10B->getId(),
),
),
// Set up data set #2, tied by weight, already ordered by name.
array(
array(
$language10A->getId() => $language10A,
$language10B->getId() => $language10B,
),
array(
$language10A->getId(),
$language10B->getId(),
),
),
// Set up data set #3, tied by weight, out of order by name.
array(
array(
$language10B->getId() => $language10B,
$language10A->getId() => $language10A,
),
array(
$language10A->getId(),
$language10B->getId(),
),
),
);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment