.../modules/node/src/Tests/Views/FrontPageTest.php | 53 ++++++++++++++++++++ .../src/Tests/Cache/AssertPageCacheTagsTrait.php | 58 ++++++++++++++++++++++ .../Tests/Cache/PageCacheTagsIntegrationTest.php | 39 ++------------- core/modules/views/src/Tests/GlossaryTest.php | 20 ++++++++ 4 files changed, 136 insertions(+), 34 deletions(-) diff --git a/core/modules/node/src/Tests/Views/FrontPageTest.php b/core/modules/node/src/Tests/Views/FrontPageTest.php index ac93dd3..7ee6764 100644 --- a/core/modules/node/src/Tests/Views/FrontPageTest.php +++ b/core/modules/node/src/Tests/Views/FrontPageTest.php @@ -7,6 +7,7 @@ namespace Drupal\node\Tests\Views; +use Drupal\system\Tests\Cache\AssertPageCacheTagsTrait; use Drupal\views\Tests\ViewTestBase; use Drupal\views\ViewExecutable; use Drupal\views\Views; @@ -18,6 +19,13 @@ */ class FrontPageTest extends ViewTestBase { + use AssertPageCacheTagsTrait; + + /** + * {@inheritdoc} + */ + protected $dumpHeaders = TRUE; + /** * The entity storage for nodes. * @@ -174,4 +182,49 @@ public function testAdminFrontPage() { $this->assertPattern('/class=".+view-frontpage/', 'Frontpage view was rendered'); } + /** + * Tests the cache tags on the front page. + */ + public function testCacheTags() { + $this->enablePageCaching(); + + // Create some nodes on the frontpage view. Add more than 10 nodes in order + // to enable paging. + $this->drupalCreateContentType(['type' => 'article']); + for ($i = 0; $i < 15; $i++) { + $this->drupalCreateNode(['type' => 'article', 'created' => $i]); + } + + // First page. + $this->assertPageCacheTags('node', [], [ + 'view:frontpage', + 'node_list', + 'node_view', + 'node:6', 'node:7', 'node:8', 'node:9', 'node:10', + 'node:11', 'node:12', 'node:13', 'node:14', 'node:15', + 'filter_format:plain_text', + 'user_view', + 'user:0', + 'rendered', + 'theme:classy', + 'theme_global_settings', + ]); + + // Second page. + $this->assertPageCacheTags('node', ['query' => ['page' => 1]], [ + // The cache tags for the listed nodes. + 'node:1', 'node:2', 'node:3', 'node:4', 'node:5', + // The rest. + 'view:frontpage', + 'node_list', + 'node_view', + 'filter_format:plain_text', + 'user_view', + 'user:0', + 'rendered', + 'theme:classy', + 'theme_global_settings', + ]); + } + } diff --git a/core/modules/system/src/Tests/Cache/AssertPageCacheTagsTrait.php b/core/modules/system/src/Tests/Cache/AssertPageCacheTagsTrait.php new file mode 100644 index 0000000..a73e88c --- /dev/null +++ b/core/modules/system/src/Tests/Cache/AssertPageCacheTagsTrait.php @@ -0,0 +1,58 @@ +set('cache.page.use_internal', 1); + $config->set('cache.page.max_age', 300); + $config->save(); + } + + /** + * Fills page cache for the given path, asserts cache tags on page cache hit. + * + * @param string $path + * The Drupal page path to test. See WebTestBase::drupalGet(). + * @param array $options + * Options to be forwarded to URL generator. See WebTestBase::drupalGet(). + * @param string[] $expected_tags + * The expected cache tags for the page cache entry of the given $path. + */ + protected function assertPageCacheTags($path, array $options = [], array $expected_tags) { + sort($expected_tags); + $this->drupalGet($path, $options); + $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS'); + $actual_tags = explode(' ', $this->drupalGetHeader('X-Drupal-Cache-Tags')); + sort($actual_tags); + $this->assertIdentical($actual_tags, $expected_tags); + $this->drupalGet($path, $options); + $actual_tags = explode(' ', $this->drupalGetHeader('X-Drupal-Cache-Tags')); + sort($actual_tags); + $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT'); + $this->assertIdentical($actual_tags, $expected_tags); + $options['absolute'] = TRUE; + $cid_parts = array(_url($path, $options), 'html'); + $cid = implode(':', $cid_parts); + $cache_entry = \Drupal::cache('render')->get($cid); + sort($cache_entry->tags); + $this->assertEqual($cache_entry->tags, $expected_tags); + } + +} diff --git a/core/modules/system/src/Tests/Cache/PageCacheTagsIntegrationTest.php b/core/modules/system/src/Tests/Cache/PageCacheTagsIntegrationTest.php index 932db0c..33ba3c0 100644 --- a/core/modules/system/src/Tests/Cache/PageCacheTagsIntegrationTest.php +++ b/core/modules/system/src/Tests/Cache/PageCacheTagsIntegrationTest.php @@ -8,7 +8,6 @@ namespace Drupal\system\Tests\Cache; use Drupal\simpletest\WebTestBase; -use Drupal\Core\Cache\Cache; /** * Enables the page cache and tests its cache tags in various scenarios. @@ -20,6 +19,8 @@ */ class PageCacheTagsIntegrationTest extends WebTestBase { + use AssertPageCacheTagsTrait; + protected $profile = 'standard'; protected $dumpHeaders = TRUE; @@ -27,10 +28,7 @@ class PageCacheTagsIntegrationTest extends WebTestBase { protected function setUp() { parent::setUp(); - $config = \Drupal::config('system.performance'); - $config->set('cache.page.use_internal', 1); - $config->set('cache.page.max_age', 300); - $config->save(); + $this->enablePageCaching(); } /** @@ -67,7 +65,7 @@ function testPageCacheTags() { )); // Full node page 1. - $this->verifyPageCacheTags('node/' . $node_1->id(), array( + $this->assertPageCacheTags('node/' . $node_1->id(), [], array( 'rendered', 'theme:bartik', 'theme_global_settings', @@ -97,7 +95,7 @@ function testPageCacheTags() { )); // Full node page 2. - $this->verifyPageCacheTags('node/' . $node_2->id(), array( + $this->assertPageCacheTags('node/' . $node_2->id(), [], array( 'rendered', 'theme:bartik', 'theme_global_settings', @@ -132,31 +130,4 @@ function testPageCacheTags() { )); } - /** - * Fills page cache for the given path, verify cache tags on page cache hit. - * - * @param $path - * The Drupal page path to test. - * @param $expected_tags - * The expected cache tags for the page cache entry of the given $path. - */ - protected function verifyPageCacheTags($path, $expected_tags) { - sort($expected_tags); - $this->drupalGet($path); - $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS'); - $actual_tags = explode(' ', $this->drupalGetHeader('X-Drupal-Cache-Tags')); - sort($actual_tags); - $this->assertIdentical($actual_tags, $expected_tags); - $this->drupalGet($path); - $actual_tags = explode(' ', $this->drupalGetHeader('X-Drupal-Cache-Tags')); - sort($actual_tags); - $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT'); - $this->assertIdentical($actual_tags, $expected_tags); - $cid_parts = array(_url($path, array('absolute' => TRUE)), 'html'); - $cid = implode(':', $cid_parts); - $cache_entry = \Drupal::cache('render')->get($cid); - sort($cache_entry->tags); - $this->assertEqual($cache_entry->tags, $expected_tags); - } - } diff --git a/core/modules/views/src/Tests/GlossaryTest.php b/core/modules/views/src/Tests/GlossaryTest.php index 61fb6a3..41c1f57 100644 --- a/core/modules/views/src/Tests/GlossaryTest.php +++ b/core/modules/views/src/Tests/GlossaryTest.php @@ -8,6 +8,7 @@ namespace Drupal\views\Tests; use Drupal\Component\Utility\Unicode; +use Drupal\system\Tests\Cache\AssertPageCacheTagsTrait; use Drupal\views\Views; /** @@ -17,6 +18,13 @@ */ class GlossaryTest extends ViewTestBase { + use AssertPageCacheTagsTrait; + + /** + * {@inheritdoc} + */ + protected $dumpHeaders = TRUE; + /** * Modules to enable. * @@ -76,6 +84,18 @@ public function testGlossaryView() { $result_count = trim(str_replace(array('|', '(', ')'), '', (string) $result[0])); $this->assertEqual($result_count, $count, 'The expected number got rendered.'); } + + // Verify cache tags. + $this->enablePageCaching(); + $this->assertPageCacheTags('glossary', [], [ + 'view:glossary', + 'node_list', + // @todo Make sure the cache tags for the individual nodes also exist. + 'user_list', + 'rendered', + 'theme:classy', + 'theme_global_settings', + ]); } }