View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\CommentInterface;
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 = 'stark';
protected function setUp() : void {
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(CommentInterface::ANONYMOUS_MAYNOT_CONTACT);
$this
->drupalGet('admin/content/comment');
$this
->assertSession()
->pageTextContains('No comments available.');
$this
->drupalLogout();
$subject = $this
->randomMachineName();
$body = $this
->randomMachineName();
$this
->postComment($this->node, $body, $subject, TRUE);
$this
->assertSession()
->pageTextContains('Your comment has been queued for review by site administrators and will be published after 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
->assertSession()
->pageTextContains('Unapproved comments (2)');
$edit = [
"comments[{$comments[0]->id()}]" => 1,
"comments[{$comments[1]->id()}]" => 1,
];
$this
->submitForm($edit, 'Update');
$this
->assertSession()
->pageTextContains('Unapproved comments (0)');
$edit = [
'operation' => 'delete',
"comments[{$comments[0]->id()}]" => 1,
"comments[{$comments[1]->id()}]" => 1,
"comments[{$anonymous_comment4->id()}]" => 1,
];
$this
->submitForm($edit, 'Update');
$this
->assertSession()
->pageTextContains('Are you sure you want to delete these comments and all their children?');
$this
->submitForm([], 'Delete');
$this
->assertSession()
->pageTextContains('No comments available.');
$edit = [
'operation' => 'delete',
];
$this
->submitForm($edit, 'Update');
$this
->assertSession()
->pageTextContains('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
->assertSession()
->pageTextContains(Html::escape($this->node
->label()));
$this->node
->setUnpublished()
->save();
$this
->drupalGet('admin/content/comment');
$this
->assertSession()
->pageTextNotContains(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
->setCommentAnonymous(CommentInterface::ANONYMOUS_MAYNOT_CONTACT);
$subject = $this
->randomMachineName();
$body = $this
->randomMachineName();
$this
->postComment($this->node, $body, $subject, TRUE);
$this
->assertSession()
->pageTextContains('Your comment has been queued for review by site administrators and will be published after 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
->assertSession()
->checkboxChecked('edit-status-0');
$this
->drupalGet('node/' . $this->node
->id());
$this
->clickLink('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
->assertSession()
->pageTextContains('Comment type');
$this
->assertSession()
->pageTextContains('Description');
$this
->assertSession()
->pageTextContains('Default comment field');
$this
->clickLink('Manage fields');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('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
->setCommentAnonymous(CommentInterface::ANONYMOUS_MUST_CONTACT);
$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
->assertSession()
->fieldNotExists('edit-mail');
$this
->drupalGet('comment/' . $anonymous_comment
->id() . '/edit');
$this
->assertSession()
->fieldValueEquals('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(CommentInterface::ANONYMOUS_MAYNOT_CONTACT);
$this
->drupalLogin($this->webUser);
$count_query = \Drupal::entityTypeManager()
->getStorage('comment')
->getQuery()
->accessCheck(FALSE)
->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
->drupalGet('admin/content/comment');
$this
->submitForm($edit, 'Update');
$this
->assertSession()
->responseContains(new FormattableMarkup('@label (Original translation) - <em>The following comment translations will be deleted:</em>', [
'@label' => $comment1
->label(),
]));
$this
->assertSession()
->responseContains(new FormattableMarkup('@label (Original translation) - <em>The following comment translations will be deleted:</em>', [
'@label' => $comment2
->label(),
]));
$this
->assertSession()
->pageTextContains('English');
$this
->assertSession()
->pageTextContains('Urdu');
$this
->submitForm([], 'Delete');
$after_count = $count_query
->execute();
$this
->assertEquals($before_count, $after_count, 'No comment or translation found.');
}
}