diff --git a/core/modules/rest/src/Tests/Views/StyleSerializerTest.php b/core/modules/rest/src/Tests/Views/StyleSerializerTest.php index ebb3a66..885d92a 100644 --- a/core/modules/rest/src/Tests/Views/StyleSerializerTest.php +++ b/core/modules/rest/src/Tests/Views/StyleSerializerTest.php @@ -9,6 +9,7 @@ use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Cache\Cache; +use Drupal\entity_test\Entity\EntityTest; use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; use Drupal\views\Entity\View; use Drupal\views\Plugin\views\display\DisplayPluginBase; @@ -272,12 +273,14 @@ public function testRestRenderCaching() { $this->assertTrue($render_cache->get($original)); // Create a new entity and ensure that the cache tags are taken over. - entity_create('entity_test', array('name' => 'test_11', 'user_id' => $this->adminUser->id()))->save(); + EntityTest::create(['name' => 'test_11', 'user_id' => $this->adminUser->id()])->save(); $result3 = $this->drupalGetJSON('test/serialize/entity'); $this->addRequestWithFormat('json'); $this->assertHeader('content-type', 'application/json'); $this->assertNotEqual($result3, $result2); + // Add the new entity cache tag and remove the first one, because we just + // show 10 items in total. $cache_tags[] = 'entity_test:11'; unset($cache_tags[array_search('entity_test:1', $cache_tags)]); diff --git a/core/modules/system/tests/modules/entity_test/src/Cache/EntityTestViewGrantsCacheContext.php b/core/modules/system/tests/modules/entity_test/src/Cache/EntityTestViewGrantsCacheContext.php index bd46dc2..53649f5 100644 --- a/core/modules/system/tests/modules/entity_test/src/Cache/EntityTestViewGrantsCacheContext.php +++ b/core/modules/system/tests/modules/entity_test/src/Cache/EntityTestViewGrantsCacheContext.php @@ -27,7 +27,8 @@ public static function getLabel() { * {@inheritdoc} */ public function getContext() { - // Return a constant value. + // Return a constant value, so we can fetch render cache both in actual + // requests and test code itself. return '299792458'; } diff --git a/core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php b/core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php index ae85779..0ac8141 100644 --- a/core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php +++ b/core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php @@ -18,7 +18,9 @@ use AssertPageCacheContextsAndTagsTrait; /** - * Asserts a view's result & output cache items' cache tags. + * Asserts a view's result & render cache items' cache tags. + * + * This methods uses a full view object in order to render the view. * * @param \Drupal\views\ViewExecutable $view * The view to test, must have caching enabled. @@ -106,6 +108,18 @@ protected function assertViewsCacheTags(ViewExecutable $view, $expected_results_ return $build; } + /** + * Asserts a view's result & render cache items' cache tags. + * + * This method starts with a pre bubbling basic render array. + * + * @param \Drupal\views\ViewExecutable $view + * The view. + * @param array $expected_render_array_cache_tags + * The expected render cache tags. + * @param bool $views_caching_is_enabled + * Defines whether views output / render caching is enabled. + */ protected function assertViewsCacheTagsFromStaticRenderArray(ViewExecutable $view, array $expected_render_array_cache_tags, $views_caching_is_enabled) { $original = $build = DisplayPluginBase::buildBasicRenderable($view->id(), $view->current_display ?: 'default', $view->args); diff --git a/core/modules/views/src/Tests/RenderCacheIntegrationTest.php b/core/modules/views/src/Tests/RenderCacheIntegrationTest.php index dd5749d..ce61025 100644 --- a/core/modules/views/src/Tests/RenderCacheIntegrationTest.php +++ b/core/modules/views/src/Tests/RenderCacheIntegrationTest.php @@ -111,6 +111,7 @@ protected function assertCacheTagsForFieldBasedView($do_assert_views_caches) { } // Page 1. $this->pass('Page 1'); + \Drupal::request()->query->set('page', 0); $tags_page_1 = Cache::mergeTags($base_tags, $entities[1]->getCacheTags(), $entities[2]->getCacheTags(), $entities[3]->getCacheTags(), $entities[4]->getCacheTags(), $entities[5]->getCacheTags()); $this->assertViewsCacheTags($view, $tags_page_1, $do_assert_views_caches, $tags_page_1); $this->assertViewsCacheTagsFromStaticRenderArray($view, $tags_page_1, $do_assert_views_caches); @@ -118,6 +119,7 @@ protected function assertCacheTagsForFieldBasedView($do_assert_views_caches) { // Page 2. $this->pass('Page 2'); $view->setCurrentPage(1); + \Drupal::request()->query->set('page', 1); $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(); @@ -126,6 +128,7 @@ protected function assertCacheTagsForFieldBasedView($do_assert_views_caches) { $this->pass('Page invalidations'); $this->pass('Page 2'); $view->setCurrentPage(1); + \Drupal::request()->query->set('page', 1); $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); @@ -135,11 +138,11 @@ protected function assertCacheTagsForFieldBasedView($do_assert_views_caches) { $this->pass('Page 1'); $view->setCurrentPage(0); + \Drupal::request()->query->set('page', 0); $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); $this->assertViewsCacheTagsFromStaticRenderArray($view, $tags_page_1, $do_assert_views_caches); - $this->assertViewsCacheTagsFromStaticRenderArray($view, $tags_page_1, $do_assert_views_caches); $this->assertTrue(strpos($build['#markup'], $random_name) !== FALSE); } @@ -235,7 +238,7 @@ public function testViewAddCacheMetadata() { $view = View::load('test_display'); $view->save(); - $this->assertEqual(['languages:' . LanguageInterface::TYPE_CONTENT, 'languages:' . LanguageInterface::TYPE_INTERFACE, 'url.query_args.pagers:0', 'user.node_grants:view'], $view->getDisplay('default')['cache_metadata']['contexts']); + $this->assertEqual(['languages:' . LanguageInterface::TYPE_CONTENT, 'languages:' . LanguageInterface::TYPE_INTERFACE, 'url.query_args.pagers:0', 'user.node_grants:view', 'user.permissions'], $view->getDisplay('default')['cache_metadata']['contexts']); } }