View source
<?php
namespace Drupal\Tests\comment\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Html;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\user\RoleInterface;
use Drupal\comment\Entity\Comment;
class CommentAdminTest extends CommentTestBase {
protected $defaultTheme = 'classy';
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('page_title_block');
}
public function testApprovalAdminInterface() {
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments' => TRUE,
'post comments' => TRUE,
'skip comment approval' => FALSE,
]);
$this
->drupalLogin($this->adminUser);
$this
->setCommentAnonymous('0');
$this
->drupalGet('admin/content/comment');
$this
->assertText(t('No comments available.'));
$this
->drupalLogout();
$subject = $this
->randomMachineName();
$body = $this
->randomMachineName();
$this
->postComment($this->node, $body, $subject, TRUE);
$this
->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
$this
->drupalLogin($this->adminUser);
$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',
]);
$this
->drupalLogout();
$this
->assertFalse($this
->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
$this
->drupalLogin($this->adminUser);
$this
->performCommentOperation($anonymous_comment4, 'publish', TRUE);
$this
->drupalLogout();
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertTrue($this
->commentExists($anonymous_comment4), 'Anonymous comment visible.');
$comments[] = $this
->postComment($this->node, $this
->randomMachineName(), $this
->randomMachineName(), TRUE);
$comments[] = $this
->postComment($this->node, $this
->randomMachineName(), $this
->randomMachineName(), TRUE);
$this
->drupalLogin($this->adminUser);
$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.');
$edit = [
'operation' => 'delete',
"comments[{$comments[0]->id()}]" => 1,
"comments[{$comments[1]->id()}]" => 1,
"comments[{$anonymous_comment4->id()}]" => 1,
];
$this
->drupalPostForm(NULL, $edit, t('Update'));
$this
->assertText(t('Are you sure you want to delete these comments and all their children?'), 'Confirmation required.');
$this
->drupalPostForm(NULL, [], t('Delete'));
$this
->assertText(t('No comments available.'), 'All comments were deleted.');
$edit = [
'operation' => 'delete',
];
$this
->drupalPostForm(NULL, $edit, t('Update'));
$this
->assertText(t('Select one or more comments to perform the update on.'));
$this
->drupalGet('admin/content/comment');
$this
->postComment($this->node, $this
->randomMachineName());
$this
->drupalGet('admin/content/comment');
$this
->assertText(Html::escape($this->node
->label()));
$this->node
->setUnpublished()
->save();
$this
->drupalGet('admin/content/comment');
$this
->assertNoText(Html::escape($this->node
->label()));
}
public function testApprovalNodeInterface() {
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments' => TRUE,
'post comments' => TRUE,
'skip comment approval' => FALSE,
]);
$this
->drupalLogin($this->adminUser);
$this
->setCommentAnonymous('0');
$this
->drupalLogout();
$subject = $this
->randomMachineName();
$body = $this
->randomMachineName();
$this
->postComment($this->node, $body, $subject, TRUE);
$this
->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
$this
->drupalLogin($this->adminUser);
$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',
]);
$this
->drupalLogout();
$this
->assertFalse($this
->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('comment/1/approve');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet('comment/1/approve', [
'query' => [
'token' => 'forged',
],
]);
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet('comment/1/edit');
$this
->assertFieldChecked('edit-status-0');
$this
->drupalGet('node/' . $this->node
->id());
$this
->clickLink(t('Approve'));
$this
->drupalLogout();
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertTrue($this
->commentExists($anonymous_comment4), 'Anonymous comment visible.');
}
public function testCommentAdmin() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/structure/comment');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertText('Comment type');
$this
->assertText('Description');
$this
->assertText('Default comment field');
$this
->clickLink('Manage fields');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertText('comment_body');
}
public function testEditComment() {
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
'access comments',
'post comments',
'skip comment approval',
]);
$this
->drupalLogin($this->webUser);
$comment = $this
->postComment($this->node, $this
->randomMachineName());
$this
->drupalLogout();
$this
->drupalLogin($this->adminUser);
$this
->setCommentAnonymous('2');
$this
->drupalLogout();
$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,
]);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('comment/' . $comment
->id() . '/edit');
$this
->assertNoFieldById('edit-mail', $comment
->getAuthorEmail());
$this
->drupalGet('comment/' . $anonymous_comment
->id() . '/edit');
$this
->assertFieldById('edit-mail', $anonymous_comment
->getAuthorEmail());
}
public function testCommentedTranslationDeletion() {
\Drupal::service('module_installer')
->install([
'language',
'locale',
]);
\Drupal::service('router.builder')
->rebuildIfNeeded();
ConfigurableLanguage::createFromLangcode('ur')
->save();
$this
->rebuildContainer();
$this
->setCommentAnonymous('0');
$this
->drupalLogin($this->webUser);
$count_query = \Drupal::entityTypeManager()
->getStorage('comment')
->getQuery()
->count();
$before_count = $count_query
->execute();
$comment1 = $this
->postComment($this->node, $this
->randomMachineName(), $this
->randomMachineName(), TRUE);
$comment2 = $this
->postComment($this->node, $this
->randomMachineName(), $this
->randomMachineName(), TRUE);
$comment1
->addTranslation('ur', [
'subject' => 'ur ' . $comment1
->label(),
])
->save();
$comment2
->addTranslation('ur', [
'subject' => 'ur ' . $comment1
->label(),
])
->save();
$this
->drupalLogout();
$this
->drupalLogin($this->adminUser);
$edit = [
'operation' => 'delete',
"comments[{$comment1->id()}]" => 1,
"comments[{$comment2->id()}]" => 1,
];
$this
->drupalPostForm('admin/content/comment', $edit, t('Update'));
$this
->assertRaw(new FormattableMarkup('@label (Original translation) - <em>The following comment translations will be deleted:</em>', [
'@label' => $comment1
->label(),
]));
$this
->assertRaw(new FormattableMarkup('@label (Original translation) - <em>The following comment translations will be deleted:</em>', [
'@label' => $comment2
->label(),
]));
$this
->assertText('English');
$this
->assertText('Urdu');
$this
->drupalPostForm(NULL, [], t('Delete'));
$after_count = $count_query
->execute();
$this
->assertEqual($after_count, $before_count, 'No comment or translation found.');
}
}