function DrupalRenderTestCase::testDrupalRenderSorting
Test sorting by weight.
File
-
modules/
simpletest/ tests/ common.test, line 2141
Class
- DrupalRenderTestCase
- Tests for drupal_render().
Code
function testDrupalRenderSorting() {
$first = $this->randomName();
$second = $this->randomName();
// Build an array with '#weight' set for each element.
$elements = array(
'second' => array(
'#weight' => 10,
'#markup' => $second,
),
'first' => array(
'#weight' => 0,
'#markup' => $first,
),
);
$output = drupal_render($elements);
// The lowest weight element should appear last in $output.
$this->assertTrue(strpos($output, $second) > strpos($output, $first), 'Elements were sorted correctly by weight.');
// Confirm that the $elements array has '#sorted' set to TRUE.
$this->assertTrue($elements['#sorted'], "'#sorted' => TRUE was added to the array");
// Pass $elements through element_children() and ensure it remains
// sorted in the correct order. drupal_render() will return an empty string
// if used on the same array in the same request.
$children = element_children($elements);
$this->assertTrue(array_shift($children) == 'first', 'Child found in the correct order.');
$this->assertTrue(array_shift($children) == 'second', 'Child found in the correct order.');
// The same array structure again, but with #sorted set to TRUE.
$elements = array(
'second' => array(
'#weight' => 10,
'#markup' => $second,
),
'first' => array(
'#weight' => 0,
'#markup' => $first,
),
'#sorted' => TRUE,
);
$output = drupal_render($elements);
// The elements should appear in output in the same order as the array.
$this->assertTrue(strpos($output, $second) < strpos($output, $first), 'Elements were not sorted.');
// The order of children with same weight should be preserved.
$element_mixed_weight = array(
'child5' => array(
'#weight' => 10,
),
'child3' => array(
'#weight' => -10,
),
'child1' => array(),
'child4' => array(
'#weight' => 10,
),
'child2' => array(),
'child6' => array(
'#weight' => 10,
),
'child9' => array(),
'child8' => array(
'#weight' => 10,
),
'child7' => array(),
);
$expected = array(
'child3',
'child1',
'child2',
'child9',
'child7',
'child5',
'child4',
'child6',
'child8',
);
$this->assertEqual($expected, element_children($element_mixed_weight, TRUE), 'Order of elements with the same weight is preserved.');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.