RenderCacheIntegrationTest.php 11.5 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\views\Kernel;
4 5

use Drupal\Core\Cache\Cache;
6
use Drupal\Core\Entity\EntityInterface;
7
use Drupal\Core\Language\LanguageInterface;
8
use Drupal\entity_test\Entity\EntityTest;
9
use Drupal\views\Tests\AssertViewsCacheTagsTrait;
10
use Drupal\views\Views;
11
use Drupal\views\Entity\View;
12 13

/**
14
 * Tests the general integration between views and the render cache.
15 16 17
 *
 * @group views
 */
18
class RenderCacheIntegrationTest extends ViewsKernelTestBase {
19 20 21 22 23 24

  use AssertViewsCacheTagsTrait;

  /**
   * {@inheritdoc}
   */
25
  public static $testViews = ['test_view', 'test_display', 'entity_test_fields', 'entity_test_row'];
26 27 28 29

  /**
   * {@inheritdoc}
   */
30
  public static $modules = ['entity_test', 'user', 'node'];
31 32 33 34

  /**
   * {@inheritdoc}
   */
35
  protected function setUp($import_test_views = TRUE) {
36 37 38 39 40 41 42 43 44 45
    parent::setUp();

    $this->installEntitySchema('entity_test');
    $this->installEntitySchema('user');
  }

  /**
   * Tests a field-based view's cache tags when using the "none" cache plugin.
   */
  public function testFieldBasedViewCacheTagsWithCachePluginNone() {
46 47 48 49 50 51
    $view = Views::getview('entity_test_fields');
    $view->getDisplay()->overrideOption('cache', [
      'type' => 'none',
    ]);
    $view->save();

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    $this->assertCacheTagsForFieldBasedView(FALSE);
  }

  /**
   * Tests a field-based view's cache tags when using the "tag" cache plugin.
   */
  public function testFieldBasedViewCacheTagsWithCachePluginTag() {
    $view = Views::getview('entity_test_fields');
    $view->getDisplay()->overrideOption('cache', [
      'type' => 'tag',
    ]);
    $view->save();

    $this->assertCacheTagsForFieldBasedView(TRUE);
  }

  /**
   * Tests a field-based view's cache tags when using the "time" cache plugin.
   */
  public function testFieldBasedViewCacheTagsWithCachePluginTime() {
    $view = Views::getview('entity_test_fields');
    $view->getDisplay()->overrideOption('cache', [
      'type' => 'time',
      'options' => [
        'results_lifespan' => 3600,
        'output_lifespan' => 3600,
      ],
    ]);
    $view->save();

    $this->assertCacheTagsForFieldBasedView(TRUE);
  }

