You are here

LinkGenerationTest.php in Zircon Profile 8

Same filename and directory in other branches
  1. 8.0 core/modules/system/src/Tests/Utility/LinkGenerationTest.php

File

core/modules/system/src/Tests/Utility/LinkGenerationTest.php
View source
<?php

/**
 * @file
 * Contains \Drupal\system\Tests\Utility\LinkGenerationTest.
 */
namespace Drupal\system\Tests\Utility;

use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Url;
use Drupal\simpletest\KernelTestBase;

/**
 * Tests link generation with hooks.
 *
 * @group Utility
 */
class LinkGenerationTest extends KernelTestBase {
  public static $modules = [
    'link_generation_test',
  ];

  /**
   * Tests how hook_link_alter() can affect escaping of the link text.
   */
  function testHookLinkAlter() {
    $url = Url::fromUri('http://example.com');
    $renderer = \Drupal::service('renderer');
    $link = $renderer
      ->executeInRenderContext(new RenderContext(), function () use ($url) {
      return \Drupal::l([
        '#markup' => '<em>link with markup</em>',
      ], $url);
    });
    $this
      ->setRawContent($link);
    $this
      ->assertTrue(SafeMarkup::isSafe($link), 'The output of link generation is marked safe as it is a link.');

    // Ensure the content of the link is not escaped.
    $this
      ->assertRaw('<em>link with markup</em>');

    // Test just adding text to an already safe string.
    \Drupal::state()
      ->set('link_generation_test_link_alter', TRUE);
    $link = $renderer
      ->executeInRenderContext(new RenderContext(), function () use ($url) {
      return \Drupal::l([
        '#markup' => '<em>link with markup</em>',
      ], $url);
    });
    $this
      ->setRawContent($link);
    $this
      ->assertTrue(SafeMarkup::isSafe($link), 'The output of link generation is marked safe as it is a link.');

    // Ensure the content of the link is escaped.
    $this
      ->assertEscaped('<em>link with markup</em> <strong>Test!</strong>');

    // Test passing a safe string to t().
    \Drupal::state()
      ->set('link_generation_test_link_alter_safe', TRUE);
    $link = $renderer
      ->executeInRenderContext(new RenderContext(), function () use ($url) {
      return \Drupal::l([
        '#markup' => '<em>link with markup</em>',
      ], $url);
    });
    $this
      ->setRawContent($link);
    $this
      ->assertTrue(SafeMarkup::isSafe($link), 'The output of link generation is marked safe as it is a link.');

    // Ensure the content of the link is escaped.
    $this
      ->assertRaw('<em>link with markup</em> <strong>Test!</strong>');

    // Test passing an unsafe string to t().
    $link = $renderer
      ->executeInRenderContext(new RenderContext(), function () use ($url) {
      return \Drupal::l('<em>link with markup</em>', $url);
    });
    $this
      ->setRawContent($link);
    $this
      ->assertTrue(SafeMarkup::isSafe($link), 'The output of link generation is marked safe as it is a link.');

    // Ensure the content of the link is escaped.
    $this
      ->assertEscaped('<em>link with markup</em>');
    $this
      ->assertRaw('<strong>Test!</strong>');
  }

}

Classes

Namesort descending Description
LinkGenerationTest Tests link generation with hooks.