diff --git a/core/lib/Drupal/Core/Render/Element.php b/core/lib/Drupal/Core/Render/Element.php index ef5c696..c3d2952 100644 --- a/core/lib/Drupal/Core/Render/Element.php +++ b/core/lib/Drupal/Core/Render/Element.php @@ -89,7 +89,7 @@ public static function children(array &$elements, $sort = FALSE) { } // Sort the children if necessary. if ($sort && $sortable) { - uasort($children, 'element_sort'); + uasort($children, 'Drupal\Component\Utility\SortArray::sortByWeightProperty'); // Put the sorted children back into $elements in the correct order, to // preserve sorting if the same element is passed through // element_children() twice. diff --git a/core/tests/Drupal/Tests/Core/Render/ElementTest.php b/core/tests/Drupal/Tests/Core/Render/ElementTest.php index 6148589..2f55339 100644 --- a/core/tests/Drupal/Tests/Core/Render/ElementTest.php +++ b/core/tests/Drupal/Tests/Core/Render/ElementTest.php @@ -5,7 +5,7 @@ * Contains \Drupal\Tests\Core\Render\ElementTest. */ -namespace Drupal\Tests\Core\Render; +namespace Drupal\Tests\Core\Render { use Drupal\Tests\UnitTestCase; use Drupal\Core\Render\Element; @@ -67,7 +67,103 @@ public function testChild() { * Tests the children() method. */ public function testChildren() { + $element = array( + 'child2' => array('#weight' => 10), + 'child1' => array('#weight' => 0), + 'child3' => array('#weight' => 20), + '#property' => 'property', + ); + + $expected = array('child2', 'child1', 'child3'); + $element_copy = $element; + $this->assertSame($expected, Element::children($element_copy)); + + // If #sorted is already set, no sorting should happen. + $element_copy = $element; + $element_copy['#sorted'] = TRUE; + $expected = array('child2', 'child1', 'child3'); + $this->assertSame($expected, Element::children($element_copy, TRUE)); + + // Test with weight sorting, #sorted property should be added. + $expected = array('child1', 'child2', 'child3'); + $element_copy = $element; + $this->assertSame($expected, Element::children($element_copy, TRUE)); + $this->assertArrayHasKey('#sorted', $element_copy); + $this->assertTrue($element_copy['#sorted']); + + // The order should stay the same if no weights present. + $element_no_weight = array( + 'child2' => array(), + 'child1' => array(), + 'child3' => array(), + '#property' => 'property', + ); + + $expected = array('child2', 'child1', 'child3'); + $this->assertSame($expected, Element::children($element_no_weight, TRUE)); + } + + /** + * Tests the visibleChildren() method. + * + * @param array $element + * The test element array. + * @param array $expected_keys + * The expected keys to be returned from Element::getVisibleChildren(). + * + * @dataProvider providerVisibleChildren + */ + public function testVisibleChildren(array $element, array $expected_keys) { + $this->assertSame($expected_keys, Element::getVisibleChildren($element)); + } + + /** + * Data provider for testVisibleChildren. + * + * @return array + */ + public function providerVisibleChildren() { + return array( + array(array('#property1' => '', '#property2' => array()), array()), + array(array('#property1' => '', 'child1' => array()), array('child1')), + array(array('#property1' => '', 'child1' => array(), 'child2' => array('#access' => TRUE)), array('child1', 'child2')), + array(array('#property1' => '', 'child1' => array(), 'child2' => array('#access' => FALSE)), array('child1')), + array(array('#property1' => '', 'child1' => array(), 'child2' => array('#type' => 'textfield')), array('child1', 'child2')), + array(array('#property1' => '', 'child1' => array(), 'child2' => array('#type' => 'value')), array('child1')), + array(array('#property1' => '', 'child1' => array(), 'child2' => array('#type' => 'hidden')), array('child1')), + ); + } + + /** + * Tests the setAttributes() method. + * + * @dataProvider providerTestSetAttributes + */ + public function testSetAttributes($element, $map, $expected_element) { + Element::setAttributes($element, $map); + $this->assertSame($expected_element, $element); + } + /** + * Data provider for testSetAttributes(). + */ + public function providerTestSetAttributes() { + $base = array('#id' => 'id', '#class' => array()); + return array( + array($base, array(), $base), + array($base, array('id', 'class'), $base + array('#attributes' => array('id' => 'id', 'class' => array()))), + array($base + array('#attributes' => array('id' => 'id-not-overwritten')), array('id', 'class'), $base + array('#attributes' => array('id' => 'id-not-overwritten', 'class' => array()))), + ); } } + +} + +namespace { + if (!function_exists('t')) { + function t($string) { + return $string; + } + } +}