You are here

function TokenReplaceTest::testTokenReplacement in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Tests/TokenReplaceTest.php \Drupal\views\Tests\TokenReplaceTest::testTokenReplacement()

Tests core token replacements generated from a view.

File

core/modules/views/src/Tests/TokenReplaceTest.php, line 38
Contains \Drupal\views\Tests\TokenReplaceTest.

Class

TokenReplaceTest
Tests core view token replacement.

Namespace

Drupal\views\Tests

Code

function testTokenReplacement() {
  $token_handler = \Drupal::token();
  $view = Views::getView('test_tokens');
  $view
    ->setDisplay('page_1');
  $this
    ->executeView($view);
  $expected = array(
    '[view:label]' => 'Test tokens',
    '[view:description]' => 'Test view to token replacement tests.',
    '[view:id]' => 'test_tokens',
    '[view:title]' => 'Test token page',
    '[view:url]' => $view
      ->getUrl(NULL, 'page_1')
      ->setAbsolute(TRUE)
      ->toString(),
    '[view:total-rows]' => (string) $view->total_rows,
    '[view:base-table]' => 'views_test_data',
    '[view:base-field]' => 'id',
    '[view:items-per-page]' => '10',
    '[view:current-page]' => '1',
    '[view:page-count]' => '1',
  );
  $base_bubbleable_metadata = BubbleableMetadata::createFromObject($view->storage);
  $metadata_tests = [];
  $metadata_tests['[view:label]'] = $base_bubbleable_metadata;
  $metadata_tests['[view:description]'] = $base_bubbleable_metadata;
  $metadata_tests['[view:id]'] = $base_bubbleable_metadata;
  $metadata_tests['[view:title]'] = $base_bubbleable_metadata;
  $metadata_tests['[view:url]'] = $base_bubbleable_metadata;
  $metadata_tests['[view:total-rows]'] = $base_bubbleable_metadata;
  $metadata_tests['[view:base-table]'] = $base_bubbleable_metadata;
  $metadata_tests['[view:base-field]'] = $base_bubbleable_metadata;
  $metadata_tests['[view:items-per-page]'] = $base_bubbleable_metadata;
  $metadata_tests['[view:current-page]'] = $base_bubbleable_metadata;
  $metadata_tests['[view:page-count]'] = $base_bubbleable_metadata;
  foreach ($expected as $token => $expected_output) {
    $bubbleable_metadata = new BubbleableMetadata();
    $output = $token_handler
      ->replace($token, array(
      'view' => $view,
    ), [], $bubbleable_metadata);
    $this
      ->assertIdentical($output, $expected_output, format_string('Token %token replaced correctly.', array(
      '%token' => $token,
    )));
    $this
      ->assertEqual($bubbleable_metadata, $metadata_tests[$token]);
  }
}