diff --git a/core/core.services.yml b/core/core.services.yml index a7a2b3b..a387699 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -503,11 +503,6 @@ services: arguments: ['@settings'] tags: - { name: http_middleware, priority: 300 } - http_middleware.page_cache: - class: Drupal\Core\StackMiddleware\PageCache - arguments: ['@cache.render', '@page_cache_request_policy', '@page_cache_response_policy'] - tags: - - { name: http_middleware, priority: 200 } http_middleware.kernel_pre_handle: class: Drupal\Core\StackMiddleware\KernelPreHandle arguments: ['@kernel'] diff --git a/core/lib/Drupal/Core/Form/FormCache.php b/core/lib/Drupal/Core/Form/FormCache.php index ea38fd2..fa03bac 100644 --- a/core/lib/Drupal/Core/Form/FormCache.php +++ b/core/lib/Drupal/Core/Form/FormCache.php @@ -210,11 +210,6 @@ public function setCache($form_build_id, $form, FormStateInterface $form_state) $this->keyValueExpirableFactory->get('form')->setWithExpire($form_build_id, $form, $expire); } - // Cache form state. - if ($this->configFactory->get('system.performance')->get('cache.page.use_internal') && $this->isPageCacheable()) { - $form_state->addBuildInfo('immutable', TRUE); - } - // Store the known list of safe strings for form re-use. // @todo Ensure we are not storing an excessively large string list in: // https://www.drupal.org/node/2295823 @@ -225,14 +220,4 @@ public function setCache($form_build_id, $form, FormStateInterface $form_state) } } - /** - * Checks if the page is cacheable. - * - * @return bool - * TRUE is the page is cacheable, FALSE if not. - */ - protected function isPageCacheable() { - return ($this->requestPolicy->check($this->requestStack->getCurrentRequest()) === RequestPolicyInterface::ALLOW); - } - } diff --git a/core/modules/block/src/Tests/BlockTest.php b/core/modules/block/src/Tests/BlockTest.php index e6709da..498befb 100644 --- a/core/modules/block/src/Tests/BlockTest.php +++ b/core/modules/block/src/Tests/BlockTest.php @@ -22,6 +22,11 @@ class BlockTest extends BlockTestBase { /** + * {@inheritdoc} + */ + protected static $modules = ['page_cache']; + + /** * Tests block visibility. */ function testBlockVisibility() { @@ -309,7 +314,6 @@ public function testBlockCacheTags() { // Enable page caching. $config = $this->config('system.performance'); - $config->set('cache.page.use_internal', 1); $config->set('cache.page.max_age', 300); $config->save(); diff --git a/core/modules/block/src/Tests/BlockTestBase.php b/core/modules/block/src/Tests/BlockTestBase.php index 1f3cb74..307a435 100644 --- a/core/modules/block/src/Tests/BlockTestBase.php +++ b/core/modules/block/src/Tests/BlockTestBase.php @@ -19,7 +19,7 @@ * * @var array */ - public static $modules = array('block', 'filter', 'test_page_test', 'help', 'block_test'); + protected static $modules = array('block', 'filter', 'test_page_test', 'help', 'block_test'); /** * A list of theme regions to test. diff --git a/core/modules/file/src/Tests/DownloadTest.php b/core/modules/file/src/Tests/DownloadTest.php index 9de1b3a..4df65e5 100644 --- a/core/modules/file/src/Tests/DownloadTest.php +++ b/core/modules/file/src/Tests/DownloadTest.php @@ -56,8 +56,8 @@ public function testPrivateFileTransferWithoutPageCache() { */ public function testPrivateFileTransferWithPageCache() { // Turn on page caching and rerun the test. + $this->container->get('module_installer')->install(['page_cache']); $config = $this->config('system.performance'); - $config->set('cache.page.use_internal', 1); $config->set('cache.page.max_age', 300); $config->save(); diff --git a/core/modules/image/src/Tests/ImageStylesPathAndUrlTest.php b/core/modules/image/src/Tests/ImageStylesPathAndUrlTest.php index abe8ffb..88cf620 100644 --- a/core/modules/image/src/Tests/ImageStylesPathAndUrlTest.php +++ b/core/modules/image/src/Tests/ImageStylesPathAndUrlTest.php @@ -247,8 +247,8 @@ function doImageStyleUrlAndPathTests($scheme, $clean_url = TRUE, $extra_slash = */ protected function enablePageCache() { // Turn on page caching and rerun the test. + $this->container->get('module_installer')->install(['page_cache']); $config = $this->config('system.performance'); - $config->set('cache.page.use_internal', 1); $config->set('cache.page.max_age', 300); $config->save(); } diff --git a/core/modules/language/src/LanguageNegotiator.php b/core/modules/language/src/LanguageNegotiator.php index bc924fb..2354d88 100644 --- a/core/modules/language/src/LanguageNegotiator.php +++ b/core/modules/language/src/LanguageNegotiator.php @@ -188,20 +188,7 @@ protected function negotiateLanguage($type, $method_id) { $method = $this->negotiatorManager->getDefinition($method_id); if (!isset($method['types']) || in_array($type, $method['types'])) { - - // Check for a cache mode force from settings.php. - if ($this->settings->get('page_cache_without_database')) { - $cache_enabled = TRUE; - } - else { - $cache_enabled = $this->configFactory->get('system.performance')->get('cache.page.use_internal'); - } - - // If the language negotiation method has no cache preference or this is - // satisfied we can execute the callback. - if ($cache = !isset($method['cache']) || $this->currentUser->isAuthenticated() || $method['cache'] == $cache_enabled) { - $langcode = $this->getNegotiationMethodInstance($method_id)->getLangcode($this->requestStack->getCurrentRequest()); - } + $langcode = $this->getNegotiationMethodInstance($method_id)->getLangcode($this->requestStack->getCurrentRequest()); } $languages = $this->languageManager->getLanguages(); diff --git a/core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationBrowser.php b/core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationBrowser.php index e9fc38e..68e1b10 100644 --- a/core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationBrowser.php +++ b/core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationBrowser.php @@ -17,7 +17,6 @@ * @Plugin( * id = \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationBrowser::METHOD_ID, * weight = -2, - * cache = 0, * name = @Translation("Browser"), * description = @Translation("Language from the browser's language settings."), * config_route_name = "language.negotiation_browser" @@ -41,6 +40,10 @@ public function getLangcode(Request $request = NULL) { $langcodes = array_keys($this->languageManager->getLanguages()); $mappings = $this->config->get('language.mappings')->get(); $langcode = UserAgent::getBestMatchingLangcode($http_accept_language, $langcodes, $mappings); + // Internal page cache with multiple languages and browser negotiation + // could lead to wrong cached sites. Therefore disabling the internal + // page cache. + \Drupal::service('page_cache_kill_switch')->trigger(); } return $langcode; diff --git a/core/modules/migrate_drupal/config/install/migrate.migration.d6_system_performance.yml b/core/modules/migrate_drupal/config/install/migrate.migration.d6_system_performance.yml index 2b6f478..c27f3a9 100644 --- a/core/modules/migrate_drupal/config/install/migrate.migration.d6_system_performance.yml +++ b/core/modules/migrate_drupal/config/install/migrate.migration.d6_system_performance.yml @@ -14,7 +14,6 @@ process: 'css/preprocess': preprocess_css 'js/preprocess': preprocess_js 'cache/page/max_age': cache_lifetime - 'cache/page/use_internal': cache 'response/gzip': page_compression destination: plugin: config diff --git a/core/modules/migrate_drupal/src/Tests/d6/MigrateSystemPerformanceTest.php b/core/modules/migrate_drupal/src/Tests/d6/MigrateSystemPerformanceTest.php index 168e2e6..2f13720 100644 --- a/core/modules/migrate_drupal/src/Tests/d6/MigrateSystemPerformanceTest.php +++ b/core/modules/migrate_drupal/src/Tests/d6/MigrateSystemPerformanceTest.php @@ -40,8 +40,6 @@ public function testSystemPerformance() { $this->assertIdentical(FALSE, $config->get('css.preprocess')); $this->assertIdentical(FALSE, $config->get('js.preprocess')); $this->assertIdentical(0, $config->get('cache.page.max_age')); - $this->assertIdentical(TRUE, $config->get('cache.page.use_internal')); - $this->assertIdentical(TRUE, $config->get('response.gzip')); } } diff --git a/core/modules/page_cache/page_cache.info.yml b/core/modules/page_cache/page_cache.info.yml new file mode 100644 index 0000000..af72fab --- /dev/null +++ b/core/modules/page_cache/page_cache.info.yml @@ -0,0 +1,6 @@ +name: Internal page cache +type: module +description: 'Caches pages for anonymous users. Works well for small to medium-sized websites.' +package: Core +version: VERSION +core: 8.x diff --git a/core/modules/page_cache/page_cache.module b/core/modules/page_cache/page_cache.module new file mode 100644 index 0000000..6322363 --- /dev/null +++ b/core/modules/page_cache/page_cache.module @@ -0,0 +1,50 @@ +' . t('About') . ''; + $output .= '

