View source
<?php
namespace Drupal\Tests\comment\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\comment\Entity\CommentType;
use Drupal\comment\Entity\Comment;
use Drupal\comment\CommentInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\field\Entity\FieldConfig;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\BrowserTestBase;
abstract class CommentTestBase extends BrowserTestBase {
use CommentTestTrait;
protected static $modules = [
'block',
'comment',
'node',
'history',
'field_ui',
'datetime',
];
protected $adminUser;
protected $webUser;
protected $node;
protected function setUp() : void {
parent::setUp();
$types = NodeType::loadMultiple();
if (empty($types['article'])) {
$this
->drupalCreateContentType([
'type' => 'article',
'name' => t('Article'),
]);
}
$this->adminUser = $this
->drupalCreateUser([
'administer content types',
'administer comments',
'administer comment types',
'administer comment fields',
'administer comment display',
'skip comment approval',
'post comments',
'access comments',
'access user profiles',
'access content',
]);
$this->webUser = $this
->drupalCreateUser([
'access comments',
'post comments',
'create article content',
'edit own comments',
'skip comment approval',
'access content',
]);
$this
->addDefaultCommentField('node', 'article');
$this->node = $this
->drupalCreateNode([
'type' => 'article',
'promote' => 1,
'uid' => $this->webUser
->id(),
]);
$this
->drupalPlaceBlock('local_tasks_block');
}
public function postComment($entity, $comment, $subject = '', $contact = NULL, $field_name = 'comment') {
$edit = [];
$edit['comment_body[0][value]'] = $comment;
if ($entity !== NULL) {
$field = FieldConfig::loadByName($entity
->getEntityTypeId(), $entity
->bundle(), $field_name);
}
else {
$field = FieldConfig::loadByName('node', 'article', $field_name);
}
$preview_mode = $field
->getSetting('preview');
if ($entity !== NULL) {
$this
->drupalGet('comment/reply/' . $entity
->getEntityTypeId() . '/' . $entity
->id() . '/' . $field_name);
}
$display_repository = $this->container
->get('entity_display.repository');
if ($display_repository
->getFormDisplay('comment', 'comment')
->getComponent('subject')) {
$edit['subject[0][value]'] = $subject;
}
else {
$this
->assertSession()
->fieldNotExists('subject[0][value]');
}
if ($contact !== NULL && is_array($contact)) {
$edit += $contact;
}
switch ($preview_mode) {
case DRUPAL_REQUIRED:
$this
->assertSession()
->buttonNotExists('Save');
$this
->submitForm($edit, 'Preview');
case DRUPAL_OPTIONAL:
$this
->assertSession()
->buttonExists('Preview');
$this
->assertSession()
->buttonExists('Save');
$this
->submitForm($edit, 'Save');
break;
case DRUPAL_DISABLED:
$this
->assertSession()
->buttonNotExists('Preview');
$this
->assertSession()
->buttonExists('Save');
$this
->submitForm($edit, 'Save');
break;
}
$match = [];
preg_match('/#comment-([0-9]+)/', $this
->getURL(), $match);
if ($contact !== TRUE) {
if ($subject) {
$this
->assertSession()
->pageTextContains($subject);
}
$this
->assertSession()
->pageTextContains($comment);
$this
->assertArrayHasKey(1, $match);
}
if (isset($match[1])) {
\Drupal::entityTypeManager()
->getStorage('comment')
->resetCache([
$match[1],
]);
return Comment::load($match[1]);
}
}
public function commentExists(CommentInterface $comment = NULL, $reply = FALSE) {
if ($comment) {
$comment_element = $this
->cssSelect(($reply ? '.indented ' : '') . 'article#comment-' . $comment
->id());
if (empty($comment_element)) {
return FALSE;
}
$comment_title = $comment_element[0]
->find('xpath', 'div/h3/a');
if (empty($comment_title) || $comment_title
->getText() !== $comment
->getSubject()) {
return FALSE;
}
$comment_body = $comment_element[0]
->find('xpath', 'div/div/p');
if (empty($comment_body) || $comment_body
->getText() !== $comment->comment_body->value) {
return FALSE;
}
return TRUE;
}
else {
return FALSE;
}
}
public function deleteComment(CommentInterface $comment) {
$this
->drupalGet('comment/' . $comment
->id() . '/delete');
$this
->submitForm([], 'Delete');
$this
->assertSession()
->pageTextContains('The comment and all its replies have been deleted.');
}
public function setCommentSubject($enabled) {
$form_display = $this->container
->get('entity_display.repository')
->getFormDisplay('comment', 'comment');
if ($enabled) {
$form_display
->setComponent('subject', [
'type' => 'string_textfield',
]);
}
else {
$form_display
->removeComponent('subject');
}
$form_display
->save();
}
public function setCommentPreview($mode, $field_name = 'comment') {
switch ($mode) {
case DRUPAL_DISABLED:
$mode_text = 'disabled';
break;
case DRUPAL_OPTIONAL:
$mode_text = 'optional';
break;
case DRUPAL_REQUIRED:
$mode_text = 'required';
break;
}
$this
->setCommentSettings('preview', $mode, new FormattableMarkup('Comment preview @mode_text.', [
'@mode_text' => $mode_text,
]), $field_name);
}
public function setCommentForm($enabled, $field_name = 'comment') {
$this
->setCommentSettings('form_location', $enabled ? CommentItemInterface::FORM_BELOW : CommentItemInterface::FORM_SEPARATE_PAGE, 'Comment controls ' . ($enabled ? 'enabled' : 'disabled') . '.', $field_name);
}
public function setCommentAnonymous($level) {
$this
->setCommentSettings('anonymous', $level, new FormattableMarkup('Anonymous commenting set to level @level.', [
'@level' => $level,
]));
}
public function setCommentsPerPage($number, $field_name = 'comment') {
$this
->setCommentSettings('per_page', $number, new FormattableMarkup('Number of comments per page set to @number.', [
'@number' => $number,
]), $field_name);
}
public function setCommentSettings($name, $value, $message, $field_name = 'comment') {
$field = FieldConfig::loadByName('node', 'article', $field_name);
$field
->setSetting($name, $value);
$field
->save();
}
public function commentContactInfoAvailable() {
return (bool) preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this
->getSession()
->getPage()
->getContent());
}
public function performCommentOperation(CommentInterface $comment, $operation, $approval = FALSE) {
$edit = [];
$edit['operation'] = $operation;
$edit['comments[' . $comment
->id() . ']'] = TRUE;
$this
->drupalGet('admin/content/comment' . ($approval ? '/approval' : ''));
$this
->submitForm($edit, 'Update');
if ($operation == 'delete') {
$this
->submitForm([], 'Delete');
$this
->assertSession()
->pageTextContains('Deleted 1 comment.');
}
else {
$this
->assertSession()
->pageTextContains('The update has been performed.');
}
}
public function getUnapprovedComment($subject) {
$this
->drupalGet('admin/content/comment/approval');
preg_match('/href="(.*?)#comment-([^"]+)"(.*?)>(' . $subject . ')/', $this
->getSession()
->getPage()
->getContent(), $match);
return $match[2];
}
protected function createCommentType($label) {
$bundle = CommentType::create([
'id' => $label,
'label' => $label,
'description' => '',
'target_entity_type_id' => 'node',
]);
$bundle
->save();
return $bundle;
}
}