trait PageCachePolicyTrait
Same name and namespace in other branches
- main core/tests/Drupal/Tests/Traits/Core/Cache/PageCachePolicyTrait.php \Drupal\Tests\Traits\Core\Cache\PageCachePolicyTrait
Trait for using page caching modules in Kernel tests.
To use Drupal's page caching with HTTP requests in a Kernel test, the following are necessary:
- Either or both of the 'page_cache' and 'dynamic_page_cache' modules must be installed, as desired.
- The test class must implement \Drupal\Core\DependencyInjection\ServiceModifierInterface. This trait provides an implementation of the alter() method which replaces the cache policies of the page cache modules.
- The 'user' module must be installed if 'dynamic_page_cache' is installed.
Hierarchy
- trait \Drupal\Tests\Traits\Core\Cache\PageCachePolicyTrait
See also
\Drupal\Tests\HttpKernelUiHelperTrait
2 files declare their use of PageCachePolicyTrait
- KernelTestHttpCacheRequestTest.php in core/
tests/ Drupal/ KernelTests/ KernelTestHttpCacheRequestTest.php - KernelTestHttpDynamicCacheRequestTest.php in core/
tests/ Drupal/ KernelTests/ KernelTestHttpDynamicCacheRequestTest.php
File
-
core/
tests/ Drupal/ Tests/ Traits/ Core/ Cache/ PageCachePolicyTrait.php, line 28
Namespace
Drupal\Tests\Traits\Core\CacheView source
trait PageCachePolicyTrait {
/**
* Sets up mock cache policies.
*
* To use this, the test class must implement
* \Drupal\Core\DependencyInjection\ServiceModifierInterface.
*
* @param \Drupal\Core\DependencyInjection\ContainerBuilder $container
* The DI container.
*/
public function alter(ContainerBuilder $container) : void {
if ($container->hasDefinition('page_cache_request_policy')) {
$service_definition = $container->getDefinition('page_cache_request_policy');
$service_definition->setClass(KernelTestPageCacheRequestPolicy::class);
}
if ($container->hasDefinition('dynamic_page_cache_request_policy')) {
$service_definition = $container->getDefinition('dynamic_page_cache_request_policy');
$service_definition->setClass(KernelTestDynamicPageCacheRequestPolicy::class);
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.