LinkGenerationTest.php 2.69 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

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

namespace Drupal\system\Tests\Utility;

10
use Drupal\Component\Render\MarkupInterface;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
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);
35
    $this->assertTrue($link instanceof MarkupInterface, 'The output of link generation is marked safe as it is a link.');
36 37 38 39 40 41 42 43 44
    // 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);
45
    $this->assertTrue($link instanceof MarkupInterface, 'The output of link generation is marked safe as it is a link.');
46 47 48 49 50 51 52 53 54
    // 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);
55
    $this->assertTrue($link instanceof MarkupInterface, 'The output of link generation is marked safe as it is a link.');
56 57 58 59 60 61 62 63
    // 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);
64
    $this->assertTrue($link instanceof MarkupInterface, 'The output of link generation is marked safe as it is a link.');
65 66 67 68 69 70
    // Ensure the content of the link is escaped.
    $this->assertEscaped('<em>link with markup</em>');
    $this->assertRaw('<strong>Test!</strong>');
  }

}