You are here

public function RendererTest::testRenderSorting in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Render/RendererTest.php \Drupal\Tests\Core\Render\RendererTest::testRenderSorting()
  2. 10 core/tests/Drupal/Tests/Core/Render/RendererTest.php \Drupal\Tests\Core\Render\RendererTest::testRenderSorting()

@covers ::render @covers ::doRender

File

core/tests/Drupal/Tests/Core/Render/RendererTest.php, line 473
Contains \Drupal\Tests\Core\Render\RendererTest.

Class

RendererTest
@coversDefaultClass \Drupal\Core\Render\Renderer @group Render

Namespace

Drupal\Tests\Core\Render

Code

public function testRenderSorting() {
  $first = $this
    ->randomMachineName();
  $second = $this
    ->randomMachineName();

  // Build an array with '#weight' set for each element.
  $elements = [
    'second' => [
      '#weight' => 10,
      '#markup' => $second,
    ],
    'first' => [
      '#weight' => 0,
      '#markup' => $first,
    ],
  ];
  $output = $this->renderer
    ->renderRoot($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 \Drupal\Core\Render\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.');
}