core/modules/page_cache/page_cache.module | 2 +- .../modules/page_cache/src/Tests/PageCacheTest.php | 35 +++++++++++++++++++ .../tests/modules/page_cache_form_test.info.yml | 6 ++++ .../page_cache/tests/modules/src/Form/TestForm.php | 35 +++++++++++++++++++ .../tests/Drupal/Tests/Core/Form/FormCacheTest.php | 39 ---------------------- 5 files changed, 77 insertions(+), 40 deletions(-) diff --git a/core/modules/page_cache/page_cache.module b/core/modules/page_cache/page_cache.module index 2c7d01f..6322363 100644 --- a/core/modules/page_cache/page_cache.module +++ b/core/modules/page_cache/page_cache.module @@ -21,7 +21,7 @@ function page_cache_help($route_name, RouteMatchInterface $route_match) { $output .= '
'; $output .= '
' . t('Speeding up your site') . '
'; $output .= '
'; - $output .= '

' . t('Pages requested by anonymous users are stored in a compressed format; 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('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') . '
'; diff --git a/core/modules/page_cache/src/Tests/PageCacheTest.php b/core/modules/page_cache/src/Tests/PageCacheTest.php index 1856315..beb56d9 100644 --- a/core/modules/page_cache/src/Tests/PageCacheTest.php +++ b/core/modules/page_cache/src/Tests/PageCacheTest.php @@ -9,11 +9,13 @@ use Drupal\Component\Datetime\DateTimePlus; use Drupal\Component\Utility\String; +use Drupal\Core\Form\FormState; use Drupal\Core\Routing\RequestContext; use Drupal\Core\Url; use Drupal\language\Entity\ConfigurableLanguage; use Drupal\simpletest\WebTestBase; use Drupal\Core\Cache\Cache; +use Symfony\Component\HttpFoundation\Request; /** * Enables the page cache and tests it with various HTTP requests. @@ -194,4 +196,37 @@ public function testPageCacheWithoutVaryCookie() { $this->assertTrue(strpos($this->drupalGetHeader('Vary'), 'Cookie') === FALSE, 'Vary: Cookie header was not sent.'); } + /** + * Test the setting of forms to be immutable. + */ + public function testFormImmutability() { + // Install the module that provide sthe test form. + $this->container->get('module_installer')->install(['page_cache_form_test']); + + $verify_immutable_flag = function($request_method, $immutable_flag_exists) { + // Mock a request. + $request = Request::create('/'); + $request->setMethod($request_method); + $this->container->get('request_stack')->push($request); + + // 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); + + $this->assertIdentical($immutable_flag_exists, array_key_exists('immutable', $form_state->getBuildInfo())); + if ($immutable_flag_exists) { + $this->assertIdentical($form_state->getBuildInfo()['immutable'], TRUE); + } + }; + + // Verify the immutable flag is being set on cacheable requests. + $verify_immutable_flag('POST', FALSE); + $verify_immutable_flag('GET', TRUE); + + // 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/tests/Drupal/Tests/Core/Form/FormCacheTest.php b/core/tests/Drupal/Tests/Core/Form/FormCacheTest.php index c1f33fc..1820932 100644 --- a/core/tests/Drupal/Tests/Core/Form/FormCacheTest.php +++ b/core/tests/Drupal/Tests/Core/Form/FormCacheTest.php @@ -487,45 +487,6 @@ public function testSetCacheBuildIdMismatch() { } /** - * @covers ::setCache - * - * @todo Move this test to page_cache module. - */ - public function atestSetCacheImmutableForm() { - $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 page_cache module 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() {