Chris@0: drupalLogin($this->adminUser); Chris@18: $this->setCommentAnonymous(CommentInterface::ANONYMOUS_MAYNOT_CONTACT); Chris@0: $this->drupalLogout(); Chris@0: Chris@0: // Preview comments (with `skip comment approval` permission). Chris@0: $edit = []; Chris@0: $title = 'comment title with skip comment approval'; Chris@0: $body = 'comment body with skip comment approval'; Chris@0: $edit['subject[0][value]'] = $title; Chris@0: $edit['comment_body[0][value]'] = $body; Chris@18: $this->drupalPostForm($this->node->toUrl(), $edit, t('Preview')); Chris@0: // Cannot use assertRaw here since both title and body are in the form. Chris@0: $preview = (string) $this->cssSelect('.preview')[0]->getHtml(); Chris@0: $this->assertTrue(strpos($preview, $title) !== FALSE, 'Anonymous user can preview comment title.'); Chris@0: $this->assertTrue(strpos($preview, $body) !== FALSE, 'Anonymous user can preview comment body.'); Chris@0: Chris@0: // Preview comments (without `skip comment approval` permission). Chris@0: user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, ['skip comment approval']); Chris@0: $edit = []; Chris@0: $title = 'comment title without skip comment approval'; Chris@0: $body = 'comment body without skip comment approval'; Chris@0: $edit['subject[0][value]'] = $title; Chris@0: $edit['comment_body[0][value]'] = $body; Chris@18: $this->drupalPostForm($this->node->toUrl(), $edit, t('Preview')); Chris@0: // Cannot use assertRaw here since both title and body are in the form. Chris@0: $preview = (string) $this->cssSelect('.preview')[0]->getHtml(); Chris@0: $this->assertTrue(strpos($preview, $title) !== FALSE, 'Anonymous user can preview comment title.'); Chris@0: $this->assertTrue(strpos($preview, $body) !== FALSE, 'Anonymous user can preview comment body.'); Chris@0: user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['skip comment approval']); Chris@0: Chris@0: // Post anonymous comment without contact info. Chris@0: $anonymous_comment1 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName()); Chris@0: $this->assertTrue($this->commentExists($anonymous_comment1), 'Anonymous comment without contact info found.'); Chris@0: Chris@0: // Ensure anonymous users cannot post in the name of registered users. Chris@0: $edit = [ Chris@18: 'name' => $this->adminUser->getAccountName(), Chris@0: 'comment_body[0][value]' => $this->randomMachineName(), Chris@0: ]; Chris@0: $this->drupalPostForm('comment/reply/node/' . $this->node->id() . '/comment', $edit, t('Save')); Chris@0: $this->assertRaw(t('The name you used (%name) belongs to a registered user.', [ Chris@18: '%name' => $this->adminUser->getAccountName(), Chris@0: ])); Chris@0: Chris@0: // Allow contact info. Chris@0: $this->drupalLogin($this->adminUser); Chris@18: $this->setCommentAnonymous(CommentInterface::ANONYMOUS_MAY_CONTACT); Chris@0: Chris@0: // Attempt to edit anonymous comment. Chris@0: $this->drupalGet('comment/' . $anonymous_comment1->id() . '/edit'); Chris@0: $edited_comment = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName()); Chris@0: $this->assertTrue($this->commentExists($edited_comment, FALSE), 'Modified reply found.'); Chris@0: $this->drupalLogout(); Chris@0: Chris@0: // Post anonymous comment with contact info (optional). Chris@0: $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment'); Chris@0: $this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.'); Chris@0: Chris@0: // Check the presence of expected cache tags. Chris@0: $this->assertCacheTag('config:field.field.node.article.comment'); Chris@0: $this->assertCacheTag('config:user.settings'); Chris@0: Chris@0: $anonymous_comment2 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName()); Chris@0: $this->assertTrue($this->commentExists($anonymous_comment2), 'Anonymous comment with contact info (optional) found.'); Chris@0: Chris@0: // Ensure anonymous users cannot post in the name of registered users. Chris@0: $edit = [ Chris@18: 'name' => $this->adminUser->getAccountName(), Chris@0: 'mail' => $this->randomMachineName() . '@example.com', Chris@0: 'subject[0][value]' => $this->randomMachineName(), Chris@0: 'comment_body[0][value]' => $this->randomMachineName(), Chris@0: ]; Chris@0: $this->drupalPostForm('comment/reply/node/' . $this->node->id() . '/comment', $edit, t('Save')); Chris@0: $this->assertRaw(t('The name you used (%name) belongs to a registered user.', [ Chris@18: '%name' => $this->adminUser->getAccountName(), Chris@0: ])); Chris@0: Chris@0: // Require contact info. Chris@0: $this->drupalLogin($this->adminUser); Chris@18: $this->setCommentAnonymous(CommentInterface::ANONYMOUS_MUST_CONTACT); Chris@0: $this->drupalLogout(); Chris@0: Chris@0: // Try to post comment with contact info (required). Chris@0: $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment'); Chris@0: $this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.'); Chris@0: Chris@0: $anonymous_comment3 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE); Chris@0: // Name should have 'Anonymous' for value by default. Chris@0: $this->assertText(t('Email field is required.'), 'Email required.'); Chris@0: $this->assertFalse($this->commentExists($anonymous_comment3), 'Anonymous comment with contact info (required) not found.'); Chris@0: Chris@0: // Post comment with contact info (required). Chris@0: $author_name = $this->randomMachineName(); Chris@0: $author_mail = $this->randomMachineName() . '@example.com'; Chris@0: $anonymous_comment3 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), ['name' => $author_name, 'mail' => $author_mail]); Chris@0: $this->assertTrue($this->commentExists($anonymous_comment3), 'Anonymous comment with contact info (required) found.'); Chris@0: Chris@0: // Make sure the user data appears correctly when editing the comment. Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $this->drupalGet('comment/' . $anonymous_comment3->id() . '/edit'); Chris@0: $this->assertRaw($author_name, "The anonymous user's name is correct when editing the comment."); Chris@0: $this->assertFieldByName('uid', '', 'The author field is empty (i.e. anonymous) when editing the comment.'); Chris@0: $this->assertRaw($author_mail, "The anonymous user's email address is correct when editing the comment."); Chris@0: Chris@0: // Unpublish comment. Chris@0: $this->performCommentOperation($anonymous_comment3, 'unpublish'); Chris@0: Chris@0: $this->drupalGet('admin/content/comment/approval'); Chris@0: $this->assertRaw('comments[' . $anonymous_comment3->id() . ']', 'Comment was unpublished.'); Chris@0: Chris@0: // Publish comment. Chris@0: $this->performCommentOperation($anonymous_comment3, 'publish', TRUE); Chris@0: Chris@0: $this->drupalGet('admin/content/comment'); Chris@0: $this->assertRaw('comments[' . $anonymous_comment3->id() . ']', 'Comment was published.'); Chris@0: Chris@0: // Delete comment. Chris@0: $this->performCommentOperation($anonymous_comment3, 'delete'); Chris@0: Chris@0: $this->drupalGet('admin/content/comment'); Chris@0: $this->assertNoRaw('comments[' . $anonymous_comment3->id() . ']', 'Comment was deleted.'); Chris@0: $this->drupalLogout(); Chris@0: Chris@0: // Comment 3 was deleted. Chris@0: $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $anonymous_comment3->id()); Chris@0: $this->assertResponse(403); Chris@0: Chris@0: // Reset. Chris@0: user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [ Chris@0: 'access comments' => FALSE, Chris@0: 'post comments' => FALSE, Chris@0: 'skip comment approval' => FALSE, Chris@0: ]); Chris@0: Chris@0: // Attempt to view comments while disallowed. Chris@0: // NOTE: if authenticated user has permission to post comments, then a Chris@0: // "Login or register to post comments" type link may be shown. Chris@0: $this->drupalGet('node/' . $this->node->id()); Chris@18: $this->assertSession()->responseNotMatches('@