View source
<?php
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;
class DynamicPageCacheIntegrationTest extends BrowserTestBase {
protected static $modules = [
'dynamic_page_cache_test',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
\Drupal::service('module_installer')
->uninstall([
'page_cache',
]);
}
public function testDynamicPageCache() {
$url = Url::fromUri('route:dynamic_page_cache_test.response');
$this
->drupalGet($url);
$this
->assertSession()
->responseHeaderDoesNotExist(DynamicPageCacheSubscriber::HEADER);
$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');
$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');
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');
$url = Url::fromUri('route:dynamic_page_cache_test.html', [
'query' => [
'animal' => 'piglet',
],
]);
$this
->drupalGet($url);
$this
->assertSession()
->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'HIT');
}
$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);
$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);
$this
->drupalGet('dynamic-page-cache-test/html/uncacheable/max-age');
$this
->assertSession()
->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'UNCACHEABLE');
$this
->drupalGet('dynamic-page-cache-test/html/uncacheable/contexts');
$this
->assertSession()
->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'UNCACHEABLE');
$this
->drupalGet('dynamic-page-cache-test/html/uncacheable/tags');
$this
->assertSession()
->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'MISS');
}
}