function FieldKernelTest::testArgumentTokens

Same name in other branches
  1. 9 core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php \Drupal\Tests\views\Kernel\Handler\FieldKernelTest::testArgumentTokens()
  2. 8.9.x core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php \Drupal\Tests\views\Kernel\Handler\FieldKernelTest::testArgumentTokens()
  3. 10 core/modules/views/tests/src/Kernel/Handler/FieldKernelTest.php \Drupal\Tests\views\Kernel\Handler\FieldKernelTest::testArgumentTokens()

Tests the arguments tokens on field level.

File

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

Class

FieldKernelTest
Tests the generic field handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testArgumentTokens() : void {
    
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');
    $view = Views::getView('test_field_argument_tokens');
    $this->executeView($view, [
        '{{ { "#pre_render": ["\\Drupal\\views_test_data\\Controller\\ViewsTestDataController::preRender"]} }}',
    ]);
    $name_field_0 = $view->field['name'];
    // Test the old style tokens.
    $name_field_0->options['alter']['alter_text'] = TRUE;
    $name_field_0->options['alter']['text'] = '%1 !1';
    $row = $view->result[0];
    $output = (string) $renderer->executeInRenderContext(new RenderContext(), function () use ($name_field_0, $row) {
        return $name_field_0->advancedRender($row);
    });
    $this->assertStringNotContainsString('\\Drupal\\views_test_data\\Controller\\ViewsTestDataController::preRender executed', (string) $output, 'Ensure that the pre_render function was not executed');
    $this->assertEquals('%1 !1', (string) $output, "Ensure that old style placeholders aren't replaced");
    // This time use new style tokens but ensure that we still don't allow
    // arbitrary code execution.
    $name_field_0->options['alter']['alter_text'] = TRUE;
    $name_field_0->options['alter']['text'] = '{{ arguments.null }} {{ raw_arguments.null }}';
    $row = $view->result[0];
    $output = (string) $renderer->executeInRenderContext(new RenderContext(), function () use ($name_field_0, $row) {
        return $name_field_0->advancedRender($row);
    });
    $this->assertStringNotContainsString('\\Drupal\\views_test_data\\Controller\\ViewsTestDataController::preRender executed', (string) $output, 'Ensure that the pre_render function was not executed');
    $this->assertEquals('{{ { "#pre_render": ["\\Drupal\\views_test_data\\Controller\\ViewsTestDataController::preRender"]} }} {{ { "#pre_render": ["\\Drupal\\views_test_data\\Controller\\ViewsTestDataController::preRender"]} }}', (string) $output, 'Ensure that new style placeholders are replaced');
}

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