diff --git a/core/includes/common.inc b/core/includes/common.inc index 03eaa39..881344c 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -2,6 +2,7 @@ use Drupal\Component\Utility\Crypt; use Drupal\Component\Utility\Json; +use Drupal\Component\Utility\SortArray; use Drupal\Component\Utility\String; use Drupal\Component\Utility\Tags; use Drupal\Component\Utility\Url; @@ -4212,14 +4213,11 @@ function drupal_render_cid_create($elements) { * that optionally include a '#weight' key. * @param $b * Second item for comparison. + * + * @see \Drupal\Component\Utility\SortArray::sortByWeightProperty() */ function element_sort($a, $b) { - $a_weight = (is_array($a) && isset($a['#weight'])) ? $a['#weight'] : 0; - $b_weight = (is_array($b) && isset($b['#weight'])) ? $b['#weight'] : 0; - if ($a_weight == $b_weight) { - return 0; - } - return ($a_weight < $b_weight) ? -1 : 1; + return SortArray::sortByWeightProperty($a, $b); } /** @@ -4234,11 +4232,11 @@ function element_sort($a, $b) { * that optionally include a '#title' key. * @param $b * Second item for comparison. + * + * @see \Drupal\Component\Utility\SortArray::sortByTitleProperty() */ function element_sort_by_title($a, $b) { - $a_title = (is_array($a) && isset($a['#title'])) ? $a['#title'] : ''; - $b_title = (is_array($b) && isset($b['#title'])) ? $b['#title'] : ''; - return strnatcasecmp($a_title, $b_title); + return SortArray::sortByTitleProperty($a, $b); } /** @@ -4296,14 +4294,12 @@ function element_info_property($type, $property_name, $default = NULL) { * element, a default value of 0 will be used. * @param $b * Second item for comparison. + * + * @see \Drupal\Component\Utility\SortArray::sortByWeightElement() + * */ -function drupal_sort_weight($a, $b) { - $a_weight = (is_array($a) && isset($a['weight'])) ? $a['weight'] : 0; - $b_weight = (is_array($b) && isset($b['weight'])) ? $b['weight'] : 0; - if ($a_weight == $b_weight) { - return 0; - } - return ($a_weight < $b_weight) ? -1 : 1; +function drupal_sort_weight(array $a, array $b) { + return SortArray::sortByWeightElement($a, $b); } /** @@ -4316,15 +4312,11 @@ function drupal_sort_weight($a, $b) { * that optionally include a 'title' key. * @param $b * Second item for comparison. + * + * @see \Drupal\Component\Utility\SortArray::sortByTitleElement() */ function drupal_sort_title($a, $b) { - if (!isset($b['title'])) { - return -1; - } - if (!isset($a['title'])) { - return 1; - } - return strcasecmp($a['title'], $b['title']); + return SortArray::sortByTitleElement($a, $b); } /** diff --git a/core/lib/Drupal/Component/Utility/SortArray.php b/core/lib/Drupal/Component/Utility/SortArray.php new file mode 100644 index 0000000..fbe545b --- /dev/null +++ b/core/lib/Drupal/Component/Utility/SortArray.php @@ -0,0 +1,111 @@ + 'SortArray test', + 'description' => 'Test that the SortArray functions work properly.', + 'group' => 'Common', + ); + } + + /** + * Tests SortArray::sortByWeightElement() input against expected output. + * + * @dataProvider providerSortByWeightElement + * + * @param array $a + * The first input array for the SortArray::sortByWeightElement() method. + * @param array $b + * The second input array for the SortArray::sortByWeightElement(). + * @param integer $expected + * The expected output from calling the method. + * + * @see Drupal\Component\Utility\SortArray::sortByWeightElement() + * @see Drupal\Tests\Component\Utility\SortArrayTest::providersortByWeightElement() + */ + public function testSortByWeightElement($a, $b, $expected) { + $result = SortArray::sortByWeightElement($a, $b); + $this->assertEquals($expected, $result); + } + + /** + * Data provider for SortArray::sortByWeightElement(). + * + * @return array + * An array of tests, matching the parameter inputs for + * testSortByWeightElement. + * + * @see \Drupal\Component\Utility\SortArray::sortByWeightElement() + * @see \Drupal\Tests\Component\Utility\SortArrayTest::testSortByWeightElement() + */ + public function providerSortByWeightElement() { + $tests = array(); + + // Weights set and equal + $tests[] = array( + array('weight' => 1), + array('weight' => 1), + 0 + ); + + // Weights set and $a is less (lighter) than $b + $tests[] = array( + array('weight' => 1), + array('weight' => 2), + -1 + ); + + // Weights set and $a is greater (heavier) than $b + $tests[] = array( + array('weight' => 2), + array('weight' => 1), + 1 + ); + + // Weights not set + $tests[] = array( + array(), + array(), + 0 + ); + + // Weights for $b not set + $tests[] = array( + array('weight' => 1), + array(), + 1 + ); + + // Weights for $a not set + $tests[] = array( + array(), + array('weight' => 1), + -1 + ); + + return $tests; + } + + /** + * Tests SortArray::sortByWeightProperty() input against expected output. + * + * @dataProvider providerSortByWeightProperty + * + * @param array $a + * The first input array for the SortArray::sortByWeightProperty() method. + * @param array $b + * The second input array for the SortArray::sortByWeightProperty(). + * @param integer $expected + * The expected output from calling the method. + * + * @see Drupal\Component\Utility\SortArray::sortByWeightProperty() + * @see Drupal\Tests\Component\Utility\SortArrayTest::SortByWeightProperty() + */ + public function testSortByWeightProperty($a, $b, $expected) { + $result = SortArray::sortByWeightProperty($a, $b); + $this->assertEquals($expected, $result); + } + + /** + * Data provider for SortArray::sortByWeightProperty(). + * + * @return array + * An array of tests, matching the parameter inputs for + * testSortByWeightProperty. + * + * @see \Drupal\Component\Utility\SortArray::sortByWeightProperty() + * @see \Drupal\Tests\Component\Utility\SortArrayTest::testSortByWeightProperty() + */ + public function providerSortByWeightProperty() { + $tests = array(); + + // Weights set and equal + $tests[] = array( + array('#weight' => 1), + array('#weight' => 1), + 0 + ); + + // Weights set and $a is less (lighter) than $b + $tests[] = array( + array('#weight' => 1), + array('#weight' => 2), + -1 + ); + + // Weights set and $a is greater (heavier) than $b + $tests[] = array( + array('#weight' => 2), + array('#weight' => 1), + 1 + ); + + // Weights not set + $tests[] = array( + array(), + array(), + 0 + ); + + // Weights for $b not set + $tests[] = array( + array('#weight' => 1), + array(), + 1 + ); + + // Weights for $a not set + $tests[] = array( + array(), + array('#weight' => 1), + -1 + ); + + return $tests; + } + + /** + * Tests SortArray::sortByTitleElement() input against expected output. + * + * @dataProvider SortByTitleElement + * + * @param array $a + * The first input item for comparison. + * @param array $b + * The second item for comparison. + * @param integer $expected + * The expected output from calling the method. + * + * @see Drupal\Component\Utility\SortArray::sortByTitleElement() + * @see Drupal\Tests\Component\Utility\SortArrayTest::SortByTitleElement() + */ + public function testSortByTitleElement($a, $b, $expected) { + $result = SortArray::sortByTitleElement($a, $b); + $this->assertEquals($expected, $result); + } + + /** + * Data provider for SortArray::sortByTitleElement(). + * + * @return array + * An array of tests, matching the parameter inputs for + * testSortByTitleElement. + * + * @see \Drupal\Component\Utility\SortArray::sortByTitleElement() + * @see \Drupal\Tests\Component\Utility\SortArrayTest::testSortByTitleElement() + */ + public function SortByTitleElement() { + $tests = array(); + + // Titles set and equal + $tests[] = array( + array('title' => 'test'), + array('title' => 'test'), + 0 + ); + + // Title $a not set + $tests[] = array( + array(), + array('title' => 'test'), + 1 + ); + + // Title $b not set + $tests[] = array( + array('title' => 'test'), + array(), + -1 + ); + + // Titles set but not equal + $tests[] = array( + array('title' => 'test'), + array('title' => 'testing'), + -3 + ); + + // Titles set but not equal + $tests[] = array( + array('title' => 'testing'), + array('title' => 'test'), + 3 + ); + + return $tests; + } + + /** + * Tests SortArray::sortByTitleProperty() input against expected output. + * + * @dataProvider SortByTitleProperty + * + * @param array $a + * The first input item for comparison. + * @param array $b + * The second item for comparison. + * @param integer $expected + * The expected output from calling the method. + * + * @see Drupal\Component\Utility\SortArray::sortByTitleProperty() + * @see Drupal\Tests\Component\Utility\SortArrayTest::SortByTitleProperty() + */ + public function testSortByTitleProperty($a, $b, $expected) { + $result = SortArray::sortByTitleProperty($a, $b); + $this->assertEquals($expected, $result); + } + + /** + * Data provider for SortArray::sortByTitleProperty(). + * + * @return array + * An array of tests, matching the parameter inputs for + * testSortByTitleProperty. + * + * @see \Drupal\Component\Utility\SortArray::sortByTitleProperty() + * @see \Drupal\Tests\Component\Utility\SortArrayTest::testSortByTitleProperty() + */ + public function SortByTitleProperty() { + $tests = array(); + + // Titles set and equal + $tests[] = array( + array('#title' => 'test'), + array('#title' => 'test'), + 0 + ); + + // Title $a not set + $tests[] = array( + array(), + array('#title' => 'test'), + -4 + ); + + // Title $b not set + $tests[] = array( + array('#title' => 'test'), + array(), + 4 + ); + + // Titles set but not equal + $tests[] = array( + array('#title' => 'test'), + array('#title' => 'testing'), + -1 + ); + + // Titles set but not equal + $tests[] = array( + array('#title' => 'testing'), + array('#title' => 'test'), + 1 + ); + + return $tests; + } + +}