You are here

function DrupalRenderUnitTestCase::testDrupalRenderSorting in SimpleTest 7

Test sorting by weight.

File

tests/common.test, line 1227
Tests for common.inc functionality.

Class

DrupalRenderUnitTestCase
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), 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.'));
}