You are here

public function FieldKernelTest::testRewriteHtmlWithTokensAndAggregation in Drupal 8

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

Tests rewriting of the output with HTML and aggregation.

File

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

Class

FieldKernelTest
Tests the generic field handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testRewriteHtmlWithTokensAndAggregation() {

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = \Drupal::service('renderer');
  $view = Views::getView('test_view');
  $view
    ->setDisplay();
  $view->displayHandlers
    ->get('default')->options['fields']['id']['group_type'] = 'sum';
  $view->displayHandlers
    ->get('default')
    ->setOption('group_by', TRUE);
  $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>');
}