diff --git a/core/modules/contact/tests/src/Functional/ContactPersonalTest.php b/core/modules/contact/tests/src/Functional/ContactPersonalTest.php index 7e6ecad7cd6..5f6e8e6ad21 100644 --- a/core/modules/contact/tests/src/Functional/ContactPersonalTest.php +++ b/core/modules/contact/tests/src/Functional/ContactPersonalTest.php @@ -6,8 +6,8 @@ use Drupal\Component\Utility\SafeMarkup; use Drupal\Component\Render\PlainTextOutput; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Test\AssertMailTrait; -use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; use Drupal\Tests\BrowserTestBase; +use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait; use Drupal\user\RoleInterface; /** diff --git a/core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php b/core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php index e69329cb5fb..c755ac4906e 100644 --- a/core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php +++ b/core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php @@ -11,7 +11,7 @@ use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Url; use Drupal\language\Entity\ConfigurableLanguage; use Drupal\Component\Utility\SafeMarkup; -use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; +use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait; /** * Tests the Content Translation UI. diff --git a/core/modules/history/tests/src/Functional/HistoryTest.php b/core/modules/history/tests/src/Functional/HistoryTest.php index 6b4708b8980..065b9af7aab 100644 --- a/core/modules/history/tests/src/Functional/HistoryTest.php +++ b/core/modules/history/tests/src/Functional/HistoryTest.php @@ -4,8 +4,8 @@ namespace Drupal\Tests\history\Functional; use Drupal\Component\Serialization\Json; use Drupal\Core\Url; -use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; use Drupal\Tests\BrowserTestBase; +use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait; use GuzzleHttp\Cookie\CookieJar; /** diff --git a/core/modules/media/tests/src/Functional/MediaAccessTest.php b/core/modules/media/tests/src/Functional/MediaAccessTest.php index 2ccf0b5cc28..070a980dfc1 100644 --- a/core/modules/media/tests/src/Functional/MediaAccessTest.php +++ b/core/modules/media/tests/src/Functional/MediaAccessTest.php @@ -3,7 +3,7 @@ namespace Drupal\Tests\media\Functional; use Drupal\media\Entity\Media; -use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; +use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait; use Drupal\user\Entity\Role; use Drupal\user\RoleInterface; diff --git a/core/modules/node/tests/src/Functional/NodeAccessAutoBubblingTest.php b/core/modules/node/tests/src/Functional/NodeAccessAutoBubblingTest.php index 6af8d8de661..20154553fc1 100644 --- a/core/modules/node/tests/src/Functional/NodeAccessAutoBubblingTest.php +++ b/core/modules/node/tests/src/Functional/NodeAccessAutoBubblingTest.php @@ -3,7 +3,7 @@ namespace Drupal\Tests\node\Functional; use Drupal\Core\Url; -use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; +use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait; /** * Tests the node access automatic cacheability bubbling logic. diff --git a/core/modules/node/tests/src/Functional/NodeBlockFunctionalTest.php b/core/modules/node/tests/src/Functional/NodeBlockFunctionalTest.php index ad3613d74f8..c203c5017a1 100644 --- a/core/modules/node/tests/src/Functional/NodeBlockFunctionalTest.php +++ b/core/modules/node/tests/src/Functional/NodeBlockFunctionalTest.php @@ -4,7 +4,7 @@ namespace Drupal\Tests\node\Functional; use Drupal\block\Entity\Block; use Drupal\Core\EventSubscriber\MainContentViewSubscriber; -use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; +use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait; use Drupal\user\RoleInterface; /** diff --git a/core/modules/page_cache/src/Tests/PageCacheTagsIntegrationTest.php b/core/modules/page_cache/src/Tests/PageCacheTagsIntegrationTest.php index 6ec8a29cf6c..be704d16edf 100644 --- a/core/modules/page_cache/src/Tests/PageCacheTagsIntegrationTest.php +++ b/core/modules/page_cache/src/Tests/PageCacheTagsIntegrationTest.php @@ -5,8 +5,8 @@ namespace Drupal\page_cache\Tests; use Drupal\Core\EventSubscriber\MainContentViewSubscriber; use Drupal\Core\Language\LanguageInterface; use Drupal\simpletest\WebTestBase; -use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; use Drupal\node\NodeInterface; +use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait; /** * Enables the page cache and tests its cache tags in various scenarios. diff --git a/core/modules/statistics/tests/src/Functional/StatisticsReportsTest.php b/core/modules/statistics/tests/src/Functional/StatisticsReportsTest.php index edebca166c1..0f169238c6a 100644 --- a/core/modules/statistics/tests/src/Functional/StatisticsReportsTest.php +++ b/core/modules/statistics/tests/src/Functional/StatisticsReportsTest.php @@ -3,7 +3,7 @@ namespace Drupal\Tests\statistics\Functional; use Drupal\Core\Cache\Cache; -use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; +use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait; /** * Tests display of statistics report blocks. diff --git a/core/modules/system/src/Tests/Cache/AssertPageCacheContextsAndTagsTrait.php b/core/modules/system/src/Tests/Cache/AssertPageCacheContextsAndTagsTrait.php index ae8379bc1ce..b115a61ee2f 100644 --- a/core/modules/system/src/Tests/Cache/AssertPageCacheContextsAndTagsTrait.php +++ b/core/modules/system/src/Tests/Cache/AssertPageCacheContextsAndTagsTrait.php @@ -9,6 +9,12 @@ use Drupal\Core\Url; * Provides test assertions for testing page-level cache contexts & tags. * * Can be used by test classes that extend \Drupal\simpletest\WebTestBase. + * + * @deprecated Scheduled for removal in Drupal 9.0.0. Use + * \Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait + * instead. + * + * @see https://www.drupal.org/node/2896632 */ trait AssertPageCacheContextsAndTagsTrait { diff --git a/core/modules/system/tests/src/Functional/Cache/AssertPageCacheContextsAndTagsTrait.php b/core/modules/system/tests/src/Functional/Cache/AssertPageCacheContextsAndTagsTrait.php new file mode 100644 index 00000000000..e0450d1660a --- /dev/null +++ b/core/modules/system/tests/src/Functional/Cache/AssertPageCacheContextsAndTagsTrait.php @@ -0,0 +1,192 @@ +config('system.performance'); + $config->set('cache.page.max_age', 300); + $config->save(); + } + + /** + * Gets a specific header value as array. + * + * @param string $header_name + * The header name. + * + * @return string[] + * The header value, potentially exploded by spaces. + */ + protected function getCacheHeaderValues($header_name) { + $header_value = $this->drupalGetHeader($header_name); + if (empty($header_value)) { + return []; + } + else { + return explode(' ', $header_value); + } + } + + /** + * Asserts whether an expected cache context was present in the last response. + * + * @param string $expected_cache_context + * The expected cache context. + */ + protected function assertCacheContext($expected_cache_context) { + $cache_contexts = explode(' ', $this->drupalGetHeader('X-Drupal-Cache-Contexts')); + $this->assertTrue(in_array($expected_cache_context, $cache_contexts), "'" . $expected_cache_context . "' is present in the X-Drupal-Cache-Contexts header."); + } + + /** + * Asserts that a cache context was not present in the last response. + * + * @param string $not_expected_cache_context + * The expected cache context. + */ + protected function assertNoCacheContext($not_expected_cache_context) { + $cache_contexts = explode(' ', $this->drupalGetHeader('X-Drupal-Cache-Contexts')); + $this->assertFalse(in_array($not_expected_cache_context, $cache_contexts), "'" . $not_expected_cache_context . "' is not present in the X-Drupal-Cache-Contexts header."); + } + + /** + * Asserts page cache miss, then hit for the given URL; checks cache headers. + * + * @param \Drupal\Core\Url $url + * The URL to test. + * @param string[] $expected_contexts + * The expected cache contexts for the given URL. + * @param string[] $expected_tags + * The expected cache tags for the given URL. + */ + protected function assertPageCacheContextsAndTags(Url $url, array $expected_contexts, array $expected_tags) { + $absolute_url = $url->setAbsolute()->toString(); + sort($expected_contexts); + sort($expected_tags); + + // Assert cache miss + expected cache contexts + tags. + $this->drupalGet($absolute_url); + $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS'); + $this->assertCacheTags($expected_tags); + $this->assertCacheContexts($expected_contexts); + + // Assert cache hit + expected cache contexts + tags. + $this->drupalGet($absolute_url); + $this->assertCacheTags($expected_tags); + $this->assertCacheContexts($expected_contexts); + + // Assert page cache item + expected cache tags. + $cid_parts = [$url->setAbsolute()->toString(), 'html']; + $cid = implode(':', $cid_parts); + $cache_entry = \Drupal::cache('page')->get($cid); + sort($cache_entry->tags); + $this->assertEqual($cache_entry->tags, $expected_tags); + $this->debugCacheTags($cache_entry->tags, $expected_tags); + } + + /** + * Provides debug information for cache tags. + * + * @param string[] $actual_tags + * The actual cache tags. + * @param string[] $expected_tags + * The expected cache tags. + */ + protected function debugCacheTags(array $actual_tags, array $expected_tags) { + if ($actual_tags !== $expected_tags) { + debug('Unwanted cache tags in response: ' . implode(',', array_diff($actual_tags, $expected_tags))); + debug('Missing cache tags in response: ' . implode(',', array_diff($expected_tags, $actual_tags))); + } + } + + /** + * Ensures that some cache tags are present in the current response. + * + * @param string[] $expected_tags + * The expected tags. + * @param bool $include_default_tags + * (optional) Whether the default cache tags should be included. + */ + protected function assertCacheTags(array $expected_tags, $include_default_tags = TRUE) { + // The anonymous role cache tag is only added if the user is anonymous. + if ($include_default_tags) { + if (\Drupal::currentUser()->isAnonymous()) { + $expected_tags = Cache::mergeTags($expected_tags, ['config:user.role.anonymous']); + } + $expected_tags[] = 'http_response'; + } + $actual_tags = $this->getCacheHeaderValues('X-Drupal-Cache-Tags'); + $expected_tags = array_unique($expected_tags); + sort($expected_tags); + sort($actual_tags); + $this->assertIdentical($actual_tags, $expected_tags); + $this->debugCacheTags($actual_tags, $expected_tags); + } + + /** + * Ensures that some cache contexts are present in the current response. + * + * @param string[] $expected_contexts + * The expected cache contexts. + * @param string $message + * (optional) A verbose message to output. + * @param bool $include_default_contexts + * (optional) Whether the default contexts should automatically be included. + * + * @return bool + * TRUE if the assertion succeeded, FALSE otherwise. + */ + protected function assertCacheContexts(array $expected_contexts, $message = NULL, $include_default_contexts = TRUE) { + if ($include_default_contexts) { + $default_contexts = ['languages:language_interface', 'theme']; + // Add the user.permission context to the list of default contexts except + // when user is already there. + if (!in_array('user', $expected_contexts)) { + $default_contexts[] = 'user.permissions'; + } + $expected_contexts = Cache::mergeContexts($expected_contexts, $default_contexts); + } + + $actual_contexts = $this->getCacheHeaderValues('X-Drupal-Cache-Contexts'); + sort($expected_contexts); + sort($actual_contexts); + $match = $actual_contexts === $expected_contexts; + if (!$match) { + debug('Unwanted cache contexts in response: ' . implode(',', array_diff($actual_contexts, $expected_contexts))); + debug('Missing cache contexts in response: ' . implode(',', array_diff($expected_contexts, $actual_contexts))); + } + + $this->assertIdentical($actual_contexts, $expected_contexts, $message); + + // For compatibility with both BrowserTestBase and WebTestBase always return + // a boolean. + return $match; + } + + /** + * Asserts the max age header. + * + * @param int $max_age + */ + protected function assertCacheMaxAge($max_age) { + $cache_control_header = $this->drupalGetHeader('Cache-Control'); + if (strpos($cache_control_header, 'max-age:' . $max_age) === FALSE) { + debug('Expected max-age:' . $max_age . '; Response max-age:' . $cache_control_header); + } + $this->assertTrue(strpos($cache_control_header, 'max-age:' . $max_age)); + } + +} diff --git a/core/modules/system/src/Tests/Cache/SessionExistsCacheContextTest.php b/core/modules/system/tests/src/Functional/Cache/SessionExistsCacheContextTest.php similarity index 82% rename from core/modules/system/src/Tests/Cache/SessionExistsCacheContextTest.php rename to core/modules/system/tests/src/Functional/Cache/SessionExistsCacheContextTest.php index 8815bf340f3..78fb983a612 100644 --- a/core/modules/system/src/Tests/Cache/SessionExistsCacheContextTest.php +++ b/core/modules/system/tests/src/Functional/Cache/SessionExistsCacheContextTest.php @@ -1,16 +1,16 @@ cookies, function ($item) { return $item['value'] !== 'deleted'; }); - $this->assertEqual($expected_present, isset($non_deleted_cookies[$this->getSessionName()]), 'Session cookie exists.'); + $this->assertEqual($expected_present, (bool) $this->getSession()->getCookie($this->getSessionName()), 'Session cookie exists.'); } } diff --git a/core/modules/toolbar/tests/src/Functional/ToolbarCacheContextsTest.php b/core/modules/toolbar/tests/src/Functional/ToolbarCacheContextsTest.php index 2f6f92da817..5e4a0a257e4 100644 --- a/core/modules/toolbar/tests/src/Functional/ToolbarCacheContextsTest.php +++ b/core/modules/toolbar/tests/src/Functional/ToolbarCacheContextsTest.php @@ -4,7 +4,7 @@ namespace Drupal\Tests\toolbar\Functional; use Drupal\Core\Cache\Cache; use Drupal\Core\EventSubscriber\MainContentViewSubscriber; -use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; +use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait; use Drupal\Tests\BrowserTestBase; diff --git a/core/modules/tracker/tests/src/Functional/TrackerTest.php b/core/modules/tracker/tests/src/Functional/TrackerTest.php index 17f95ee50da..1a77d95ac89 100644 --- a/core/modules/tracker/tests/src/Functional/TrackerTest.php +++ b/core/modules/tracker/tests/src/Functional/TrackerTest.php @@ -9,8 +9,8 @@ use Drupal\Core\EventSubscriber\MainContentViewSubscriber; use Drupal\Core\Session\AccountInterface; use Drupal\field\Entity\FieldStorageConfig; use Drupal\node\Entity\Node; -use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; use Drupal\Tests\BrowserTestBase; +use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait; /** * Create and delete nodes and check for their display in the tracker listings. diff --git a/core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php b/core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php index 123a9cb5a28..b01e9854503 100644 --- a/core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php +++ b/core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php @@ -3,7 +3,7 @@ namespace Drupal\views\Tests; use Drupal\Core\Cache\Cache; -use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; +use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait; use Drupal\views\Plugin\views\display\DisplayPluginBase; use Drupal\views\ViewExecutable; use Symfony\Component\HttpFoundation\Request; diff --git a/core/modules/views/tests/src/Functional/Handler/FieldWebTest.php b/core/modules/views/tests/src/Functional/Handler/FieldWebTest.php index 318be3f902c..a3646f0c0b3 100644 --- a/core/modules/views/tests/src/Functional/Handler/FieldWebTest.php +++ b/core/modules/views/tests/src/Functional/Handler/FieldWebTest.php @@ -7,7 +7,7 @@ use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Render\RenderContext; use Drupal\Core\Url; -use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; +use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait; use Drupal\Tests\views\Functional\ViewTestBase; use Drupal\views\Views; diff --git a/core/modules/views/tests/src/Functional/Plugin/CacheWebTest.php b/core/modules/views/tests/src/Functional/Plugin/CacheWebTest.php index 40bf60189cb..f8ce5a927f1 100644 --- a/core/modules/views/tests/src/Functional/Plugin/CacheWebTest.php +++ b/core/modules/views/tests/src/Functional/Plugin/CacheWebTest.php @@ -2,7 +2,7 @@ namespace Drupal\Tests\views\Functional\Plugin; -use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; +use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait; use Drupal\Tests\views\Functional\ViewTestBase; use Drupal\views\Plugin\views\display\DisplayPluginBase; use Drupal\views\Views; diff --git a/core/modules/views/tests/src/Functional/Plugin/ContextualFiltersBlockContextTest.php b/core/modules/views/tests/src/Functional/Plugin/ContextualFiltersBlockContextTest.php index 567e1c930a6..215ee2890f5 100644 --- a/core/modules/views/tests/src/Functional/Plugin/ContextualFiltersBlockContextTest.php +++ b/core/modules/views/tests/src/Functional/Plugin/ContextualFiltersBlockContextTest.php @@ -3,9 +3,9 @@ namespace Drupal\Tests\views\Functional\Plugin; use Drupal\Core\Plugin\Context\ContextDefinitionInterface; +use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait; use Drupal\Tests\views\Functional\ViewTestBase; use Drupal\views\Tests\ViewTestData; -use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; /** * A test for contextual filters exposed as block context. diff --git a/core/modules/views/tests/src/Functional/Plugin/DisplayPageWebTest.php b/core/modules/views/tests/src/Functional/Plugin/DisplayPageWebTest.php index 3d2aca6639f..501d9226eed 100644 --- a/core/modules/views/tests/src/Functional/Plugin/DisplayPageWebTest.php +++ b/core/modules/views/tests/src/Functional/Plugin/DisplayPageWebTest.php @@ -2,7 +2,7 @@ namespace Drupal\Tests\views\Functional\Plugin; -use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; +use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait; use Drupal\Tests\views\Functional\ViewTestBase; use Drupal\views\Views; diff --git a/core/modules/views/tests/src/Functional/Plugin/ExposedFormTest.php b/core/modules/views/tests/src/Functional/Plugin/ExposedFormTest.php index 38b7327b4d8..134f7b54586 100644 --- a/core/modules/views/tests/src/Functional/Plugin/ExposedFormTest.php +++ b/core/modules/views/tests/src/Functional/Plugin/ExposedFormTest.php @@ -4,7 +4,7 @@ namespace Drupal\Tests\views\Functional\Plugin; use Drupal\Component\Utility\Html; use Drupal\entity_test\Entity\EntityTest; -use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; +use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait; use Drupal\Tests\views\Functional\ViewTestBase; use Drupal\views\ViewExecutable; use Drupal\views\Views; diff --git a/core/modules/views/tests/src/Functional/Plugin/PagerTest.php b/core/modules/views/tests/src/Functional/Plugin/PagerTest.php index eda88916d66..8085256df0f 100644 --- a/core/modules/views/tests/src/Functional/Plugin/PagerTest.php +++ b/core/modules/views/tests/src/Functional/Plugin/PagerTest.php @@ -2,7 +2,7 @@ namespace Drupal\Tests\views\Functional\Plugin; -use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; +use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait; use Drupal\Tests\views\Functional\ViewTestBase; use Drupal\views\Views; use Drupal\language\Entity\ConfigurableLanguage; diff --git a/core/modules/views_ui/tests/src/Functional/DisplayPathTest.php b/core/modules/views_ui/tests/src/Functional/DisplayPathTest.php index f8e58e719c7..af2c4e2a6f0 100644 --- a/core/modules/views_ui/tests/src/Functional/DisplayPathTest.php +++ b/core/modules/views_ui/tests/src/Functional/DisplayPathTest.php @@ -4,7 +4,7 @@ namespace Drupal\Tests\views_ui\Functional; use Drupal\Core\Menu\MenuTreeParameters; use Drupal\menu_link_content\Entity\MenuLinkContent; -use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; +use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait; /** * Tests the UI of generic display path plugin. @@ -13,6 +13,7 @@ use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; * @see \Drupal\views\Plugin\views\display\PathPluginBase */ class DisplayPathTest extends UITestBase { + use AssertPageCacheContextsAndTagsTrait; protected function setUp($import_test_views = TRUE) {