Newer
Older
catch
committed
<?php
namespace Drupal\comment\Tests;
use Drupal\user\RoleInterface;
use Drupal\comment\Entity\Comment;
catch
committed
/**
* Tests comment approval functionality.
*
* @group comment
catch
committed
*/
class CommentAdminTest extends CommentTestBase {
protected function setUp() {
parent::setUp();
$this->drupalPlaceBlock('page_title_block');
}
catch
committed
/**
* Test comment approval functionality through admin/content/comment.
*/
function testApprovalAdminInterface() {
// Set anonymous comments to require approval.
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
catch
committed
'access comments' => TRUE,
'post comments' => TRUE,
'skip comment approval' => FALSE,
]);
$this->drupalLogin($this->adminUser);
catch
committed
$this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
// Test that the comments page loads correctly when there are no comments
$this->drupalGet('admin/content/comment');
$this->assertText(t('No comments available.'));
$this->drupalLogout();
// Post anonymous comment without contact info.
Alex Pott
committed
$subject = $this->randomMachineName();
$body = $this->randomMachineName();
catch
committed
$this->postComment($this->node, $body, $subject, TRUE); // Set $contact to true so that it won't check for id and message.
Jennifer Hodgdon
committed
$this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
catch
committed
// Get unapproved comment id.
$this->drupalLogin($this->adminUser);
catch
committed
$anonymous_comment4 = $this->getUnapprovedComment($subject);
$anonymous_comment4 = Comment::create([
'cid' => $anonymous_comment4,
'subject' => $subject,
'comment_body' => $body,
'entity_id' => $this->node->id(),
'entity_type' => 'node',
'field_name' => 'comment'
]);
catch
committed
$this->drupalLogout();
Jennifer Hodgdon
committed
$this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
catch
committed
// Approve comment.
$this->drupalLogin($this->adminUser);
catch
committed
$this->performCommentOperation($anonymous_comment4, 'publish', TRUE);
$this->drupalLogout();
$this->drupalGet('node/' . $this->node->id());
Jennifer Hodgdon
committed
$this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
catch
committed
// Post 2 anonymous comments without contact info.
Alex Pott
committed
$comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
$comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
catch
committed
// Publish multiple comments in one operation.
$this->drupalLogin($this->adminUser);
catch
committed
$this->drupalGet('admin/content/comment/approval');
$this->assertText(t('Unapproved comments (@count)', ['@count' => 2]), 'Two unapproved comments waiting for approval.');
$edit = [
"comments[{$comments[0]->id()}]" => 1,
"comments[{$comments[1]->id()}]" => 1,
];
$this->drupalPostForm(NULL, $edit, t('Update'));
$this->assertText(t('Unapproved comments (@count)', ['@count' => 0]), 'All comments were approved.');
catch
committed
// Delete multiple comments in one operation.
$edit = [
catch
committed
'operation' => 'delete',
"comments[{$comments[0]->id()}]" => 1,
"comments[{$comments[1]->id()}]" => 1,
"comments[{$anonymous_comment4->id()}]" => 1,
];
$this->drupalPostForm(NULL, $edit, t('Update'));
Jennifer Hodgdon
committed
$this->assertText(t('Are you sure you want to delete these comments and all their children?'), 'Confirmation required.');
$this->drupalPostForm(NULL, $edit, t('Delete comments'));
Jennifer Hodgdon
committed
$this->assertText(t('No comments available.'), 'All comments were deleted.');
Angie Byron
committed
// Test message when no comments selected.
$edit = [
Angie Byron
committed
'operation' => 'delete',
];
Angie Byron
committed
$this->drupalPostForm(NULL, $edit, t('Update'));
$this->assertText(t('Select one or more comments to perform the update on.'));
catch
committed
}
/**
* Tests comment approval functionality through the node interface.
*/
function testApprovalNodeInterface() {
// Set anonymous comments to require approval.
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
catch
committed
'access comments' => TRUE,
'post comments' => TRUE,
'skip comment approval' => FALSE,
]);
$this->drupalLogin($this->adminUser);
catch
committed
$this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
$this->drupalLogout();
// Post anonymous comment without contact info.
Alex Pott
committed
$subject = $this->randomMachineName();
$body = $this->randomMachineName();
catch
committed
$this->postComment($this->node, $body, $subject, TRUE); // Set $contact to true so that it won't check for id and message.
Jennifer Hodgdon
committed
$this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
catch
committed
// Get unapproved comment id.
$this->drupalLogin($this->adminUser);
catch
committed
$anonymous_comment4 = $this->getUnapprovedComment($subject);
$anonymous_comment4 = Comment::create([
'cid' => $anonymous_comment4,
'subject' => $subject,
'comment_body' => $body,
'entity_id' => $this->node->id(),
'entity_type' => 'node',
'field_name' => 'comment'
]);
catch
committed
$this->drupalLogout();
Jennifer Hodgdon
committed
$this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
catch
committed
// Approve comment.
$this->drupalLogin($this->adminUser);
catch
committed
$this->drupalGet('comment/1/approve');
Jennifer Hodgdon
committed
$this->assertResponse(403, 'Forged comment approval was denied.');
$this->drupalGet('comment/1/approve', ['query' => ['token' => 'forged']]);
Jennifer Hodgdon
committed
$this->assertResponse(403, 'Forged comment approval was denied.');
Alex Pott
committed
$this->drupalGet('comment/1/edit');
$this->assertFieldChecked('edit-status-0');
$this->drupalGet('node/' . $this->node->id());
$this->clickLink(t('Approve'));
catch
committed
$this->drupalLogout();
$this->drupalGet('node/' . $this->node->id());
Jennifer Hodgdon
committed
$this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
catch
committed
}
/**
* Tests comment bundle admin.
*/
public function testCommentAdmin() {
// Login.
$this->drupalLogin($this->adminUser);
// Browse to comment bundle overview.
$this->drupalGet('admin/structure/comment');
$this->assertResponse(200);
// Make sure titles visible.
$this->assertText('Comment type');
Alex Pott
committed
$this->assertText('Description');
// Make sure the description is present.
$this->assertText('Default comment field');
// Manage fields.
$this->clickLink('Manage fields');
$this->assertResponse(200);
// Make sure comment_body field is shown.
$this->assertText('comment_body');
// Rest from here on in is field_ui.
}
Alex Pott
committed
/**
* Tests editing a comment as an admin.
*/
public function testEditComment() {
// Enable anonymous user comments.
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
Alex Pott
committed
'access comments',
'post comments',
'skip comment approval',
]);
Alex Pott
committed
// Log in as a web user.
Alex Pott
committed
$this->drupalLogin($this->webUser);
// Post a comment.
$comment = $this->postComment($this->node, $this->randomMachineName());
$this->drupalLogout();
// Post anonymous comment.
$this->drupalLogin($this->adminUser);
$this->setCommentAnonymous('2'); // Ensure that we need email id before posting comment.
$this->drupalLogout();
// Post comment with contact info (required).
$author_name = $this->randomMachineName();
$author_mail = $this->randomMachineName() . '@example.com';
$anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), ['name' => $author_name, 'mail' => $author_mail]);
Alex Pott
committed
// Log in as an admin user.
Alex Pott
committed
$this->drupalLogin($this->adminUser);
// Make sure the comment field is not visible when
// the comment was posted by an authenticated user.
$this->drupalGet('comment/' . $comment->id() . '/edit');
$this->assertNoFieldById('edit-mail', $comment->getAuthorEmail());
// Make sure the comment field is visible when
// the comment was posted by an anonymous user.
$this->drupalGet('comment/' . $anonymous_comment->id() . '/edit');
$this->assertFieldById('edit-mail', $anonymous_comment->getAuthorEmail());
}