function FunctionsTest::testDrupalPreRenderLinks

Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php \Drupal\Tests\system\Kernel\Theme\FunctionsTest::testDrupalPreRenderLinks()
  2. 8.9.x core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php \Drupal\Tests\system\Kernel\Theme\FunctionsTest::testDrupalPreRenderLinks()
  3. 10 core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php \Drupal\Tests\system\Kernel\Theme\FunctionsTest::testDrupalPreRenderLinks()

Tests the use of Link::preRenderLinks() on a nested array of links.

See also

\Drupal\Core\Render\Element\Link::preRenderLinks()

File

core/modules/system/tests/src/Kernel/Theme/FunctionsTest.php, line 427

Class

FunctionsTest
Tests for common theme functions.

Namespace

Drupal\Tests\system\Kernel\Theme

Code

public function testDrupalPreRenderLinks() : void {
    // Define the base array to be rendered, containing a variety of different
    // kinds of links.
    $base_array = [
        '#theme' => 'links',
        '#pre_render' => [
            [
                Link::class,
                'preRenderLinks',
            ],
        ],
        '#links' => [
            'parent_link' => [
                'title' => 'Parent link original',
                'url' => Url::fromRoute('router_test.1'),
            ],
        ],
        'first_child' => [
            '#theme' => 'links',
            '#links' => [
                // This should be rendered if 'first_child' is rendered separately,
                // but ignored if the parent is being rendered (since it duplicates
                // one of the parent's links).
'parent_link' => [
                    'title' => 'Parent link copy',
                    'url' => Url::fromRoute('router_test.6'),
                ],
                // This should always be rendered.
'first_child_link' => [
                    'title' => 'First child link',
                    'url' => Url::fromRoute('router_test.7'),
                ],
            ],
        ],
        // This should always be rendered as part of the parent.
'second_child' => [
            '#theme' => 'links',
            '#links' => [
                'second_child_link' => [
                    'title' => 'Second child link',
                    'url' => Url::fromRoute('router_test.8'),
                ],
            ],
        ],
        // This should never be rendered, since the user does not have access to
        // it.
'third_child' => [
            '#theme' => 'links',
            '#links' => [
                'third_child_link' => [
                    'title' => 'Third child link',
                    'url' => Url::fromRoute('router_test.9'),
                ],
            ],
            '#access' => FALSE,
        ],
    ];
    // Start with a fresh copy of the base array, and try rendering the entire
    // thing. We expect a single <ul> with appropriate links contained within
    // it.
    $render_array = $base_array;
    $html = (string) \Drupal::service('renderer')->renderRoot($render_array);
    $dom = Html::load($html);
    $this->assertEquals(1, $dom->getElementsByTagName('ul')->length, 'One "ul" tag found in the rendered HTML.');
    $list_elements = $dom->getElementsByTagName('li');
    $this->assertEquals(3, $list_elements->length, 'Three "li" tags found in the rendered HTML.');
    $this->assertEquals('Parent link original', $list_elements->item(0)->nodeValue, 'First expected link found.');
    $this->assertEquals('First child link', $list_elements->item(1)->nodeValue, 'Second expected link found.');
    $this->assertEquals('Second child link', $list_elements->item(2)->nodeValue, 'Third expected link found.');
    $this->assertStringNotContainsString('Parent link copy', $html, '"Parent link copy" link not found.');
    $this->assertStringNotContainsString('Third child link', $html, '"Third child link" link not found.');
    // Now render 'first_child', followed by the rest of the links, and make
    // sure we get two separate <ul>'s with the appropriate links contained
    // within each.
    $render_array = $base_array;
    $child_html = (string) \Drupal::service('renderer')->renderRoot($render_array['first_child']);
    $parent_html = (string) \Drupal::service('renderer')->renderRoot($render_array);
    // First check the child HTML.
    $dom = Html::load($child_html);
    $this->assertEquals(1, $dom->getElementsByTagName('ul')->length, 'One "ul" tag found in the rendered child HTML.');
    $list_elements = $dom->getElementsByTagName('li');
    $this->assertEquals(2, $list_elements->length, 'Two "li" tags found in the rendered child HTML.');
    $this->assertEquals('Parent link copy', $list_elements->item(0)->nodeValue, 'First expected link found.');
    $this->assertEquals('First child link', $list_elements->item(1)->nodeValue, 'Second expected link found.');
    // Then check the parent HTML.
    $dom = Html::load($parent_html);
    $this->assertEquals(1, $dom->getElementsByTagName('ul')->length, 'One "ul" tag found in the rendered parent HTML.');
    $list_elements = $dom->getElementsByTagName('li');
    $this->assertEquals(2, $list_elements->length, 'Two "li" tags found in the rendered parent HTML.');
    $this->assertEquals('Parent link original', $list_elements->item(0)->nodeValue, 'First expected link found.');
    $this->assertEquals('Second child link', $list_elements->item(1)->nodeValue, 'Second expected link found.');
    $this->assertStringNotContainsString('First child link', $parent_html, '"First child link" link not found.');
    $this->assertStringNotContainsString('Third child link', $parent_html, '"Third child link" link not found.');
}

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