You are here

public function FieldKernelTest::testArgumentTokens 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::testArgumentTokens()
  2. 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 236

Class

FieldKernelTest
Tests the generic field handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testArgumentTokens() {

  /** @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 = $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
    ->assertEqual('%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 = $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
    ->assertEqual('{{ { "#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');
}