function FieldKernelTest::testRewriteHtmlWithTokens

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php \Drupal\Tests\views\Kernel\Handler\FieldKernelTest::testRewriteHtmlWithTokens()
  2. 8.9.x core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php \Drupal\Tests\views\Kernel\Handler\FieldKernelTest::testRewriteHtmlWithTokens()
  3. 11.x 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 166

Class

FieldKernelTest
Tests the generic field handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testRewriteHtmlWithTokens() : void {
  /** @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 = (string) $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 = (string) $renderer->executeInRenderContext(new RenderContext(), function () use ($id_field, $row) {
    return $id_field->theme($row);
  });
  $this->assertSubString($output, '<p>1 alert("Script removed")</p>');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.