You are here

public function UrlTest::testLinkRenderArrayText in Drupal 10

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()

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

File

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

Class

UrlTest
Confirm that \Drupal\Core\Url, \Drupal\Component\Utility\UrlHelper::filterQueryParameters(), \Drupal\Component\Utility\UrlHelper::buildQuery(), and \Drupal\Core\Utility\LinkGeneratorInterface::generate() work correctly with various input.

Namespace

Drupal\Tests\system\Kernel\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
      ->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);
}