Newer
Older
catch
committed
<?php
namespace Drupal\comment\Tests;
use Drupal\user\RoleInterface;
catch
committed
/**
* Tests anonymous commenting.
*
* @group comment
catch
committed
*/
class CommentAnonymousTest extends CommentTestBase {
Alex Pott
committed
protected function setUp() {
catch
committed
parent::setUp();
Angie Byron
committed
// Enable anonymous and authenticated user comments.
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
Angie Byron
committed
'access comments',
'post comments',
'skip comment approval',
]);
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, [
Angie Byron
committed
'access comments',
'post comments',
'skip comment approval',
]);
catch
committed
}
/**
* Tests anonymous comment functionality.
*/
public function testAnonymous() {
$this->drupalLogin($this->adminUser);
$this->setCommentAnonymous(COMMENT_ANONYMOUS_MAYNOT_CONTACT);
catch
committed
$this->drupalLogout();
Angie Byron
committed
// Preview comments (with `skip comment approval` permission).
$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->drupalPostForm($this->node->urlInfo(), $edit, t('Preview'));
// Cannot use assertRaw here since both title and body are in the form.
$preview = (string) $this->cssSelect('.preview')[0]->asXML();
$this->assertTrue(strpos($preview, $title) !== FALSE, 'Anonymous user can preview comment title.');
$this->assertTrue(strpos($preview, $body) !== FALSE, 'Anonymous user can preview comment body.');
// Preview comments (without `skip comment approval` permission).
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->drupalPostForm($this->node->urlInfo(), $edit, t('Preview'));
// Cannot use assertRaw here since both title and body are in the form.
$preview = (string) $this->cssSelect('.preview')[0]->asXML();
$this->assertTrue(strpos($preview, $title) !== FALSE, 'Anonymous user can preview comment title.');
$this->assertTrue(strpos($preview, $body) !== FALSE, 'Anonymous user can preview comment body.');
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['skip comment approval']);
catch
committed
// Post anonymous comment without contact info.
Alex Pott
committed
$anonymous_comment1 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName());
Jennifer Hodgdon
committed
$this->assertTrue($this->commentExists($anonymous_comment1), 'Anonymous comment without contact info found.');
catch
committed
// Ensure anonymous users cannot post in the name of registered users.
$edit = [
'name' => $this->adminUser->getUsername(),
'comment_body[0][value]' => $this->randomMachineName(),
];
$this->drupalPostForm('comment/reply/node/' . $this->node->id() . '/comment', $edit, t('Save'));
$this->assertRaw(t('The name you used (%name) belongs to a registered user.', [
'%name' => $this->adminUser->getUsername(),
]));
catch
committed
// Allow contact info.
$this->drupalLogin($this->adminUser);
$this->setCommentAnonymous(COMMENT_ANONYMOUS_MAY_CONTACT);
catch
committed
// Attempt to edit anonymous comment.
$this->drupalGet('comment/' . $anonymous_comment1->id() . '/edit');
Alex Pott
committed
$edited_comment = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName());
Jennifer Hodgdon
committed
$this->assertTrue($this->commentExists($edited_comment, FALSE), 'Modified reply found.');
catch
committed
$this->drupalLogout();
// Post anonymous comment with contact info (optional).
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
Jennifer Hodgdon
committed
$this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.');
catch
committed
// Check the presence of expected cache tags.
$this->assertCacheTag('config:field.field.node.article.comment');
$this->assertCacheTag('config:user.settings');
Alex Pott
committed
$anonymous_comment2 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName());
Jennifer Hodgdon
committed
$this->assertTrue($this->commentExists($anonymous_comment2), 'Anonymous comment with contact info (optional) found.');
catch
committed
// Ensure anonymous users cannot post in the name of registered users.
$edit = [
'name' => $this->adminUser->getUsername(),
Alex Pott
committed
'mail' => $this->randomMachineName() . '@example.com',
'subject[0][value]' => $this->randomMachineName(),
'comment_body[0][value]' => $this->randomMachineName(),
];
$this->drupalPostForm('comment/reply/node/' . $this->node->id() . '/comment', $edit, t('Save'));
$this->assertRaw(t('The name you used (%name) belongs to a registered user.', [
'%name' => $this->adminUser->getUsername(),
]));
catch
committed
// Require contact info.
$this->drupalLogin($this->adminUser);
$this->setCommentAnonymous(COMMENT_ANONYMOUS_MUST_CONTACT);
catch
committed
$this->drupalLogout();
// Try to post comment with contact info (required).
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
Jennifer Hodgdon
committed
$this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.');
catch
committed
Alex Pott
committed
$anonymous_comment3 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
Jennifer Hodgdon
committed
// Name should have 'Anonymous' for value by default.
$this->assertText(t('Email field is required.'), 'Email required.');
Jennifer Hodgdon
committed
$this->assertFalse($this->commentExists($anonymous_comment3), 'Anonymous comment with contact info (required) not found.');
catch
committed
// Post comment with contact info (required).
Alex Pott
committed
$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]);
Jennifer Hodgdon
committed
$this->assertTrue($this->commentExists($anonymous_comment3), 'Anonymous comment with contact info (required) found.');
catch
committed
// Make sure the user data appears correctly when editing the comment.
$this->drupalLogin($this->adminUser);
$this->drupalGet('comment/' . $anonymous_comment3->id() . '/edit');
Jennifer Hodgdon
committed
$this->assertRaw($author_name, "The anonymous user's name is correct when editing the comment.");
$this->assertFieldByName('uid', '', 'The author field is empty (i.e. anonymous) when editing the comment.');
$this->assertRaw($author_mail, "The anonymous user's email address is correct when editing the comment.");
catch
committed
// Unpublish comment.
$this->performCommentOperation($anonymous_comment3, 'unpublish');
$this->drupalGet('admin/content/comment/approval');
$this->assertRaw('comments[' . $anonymous_comment3->id() . ']', 'Comment was unpublished.');
catch
committed
// Publish comment.
$this->performCommentOperation($anonymous_comment3, 'publish', TRUE);
$this->drupalGet('admin/content/comment');
$this->assertRaw('comments[' . $anonymous_comment3->id() . ']', 'Comment was published.');
catch
committed
// Delete comment.
$this->performCommentOperation($anonymous_comment3, 'delete');
$this->drupalGet('admin/content/comment');
$this->assertNoRaw('comments[' . $anonymous_comment3->id() . ']', 'Comment was deleted.');
catch
committed
$this->drupalLogout();
// Comment 3 was deleted.
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $anonymous_comment3->id());
$this->assertResponse(403);
catch
committed
// Reset.
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
catch
committed
'access comments' => FALSE,
'post comments' => FALSE,
'skip comment approval' => FALSE,
]);
catch
committed
// Attempt to view comments while disallowed.
// NOTE: if authenticated user has permission to post comments, then a
// "Login or register to post comments" type link may be shown.
$this->drupalGet('node/' . $this->node->id());
Jennifer Hodgdon
committed
$this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
$this->assertNoLink('Add new comment', 'Link to add comment was found.');
catch
committed
// Attempt to view node-comment form while disallowed.
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
$this->assertResponse(403);
catch
committed
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
catch
committed
'access comments' => TRUE,
'post comments' => FALSE,
'skip comment approval' => FALSE,
]);
$this->drupalGet('node/' . $this->node->id());
Jennifer Hodgdon
committed
$this->assertPattern('@<h2[^>]*>Comments</h2>@', 'Comments were displayed.');
$this->assertLink('Log in', 1, 'Link to login was found.');
Jennifer Hodgdon
committed
$this->assertLink('register', 1, 'Link to register was found.');
catch
committed
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
catch
committed
'access comments' => FALSE,
'post comments' => TRUE,
'skip comment approval' => TRUE,
]);
$this->drupalGet('node/' . $this->node->id());
Jennifer Hodgdon
committed
$this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
$this->assertFieldByName('subject[0][value]', '', 'Subject field found.');
Angie Byron
committed
$this->assertFieldByName('comment_body[0][value]', '', 'Comment field found.');
catch
committed
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $anonymous_comment2->id());
$this->assertResponse(403);
catch
committed
}
Angie Byron
committed