Line # | Frequency | Source Line |
1 | | <?php
|
2 | | // $Id: comment.test,v 1.14 2008/04/01 23:33:54 boombatower Exp $
|
3 | |
|
4 | | class CommentTestCase extends DrupalTestCase {
|
5 | | protected $admin_user;
|
6 | | protected $web_user;
|
7 | | protected $node;
|
8 | |
|
9 | | /**
|
10 | | * Implementation of getInfo().
|
11 | | */
|
12 | | function getInfo() {
|
13 | | return array(
|
14 | 1 | 'name' => t('Comment functionality'),
|
15 | | 'description' => t('Thoroughly test comment administration and user interfaces.'),
|
16 | | 'group' => t('Comment Tests'),
|
17 | | );
|
18 | | }
|
19 | |
|
20 | | /**
|
21 | | * Implementation of setUp().
|
22 | | */
|
23 | | function setUp() {
|
24 | | parent::setUp();
|
25 | |
|
26 | | $this->drupalModuleEnable('comment');
|
27 | |
|
28 | | // Create users.
|
29 | | $this->admin_user = $this->drupalCreateUser(array('administer content types', 'administer comments', 'administer permissions'));
|
30 | | $this->web_user = $this->drupalCreateUser(array('access comments', 'post comments', 'create story content'));
|
31 | |
|
32 | | $this->drupalLogin($this->web_user);
|
33 | | $this->node = $this->drupalCreateNode(array('type' => 'story'));
|
34 | | $this->assertTrue($this->node, t('Story node created.'));
|
35 | | $this->drupalLogout();
|
36 | | }
|
37 | |
|
38 | | /**
|
39 | | * Test comment interface.
|
40 | | */
|
41 | | function testCommentInterface() {
|
42 | | // Set comments to not have subject.
|
43 | | $this->drupalLogin($this->admin_user);
|
44 | | $this->setCommentPreview(TRUE);
|
45 | | $this->setCommentSubject(FALSE);
|
46 | | $this->drupalLogout();
|
47 | |
|
48 | | // Post comment without subject.
|
49 | | $this->drupalLogin($this->web_user);
|
50 | | $this->drupalGet('comment/reply/'. $this->node->nid);
|
51 | | $this->assertNoFieldByName('subject', '', t('Subject field not found.'));
|
52 | |
|
53 | | // Set comments to have subject and preview to required.
|
54 | | $this->drupalLogout();
|
55 | | $this->drupalLogin($this->admin_user);
|
56 | | $this->setCommentSubject(TRUE);
|
57 | | $this->setCommentPreview(TRUE);
|
58 | | $this->drupalLogout();
|
59 | |
|
60 | | // Create comment that requires preview.
|
61 | | $this->drupalLogin($this->web_user);
|
62 | | $comment = $this->postComment($this->node, $this->randomName(), $this->randomName());
|
63 | | $this->assertTrue($this->commentExists($comment), t('Comment found.'));
|
64 | |
|
65 | | // Reply to comment.
|
66 | | $this->drupalGet('comment/reply/'. $this->node->nid .'/'. $comment->id);
|
67 | | $reply = $this->postComment(NULL, $this->randomName(), $this->randomName());
|
68 | | $this->assertTrue($this->commentExists($reply, TRUE), t('Reply found.'));
|
69 | |
|
70 | | // Edit reply.
|
71 | | $this->drupalGet('comment/edit/'. $reply->id);
|
72 | | $reply = $this->postComment(NULL, $this->randomName(), $this->randomName());
|
73 | | $this->assertTrue($this->commentExists($reply, TRUE), t('Modified reply found.'));
|
74 | |
|
75 | | // Delete comment and make sure that reply is also removed.
|
76 | | $this->drupalLogout();
|
77 | | $this->drupalLogin($this->admin_user);
|
78 | | $this->deleteComment($comment);
|
79 | |
|
80 | | $this->drupalGet('node/'. $this->node->nid);
|
81 | | $this->assertFalse($this->commentExists($comment), t('Comment not found.'));
|
82 | | $this->assertFalse($this->commentExists($reply, TRUE), t('Reply not found.'));
|
83 | | }
|
84 | |
|
85 | | /**
|
86 | | * Test comment form on node page.
|
87 | | */
|
88 | | function testFormOnPage() {
|
89 | | // Enabled comment form on node page.
|
90 | | $this->drupalLogin($this->admin_user);
|
91 | | $this->setCommentForm(TRUE);
|
92 | | $this->drupalLogout();
|
93 | |
|
94 | | // Submit comment through node form.
|
95 | | $this->drupalLogin($this->web_user);
|
96 | | $this->drupalGet('node/'. $this->node->nid);
|
97 | | $form_comment = $this->postComment(NULL, $this->randomName(), $this->randomName());
|
98 | | $this->assertTrue($this->commentExists($form_comment), t('Form comment found.'));
|
99 | |
|
100 | | // Disable comment form on node page.
|
101 | | $this->drupalLogout();
|
102 | | $this->drupalLogin($this->admin_user);
|
103 | | $this->setCommentForm(FALSE);
|
104 | | }
|
105 | |
|
106 | | /**
|
107 | | * Test anonymous comment functionality.
|
108 | | */
|
109 | | function testAnonymous() {
|
110 | | $this->drupalLogin($this->admin_user);
|
111 | | // Enabled anonymous user comments.
|
112 | | $this->setAnonymousUserComment(TRUE, TRUE);
|
113 | | $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
|
114 | | $this->drupalLogout();
|
115 | |
|
116 | | // Post anonymous comment without contact info.
|
117 | | $anonymous_comment1 = $this->postComment($this->node, $this->randomName(), $this->randomName());
|
118 | | $this->assertTrue($this->commentExists($anonymous_comment1), t('Anonymous comment without contact info found.'));
|
119 | |
|
120 | | // Allow contact info.
|
121 | | $this->drupalLogin($this->admin_user);
|
122 | | $this->setCommentAnonymous('1');
|
123 | | $this->drupalLogout();
|
124 | |
|
125 | | // Post anonymous comment with contact info (optional).
|
126 | | $this->drupalGet('comment/reply/'. $this->node->nid);
|
127 | | $this->assertTrue($this->commentContactInfoAvailable(), t('Contact information available.'));
|
128 | |
|
129 | | $anonymous_comment2 = $this->postComment($this->node, $this->randomName(), $this->randomName());
|
130 | | $this->assertTrue($this->commentExists($anonymous_comment2), t('Anonymous comment with contact info (optional) found.'));
|
131 | |
|
132 | | // Require contact info.
|
133 | | $this->drupalLogin($this->admin_user);
|
134 | | $this->setCommentAnonymous('2');
|
135 | | $this->drupalLogout();
|
136 | |
|
137 | | // Try to post comment with contact info (required).
|
138 | | $this->drupalGet('comment/reply/'. $this->node->nid);
|
139 | | $this->assertTrue($this->commentContactInfoAvailable(), t('Contact information available.'));
|
140 | |
|
141 | | $anonymous_comment3 = $this->postComment($this->node, $this->randomName(), $this->randomName(), TRUE, TRUE);
|
142 | | $this->assertText(t('E-mail field is required.'), t('E-mail required.')); // Name should have 'Anonymous' for value by default.
|
143 | | $this->assertFalse($this->commentExists($anonymous_comment3), t('Anonymous comment with contact info (required) not found.'));
|
144 | |
|
145 | | // Post comment with contact info (required).
|
146 | | $anonymous_comment3 = $this->postComment($this->node, $this->randomName(), $this->randomName(), TRUE, array('mail' => 'tester@simpletest.org'));
|
147 | | $this->assertTrue($this->commentExists($anonymous_comment3), t('Anonymous comment with contact info (required) found.'));
|
148 | |
|
149 | | // Unpublish comment.
|
150 | | $this->drupalLogin($this->admin_user);
|
151 | | $this->performCommentOperation($anonymous_comment3, 'unpublish');
|
152 | |
|
153 | | $this->drupalGet('admin/content/comment/approval');
|
154 | | $this->assertRaw('comments['. $anonymous_comment3->id .']', t('Comment was unpublished.'));
|
155 | |
|
156 | | // Publish comment.
|
157 | | $this->performCommentOperation($anonymous_comment3, 'publish', TRUE);
|
158 | |
|
159 | | $this->drupalGet('admin/content/comment');
|
160 | | $this->assertRaw('comments['. $anonymous_comment3->id .']', t('Comment was published.'));
|
161 | |
|
162 | | // Delete comment.
|
163 | | $this->performCommentOperation($anonymous_comment3, 'delete');
|
164 | |
|
165 | | $this->drupalGet('admin/content/comment');
|
166 | | $this->assertNoRaw('comments['. $anonymous_comment3->id .']', t('Comment was deleted.'));
|
167 | |
|
168 | | // Set anonymouse comments to require approval.
|
169 | | $this->setAnonymousUserComment(TRUE, FALSE);
|
170 | | $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
|
171 | | $this->drupalLogout();
|
172 | |
|
173 | | // Post anonymous comment without contact info.
|
174 | | $subject = $this->randomName();
|
175 | | $body = $this->randomName();
|
176 | | $this->postComment($this->node, $subject, $body, TRUE, TRUE); // Set $contact to true so that it won't check for id and message.
|
177 | | $this->assertText(t('Your comment has been queued for moderation by site administrators and will be published after approval.'), t('Comment requires approval.'));
|
178 | |
|
179 | | // Get unaproved comment id.
|
180 | | $this->drupalLogin($this->admin_user);
|
181 | | $anonymous_comment4 = $this->getUnaprovedComment($subject);
|
182 | | $anonymous_comment4 = (object) array('id' => $anonymous_comment4, 'subject' => $subject, 'comment' => $body);
|
183 | | $this->drupalLogout();
|
184 | |
|
185 | | $this->assertFalse($this->commentExists($anonymous_comment4), t('Anonymous comment was not published.'));
|
186 | |
|
187 | | // Approve comment.
|
188 | | $this->drupalLogin($this->admin_user);
|
189 | | $this->performCommentOperation($anonymous_comment4, 'publish', TRUE);
|
190 | | $this->drupalLogout();
|
191 | |
|
192 | | $this->drupalGet('node/'. $this->node->nid);
|
193 | | $this->assertTrue($this->commentExists($anonymous_comment4), t('Anonymous comment visible.'));
|
194 | |
|
195 | | // Reset.
|
196 | | $this->drupalLogin($this->admin_user);
|
197 | | $this->setAnonymousUserComment(FALSE, FALSE);
|
198 | | }
|
199 | |
|
200 | | /**
|
201 | | * Post comment.
|
202 | | *
|
203 | | * @param object $node Node to post comment on.
|
204 | | * @param string $subject Comment subject.
|
205 | | * @param string $comment Comment body.
|
206 | | * @param boolean $preview Should preview be required.
|
207 | | * @param mixed $contact Set to NULL for no contact info, TRUE to ignore success checking, and array of values to set contact info.
|
208 | | */
|
209 | | function postComment($node, $subject, $comment, $preview = TRUE, $contact = NULL) {
|
210 | | $edit = array();
|
211 | | $edit['subject'] = $subject;
|
212 | | $edit['comment'] = $comment;
|
213 | | if ($contact !== NULL && is_array($contact)) {
|
214 | | $edit += $contact;
|
215 | | }
|
216 | |
|
217 | | if ($node !== NULL) {
|
218 | | $this->drupalGet('comment/reply/'. $node->nid);
|
219 | | }
|
220 | | if ($preview) {
|
221 | | $this->assertNoFieldByName('op', t('Save'), t('Save button not found.')); // Preview required so no save button should be found.
|
222 | | $this->drupalPost(NULL, $edit, t('Preview'));
|
223 | | }
|
224 | | $this->drupalPost(NULL, array(), t('Save'));
|
225 | |
|
226 | | $match = array();
|
227 | | // Get comment ID
|
228 | | preg_match('/#comment-([^"]+)/', $this->getURL(), $match);
|
229 | | // get comment
|
230 | | if ($contact !== TRUE) { // If true then attempting to find error message.
|
231 | | $this->assertText($subject, 'Comment posted.');
|
232 | | $this->assertTrue((!empty($match) && !empty($match[1])), t('Comment id found.'));
|
233 | | }
|
234 | | if (isset($match[1])) {
|
235 | | return (object) array('id' => $match[1], 'subject' => $subject, 'comment' => $comment);
|
236 | | }
|
237 | | }
|
238 | |
|
239 | | /**
|
240 | | * Checks current pag for specified comment.
|
241 | | *
|
242 | | * @param object $comment Comment object.
|
243 | | * @param boolean $reply The comment is a reply to another comment.
|
244 | | * @return boolean Comment found.
|
245 | | */
|
246 | | function commentExists($comment, $reply = FALSE) {
|
247 | | if ($comment && is_object($comment)) {
|
248 | | $regex = '/'. ($reply ? '<div class="indented">(.*?)' : '');
|
249 | | $regex .= '<a id="comment-'. $comment->id .'"(.*?)'; // Comment anchor.
|
250 | | $regex .= '<div(.*?)'; // Begin in comment div.
|
251 | | $regex .= $comment->subject .'(.*?)'; // Match subject.
|
252 | | $regex .= $comment->comment .'(.*?)'; // Match comment.
|
253 | | $regex .= '<\/div>/s'; // Dot matches newlines and ensure that match doesn't bleed outside comment div.
|
254 | | return preg_match($regex, $this->drupalGetContent());
|
255 | | }
|
256 | | else {
|
257 | | return FALSE;
|
258 | | }
|
259 | | }
|
260 | |
|
261 | | /**
|
262 | | * Delete comment.
|
263 | | *
|
264 | | * @param object $comment Comment to delete.
|
265 | | */
|
266 | | function deleteComment($comment) {
|
267 | | $this->drupalPost('comment/delete/'. $comment->id, array(), t('Delete'));
|
268 | | $this->assertText(t('The comment and all its replies have been deleted.'), t('Comment deleted.'));
|
269 | | }
|
270 | |
|
271 | | /**
|
272 | | * Set comment subject setting.
|
273 | | *
|
274 | | * @param boolean $enabled Subject value.
|
275 | | */
|
276 | | function setCommentSubject($enabled) {
|
277 | | $this->setCommentSettings('comment_subject_field', ($enabled ? '1' : '0'), 'Comment subject '. ($enabled ? 'enabled' : 'disabled') .'.');
|
278 | | }
|
279 | |
|
280 | | /**
|
281 | | * Set comment preview setting.
|
282 | | *
|
283 | | * @param boolean $required Preview value.
|
284 | | */
|
285 | | function setCommentPreview($required) {
|
286 | | $this->setCommentSettings('comment_preview', ($required ? '1' : '0'), 'Comment preview '. ($required ? 'required' : 'optional') .'.');
|
287 | | }
|
288 | |
|
289 | | /**
|
290 | | * Set comment form setting.
|
291 | | *
|
292 | | * @param boolean $enabled Form value.
|
293 | | */
|
294 | | function setCommentForm($enabled) {
|
295 | | $this->setCommentSettings('comment_form_location', ($enabled ? '1' : '3'), 'Comment controls '. ($enabled ? 'enabled' : 'disabled') .'.');
|
296 | | }
|
297 | |
|
298 | | /**
|
299 | | * Set comment anonymous level setting.
|
300 | | *
|
301 | | * @param integer $level Anonymous level.
|
302 | | */
|
303 | | function setCommentAnonymous($level) {
|
304 | | $this->setCommentSettings('comment_anonymous', $level, 'Anonymous commenting set to level '. $level .'.');
|
305 | | }
|
306 | |
|
307 | | /**
|
308 | | * Set comment setting for story content type.
|
309 | | *
|
310 | | * @param string $name Name of variable.
|
311 | | * @param string $vale Value of variable.
|
312 | | * @param string $message Status message to display.
|
313 | | */
|
314 | | function setCommentSettings($name, $value, $message) {
|
315 | | $this->drupalVariableSet($name .'_story', $value);
|
316 | | $this->assertTrue(TRUE, t($message)); // Display status message.
|
317 | | }
|
318 | |
|
319 | | /**
|
320 | | * Set anonymous comment setting.
|
321 | | *
|
322 | | * @param boolean $enabled Allow anonymous commenting.
|
323 | | * @param boolean $without_approval Allow anonymous commenting without approval.
|
324 | | */
|
325 | | function setAnonymousUserComment($enabled, $without_approval) {
|
326 | | $edit = array();
|
327 | | $edit['1[access comments]'] = $enabled;
|
328 | | $edit['1[post comments]'] = $enabled;
|
329 | | $edit['1[post comments without approval]'] = $without_approval;
|
330 | | $this->drupalPost('admin/user/permissions', $edit, t('Save permissions'));
|
331 | | $this->assertText(t('The changes have been saved.'), t('Anonymous user comments '. ($enabled ? 'enabled' : 'disabled') .'.'));
|
332 | | }
|
333 | |
|
334 | | /**
|
335 | | * Check for contact info.
|
336 | | *
|
337 | | * @return boolean Contact info is avialable.
|
338 | | */
|
339 | | function commentContactInfoAvailable() {
|
340 | | return preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this->drupalGetContent());
|
341 | | }
|
342 | |
|
343 | | /**
|
344 | | * Perform the specified operation on the specified comment.
|
345 | | *
|
346 | | * @param object $comment Comment to perform operation on.
|
347 | | * @param string $operation Operation to perform.
|
348 | | * @param boolean $aproval Operation is found on approval page.
|
349 | | */
|
350 | | function performCommentOperation($comment, $operation, $approval = FALSE) {
|
351 | | $edit = array();
|
352 | | $edit['operation'] = $operation;
|
353 | | $edit['comments['. $comment->id .']'] = TRUE;
|
354 | | $this->drupalPost('admin/content/comment'. ($approval ? '/approval' : ''), $edit, t('Update'));
|
355 | |
|
356 | | if ($operation == 'delete') {
|
357 | | $this->drupalPost(NULL, array(), t('Delete comments'));
|
358 | | $this->assertText(t('The comments have been deleted.'), t('Operation "'. $operation .'" was performed on comment.'));
|
359 | | }
|
360 | | else {
|
361 | | $this->assertText(t('The update has been performed.'), t('Operation "'. $operation .'" was performed on comment.'));
|
362 | | }
|
363 | | }
|
364 | |
|
365 | | /**
|
366 | | * Get the comment id for an unaproved comment.
|
367 | | *
|
368 | | * @param string $subject Comment subject to find.
|
369 | | * @return integer Comment id.
|
370 | | */
|
371 | | function getUnaprovedComment($subject) {
|
372 | | $this->drupalGet('admin/content/comment/approval');
|
373 | | preg_match('/href="(.*?)#comment-([^"]+)"(.*?)>('. $subject .')/', $this->drupalGetContent(), $match);
|
374 | | return $match[2];
|
375 | | }
|
376 | | }
|