You are here

public function FieldKernelTest::testRewriteHtmlWithTokens in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php \Drupal\Tests\views\Kernel\Handler\FieldKernelTest::testRewriteHtmlWithTokens()

Tests rewriting of the output with HTML.

File

core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php, line 169

Class

FieldKernelTest
Tests the generic field handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testRewriteHtmlWithTokens() {

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = \Drupal::service('renderer');
  $view = Views::getView('test_view');
  $view
    ->initHandlers();
  $this
    ->executeView($view);
  $row = $view->result[0];
  $id_field = $view->field['id'];
  $id_field->options['alter']['text'] = '<p>{{ id }}</p>';
  $id_field->options['alter']['alter_text'] = TRUE;
  $output = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($id_field, $row) {
    return $id_field
      ->theme($row);
  });
  $this
    ->assertSubString($output, '<p>1</p>');

  // Add a non-safe HTML tag and make sure this gets removed.
  $id_field->options['alter']['text'] = '<p>{{ id }} <script>alert("Script removed")</script></p>';
  $id_field->options['alter']['alter_text'] = TRUE;
  $output = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($id_field, $row) {
    return $id_field
      ->theme($row);
  });
  $this
    ->assertSubString($output, '<p>1 alert("Script removed")</p>');
}