/[drupal]/contributions/modules/comment_notify/comment_notify.test
ViewVC logotype

Contents of /contributions/modules/comment_notify/comment_notify.test

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Sat Nov 7 18:39:55 2009 UTC (2 weeks, 6 days ago) by greggles
Branch: MAIN
Changes since 1.2: +35 -6 lines
File MIME type: text/x-php
bug #617070 by greggles: Doesn't record subscription when only one subscription type is enabled (and lots more tests)
1 <?php
2 // $Id: comment_notify.test,v 1.2 2009/07/14 14:36:59 greggles Exp $
3
4 class CommentNotifyTestCase extends DrupalWebTestCase {
5 /**
6 * Implementation of getInfo().
7 */
8 public static function getInfo() {
9 return array(
10 'name' => t('Comment notify general tests'),
11 'description' => t('Test the various comment notification settings.'),
12 'group' => t('Comment notify'),
13 );
14 }
15
16 /**
17 * Implementation of setUp().
18 */
19 function setUp() {
20 parent::setUp('comment_notify');
21 // Create a content type where commenting is enabled.
22 // Allow contact info for anons on that content type, and make preview optional.
23
24 // Do some default comment notify settings?.
25 // variable_set('some_variable', 'some value');
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('story' => 'story'));
39 variable_set('comment_anonymous_story', 1);
40
41 // Create a node with comments allowed.
42 $this->node = $this->drupalCreateNode(array('type' => 'story', 'promote' => 1, 'comment' => 2));
43
44 // Get the page and post the comment.
45 $this->drupalGet('comment/reply/' . $this->node->nid);
46
47 // Ensure that the contact form won't be shown without categories.
48 $this->setPermission('anonymous user', array('access comments' => TRUE, 'access content' => TRUE, 'post comments' => TRUE, 'post comments without approval' => TRUE, 'subscribe to comments' => TRUE));
49 $this->drupalLogout();
50
51 $subscribe_1 = array('notify' => TRUE, 'notify_type' => 1);
52 $this->postCommentNotifyComment($this->node, $this->randomName(), $this->randomName(), TRUE, TRUE, $subscribe_1);
53 $this->assertText(t('If you want to subscribe to comments you must supply a valid e-mail address.'), t('Anonymous user must leave E-mail if they want to get notifications.'));
54
55 $anonymous_comment_1 = $this->postCommentNotifyComment($this->node, $this->randomName(), $this->randomName(), TRUE, $subscribe_1, array('mail' => $this->randomName() .'@'. $this->randomName()));
56
57 // Confirm that the notification is saved.
58 $result = db_result(db_query("SELECT notify FROM {comment_notify} WHERE cid = %d", $anonymous_comment_1['id']));
59 $this->assertEqual($result, $subscribe_1['notify_type'], 'Notify selection option 1 is saved properly.');
60
61 // Notify type 2.
62 $subscribe_2 = array('notify' => TRUE, 'notify_type' => 2);
63 $anonymous_comment_2 = $this->postCommentNotifyComment($this->node, $this->randomName(), $this->randomName(), TRUE, $subscribe_2, array('mail' => $this->randomName() .'@'. $this->randomName()));
64
65 // Confirm that the notification is saved.
66 $result = db_result(db_query("SELECT notify FROM {comment_notify} WHERE cid = %d", $anonymous_comment_2['id']));
67 $this->assertEqual($result, $subscribe_2['notify_type'], 'Notify selection option 2 is saved properly.');
68
69 // Notify type 0 (i.e. only one mode is enabled).
70 variable_set('comment_notify_available_alerts', array(1 => 0, 2 => 2));
71 $subscribe_0 = array('notify' => TRUE);
72 $anonymous_comment_0 = $this->postCommentNotifyComment($this->node, $this->randomName(), $this->randomName(), TRUE, $subscribe_0, array('mail' => $this->randomName() .'@'. $this->randomName()));
73
74 // Confirm that the notification is saved.
75 $result = db_result(db_query("SELECT notify FROM {comment_notify} WHERE cid = %d", $anonymous_comment_0['id']));
76 $this->assertEqual($result, 2, 'Notify selection option 0 is saved properly.');
77
78
79
80 // TODO a whole bunch more tests that check if mail was "sent".
81 }
82
83
84 /**
85 * Set permission.
86 *
87 * @param string $role User role to set permissions for.
88 * @param array $permissions Key-value array of permissions to set.
89 */
90 function setPermission($role, $permissions) {
91 // Get role id (rid) for specified role.
92 $rid = db_result(db_query("SELECT rid FROM {role} WHERE name = '%s'", array('%s' => $role)));
93 if ($rid === FALSE) {
94 $this->fail(t(' [permission] Role "' . $role . '" not found.'));
95 }
96
97 // Create edit array from permission.
98 $edit = array();
99 foreach ($permissions as $name => $value) {
100 $edit[$rid . '[' . $name . ']'] = $value;
101 }
102
103 $this->drupalPost('admin/user/permissions', $edit, t('Save permissions'));
104 $this->assertText(t('The changes have been saved.'), t(' [permission] Saved changes.'));
105 }
106
107 /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
108 ////////////////////////// COPIED FROM 7.X COMMENT.TEST \\\\\\\\\\\\\\\\\\\\\
109 //////////////////////////////and tweaked a little\\\\\\\\\\\\\\\\\\\\\\\\\\\
110 /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
111
112 /**
113 * Post comment.
114 *
115 * @param object $node Node to post comment on.
116 * @param string $subject Comment subject.
117 * @param string $comment Comment body.
118 * @param boolean $preview Should preview be required.
119 * @param mixed $contact Set to NULL for no contact info, TRUE to ignore success checking, and array of values to set contact info.
120 */
121 function postCommentNotifyComment($node, $subject, $comment, $preview = TRUE, $notify, $contact = NULL) {
122 $edit = array();
123 $edit['subject'] = $subject;
124 $edit['comment'] = $comment;
125
126 if ($notify !== NULL && is_array($notify)) {
127 $edit += $notify;
128 }
129
130 if ($contact !== NULL && is_array($contact)) {
131 $edit += $contact;
132 }
133
134 if ($node !== NULL) {
135 $this->drupalGet('comment/reply/' . $node->nid);
136 }
137
138 if ($preview) {
139 $this->assertNoFieldByName('op', t('Save'), t('Save button not found.')); // Preview required so no save button should be found.
140 $this->drupalPost(NULL, $edit, t('Preview'));
141 }
142 $this->drupalPost(NULL, $edit, t('Save'));
143 $match = array();
144 // Get comment ID
145 preg_match('/#comment-([^"]+)/', $this->getURL(), $match);
146
147 // Get comment.
148 if (!empty($match[1])) { // If true then attempting to find error message.
149 if ($subject) {
150 $this->assertText($subject, 'Comment subject posted.');
151 }
152 $this->assertText($comment, 'Comment body posted.');
153 $this->assertTrue((!empty($match) && !empty($match[1])), t('Comment id found.'));
154 }
155
156 if (isset($match[1])) {
157 return array('id' => $match[1], 'subject' => $subject, 'comment' => $comment);
158 }
159 }
160
161 /**
162 * Checks current page for specified comment.
163 *
164 * @param object $comment Comment object.
165 * @param boolean $reply The comment is a reply to another comment.
166 * @return boolean Comment found.
167 */
168 function commentExists($comment, $reply = FALSE) {
169 if ($comment && is_object($comment)) {
170 $regex = '/' . ($reply ? '<div class="indented">(.*?)' : '');
171 $regex .= '<a id="comment-' . $comment->id . '"(.*?)'; // Comment anchor.
172 $regex .= '<div(.*?)'; // Begin in comment div.
173 $regex .= $comment->subject . '(.*?)'; // Match subject.
174 $regex .= $comment->comment . '(.*?)'; // Match comment.
175 $regex .= '<\/div>/s'; // Dot matches newlines and ensure that match doesn't bleed outside comment div.
176
177 return (boolean)preg_match($regex, $this->drupalGetContent());
178 }
179 else {
180 return FALSE;
181 }
182 }
183
184 }

  ViewVC Help
Powered by ViewVC 1.1.2