Chris@0: drupalCreateContentType(['type' => 'article', 'name' => t('Article')]); Chris@0: } Chris@0: Chris@0: // Create two test users. Chris@0: $this->adminUser = $this->drupalCreateUser([ Chris@0: 'administer content types', Chris@0: 'administer comments', Chris@0: 'administer comment types', Chris@0: 'administer comment fields', Chris@0: 'administer comment display', Chris@0: 'skip comment approval', Chris@0: 'post comments', Chris@0: 'access comments', Chris@0: // Usernames aren't shown in comment edit form autocomplete unless this Chris@0: // permission is granted. Chris@0: 'access user profiles', Chris@0: 'access content', Chris@0: ]); Chris@0: $this->webUser = $this->drupalCreateUser([ Chris@0: 'access comments', Chris@0: 'post comments', Chris@0: 'create article content', Chris@0: 'edit own comments', Chris@0: 'skip comment approval', Chris@0: 'access content', Chris@0: ]); Chris@0: Chris@0: // Create comment field on article. Chris@0: $this->addDefaultCommentField('node', 'article'); Chris@0: Chris@0: // Create a test node authored by the web user. Chris@0: $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]); Chris@0: $this->drupalPlaceBlock('local_tasks_block'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Posts a comment. Chris@0: * Chris@0: * @param \Drupal\Core\Entity\EntityInterface|null $entity Chris@0: * Node to post comment on or NULL to post to the previously loaded page. Chris@0: * @param string $comment Chris@0: * Comment body. Chris@0: * @param string $subject Chris@0: * Comment subject. Chris@0: * @param string $contact Chris@0: * Set to NULL for no contact info, TRUE to ignore success checking, and Chris@0: * array of values to set contact info. Chris@0: * @param string $field_name Chris@0: * (optional) Field name through which the comment should be posted. Chris@0: * Defaults to 'comment'. Chris@0: * Chris@0: * @return \Drupal\comment\CommentInterface|null Chris@0: * The posted comment or NULL when posted comment was not found. Chris@0: */ Chris@0: public function postComment($entity, $comment, $subject = '', $contact = NULL, $field_name = 'comment') { Chris@0: $edit = []; Chris@0: $edit['comment_body[0][value]'] = $comment; Chris@0: Chris@0: if ($entity !== NULL) { Chris@0: $field = FieldConfig::loadByName($entity->getEntityTypeId(), $entity->bundle(), $field_name); Chris@0: } Chris@0: else { Chris@0: $field = FieldConfig::loadByName('node', 'article', $field_name); Chris@0: } Chris@0: $preview_mode = $field->getSetting('preview'); Chris@0: Chris@0: // Must get the page before we test for fields. Chris@0: if ($entity !== NULL) { Chris@0: $this->drupalGet('comment/reply/' . $entity->getEntityTypeId() . '/' . $entity->id() . '/' . $field_name); Chris@0: } Chris@0: Chris@0: // Determine the visibility of subject form field. Chris@0: if (entity_get_form_display('comment', 'comment', 'default')->getComponent('subject')) { Chris@0: // Subject input allowed. Chris@0: $edit['subject[0][value]'] = $subject; Chris@0: } Chris@0: else { Chris@0: $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.'); Chris@0: } Chris@0: Chris@0: if ($contact !== NULL && is_array($contact)) { Chris@0: $edit += $contact; Chris@0: } Chris@0: switch ($preview_mode) { Chris@0: case DRUPAL_REQUIRED: Chris@0: // Preview required so no save button should be found. Chris@0: $this->assertNoFieldByName('op', t('Save'), 'Save button not found.'); Chris@0: $this->drupalPostForm(NULL, $edit, t('Preview')); Chris@0: // Don't break here so that we can test post-preview field presence and Chris@0: // function below. Chris@0: case DRUPAL_OPTIONAL: Chris@0: $this->assertFieldByName('op', t('Preview'), 'Preview button found.'); Chris@0: $this->assertFieldByName('op', t('Save'), 'Save button found.'); Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: break; Chris@0: Chris@0: case DRUPAL_DISABLED: Chris@0: $this->assertNoFieldByName('op', t('Preview'), 'Preview button not found.'); Chris@0: $this->assertFieldByName('op', t('Save'), 'Save button found.'); Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: break; Chris@0: } Chris@0: $match = []; Chris@0: // Get comment ID Chris@0: preg_match('/#comment-([0-9]+)/', $this->getURL(), $match); Chris@0: Chris@0: // Get comment. Chris@0: if ($contact !== TRUE) { Chris@0: // If true then attempting to find error message. Chris@0: if ($subject) { Chris@0: $this->assertText($subject, 'Comment subject posted.'); Chris@0: } Chris@0: $this->assertText($comment, 'Comment body posted.'); Chris@0: $this->assertTrue((!empty($match) && !empty($match[1])), 'Comment id found.'); Chris@0: } Chris@0: Chris@0: if (isset($match[1])) { Chris@0: \Drupal::entityManager()->getStorage('comment')->resetCache([$match[1]]); Chris@0: return Comment::load($match[1]); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks current page for specified comment. Chris@0: * Chris@0: * @param \Drupal\comment\CommentInterface $comment Chris@0: * The comment object. Chris@0: * @param bool $reply Chris@0: * Boolean indicating whether the comment is a reply to another comment. Chris@0: * Chris@0: * @return bool Chris@0: * Boolean indicating whether the comment was found. Chris@0: */ Chris@0: public function commentExists(CommentInterface $comment = NULL, $reply = FALSE) { Chris@0: if ($comment) { Chris@18: $comment_element = $this->cssSelect('.comment-wrapper ' . ($reply ? '.indented ' : '') . 'article#comment-' . $comment->id()); Chris@0: if (empty($comment_element)) { Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: $comment_title = $comment_element[0]->xpath('div/h3/a'); Chris@0: if (empty($comment_title) || ((string) $comment_title[0]) !== $comment->getSubject()) { Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: $comment_body = $comment_element[0]->xpath('div/div/p'); Chris@0: if (empty($comment_body) || ((string) $comment_body[0]) !== $comment->comment_body->value) { Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: return TRUE; Chris@0: } Chris@0: else { Chris@0: return FALSE; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Deletes a comment. Chris@0: * Chris@0: * @param \Drupal\comment\CommentInterface $comment Chris@0: * Comment to delete. Chris@0: */ Chris@0: public function deleteComment(CommentInterface $comment) { Chris@0: $this->drupalPostForm('comment/' . $comment->id() . '/delete', [], t('Delete')); Chris@0: $this->assertText(t('The comment and all its replies have been deleted.'), 'Comment deleted.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the value governing whether the subject field should be enabled. Chris@0: * Chris@0: * @param bool $enabled Chris@0: * Boolean specifying whether the subject field should be enabled. Chris@0: */ Chris@0: public function setCommentSubject($enabled) { Chris@0: $form_display = entity_get_form_display('comment', 'comment', 'default'); Chris@0: if ($enabled) { Chris@0: $form_display->setComponent('subject', [ Chris@0: 'type' => 'string_textfield', Chris@0: ]); Chris@0: } Chris@0: else { Chris@0: $form_display->removeComponent('subject'); Chris@0: } Chris@0: $form_display->save(); Chris@0: // Display status message. Chris@0: $this->pass('Comment subject ' . ($enabled ? 'enabled' : 'disabled') . '.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the value governing the previewing mode for the comment form. Chris@0: * Chris@0: * @param int $mode Chris@0: * The preview mode: DRUPAL_DISABLED, DRUPAL_OPTIONAL or DRUPAL_REQUIRED. Chris@0: * @param string $field_name Chris@0: * (optional) Field name through which the comment should be posted. Chris@0: * Defaults to 'comment'. Chris@0: */ Chris@0: public function setCommentPreview($mode, $field_name = 'comment') { Chris@0: switch ($mode) { Chris@0: case DRUPAL_DISABLED: Chris@0: $mode_text = 'disabled'; Chris@0: break; Chris@0: Chris@0: case DRUPAL_OPTIONAL: Chris@0: $mode_text = 'optional'; Chris@0: break; Chris@0: Chris@0: case DRUPAL_REQUIRED: Chris@0: $mode_text = 'required'; Chris@0: break; Chris@0: } Chris@0: $this->setCommentSettings('preview', $mode, format_string('Comment preview @mode_text.', ['@mode_text' => $mode_text]), $field_name); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the value governing whether the comment form is on its own page. Chris@0: * Chris@0: * @param bool $enabled Chris@0: * TRUE if the comment form should be displayed on the same page as the Chris@0: * comments; FALSE if it should be displayed on its own page. Chris@0: * @param string $field_name Chris@0: * (optional) Field name through which the comment should be posted. Chris@0: * Defaults to 'comment'. Chris@0: */ Chris@0: public function setCommentForm($enabled, $field_name = 'comment') { Chris@0: $this->setCommentSettings('form_location', ($enabled ? CommentItemInterface::FORM_BELOW : CommentItemInterface::FORM_SEPARATE_PAGE), 'Comment controls ' . ($enabled ? 'enabled' : 'disabled') . '.', $field_name); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the value governing restrictions on anonymous comments. Chris@0: * Chris@0: * @param int $level Chris@0: * The level of the contact information allowed for anonymous comments: Chris@0: * - 0: No contact information allowed. Chris@0: * - 1: Contact information allowed but not required. Chris@0: * - 2: Contact information required. Chris@0: */ Chris@0: public function setCommentAnonymous($level) { Chris@0: $this->setCommentSettings('anonymous', $level, format_string('Anonymous commenting set to level @level.', ['@level' => $level])); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the value specifying the default number of comments per page. Chris@0: * Chris@0: * @param int $number Chris@0: * Comments per page value. Chris@0: * @param string $field_name Chris@0: * (optional) Field name through which the comment should be posted. Chris@0: * Defaults to 'comment'. Chris@0: */ Chris@0: public function setCommentsPerPage($number, $field_name = 'comment') { Chris@0: $this->setCommentSettings('per_page', $number, format_string('Number of comments per page set to @number.', ['@number' => $number]), $field_name); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets a comment settings variable for the article content type. Chris@0: * Chris@0: * @param string $name Chris@0: * Name of variable. Chris@0: * @param string $value Chris@0: * Value of variable. Chris@0: * @param string $message Chris@0: * Status message to display. Chris@0: * @param string $field_name Chris@0: * (optional) Field name through which the comment should be posted. Chris@0: * Defaults to 'comment'. Chris@0: */ Chris@0: public function setCommentSettings($name, $value, $message, $field_name = 'comment') { Chris@0: $field = FieldConfig::loadByName('node', 'article', $field_name); Chris@0: $field->setSetting($name, $value); Chris@0: $field->save(); Chris@0: // Display status message. Chris@0: $this->pass($message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether the commenter's contact information is displayed. Chris@0: * Chris@0: * @return bool Chris@0: * Contact info is available. Chris@0: */ Chris@0: public function commentContactInfoAvailable() { Chris@0: return preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this->getRawContent()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Performs the specified operation on the specified comment. Chris@0: * Chris@0: * @param \Drupal\comment\CommentInterface $comment Chris@0: * Comment to perform operation on. Chris@0: * @param string $operation Chris@0: * Operation to perform. Chris@0: * @param bool $approval Chris@0: * Operation is found on approval page. Chris@0: */ Chris@0: public function performCommentOperation(CommentInterface $comment, $operation, $approval = FALSE) { Chris@0: $edit = []; Chris@0: $edit['operation'] = $operation; Chris@0: $edit['comments[' . $comment->id() . ']'] = TRUE; Chris@0: $this->drupalPostForm('admin/content/comment' . ($approval ? '/approval' : ''), $edit, t('Update')); Chris@0: Chris@0: if ($operation == 'delete') { Chris@0: $this->drupalPostForm(NULL, [], t('Delete')); Chris@0: $this->assertRaw(\Drupal::translation()->formatPlural(1, 'Deleted 1 comment.', 'Deleted @count comments.'), format_string('Operation "@operation" was performed on comment.', ['@operation' => $operation])); Chris@0: } Chris@0: else { Chris@0: $this->assertText(t('The update has been performed.'), format_string('Operation "@operation" was performed on comment.', ['@operation' => $operation])); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the comment ID for an unapproved comment. Chris@0: * Chris@0: * @param string $subject Chris@0: * Comment subject to find. Chris@0: * Chris@0: * @return int Chris@0: * Comment id. Chris@0: */ Chris@0: public function getUnapprovedComment($subject) { Chris@0: $this->drupalGet('admin/content/comment/approval'); Chris@0: preg_match('/href="(.*?)#comment-([^"]+)"(.*?)>(' . $subject . ')/', $this->getRawContent(), $match); Chris@0: Chris@0: return $match[2]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a comment comment type (bundle). Chris@0: * Chris@0: * @param string $label Chris@0: * The comment type label. Chris@0: * Chris@0: * @return \Drupal\comment\Entity\CommentType Chris@0: * Created comment type. Chris@0: */ Chris@0: protected function createCommentType($label) { Chris@0: $bundle = CommentType::create([ Chris@0: 'id' => $label, Chris@0: 'label' => $label, Chris@0: 'description' => '', Chris@0: 'target_entity_type_id' => 'node', Chris@0: ]); Chris@0: $bundle->save(); Chris@0: return $bundle; Chris@0: } Chris@0: Chris@0: }