You are here

public function UrlTest::testLinkRenderArrayText in Drupal 8

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 \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\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);
}