core/lib/Drupal/Core/Form/FormBuilder.php | 13 ++--------- .../block/src/Tests/Views/DisplayBlockTest.php | 26 ++++++++++++---------- .../src/Tests/BlockContentTranslationUITest.php | 3 ++- .../src/Tests/ContentTestTranslationUITest.php | 3 ++- .../src/Tests/MenuLinkContentTranslationUITest.php | 2 +- .../node/src/Tests/NodeBlockFunctionalTest.php | 2 +- .../src/Tests/ShortcutTranslationUITest.php | 2 +- .../system/src/Tests/Common/RenderWebTest.php | 8 +++---- .../system/src/Tests/Session/SessionTest.php | 5 +++++ .../modules/common_test/common_test.routing.yml | 1 + .../Drupal/Tests/Core/Form/FormBuilderTest.php | 19 +++++++++++----- 11 files changed, 47 insertions(+), 37 deletions(-) diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php index b32ce5a..ba8c055 100644 --- a/core/lib/Drupal/Core/Form/FormBuilder.php +++ b/core/lib/Drupal/Core/Form/FormBuilder.php @@ -648,11 +648,8 @@ public function prepareForm($form_id, &$form, FormStateInterface &$form_state) { // Only update the action if it is not already set. if (!isset($form['#action'])) { $form['#action'] = $this->buildFormAction(); - } - - // If the form method is specified in the form, pass it on to FormState. - if (isset($form['#method'])) { - $form_state->setMethod($form['#method']); + $form['#cache']['contexts'][] = 'url.path'; + $form['#cache']['contexts'][] = 'url.query_args'; } // Fix the form method, if it is 'get' in $form_state, but not in $form. @@ -660,12 +657,6 @@ public function prepareForm($form_id, &$form, FormStateInterface &$form_state) { $form['#method'] = 'get'; } - // Mark every non-GET form as uncacheable. - // @todo Refine in https://www.drupal.org/node/2526472. - if (!$form_state->isMethodType('get')) { - $form['#cache']['max-age'] = 0; - } - // Generate a new #build_id for this form, if none has been set already. // The form_build_id is used as key to cache a particular build of the form. // For multi-step forms, this allows the user to go back to an earlier diff --git a/core/modules/block/src/Tests/Views/DisplayBlockTest.php b/core/modules/block/src/Tests/Views/DisplayBlockTest.php index 0beb539..f7b3ffe 100644 --- a/core/modules/block/src/Tests/Views/DisplayBlockTest.php +++ b/core/modules/block/src/Tests/Views/DisplayBlockTest.php @@ -8,6 +8,7 @@ namespace Drupal\block\Tests\Views; use Drupal\Component\Serialization\Json; +use Drupal\Core\Url; use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; use Drupal\views\Entity\View; use Drupal\views\Views; @@ -271,6 +272,7 @@ public function testBlockRendering() { * Tests the various testcases of empty block rendering. */ public function testBlockEmptyRendering() { + $url = new Url('test_page_test.test_page'); // Remove all views_test_data entries. \Drupal::database()->truncate('views_test_data')->execute(); /** @var \Drupal\views\ViewEntityInterface $view */ @@ -285,12 +287,12 @@ public function testBlockEmptyRendering() { $display['display_options']['block_hide_empty'] = TRUE; $view->save(); - $this->drupalGet(''); + $this->drupalGet($url); $this->assertEqual(0, count($this->xpath('//div[contains(@class, "block-views-blocktest-view-block-block-1")]'))); // Ensure that the view cachability metadata is propagated even, for an // empty block. - $this->assertCacheTags(array_merge($block->getCacheTags(), ['block_view', 'config:block_list', 'config:system.site', 'config:views.view.test_view_block' ,'rendered'])); - $this->assertCacheContexts(['url.query_args:_wrapper_format', 'user.roles:authenticated']); + $this->assertCacheTags(array_merge($block->getCacheTags(), ['block_view', 'config:block_list', 'config:views.view.test_view_block' ,'rendered'])); + $this->assertCacheContexts(['url.query_args:_wrapper_format']); // Add a header displayed on empty result. $display = &$view->getDisplay('block_1'); @@ -305,10 +307,10 @@ public function testBlockEmptyRendering() { ]; $view->save(); - $this->drupalGet(''); + $this->drupalGet($url); $this->assertEqual(1, count($this->xpath('//div[contains(@class, "block-views-blocktest-view-block-block-1")]'))); - $this->assertCacheTags(array_merge($block->getCacheTags(), ['block_view', 'config:block_list', 'config:system.site', 'config:views.view.test_view_block' ,'rendered'])); - $this->assertCacheContexts(['url.query_args:_wrapper_format', 'user.roles:authenticated']); + $this->assertCacheTags(array_merge($block->getCacheTags(), ['block_view', 'config:block_list', 'config:views.view.test_view_block' ,'rendered'])); + $this->assertCacheContexts(['url.query_args:_wrapper_format']); // Hide the header on empty results. $display = &$view->getDisplay('block_1'); @@ -323,10 +325,10 @@ public function testBlockEmptyRendering() { ]; $view->save(); - $this->drupalGet(''); + $this->drupalGet($url); $this->assertEqual(0, count($this->xpath('//div[contains(@class, "block-views-blocktest-view-block-block-1")]'))); - $this->assertCacheTags(array_merge($block->getCacheTags(), ['block_view', 'config:block_list', 'config:system.site', 'config:views.view.test_view_block' ,'rendered'])); - $this->assertCacheContexts(['url.query_args:_wrapper_format', 'user.roles:authenticated']); + $this->assertCacheTags(array_merge($block->getCacheTags(), ['block_view', 'config:block_list', 'config:views.view.test_view_block' ,'rendered'])); + $this->assertCacheContexts(['url.query_args:_wrapper_format']); // Add an empty text. $display = &$view->getDisplay('block_1'); @@ -340,10 +342,10 @@ public function testBlockEmptyRendering() { ]; $view->save(); - $this->drupalGet(''); + $this->drupalGet($url); $this->assertEqual(1, count($this->xpath('//div[contains(@class, "block-views-blocktest-view-block-block-1")]'))); - $this->assertCacheTags(array_merge($block->getCacheTags(), ['block_view', 'config:block_list', 'config:system.site', 'config:views.view.test_view_block' ,'rendered'])); - $this->assertCacheContexts(['url.query_args:_wrapper_format', 'user.roles:authenticated']); + $this->assertCacheTags(array_merge($block->getCacheTags(), ['block_view', 'config:block_list', 'config:views.view.test_view_block' ,'rendered'])); + $this->assertCacheContexts(['url.query_args:_wrapper_format']); } /** diff --git a/core/modules/block_content/src/Tests/BlockContentTranslationUITest.php b/core/modules/block_content/src/Tests/BlockContentTranslationUITest.php index 4ce1565..964f0f9 100644 --- a/core/modules/block_content/src/Tests/BlockContentTranslationUITest.php +++ b/core/modules/block_content/src/Tests/BlockContentTranslationUITest.php @@ -36,7 +36,8 @@ class BlockContentTranslationUITest extends ContentTranslationUITestBase { protected $defaultCacheContexts = [ 'languages:language_interface', 'theme', - 'url.query_args:_wrapper_format', + 'url.path', + 'url.query_args', 'user.permissions', 'user.roles:authenticated', ]; diff --git a/core/modules/content_translation/src/Tests/ContentTestTranslationUITest.php b/core/modules/content_translation/src/Tests/ContentTestTranslationUITest.php index 43fd779..08b1d4a 100644 --- a/core/modules/content_translation/src/Tests/ContentTestTranslationUITest.php +++ b/core/modules/content_translation/src/Tests/ContentTestTranslationUITest.php @@ -32,7 +32,8 @@ class ContentTestTranslationUITest extends ContentTranslationUITestBase { protected $defaultCacheContexts = [ 'languages:language_interface', 'theme', - 'url.query_args:_wrapper_format', + 'url.path', + 'url.query_args', 'user.permissions', 'user.roles:authenticated', ]; diff --git a/core/modules/menu_link_content/src/Tests/MenuLinkContentTranslationUITest.php b/core/modules/menu_link_content/src/Tests/MenuLinkContentTranslationUITest.php index f72cb29..1404384 100644 --- a/core/modules/menu_link_content/src/Tests/MenuLinkContentTranslationUITest.php +++ b/core/modules/menu_link_content/src/Tests/MenuLinkContentTranslationUITest.php @@ -20,7 +20,7 @@ class MenuLinkContentTranslationUITest extends ContentTranslationUITestBase { /** * {inheritdoc} */ - protected $defaultCacheContexts = ['languages:language_interface', 'theme', 'url.query_args:_wrapper_format', 'user.permissions', 'user.roles:authenticated']; + protected $defaultCacheContexts = ['languages:language_interface', 'theme', 'url.path', 'url.query_args', 'user.permissions', 'user.roles:authenticated']; /** * Modules to enable. diff --git a/core/modules/node/src/Tests/NodeBlockFunctionalTest.php b/core/modules/node/src/Tests/NodeBlockFunctionalTest.php index 6f319f5..b6855ba 100644 --- a/core/modules/node/src/Tests/NodeBlockFunctionalTest.php +++ b/core/modules/node/src/Tests/NodeBlockFunctionalTest.php @@ -148,7 +148,7 @@ public function testRecentNodeBlock() { $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'user', 'route']); $this->drupalGet('node/add/article'); $this->assertText($label, 'Block was displayed on the node/add/article page.'); - $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'user', 'route']); + $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.path', 'url.query_args', 'user', 'route']); $this->drupalGet('node/' . $node1->id()); $this->assertText($label, 'Block was displayed on the node/N when node is of type article.'); $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'user', 'route', 'timezone']); diff --git a/core/modules/shortcut/src/Tests/ShortcutTranslationUITest.php b/core/modules/shortcut/src/Tests/ShortcutTranslationUITest.php index 5fa63d8..e42262f 100644 --- a/core/modules/shortcut/src/Tests/ShortcutTranslationUITest.php +++ b/core/modules/shortcut/src/Tests/ShortcutTranslationUITest.php @@ -21,7 +21,7 @@ class ShortcutTranslationUITest extends ContentTranslationUITestBase { /** * {inheritdoc} */ - protected $defaultCacheContexts = ['languages:language_interface', 'theme', 'user', 'url.query_args:_wrapper_format', 'url.site']; + protected $defaultCacheContexts = ['languages:language_interface', 'theme', 'user', 'url.path', 'url.query_args', 'url.site']; /** * Modules to enable. diff --git a/core/modules/system/src/Tests/Common/RenderWebTest.php b/core/modules/system/src/Tests/Common/RenderWebTest.php index 5cfe3ab..f6252e9 100644 --- a/core/modules/system/src/Tests/Common/RenderWebTest.php +++ b/core/modules/system/src/Tests/Common/RenderWebTest.php @@ -30,17 +30,17 @@ class RenderWebTest extends WebTestBase { * Asserts the cache context for the wrapper format is always present. */ function testWrapperFormatCacheContext() { - $this->drupalGet(''); + $this->drupalGet('common-test/type-link-active-class'); $this->assertIdentical(0, strpos($this->getRawContent(), "\nassertIdentical('text/html; charset=UTF-8', $this->drupalGetHeader('Content-Type')); - $this->assertTitle('Log in | Drupal'); + $this->assertTitle('Test active link class | Drupal'); $this->assertCacheContext('url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT); - $this->drupalGet('', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'json']]); + $this->drupalGet('common-test/type-link-active-class', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'json']]); $this->assertIdentical('application/json', $this->drupalGetHeader('Content-Type')); $json = Json::decode($this->getRawContent()); $this->assertEqual(['content', 'title'], array_keys($json)); - $this->assertIdentical('Log in', $json['title']); + $this->assertIdentical('Test active link class', $json['title']); $this->assertCacheContext('url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT); } diff --git a/core/modules/system/src/Tests/Session/SessionTest.php b/core/modules/system/src/Tests/Session/SessionTest.php index c194c79..5b33bd3 100644 --- a/core/modules/system/src/Tests/Session/SessionTest.php +++ b/core/modules/system/src/Tests/Session/SessionTest.php @@ -153,6 +153,11 @@ public function testSessionPersistenceOnLogin() { * Test that empty anonymous sessions are destroyed. */ function testEmptyAnonymousSession() { + // Disable the smart_cache module; it'd cause session_test's debug output + // (that is added in SessionTestSubscriber::onKernelResponseSessionTest()) + // to not be added. + $this->container->get('module_installer')->uninstall(['smart_cache']); + // Verify that no session is automatically created for anonymous user when // page caching is disabled. $this->container->get('module_installer')->uninstall(['page_cache']); diff --git a/core/modules/system/tests/modules/common_test/common_test.routing.yml b/core/modules/system/tests/modules/common_test/common_test.routing.yml index 92c80be..8062d06 100644 --- a/core/modules/system/tests/modules/common_test/common_test.routing.yml +++ b/core/modules/system/tests/modules/common_test/common_test.routing.yml @@ -1,6 +1,7 @@ common_test.l_active_class: path: '/common-test/type-link-active-class' defaults: + _title: 'Test active link class' _controller: '\Drupal\common_test\Controller\CommonTestController::typeLinkActiveClass' requirements: _access: 'TRUE' diff --git a/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php b/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php index d95e3bc..790ad86 100644 --- a/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php +++ b/core/tests/Drupal/Tests/Core/Form/FormBuilderTest.php @@ -773,7 +773,7 @@ public function providerTestInvalidToken() { * * @dataProvider providerTestFormTokenCacheability */ - function testFormTokenCacheability($token, $is_authenticated, $expected_form_cacheability, $expected_token_cacheability) { + function testFormTokenCacheability($token, $action, $is_authenticated, $expected_form_cacheability, $expected_token_cacheability) { $user = $this->prophesize(AccountProxyInterface::class); $user->isAuthenticated() ->willReturn($is_authenticated); @@ -787,6 +787,10 @@ function testFormTokenCacheability($token, $is_authenticated, $expected_form_cac $form['#token'] = $token; } + if (isset($action)) { + $form['#action'] = $action; + } + $form_arg = $this->getMock('Drupal\Core\Form\FormInterface'); $form_arg->expects($this->once()) ->method('getFormId') @@ -820,10 +824,15 @@ function testFormTokenCacheability($token, $is_authenticated, $expected_form_cac */ function providerTestFormTokenCacheability() { return [ - 'token:none,authenticated:true' => [NULL, TRUE, ['contexts' => ['user.roles:authenticated']], ['max-age' => 0]], - 'token:false,authenticated:true' => [FALSE, TRUE, NULL, NULL], - 'token:none,authenticated:false' => [NULL, FALSE, ['contexts' => ['user.roles:authenticated']], NULL], - 'token:false,authenticated:false' => [FALSE, FALSE, NULL, NULL], + '#token:none,#action:something,authenticated:true' => [NULL, '/some/path', TRUE, ['contexts' => ['user.roles:authenticated']], ['max-age' => 0]], + '#token:false,#action:something,authenticated:true' => [FALSE, '/some/path', TRUE, NULL, NULL], + '#token:none,#action:something,authenticated:false' => [NULL, '/some/path', FALSE, ['contexts' => ['user.roles:authenticated']], NULL], + '#token:false,#action:something,authenticated:false' => [FALSE, '/some/path', FALSE, NULL, NULL], + + '#token:none,#action:none,authenticated:true' => [NULL, NULL, TRUE, ['contexts' => ['url.path', 'url.query_args', 'user.roles:authenticated']], ['max-age' => 0]], + '#token:false,#action:none,authenticated:true' => [FALSE, NULL, TRUE, ['contexts' => ['url.path', 'url.query_args']], NULL], + '#token:none,#action:none,authenticated:false' => [NULL, NULL, FALSE, ['contexts' => ['url.path', 'url.query_args', 'user.roles:authenticated']], NULL], + '#token:false,#action:none,authenticated:false' => [FALSE, NULL, FALSE, ['contexts' => ['url.path', 'url.query_args']], NULL], ]; }