Issue #1046982 by cousin_itt: Added tests for role-based blocking.
[project/privatemsg.git] / privatemsg.test
1 <?php
2 /**
3 * @file
4 * Test file for privatemsg.module
5 */
6
7 class PrivatemsgTestCase extends DrupalWebTestCase {
8 /**
9 * Implements getInfo().
10 */
11 function getInfo() {
12 return array
13 (
14 // 'name' should start with what is being tested (menu item) followed by what about it
15 // is being tested (creation/deletion).
16 'name' => t('Privatemsg functionality.'),
17 // 'description' should be one or more complete sentences that provide more details on what
18 // exactly is being tested.
19 'description' => t('Test sending, receiving, listing, deleting messages and other features.'),
20 // 'group' should be a logical grouping of test cases, like a category. In most cases, that
21 // is the module the test case is for.
22 'group' => t('Privatemsg'),
23 );
24 }
25
26 /**
27 * Implements setUp().
28 */
29 function setUp() {
30 parent::setUp('privatemsg');
31 }
32
33 /**
34 * Test user access to /messages
35 * Create user with no 'read privatemsg' permission. Try to access mailbox and see if it gives access denied error
36 * Create user with 'read privatemsg' permission. Try to access mailbox and see if it gives allows access
37 */
38 function testPrivatemsgReadPrivatemsgPermission() {
39 $user_no_read_msg = $this->drupalCreateUser(); // set up user with default permissions (meaning: no read privatemsg permission
40 $this->drupalLogin($user_no_read_msg);
41 $this->drupalGet('messages');
42 $this->assertResponse(403, t('HTTP Response 403: Access to mailbox was blocked to user without "<em>read privatemsg</em>" permission'));
43
44 $user_read_msg = $this->drupalCreateUser(array('read privatemsg')); // set up user with default permissions (meaning: no read privatemsg permission
45 $this->drupalLogin($user_read_msg);
46 $this->drupalGet('messages');
47 $this->assertResponse(200, t('HTTP Response 200: Access to mailbox was authorized to user with "<em>read privatemsg</em>" permission'));
48 }
49 /**
50 * Test user access to /messages/new
51 * Create user with no 'write privatemsg' permission. Try to access Write New Message page and see if it gives access denied error
52 * Create user with 'write privatemsg' permission. Try to access Write New Message page and see if it gives allows access
53 */
54 function testPrivatemsgWritePrivatemsgPermission() {
55 $user_no_write_msg = $this->drupalCreateUser(); // set up user with default permissions (meaning: no read privatemsg permission
56 $this->drupalLogin($user_no_write_msg);
57 $this->drupalGet('messages/new');
58 $this->assertResponse(403, t('HTTP Response 403: Access to Write New Message page was blocked to user without "<em>write privatemsg</em>" permission'));
59
60 $user_write_msg = $this->drupalCreateUser(array('write privatemsg')); // set up user with default permissions (meaning: no read privatemsg permission
61 $this->drupalLogin($user_write_msg);
62 $this->drupalGet('messages/new');
63 $this->assertResponse(200, t('HTTP Response 200: Access to Write New Message page was authorized to user with "<em>write privatemsg</em>" permission'));
64 }
65
66 function testPaging() {
67 $author = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
68 $recipient = $this->drupalCreateUser(array('read privatemsg'));
69
70 // Set lower values so that we don't need to generate 100's of messages.
71 variable_set('privatemsg_view_default_amount', 5);
72 variable_set('privatemsg_view_max_amount', 10);
73
74 $subject_single = $this->randomName(20);
75 $subject = $this->randomName(20);
76 $bodies = array();
77 for ($i = 0; $i < 24; $i++) {
78 $bodies[$i] = $this->randomName(100);
79 }
80 privatemsg_new_thread(array($recipient), $subject_single, $bodies[23], array('author' => $author));
81 $thread = privatemsg_new_thread(array($recipient), $subject, $bodies[0], array('author' => $author));
82 for ($i = 1; $i < 23; $i++) {
83 privatemsg_reply($thread['message']['thread_id'], $bodies[$i], array('author' => $author));
84 }
85
86 $this->drupalLogin($recipient);
87 $this->drupalGet('messages');
88 $this->clickLink($subject_single);
89
90 $this->assertText(t('Displaying messages 1 - 1 of 1'), t('Pager is displayed'));
91 $this->assertNoText(t('&gt;&gt;'), t('Newer messages link not displayed.'));
92 $this->assertNoText(t('&lt;&lt;'), t('Older messages link not displayed.'));
93
94 $this->drupalGet('messages');
95 $this->clickLink($subject);
96
97 // Verify that only the last 10 messages are displayed.
98 $this->assertText(t('&lt;&lt; Displaying messages 14 - 23 of 23'), t('Pager is displayed'));
99 $this->assertNoText($bodies[0], t('First message is not displayed.'));
100 $this->assertNoText($bodies[12], t('Hidden message is not displayed.'));
101 $this->assertText($bodies[13], t('Message is displayed.'));
102 $this->assertText($bodies[22], t('Message is displayed.'));
103 $this->assertNoText(t('&gt;&gt;'), t('Newer messages link not displayed.'));
104
105 variable_set('privatemsg_view_use_max_as_default', TRUE);
106 $this->drupalGet('messages');
107 $this->clickLink($subject);
108
109 // Now with separate default value.
110 // Verify that only the last 5 messages are displayed.
111 $this->assertText(t('&lt;&lt; Displaying messages 19 - 23 of 23'), t('Pager is displayed'));
112 $this->assertNoText($bodies[0], t('First message is not displayed.'));
113 $this->assertNoText($bodies[17], t('Hidden message is not displayed.'));
114 $this->assertText($bodies[18], t('Message is displayed.'));
115 $this->assertText($bodies[22], t('Message is displayed.'));
116 $this->assertNoText(t('>>'), t('Newer messages link not displayed.'));
117
118 // Load older messages and verify again.
119 $this->clickLink(t('<<'));
120 $this->assertText(t('&lt;&lt; Displaying messages 9 - 18 of 23 &gt;&gt;'), t('Pager is displayed'));
121 $this->assertNoText($bodies[0], t('First message is not displayed.'));
122 $this->assertNoText($bodies[7], t('Hidden message is not displayed.'));
123 $this->assertText($bodies[8], t('Message is displayed.'));
124 $this->assertText($bodies[17], t('Message is displayed.'));
125 $this->assertNoText($bodies[22], t('Hidden message is not displayed.'));
126
127 // Load older messages and verify again.
128 $this->clickLink(t('<<'));
129 $this->assertText(t('Displaying messages 1 - 8 of 23 &gt;&gt;'), t('Pager is displayed'));
130 $this->assertText($bodies[0], t('Message is displayed.'));
131 $this->assertText($bodies[7], t('Message is displayed.'));
132 $this->assertNoText($bodies[9], t('Hidden message is not displayed.'));
133 $this->assertNoText(t('&lt;&lt;'), t('Older messages link not displayed.'));
134
135 // Going back should follow the same order.
136 $this->clickLink(t('>>'));
137 $this->assertText(t('&lt;&lt; Displaying messages 9 - 18 of 23 &gt;&gt;'), t('Pager is displayed'));
138 $this->assertNoText($bodies[0], t('First message is not displayed.'));
139 $this->assertNoText($bodies[7], t('Hidden message is not displayed.'));
140 $this->assertText($bodies[8], t('Message is displayed.'));
141 $this->assertText($bodies[17], t('Message is displayed.'));
142 $this->assertNoText($bodies[22], t('Hidden message is not displayed.'));
143
144 variable_set('privatemsg_view_max_amount', PRIVATEMSG_UNLIMITED);
145 $this->drupalGet('messages');
146 $this->clickLink($subject);
147
148 // Now with separate default value.
149 // Verify that only the last 5 messages are displayed.
150 $this->assertText(t('&lt;&lt; Displaying messages 19 - 23 of 23'), t('Pager is displayed'));
151 $this->assertNoText($bodies[0], t('First message is not displayed.'));
152 $this->assertNoText($bodies[17], t('Hidden message is not displayed.'));
153 $this->assertText($bodies[18], t('Message is displayed.'));
154 $this->assertText($bodies[22], t('Message is displayed.'));
155 $this->assertNoText(t('&gt;&gt;'), t('Newer messages link not displayed.'));
156
157 // Load older messages and verify again.
158 $this->clickLink(t('<<'));
159 $this->assertText(t('Displaying messages 1 - 23 of 23'), t('Pager is displayed'));
160 $this->assertText($bodies[0], t('Message is displayed.'));
161 $this->assertText($bodies[22], t('Message is displayed.'));
162 $this->assertNoText(t('&gt;&gt;'), t('Newer messages link not displayed.'));
163 $this->assertNoText(t('&lt;&lt;'), t('Older messages link not displayed.'));
164
165 // Check with max_amount = UNLIMITED and different default amount disabled.
166 variable_set('privatemsg_view_use_max_as_default', FALSE);
167
168 $this->drupalGet('messages');
169 $this->clickLink($subject);
170 $this->assertText(t('Displaying messages 1 - 23 of 23'), t('Pager is displayed'));
171 $this->assertText($bodies[0], t('Message is displayed.'));
172 $this->assertText($bodies[22], t('Message is displayed.'));
173 $this->assertNoText(t('&gt;&gt;'), t('Newer messages link not displayed.'));
174 $this->assertNoText(t('&lt;&lt;'), t('Older messages link not displayed.'));
175 }
176
177 /**
178 * Test sending message from the /messages/new page between two people
179 */
180 function testWriteReplyPrivatemsg() {
181 // Create a author and two recipients.
182 $author = $this->drupalCreateUser(array('write privatemsg', 'administer filters'));
183 $recipient = $this->drupalCreateUser(array('read privatemsg'));
184 $recipient2 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
185
186 // Login author and go to new message form.
187 $this->drupalLogin($author);
188 $this->drupalGet('messages/new');
189
190 // Prepare edit arrays, single recipient.
191 $edit = array(
192 'recipient' => $recipient->name,
193 'subject' => $this->randomName(20),
194 'body' => $this->randomName(100),
195 );
196 // Two recipients.
197 $edit2 = array(
198 'recipient' => $recipient->name . ', ' . $recipient2->name,
199 'subject' => $this->randomName(20),
200 'body' => $this->randomName(100),
201 );
202 // No recipients.
203 $editnone = array(
204 'recipient' => '',
205 'subject' => $this->randomName(20),
206 'body' => $this->randomName(100),
207 );
208 // Invalid recipient
209 $editinvalid = array(
210 'recipient' => $this->randomName(5),
211 'subject' => $this->randomName(20),
212 'body' => $this->randomName(100),
213 );
214 // Empty body.
215 $editnobody = array(
216 'recipient' => $recipient->name,
217 'subject' => $this->randomName(20),
218 'body' => '',
219 );
220 // Empty subject.
221 $editnosubject = array(
222 'recipient' => $recipient->name,
223 'subject' => '',
224 'body' => $this->randomName(100),
225 );
226 // Empty subject and body.
227 $editempty = array(
228 'recipient' => $recipient->name,
229 'subject' => '',
230 'body' => '',
231 );
232 // Empty subject and body.
233 $editonlyspace = array(
234 'recipient' => $recipient2->name,
235 'subject' => ' ',
236 'body' => $this->randomName(10),
237 );
238 // Invalid and valid recipient
239 $editmixed = array(
240 'recipient' => ($invalidmixed = $this->randomName(5)) . ', ' . $recipient->name,
241 'subject' => $this->randomName(20),
242 'body' => $this->randomName(100),
243 );
244
245 // message with a bold part, not allowed with default format
246 $editformatted = array(
247 'recipient' => $recipient2->name,
248 'subject' => $this->randomName(20),
249 'body' => $this->randomName(100) . '<b>formatted message #1</b>',
250 'format' => 2,
251 );
252
253 // Submit the messages.
254 $this->drupalPost('messages/new', $edit, t('Send message'));
255 $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $recipient->name)), 'Message sent confirmation displayed.');
256
257 $this->drupalPost('messages/new', $edit2, t('Send message'));
258 $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => implode(', ', array($recipient->name, $recipient2->name)))), 'Message sent confirmation displayed.');
259
260 $this->drupalPost('messages/new', $editnone, t('Send message'));
261 $this->assertText(t('Disallowed to send a message without at least one valid recipient'), 'Message was not sent.');
262
263 $this->drupalPost('messages/new', $editinvalid, t('Send message'));
264 $this->assertText(t('Disallowed to send a message without at least one valid recipient'), 'Message was not sent.');
265 $this->assertText(t('The following users will not receive this private message: @recipients', array('@recipients' => $editinvalid['recipient'])), 'Message about non-existing user displayed.');
266
267 $this->drupalPost('messages/new', $editnobody, t('Send message'));
268 $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $recipient->name)), 'Message sent confirmation displayed.');
269
270 $this->drupalPost('messages/new', $editnosubject, t('Send message'));
271 $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $recipient->name)), 'Message sent confirmation displayed.');
272
273 $this->drupalPost('messages/new', $editempty, t('Send message'));
274 $this->assertText(t('Disallowed to send a message without subject'), 'Empty subject message displayed.');
275
276 $this->drupalPost('messages/new', $editonlyspace, t('Send message'));
277 $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $recipient2->name)), 'Message sent confirmation displayed.');
278
279 $this->drupalPost('messages/new', $editmixed, t('Send message'));
280 $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $recipient->name)), 'Message sent confirmation displayed.');
281 $this->assertText(t('The following users will not receive this private message: @recipients', array('@recipients' => $invalidmixed)), 'Message about non-existing user displayed.');
282
283 $this->drupalPost('messages/new', $editformatted, t('Send message'));
284 $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $recipient2->name)), 'Message sent confirmation displayed.');
285
286 // Login as recipient2 and try to write some replies.
287 $this->drupalLogin($recipient2);
288 $this->drupalGet('messages');
289
290 // Check that the message with only a space in the subject uses the body
291 // as subject.
292 $this->clickLink($editonlyspace['body']);
293 $this->drupalGet('messages');
294
295 $this->assertNoText($edit['subject'], 'Message sent to other recipient not found.');
296 $this->assertText($edit2['subject'], 'Sent message subject found.');
297 $this->clickLink($edit2['subject']);
298
299 $this->assertText($edit2['body'], 'Found message body.');
300
301 // Prepare replies.
302 $reply = array(
303 'body' => $this->randomName(100),
304 );
305 // Empty body.
306 $replyempty = array(
307 'body' => '',
308 );
309
310 $this->drupalPost(NULL, $reply, t('Send message'));
311 $this->assertText($reply['body'], 'New message body displayed.');
312
313 $this->drupalPost(NULL, $replyempty, t('Send message'));
314 $this->assertText(t('Disallowed to send reply without a message.'));
315
316 // reply with a bold part, not allowed with default format
317 $replyformatted = array(
318 'body' => $this->randomName(100) . '<b>formatted message #2</b>',
319 );
320 $this->drupalGet('messages');
321 $this->clickLink($editformatted['subject']);
322 $this->assertRaw($editformatted['body'], 'Found formatted message body.');
323
324 $this->drupalPost(NULL, $replyformatted, t('Send message'));
325 $this->assertNoRaw($replyformatted['body'], 'Did not find formatted reply body.');
326 $this->assertText(strip_tags($replyformatted['body']), 'New reply body displayed.');
327
328 // Login using recipient and try to read the message by going to inbox first.
329 $this->drupalLogin($recipient);
330 $this->drupalGet('messages');
331
332 // Assert if we see the subject of the messages.
333 $this->assertText($edit['subject'], 'Sent message subject found.');
334 $this->assertText($edit2['subject'], 'Sent message subject found.');
335 $this->assertText($editnobody['subject'], 'Sent message subject found.');
336 $this->assertText(trim(truncate_utf8(strip_tags($editnosubject['body']), 50, TRUE, TRUE)), 'Sent message subject found.');
337 $this->assertText($editmixed['subject'], 'Sent message subject found.');
338
339 // Assert that we don't see those that were invalid.
340 $this->assertNoText($editnone['subject'], 'Invalid message subject not found.');
341 $this->assertNoText($editinvalid['subject'], 'Invalid message subject not found.');
342
343 // Navigate into the message.
344 $this->clickLink($edit['subject']);
345 // Confirm that we can read the message that was sent.
346 $this->assertText($edit['body'], 'Found message body.');
347 $this->assertNoText(t('Reply to thread:'), 'Reply form is not displayed.');
348
349 // Navigate into the message.
350 $this->drupalGet('messages');
351 $this->clickLink($edit2['subject']);
352 // Confirm that we can read the message that was sent.
353 $this->assertText($edit2['body'], 'Found message body.');
354 // Confirm that we can read the reply that was sent.
355 $this->assertText($reply['body'], 'Found reply body.');
356 }
357
358 /**
359 * Test correct handling of read all permissions.
360 */
361 function testReadAllPermission() {
362 $author = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
363 $recipient = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
364 $admin = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'read all private messages'));
365
366 // Create new message.
367 $edit = array(
368 'recipient' => $recipient->name,
369 'subject' => $this->randomName(20),
370 'body' => $this->randomName(100),
371 );
372 $this->drupalLogin($author);
373 $this->drupalPost('messages/new', $edit, t('Send message'));
374
375 $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $recipient->name)), t('Message sent confirmation displayed'));
376
377 $this->drupalLogin($admin);
378 $this->drupalGet('messages/view/1');
379
380 $this->assertText(t('This conversation is being viewed with escalated priviledges and may not be the same as shown to normal users.'), t('Notice about read all mode displayed.'));
381
382 // Send a first response.
383 $admin_edit = array(
384 'body' => $this->randomName(100),
385 );
386 $this->drupalPost('messages/view/1', $admin_edit, t('Send message'));
387
388 // Make sure that the notice is not displayed anymore.
389 $this->assertNoText(t('This conversation is being viewed with escalated priviledges and may not be the same as shown to normal users.'), t('Notice about read all mode not displayed.'));
390
391 // Make sure that both the existing message body and the new one are displayed.
392 $this->assertText($edit['body'], t('First message body displayed.'));
393 $this->assertText($admin_edit['body'], t('New message body displayed.'));
394
395 $admin_recipient_count = db_result(db_query("SELECT COUNT(*) FROM {pm_index} WHERE uid = %d AND thread_id = %d", $admin->uid, 1));
396 $this->assertEqual($admin_recipient_count, 2, t('Admin is listed as recipient for every message once.'));
397
398
399 // Send a second response.
400 $admin_edit2 = array(
401 'body' => $this->randomName(100),
402 );
403 $this->drupalPost('messages/view/1', $admin_edit2, t('Send message'));
404
405 // Make sure that both the existing message body and the new one are displayed.
406 $this->assertText($edit['body'], t('First message body displayed.'));
407 $this->assertText($admin_edit['body'], t('Second response body displayed.'));
408 $this->assertText($admin_edit2['body'], t('Third message body displayed.'));
409
410 $admin_recipient_count = db_result(db_query("SELECT COUNT(*) FROM {pm_index} WHERE uid = %d AND thread_id = %d", $admin->uid, 1));
411 $this->assertEqual($admin_recipient_count, 3, t('Admin is listed as recipient for every message once.'));
412
413 }
414
415 /**
416 * Tests for the flush feature
417 */
418 function testPrivatemsgFlush() {
419 $author = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
420 $recipient = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
421
422 // Send 10 messages.
423 for ($i = 0; $i < 10; $i++) {
424 privatemsg_new_thread(array($recipient), 'Message #'. $i, 'This is the body', array('author' => $author));
425 }
426
427 // Delete message 1, 3, 4, 6, 9 for author.
428 foreach (array(1, 3, 4, 6, 9) as $pmid) {
429 privatemsg_message_change_delete($pmid, TRUE, $author);
430 }
431
432 // Delete message 1, 2, 4, 6, 8 for recipient.
433 foreach (array(1, 3, 4, 6, 9) as $pmid) {
434 privatemsg_message_change_delete($pmid, TRUE, $recipient);
435 }
436
437 // Now, mid 1, 4 and 6 have been deleted by both.
438
439 // Flush configuration, enable, delay is default, 30 days
440 variable_set('privatemsg_flush_enabled', TRUE);
441
442 // Set back the deleted timestamp 35 days back of mid 4.
443 db_query('UPDATE {pm_index} SET deleted = %d WHERE mid = 4', time() - 35 * 86400);
444 // Set back the deleted timestamp of mid 6, but only 20 back.
445 db_query('UPDATE {pm_index} SET deleted = %d WHERE mid = 6', time() - 20 * 86400);
446
447 // Run flush.
448 privatemsg_cron();
449
450 // Check if the undeleted messages are still there.
451 foreach (array(2, 3, 5, 7, 8, 9, 10) as $pmid) {
452 $message = privatemsg_message_load($pmid, $author);
453 $this->assertTrue(!empty($message), t('Undeleted message #%id is still in the system', array('%id' => $pmid)));
454 }
455
456 // Check if the "recently" deleted messages are still there.
457 foreach (array(1, 6) as $pmid) {
458 $message = privatemsg_message_load($pmid, $author);
459 $this->assertTrue(!empty($message), t('Deleted message #%id is still in the system', array('%id' => $pmid)));
460 }
461
462 // Mid 4 should have been flushed.
463 $message = privatemsg_message_load(4, $author);
464 $this->assertTrue(empty($message), t('Message #4 has been flushed'));
465 }
466
467 function testDelete() {
468 // Create users.
469 $author = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'delete privatemsg'));
470 $recipient = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'delete privatemsg'));
471 $recipient2 = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
472 $admin = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'delete privatemsg', 'read all private messages'));
473
474 // Create texts.
475 $subject = $this->randomName(20);
476 $body1 = $this->randomName(100);
477 $body2 = $this->randomName(100);
478
479 // Create message and response.
480 $return = privatemsg_new_thread(array($recipient, $recipient2), $subject, $body1, array('author' => $author));
481 privatemsg_reply($return['message']['thread_id'], $body2, array('author' => $recipient));
482
483 // Check with user without delete permission.
484 $this->drupalLogin($recipient2);
485 $this->drupalGet('messages/view/' . $return['message']['thread_id']);
486 $this->assertText($subject, 'Subject is displayed');
487 $this->assertText($body1, 'First message is displayed');
488 $this->assertText($body2, 'Second message is displayed');
489 $this->assertNoText(t('Delete message'), 'Delete message is link is not displayed for user without permission');
490
491 // Check if access for that user is denied.
492 $this->drupalGet('messages/delete/' . $return['message']['thread_id'] . '/' . $return['message']['mid']);
493 $this->assertText(t('Access denied'));
494
495 // Check with user with delete access.
496 $this->drupalLogin($recipient);
497 $this->drupalGet('messages/view/' . $return['message']['thread_id']);
498 $this->assertText(t('Delete message'), 'Delete message is link is displayed for user without permission');
499
500 // Click delete link of the second message and cancel.
501 $this->clickLink(t('Delete message'), 1);
502 $this->assertText(t('Are you sure you want to delete this message?'), 'Confirmation message displayed');
503 $this->clickLink(t('Cancel'));
504 $this->assertText($body2, 'Second message is still displayed');
505
506 // Confirm message deletion.
507 $this->clickLink(t('Delete message'), 1);
508 $this->assertText(t('Are you sure you want to delete this message?'), 'Confirmation message displayed');
509 $this->drupalPost(NULL, array(), t('Delete'));
510 $this->assertText(t('Message has been deleted.'), 'Message has been deleted');
511 $this->assertText($body1, 'First message is still displayed');
512 $this->assertNoText($body2, 'Second message was deleted');
513
514 // Click delete link of the first message and cancel.
515 $this->clickLink(t('Delete message'));
516 $this->assertText(t('Are you sure you want to delete this message?'), 'Confirmation message displayed');
517 $this->clickLink(t('Cancel'));
518 $this->assertText($body1, 'First message is still displayed');
519
520 // Confirm message deletion.
521 $this->clickLink(t('Delete message'));
522 $this->assertText(t('Are you sure you want to delete this message?'), 'Confirmation message displayed');
523 $this->drupalPost(NULL, array(), t('Delete'));
524 $this->assertText(t('Message has been deleted.'), 'Message deleted has been deleted');
525 $this->assertNoText($subject, 'All messages of that thread have been deleted');
526
527 // Test if the message has not been deleted for other users.
528 $this->drupalLogin($recipient2);
529 $this->drupalGet('messages/view/' . $return['message']['thread_id']);
530 $this->assertText($body1, 'First message is still displayed');
531 $this->assertText($body2, 'First message is still displayed');
532
533 // Test delete all checkbox.
534 $this->drupalLogin($admin);
535 $this->drupalGet('messages/view/' . $return['message']['thread_id']);
536 $this->clickLink(t('Delete message'), 1);
537 $this->drupalPost(NULL, array('delete_options' => TRUE), t('Delete'));
538 $this->assertText(t('Message has been deleted for all users.'), 'Message deleted has been deleted');
539
540 // Test if the message has been deleted for all users.
541 $this->drupalLogin($recipient2);
542 $this->drupalGet('messages/view/' . $return['message']['thread_id']);
543 $this->assertText($body1, 'First message is still displayed');
544 $this->assertNoText($body2, 'Second message has been deleted for all users');
545
546 // Check that messages of deleted users are hidden.
547 $edit = array('body' => $this->randomName(100));
548 $this->drupalPost(NULL, $edit, t('Send message'));
549
550 $this->drupalLogin($admin);
551 $this->drupalGet('messages/view/' . $return['message']['thread_id']);
552 $this->assertText($edit['body'], t('New reply is displayed'));
553 user_delete(array(), $recipient2->uid);
554 $this->drupalGet('messages/view/' . $return['message']['thread_id']);
555 $this->assertText($body1, 'First message is still displayed');
556 $this->assertNoText($edit['body'], t('Reply of deleted user is not displayed anymore'));
557 }
558
559 /**
560 * Test preview functionality.
561 */
562 function testPreview() {
563 $user = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
564
565 $message = array(
566 'recipient' => $user->name,
567 'subject' => $this->randomName(),
568 'body' => $this->randomName(50),
569 );
570 $this->drupalLogin($user);
571 $this->drupalPost('messages/new', $message, t('Preview message'));
572 $this->assertTitle(t('Write new message to @user', array('@user' => $user->name)) . ' | Drupal', t('Correct title is displayed.'));
573 $this->assertFieldByXPath("//div[@class='message-body']/p", $message['body'], t('Message body is previewed'));
574 }
575 }
576
577 /**
578 * Tests for node, blocks and profile integration.
579 */
580 class PrivatemsgLinksTestCase extends DrupalWebTestCase {
581
582 /**
583 * Implements of getInfo().
584 */
585 public function getInfo() {
586 return array
587 (
588 'name' => t('Privatemsg links'),
589 'description' => t('Tests links displayed in nodes, profiles and blocks.'),
590 'group' => t('Privatemsg'),
591 );
592 }
593
594 /**
595 * Implements setUp().
596 */
597 function setUp() {
598 parent::setUp('privatemsg');
599 }
600
601 /**
602 * Tests menu block.
603 */
604 function testMenuBlock() {
605 $admin = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'administer privatemsg settings', 'administer blocks', 'administer menu'));
606 $user = $this->drupalCreateUser(array('read privatemsg'));
607
608 // Enable block.
609 $this->drupalLogin($admin);
610 $blocks = array(
611 'privatemsg_privatemsg-menu[region]' => 'right',
612 );
613 $this->drupalPost('admin/build/block', $blocks, t('Save blocks'));
614
615 // Disable secondary menu.
616 /* @todo: Not yet possible because simpletest needs a log out link to verify that the user is logged in.
617 $menu_settings = array(
618 'menu_secondary_links_source' => '',
619 );
620 $this->drupalPost('admin/structure/menu/settings', $menu_settings, t('Save configuration'));
621 *
622 */
623
624 $this->drupalGet('');
625 $this->assertRaw('<h2>Private messages</h2>', t('Privatemsg menu block title displayed.'));
626 $this->assertText('Write new message', t('Write new message link displayed.'));
627
628 $message = array(
629 'recipient' => $user->name,
630 'subject' => $this->randomName(),
631 'body' => $this->randomName(50),
632 );
633 $this->drupalPost('messages/new', $message, t('Send message'));
634
635 $this->drupalLogin($user);
636 $this->assertNoText(t('Write new message'), t('Write new message link not displayed.'));
637 $this->assertNoUniqueText(t('Messages (1 new)'), t('Messages link including new message information displayed'));
638 }
639
640 /**
641 * Tests menu block.
642 */
643 function testNewBlock() {
644 $admin = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'administer privatemsg settings', 'administer blocks', 'administer menu'));
645 $user = $this->drupalCreateUser(array('read privatemsg'));
646
647 // Enable block.
648 $this->drupalLogin($admin);
649 $blocks = array(
650 'privatemsg_privatemsg-new[region]' => 'right',
651 );
652 $this->drupalPost('admin/build/block', $blocks, t('Save blocks'));
653
654 $this->drupalGet('');
655 $this->assertNoRaw('<h2>New message</h2>', t('Privatemsg new block title not displayed.'));
656
657 $message = array(
658 'recipient' => $user->name,
659 'subject' => $this->randomName(),
660 'body' => $this->randomName(50),
661 );
662 $this->drupalPost('messages/new', $message, t('Send message'));
663
664 $this->drupalLogin($user);
665 $this->assertRaw('<h2>New message</h2>', t('Privatemsg new block title displayed.'));
666
667 $this->assertText(t('You have a new message, click here to read it'), t('New message indication displayed.'));
668 }
669 }