You are here

comment_notify.test in Comment Notify 7

Same filename and directory in other branches
  1. 6 comment_notify.test

Creates tests for comment_notify module.

File

comment_notify.test
View source
<?php

/**
 * @file
 * Creates tests for comment_notify module.
 */
class CommentNotifyTestCase extends DrupalWebTestCase {

  /**
   * Implementation of getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => t('Comment notify general tests'),
      'description' => t('Test the various comment notification settings.'),
      'group' => t('Comment notify'),
    );
  }

  /**
   * Implementation of setUp().
   */
  function setUp() {
    parent::setUp('comment_notify');

    // Create a content type where commenting is enabled.
    // Allow contact info for anons on that content type, and make preview
    // optional.
  }

  /**
   * Test various behaviors for anonymous users.
   */
  function testCommentNotifyAnonymousUserFunctionalTest() {

    // Code that does something to be tested.
    // Create and login administrative user.
    $admin_user = $this
      ->drupalCreateUser(array(
      'administer comment notify',
      'administer permissions',
      'administer comments',
    ));
    $this
      ->drupalLogin($admin_user);

    // Enable comment notify on this node and allow anonymous information in
    // comments.
    variable_set('comment_notify_node_types', array(
      'article' => 'article',
      'page' => 0,
    ));
    variable_set('comment_anonymous_article', '1');

    // Create a node with comments allowed.
    $this->node = $this
      ->drupalCreateNode(array(
      'type' => 'article',
      'promote' => NODE_PROMOTED,
      'comment' => COMMENT_NODE_OPEN,
    ));

    // Allow anonymous users to post comments and get notifications.
    user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, [
      'access comments',
      'access content',
      'post comments',
      'skip comment approval',
      'subscribe to comments',
    ]);
    $this
      ->drupalLogout();

    // Notify type 1 - All comments on the node.
    // First confirm that we properly require an e-mail address.
    $subscribe_1 = array(
      'notify' => TRUE,
      'notify_type' => 1,
    );
    $this
      ->postCommentNotifyComment($this->node, $this
      ->randomName(), $this
      ->randomName(), $subscribe_1);

    // This is still a bad test to test for a static string showing on the page,
    // but at least the definition of the string is centralized.
    $expected_error = comment_notify_variable_registry_get('error_anonymous_email_missing');
    $this
      ->assertText(t($expected_error));

    // Try again with an e-mail address.
    $contact_1 = array(
      'name' => $this
        ->randomName(),
      'mail' => $this
        ->getRandomEmailAddress(),
    );
    $anonymous_comment_1 = $this
      ->postCommentNotifyComment($this->node, $this
      ->randomName(), $this
      ->randomName(), $subscribe_1, $contact_1);

    // Confirm that the notification is saved.
    $result = comment_notify_get_notification_type($anonymous_comment_1['id']);
    $this
      ->assertEqual($result, $subscribe_1['notify_type'], 'Notify selection option 1 is saved properly.');

    // Notify type 2 - replies to a comment.
    $subscribe_2 = array(
      'notify' => TRUE,
      'notify_type' => 2,
    );
    $contact_2 = array(
      'name' => $this
        ->randomName(),
      'mail' => $this
        ->getRandomEmailAddress(),
    );
    $anonymous_comment_2 = $this
      ->postCommentNotifyComment($this->node, $this
      ->randomName(), $this
      ->randomName(), $subscribe_2, $contact_2);

    // Confirm that the notification is saved.
    $result = comment_notify_get_notification_type($anonymous_comment_2['id']);
    $this
      ->assertEqual($result, $subscribe_2['notify_type'], 'Notify selection option 2 is saved properly.');

    // Confirm that the original subscriber with all comments on this node got
    // their mail.
    $this
      ->assertMail('to', $contact_1['mail'], t('Message was sent to the proper anonymous user.'));

    // Notify type 0 (i.e. only one mode is enabled).
    variable_set('comment_notify_available_alerts', array(
      1 => 0,
      2 => 2,
    ));
    $subscribe_0 = array(
      'notify' => TRUE,
    );
    $contact_0 = array(
      'mail' => $this
        ->getRandomEmailAddress(),
    );
    $anonymous_comment_0 = $this
      ->postCommentNotifyComment($this->node, $this
      ->randomName(), $this
      ->randomName(), $subscribe_0, $contact_0);

    // Confirm that the notification is saved.
    $result = comment_notify_get_notification_type($anonymous_comment_0['id']);
    $this
      ->assertEqual($result, 2, 'Notify selection option 0 is saved properly.');

    // TODO yet more tests.
  }

  /**
   * Below code is copied from comment.test.
   *
   * And tweaked a little.
   */

  /**
   * Post comment.
   *
   * @param object $node
   *   Node to post comment on.
   * @param string $subject
   *   Comment subject.
   * @param string $comment
   *   Comment body.
   * @param bool $preview
   *   Should preview be required.
   * @param mixed $contact
   *   Set to NULL for no contact info, TRUE to ignore success checking, and
   *   array of values to set contact info.
   */
  function postCommentNotifyComment($node, $subject, $comment, $notify, $contact = NULL) {
    $langcode = LANGUAGE_NONE;
    $edit = array();
    $edit['subject'] = $subject;
    $edit['comment_body[' . $langcode . '][0][value]'] = $comment;
    if ($notify !== NULL && is_array($notify)) {
      $edit += $notify;
    }
    if ($contact !== NULL && is_array($contact)) {
      $edit += $contact;
    }
    $this
      ->drupalPost('node/' . $node->nid, $edit, t('Save'));
    $match = array();

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

    // Get comment.
    if (!empty($match[1])) {

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

  /**
   * 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.
   */
  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 (bool) preg_match($regex, $this
        ->drupalGetContent());
    }
    else {
      return FALSE;
    }
  }

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

}

Classes

Namesort descending Description
CommentNotifyTestCase @file Creates tests for comment_notify module.