Projet

Général

Profil

Paste
Télécharger (6,81 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / comment_notify / comment_notify.test @ 87dbc3bf

1
<?php
2

    
3
/**
4
 * @file
5
 * Creates tests for comment_notify module.
6
 */
7
class CommentNotifyTestCase extends DrupalWebTestCase {
8
  /**
9
   * Implementation of getInfo().
10
   */
11
  public static function getInfo() {
12
    return array(
13
      'name' => t('Comment notify general tests'),
14
      'description' => t('Test the various comment notification settings.'),
15
      'group' => t('Comment notify'),
16
    );
17
  }
18

    
19
  /**
20
   * Implementation of setUp().
21
   */
22
  function setUp() {
23
    parent::setUp('comment_notify');
24
    // Create a content type where commenting is enabled.
25
    // Allow contact info for anons on that content type, and make preview optional.
26
  }
27

    
28
  /**
29
   * Test various behaviors for anonymous users.
30
   */
31
  function testCommentNotifyAnonymousUserFunctionalTest() {
32
    // Code that does something to be tested.
33
    // Create and login administrative user.
34
    $admin_user = $this->drupalCreateUser(array('administer comment notify', 'administer permissions', 'administer comments'));
35
    $this->drupalLogin($admin_user);
36

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

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

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

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

    
53
    // 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.
54
    $expected_error = comment_notify_variable_registry_get('error_anonymous_email_missing');
55
    $this->assertText(t($expected_error));
56

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

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

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

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

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

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

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

    
87
    // TODO yet more tests.
88
  }
89

    
90
  /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
91
  ////////////////////////// COPIED FROM 7.X COMMENT.TEST \\\\\\\\\\\\\\\\\\\\\
92
  //////////////////////////////and tweaked a little\\\\\\\\\\\\\\\\\\\\\\\\\\\
93
  /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
94

    
95
  /**
96
   * Post comment.
97
   *
98
   * @param object $node Node to post comment on.
99
   * @param string $subject Comment subject.
100
   * @param string $comment Comment body.
101
   * @param boolean $preview Should preview be required.
102
   * @param mixed $contact Set to NULL for no contact info, TRUE to ignore success checking, and array of values to set contact info.
103
   */
104
  function postCommentNotifyComment($node, $subject, $comment, $notify, $contact = NULL) {
105
    $langcode = LANGUAGE_NONE;
106
    $edit = array();
107
    $edit['subject'] = $subject;
108
    $edit['comment_body[' . $langcode . '][0][value]'] = $comment;
109

    
110
    if ($notify !== NULL && is_array($notify)) {
111
      $edit += $notify;
112
    }
113

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

    
118
    $this->drupalPost('node/' . $node->nid, $edit, t('Save'));
119

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

    
124
    // Get comment.
125
    if (!empty($match[1])) { // If true then attempting to find error message.
126
      if ($subject) {
127
        $this->assertText($subject, 'Comment subject posted.');
128
      }
129
      $this->assertText($comment, 'Comment body posted.');
130
      $this->assertTrue((!empty($match) && !empty($match[1])), t('Comment id found.'));
131
    }
132

    
133
    if (isset($match[1])) {
134
      return array('id' => $match[1], 'subject' => $subject, 'comment' => $comment);
135
    }
136
  }
137

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

    
154
      return (boolean)preg_match($regex, $this->drupalGetContent());
155
    }
156
    else {
157
      return FALSE;
158
    }
159
  }
160

    
161
  /**
162
   * Returns a randomly generated valid email address.
163
   * 
164
   * @return string.
165
   */
166
  function getRandomEmailAddress() {
167
    return $this->randomName() . '@example.com';
168
  }
169
}