function RendererBubblingTest::testBubblingWithoutPreRender

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php \Drupal\Tests\Core\Render\RendererBubblingTest::testBubblingWithoutPreRender()
  2. 10 core/tests/Drupal/Tests/Core/Render/RendererBubblingTest.php \Drupal\Tests\Core\Render\RendererBubblingTest::testBubblingWithoutPreRender()
  3. 11.x 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

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',
            ],
        ],
    ];
    $this->assertTrue(strlen($this->renderer
        ->renderRoot($element)) > 0, 'The element was retrieved from cache.');
    $this->assertEquals($element['#attached']['library'], $expected_libraries, 'The element, child and subchild #attached libraries are included.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.