core/modules/comment/src/CommentForm.php | 11 ++++++++++- core/modules/comment/src/Tests/CommentAnonymousTest.php | 7 ++++--- core/modules/system/config/install/system.performance.yml | 2 +- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index 95629b2..8f7368a 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -94,6 +94,10 @@ public function form(array $form, FormStateInterface $form_state) { $form['#attributes']['data-user-info-from-browser'] = TRUE; } + // Vary per role, because we check a permission above and attach an asset + // library only for authenticated users. + $form['#cache']['contexts'][] = 'user.roles'; + // If not replying to a comment, use our dedicated page callback for new // Comments on entities. if (!$comment->id() && !$comment->hasParentComment()) { @@ -210,7 +214,12 @@ public function form(array $form, FormStateInterface $form_state) { '#access' => $is_admin, ); - $form['#cache']['tags'] = Cache::mergeTags(isset($form['#cache']['tags']) ? $form['#cache']['tags'] : [], $config->getCacheTags()); + $form['#cache']['tags'] = Cache::mergeTags( + isset($form['#cache']['tags']) ? $form['#cache']['tags'] : [], + $config->getCacheTags(), + // The form depends on the field definition. + $field_definition->getConfig($entity->bundle())->getCacheTags() + ); return parent::form($form, $form_state, $comment); } diff --git a/core/modules/comment/src/Tests/CommentAnonymousTest.php b/core/modules/comment/src/Tests/CommentAnonymousTest.php index 8305651..8143819 100644 --- a/core/modules/comment/src/Tests/CommentAnonymousTest.php +++ b/core/modules/comment/src/Tests/CommentAnonymousTest.php @@ -35,7 +35,7 @@ protected function setUp() { */ function testAnonymous() { $this->drupalLogin($this->adminUser); - $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info. + $this->setCommentAnonymous(COMMENT_ANONYMOUS_MAYNOT_CONTACT); $this->drupalLogout(); // Post anonymous comment without contact info. @@ -44,7 +44,7 @@ function testAnonymous() { // Allow contact info. $this->drupalLogin($this->adminUser); - $this->setCommentAnonymous('1'); + $this->setCommentAnonymous(COMMENT_ANONYMOUS_MAY_CONTACT); // Attempt to edit anonymous comment. $this->drupalGet('comment/' . $anonymous_comment1->id() . '/edit'); @@ -57,6 +57,7 @@ function testAnonymous() { $this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.'); // Check the presence of expected cache tags. + $this->assertCacheTag('config:field.field.node.article.comment'); $this->assertCacheTag('config:user.settings'); $anonymous_comment2 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName()); @@ -76,7 +77,7 @@ function testAnonymous() { // Require contact info. $this->drupalLogin($this->adminUser); - $this->setCommentAnonymous('2'); + $this->setCommentAnonymous(COMMENT_ANONYMOUS_MUST_CONTACT); $this->drupalLogout(); // Try to post comment with contact info (required). diff --git a/core/modules/system/config/install/system.performance.yml b/core/modules/system/config/install/system.performance.yml index 1e75b4b..98f34f0 100644 --- a/core/modules/system/config/install/system.performance.yml +++ b/core/modules/system/config/install/system.performance.yml @@ -1,6 +1,6 @@ cache: page: - use_internal: false + use_internal: true max_age: 0 css: preprocess: true