comment_notify.test 6.7 KB
Newer Older
1
<?php
2
// $Id$
3

4 5 6 7
/**
 * @file
 * Creates tests for comment_notify module.
 */
8 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
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.
35
    $admin_user = $this->drupalCreateUser(array('administer comment notify', 'administer permissions', 'administer comments'));
36 37 38
    $this->drupalLogin($admin_user);

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

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

45
    // Allow anonymous users to post comments and get notifications.
46
    user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access comments', 'access content', 'post comments', 'post comments without approval', 'subscribe to comments'));
47 48
    $this->drupalLogout();

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

    // 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));
57

58
    // Try again with an e-mail address.
59 60
    $contact_1 = array('name' => $this->randomName(), 'mail' => $this->randomName() .'@'. $this->randomName());
    $anonymous_comment_1 = $this->postCommentNotifyComment($this->node, $this->randomName(), $this->randomName(), $subscribe_1, $contact_1);
61 62

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

66
    // Notify type 2 - replies to a comment.
67
    $subscribe_2 = array('notify' => TRUE, 'notify_type' => 2);
68 69
    $contact_2 = array('name' => $this->randomName(), 'mail' => $this->randomName() .'@'. $this->randomName());
    $anonymous_comment_2 = $this->postCommentNotifyComment($this->node, $this->randomName(), $this->randomName(), $subscribe_2, $contact_2);
70 71

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

75 76 77
    // 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.'));

78 79 80
    // 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);
81
    $contact_0 = array('mail' => $this->randomName() . '@' . $this->randomName());
82
    $anonymous_comment_0 = $this->postCommentNotifyComment($this->node, $this->randomName(), $this->randomName(), $subscribe_0, $contact_0);
83 84

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

88
    // TODO yet more tests.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  }

  /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  ////////////////////////// COPIED FROM 7.X 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 boolean $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.
   */
105 106
  function postCommentNotifyComment($node, $subject, $comment, $notify, $contact = NULL) {
    $langcode = LANGUAGE_NONE;
107 108
    $edit = array();
    $edit['subject'] = $subject;
109
    $edit['comment_body[' . $langcode . '][0][value]'] = $comment;
110 111 112 113

    if ($notify !== NULL && is_array($notify)) {
      $edit += $notify;
    }
114 115 116 117 118

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

119 120
    $this->drupalPost('node/' . $node->nid, $edit, t('Save'));
    
121 122 123 124 125
    $match = array();
    // Get comment ID
    preg_match('/#comment-([^"]+)/', $this->getURL(), $match);

    // Get comment.
126
    if (!empty($match[1])) { // If true then attempting to find error message.
127 128 129 130 131 132 133 134
      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])) {
135
      return array('id' => $match[1], 'subject' => $subject, 'comment' => $comment);
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 162
    }
  }

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

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

}