CommentNotifyTestBase.php 4.12 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\Tests\comment_notify\Functional;

use Drupal\comment\Tests\CommentTestTrait;
6
use Drupal\comment\CommentInterface;
7
use Drupal\Core\Test\AssertMailTrait;
8
use Drupal\field\Entity\FieldConfig;
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
use Drupal\Tests\BrowserTestBase;

/**
 * Comment notify Base Test class.
 */
abstract class CommentNotifyTestBase extends BrowserTestBase {

  use CommentTestTrait;
  use AssertMailTrait;

  /**
   * Admin User.
   *
   * @var \Drupal\user\Entity\User $adminUser
   */
  protected $adminUser;

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = [
    'comment_notify',
    'node',
    'comment',
    'token',
  ];

  /**
   * Test that the config page is working.
   */
  protected function setUp() {
    parent::setUp();

    // Create and login administrative user.
    $this->adminUser = $this->drupalCreateUser(
      [
        'administer comment notify',
        'administer permissions',
        'administer comments',
      ]
    );
52 53 54 55 56 57 58 59 60 61

    // Enable comment notify on this node and allow anonymous information in
    // comments.
    $this->drupalCreateContentType([
      'type' => 'article',
    ]);
    $this->addDefaultCommentField('node', 'article');
    $comment_field = FieldConfig::loadByName('node', 'article', 'comment');
    $comment_field->setSetting('anonymous', CommentInterface::ANONYMOUS_MAY_CONTACT);
    $comment_field->save();
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  }

  /**
   * Post comment.
   *
   * @param \Drupal\node\NodeInterface $node
   *   Node to post comment on.
   * @param string $subject
   *   Comment subject.
   * @param string $comment
   *   Comment body.
   * @param array $notify
   *   An array with the notify values.
   * @param mixed $contact
   *   Set to NULL for no contact info, TRUE to ignore success checking, and
   *   array of values to set contact info.
   *
   * @return array|bool
   *    return an array with the comment or false if the post comment fails.
   */
82
  protected function postComment($url, $subject, $comment, $notify, $contact = NULL) {
83 84 85 86 87 88 89 90 91 92 93 94
    $edit = [];
    $edit['subject[0][value]'] = $subject;
    $edit['comment_body[0][value]'] = $comment;

    if ($notify !== NULL && is_array($notify)) {
      $edit += $notify;
    }

    if ($contact !== NULL && is_array($contact)) {
      $edit += $contact;
    }

95
    $this->drupalPostForm($url, $edit, t('Save'));
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

    $match = array();
    // Get comment ID.
    preg_match('/#comment-([^"]+)/', $this->getURL(), $match);

    // Get comment.
    // If true then attempting to find error message.
    if (!empty($match[1])) {
      if ($subject) {
        $this->assertTrue($this->getSession()->getPage()->hasContent($subject), 'Comment subject posted.');
      }
      $this->assertTrue($this->getSession()->getPage()->hasContent($comment), 'Comment body posted.');
      $this->assertTrue((!empty($match) && !empty($match[1])), t('Comment id found.'));
    }

    if (isset($match[1])) {
      return ['id' => $match[1], 'subject' => $subject, 'comment' => $comment];
    }

    return FALSE;
  }

  /**
   * Checks current page for specified comment.
   *
   * @param object $comment
   *    Comment object.
   * @param bool $reply
   *    The comment is a reply to another comment.
   *
   * @return bool
   *    Comment found.
   */
  protected function commentExists($comment, $reply = FALSE) {
    if ($comment && is_object($comment)) {
      $regex = '/' . ($reply ? '<div class="indented">(.*?)' : '');
      // Comment anchor.
      $regex .= '<a id="comment-' . $comment->id . '"(.*?)';
      // Begin in comment div.
      $regex .= '<div(.*?)';
      // Match subject.
      $regex .= $comment->subject . '(.*?)';
      // Match comment.
      $regex .= $comment->comment . '(.*?)';
      // Dot matches newlines and ensure that match doesn't bleed outside
      // comment div.
      $regex .= '<\/div>/s';

      return (boolean) preg_match($regex, $this->getSession()->getPage()->getContent());
    }
    else {
      return FALSE;
    }
  }

  /**
   * Returns a randomly generated valid email address.
   *
   * @return string.
   *   A random email.
   */
  public function getRandomEmailAddress() {
    return $this->randomMachineName() . '@example.com';
  }

}