  /**
   * Tests cache tags on output & result cache items for a field-based view.
   *
   * @param bool $do_assert_views_caches
   *   Whether to check Views' result & output caches.
   */
  protected function assertCacheTagsForFieldBasedView($do_assert_views_caches) {
    $this->pass('Checking cache tags for field-based view.');
    $view = Views::getview('entity_test_fields');

    // Empty result (no entities yet).
96
    $this->pass('Test without entities');
97
    $base_tags = ['config:views.view.entity_test_fields', 'entity_test_list'];
98
    $this->assertViewsCacheTags($view, $base_tags, $do_assert_views_caches, $base_tags);
99
    $this->assertViewsCacheTagsFromStaticRenderArray($view, $base_tags, $do_assert_views_caches);
100 101 102


    // Non-empty result (1 entity).
103
    /** @var \Drupal\Core\Entity\EntityInterface[] $entities */
104 105 106
    $entities[] = $entity = EntityTest::create();
    $entity->save();

107
    $this->pass('Test with entities');
108 109
    $tags_with_entity = Cache::mergeTags($base_tags, $entities[0]->getCacheTags());
    $this->assertViewsCacheTags($view, $tags_with_entity, $do_assert_views_caches, $tags_with_entity);
110
    $this->assertViewsCacheTagsFromStaticRenderArray($view, $tags_with_entity, $do_assert_views_caches);
111 112 113 114 115 116 117


    // Paged result (more entities than the items-per-page limit).
    for ($i = 0; $i < 5; $i++) {
      $entities[] = $entity = EntityTest::create();
      $entity->save();
    }
118

119
    // Page 1.
120 121 122
    $this->pass('Test pager');
    $this->pass('Page 1');
    \Drupal::request()->query->set('page', 0);
123 124 125 126 127
    $tags_page_1 = Cache::mergeTags($base_tags, $entities[1]->getCacheTags());
    $tags_page_1 = Cache::mergeTags($tags_page_1, $entities[2]->getCacheTags());
    $tags_page_1 = Cache::mergeTags($tags_page_1, $entities[3]->getCacheTags());
    $tags_page_1 = Cache::mergeTags($tags_page_1, $entities[4]->getCacheTags());
    $tags_page_1 = Cache::mergeTags($tags_page_1, $entities[5]->getCacheTags());
128
    $this->assertViewsCacheTags($view, $tags_page_1, $do_assert_views_caches, $tags_page_1);
129
    $this->assertViewsCacheTagsFromStaticRenderArray($view, $tags_page_1, $do_assert_views_caches);
130 131
    $view->destroy();
    // Page 2.
132
    $this->pass('Page 2');
133
    $view->setCurrentPage(1);
134
    \Drupal::request()->query->set('page', 1);
135 136 137 138 139
    $tags_page_2 = Cache::mergeTags($base_tags, $entities[0]->getCacheTags());
    $this->assertViewsCacheTags($view, $tags_page_2, $do_assert_views_caches, $tags_page_2);
    $view->destroy();

    // Ensure that invalidation works on both pages.
140 141
    $this->pass('Page invalidations');
    $this->pass('Page 2');
142
    $view->setCurrentPage(1);
143
    \Drupal::request()->query->set('page', 1);
144 145 146
    $entities[0]->name->value = $random_name = $this->randomMachineName();
    $entities[0]->save();
    $build = $this->assertViewsCacheTags($view, $tags_page_2, $do_assert_views_caches, $tags_page_2);
147 148 149
    // @todo Static render arrays don't support different pages yet, see
    //   https://www.drupal.org/node/2500701.
    // $this->assertViewsCacheTagsFromStaticRenderArray($view, $tags_page_2, $do_assert_views_caches);
150 151 152
    $this->assertTrue(strpos($build['#markup'], $random_name) !== FALSE);
    $view->destroy();

153
    $this->pass('Page 1');
154
    $view->setCurrentPage(0);
155
    \Drupal::request()->query->set('page', 0);
156 157 158
    $entities[1]->name->value = $random_name = $this->randomMachineName();
    $entities[1]->save();
    $build = $this->assertViewsCacheTags($view, $tags_page_1, $do_assert_views_caches, $tags_page_1);
159
    $this->assertViewsCacheTagsFromStaticRenderArray($view, $tags_page_1, $do_assert_views_caches);
160
    $this->assertTrue(strpos($build['#markup'], $random_name) !== FALSE);
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    $view->destroy();

    // Setup arguments to ensure that render caching also varies by them.
    $this->pass('Test arguments');

    // Custom assert for a single result row.
    $single_entity_assertions = function(array $build, EntityInterface $entity) {
      $this->setRawContent($build['#markup']);

      $result = $this->cssSelect('div.views-row');
      $count = count($result);
      $this->assertEqual($count, 1);

      $this->assertEqual((string) $result[0]->div->span, (string) $entity->id());
    };

    // Execute the view once with a static renderable and one with a full
    // prepared render array.
    $tags_argument = Cache::mergeTags($base_tags, $entities[0]->getCacheTags());
    $view->setArguments([$entities[0]->id()]);
    $build = $this->assertViewsCacheTags($view, $tags_argument, $do_assert_views_caches, $tags_argument);
    $single_entity_assertions($build, $entities[0]);

    $view->setArguments([$entities[0]->id()]);
    $build = $this->assertViewsCacheTagsFromStaticRenderArray($view, $tags_argument, $do_assert_views_caches);
    $single_entity_assertions($build, $entities[0]);

    // Set a different argument and ensure that the result is different.
    $tags2_argument = Cache::mergeTags($base_tags, $entities[1]->getCacheTags());
    $view->setArguments([$entities[1]->id()]);
    $build = $this->assertViewsCacheTagsFromStaticRenderArray($view, $tags2_argument, $do_assert_views_caches);
    $single_entity_assertions($build, $entities[1]);

    $view->destroy();
195 196 197 198 199 200
  }

  /**
   * Tests a entity-based view's cache tags when using the "none" cache plugin.
   */
  public function testEntityBasedViewCacheTagsWithCachePluginNone() {
201 202 203 204 205 206
    $view = Views::getview('entity_test_row');
    $view->getDisplay()->overrideOption('cache', [
      'type' => 'none',
    ]);
    $view->save();

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    $this->assertCacheTagsForEntityBasedView(FALSE);
  }

