first commit

This commit is contained in:
2024-07-15 12:33:27 +02:00
commit ce50ae282b
22084 changed files with 2623791 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
name: 'Internal Dynamic Page Cache'
type: module
description: 'Caches pages, including those with dynamic content, for all users.'
package: Core
# version: VERSION
# Information added by Drupal.org packaging script on 2024-07-04
version: '10.3.1'
project: 'drupal'
datestamp: 1720094222

View File

@@ -0,0 +1,27 @@
<?php
/**
* @file
* Caches responses for all users, handling dynamic content correctly.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function dynamic_page_cache_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.dynamic_page_cache':
$output = '<h2>' . t('About') . '</h2>';
$output .= '<p>' . t('The Internal Dynamic Page Cache module caches pages for all users in the database, handling dynamic content correctly. For more information, see the <a href=":dynamic_page_cache-documentation">online documentation for the Internal Dynamic Page Cache module</a>.', [':dynamic_page_cache-documentation' => 'https://www.drupal.org/documentation/modules/dynamic_page_cache']) . '</p>';
$output .= '<h2>' . t('Uses') . '</h2>';
$output .= '<dl>';
$output .= '<dt>' . t('Speeding up your site') . '</dt>';
$output .= '<dd>' . t('Pages which are suitable for caching are cached the first time they are requested, then the cached version is served for all later requests. Dynamic content is handled automatically so that both cache correctness and hit ratio is maintained.') . '</dd>';
$output .= '<dd>' . t('The module requires no configuration. Every part of the page contains metadata that allows Internal Dynamic Page Cache to figure this out on its own.') . '</dd>';
$output .= '</dl>';
return $output;
}
}

View File

@@ -0,0 +1,33 @@
services:
_defaults:
autoconfigure: true
cache.dynamic_page_cache:
class: Drupal\Core\Cache\CacheBackendInterface
tags:
- { name: cache.bin }
factory: ['@cache_factory', 'get']
arguments: [dynamic_page_cache]
variation_cache.dynamic_page_cache:
class: Drupal\Core\Cache\VariationCacheInterface
factory: ['@variation_cache_factory', 'get']
arguments: [dynamic_page_cache]
dynamic_page_cache_subscriber:
class: Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber
arguments: ['@dynamic_page_cache_request_policy', '@dynamic_page_cache_response_policy', '@variation_cache.dynamic_page_cache', '@cache_contexts_manager', '%renderer.config%']
# Request & response policies.
dynamic_page_cache_request_policy:
class: Drupal\dynamic_page_cache\PageCache\RequestPolicy\DefaultRequestPolicy
tags:
- { name: service_collector, tag: dynamic_page_cache_request_policy, call: addPolicy}
dynamic_page_cache_response_policy:
class: Drupal\Core\PageCache\ChainResponsePolicy
tags:
- { name: service_collector, tag: dynamic_page_cache_response_policy, call: addPolicy}
lazy: true
dynamic_page_cache_deny_admin_routes:
class: Drupal\dynamic_page_cache\PageCache\ResponsePolicy\DenyAdminRoutes
arguments: ['@current_route_match']
public: false
tags:
- { name: dynamic_page_cache_response_policy }

View File

@@ -0,0 +1,285 @@
<?php
namespace Drupal\dynamic_page_cache\EventSubscriber;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\CacheableResponseInterface;
use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\Core\Cache\VariationCacheInterface;
use Drupal\Core\PageCache\RequestPolicyInterface;
use Drupal\Core\PageCache\ResponsePolicyInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Returns cached responses as early and avoiding as much work as possible.
*
* Dynamic Page Cache is able to cache so much because it utilizes cache
* contexts: the cache contexts that are present capture the variations of every
* component of the page. That, combined with the fact that cacheability
* metadata is bubbled, means that the cache contexts at the page level
* represent the complete set of contexts that the page varies by.
*
* The reason Dynamic Page Cache is implemented as two event subscribers (a late
* REQUEST subscriber immediately after routing for cache hits, and an early
* RESPONSE subscriber for cache misses) is because many cache contexts can only
* be evaluated after routing. (Examples: 'user', 'user.permissions', 'route' )
* Consequently, it is impossible to implement Dynamic Page Cache as a kernel
* middleware that simply caches per URL.
*
* @see \Drupal\Core\Render\MainContent\HtmlRenderer
* @see \Drupal\Core\Cache\CacheableResponseInterface
*/
class DynamicPageCacheSubscriber implements EventSubscriberInterface {
/**
* Name of Dynamic Page Cache's response header.
*/
const HEADER = 'X-Drupal-Dynamic-Cache';
/**
* A request policy rule determining the cacheability of a response.
*
* @var \Drupal\Core\PageCache\RequestPolicyInterface
*/
protected $requestPolicy;
/**
* A response policy rule determining the cacheability of the response.
*
* @var \Drupal\Core\PageCache\ResponsePolicyInterface
*/
protected $responsePolicy;
/**
* The variation cache.
*
* @var \Drupal\Core\Cache\VariationCacheInterface
*/
protected $cache;
/**
* The default cache contexts to vary every cache item by.
*
* @var string[]
*/
protected $cacheContexts = [
'route',
// Some routes' controllers rely on the request format (they don't have
// a separate route for each request format). Additionally, a controller
// may be returning a domain object that a KernelEvents::VIEW subscriber
// must turn into an actual response, but perhaps a format is being
// requested that the subscriber does not support.
// @see \Drupal\Core\EventSubscriber\RenderArrayNonHtmlSubscriber::onResponse()
'request_format',
];
/**
* The cache contexts manager service.
*
* @var \Drupal\Core\Cache\Context\CacheContextsManager
*/
protected $cacheContextsManager;
/**
* The renderer configuration array.
*
* @var array
*/
protected $rendererConfig;
/**
* Internal cache of request policy results.
*
* @var \SplObjectStorage
*/
protected $requestPolicyResults;
/**
* Constructs a new DynamicPageCacheSubscriber object.
*
* @param \Drupal\Core\PageCache\RequestPolicyInterface $request_policy
* A policy rule determining the cacheability of a request.
* @param \Drupal\Core\PageCache\ResponsePolicyInterface $response_policy
* A policy rule determining the cacheability of the response.
* @param \Drupal\Core\Cache\VariationCacheInterface $cache
* The variation cache.
* @param \Drupal\Core\Cache\Context\CacheContextsManager $cache_contexts_manager
* The cache contexts manager service.
* @param array $renderer_config
* The renderer configuration array.
*/
public function __construct(RequestPolicyInterface $request_policy, ResponsePolicyInterface $response_policy, VariationCacheInterface $cache, CacheContextsManager $cache_contexts_manager, array $renderer_config) {
$this->requestPolicy = $request_policy;
$this->responsePolicy = $response_policy;
$this->cache = $cache;
$this->cacheContextsManager = $cache_contexts_manager;
$this->rendererConfig = $renderer_config;
$this->requestPolicyResults = new \SplObjectStorage();
}
/**
* Sets a response in case of a Dynamic Page Cache hit.
*
* @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
* The event to process.
*/
public function onRequest(RequestEvent $event) {
// Don't cache the response if the Dynamic Page Cache request policies are
// not met. Store the result in a static keyed by current request, so that
// onResponse() does not have to redo the request policy check.
$request = $event->getRequest();
$request_policy_result = $this->requestPolicy->check($request);
$this->requestPolicyResults[$request] = $request_policy_result;
if ($request_policy_result === RequestPolicyInterface::DENY) {
return;
}
// Sets the response for the current route, if cached.
$cached = $this->cache->get(['response'], (new CacheableMetadata())->setCacheContexts($this->cacheContexts));
if ($cached) {
$response = $cached->data;
$response->headers->set(self::HEADER, 'HIT');
$event->setResponse($response);
}
}
/**
* Stores a response in case of a Dynamic Page Cache miss, if cacheable.
*
* @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
* The event to process.
*/
public function onResponse(ResponseEvent $event) {
$response = $event->getResponse();
// Dynamic Page Cache only works with cacheable responses. It does not work
// with plain Response objects. (Dynamic Page Cache needs to be able to
// access and modify the cacheability metadata associated with the
// response.)
if (!$response instanceof CacheableResponseInterface) {
return;
}
// There's no work left to be done if this is a Dynamic Page Cache hit.
if ($response->headers->get(self::HEADER) === 'HIT') {
return;
}
// There's no work left to be done if this is an uncacheable response.
if (!$this->shouldCacheResponse($response)) {
// The response is uncacheable, mark it as such.
$response->headers->set(self::HEADER, 'UNCACHEABLE');
return;
}
// Don't cache the response if Dynamic Page Cache's request subscriber did
// not fire, because that means it is impossible to have a Dynamic Page
// Cache hit. This can happen when the master request is for example a 403
// or 404, in which case a subrequest is performed by the router. In that
// case, it is the subrequest's response that is cached by Dynamic Page
// Cache, because the routing happens in a request subscriber earlier than
// Dynamic Page Cache's and immediately sets a response, i.e. the one
// returned by the subrequest, and thus causes Dynamic Page Cache's request
// subscriber to not fire for the master request.
// @see \Drupal\Core\Routing\AccessAwareRouter::checkAccess()
// @see \Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber::on403()
$request = $event->getRequest();
if (!isset($this->requestPolicyResults[$request])) {
return;
}
// Don't cache the response if the Dynamic Page Cache request & response
// policies are not met.
// @see onRequest()
if ($this->requestPolicyResults[$request] === RequestPolicyInterface::DENY || $this->responsePolicy->check($response, $request) === ResponsePolicyInterface::DENY) {
return;
}
$cacheable_metadata = CacheableMetadata::createFromObject($response->getCacheableMetadata());
$this->cache->set(
['response'],
$response,
$cacheable_metadata->addCacheContexts($this->cacheContexts),
(new CacheableMetadata())->setCacheContexts($this->cacheContexts)
);
// The response was generated, mark the response as a cache miss. The next
// time, it will be a cache hit.
$response->headers->set(self::HEADER, 'MISS');
}
/**
* Whether the given response should be cached by Dynamic Page Cache.
*
* We consider any response that has cacheability metadata meeting the auto-
* placeholdering conditions to be uncacheable. Because those conditions
* indicate poor cacheability, and if it doesn't make sense to cache parts of
* a page, then neither does it make sense to cache an entire page.
*
* But note that auto-placeholdering avoids such cacheability metadata ever
* bubbling to the response level: while rendering, the Renderer checks every
* subtree to see if meets the auto-placeholdering conditions. If it does, it
* is automatically placeholdered, and consequently the cacheability metadata
* of the placeholdered content does not bubble up to the response level.
*
* @param \Drupal\Core\Cache\CacheableResponseInterface $response
* The response whose cacheability to analyze.
*
* @return bool
* Whether the given response should be cached.
*
* @see \Drupal\Core\Render\Renderer::shouldAutomaticallyPlaceholder()
*/
protected function shouldCacheResponse(CacheableResponseInterface $response) {
$conditions = $this->rendererConfig['auto_placeholder_conditions'];
// Create a new CacheableMetadata to avoid changing the response itself.
$cacheability = CacheableMetadata::createFromObject($response->getCacheableMetadata());
// Response's max-age is at or below the configured threshold.
if ($cacheability->getCacheMaxAge() !== Cache::PERMANENT && $cacheability->getCacheMaxAge() <= $conditions['max-age']) {
return FALSE;
}
// Optimize the contexts and let them affect the cache tags to mimic what
// happens to the cacheability in the variation cache.
$cacheability->addCacheableDependency($this->cacheContextsManager->convertTokensToKeys($cacheability->getCacheContexts()));
$cacheability->setCacheContexts($this->cacheContextsManager->optimizeTokens($cacheability->getCacheContexts()));
// Response has a high-cardinality cache context.
if (array_intersect($cacheability->getCacheContexts(), $conditions['contexts'])) {
return FALSE;
}
// Response has a high-invalidation frequency cache tag.
if (array_intersect($cacheability->getCacheTags(), $conditions['tags'])) {
return FALSE;
}
return TRUE;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
$events = [];
// Run after AuthenticationSubscriber (necessary for the 'user' cache
// context; priority 300) and MaintenanceModeSubscriber (Dynamic Page Cache
// should not be polluted by maintenance mode-specific behavior; priority
// 30), but before ContentControllerSubscriber (updates _controller, but
// that is a no-op when Dynamic Page Cache runs; priority 25).
$events[KernelEvents::REQUEST][] = ['onRequest', 27];
// Run before HtmlResponseSubscriber::onRespond(), which has priority 0.
$events[KernelEvents::RESPONSE][] = ['onResponse', 100];
return $events;
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Drupal\dynamic_page_cache\PageCache\RequestPolicy;
use Drupal\Core\PageCache\ChainRequestPolicy;
use Drupal\Core\PageCache\RequestPolicy\CommandLineOrUnsafeMethod;
/**
* The default Dynamic Page Cache request policy.
*
* Delivery of cached pages is denied if either the application is running from
* the command line or the request was not initiated with a safe method (GET or
* HEAD).
*/
class DefaultRequestPolicy extends ChainRequestPolicy {
/**
* Constructs the default Dynamic Page Cache request policy.
*/
public function __construct() {
$this->addPolicy(new CommandLineOrUnsafeMethod());
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace Drupal\dynamic_page_cache\PageCache\ResponsePolicy;
use Drupal\Core\PageCache\ResponsePolicyInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Cache policy for routes with the '_admin_route' option set.
*
* This policy rule denies caching of responses generated for admin routes,
* because admin routes have very low cache hit ratios due to low traffic and
* form submissions.
*/
class DenyAdminRoutes implements ResponsePolicyInterface {
/**
* The current route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* Constructs a deny admin route page cache policy.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
*/
public function __construct(RouteMatchInterface $route_match) {
$this->routeMatch = $route_match;
}
/**
* {@inheritdoc}
*/
public function check(Response $response, Request $request) {
if (($route = $this->routeMatch->getRouteObject()) && $route->getOption('_admin_route')) {
return static::DENY;
}
}
}

View File

@@ -0,0 +1,10 @@
name: 'Test Dynamic Page Cache'
type: module
description: 'Provides test routes/responses for Dynamic Page Cache.'
package: Testing
# version: VERSION
# Information added by Drupal.org packaging script on 2024-07-04
version: '10.3.1'
project: 'drupal'
datestamp: 1720094222

View File

@@ -0,0 +1,75 @@
dynamic_page_cache_test.response:
path: '/dynamic-page-cache-test/response'
defaults:
_controller: '\Drupal\dynamic_page_cache_test\DynamicPageCacheTestController::response'
requirements:
_access: 'TRUE'
dynamic_page_cache_test.response.admin:
path: '/dynamic-page-cache-test/response/admin'
defaults:
_controller: '\Drupal\dynamic_page_cache_test\DynamicPageCacheTestController::response'
requirements:
_access: 'TRUE'
options:
_admin_route: TRUE
dynamic_page_cache_test.cacheable_response:
path: '/dynamic-page-cache-test/cacheable-response'
defaults:
_controller: '\Drupal\dynamic_page_cache_test\DynamicPageCacheTestController::cacheableResponse'
requirements:
_access: 'TRUE'
dynamic_page_cache_test.cacheable_response.admin:
path: '/dynamic-page-cache-test/cacheable-response/admin'
defaults:
_controller: '\Drupal\dynamic_page_cache_test\DynamicPageCacheTestController::cacheableResponse'
requirements:
_access: 'TRUE'
options:
_admin_route: TRUE
dynamic_page_cache_test.html:
path: '/dynamic-page-cache-test/html'
defaults:
_controller: '\Drupal\dynamic_page_cache_test\DynamicPageCacheTestController::html'
requirements:
_access: 'TRUE'
dynamic_page_cache_test.html.admin:
path: '/dynamic-page-cache-test/html/admin'
defaults:
_controller: '\Drupal\dynamic_page_cache_test\DynamicPageCacheTestController::html'
requirements:
_access: 'TRUE'
options:
_admin_route: TRUE
dynamic_page_cache_test.html.with_cache_contexts:
path: '/dynamic-page-cache-test/html/with-cache-contexts'
defaults:
_controller: '\Drupal\dynamic_page_cache_test\DynamicPageCacheTestController::htmlWithCacheContexts'
requirements:
_access: 'TRUE'
dynamic_page_cache_test.html.uncacheable.max_age:
path: '/dynamic-page-cache-test/html/uncacheable/max-age'
defaults:
_controller: '\Drupal\dynamic_page_cache_test\DynamicPageCacheTestController::htmlUncacheableMaxAge'
requirements:
_access: 'TRUE'
dynamic_page_cache_test.html.uncacheable.contexts:
path: '/dynamic-page-cache-test/html/uncacheable/contexts'
defaults:
_controller: '\Drupal\dynamic_page_cache_test\DynamicPageCacheTestController::htmlUncacheableContexts'
requirements:
_access: 'TRUE'
dynamic_page_cache_test.html.uncacheable.tags:
path: '/dynamic-page-cache-test/html/uncacheable/tags'
defaults:
_controller: '\Drupal\dynamic_page_cache_test\DynamicPageCacheTestController::htmlUncacheableTags'
requirements:
_access: 'TRUE'

View File

@@ -0,0 +1,140 @@
<?php
namespace Drupal\dynamic_page_cache_test;
use Drupal\Core\Cache\CacheableResponse;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\user\Entity\User;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Controller routines for dynamic_page_cache_test routes.
*/
class DynamicPageCacheTestController {
use StringTranslationTrait;
/**
* A route returning a Response object.
*
* @return \Symfony\Component\HttpFoundation\Response
* A Response object.
*/
public function response() {
return new Response('foobar');
}
/**
* A route returning a CacheableResponse object.
*
* @return \Drupal\Core\Cache\CacheableResponseInterface
* A CacheableResponseInterface object.
*/
public function cacheableResponse() {
$user = User::load(1);
$response = new CacheableResponse($user->label());
$response->addCacheableDependency($user);
return $response;
}
/**
* A route returning a render array (without cache contexts, so cacheable).
*
* @return array
* A render array.
*/
public function html() {
return [
'content' => [
'#markup' => 'Hello world.',
],
];
}
/**
* A route returning a render array (with cache contexts, so cacheable).
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request.
*
* @return array
* A render array.
*
* @see html()
*/
public function htmlWithCacheContexts(Request $request) {
$build = $this->html();
$build['dynamic_part'] = [
'#markup' => $this->t('Hello there, %animal.', ['%animal' => $request->query->get('animal')]),
'#cache' => [
'contexts' => [
'url.query_args:animal',
],
],
];
return $build;
}
/**
* A route returning a render array (with max-age=0, so uncacheable)
*
* @return array
* A render array.
*
* @see html()
*/
public function htmlUncacheableMaxAge() {
$build = $this->html();
$build['very_dynamic_part'] = [
'#markup' => 'Drupal cannot handle the awesomeness of llamas.',
'#cache' => [
'max-age' => 0,
],
];
return $build;
}
/**
* A route returning a render array (with 'user' context, so uncacheable)
*
* @return array
* A render array.
*
* @see html()
*/
public function htmlUncacheableContexts() {
$build = $this->html();
$build['very_dynamic_part'] = [
'#markup' => $this->t('@username cannot handle the awesomeness of llamas.', ['@username' => \Drupal::currentUser()->getDisplayName()]),
'#cache' => [
'contexts' => [
'user',
],
],
];
return $build;
}
/**
* A route returning a render array (with a cache tag preventing caching).
*
* @return array
* A render array.
*
* @see html()
*/
public function htmlUncacheableTags() {
$build = $this->html();
$build['very_dynamic_part'] = [
'#markup' => 'Drupal cannot handle the awesomeness of llamas.',
'#cache' => [
'tags' => [
'current-temperature',
],
],
];
return $build;
}
}

View File

@@ -0,0 +1,128 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\dynamic_page_cache\Functional;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\Core\Url;
use Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber;
use Drupal\Tests\BrowserTestBase;
/**
* Enables the Dynamic Page Cache and tests it in various scenarios.
*
* This does not test the self-healing of the redirect with conditional cache
* contexts, because Dynamic Page Cache just reuses
* \Drupal\Core\Render\RenderCache so that it doesn't have to implement and test
* all of that again. It is tested in
* RendererBubblingTest::testConditionalCacheContextBubblingSelfHealing().
*
* @group dynamic_page_cache
*
* @see \Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber
*/
class DynamicPageCacheIntegrationTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['dynamic_page_cache_test'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Uninstall the page_cache module; we want to test the Dynamic Page Cache
// alone.
\Drupal::service('module_installer')->uninstall(['page_cache']);
}
/**
* Tests that Dynamic Page Cache works correctly, and verifies the edge cases.
*/
public function testDynamicPageCache(): void {
// Controllers returning plain response objects are ignored by Dynamic Page
// Cache.
$url = Url::fromUri('route:dynamic_page_cache_test.response');
$this->drupalGet($url);
$this->assertSession()->responseHeaderDoesNotExist(DynamicPageCacheSubscriber::HEADER);
// Controllers returning CacheableResponseInterface (cacheable response)
// objects are handled by Dynamic Page Cache.
$url = Url::fromUri('route:dynamic_page_cache_test.cacheable_response');
$this->drupalGet($url);
$this->assertSession()->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'MISS');
$this->drupalGet($url);
$this->assertSession()->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'HIT');
// Controllers returning render arrays, rendered as HTML responses, are
// handled by Dynamic Page Cache.
$url = Url::fromUri('route:dynamic_page_cache_test.html');
$this->drupalGet($url);
$this->assertSession()->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'MISS');
$this->drupalGet($url);
$this->assertSession()->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'HIT');
// The above is the simple case, where the render array returned by the
// response contains no cache contexts. So let's now test a route/controller
// that *does* vary by a cache context whose value we can easily control: it
// varies by the 'animal' query argument.
foreach (['llama', 'piggy', 'unicorn', 'kitten'] as $animal) {
$url = Url::fromUri('route:dynamic_page_cache_test.html.with_cache_contexts', ['query' => ['animal' => $animal]]);
$this->drupalGet($url);
$this->assertSession()->pageTextContains($animal);
$this->assertSession()->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'MISS');
$this->drupalGet($url);
$this->assertSession()->pageTextContains($animal);
$this->assertSession()->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'HIT');
// Finally, let's also verify that the 'dynamic_page_cache_test.html'
// route continued to see cache hits if we specify a query argument,
// because it *should* ignore it and continue to provide Dynamic Page
// Cache hits.
$url = Url::fromUri('route:dynamic_page_cache_test.html', ['query' => ['animal' => 'piglet']]);
$this->drupalGet($url);
$this->assertSession()->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'HIT');
}
// Controllers returning render arrays, rendered as anything except an HTML
// response, are ignored by Dynamic Page Cache (but only because those
// wrapper formats' responses do not implement CacheableResponseInterface).
$this->drupalGet('dynamic-page-cache-test/html', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']]);
$this->assertSession()->responseHeaderDoesNotExist(DynamicPageCacheSubscriber::HEADER);
$this->drupalGet('dynamic-page-cache-test/html', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_dialog']]);
$this->assertSession()->responseHeaderDoesNotExist(DynamicPageCacheSubscriber::HEADER);
$this->drupalGet('dynamic-page-cache-test/html', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_modal']]);
$this->assertSession()->responseHeaderDoesNotExist(DynamicPageCacheSubscriber::HEADER);
// Admin routes are ignored by Dynamic Page Cache.
$this->drupalGet('dynamic-page-cache-test/html/admin');
$this->assertSession()->responseHeaderDoesNotExist(DynamicPageCacheSubscriber::HEADER);
$this->drupalGet('dynamic-page-cache-test/response/admin');
$this->assertSession()->responseHeaderDoesNotExist(DynamicPageCacheSubscriber::HEADER);
$this->drupalGet('dynamic-page-cache-test/cacheable-response/admin');
$this->assertSession()->responseHeaderDoesNotExist(DynamicPageCacheSubscriber::HEADER);
// Max-age = 0 responses are ignored by Dynamic Page Cache.
$this->drupalGet('dynamic-page-cache-test/html/uncacheable/max-age');
$this->assertSession()->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'UNCACHEABLE');
// 'user' cache context responses are ignored by Dynamic Page Cache.
$this->drupalGet('dynamic-page-cache-test/html/uncacheable/contexts');
$this->assertSession()->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'UNCACHEABLE');
// 'current-temperature' cache tag responses are ignored by Dynamic Page
// Cache.
$this->drupalGet('dynamic-page-cache-test/html/uncacheable/tags');
$this->assertSession()->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'MISS');
}
}

View File

@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\dynamic_page_cache\Functional;
use Drupal\Tests\system\Functional\Module\GenericModuleTestBase;
/**
* Generic module test for dynamic_page_cache.
*
* @group dynamic_page_cache
*/
class GenericTest extends GenericModuleTestBase {}