diff --git a/core/includes/common.inc b/core/includes/common.inc index 61b63be..570fc2b 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -1,6 +1,7 @@ 'SortArray test', + 'description' => 'Test that the SortArray functions work properly.', + 'group' => 'Common', + ); + } + + /** + * Tests SortArray::sortWeight() input against expected output. + * + * @dataProvider providerSortArray + * + * @param array $a + * The first input array for the SortArray::sortWeight() method. + * @param array $b + * The second input array for the SortArray::sortWeight(). + * @param integer $expected + * The expected output from calling the method. + * + * @see Drupal\Component\Utility\SortArray::sortWeight() + * @see Drupal\Tests\Component\Utility\SortArrayTest::providerSortArray() + */ + public function testSortArray($a, $b, $expected) { + $result = SortArray::sortWeight($a, $b); + $this->assertEquals($expected, $result); + } + + /** + * Data provider for SortArray::sortWeight(). + * + * @return array + * An array of tests, matching the parameter inputs for testSortArray. + * + * @see Drupal\Component\Utility\SortArray::sortWeight() + * @see Drupal\Tests\Component\Utility\SortArrayTest::testSortArray() + */ + public function providerSortArray() { + $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; + } + +}