  /**
   * Tests a entity-based view's cache tags when using the "tag" cache plugin.
   */
  public function testEntityBasedViewCacheTagsWithCachePluginTag() {
    $view = Views::getview('entity_test_row');
    $view->getDisplay()->overrideOption('cache', [
      'type' => 'tag',
    ]);
    $view->save();

    $this->assertCacheTagsForEntityBasedView(TRUE);
  }

  /**
   * Tests a entity-based view's cache tags when using the "time" cache plugin.
   */
  public function testEntityBasedViewCacheTagsWithCachePluginTime() {
    $view = Views::getview('entity_test_row');
    $view->getDisplay()->overrideOption('cache', [
      'type' => 'time',
      'options' => [
        'results_lifespan' => 3600,
        'output_lifespan' => 3600,
      ],
    ]);
    $view->save();

    $this->assertCacheTagsForEntityBasedView(TRUE);
  }

  /**
   * Tests cache tags on output & result cache items for an entity-based view.
   */
  protected function assertCacheTagsForEntityBasedView($do_assert_views_caches) {
    $this->pass('Checking cache tags for entity-based view.');
    $view = Views::getview('entity_test_row');

    // Empty result (no entities yet).
    $base_tags = $base_render_tags = ['config:views.view.entity_test_row', 'entity_test_list'];
    $this->assertViewsCacheTags($view, $base_tags, $do_assert_views_caches, $base_tags);
250
    $this->assertViewsCacheTagsFromStaticRenderArray($view, $base_tags, $do_assert_views_caches);
251 252 253 254 255 256

    // Non-empty result (1 entity).
    $entities[] = $entity = EntityTest::create();
    $entity->save();

    $result_tags_with_entity = Cache::mergeTags($base_tags, $entities[0]->getCacheTags());
257 258
    $render_tags_with_entity = Cache::mergeTags($base_render_tags, $entities[0]->getCacheTags());
    $render_tags_with_entity = Cache::mergeTags($render_tags_with_entity, ['entity_test_view']);
259
    $this->assertViewsCacheTags($view, $result_tags_with_entity, $do_assert_views_caches, $render_tags_with_entity);
260
    $this->assertViewsCacheTagsFromStaticRenderArray($view, $render_tags_with_entity, $do_assert_views_caches);
261 262 263 264 265 266 267 268


    // Paged result (more entities than the items-per-page limit).
    for ($i = 0; $i < 5; $i++) {
      $entities[] = $entity = EntityTest::create();
      $entity->save();
    }

269 270 271 272
    $new_entities_cache_tags = Cache::mergeTags($entities[1]->getCacheTags(), $entities[2]->getCacheTags());
    $new_entities_cache_tags = Cache::mergeTags($new_entities_cache_tags, $entities[3]->getCacheTags());
    $new_entities_cache_tags = Cache::mergeTags($new_entities_cache_tags, $entities[4]->getCacheTags());
    $new_entities_cache_tags = Cache::mergeTags($new_entities_cache_tags, $entities[5]->getCacheTags());
273
    $result_tags_page_1 = Cache::mergeTags($base_tags, $new_entities_cache_tags);
274 275
    $render_tags_page_1 = Cache::mergeTags($base_render_tags, $new_entities_cache_tags);
    $render_tags_page_1 = Cache::mergeTags($render_tags_page_1, ['entity_test_view']);
276
    $this->assertViewsCacheTags($view, $result_tags_page_1, $do_assert_views_caches, $render_tags_page_1);
277
    $this->assertViewsCacheTagsFromStaticRenderArray($view, $render_tags_page_1, $do_assert_views_caches);
278 279
  }

280 281 282 283 284 285
  /**
   * Ensure that the view renderable contains the cache contexts.
   */
  public function testBuildRenderableWithCacheContexts() {
    $view = View::load('test_view');
    $display =& $view->getDisplay('default');
286
    $display['cache_metadata']['contexts'] = ['views_test_cache_context'];
287 288 289
    $executable = $view->getExecutable();

    $build = $executable->buildRenderable();
290
    $this->assertEqual(['views_test_cache_context'], $build['#cache']['contexts']);
291 292 293 294 295 296 297 298 299
  }

  /**
   * Ensures that saving a view calculates the cache contexts.
   */
  public function testViewAddCacheMetadata() {
    $view = View::load('test_display');
    $view->save();

300
    $this->assertEqual(['languages:' . LanguageInterface::TYPE_CONTENT, 'languages:' . LanguageInterface::TYPE_INTERFACE, 'url.query_args', 'user.node_grants:view', 'user.permissions'], $view->getDisplay('default')['cache_metadata']['contexts']);
301 302
  }

303
}