You are here

public function AreaTest::testRenderAreaToken in Drupal 10

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

Tests global tokens.

File

core/modules/views/tests/src/Functional/Handler/AreaTest.php, line 165

Class

AreaTest
Tests the plugin base of the area handler.

Namespace

Drupal\Tests\views\Functional\Handler

Code

public function testRenderAreaToken() {
  $admin_user = $this
    ->drupalCreateUser([
    'administer views',
    'administer site configuration',
  ]);
  $this
    ->drupalLogin($admin_user);
  $view = Views::getView('test_example_area');
  $view
    ->initHandlers();
  $this
    ->drupalGet('admin/structure/views/nojs/handler/test_example_area/default/empty/test_example');

  // Test that the list is token present.
  $element = $this
    ->xpath('//ul[@class="global-tokens"]');
  $this
    ->assertNotEmpty($element, 'Token list found on the options form.');
  $empty_handler =& $view->empty['test_example'];

  // Test the list of available tokens.
  $available = $empty_handler
    ->getAvailableGlobalTokens();
  foreach ([
    'site',
    'view',
  ] as $type) {
    $this
      ->assertNotEmpty($available[$type]);
    $this
      ->assertIsArray($available[$type]);

    // Test that each item exists in the list.
    foreach ($available[$type] as $token => $info) {
      $this
        ->assertSession()
        ->pageTextContains("[{$type}:{$token}]");
    }
  }

  // Test the rendered output of a token.
  $empty_handler->options['string'] = '[site:name]';

  // Test we have the site:name token in the output.
  $output = $view
    ->preview();
  $output = $this->container
    ->get('renderer')
    ->renderRoot($output);
  $expected = \Drupal::token()
    ->replace('[site:name]');
  $this
    ->assertStringContainsString($expected, $output);
}