You are here

public function RendererBubblingTest::testBubblingWithoutPreRender in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php \Drupal\Tests\Core\Render\RendererBubblingTest::testBubblingWithoutPreRender()

Tests bubbling of assets when NOT using #pre_render callbacks.

File

core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php, line 36
Contains \Drupal\Tests\Core\Render\RendererBubblingTest.

Class

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

Namespace

Drupal\Tests\Core\Render

Code

public function testBubblingWithoutPreRender() {
  $this
    ->setUpRequest();
  $this
    ->setupMemoryCache();
  $this->cacheContextsManager
    ->expects($this
    ->any())
    ->method('convertTokensToKeys')
    ->willReturnArgument(0);

  // Create an element with a child and subchild. Each element loads a
  // different library using #attached.
  $element = [
    '#type' => 'container',
    '#cache' => [
      'keys' => [
        'simpletest',
        'renderer',
        'children_attached',
      ],
    ],
    '#attached' => [
      'library' => [
        'test/parent',
      ],
    ],
    '#title' => 'Parent',
  ];
  $element['child'] = [
    '#type' => 'container',
    '#attached' => [
      'library' => [
        'test/child',
      ],
    ],
    '#title' => 'Child',
  ];
  $element['child']['subchild'] = [
    '#attached' => [
      'library' => [
        'test/subchild',
      ],
    ],
    '#markup' => 'Subchild',
  ];

  // Render the element and verify the presence of #attached JavaScript.
  $this->renderer
    ->renderRoot($element);
  $expected_libraries = [
    'test/parent',
    'test/child',
    'test/subchild',
  ];
  $this
    ->assertEquals($element['#attached']['library'], $expected_libraries, 'The element, child and subchild #attached libraries are included.');

  // Load the element from cache and verify the presence of the #attached
  // JavaScript.
  $element = [
    '#cache' => [
      'keys' => [
        'simpletest',
        'renderer',
        'children_attached',
      ],
    ],
  ];

  // Verify that the element was retrieved from the cache.
  $this
    ->assertNotEmpty($this->renderer
    ->renderRoot($element));
  $this
    ->assertEquals($element['#attached']['library'], $expected_libraries, 'The element, child and subchild #attached libraries are included.');
}