You are here

public function RendererTest::testRenderSorting in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Render/RendererTest.php \Drupal\Tests\Core\Render\RendererTest::testRenderSorting()
  2. 9 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 487
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
    ->assertGreaterThan(strpos($output, $first), strpos($output, $second));

  // 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::service('renderer')->render() will return an empty string if
  // used on the same array in the same request.
  $children = Element::children($elements);
  $this
    ->assertSame('first', array_shift($children), 'Child found in the correct order.');
  $this
    ->assertSame('second', array_shift($children), 'Child found in the correct order.');
}