View source
<?php
namespace Drupal\dynamic_page_cache\Tests;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\Core\Url;
use Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber;
use Drupal\simpletest\WebTestBase;
class DynamicPageCacheIntegrationTest extends WebTestBase {
protected $dumpHeaders = TRUE;
protected static $modules = [
'dynamic_page_cache_test',
];
protected function setUp() {
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
->assertFalse($this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER), 'Response object returned: Dynamic Page Cache is ignoring.');
$url = Url::fromUri('route:dynamic_page_cache_test.cacheable_response');
$this
->drupalGet($url);
$this
->assertEqual('MISS', $this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER), 'Cacheable response object returned: Dynamic Page Cache is active, Dynamic Page Cache MISS.');
$this
->drupalGet($url);
$this
->assertEqual('HIT', $this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER), 'Cacheable response object returned: Dynamic Page Cache is active, Dynamic Page Cache HIT.');
$url = Url::fromUri('route:dynamic_page_cache_test.html');
$this
->drupalGet($url);
$this
->assertEqual('MISS', $this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER), 'Render array returned, rendered as HTML response: Dynamic Page Cache is active, Dynamic Page Cache MISS.');
$this
->drupalGet($url);
$this
->assertEqual('HIT', $this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER), 'Render array returned, rendered as HTML response: Dynamic Page Cache is active, Dynamic Page Cache 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
->assertRaw($animal);
$this
->assertEqual('MISS', $this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER), 'Render array returned, rendered as HTML response: Dynamic Page Cache is active, Dynamic Page Cache MISS.');
$this
->drupalGet($url);
$this
->assertRaw($animal);
$this
->assertEqual('HIT', $this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER), 'Render array returned, rendered as HTML response: Dynamic Page Cache is active, Dynamic Page Cache HIT.');
$url = Url::fromUri('route:dynamic_page_cache_test.html', [
'query' => [
'animal' => 'piglet',
],
]);
$this
->drupalGet($url);
$this
->assertEqual('HIT', $this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER), 'Render array returned, rendered as HTML response: Dynamic Page Cache is active, Dynamic Page Cache HIT.');
}
$this
->drupalGet('dynamic-page-cache-test/html', array(
'query' => array(
MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax',
),
));
$this
->assertFalse($this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER), 'Render array returned, rendered as AJAX response: Dynamic Page Cache is ignoring.');
$this
->drupalGet('dynamic-page-cache-test/html', array(
'query' => array(
MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_dialog',
),
));
$this
->assertFalse($this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER), 'Render array returned, rendered as dialog response: Dynamic Page Cache is ignoring.');
$this
->drupalGet('dynamic-page-cache-test/html', array(
'query' => array(
MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_modal',
),
));
$this
->assertFalse($this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER), 'Render array returned, rendered as modal response: Dynamic Page Cache is ignoring.');
$this
->drupalGet('dynamic-page-cache-test/html/admin');
$this
->assertFalse($this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER), 'Response returned, rendered as HTML response, admin route: Dynamic Page Cache is ignoring');
$this
->drupalGet('dynamic-page-cache-test/response/admin');
$this
->assertFalse($this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER), 'Response returned, plain response, admin route: Dynamic Page Cache is ignoring');
$this
->drupalGet('dynamic-page-cache-test/cacheable-response/admin');
$this
->assertFalse($this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER), 'Response returned, cacheable response, admin route: Dynamic Page Cache is ignoring');
$this
->drupalGet('dynamic-page-cache-test/html/uncacheable/max-age');
$this
->assertEqual('UNCACHEABLE', $this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER), 'Render array returned, rendered as HTML response, but uncacheable: Dynamic Page Cache is running, but not caching.');
$this
->drupalGet('dynamic-page-cache-test/html/uncacheable/contexts');
$this
->assertEqual('UNCACHEABLE', $this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER), 'Render array returned, rendered as HTML response, but uncacheable: Dynamic Page Cache is running, but not caching.');
$this
->drupalGet('dynamic-page-cache-test/html/uncacheable/tags');
$this
->assertEqual('MISS', $this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER), 'By default, Drupal has no auto-placeholdering cache tags.');
}
}