DrupalRenderTestCase::testDrupalRenderSorting

7 common.test DrupalRenderTestCase::testDrupalRenderSorting()

Test sorting by weight.

File

modules/simpletest/tests/common.test, line 1570
Tests for common.inc functionality.

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), t('Elements were sorted correctly by weight.'));

  // Confirm that the $elements array has '#sorted' set to TRUE.
  $this->assertTrue($elements['#sorted'], t("'#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', t('Child found in the correct order.'));
  $this->assertTrue(array_shift($children) == 'second', t('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), t('Elements were not sorted.'));
}
Login or register to post comments