' . t('The Internal page cache module caches pages for anonymous users in the database.') . '

'; + $output .= '

' . t('Uses') . '

'; + $output .= '
'; + $output .= '
' . t('Speeding up your site') . '
'; + $output .= '
'; + $output .= '

' . t('Pages requested by anonymous users are stored the first time they are requested and then reused; depending on your site configuration and the amount of your web traffic tied to anonymous visitors, the caching system may significantly increase the speed of your site.'); + $output .= '

' . t('(For authenticated users, pages need to be assembled for each user individually, but anonymous users all get the exact same version of each page.)') . '

'; + $output .= '
'; + $output .= '
' . t('Configuring Internal page cache') . '
'; + $output .= '
'; + $output .= '

' . t('On the Performance settings page, you can configure how long browsers and proxies may cache pages, that setting is also respected by the Internal page cache module. There is no other configuration.', array('@cache-settings' => \Drupal::url('system.performance_settings'))) . '

'; + $output .= '
'; + $output .= '
'; + + return $output; + } +} + +/** + * Implements hook_form_alter(). + */ +function page_cache_form_alter(&$form, FormStateInterface $form_state, $form_id) { + // If the page that's being built is cacheable, set the 'immutable' flag, to + // ensure that when the form is used, a new form build ID is generated when + // appropriate, to prevent information disclosure. + $request_policy = \Drupal::service('page_cache_request_policy'); + $request = \Drupal::requestStack()->getCurrentRequest(); + $request_is_cacheable = $request_policy->check($request) === RequestPolicyInterface::ALLOW; + if ($request_is_cacheable) { + $form_state->addBuildInfo('immutable', TRUE); + } +} diff --git a/core/modules/page_cache/page_cache.services.yml b/core/modules/page_cache/page_cache.services.yml new file mode 100644 index 0000000..bbddfa7 --- /dev/null +++ b/core/modules/page_cache/page_cache.services.yml @@ -0,0 +1,6 @@ +services: + http_middleware.page_cache: + class: Drupal\page_cache\StackMiddleware\PageCache + arguments: ['@cache.render', '@page_cache_request_policy', '@page_cache_response_policy'] + tags: + - { name: http_middleware, priority: 200 } diff --git a/core/lib/Drupal/Core/StackMiddleware/PageCache.php b/core/modules/page_cache/src/StackMiddleware/PageCache.php similarity index 87% rename from core/lib/Drupal/Core/StackMiddleware/PageCache.php rename to core/modules/page_cache/src/StackMiddleware/PageCache.php index d4ec291..b0dcce8 100644 --- a/core/lib/Drupal/Core/StackMiddleware/PageCache.php +++ b/core/modules/page_cache/src/StackMiddleware/PageCache.php @@ -2,11 +2,12 @@ /** * @file - * Contains \Drupal\Core\StackMiddleware\PageCache. + * Contains \Drupal\page_cache\StackMiddleware\PageCache. */ -namespace Drupal\Core\StackMiddleware; +namespace Drupal\page_cache\StackMiddleware; +use Drupal\Component\Utility\UserAgent; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\PageCache\RequestPolicyInterface; @@ -52,7 +53,7 @@ class PageCache implements HttpKernelInterface { protected $responsePolicy; /** - * Constructs a ReverseProxyMiddleware object. + * Constructs a PageCache object. * * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel * The decorated kernel. @@ -74,20 +75,8 @@ public function __construct(HttpKernelInterface $http_kernel, CacheBackendInterf * {@inheritdoc} */ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) { - if ($type !== static::MASTER_REQUEST) { - // Only allow page caching on master request. - $cache_enabled = FALSE; - } - elseif (Settings::get('page_cache_without_database')) { - // Check for a cache mode force from settings.php. - $cache_enabled = TRUE; - } - else { - $config = $this->config('system.performance'); - $cache_enabled = $config->get('cache.page.use_internal'); - } - - if ($cache_enabled && $this->requestPolicy->check($request) === RequestPolicyInterface::ALLOW) { + // Only allow page caching on master request. + if ($type === static::MASTER_REQUEST && $this->requestPolicy->check($request) === RequestPolicyInterface::ALLOW) { $response = $this->lookup($request, $type, $catch); } else { @@ -232,20 +221,6 @@ protected function fetch(Request $request, $type = self::MASTER_REQUEST, $catch return $response; } - // Check if the current page may be compressed. - if (extension_loaded('zlib') && !$response->headers->get('Content-Encoding') && $this->config('system.performance')->get('response.gzip')) { - $content = $response->getContent(); - if ($content) { - $response->setContent(gzencode($content, 9, FORCE_GZIP)); - $response->headers->set('Content-Encoding', 'gzip'); - } - - // When page compression is enabled, ensure that proxy caches will record - // and deliver different versions of a page depending on whether the - // client supports gzip or not. - $response->setVary('Accept-Encoding', FALSE); - } - // Use the actual timestamp from an Expires header, if available. $date = $response->getExpires(); $expire = ($date > (new \DateTime())) ? $date->getTimestamp() : Cache::PERMANENT; @@ -326,16 +301,4 @@ protected function getCacheId(Request $request) { return implode(':', $cid_parts); } - /** - * Wraps Drupal::config(). - * - * Config factory is not injected into this class in order to prevent - * premature initialization of config storage (database). - * - * @see \Drupal::config() - */ - protected function config($name) { - return \Drupal::config($name); - } - } diff --git a/core/modules/system/src/Tests/Cache/PageCacheTagsIntegrationTest.php b/core/modules/page_cache/src/Tests/PageCacheTagsIntegrationTest.php similarity index 94% rename from core/modules/system/src/Tests/Cache/PageCacheTagsIntegrationTest.php rename to core/modules/page_cache/src/Tests/PageCacheTagsIntegrationTest.php index f4d30ec..6d9db85 100644 --- a/core/modules/system/src/Tests/Cache/PageCacheTagsIntegrationTest.php +++ b/core/modules/page_cache/src/Tests/PageCacheTagsIntegrationTest.php @@ -2,10 +2,10 @@ /** * @file - * Contains \Drupal\system\Tests\Cache\PageCacheTagsIntegrationTest. + * Contains \Drupal\page_cache\Tests\PageCacheTagsIntegrationTest. */ -namespace Drupal\system\Tests\Cache; +namespace Drupal\page_cache\Tests; use Drupal\simpletest\WebTestBase; @@ -13,7 +13,7 @@ * Enables the page cache and tests its cache tags in various scenarios. * * @group Cache - * @see \Drupal\system\Tests\Bootstrap\PageCacheTest + * @see \Drupal\page_cache\Tests\PageCacheTest * @see \Drupal\node\Tests\NodePageCacheTest * @see \Drupal\menu_ui\Tests\MenuTest::testMenuBlockPageCacheTags() */ @@ -23,6 +23,11 @@ class PageCacheTagsIntegrationTest extends WebTestBase { protected $profile = 'standard'; + /** + * {@inheritdoc} + */ + protected static $modules = ['page_cache']; + protected $dumpHeaders = TRUE; /** diff --git a/core/modules/system/src/Tests/Bootstrap/PageCacheTest.php b/core/modules/page_cache/src/Tests/PageCacheTest.php similarity index 74% rename from core/modules/system/src/Tests/Bootstrap/PageCacheTest.php rename to core/modules/page_cache/src/Tests/PageCacheTest.php index c389a2a..a00416c 100644 --- a/core/modules/system/src/Tests/Bootstrap/PageCacheTest.php +++ b/core/modules/page_cache/src/Tests/PageCacheTest.php @@ -2,21 +2,25 @@ /** * @file - * Definition of Drupal\system\Tests\Bootstrap\PageCacheTest. + * Contains \Drupal\page_cache\Tests\PageCacheTest. */ -namespace Drupal\system\Tests\Bootstrap; +namespace Drupal\page_cache\Tests; use Drupal\Component\Datetime\DateTimePlus; +use Drupal\Core\Form\FormState; +use Drupal\Core\PageCache\ChainRequestPolicy; +use Drupal\Core\PageCache\RequestPolicy\NoSessionOpen; use Drupal\Core\Routing\RequestContext; use Drupal\simpletest\WebTestBase; use Drupal\Core\Cache\Cache; use Drupal\user\Entity\Role; +use Symfony\Component\HttpFoundation\Request; /** * Enables the page cache and tests it with various HTTP requests. * - * @group Bootstrap + * @group page_cache */ class PageCacheTest extends WebTestBase { @@ -27,7 +31,7 @@ class PageCacheTest extends WebTestBase { * * @var array */ - public static $modules = array('test_page_test', 'system_test'); + public static $modules = array('test_page_test', 'system_test', 'page_cache'); protected function setUp() { parent::setUp(); @@ -36,6 +40,20 @@ protected function setUp() { ->set('name', 'Drupal') ->set('page.front', 'test-page') ->save(); + + $config = $this->config('system.performance'); + $config->set('cache.page.max_age', 300); + $config->save(); + + // As the DefaultRequestPolicy deny cache for CLI requests, + // build request policy without CLI check. + $this->container->set('page_cache_request_policy', NULL); + /** @var \Drupal\Core\Session\SessionConfigurationInterface $session_configuration */ + $session_configuration = $this->container->get('session_configuration'); + /** @var \Drupal\Core\PageCache\RequestPolicyInterface $request_policy */ + $request_policy = new ChainRequestPolicy(); + $request_policy->addPolicy(new NoSessionOpen($session_configuration)); + $this->container->set('page_cache_request_policy', $request_policy); } /** @@ -45,11 +63,6 @@ protected function setUp() { * persisted. */ function testPageCacheTags() { - $config = $this->config('system.performance'); - $config->set('cache.page.use_internal', 1); - $config->set('cache.page.max_age', 300); - $config->save(); - $path = 'system-test/cache_tags_page'; $tags = array('system_test_cache_tags_page'); $this->drupalGet($path); @@ -78,11 +91,6 @@ function testPageCacheTags() { * Tests support for different cache items with different Accept headers. */ function testAcceptHeaderRequests() { - $config = $this->config('system.performance'); - $config->set('cache.page.use_internal', 1); - $config->set('cache.page.max_age', 300); - $config->save(); - $url_generator = \Drupal::urlGenerator(); $url_generator->setContext(new RequestContext()); $accept_header_cache_uri = $url_generator->getPathFromRoute('system_test.page_cache_accept_header'); @@ -149,11 +157,6 @@ function testAcceptHeaderRequests() { * Tests support of requests with If-Modified-Since and If-None-Match headers. */ function testConditionalRequests() { - $config = $this->config('system.performance'); - $config->set('cache.page.use_internal', 1); - $config->set('cache.page.max_age', 300); - $config->save(); - // Fill the cache. $this->drupalGet(''); // Verify the page is not printed twice when the cache is cold. @@ -194,16 +197,9 @@ function testConditionalRequests() { * Tests cache headers. */ function testPageCache() { - $config = $this->config('system.performance'); - $config->set('cache.page.use_internal', 1); - $config->set('cache.page.max_age', 300); - $config->set('response.gzip', 1); - $config->save(); - // Fill the cache. $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar'))); $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.'); - $this->assertEqual(strtolower($this->drupalGetHeader('Vary')), 'cookie,accept-encoding', 'Vary header was sent.'); // Symfony's Response logic determines a specific order for the subvalues // of the Cache-Control header, even if they are explicitly passed in to // the response header bag in a different order. @@ -214,7 +210,6 @@ function testPageCache() { // Check cache. $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar'))); $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.'); - $this->assertEqual(strtolower($this->drupalGetHeader('Vary')), 'cookie,accept-encoding', 'Vary: Cookie header was sent.'); $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'max-age=300, public', 'Cache-Control header was sent.'); $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.'); $this->assertEqual($this->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.'); @@ -223,14 +218,12 @@ function testPageCache() { $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Expires', 'value' => 'Fri, 19 Nov 2008 05:00:00 GMT'))); $this->assertEqual($this->drupalGetHeader('Expires'), 'Fri, 19 Nov 2008 05:00:00 GMT', 'Default header was replaced.'); $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Vary', 'value' => 'User-Agent'))); - $this->assertEqual(strtolower($this->drupalGetHeader('Vary')), 'user-agent,accept-encoding', 'Default header was replaced.'); // Check that authenticated users bypass the cache. $user = $this->drupalCreateUser(); $this->drupalLogin($user); $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar'))); $this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'), 'Caching was bypassed.'); - $this->assertTrue(strpos(strtolower($this->drupalGetHeader('Vary')), 'cookie') === FALSE, 'Vary: Cookie header was not sent.'); $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'must-revalidate, no-cache, post-check=0, pre-check=0, private', 'Cache-Control header was sent.'); $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.'); $this->assertEqual($this->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.'); @@ -240,11 +233,6 @@ function testPageCache() { * Tests the omit_vary_cookie setting. */ public function testPageCacheWithoutVaryCookie() { - $config = $this->config('system.performance'); - $config->set('cache.page.use_internal', 1); - $config->set('cache.page.max_age', 300); - $config->save(); - $settings['settings']['omit_vary_cookie'] = (object) array( 'value' => TRUE, 'required' => TRUE, @@ -263,50 +251,37 @@ public function testPageCacheWithoutVaryCookie() { } /** - * Tests page compression. - * - * The test should pass even if zlib.output_compression is enabled in php.ini, - * .htaccess or similar, or if compression is done outside PHP, e.g. by the - * mod_deflate Apache module. + * Test the setting of forms to be immutable. */ - function testPageCompression() { - $config = $this->config('system.performance'); - $config->set('cache.page.use_internal', 1); - $config->set('cache.page.max_age', 300); - $config->set('response.gzip', 1); - $config->save(); + public function testFormImmutability() { + // Install the module that provide sthe test form. + $this->container->get('module_installer') + ->install(['page_cache_form_test']); - // Fill the cache and verify that output is compressed. - $this->drupalGet('', array(), array('Accept-Encoding: gzip,deflate')); - $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.'); - $this->setRawContent(gzinflate(substr($this->getRawContent(), 10, -8))); - $this->assertRaw('', 'Page was gzip compressed.'); + $verify_immutable_flag = function ($request_method, $immutable_flag_exists) { + // Mock a request. + $request = Request::create($this->buildUrl('')); + $request->setMethod($request_method); + $this->container->get('request_stack')->push($request); - // Verify that cached output is compressed. - $this->drupalGet('', array(), array('Accept-Encoding: gzip,deflate')); - $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.'); - $this->assertEqual($this->drupalGetHeader('Content-Encoding'), 'gzip', 'A Content-Encoding header was sent.'); - $this->setRawContent(gzinflate(substr($this->getRawContent(), 10, -8))); - $this->assertRaw('', 'Page was gzip compressed.'); + // Build a form while the page_cache module is enabled, , the + $form_state = new FormState(); + \Drupal::formBuilder() + ->buildForm('\Drupal\page_cache_form_test\Form\TestForm', $form_state); - // Verify that a client without compression support gets an uncompressed page. - $this->drupalGet(''); - $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.'); - $this->assertFalse($this->drupalGetHeader('Content-Encoding'), 'A Content-Encoding header was not sent.'); - $this->assertTitle(t('Test page | @site-name', array('@site-name' => $this->config('system.site')->get('name'))), 'Site title matches.'); - $this->assertRaw('', 'Page was not compressed.'); + $this->assertIdentical($immutable_flag_exists, array_key_exists('immutable', $form_state->getBuildInfo())); + if ($immutable_flag_exists) { + $this->assertIdentical($form_state->getBuildInfo()['immutable'], TRUE); + } + }; - // Disable compression mode. - $config->set('response.gzip', 0); - $config->save(); + // Verify the immutable flag is being set on cacheable requests. + $verify_immutable_flag('POST', FALSE); + $verify_immutable_flag('GET', TRUE); - // Verify if cached page is still available for a client with compression support. - $this->drupalGet('', array(), array('Accept-Encoding: gzip,deflate')); - $this->setRawContent(gzinflate(substr($this->getRawContent(), 10, -8))); - $this->assertRaw('', 'Page was delivered after compression mode is changed (compression support enabled).'); - - // Verify if cached page is still available for a client without compression support. - $this->drupalGet(''); - $this->assertRaw('', 'Page was delivered after compression mode is changed (compression support disabled).'); + // Uninstall the page_cache module, verify the flag is never set. + $this->container->get('module_installer')->uninstall(['page_cache']); + $verify_immutable_flag('POST', FALSE); + $verify_immutable_flag('GET', FALSE); } } diff --git a/core/modules/page_cache/tests/modules/page_cache_form_test.info.yml b/core/modules/page_cache/tests/modules/page_cache_form_test.info.yml new file mode 100644 index 0000000..e18bb53 --- /dev/null +++ b/core/modules/page_cache/tests/modules/page_cache_form_test.info.yml @@ -0,0 +1,6 @@ +name: 'Page Cache Form Test' +type: module +description: 'Support module for the Page Cache module tests.' +core: 8.x +package: Testing +version: VERSION diff --git a/core/modules/page_cache/tests/modules/src/Form/TestForm.php b/core/modules/page_cache/tests/modules/src/Form/TestForm.php new file mode 100644 index 0000000..535f2b2 --- /dev/null +++ b/core/modules/page_cache/tests/modules/src/Form/TestForm.php @@ -0,0 +1,35 @@ +Llamas are awesome, but kittens are pretty cool too!

'; + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { } + +} diff --git a/core/modules/system/config/install/system.performance.yml b/core/modules/system/config/install/system.performance.yml index 1e75b4b..11392bd 100644 --- a/core/modules/system/config/install/system.performance.yml +++ b/core/modules/system/config/install/system.performance.yml @@ -1,6 +1,5 @@ cache: page: - use_internal: false max_age: 0 css: preprocess: true @@ -13,6 +12,4 @@ fast_404: js: preprocess: true gzip: true -response: - gzip: false stale_file_threshold: 2592000 diff --git a/core/modules/system/config/schema/system.schema.yml b/core/modules/system/config/schema/system.schema.yml index 6888a56..c108618 100644 --- a/core/modules/system/config/schema/system.schema.yml +++ b/core/modules/system/config/schema/system.schema.yml @@ -161,14 +161,11 @@ system.performance: mapping: page: type: mapping - label: 'Page cache' + label: 'Page caching' mapping: - use_internal: - type: boolean - label: 'Use internal page cache' max_age: type: integer - label: 'Max age of page cache' + label: 'Max age' css: type: mapping label: 'CSS performance settings' diff --git a/core/modules/system/src/Form/PerformanceForm.php b/core/modules/system/src/Form/PerformanceForm.php index 9049c78..c5eb48c 100644 --- a/core/modules/system/src/Form/PerformanceForm.php +++ b/core/modules/system/src/Form/PerformanceForm.php @@ -121,6 +121,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#type' => 'details', '#title' => t('Caching'), '#open' => TRUE, + '#description' => $this->t('Note: Drupal provides an internal page cache module that is recommended for small to medium-sized websites.'), ); // Identical options to the ones for block caching. // @see \Drupal\Core\Block\BlockBase::buildConfigurationForm() @@ -132,14 +133,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#title' => t('Page cache maximum age'), '#default_value' => $config->get('cache.page.max_age'), '#options' => $period, - '#description' => t('The maximum time a page can be cached. This is used as the value for max-age in Cache-Control headers.'), - ); - - $form['caching']['cache'] = array( - '#type' => 'checkbox', - '#title' => t('Use internal page cache'), - '#description' => t("If a reverse proxy cache isn't available, use Drupal's internal cache system to store cached pages."), - '#default_value' => $config->get('cache.page.use_internal'), + '#description' => t('The maximum time a page can be cached by browsers and proxies. This is used as the value for max-age in Cache-Control headers.'), ); $directory = 'public://'; @@ -157,16 +151,6 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#description' => t('External resources can be optimized automatically, which can reduce both the size and number of requests made to your website.') . $disabled_message, ); - $form['bandwidth_optimization']['page_compression'] = array( - '#type' => 'checkbox', - '#title' => t('Compress cached pages'), - '#default_value' => $config->get('response.gzip'), - '#states' => array( - 'visible' => array( - 'input[name="cache"]' => array('checked' => TRUE), - ), - ), - ); $form['bandwidth_optimization']['preprocess_css'] = array( '#type' => 'checkbox', '#title' => t('Aggregate CSS files'), @@ -195,9 +179,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $this->renderCache->deleteAll(); $this->config('system.performance') - ->set('cache.page.use_internal', $form_state->getValue('cache')) ->set('cache.page.max_age', $form_state->getValue('page_cache_maximum_age')) - ->set('response.gzip', $form_state->getValue('page_compression')) ->set('css.preprocess', $form_state->getValue('preprocess_css')) ->set('js.preprocess', $form_state->getValue('preprocess_js')) ->save(); diff --git a/core/modules/system/src/Tests/Ajax/AjaxFormPageCacheTest.php b/core/modules/system/src/Tests/Ajax/AjaxFormPageCacheTest.php index 9da0935..3e36515 100644 --- a/core/modules/system/src/Tests/Ajax/AjaxFormPageCacheTest.php +++ b/core/modules/system/src/Tests/Ajax/AjaxFormPageCacheTest.php @@ -17,11 +17,15 @@ class AjaxFormPageCacheTest extends AjaxTestBase { /** * {@inheritdoc} */ + public static $modules = ['page_cache']; + + /** + * {@inheritdoc} + */ public function setUp() { parent::setUp(); $config = $this->config('system.performance'); - $config->set('cache.page.use_internal', 1); $config->set('cache.page.max_age', 300); $config->save(); } diff --git a/core/modules/system/src/Tests/Cache/AssertPageCacheContextsAndTagsTrait.php b/core/modules/system/src/Tests/Cache/AssertPageCacheContextsAndTagsTrait.php index 1523421..307da6d 100644 --- a/core/modules/system/src/Tests/Cache/AssertPageCacheContextsAndTagsTrait.php +++ b/core/modules/system/src/Tests/Cache/AssertPageCacheContextsAndTagsTrait.php @@ -20,6 +20,7 @@ * Enables page caching. */ protected function enablePageCaching() { + \Drupal::service('module_installer')->install(['page_cache']); $config = $this->config('system.performance'); $config->set('cache.page.use_internal', 1); $config->set('cache.page.max_age', 300); diff --git a/core/modules/system/src/Tests/Cache/PageCacheTagsTestBase.php b/core/modules/system/src/Tests/Cache/PageCacheTagsTestBase.php index 3e3b1bc..7909300 100644 --- a/core/modules/system/src/Tests/Cache/PageCacheTagsTestBase.php +++ b/core/modules/system/src/Tests/Cache/PageCacheTagsTestBase.php @@ -26,12 +26,16 @@ /** * {@inheritdoc} */ + protected static $modules = ['page_cache']; + + /** + * {@inheritdoc} + */ protected function setUp() { parent::setUp(); // Enable page caching. $config = $this->config('system.performance'); - $config->set('cache.page.use_internal', 1); $config->set('cache.page.max_age', 3600); $config->save(); } diff --git a/core/modules/system/src/Tests/Form/FormStoragePageCacheTest.php b/core/modules/system/src/Tests/Form/FormStoragePageCacheTest.php index d04be90..bac12db 100644 --- a/core/modules/system/src/Tests/Form/FormStoragePageCacheTest.php +++ b/core/modules/system/src/Tests/Form/FormStoragePageCacheTest.php @@ -19,7 +19,7 @@ class FormStoragePageCacheTest extends WebTestBase { /** * @var array */ - public static $modules = array('form_test'); + public static $modules = array('form_test', 'page_cache'); /** * {@inheritdoc} @@ -28,7 +28,6 @@ protected function setUp() { parent::setUp(); $config = $this->config('system.performance'); - $config->set('cache.page.use_internal', 1); $config->set('cache.page.max_age', 300); $config->save(); } diff --git a/core/modules/system/src/Tests/Session/SessionTest.php b/core/modules/system/src/Tests/Session/SessionTest.php index 0921f66..ffd17aa 100644 --- a/core/modules/system/src/Tests/Session/SessionTest.php +++ b/core/modules/system/src/Tests/Session/SessionTest.php @@ -147,8 +147,8 @@ function testEmptyAnonymousSession() { $this->assertSessionEmpty(TRUE); // The same behavior is expected when caching is enabled. + $this->container->get('module_installer')->install(['page_cache']); $config = $this->config('system.performance'); - $config->set('cache.page.use_internal', 1); $config->set('cache.page.max_age', 300); $config->save(); $this->drupalGet(''); diff --git a/core/tests/Drupal/Tests/Core/Form/FormCacheTest.php b/core/tests/Drupal/Tests/Core/Form/FormCacheTest.php index 138309e..1820932 100644 --- a/core/tests/Drupal/Tests/Core/Form/FormCacheTest.php +++ b/core/tests/Drupal/Tests/Core/Form/FormCacheTest.php @@ -487,43 +487,6 @@ public function testSetCacheBuildIdMismatch() { } /** - * @covers ::setCache - */ - public function testSetCacheImmutableForm() { - $form_build_id = 'the_form_build_id'; - $form = [ - '#form_id' => 'the_form_id', - ]; - $form_state = new FormState(); - - $this->formCacheStore->expects($this->once()) - ->method('setWithExpire') - ->with($form_build_id, $form, $this->isType('int')); - $form_state_data = $form_state->getCacheableArray(); - $form_state_data['build_info']['safe_strings'] = []; - // Ensure that the form is marked immutable. - $form_state_data['build_info']['immutable'] = TRUE; - $this->formStateCacheStore->expects($this->once()) - ->method('setWithExpire') - ->with($form_build_id, $form_state_data, $this->isType('int')); - - // Rebuild the FormCache with a config factory that will return a config - // object with the internal page cache enabled. - $this->configFactory = $this->getConfigFactoryStub(['system.performance' => ['cache.page.use_internal' => TRUE]]); - $root = dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__)))); - $this->formCache = $this->getMockBuilder('Drupal\Core\Form\FormCache') - ->setConstructorArgs([$root, $this->keyValueExpirableFactory, $this->moduleHandler, $this->account, $this->csrfToken, $this->logger, $this->configFactory, $this->requestStack, $this->requestPolicy]) - ->setMethods(['isPageCacheable']) - ->getMock(); - - $this->formCache->expects($this->once()) - ->method('isPageCacheable') - ->willReturn(TRUE); - - $this->formCache->setCache($form_build_id, $form, $form_state); - } - - /** * Ensures SafeMarkup does not bleed from one test to another. */ protected function resetSafeMarkup() {