View source
<?php
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\CommentInterface;
use Drupal\user\RoleInterface;
class CommentAnonymousTest extends CommentTestBase {
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments',
'post comments',
'skip comment approval',
]);
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, [
'access comments',
'post comments',
'skip comment approval',
]);
}
public function testAnonymous() {
$this
->drupalLogin($this->adminUser);
$this
->setCommentAnonymous(CommentInterface::ANONYMOUS_MAYNOT_CONTACT);
$this
->drupalLogout();
$edit = [];
$title = 'comment title with skip comment approval';
$body = 'comment body with skip comment approval';
$edit['subject[0][value]'] = $title;
$edit['comment_body[0][value]'] = $body;
$this
->drupalGet($this->node
->toUrl());
$this
->submitForm($edit, 'Preview');
$preview = (string) $this
->cssSelect('[data-drupal-selector="edit-comment-preview"]')[0]
->getHtml();
$this
->assertStringContainsString($title, $preview, 'Anonymous user can preview comment title.');
$this
->assertStringContainsString($body, $preview, 'Anonymous user can preview comment body.');
user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, [
'skip comment approval',
]);
$edit = [];
$title = 'comment title without skip comment approval';
$body = 'comment body without skip comment approval';
$edit['subject[0][value]'] = $title;
$edit['comment_body[0][value]'] = $body;
$this
->drupalGet($this->node
->toUrl());
$this
->submitForm($edit, 'Preview');
$preview = (string) $this
->cssSelect('[data-drupal-selector="edit-comment-preview"]')[0]
->getHtml();
$this
->assertStringContainsString($title, $preview, 'Anonymous user can preview comment title.');
$this
->assertStringContainsString($body, $preview, 'Anonymous user can preview comment body.');
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
'skip comment approval',
]);
$anonymous_comment1 = $this
->postComment($this->node, $this
->randomMachineName(), $this
->randomMachineName());
$this
->assertTrue($this
->commentExists($anonymous_comment1), 'Anonymous comment without contact info found.');
$edit = [
'name' => $this->adminUser
->getAccountName(),
'comment_body[0][value]' => $this
->randomMachineName(),
];
$this
->drupalGet('comment/reply/node/' . $this->node
->id() . '/comment');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('The name you used (' . $this->adminUser
->getAccountName() . ') belongs to a registered user.');
$this
->drupalLogin($this->adminUser);
$this
->setCommentAnonymous(CommentInterface::ANONYMOUS_MAY_CONTACT);
$this
->drupalGet('comment/' . $anonymous_comment1
->id() . '/edit');
$edited_comment = $this
->postComment(NULL, $this
->randomMachineName(), $this
->randomMachineName());
$this
->assertTrue($this
->commentExists($edited_comment, FALSE), 'Modified reply found.');
$this
->drupalLogout();
$this
->drupalGet('comment/reply/node/' . $this->node
->id() . '/comment');
$this
->assertTrue($this
->commentContactInfoAvailable(), 'Contact information available.');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'config:field.field.node.article.comment');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'config:user.settings');
$anonymous_comment2 = $this
->postComment($this->node, $this
->randomMachineName(), $this
->randomMachineName());
$this
->assertTrue($this
->commentExists($anonymous_comment2), 'Anonymous comment with contact info (optional) found.');
$edit = [
'name' => $this->adminUser
->getAccountName(),
'mail' => $this
->randomMachineName() . '@example.com',
'subject[0][value]' => $this
->randomMachineName(),
'comment_body[0][value]' => $this
->randomMachineName(),
];
$this
->drupalGet('comment/reply/node/' . $this->node
->id() . '/comment');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('The name you used (' . $this->adminUser
->getAccountName() . ') belongs to a registered user.');
$this
->drupalLogin($this->adminUser);
$this
->setCommentAnonymous(CommentInterface::ANONYMOUS_MUST_CONTACT);
$this
->drupalLogout();
$this
->drupalGet('comment/reply/node/' . $this->node
->id() . '/comment');
$this
->assertTrue($this
->commentContactInfoAvailable(), 'Contact information available.');
$anonymous_comment3 = $this
->postComment($this->node, $this
->randomMachineName(), $this
->randomMachineName(), TRUE);
$this
->assertSession()
->pageTextContains('Email field is required.');
$this
->assertFalse($this
->commentExists($anonymous_comment3), 'Anonymous comment with contact info (required) not found.');
$author_name = $this
->randomMachineName();
$author_mail = $this
->randomMachineName() . '@example.com';
$anonymous_comment3 = $this
->postComment($this->node, $this
->randomMachineName(), $this
->randomMachineName(), [
'name' => $author_name,
'mail' => $author_mail,
]);
$this
->assertTrue($this
->commentExists($anonymous_comment3), 'Anonymous comment with contact info (required) found.');
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('comment/' . $anonymous_comment3
->id() . '/edit');
$this
->assertSession()
->responseContains($author_name);
$this
->assertSession()
->fieldValueEquals('uid', '');
$this
->assertSession()
->responseContains($author_mail);
$this
->performCommentOperation($anonymous_comment3, 'unpublish');
$this
->drupalGet('admin/content/comment/approval');
$this
->assertSession()
->responseContains('comments[' . $anonymous_comment3
->id() . ']');
$this
->performCommentOperation($anonymous_comment3, 'publish', TRUE);
$this
->drupalGet('admin/content/comment');
$this
->assertSession()
->responseContains('comments[' . $anonymous_comment3
->id() . ']');
$this
->performCommentOperation($anonymous_comment3, 'delete');
$this
->drupalGet('admin/content/comment');
$this
->assertSession()
->responseNotContains('comments[' . $anonymous_comment3
->id() . ']');
$this
->drupalLogout();
$this
->drupalGet('comment/reply/node/' . $this->node
->id() . '/comment/' . $anonymous_comment3
->id());
$this
->assertSession()
->statusCodeEquals(403);
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments' => FALSE,
'post comments' => FALSE,
'skip comment approval' => FALSE,
]);
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertSession()
->responseNotMatches('@<h2[^>]*>Comments</h2>@');
$this
->assertSession()
->linkNotExists('Add new comment', 'Link to add comment was found.');
$this
->drupalGet('comment/reply/node/' . $this->node
->id() . '/comment');
$this
->assertSession()
->statusCodeEquals(403);
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments' => TRUE,
'post comments' => FALSE,
'skip comment approval' => FALSE,
]);
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertSession()
->responseMatches('@<h2[^>]*>Comments</h2>@');
$this
->assertSession()
->linkExists('Log in', 1, 'Link to login was found.');
$this
->assertSession()
->linkExists('register', 1, 'Link to register was found.');
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments' => FALSE,
'post comments' => TRUE,
'skip comment approval' => TRUE,
]);
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertSession()
->responseNotMatches('@<h2[^>]*>Comments</h2>@');
$this
->assertSession()
->fieldValueEquals('subject[0][value]', '');
$this
->assertSession()
->fieldValueEquals('comment_body[0][value]', '');
$this
->drupalGet('comment/reply/node/' . $this->node
->id() . '/comment/' . $anonymous_comment2
->id());
$this
->assertSession()
->statusCodeEquals(403);
}
}