function UrlTest::testLinkRenderArrayText

Same name in other branches
  1. 9 core/modules/system/tests/src/Kernel/Common/UrlTest.php \Drupal\Tests\system\Kernel\Common\UrlTest::testLinkRenderArrayText()
  2. 8.9.x core/modules/system/tests/src/Functional/Common/UrlTest.php \Drupal\Tests\system\Functional\Common\UrlTest::testLinkRenderArrayText()
  3. 11.x core/modules/system/tests/src/Kernel/Common/UrlTest.php \Drupal\Tests\system\Kernel\Common\UrlTest::testLinkRenderArrayText()

Tests that link functions support render arrays as 'text'.

File

core/modules/system/tests/src/Kernel/Common/UrlTest.php, line 135

Class

UrlTest
Tests the Url object.

Namespace

Drupal\Tests\system\Kernel\Common

Code

public function testLinkRenderArrayText() : void {
    
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container
        ->get('renderer');
    // Build a link with the link generator for reference.
    $l = Link::fromTextAndUrl('foo', Url::fromUri('https://www.drupal.org'))->toString();
    // Test a renderable array passed to the link generator.
    $renderer->executeInRenderContext(new RenderContext(), function () use ($l) {
        $renderable_text = [
            '#markup' => 'foo',
        ];
        $l_renderable_text = \Drupal::service('link_generator')->generate($renderable_text, Url::fromUri('https://www.drupal.org'));
        $this->assertEquals($l, $l_renderable_text);
    });
    // Test a themed link with plain text 'text'.
    $type_link_plain_array = [
        '#type' => 'link',
        '#title' => 'foo',
        '#url' => Url::fromUri('https://www.drupal.org'),
    ];
    $type_link_plain = $renderer->renderRoot($type_link_plain_array);
    $this->assertEquals($l, $type_link_plain);
    // Build a themed link with renderable 'text'.
    $type_link_nested_array = [
        '#type' => 'link',
        '#title' => [
            '#markup' => 'foo',
        ],
        '#url' => Url::fromUri('https://www.drupal.org'),
    ];
    $type_link_nested = $renderer->renderRoot($type_link_nested_array);
    $this->assertEquals($l, $type_link_nested);
}

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