function UrlTest::testLinkRenderArrayText

Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Kernel/Common/UrlTest.php \Drupal\Tests\system\Kernel\Common\UrlTest::testLinkRenderArrayText()
  2. 10 core/modules/system/tests/src/Kernel/Common/UrlTest.php \Drupal\Tests\system\Kernel\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/Functional/Common/UrlTest.php, line 171

Class

UrlTest
Confirm that <a href="/api/drupal/core%21lib%21Drupal%21Core%21Url.php/class/Url/8.9.x" title="Defines an object that holds information about a URL." class="local">\Drupal\Core\Url</a>, <a href="/api/drupal/core%21lib%21Drupal%21Component%21Utility%21UrlHelper.php/function/UrlHelper%3A%3AfilterQueryParameters/8.9.x" title="Filters a URL query parameter array to remove unwanted elements." class="local">\Drupal\Component\Utility\UrlHelper::filterQueryParameters</a>(), <a href="/api/drupal/core%21lib%21Drupal%21Component%21Utility%21UrlHelper.php/function/UrlHelper%3A%3AbuildQuery/8.9.x" title="Parses an array into a valid, rawurlencoded query string." class="local">\Drupal\Component\Utility\UrlHelper::buildQuery</a>(), and <a href="/api/drupal/core%21lib%21Drupal%21Core%21Utility%21LinkGeneratorInterface.php/function/LinkGeneratorInterface%3A%3Agenerate/8.9.x" title="Renders a link to a URL." class="local">\Drupal\Core\Utility\LinkGeneratorInterface::generate</a>() work correctly with various input.

Namespace

Drupal\Tests\system\Functional\Common

Code

public function testLinkRenderArrayText() {
    
    /** @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 ($renderer, $l) {
        $renderable_text = [
            '#markup' => 'foo',
        ];
        $l_renderable_text = \Drupal::service('link_generator')->generate($renderable_text, Url::fromUri('https://www.drupal.org'));
        $this->assertEqual($l_renderable_text, $l);
    });
    // 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->assertEqual($type_link_plain, $l);
    // 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->assertEqual($type_link_nested, $l);
}

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