core/lib/Drupal/Core/Cache/DatabaseBackend.php | 2 +- .../Core/EventSubscriber/HtmlViewSubscriber.php | 32 +------------------- .../lib/Drupal/node/Tests/NodePageCacheTest.php | 5 ++- .../system/Tests/Bootstrap/PageCacheTest.php | 1 + .../Tests/Cache/PageCacheTagsIntegrationTest.php | 17 +++++++---- 5 files changed, 18 insertions(+), 39 deletions(-) diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackend.php b/core/lib/Drupal/Core/Cache/DatabaseBackend.php index 4f98c50..0647faa 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackend.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackend.php @@ -366,7 +366,7 @@ public function garbageCollection() { * @return array * An indexed array of flattened tag identifiers. */ - protected function flattenTags(array $tags) { + public static function flattenTags(array $tags) { if (isset($tags[0])) { return $tags; } diff --git a/core/lib/Drupal/Core/EventSubscriber/HtmlViewSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/HtmlViewSubscriber.php index e5c5e57..5945a1a 100644 --- a/core/lib/Drupal/Core/EventSubscriber/HtmlViewSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/HtmlViewSubscriber.php @@ -77,7 +77,7 @@ public function onHtmlPage(GetResponseForControllerResultEvent $event) { // recommended. $response = new Response((string) $this->pageRenderer->render($page), $page->getStatusCode()); if ($tags = $page->getCacheTags()) { - $response->headers->set('X-Drupal-Cache-Tags', $this->flattenTags($tags)); + $response->headers->set('X-Drupal-Cache-Tags', implode(' ', \Drupal\Core\Cache\DatabaseBackend::flattenTags($tags))); } if ($keys = $page->getCacheKeys()) { $response->headers->set('cache_keys', serialize($keys)); @@ -93,36 +93,6 @@ public function onHtmlPage(GetResponseForControllerResultEvent $event) { } /** - * 'Flattens' a cache tags array into a space-separated flattened tags list. - * - * @param array $tags - * Associative array of tags to flatten. - * - * @return string - * A space-separated list of flattened tags. - * - * @see \Drupal\Core\Cache\DatabaseBackend::flattenTags() - */ - protected function flattenTags(array $tags) { - if (isset($tags[0])) { - return $tags; - } - - $flat_tags = array(); - foreach ($tags as $namespace => $values) { - if (is_array($values)) { - foreach ($values as $value) { - $flat_tags[] = "$namespace:$value"; - } - } - else { - $flat_tags[] = "$namespace:$values"; - } - } - return implode(' ', $flat_tags); - } - - /** * Registers the methods in this class that should be listeners. * * @return array diff --git a/core/modules/node/lib/Drupal/node/Tests/NodePageCacheTest.php b/core/modules/node/lib/Drupal/node/Tests/NodePageCacheTest.php index 0fc6ed2..70c3dc2 100644 --- a/core/modules/node/lib/Drupal/node/Tests/NodePageCacheTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/NodePageCacheTest.php @@ -59,7 +59,10 @@ public function testNodeDelete() { $cid_parts = array(url($node_path, array('absolute' => TRUE)), 'html'); $cid = sha1(implode(':', $cid_parts)); $cache_entry = \Drupal::cache('page')->get($cid); - $this->assertIdentical($cache_entry->tags, array('content:1', 'node_view:' . $node_id, 'node:' . $node_id, 'user:' . $author->id(), 'filter_format:plain_text')); + sort($cache_entry->tags); + $expected_tags = array('content:1', 'node_view:' . $node_id, 'node:' . $node_id, 'user:' . $author->id(), 'filter_format:plain_text'); + sort($expected_tags); + $this->assertIdentical($cache_entry->tags, $expected_tags); // Login and delete the node. $this->drupalLogin($this->adminUser); diff --git a/core/modules/system/lib/Drupal/system/Tests/Bootstrap/PageCacheTest.php b/core/modules/system/lib/Drupal/system/Tests/Bootstrap/PageCacheTest.php index 3ca1f19..ec596fa 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Bootstrap/PageCacheTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Bootstrap/PageCacheTest.php @@ -65,6 +65,7 @@ function testPageCacheTags() { $cid_parts = array(url($path, array('absolute' => TRUE)), 'html'); $cid = sha1(implode(':', $cid_parts)); $cache_entry = \Drupal::cache('page')->get($cid); + sort($cache_entry->tags); $this->assertIdentical($cache_entry->tags, array('content:1', 'system_test_cache_tags_page:1')); Cache::invalidateTags($tags); diff --git a/core/modules/system/lib/Drupal/system/Tests/Cache/PageCacheTagsIntegrationTest.php b/core/modules/system/lib/Drupal/system/Tests/Cache/PageCacheTagsIntegrationTest.php index 2dffc4f..1f0c39e 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Cache/PageCacheTagsIntegrationTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Cache/PageCacheTagsIntegrationTest.php @@ -66,24 +66,24 @@ function testPageCacheTags() { // Full node page 1. $this->verifyPageCacheTags('node/' . $node_1->id(), array( + 'content:1', 'node_view:1', 'node:' . $node_1->id(), 'user:' . $author_1->id(), 'filter_format:basic_html', 'menu:footer', 'menu:main', - 'content:1', )); // Full node page 2. $this->verifyPageCacheTags('node/' . $node_2->id(), array( + 'content:1', 'node_view:1', 'node:' . $node_2->id(), 'user:' . $author_2->id(), 'filter_format:full_html', 'menu:footer', 'menu:main', - 'content:1', )); } @@ -96,17 +96,22 @@ function testPageCacheTags() { * The expected cache tags for the page cache entry of the given $path. */ protected function verifyPageCacheTags($path, $expected_tags) { - $expected_tags_header = implode(' ', $expected_tags); + sort($expected_tags); $this->drupalGet($path); $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS'); - $this->assertIdentical($this->drupalGetHeader('X-Drupal-Cache-Tags'), $expected_tags_header); + $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($this->drupalGetHeader('X-Drupal-Cache-Tags'), $expected_tags_header); + $this->assertIdentical($actual_tags, $expected_tags); $cid_parts = array(url($path, array('absolute' => TRUE)), 'html'); $cid = sha1(implode(':', $cid_parts)); $cache_entry = \Drupal::cache('page')->get($cid); - $this->assertIdentical($cache_entry->tags, $expected_tags); + sort($cache_entry->tags); + $this->assertEqual($cache_entry->tags, $expected_tags); } }