3 namespace Drupal\Tests\comment\Functional;
5 use Drupal\comment\Entity\CommentType;
6 use Drupal\comment\Entity\Comment;
7 use Drupal\comment\CommentInterface;
8 use Drupal\comment\Tests\CommentTestTrait;
9 use Drupal\field\Entity\FieldConfig;
10 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
11 use Drupal\node\Entity\NodeType;
12 use Drupal\Tests\BrowserTestBase;
15 * Provides setup and helper methods for comment tests.
17 abstract class CommentTestBase extends BrowserTestBase {
26 public static $modules = ['block', 'comment', 'node', 'history', 'field_ui', 'datetime'];
29 * An administrative user with permission to configure comment settings.
31 * @var \Drupal\user\UserInterface
36 * A normal user with permission to post comments.
38 * @var \Drupal\user\UserInterface
43 * A test node to which comments will be posted.
45 * @var \Drupal\node\NodeInterface
49 protected function setUp() {
52 // Create an article content type only if it does not yet exist, so that
53 // child classes may specify the standard profile.
54 $types = NodeType::loadMultiple();
55 if (empty($types['article'])) {
56 $this->drupalCreateContentType(['type' => 'article', 'name' => t('Article')]);
59 // Create two test users.
60 $this->adminUser = $this->drupalCreateUser([
61 'administer content types',
62 'administer comments',
63 'administer comment types',
64 'administer comment fields',
65 'administer comment display',
66 'skip comment approval',
69 // Usernames aren't shown in comment edit form autocomplete unless this
70 // permission is granted.
71 'access user profiles',
74 $this->webUser = $this->drupalCreateUser([
77 'create article content',
79 'skip comment approval',
83 // Create comment field on article.
84 $this->addDefaultCommentField('node', 'article');
86 // Create a test node authored by the web user.
87 $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
88 $this->drupalPlaceBlock('local_tasks_block');
94 * @param \Drupal\Core\Entity\EntityInterface|null $entity
95 * Node to post comment on or NULL to post to the previously loaded page.
96 * @param string $comment
98 * @param string $subject
100 * @param string $contact
101 * Set to NULL for no contact info, TRUE to ignore success checking, and
102 * array of values to set contact info.
103 * @param string $field_name
104 * (optional) Field name through which the comment should be posted.
105 * Defaults to 'comment'.
107 * @return \Drupal\comment\CommentInterface|null
108 * The posted comment or NULL when posted comment was not found.
110 public function postComment($entity, $comment, $subject = '', $contact = NULL, $field_name = 'comment') {
112 $edit['comment_body[0][value]'] = $comment;
114 if ($entity !== NULL) {
115 $field = FieldConfig::loadByName($entity->getEntityTypeId(), $entity->bundle(), $field_name);
118 $field = FieldConfig::loadByName('node', 'article', $field_name);
120 $preview_mode = $field->getSetting('preview');
122 // Must get the page before we test for fields.
123 if ($entity !== NULL) {
124 $this->drupalGet('comment/reply/' . $entity->getEntityTypeId() . '/' . $entity->id() . '/' . $field_name);
127 // Determine the visibility of subject form field.
128 if (entity_get_form_display('comment', 'comment', 'default')->getComponent('subject')) {
129 // Subject input allowed.
130 $edit['subject[0][value]'] = $subject;
133 $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.');
136 if ($contact !== NULL && is_array($contact)) {
139 switch ($preview_mode) {
140 case DRUPAL_REQUIRED:
141 // Preview required so no save button should be found.
142 $this->assertNoFieldByName('op', t('Save'), 'Save button not found.');
143 $this->drupalPostForm(NULL, $edit, t('Preview'));
144 // Don't break here so that we can test post-preview field presence and
146 case DRUPAL_OPTIONAL:
147 $this->assertFieldByName('op', t('Preview'), 'Preview button found.');
148 $this->assertFieldByName('op', t('Save'), 'Save button found.');
149 $this->drupalPostForm(NULL, $edit, t('Save'));
152 case DRUPAL_DISABLED:
153 $this->assertNoFieldByName('op', t('Preview'), 'Preview button not found.');
154 $this->assertFieldByName('op', t('Save'), 'Save button found.');
155 $this->drupalPostForm(NULL, $edit, t('Save'));
160 preg_match('/#comment-([0-9]+)/', $this->getURL(), $match);
163 if ($contact !== TRUE) {
164 // If true then attempting to find error message.
166 $this->assertText($subject, 'Comment subject posted.');
168 $this->assertText($comment, 'Comment body posted.');
169 $this->assertTrue((!empty($match) && !empty($match[1])), 'Comment id found.');
172 if (isset($match[1])) {
173 \Drupal::entityManager()->getStorage('comment')->resetCache([$match[1]]);
174 return Comment::load($match[1]);
179 * Checks current page for specified comment.
181 * @param \Drupal\comment\CommentInterface $comment
182 * The comment object.
184 * Boolean indicating whether the comment is a reply to another comment.
187 * Boolean indicating whether the comment was found.
189 public function commentExists(CommentInterface $comment = NULL, $reply = FALSE) {
191 $comment_element = $this->cssSelect('.comment-wrapper ' . ($reply ? '.indented ' : '') . '#comment-' . $comment->id() . ' ~ article');
192 if (empty($comment_element)) {
196 $comment_title = $comment_element[0]->find('xpath', 'div/h3/a');
197 if (empty($comment_title) || $comment_title->getText() !== $comment->getSubject()) {
201 $comment_body = $comment_element[0]->find('xpath', 'div/div/p');
202 if (empty($comment_body) || $comment_body->getText() !== $comment->comment_body->value) {
216 * @param \Drupal\comment\CommentInterface $comment
219 public function deleteComment(CommentInterface $comment) {
220 $this->drupalPostForm('comment/' . $comment->id() . '/delete', [], t('Delete'));
221 $this->assertText(t('The comment and all its replies have been deleted.'), 'Comment deleted.');
225 * Sets the value governing whether the subject field should be enabled.
227 * @param bool $enabled
228 * Boolean specifying whether the subject field should be enabled.
230 public function setCommentSubject($enabled) {
231 $form_display = entity_get_form_display('comment', 'comment', 'default');
233 $form_display->setComponent('subject', [
234 'type' => 'string_textfield',
238 $form_display->removeComponent('subject');
240 $form_display->save();
241 // Display status message.
242 $this->pass('Comment subject ' . ($enabled ? 'enabled' : 'disabled') . '.');
246 * Sets the value governing the previewing mode for the comment form.
249 * The preview mode: DRUPAL_DISABLED, DRUPAL_OPTIONAL or DRUPAL_REQUIRED.
250 * @param string $field_name
251 * (optional) Field name through which the comment should be posted.
252 * Defaults to 'comment'.
254 public function setCommentPreview($mode, $field_name = 'comment') {
256 case DRUPAL_DISABLED:
257 $mode_text = 'disabled';
260 case DRUPAL_OPTIONAL:
261 $mode_text = 'optional';
264 case DRUPAL_REQUIRED:
265 $mode_text = 'required';
268 $this->setCommentSettings('preview', $mode, format_string('Comment preview @mode_text.', ['@mode_text' => $mode_text]), $field_name);
272 * Sets the value governing whether the comment form is on its own page.
274 * @param bool $enabled
275 * TRUE if the comment form should be displayed on the same page as the
276 * comments; FALSE if it should be displayed on its own page.
277 * @param string $field_name
278 * (optional) Field name through which the comment should be posted.
279 * Defaults to 'comment'.
281 public function setCommentForm($enabled, $field_name = 'comment') {
282 $this->setCommentSettings('form_location', ($enabled ? CommentItemInterface::FORM_BELOW : CommentItemInterface::FORM_SEPARATE_PAGE), 'Comment controls ' . ($enabled ? 'enabled' : 'disabled') . '.', $field_name);
286 * Sets the value governing restrictions on anonymous comments.
289 * The level of the contact information allowed for anonymous comments:
290 * - 0: No contact information allowed.
291 * - 1: Contact information allowed but not required.
292 * - 2: Contact information required.
294 public function setCommentAnonymous($level) {
295 $this->setCommentSettings('anonymous', $level, format_string('Anonymous commenting set to level @level.', ['@level' => $level]));
299 * Sets the value specifying the default number of comments per page.
302 * Comments per page value.
303 * @param string $field_name
304 * (optional) Field name through which the comment should be posted.
305 * Defaults to 'comment'.
307 public function setCommentsPerPage($number, $field_name = 'comment') {
308 $this->setCommentSettings('per_page', $number, format_string('Number of comments per page set to @number.', ['@number' => $number]), $field_name);
312 * Sets a comment settings variable for the article content type.
314 * @param string $name
316 * @param string $value
318 * @param string $message
319 * Status message to display.
320 * @param string $field_name
321 * (optional) Field name through which the comment should be posted.
322 * Defaults to 'comment'.
324 public function setCommentSettings($name, $value, $message, $field_name = 'comment') {
325 $field = FieldConfig::loadByName('node', 'article', $field_name);
326 $field->setSetting($name, $value);
328 // Display status message.
329 $this->pass($message);
333 * Checks whether the commenter's contact information is displayed.
336 * Contact info is available.
338 public function commentContactInfoAvailable() {
339 return preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this->getRawContent());
343 * Performs the specified operation on the specified comment.
345 * @param \Drupal\comment\CommentInterface $comment
346 * Comment to perform operation on.
347 * @param string $operation
348 * Operation to perform.
349 * @param bool $approval
350 * Operation is found on approval page.
352 public function performCommentOperation(CommentInterface $comment, $operation, $approval = FALSE) {
354 $edit['operation'] = $operation;
355 $edit['comments[' . $comment->id() . ']'] = TRUE;
356 $this->drupalPostForm('admin/content/comment' . ($approval ? '/approval' : ''), $edit, t('Update'));
358 if ($operation == 'delete') {
359 $this->drupalPostForm(NULL, [], t('Delete'));
360 $this->assertRaw(\Drupal::translation()->formatPlural(1, 'Deleted 1 comment.', 'Deleted @count comments.'), format_string('Operation "@operation" was performed on comment.', ['@operation' => $operation]));
363 $this->assertText(t('The update has been performed.'), format_string('Operation "@operation" was performed on comment.', ['@operation' => $operation]));
368 * Gets the comment ID for an unapproved comment.
370 * @param string $subject
371 * Comment subject to find.
376 public function getUnapprovedComment($subject) {
377 $this->drupalGet('admin/content/comment/approval');
378 preg_match('/href="(.*?)#comment-([^"]+)"(.*?)>(' . $subject . ')/', $this->getRawContent(), $match);
384 * Creates a comment comment type (bundle).
386 * @param string $label
387 * The comment type label.
389 * @return \Drupal\comment\Entity\CommentType
390 * Created comment type.
392 protected function createCommentType($label) {
393 $bundle = CommentType::create([
397 'target_entity_type_id' => 'node',