You are here

public function FieldCustomTest::testFieldCustomTokens in Drupal 10

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

Ensure that custom fields can use tokens.

File

core/modules/views/tests/src/Kernel/Handler/FieldCustomTest.php, line 61

Class

FieldCustomTest
Tests the core Drupal\views\Plugin\views\field\Custom handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testFieldCustomTokens() {
  $view = Views::getView('test_view');
  $view
    ->setDisplay();
  $view->displayHandlers
    ->get('default')
    ->overrideOption('fields', [
    'age' => [
      'id' => 'age',
      'exclude' => TRUE,
      'table' => 'views_test_data',
      'field' => 'age',
    ],
    'name' => [
      'id' => 'name',
      'table' => 'views_test_data',
      'field' => 'name',
      'relationship' => 'none',
      'alter' => [
        'text' => 'Amount of kittens: {{ age }}',
      ],
    ],
  ]);

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = \Drupal::service('renderer');
  $preview = $view
    ->preview();
  $output = $renderer
    ->renderRoot($preview);
  $expected_text = 'Amount of kittens: ' . $view->style_plugin
    ->getField(0, 'age');
  $this
    ->assertStringContainsString($expected_text, (string) $output, 'The views token has been successfully replaced.');
}