function FieldKernelTest::testRewriteHtmlWithTokensAndAggregation

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

Class

FieldKernelTest
Tests the generic field handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testRewriteHtmlWithTokensAndAggregation() : void {
    
    /** @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 = (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.