TokenReplaceTest.php 5.27 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\views\Kernel;
4

5
use Drupal\Core\Render\BubbleableMetadata;
6 7
use Drupal\views\Views;

8 9
/**
 * Tests core view token replacement.
10 11
 *
 * @group views
12
 */
13
class TokenReplaceTest extends ViewsKernelTestBase {
14

15
  public static $modules = ['system'];
16

17 18 19 20 21
  /**
   * Views used by this test.
   *
   * @var array
   */
22
  public static $testViews = ['test_tokens', 'test_invalid_tokens'];
23

24
  protected function setUp($import_test_views = TRUE) {
25
    parent::setUp();
26
    $this->container->get('router.builder')->rebuild();
27 28 29 30 31
  }

  /**
   * Tests core token replacements generated from a view.
   */
32
  public function testTokenReplacement() {
33
    $token_handler = \Drupal::token();
34
    $view = Views::getView('test_tokens');
35
    $view->setDisplay('page_1');
36 37
    // Force the view to span more than one page to better test page_count.
    $view->display_handler->getPlugin('pager')->setItemsPerPage(4);
38
    $this->executeView($view);
39

40
    $expected = [
41
      '[view:label]' => 'Test tokens',
42
      '[view:description]' => 'Test view to token replacement tests.',
43
      '[view:id]' => 'test_tokens',
44
      '[view:title]' => 'Test token page',
45
      '[view:url]' => $view->getUrl(NULL, 'page_1')->setAbsolute(TRUE)->toString(),
46
      '[view:total-rows]' => '5',
47 48
      '[view:base-table]' => 'views_test_data',
      '[view:base-field]' => 'id',
49
      '[view:items-per-page]' => '4',
50
      '[view:current-page]' => '1',
51
      '[view:page-count]' => '2',
52
    ];
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67
    $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;

68
    foreach ($expected as $token => $expected_output) {
69
      $bubbleable_metadata = new BubbleableMetadata();
70 71
      $output = $token_handler->replace($token, ['view' => $view], [], $bubbleable_metadata);
      $this->assertIdentical($output, $expected_output, format_string('Token %token replaced correctly.', ['%token' => $token]));
72
      $this->assertEqual($bubbleable_metadata, $metadata_tests[$token]);
73 74 75
    }
  }

76 77 78
  /**
   * Tests core token replacements generated from a view.
   */
79
  public function testTokenReplacementWithMiniPager() {
80 81 82 83 84 85 86
    $token_handler = \Drupal::token();
    $view = Views::getView('test_tokens');
    $view->setDisplay('page_3');
    $this->executeView($view);

    $this->assertSame(TRUE, $view->get_total_rows, 'The query was set to calculate the total number of rows.');

87
    $expected = [
88 89 90 91 92 93 94 95 96 97 98 99 100
      '[view:label]' => 'Test tokens',
      '[view:description]' => 'Test view to token replacement tests.',
      '[view:id]' => 'test_tokens',
      '[view:title]' => 'Test token page with minipager',
      '[view:url]' => $view->getUrl(NULL, 'page_3')
        ->setAbsolute(TRUE)
        ->toString(),
      '[view:total-rows]' => '5',
      '[view:base-table]' => 'views_test_data',
      '[view:base-field]' => 'id',
      '[view:items-per-page]' => '2',
      '[view:current-page]' => '1',
      '[view:page-count]' => '3',
101
    ];
102 103 104 105 106

    $base_bubbleable_metadata = BubbleableMetadata::createFromObject($view->storage);

    foreach ($expected as $token => $expected_output) {
      $bubbleable_metadata = new BubbleableMetadata();
107
      $output = $token_handler->replace($token, ['view' => $view], [], $bubbleable_metadata);
108 109 110 111 112
      $this->assertSame($expected_output, $output, sprintf('Token %s replaced correctly.', $token));
      $this->assertEquals($base_bubbleable_metadata, $bubbleable_metadata);
    }
  }

113 114 115
  /**
   * Tests core token replacements generated from a view without results.
   */
116
  public function testTokenReplacementNoResults() {
117 118 119 120 121
    $token_handler = \Drupal::token();
    $view = Views::getView('test_tokens');
    $view->setDisplay('page_2');
    $this->executeView($view);

122
    $expected = [
123
      '[view:page-count]' => '1',
124
    ];
125 126

    foreach ($expected as $token => $expected_output) {
127 128
      $output = $token_handler->replace($token, ['view' => $view]);
      $this->assertIdentical($output, $expected_output, format_string('Token %token replaced correctly.', ['%token' => $token]));
129 130 131
    }
  }

132 133 134
  /**
   * Tests path token replacements generated from a view without a path.
   */
135
  public function testTokenReplacementNoPath() {
136 137 138 139 140
    $token_handler = \Drupal::token();
    $view = Views::getView('test_invalid_tokens');
    $view->setDisplay('block_1');
    $this->executeView($view);

141
    $expected = [
142
      '[view:url]' => '',
143
    ];
144 145

    foreach ($expected as $token => $expected_output) {
146 147
      $output = $token_handler->replace($token, ['view' => $view]);
      $this->assertIdentical($output, $expected_output, format_string('Token %token replaced correctly.', ['%token' => $token]));
148 149 150
    }
  }

151
}