diff --git a/core/modules/node/src/Tests/Views/FrontPageTest.php b/core/modules/node/src/Tests/Views/FrontPageTest.php index 4123d36..8b522e4 100644 --- a/core/modules/node/src/Tests/Views/FrontPageTest.php +++ b/core/modules/node/src/Tests/Views/FrontPageTest.php @@ -11,6 +11,7 @@ use Drupal\Core\Url; use Drupal\node\Entity\Node; use Drupal\system\Tests\Cache\AssertPageCacheTagsTrait; +use Drupal\views\Tests\AssertViewsCacheTagsTrait; use Drupal\views\Tests\ViewTestBase; use Drupal\views\ViewExecutable; use Drupal\views\Views; @@ -23,6 +24,7 @@ class FrontPageTest extends ViewTestBase { use AssertPageCacheTagsTrait; + use AssertViewsCacheTagsTrait; /** * {@inheritdoc} @@ -259,8 +261,6 @@ public function testCacheTags() { Cache::mergeTags($first_page_output_cache_tags, ['rendered']) ); - // DEBUG: commenting the rest, let's first get the above to work. -return; // Second page. $this->assertPageCacheTags(Url::fromRoute('view.frontpage.page_1', [], ['query' => ['page' => 1]]), [ // The cache tags for the listed nodes. diff --git a/core/modules/views/src/Plugin/views/cache/CachePluginBase.php b/core/modules/views/src/Plugin/views/cache/CachePluginBase.php index a1ca918..c4d628b 100644 --- a/core/modules/views/src/Plugin/views/cache/CachePluginBase.php +++ b/core/modules/views/src/Plugin/views/cache/CachePluginBase.php @@ -191,7 +191,7 @@ public function cacheSet($type) { // that is used to render the view for this request and rendering does // not happen twice. $this->storage = $this->view->display_handler->output = $this->renderer->getCacheableRenderArray($output); - \Drupal::cache($this->outputBin)->set($this->generateOutputKey(), $this->storage, $this->cacheSetExpire($type), $this->getCacheTags(), $this->storage['#cache']['tags']); + \Drupal::cache($this->outputBin)->set($this->generateOutputKey(), $this->storage, $this->cacheSetExpire($type), $this->storage['#cache']['tags']); break; } } diff --git a/core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php b/core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php new file mode 100644 index 0000000..b3002bd --- /dev/null +++ b/core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php @@ -0,0 +1,62 @@ +preview(); + + /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache_plugin */ + $cache_plugin = $view->display_handler->getPlugin('cache'); + + // Results cache. + $results_cache_item = \Drupal::cache('data')->get($cache_plugin->generateResultsKey()); + if (is_array($expected_results_cache)) { + $this->assertTrue($results_cache_item, 'Results cache item found.'); + if ($results_cache_item) { + sort($expected_results_cache); + $this->assertEqual($results_cache_item->tags, $expected_results_cache); + } + } + else { + $this->assertFalse($results_cache_item, 'Results cache item not found.'); + } + + // Output cache. + $output_cache_item = \Drupal::cache('render')->get($cache_plugin->generateOutputKey()); + if (is_array($expected_output_cache)) { + $this->assertTrue($output_cache_item, 'Output cache item found.'); + if ($output_cache_item) { + sort($expected_output_cache); + $this->assertEqual($output_cache_item->tags, $expected_output_cache); + } + } + else { + $this->assertFalse($output_cache_item, 'Output cache item not found.'); + } + + $view->destroy(); + } + +} diff --git a/core/modules/views/src/Tests/GlossaryTest.php b/core/modules/views/src/Tests/GlossaryTest.php index beb6fb6..c661839 100644 --- a/core/modules/views/src/Tests/GlossaryTest.php +++ b/core/modules/views/src/Tests/GlossaryTest.php @@ -20,6 +20,7 @@ class GlossaryTest extends ViewTestBase { use AssertPageCacheTagsTrait; + use AssertViewsCacheTagsTrait; /** * Modules to enable. diff --git a/core/modules/views/src/Tests/RenderCacheIntegrationTest.php b/core/modules/views/src/Tests/RenderCacheIntegrationTest.php new file mode 100644 index 0000000..29780b0 --- /dev/null +++ b/core/modules/views/src/Tests/RenderCacheIntegrationTest.php @@ -0,0 +1,96 @@ +installEntitySchema('entity_test'); + $this->installEntitySchema('user'); + } + + public function testFieldBasedView() { + $view = Views::getview('entity_test_fields'); + $view->getDisplay()->overrideOption('cache', [ + 'type' => 'tag', + ]); + + // Empty result. + $base_tags = ['config:views.view.entity_test_fields', 'entity_test_list']; + $this->assertViewsCacheTags($view, $base_tags, $base_tags); + + // Non-empty result. + $entities[] = $entity = EntityTest::create(); + $entity->save(); + + $tags_with_entity = array_merge($base_tags, $entities[0]->getCacheTags()); + $this->assertViewsCacheTags($view, $tags_with_entity, $tags_with_entity); + + // Create more items than available for the pager. + for ($i = 0; $i < 5; $i++) { + $entities[] = $entity = EntityTest::create(); + $entity->save(); + } + + $tags_exceeding_page = array_merge($base_tags, $entities[1]->getCacheTags(), $entities[2]->getCacheTags(), $entities[3]->getCacheTags(), $entities[4]->getCacheTags(), $entities[5]->getCacheTags()); + $this->assertViewsCacheTags($view, $tags_exceeding_page, $tags_exceeding_page); + } + + public function testEntityRenderBasedView() { + $view = Views::getview('entity_test_row'); + $view->getDisplay()->overrideOption('cache', [ + 'type' => 'tag', + ]); + + // Empty result. + $base_tags = $base_render_tags = ['config:views.view.entity_test_row', 'entity_test_list']; + $this->assertViewsCacheTags($view, $base_tags, $base_render_tags); + + $base_render_tags[] = 'entity_test_view'; + + // Non-empty result. + $entities[] = $entity = EntityTest::create(); + $entity->save(); + + $result_tags_with_entity = array_merge($base_tags, $entities[0]->getCacheTags()); + $render_tags_with_entity = array_merge($base_render_tags, $entities[0]->getCacheTags()); + $this->assertViewsCacheTags($view, $result_tags_with_entity, $render_tags_with_entity); + + // Create more items than available for the pager. + for ($i = 0; $i < 5; $i++) { + $entities[] = $entity = EntityTest::create(); + $entity->save(); + } + + $result_tags_exceeding_page = array_merge($base_tags, $entities[1]->getCacheTags(), $entities[2]->getCacheTags(), $entities[3]->getCacheTags(), $entities[4]->getCacheTags(), $entities[5]->getCacheTags()); + $render_tags_exceeding_page = array_merge($base_render_tags, $entities[1]->getCacheTags(), $entities[2]->getCacheTags(), $entities[3]->getCacheTags(), $entities[4]->getCacheTags(), $entities[5]->getCacheTags()); + $this->assertViewsCacheTags($view, $result_tags_exceeding_page, $render_tags_exceeding_page); + } + +} diff --git a/core/modules/views/src/Tests/ViewTestBase.php b/core/modules/views/src/Tests/ViewTestBase.php index 8de345e..042c5ce 100644 --- a/core/modules/views/src/Tests/ViewTestBase.php +++ b/core/modules/views/src/Tests/ViewTestBase.php @@ -156,51 +156,4 @@ protected function dataSet() { return ViewTestData::dataSet(); } - /** - * Asserts a view's result & output cache items' cache tags. - * - * @param \Drupal\views\ViewExecutable $view - * The view to test, must have caching enabled. - * @param null|string[] $expected_results_cache - * NULL when expecting no results cache item, a set of cache tags expected - * to be set on the results cache item otherwise. - * @param null|string[] $expected_output_cache - * NULL when expecting no output cache item, a set of cache tags expected to - * be set on the output cache item otherwise. - */ - protected function assertViewsCacheTags(ViewExecutable $view, $expected_results_cache, $expected_output_cache) { - $view->preview(); - - /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache_plugin */ - $cache_plugin = $view->display_handler->getPlugin('cache'); - - // Results cache. - $results_cache_item = \Drupal::cache('data')->get($cache_plugin->generateResultsKey()); - if (is_array($expected_results_cache)) { - $this->assertTrue($results_cache_item, 'Results cache item found.'); - if ($results_cache_item) { - sort($expected_results_cache); - $this->assertEqual($results_cache_item->tags, $expected_results_cache); - } - } - else { - $this->assertFalse($results_cache_item, 'Results cache item not found.'); - } - - // Output cache. - $output_cache_item = \Drupal::cache('render')->get($cache_plugin->generateOutputKey()); - if (is_array($expected_output_cache)) { - $this->assertTrue($output_cache_item, 'Output cache item found.'); - if ($output_cache_item) { - sort($expected_output_cache); - $this->assertEqual($output_cache_item->tags, $expected_output_cache); - } - } - else { - $this->assertFalse($output_cache_item, 'Output cache item not found.'); - } - - $view->destroy(); - } - } diff --git a/core/modules/views/tests/modules/views_test_config/test_views/views.view.entity_test_fields.yml b/core/modules/views/tests/modules/views_test_config/test_views/views.view.entity_test_fields.yml new file mode 100644 index 0000000..8641d2b --- /dev/null +++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.entity_test_fields.yml @@ -0,0 +1,74 @@ +langcode: und +status: true +dependencies: { } +id: entity_test_fields +label: '' +module: views +description: '' +tag: '' +base_table: entity_test +base_field: nid +core: '8' +display: + default: + display_options: + access: + type: none + cache: + type: none + exposed_form: + type: basic + fields: + id: + alter: + alter_text: false + element_class: '' + element_default_classes: true + empty: '' + hide_empty: false + empty_zero: false + hide_alter_empty: true + plugin_id: numeric + entity_type: entity_test + entity_field: id + id: id + table: entity_test + field: id + name: + alter: + alter_text: false + ellipsis: true + html: false + make_link: false + strip_tags: false + trim: false + word_boundary: true + empty_zero: false + field: name + hide_empty: false + id: name + table: entity_test + plugin_id: standard + entity_type: entity_test + entity_field: name + sorts: + id: + table: entity_test + id: id + field: id + plugin_id: standard + entity_type: entity_test + entity_field: id + order: desc + pager: + type: some + options: + items_per_page: 5 + style: + type: default + row: + type: fields + display_plugin: default + display_title: Master + id: default + position: 0 diff --git a/core/modules/views/tests/modules/views_test_config/test_views/views.view.entity_test_row.yml b/core/modules/views/tests/modules/views_test_config/test_views/views.view.entity_test_row.yml new file mode 100644 index 0000000..023a3f3 --- /dev/null +++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.entity_test_row.yml @@ -0,0 +1,41 @@ +langcode: und +status: true +dependencies: { } +id: entity_test_row +label: '' +module: views +description: '' +tag: '' +base_table: entity_test +base_field: nid +core: '8' +display: + default: + display_options: + access: + type: none + cache: + type: none + exposed_form: + type: basic + sorts: + id: + table: entity_test + id: id + field: id + plugin_id: standard + entity_type: entity_test + entity_field: id + order: desc + pager: + type: some + options: + items_per_page: 5 + style: + type: default + row: + type: 'entity:entity_test' + display_plugin: default + display_title: Master + id: default + position: 0