Projet

Général

Profil

Paste
Télécharger (65 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2
/**
3
 * @file
4
 * Test file for privatemsg.module
5
 *
6
 */
7

    
8
/**
9
 * Base class for Privatemsg tests.
10
 */
11
class PrivatemsgBaseTestCase extends DrupalWebTestCase {
12
  /**
13
   * Use testing profile.
14
   */
15
  protected $profile = 'testing';
16
}
17

    
18
class PrivatemsgTestCase extends PrivatemsgBaseTestCase {
19
  /**
20
   * Implements getInfo().
21
   */
22
  public static function getInfo() {
23
    return array
24
    (
25
      // 'name' should start with what is being tested (menu item) followed by what about it
26
      // is being tested (creation/deletion).
27
      'name' => t('Privatemsg functionality.'),
28
      // 'description' should be one or more complete sentences that provide more details on what
29
      // exactly is being tested.
30
      'description' => t('Test sending, receiving, listing, deleting messages and other features.'),
31
      // 'group' should be a logical grouping of test cases, like a category.  In most cases, that
32
      // is the module the test case is for.
33
      'group' => t('Privatemsg'),
34
    );
35
  }
36

    
37
  /**
38
   * Implements setUp().
39
   */
40
  function setUp() {
41
    parent::setUp('privatemsg');
42

    
43

    
44
    // Create the full html format.
45
    $full_html_format = array(
46
      'format' => 'full_html',
47
      'name' => 'Full HTML',
48
      'weight' => 1,
49
      'filters' => array(
50
        // URL filter.
51
        'filter_url' => array(
52
          'weight' => 0,
53
          'status' => 1,
54
        ),
55
        // Line break filter.
56
        'filter_autop' => array(
57
          'weight' => 1,
58
          'status' => 1,
59
        ),
60
        // HTML corrector filter.
61
        'filter_htmlcorrector' => array(
62
          'weight' => 10,
63
          'status' => 1,
64
        ),
65
      ),
66
    );
67
    $full_html_format = (object) $full_html_format;
68
    filter_format_save($full_html_format);
69
    // Refresh permissions.
70
    $this->checkPermissions(array(), TRUE);
71
  }
72

    
73
  /**
74
   * Test user access to /messages
75
   * Create user with no 'read privatemsg' permission. Try to access mailbox and see if it gives access denied error
76
   * Create user with 'read privatemsg' permission. Try to access mailbox and see if it gives allows access
77
   */
78
  function testPrivatemsgReadPrivatemsgPermission() {
79
    $user_no_read_msg = $this->drupalCreateUser(); // set up user with default permissions (meaning: no read privatemsg permission
80
    $author     = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
81
    $recipient  = $this->drupalCreateUser(array('read privatemsg'));
82
    $no_recipient  = $this->drupalCreateUser(array('read privatemsg'));
83

    
84

    
85
    $subject = $this->randomName(20);
86
    $body = $this->randomName(50);
87

    
88
    $response = privatemsg_new_thread(array($recipient), $subject, $body, array('author' => $author));
89

    
90
    $this->drupalLogin($user_no_read_msg);
91
    $this->drupalGet('messages');
92
    $this->assertResponse(403, t('HTTP Response 403: Access to mailbox was blocked to user without "<em>read privatemsg</em>" permission'));
93

    
94
    $this->drupalLogin($no_recipient);
95
    $this->drupalGet('messages');
96
    $this->assertResponse(200, t('HTTP Response 200: Access to mailbox was authorized to user with "<em>read privatemsg</em>" permission'));
97

    
98
    $this->drupalGet('messages/view/' . $response['message']->thread_id);
99
    $this->assertResponse(403, t('HTTP Response 403: Access to thread is blocked for non-recipients.'));
100

    
101
    $this->drupalLogin($recipient);
102
    $this->drupalGet('messages/view/' . $response['message']->thread_id);
103
    $this->assertText($subject, t('Access to thread for recipient allowed.'));
104

    
105
    $this->drupalGet('messages/view/' . $response['message']->thread_id + 1);
106
    $this->assertResponse(404, t('Non-existing thread lead to HTTP Response 404.'));
107

    
108
  }
109
  /**
110
   * Test user access to /messages/new
111
   * Create user with no 'write privatemsg' permission. Try to access Write New Message page and see if it gives access denied error
112
   * Create user with 'write privatemsg' permission. Try to access Write New Message page and see if it gives allows access
113
   */
114
  function testPrivatemsgWritePrivatemsgPermission() {
115
    $user_no_write_msg = $this->drupalCreateUser(); // set up user with default permissions (meaning: no read privatemsg permission
116
    $this->drupalLogin($user_no_write_msg);
117
    $this->drupalGet('messages/new');
118
    $this->assertResponse(403, t('HTTP Response 403: Access to Write New Message page was blocked to user without "<em>write privatemsg</em>" permission'));
119

    
120
    $user_write_msg = $this->drupalCreateUser(array('write privatemsg')); // set up user with write privatemsg permissions
121
    $this->drupalLogin($user_write_msg);
122
    $this->drupalGet('messages/new');
123
    $this->assertResponse(200, t('HTTP Response 200: Access to Write New Message page was authorized to user with "<em>write privatemsg</em>" permission'));
124
  }
125

    
126
  function testPaging() {
127
    $author     = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
128
    $recipient  = $this->drupalCreateUser(array('read privatemsg'));
129

    
130
    // Set lower values so that we don't need to generate 100's of messages.
131
    variable_set('privatemsg_view_default_amount', 5);
132
    variable_set('privatemsg_view_max_amount', 10);
133

    
134
    $subject_single = $this->randomName(20);
135
    $subject = $this->randomName(20);
136
    $bodies = array();
137
    for ($i = 0; $i < 24; $i++) {
138
      $bodies[$i] = $this->randomName(100);
139
    }
140
    privatemsg_new_thread(array($recipient), $subject_single, $bodies[23], array('author' => $author));
141
    $thread = privatemsg_new_thread(array($recipient), $subject, $bodies[0], array('author' => $author));
142
    for ($i = 1; $i < 23; $i++) {
143
      privatemsg_reply($thread['message']->thread_id, $bodies[$i], array('author' => $author));
144
    }
145

    
146
    $this->drupalLogin($recipient);
147
    $this->drupalGet('messages');
148
    $this->clickLink($subject_single);
149

    
150
    $this->assertNoText(t('Displaying messages 1 - 1 of 1'), t('Pager is displayed'));
151
    $this->assertNoText(t('&gt;&gt;'), t('Newer messages link not displayed.'));
152
    $this->assertNoText(t('&lt;&lt;'), t('Older messages link not displayed.'));
153

    
154
    $this->drupalGet('messages');
155
    $this->clickLink($subject);
156

    
157
    // Verify that only the last 10 messages are displayed.
158
    $this->assertText(t('&lt;&lt; Displaying messages 14 - 23 of 23'), t('Pager is displayed'));
159
    $this->assertNoText($bodies[0], t('First message is not displayed.'));
160
    $this->assertNoText($bodies[12], t('Hidden message is not displayed.'));
161
    $this->assertText($bodies[13], t('Message is displayed.'));
162
    $this->assertText($bodies[22], t('Message is displayed.'));
163
    $this->assertNoText(t('&gt;&gt;'), t('Newer messages link not displayed.'));
164

    
165
    variable_set('privatemsg_view_use_max_as_default', TRUE);
166
    $this->drupalGet('messages');
167
    $this->clickLink($subject);
168

    
169
    // Now with separate default value.
170
    // Verify that only the last 5 messages are displayed.
171
    $this->assertText(t('&lt;&lt; Displaying messages 19 - 23 of 23'), t('Pager is displayed'));
172
    $this->assertNoText($bodies[0], t('First message is not displayed.'));
173
    $this->assertNoText($bodies[17], t('Hidden message is not displayed.'));
174
    $this->assertText($bodies[18], t('Message is displayed.'));
175
    $this->assertText($bodies[22], t('Message is displayed.'));
176
    $this->assertNoText(t('>>'), t('Newer messages link not displayed.'));
177

    
178
    // Load older messages and verify again.
179
    $this->clickLink(t('<<'));
180
    $this->assertText(t('&lt;&lt; Displaying messages 9 - 18 of 23 &gt;&gt;'), t('Pager is displayed'));
181
    $this->assertNoText($bodies[0], t('First message is not displayed.'));
182
    $this->assertNoText($bodies[7], t('Hidden message is not displayed.'));
183
    $this->assertText($bodies[8], t('Message is displayed.'));
184
    $this->assertText($bodies[17], t('Message is displayed.'));
185
    $this->assertNoText($bodies[22], t('Hidden message is not displayed.'));
186

    
187
    // Load older messages and verify again.
188
    $this->clickLink(t('<<'));
189
    $this->assertText(t('Displaying messages 1 - 8 of 23 &gt;&gt;'), t('Pager is displayed'));
190
    $this->assertText($bodies[0], t('Message is displayed.'));
191
    $this->assertText($bodies[7], t('Message is displayed.'));
192
    $this->assertNoText($bodies[9], t('Hidden message is not displayed.'));
193
    $this->assertNoText(t('&lt;&lt;'), t('Older messages link not displayed.'));
194

    
195
    // Going back should follow the same order.
196
    $this->clickLink(t('>>'));
197
    $this->assertText(t('&lt;&lt; Displaying messages 9 - 18 of 23 &gt;&gt;'), t('Pager is displayed'));
198
    $this->assertNoText($bodies[0], t('First message is not displayed.'));
199
    $this->assertNoText($bodies[7], t('Hidden message is not displayed.'));
200
    $this->assertText($bodies[8], t('Message is displayed.'));
201
    $this->assertText($bodies[17], t('Message is displayed.'));
202
    $this->assertNoText($bodies[22], t('Hidden message is not displayed.'));
203

    
204
    variable_set('privatemsg_view_max_amount', PRIVATEMSG_UNLIMITED);
205
    $this->drupalGet('messages');
206
    $this->clickLink($subject);
207

    
208
    // Now with separate default value.
209
    // Verify that only the last 5 messages are displayed.
210
    $this->assertText(t('&lt;&lt; Displaying messages 19 - 23 of 23'), t('Pager is displayed'));
211
    $this->assertNoText($bodies[0], t('First message is not displayed.'));
212
    $this->assertNoText($bodies[17], t('Hidden message is not displayed.'));
213
    $this->assertText($bodies[18], t('Message is displayed.'));
214
    $this->assertText($bodies[22], t('Message is displayed.'));
215
    $this->assertNoText(t('&gt;&gt;'), t('Newer messages link not displayed.'));
216

    
217
    // Load older messages and verify again.
218
    $this->clickLink(t('<<'));
219
    $this->assertNoText(t('Displaying messages 1 - 23 of 23'), t('Pager is displayed'));
220
    $this->assertText($bodies[0], t('Message is displayed.'));
221
    $this->assertText($bodies[22], t('Message is displayed.'));
222
    $this->assertNoText(t('&gt;&gt;'), t('Newer messages link not displayed.'));
223
    $this->assertNoText(t('&lt;&lt;'), t('Older messages link not displayed.'));
224

    
225
    // Check with max_amount = UNLIMITED and different default amount disabled.
226
    variable_set('privatemsg_view_use_max_as_default', FALSE);
227

    
228
    $this->drupalGet('messages');
229
    $this->clickLink($subject);
230
    $this->assertNoText(t('Displaying messages 1 - 23 of 23'), t('Pager is displayed'));
231
    $this->assertText($bodies[0], t('Message is displayed.'));
232
    $this->assertText($bodies[22], t('Message is displayed.'));
233
    $this->assertNoText(t('&gt;&gt;'), t('Newer messages link not displayed.'));
234
    $this->assertNoText(t('&lt;&lt;'), t('Older messages link not displayed.'));
235
  }
236

    
237
  /**
238
   * Test sending message from the /messages/new page between two people
239
   */
240
  function testWriteReplyPrivatemsg() {
241
    // Create an author and two recipients.
242
    $author     = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'select text format for privatemsg', filter_permission_name(filter_format_load('full_html'))));
243
    $recipient  = $this->drupalCreateUser(array('read privatemsg'));
244
    $recipient2  = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
245
    // Set up a user with "read/write privatemsg" permissions.
246
    $blocked_recipient = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
247
    // Block this recipient to test users who cancelled their accounts.
248
    user_save($blocked_recipient, array('status' => 0));
249

    
250
    // Login author and go to new message form.
251
    $this->drupalLogin($author);
252
    $this->drupalGet('messages/new');
253

    
254
    // Prepare edit arrays, single recipient with [user].
255
    $edit = array(
256
      'recipient'   => $recipient->name . ' [user]',
257
      'subject'     => $this->randomName(20),
258
      'body[value]' => $this->randomName(100),
259
    );
260
    // Two recipients.
261
    $edit2 = array(
262
      'recipient'   => $recipient->name . ', ' . $recipient2->name,
263
      'subject'     => $this->randomName(20),
264
      'body[value]' => $this->randomName(100),
265
    );
266
    // No recipients.
267
    $editnone = array(
268
      'recipient'   => '',
269
      'subject'     => $this->randomName(20),
270
      'body[value]' => $this->randomName(100),
271
    );
272
    // Invalid recipient.
273
    $editinvalid = array(
274
      'recipient'   => $this->randomName(5),
275
      'subject'     => $this->randomName(20),
276
      'body[value]' => $this->randomName(100),
277
    );
278
    // Blocked recipient.
279
    $editrecipientblocked = array(
280
      'recipient'   => $blocked_recipient->name,
281
      'subject'     => $this->randomName(20),
282
      'body[value]' => $this->randomName(100),
283
    );
284
    // Message for which the author will be blocked later.
285
    $editauthorblocked = array(
286
      'recipient'   => $recipient2->name,
287
      'subject'     => $this->randomName(20),
288
      'body[value]' => $this->randomName(100),
289
    );
290
    // Empty body.
291
    $editnobody = array(
292
      'recipient'   => $recipient->name,
293
      'subject'     => $this->randomName(20),
294
      'body[value]' => '',
295
    );
296
    // Empty subject.
297
    $editnosubject = array(
298
      'recipient'   => $recipient->name,
299
      'subject'     => '',
300
      'body[value]' => $this->randomName(100),
301
    );
302
    // Empty subject and body.
303
    $editempty = array(
304
      'recipient'   => $recipient->name,
305
      'subject'     => '',
306
      'body[value]' => '',
307
    );
308
    // Empty subject and body.
309
    $editonlyspace = array(
310
      'recipient'   => $recipient2->name,
311
      'subject'     => ' ',
312
      'body[value]' => $this->randomName(10),
313
    );
314
    // Invalid and valid recipient
315
    $editmixed = array(
316
      'recipient'   => ($invalidmixed = $this->randomName(5)) . ', ' . $recipient->name,
317
      'subject'     => $this->randomName(20),
318
      'body[value]' => $this->randomName(100),
319
    );
320

    
321
    // message with a bold part, not allowed with default format
322
    $editformatted = array(
323
      'recipient'   => $recipient2->name,
324
      'subject'     => $this->randomName(20),
325
      'body[value]' => $this->randomName(100) . '<b>formatted message #1</b>',
326
      'body[format]' => 'full_html',
327
    );
328

    
329
    // Submit the messages.
330
    $this->drupalPost('messages/new', $edit, t('Send message'));
331
    $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $recipient->name)), 'Message sent confirmation displayed.');
332

    
333
    $this->drupalPost('messages/new', $edit2, t('Send message'));
334
    $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => implode(', ', array($recipient->name, $recipient2->name)))), 'Message sent confirmation displayed.');
335

    
336
    $this->drupalPost('messages/new', $editnone, t('Send message'));
337
    $this->assertText(t('To field is required.'), 'Message was not sent.');
338

    
339
    $this->drupalPost('messages/new', $editinvalid, t('Send message'));
340
    $this->assertText(t('You must include at least one valid recipient.'), 'Message was not sent.');
341
    $this->assertText(t('The following users will not receive this private message: @recipients', array('@recipients' => $editinvalid['recipient'])), 'Message about non-existing user displayed.');
342

    
343
    $this->drupalPost('messages/new', $editrecipientblocked, t('Send message'));
344
    $this->assertText(t('@recipients has disabled his or her account.', array('@recipients' => $blocked_recipient->name)), 'Message about blocked user displayed.');
345
    $this->assertText(t('You are not allowed to send this message because all recipients are blocked.'), 'Message was not sent.');
346

    
347
    // We will block the author later to test whether the reply form appears.
348
    $this->drupalPost('messages/new', $editauthorblocked, t('Send message'));
349
    $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $recipient2->name)), 'Message sent confirmation displayed.');
350

    
351
    $this->drupalPost('messages/new', $editnobody, t('Send message'));
352
    $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $recipient->name)), 'Message sent confirmation displayed.');
353

    
354
    $this->drupalPost('messages/new', $editnosubject, t('Send message'));
355
    $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $recipient->name)), 'Message sent confirmation displayed.');
356

    
357
    $this->drupalPost('messages/new', $editempty, t('Send message'));
358
    $this->assertText(t('You must include a subject line with your message.'), 'Empty subject message displayed.');
359

    
360
    $this->drupalPost('messages/new', $editonlyspace, t('Send message'));
361
    $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $recipient2->name)), 'Message sent confirmation displayed.');
362

    
363
    $this->drupalPost('messages/new', $editmixed, t('Send message'));
364
    $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $recipient->name)), 'Message sent confirmation displayed.');
365
    $this->assertText(t('The following users will not receive this private message: @recipients', array('@recipients' => $invalidmixed)), 'Message about non-existing user displayed.');
366

    
367
    $this->drupalPost('messages/new', $editformatted, t('Send message'));
368
    $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $recipient2->name)), 'Message sent confirmation displayed.');
369

    
370
    // Login as recipient2 and try to write some replies.
371
    $this->drupalLogin($recipient2);
372
    $this->drupalGet('messages');
373

    
374
    // Check that the message with only a space in the subject uses the body
375
    // as subject.
376
    $this->clickLink($editonlyspace['body[value]']);
377
    $this->drupalGet('messages');
378

    
379
    $this->assertNoText($edit['subject'], 'Message sent to other recipient not found.');
380
    $this->assertText($edit2['subject'], 'Sent message subject found.');
381
    $this->clickLink($edit2['subject']);
382

    
383
    $this->assertText($edit2['body[value]'], 'Found message body.');
384

    
385
    // Prepare replies.
386
    $reply = array(
387
      'body[value]' => $this->randomName(100),
388
    );
389
    // Empty body.
390
    $replyempty = array(
391
      'body[value]' => '',
392
    );
393

    
394
    $this->drupalPost(NULL, $reply, t('Send message'));
395
    $this->assertText($reply['body[value]'], 'New message body displayed.');
396

    
397
    $this->drupalPost(NULL, $replyempty, t('Send message'));
398
    $this->assertText(t('You must include a message in your reply.'));
399

    
400
    // reply with a bold part, not allowed with default format
401
    $replyformatted = array(
402
      'body[value]' => $this->randomName(100) . '<b>formatted message #2</b>',
403
    );
404
    $this->drupalGet('messages');
405
    $this->clickLink($editformatted['subject']);
406
    $this->assertRaw($editformatted['body[value]'], 'Found formatted message body.');
407

    
408
    $this->drupalPost(NULL, $replyformatted, t('Send message'));
409
    $this->assertNoRaw($replyformatted['body[value]'], 'Did not find formatted reply body.');
410
    $this->assertText(check_plain($replyformatted['body[value]']), 'New reply body displayed.');
411

    
412
    // Login using recipient and try to read the message by going to inbox first.
413
    $this->drupalLogin($recipient);
414
    $this->drupalGet('messages');
415

    
416
    // Assert if we see the subject of the messages.
417
    $this->assertText($edit['subject'], 'Sent message subject found.');
418
    $this->assertText($edit2['subject'], 'Sent message subject found.');
419
    $this->assertText($editnobody['subject'], 'Sent message subject found.');
420
    $this->assertText(trim(truncate_utf8(strip_tags($editnosubject['body[value]']), 50, TRUE, TRUE)), 'Sent message subject found.');
421
    $this->assertText($editmixed['subject'], 'Sent message subject found.');
422

    
423
    // Assert that we don't see those that were invalid.
424
    $this->assertNoText($editnone['subject'], 'Invalid message subject not found.');
425
    $this->assertNoText($editinvalid['subject'], 'Invalid message subject not found.');
426

    
427
    // Navigate into the message.
428
    $this->clickLink($edit['subject']);
429
    // Confirm that we can read the message that was sent.
430
    $this->assertText($edit['body[value]'], 'Found message body.');
431
    $this->assertNoText(t('Reply to thread:'), 'Reply form is not displayed.');
432

    
433
    // Verify that the participants information is correct.
434
    $this->assertText(t('Between you and @author', array('@author' => $author->name)));
435

    
436
    // Navigate into the message.
437
    $this->drupalGet('messages');
438
    $this->clickLink($edit2['subject']);
439
    // Confirm that we can read the message that was sent.
440
    $this->assertText($edit2['body[value]'], 'Found message body.');
441
    // Confirm that we can read the reply that was sent.
442
    $this->assertText($reply['body[value]'], 'Found reply body.');
443

    
444
    // Block the author.
445
    user_save($author, array('status' => 0));
446
    $this->drupalLogin($recipient2);
447
    // Navigate into the message.
448
    $this->drupalGet('messages');
449
    $this->clickLink($editauthorblocked['subject']);
450
    // Confirm that the reply form is not shown.
451
    $this->assertNoText(t('Reply'), 'Reply form is not displayed.');
452
    $this->assertText(t('You can not reply to this conversation because all recipients are blocked.'));
453
  }
454

    
455
  /**
456
   * Test functionality around disabling private messaging.
457
   */
458
  function testDisablePrivatemsg() {
459
    $admin_user = $this->drupalCreateUser(array('administer permissions'));
460
    $enableduser = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg')); // set up user with read/write privatemsg permissions
461
    $enableduser2 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg')); // set up user with read/write privatemsg permissions
462
    $disableduser = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'allow disabling privatemsg')); // set up user with read/write privatemsg permissions
463

    
464
    // Create a message between the users that we can use to test
465
    $return = privatemsg_new_thread(array($disableduser), $this->randomName(20), $this->randomName(100), array('author' => $enableduser));
466
    $mid = $return['message']->thread_id;
467

    
468
    $this->drupalLogin($disableduser);
469
    // Now disable $disabledUser.
470
    $this->drupalGet('user/' . $disableduser->uid . '/edit');
471
    $edit['pm_enable'] = FALSE;
472
    $this->drupalPost('user/' . $disableduser->uid . '/edit', $edit, t('Save'));
473

    
474
    // Verify that disableduser can list messages.
475
    $this->drupalGet('messages');
476
    $this->assertResponse(200, t('HTTP Response 200: Access to reading messages page is allowed.'));
477

    
478
    // Verify that $disableduser can read messages but there is not reply form
479
    $this->drupalGet('messages/view/' . $return['message']->thread_id);
480
    $this->assertResponse(200, t('HTTP Response 200: Access to message thread page is allowed.'));
481
    $this->assertNoText(t('Reply to thread'), 'No reply form shown.');
482

    
483
    // Verify that $disableduser cannot send a new message.
484
    $this->drupalGet('messages/new');
485
    $this->assertResponse(403, t('HTTP Response 403: Access to Write New Message page was blocked to user with private messaging disabled'));
486

    
487
    // Use a newly loaded user object to test the API calls.
488
    $disableduser_loaded = user_load($disableduser->uid, TRUE);
489

    
490
    // Check that $disableduser cannot submit a reply
491
    $result = privatemsg_reply($return['message']->thread_id, $this->randomName(100), array('author' => $disableduser_loaded));
492

    
493
    $this->assertFalse($result['success'], 'Message reply was not sent.');
494

    
495
    // Log in as $enableduser and try to send to $disabled user.
496
    // Make sure that a message to multiple recipients still works if one is
497
    // disabled.
498
    $message = array(
499
      'recipient'   => $disableduser->name,
500
      'subject'     => $this->randomName(20),
501
      'body[value]'        => $this->randomName(100),
502
    );
503
    $this->drupalLogin($enableduser);
504
    $this->drupalPost('messages/new', $message, t('Send message'));
505
    $this->assertText(t('You are not allowed to send this message because all recipients are blocked.'));
506

    
507
    // Make sure that a message to multiple recipients still works if one is
508
    // disabled.
509
    $messagemultiple = array(
510
      'recipient'   => $enableduser2->name . ', ' . $disableduser->name,
511
      'subject'     => $this->randomName(20),
512
      'body[value]'        => $this->randomName(100),
513
    );
514
    $this->drupalPost('messages/new', $messagemultiple, t('Send message'));
515
    $this->assertText(t('@recipient has disabled private message receiving.', array('@recipient' => $disableduser->name)), 'Message about user with disabled private messaging.');
516
    $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $enableduser2->name)), 'Message sent confirmation displayed.');
517

    
518
    // Remove the permission to disable privatemsg.
519
    $this->drupalLogin($admin_user);
520
    // 6 is the rid of the custom $disableduser role.
521
    $edit = array('6[allow disabling privatemsg]' => FALSE);
522
    $this->drupalPost('admin/people/permissions', $edit, t('Save permissions'));
523

    
524
    // Make sure that the option is not visible anymore.
525
    $this->drupalLogin($disableduser);
526
    $this->drupalGet('user/' . $disableduser->uid . '/edit');
527
    $this->assertNoText(t('Enable Private Messaging'), t('Disable privatemsg setting not displayed'));
528

    
529
    // Verify that the user is now allowed to write messages again.
530
    $this->drupalGet('messages/new');
531
    $this->assertNoText(t('You are not authorized to access this page.'), t('Access denied page is not displayed.'));
532
    $this->assertText(t('Write new message'), t('Write message form is displayed.'));
533
  }
534

    
535
  /**
536
   * Test correct handling of read all permissions.
537
   */
538
  function testReadAllPermission() {
539
    $author    = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
540
    $recipient = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
541
    $admin     = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'read all private messages'));
542

    
543
    // Create new message.
544
    $edit = array(
545
      'recipient'   => $recipient->name,
546
      'subject'     => $this->randomName(20),
547
      'body[value]'        => $this->randomName(100),
548
    );
549
    $this->drupalLogin($author);
550
    $this->drupalPost('messages/new', $edit, t('Send message'));
551

    
552
    $this->assertText(t('A message has been sent to @recipients.', array('@recipients' => $recipient->name)), t('Message sent confirmation displayed'));
553

    
554
    $this->drupalLogin($admin);
555
    $this->drupalGet('messages/view/1');
556

    
557
    $this->assertText(t('This conversation is being viewed with escalated privileges and may not be the same as shown to normal users.'), t('Notice about read all mode displayed.'));
558

    
559
    // Send a first response.
560
    $admin_edit = array(
561
      'body[value]'        => $this->randomName(100),
562
    );
563
    $this->drupalPost('messages/view/1', $admin_edit, t('Send message'));
564

    
565
    // Make sure that the notice is not displayed anymore.
566
    // @tod: Commented out because this does not work as expected.
567
    $this->assertNoText(t('This conversation is being viewed with escalated privileges and may not be the same as shown to normal users.'), t('Notice about read all mode not displayed.'));
568

    
569
    // Make sure that both the existing message body and the new one are displayed.
570
    $this->assertText($edit['body[value]'], t('First message body displayed.'));
571
    $this->assertText($admin_edit['body[value]'], t('New message body displayed.'));
572

    
573
    $admin_recipient_count = db_query("SELECT COUNT(*) FROM {pm_index} WHERE recipient = :recipient AND thread_id = :thread_id", array(
574
      ':recipient' => $admin->uid,
575
      ':thread_id' => 1,
576
    ))->fetchField();
577
    $this->assertEqual($admin_recipient_count, 2, t('Admin is listed as recipient for every message once.'));
578

    
579

    
580
    // Send a second response.
581
    $admin_edit2 = array(
582
      'body[value]' => $this->randomName(100),
583
    );
584
    $this->drupalPost('messages/view/1', $admin_edit2, t('Send message'));
585

    
586
    // Make sure that both the existing message body and the new one are displayed.
587
    $this->assertText($edit['body[value]'], t('First message body displayed.'));
588
    $this->assertText($admin_edit['body[value]'], t('Second response body displayed.'));
589
    $this->assertText($admin_edit2['body[value]'], t('Third message body displayed.'));
590

    
591
    $admin_recipient_count = db_query("SELECT COUNT(*) FROM {pm_index} WHERE recipient = :recipient AND thread_id = :thread_id", array(
592
      ':recipient' => $admin->uid,
593
      ':thread_id' => 1,
594
    ))->fetchField();
595
    $this->assertEqual($admin_recipient_count, 3, t('Admin is listed as recipient for every message once.'));
596

    
597
  }
598

    
599
  /**
600
   * Tests for the flush feature
601
   */
602
  function testPrivatemsgFlush() {
603
    $author = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
604
    $recipient = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
605

    
606
    // Send 10 messages.
607
    for ($i = 0; $i < 10; $i++) {
608
      privatemsg_new_thread(array($recipient), 'Message #' . $i, 'This is the body', array('author' => $author));
609
    }
610

    
611
    // Delete message 1, 3, 4, 6, 9 for author.
612
    foreach (array(1, 3, 4, 6, 9) as $pmid) {
613
      privatemsg_message_change_delete($pmid, TRUE, $author);
614
    }
615

    
616
    // Delete message 1, 2, 4, 6, 8 for recipient.
617
    foreach (array(1, 3, 4, 6, 9) as $pmid) {
618
      privatemsg_message_change_delete($pmid, TRUE, $recipient);
619
    }
620

    
621
    // Now, mid 1, 4 and 6 have been deleted by both.
622

    
623
    // Flush configuration, enable, delay is default, 30 days
624
    variable_set('privatemsg_flush_enabled', TRUE);
625

    
626
    // Set back the deleted timestamp 35 days back of mid 4.
627
    db_update('pm_index')
628
      ->fields(array('deleted' => REQUEST_TIME - 35 * 86400))
629
      ->condition('mid', 4)
630
      ->execute();
631
    // Set back the deleted timestamp of mid 6, but only 20 days back.
632
    db_update('pm_index')
633
      ->fields(array('deleted' => REQUEST_TIME - 20 * 86400))
634
      ->condition('mid', 6)
635
      ->execute();
636

    
637
    // Run flush.
638
    privatemsg_cron();
639

    
640
    // Check if the undeleted messages are still there.
641
    foreach (array(2, 3, 5, 7, 8, 9, 10) as $pmid) {
642
      $message = privatemsg_message_load($pmid, $author);
643
      $this->assertTrue(!empty($message), t('Undeleted message #%id is still in the system', array('%id' => $pmid)));
644
    }
645

    
646
    // Check if the "recently" deleted  messages are still there.
647
    foreach (array(1, 6) as $pmid) {
648
      $message = privatemsg_message_load($pmid, $author);
649
      $this->assertTrue(!empty($message), t('Deleted message #%id is still in the system', array('%id' => $pmid)));
650
    }
651

    
652
    // Mid 4 should have been flushed.
653
    $message = privatemsg_message_load(4, $author);
654
    $this->assertTrue(empty($message), t('Message #4 has been flushed'));
655
  }
656

    
657
  function testDelete() {
658
    // Create users.
659
    $author = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'delete privatemsg'));
660
    $recipient = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'delete privatemsg'));
661
    $recipient2 = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'cancel account'));
662
    $admin = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'delete privatemsg', 'read all private messages'));
663
    $dummy = $this->drupalCreateUser(array('cancel account'));
664

    
665
    // Create texts.
666
    $subject = $this->randomName(20);
667
    $body1 = $this->randomName(100);
668
    $body2 = $this->randomName(100);
669

    
670
    // Create message and response.
671
    $return = privatemsg_new_thread(array($recipient, $recipient2), $subject, $body1, array('author' => $author));
672
    privatemsg_reply($return['message']->thread_id, $body2, array('author' => $recipient));
673

    
674
    // Check with user without delete permission.
675
    $this->drupalLogin($recipient2);
676
    $this->drupalGet('messages/view/' . $return['message']->thread_id);
677
    $this->assertText($subject, 'Subject is displayed');
678
    $this->assertText($body1, 'First message is displayed');
679
    $this->assertText($body2, 'Second message is displayed');
680
    $this->assertNoLink(t('Delete'), 'Delete message is link is not displayed for user without permission');
681

    
682
    // Check if access for that user is denied.
683
    $this->drupalGet('messages/delete/' . $return['message']->thread_id . '/' . $return['message']->mid);
684
    $this->assertText(t('Access denied'));
685

    
686
    // Check with user with delete access.
687
    $this->drupalLogin($recipient);
688
    $this->drupalGet('messages/view/' . $return['message']->thread_id);
689
    $this->assertText(t('Delete'), 'Delete message is link is displayed for user without permission');
690

    
691
    // Click delete link of the second message and cancel.
692
    $this->clickLink(t('Delete'), 1);
693
    $this->assertText(t('Are you sure you want to delete this message?'), 'Confirmation message displayed');
694
    $this->clickLink(t('Cancel'));
695
    $this->assertText($body2, 'Second message is still displayed');
696

    
697
    // Confirm message deletion.
698
    $this->clickLink(t('Delete'), 1);
699
    $this->assertText(t('Are you sure you want to delete this message?'), 'Confirmation message displayed');
700
    $this->drupalPost(NULL, array(), t('Delete'));
701
    $this->assertText(t('Message has been deleted.'), 'Message has been deleted');
702
    $this->assertText($body1, 'First message is still displayed');
703
    $this->assertNoText($body2, 'Second message was deleted');
704

    
705
    // Click delete link of the first message and cancel.
706
    $this->clickLink(t('Delete'));
707
    $this->assertText(t('Are you sure you want to delete this message?'), 'Confirmation message displayed');
708
    $this->clickLink(t('Cancel'));
709
    $this->assertText($body1, 'First message is still displayed');
710

    
711
    // Confirm message deletion.
712
    $this->clickLink(t('Delete'));
713
    $this->assertText(t('Are you sure you want to delete this message?'), 'Confirmation message displayed');
714
    $this->drupalPost(NULL, array(), t('Delete'));
715
    $this->assertText(t('Message has been deleted.'), 'Message deleted has been deleted');
716
    $this->assertNoText($subject, 'All messages of that thread have been deleted');
717

    
718
    // Test if the message has not been deleted for other users.
719
    $this->drupalLogin($recipient2);
720
    $this->drupalGet('messages/view/' . $return['message']->thread_id);
721
    $this->assertText($body1, 'First message is still displayed');
722
    $this->assertText($body2, 'First message is still displayed');
723

    
724
    // Test delete all checkbox.
725
    $this->drupalLogin($admin);
726
    $this->drupalGet('messages/view/' . $return['message']->thread_id);
727
    $this->clickLink(t('Delete'), 1);
728
    $this->drupalPost(NULL, array('delete_options' => TRUE), t('Delete'));
729
    $this->assertText(t('Message has been deleted for all users.'), 'Message deleted has been deleted');
730

    
731
    // Test if the message has been deleted for all users.
732
    $this->drupalLogin($recipient2);
733
    $this->drupalGet('messages/view/' . $return['message']->thread_id);
734
    $this->assertText($body1, 'First message is still displayed');
735
    $this->assertNoText($body2, 'Second message has been deleted for all users');
736

    
737
    // Check that messages of canceled users (user_cancel_delete) are deleted too.
738
    $edit = array('body[value]' => $this->randomName(100));
739
    $this->drupalPost(NULL, $edit, t('Send message'));
740
    $this->drupalGet('messages/view/' . $return['message']->thread_id);
741
    $this->assertText($edit['body[value]'], t('New reply is displayed'));
742

    
743
    // Reload user object, don't use the cache.
744
    $recipient2 = user_load($recipient2->uid, TRUE);
745

    
746
    // Attempt to cancel account.
747
    variable_set('user_cancel_method', 'user_cancel_delete');
748
    $this->drupalGet('user/' . $recipient2->uid . '/edit');
749
    $this->drupalPost(NULL, NULL, t('Cancel account'));
750

    
751
    // Confirm account cancellation.
752
    // Explicitly use time(), REQUEST_TIME is not exact enough.
753
    $timestamp = time();
754
    $this->drupalPost(NULL, NULL, t('Cancel account'));
755

    
756
    // Confirm account cancellation request.
757
    $this->drupalGet("user/$recipient2->uid/cancel/confirm/$timestamp/" . user_pass_rehash($recipient2->pass, $timestamp, $recipient2->login));
758

    
759
    // Simpletest thinks that we are still logged in.
760
    $this->loggedInUser = NULL;
761

    
762
    $this->drupalLogin($admin);
763
    $this->drupalGet('messages/view/' . $return['message']->thread_id);
764
    $this->assertText($body1, 'First message is still displayed');
765
    $this->assertNoText($edit['body[value]'], t('Reply of deleted user is not displayed anymore'));
766

    
767
    // Test if admin is allowed to delete messages of other users.
768
    $this->drupalGet('user/' . $author->uid . '/messages');
769
    $this->checkThreadDelete($return['message']);
770

    
771
    // Check if user is allowed to delete messages.
772
    $this->drupalLogin($author);
773
    $this->drupalGet('messages');
774
    $this->checkThreadDelete($return['message']);
775

    
776
    // Check that deleting users that didn't send any message does not cause
777
    // errors.
778
    $this->drupalLogin($dummy);
779

    
780
    // Reload user object, don't use the cache.
781
    $dummy = user_load($dummy->uid, TRUE);
782

    
783
    // Attempt to cancel account.
784
    $this->drupalGet('user/' . $dummy->uid . '/edit');
785
    $this->drupalPost(NULL, NULL, t('Cancel account'));
786

    
787
    // Confirm account cancellation.
788
    // Explicitly use time(), REQUEST_TIME is not exact enough.
789
    $timestamp = time();
790
    $this->drupalPost(NULL, NULL, t('Cancel account'));
791

    
792
    // Confirm account cancellation request.
793
    $this->drupalGet("user/$dummy->uid/cancel/confirm/$timestamp/" . user_pass_rehash($dummy->pass, $timestamp, $dummy->login));
794
  }
795

    
796
  function checkThreadDelete($message) {
797
    $this->assertText($message->subject, t('Message is displayed.'));
798

    
799
    $delete = array(
800
      'list[' . $message->thread_id . ']' => 1,
801
    );
802
    $this->drupalPost(NULL, $delete, t('Delete'));
803
    $this->assertText(t('Deleted @count thread.', array('@count' => 1)), t('Delete message displayed.'));
804
    $this->assertNoText($message->subject, t('Message is not displayed anymore.'));
805
    $this->assertText(t('No messages available.'), t('No messages available anymore.'));
806

    
807
    // Revert delete action.
808
    $this->clickLink(t('undone'));
809

    
810
    $this->assertText(t('Restored @count thread.', array('@count' => 1)), t('Restore message displayed'));
811
    $this->assertText($message->subject, t('Message is displayed again.'));
812
    $this->assertNoText(t('No messages available.'), t('Messages are available.'));
813
  }
814

    
815
  /**
816
   * Test preview functionality.
817
   */
818
  function testPreview() {
819
    $user = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
820

    
821
    // Enable preview button.
822
    variable_set('privatemsg_display_preview_button', TRUE);
823

    
824
    $message = array(
825
      'recipient' => $user->name,
826
      'subject' => $this->randomName(),
827
      'body[value]' => $this->randomName(50),
828
    );
829
    $this->drupalLogin($user);
830

    
831
    // Preview message.
832
    $this->drupalPost('messages/new', $message, t('Preview message'));
833
    $this->assertTitle(t('Write new message to @user', array('@user' => $user->name)) . ' | Drupal', t('Correct title is displayed.'));
834
    $this->assertFieldByXPath($this->buildXPathQuery('//div[@class=:class]/p', array(':class' => 'privatemsg-message-body')), $message['body[value]'], t('Message body is previewed'));
835
    $this->assertFieldByName('body[value]', $message['body[value]'], t('Message body field has the correct default value.'));
836

    
837
    // Send message.
838
    $this->drupalPost(NULL, array(), t('Send message'));
839
    $this->assertText($message['subject'], t('Message subject is displayed.'));
840
    $this->assertText($message['body[value]'], t('Message body is displayed.'));
841

    
842
    $this->assertText(t('A message has been sent to @recipient.', array('@recipient' => $user->name)), t('Sent confirmation displayed.'));
843

    
844
  }
845

    
846
  /**
847
   * Test autocomplete.
848
   */
849
  function testAutocomplete() {
850
    $current = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
851
    $user1 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
852
    $user2 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
853
    $user3 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
854

    
855
    $this->drupalLogin($current);
856

    
857
    // Use specific names to be able to test for specific name combinations.
858
    user_save($current, array('name' => 'wathever'));
859
    user_save($user1, array('name' => 'aaaa'));
860
    user_save($user2, array('name' => 'aaab'));
861
    user_save($user3, array('name' => 'bbbb'));
862

    
863
    $json = $this->drupalGet('messages/autocomplete/aa');
864
    $autocomplete = (array)json_decode($json);
865
    $this->assertEqual(count($autocomplete), 2, t('Autocomplete object contains two suggestions.'));
866
    $this->assertEqual($autocomplete['aaaa, '], 'aaaa');
867
    $this->assertEqual($autocomplete['aaab, '], 'aaab');
868

    
869
    $json = $this->drupalGet('messages/autocomplete/bb');
870
    $autocomplete = (array)json_decode($json);
871
    $this->assertEqual(count($autocomplete), 1, t('Autocomplete object contains one suggestion.'));
872
    $this->assertEqual($autocomplete['bbbb, '], 'bbbb');
873

    
874
    $json = $this->drupalGet('messages/autocomplete/cc');
875
    $autocomplete = (array)json_decode($json);
876
    $this->assertEqual(count($autocomplete), 0, t('Autocomplete object contains no suggestions.'));
877

    
878
    $json = $this->drupalGet('messages/autocomplete/aaaa, a');
879
    $autocomplete = (array)json_decode($json);
880
    $this->assertEqual(count($autocomplete), 1, t('Autocomplete object contains one suggestion.'));
881
    $this->assertEqual($autocomplete['aaaa, aaab, '], 'aaab');
882

    
883
    // Test XSS protection, create a username and check that the suggestion is
884
    // safe.
885
    $user4 = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
886
    $user4 = user_save($user4, array('name' => "<script>alert('XSS')</script>"));
887

    
888
    $json = $this->drupalGet('messages/autocomplete/<sc');
889
    $autocomplete = (array)json_decode($json);
890
    $this->assertEqual(count($autocomplete), 1, t('Autocomplete object contains one suggestion.'));
891
    $this->assertEqual($autocomplete[strip_tags($user4->name) . ', '], strip_tags($user4->name));
892
  }
893

    
894
  /**
895
   * Implements tearDown().
896
   */
897
  function tearDown() {
898
    //we dont really need to do this. i'm adding it just to keep it in front of my eyes so i can memorize it.
899
    parent::tearDown();
900
  }
901
}
902

    
903
/**
904
 * Tests for fields integration.
905
 */
906
class PrivatemsgFieldsTestCase extends PrivatemsgBaseTestCase {
907
  /**
908
   * Implements getInfo().
909
   */
910
  public static function getInfo() {
911
    return array
912
    (
913
      'name' => t('Privatemsg fields.'),
914
      'description' => t('Tests integration with fields.'),
915
      'group' => t('Privatemsg'),
916
    );
917
  }
918

    
919
  /**
920
   * Implements setUp().
921
   */
922
  function setUp() {
923
    parent::setUp('privatemsg', 'image', 'text', 'field_ui');
924
  }
925

    
926
  function testSingleField() {
927
    $admin = $this->drupalCreateUser(array('administer privatemsg settings', 'write privatemsg', 'read privatemsg'));
928
    $user = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
929

    
930
    $this->drupalLogin($admin);
931

    
932
    // Create a new field.
933
    $edit = array(
934
      'fields[_add_new_field][label]' => $label = $this->randomName(),
935
      'fields[_add_new_field][field_name]' => $name = strtolower($this->randomName()),
936
      'fields[_add_new_field][type]' => 'text',
937
      'fields[_add_new_field][widget_type]' => 'text_textfield',
938
    );
939
    $this->drupalPost('admin/config/messaging/privatemsg/fields', $edit, t('Save'));
940
    $this->drupalPost(NULL, array(), t('Save field settings'));
941
    $this->drupalPost(NULL, array(), t('Save settings'));
942

    
943
    // Enable preview button.
944
    variable_set('privatemsg_display_preview_button', TRUE);
945

    
946
    // Preview message.
947
    $message = array(
948
      'recipient' => $user->name,
949
      'subject' => $this->randomName(),
950
      'body[value]'    => $this->randomName(50),
951
      'field_' . $name . '[und][0][value]' => $this->randomName(50),
952
    );
953
    $this->drupalPost('messages/new', $message, t('Preview message'));
954

    
955
    // Send message.
956
    $this->drupalPost(NULL, array(), t('Send message'));
957

    
958
    // Check message.
959
    $this->drupalLogin($user);
960
    $this->drupalGet('messages');
961
    $this->clickLink($message['subject']);
962

    
963
    $this->assertText($message['body[value]'], t('Message body displayed.'));
964
    $this->assertText($message['field_' . $name . '[und][0][value]'], t('Content of new field is displayed.'));
965

    
966
    // Respond.
967
    $response = array(
968
      'body[value]' => $this->randomName(50),
969
      'field_' . $name . '[und][0][value]' => $this->randomName(50),
970
    );
971
    $this->drupalPost(NULL, $response, t('Send message'));
972

    
973
    // Check response.
974
    $this->drupalLogin($admin);
975
    $this->drupalGet('messages');
976
    $this->clickLink($message['subject']);
977

    
978
    $this->assertText($response['body[value]'], t('Message body displayed.'));
979
    $this->assertText($response['field_' . $name . '[und][0][value]'], t('Content of new field is displayed.'));
980
  }
981

    
982
  /**
983
   * Verify that a message with an empty image can be sent.
984
   */
985
  function testEmptyImage() {
986
    $admin = $this->drupalCreateUser(array('administer privatemsg settings', 'write privatemsg', 'read privatemsg'));
987
    $user = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
988

    
989
    $this->drupalLogin($admin);
990

    
991
    // Create a new field.
992
    $edit = array(
993
      'fields[_add_new_field][label]' => $label = $this->randomName(),
994
      'fields[_add_new_field][field_name]' => $name = strtolower($this->randomName()),
995
      'fields[_add_new_field][type]' => 'image',
996
      'fields[_add_new_field][widget_type]' => 'image_image',
997
    );
998
    $this->drupalPost('admin/config/messaging/privatemsg/fields', $edit, t('Save'));
999
    $this->drupalPost(NULL, array(), t('Save field settings'));
1000
    $this->drupalPost(NULL, array(), t('Save settings'));
1001

    
1002
    // Enable preview button.
1003
    variable_set('privatemsg_display_preview_button', TRUE);
1004

    
1005
    // Preview message.
1006
    $message = array(
1007
      'recipient' => $user->name,
1008
      'subject' => $this->randomName(),
1009
      'body[value]'    => $this->randomName(50),
1010
    );
1011
    $this->drupalPost('messages/new', $message, t('Preview message'));
1012

    
1013
    // Send message.
1014
    $this->drupalPost(NULL, array(), t('Send message'));
1015

    
1016
    // Check message.
1017
    $this->drupalLogin($user);
1018
    $this->drupalGet('messages');
1019
    $this->clickLink($message['subject']);
1020

    
1021
    $this->assertText($message['body[value]'], t('Message body displayed.'));
1022
  }
1023
}
1024

    
1025
/**
1026
 * Tests for fields integration.
1027
 */
1028
class PrivatemsgLinksTestCase extends PrivatemsgBaseTestCase {
1029

    
1030
  /**
1031
   * Use testing profile.
1032
   */
1033
  protected $profile = 'standard';
1034

    
1035
  /**
1036
   * Implements of getInfo().
1037
   */
1038
  public static function getInfo() {
1039
    return array
1040
    (
1041
      'name' => t('Privatemsg links'),
1042
      'description' => t('Tests links displayed in nodes, profiles and blocks.'),
1043
      'group' => t('Privatemsg'),
1044
    );
1045
  }
1046

    
1047
  /**
1048
   * Implements setUp().
1049
   */
1050
  function setUp() {
1051
    parent::setUp('privatemsg');
1052
  }
1053

    
1054
  /**
1055
   * Tests author links displayed on nodes and comments.
1056
   */
1057
  function testAuthorLinks() {
1058
    $admin = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'administer privatemsg settings', 'create article content', 'create page content'));
1059
    $user = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
1060

    
1061
    $this->drupalLogin($admin);
1062

    
1063
    $settings = array(
1064
      'privatemsg_display_link_self' => FALSE,
1065
      'privatemsg_link_node_types[article]' => TRUE,
1066
      'privatemsg_display_on_teaser' => FALSE,
1067
      'privatemsg_display_on_comments' => TRUE,
1068
    );
1069
    $this->drupalPost('admin/config/messaging/privatemsg', $settings, t('Save configuration'));
1070

    
1071
    $node1 = array(
1072
      'title' => $this->randomName(10),
1073
      'body[und][0][value]' => $this->randomString(50),
1074
    );
1075
    $this->drupalPost('node/add/article', $node1, t('Save'));
1076

    
1077
    $comment = array(
1078
      'comment_body[und][0][value]' => $this->randomName(20),
1079
    );
1080
    $this->drupalPost(NULL, $comment, t('Save'));
1081

    
1082
    $node2 = array(
1083
      'title' => $this->randomName(),
1084
      'body[und][0][value]' => $this->randomString(50),
1085
    );
1086
    $this->drupalPost('node/add/page', $node2, t('Save'));
1087

    
1088
    $this->drupalGet('node');
1089
    $this->assertNoText(t('Send author a message'), t('Send author a message link not displayed.'));
1090
    $this->clickLink($node1['title']);
1091
    $this->assertNoText(t('Send author a message'), t('Send author a message link not displayed.'));
1092

    
1093
    $this->drupalLogin($user);
1094
    $this->drupalGet('node');
1095
    $this->assertNoText(t('Send author a message'), t('Send author a message link not displayed.'));
1096
    $this->clickLink($node1['title']);
1097
    $this->assertNoUniqueText(t('Send author a message'), t('Send author a message link displayed.'));
1098

    
1099
    $this->clickLink(t('Send author a message'));
1100
    // To field and subject should be correctly pre-filled now.
1101
    $this->drupalPost(NULL, array(), t('Send message'));
1102

    
1103
    // Make sure the message was sent to the correct user.
1104
    $this->assertText(t('A message has been sent to @user', array('@user' => $admin->name)));
1105

    
1106
    // @todo: Do not guess nid.
1107
    $this->drupalGet('node/2');
1108
    $this->assertNoText(t('Send author a message'), t('Send author a message link not displayed.'));
1109

    
1110
    $this->drupalLogin($admin);
1111
    $this->drupalGet('messages');
1112
    $this->assertText(t('Message regarding @node', array('@node' => $node1['title'])));
1113
  }
1114

    
1115
  /**
1116
   * Tests menu block.
1117
   */
1118
  function testMenuBlock() {
1119
    $admin = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'administer privatemsg settings', 'administer blocks', 'administer menu'));
1120
    $user = $this->drupalCreateUser(array('read privatemsg'));
1121

    
1122
    // Enable block.
1123
    $this->drupalLogin($admin);
1124
    $blocks = array(
1125
      'blocks[privatemsg_privatemsg-menu][region]' => 'sidebar_second',
1126
    );
1127
    $this->drupalPost('admin/structure/block', $blocks, t('Save blocks'));
1128

    
1129
    // Disable secondary menu.
1130
    /* @todo: Not yet possible because simpletest needs a log out link to verify that the user is logged in.
1131
    $menu_settings = array(
1132
      'menu_secondary_links_source' => '',
1133
    );
1134
    $this->drupalPost('admin/structure/menu/settings', $menu_settings, t('Save configuration'));
1135
     *
1136
     */
1137

    
1138
    $this->drupalGet('');
1139
    $this->assertText(t('Private messages'), t('Privatemsg menu block title displayed.'));
1140
    $this->assertText('Write new message', t('Write new message link displayed.'));
1141

    
1142
    $message = array(
1143
      'recipient' => $user->name,
1144
      'subject' => $this->randomName(),
1145
      'body[value]' => $this->randomName(50),
1146
    );
1147
    $this->drupalPost('messages/new', $message, t('Send message'));
1148

    
1149
    $this->drupalLogin($user);
1150
    $this->assertNoText(t('Write new message'), t('Write new message link not displayed.'));
1151
    $this->assertNoUniqueText(t('Messages (1 new)'), t('Messages link including new message information displayed'));
1152
  }
1153

    
1154
  /**
1155
   * Tests menu block.
1156
   */
1157
  function testNewBlock() {
1158
    $admin = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg', 'administer privatemsg settings', 'administer blocks', 'administer menu'));
1159
    $user = $this->drupalCreateUser(array('read privatemsg'));
1160

    
1161
    // Enable block.
1162
    $this->drupalLogin($admin);
1163
    $blocks = array(
1164
      'blocks[privatemsg_privatemsg-new][region]' => 'sidebar_second',
1165
    );
1166
    $this->drupalPost('admin/structure/block', $blocks, t('Save blocks'));
1167

    
1168
    $this->drupalGet('');
1169
    $this->assertNoText(t('New message'), t('Privatemsg new block title not displayed.'));
1170

    
1171
    $message = array(
1172
      'recipient' => $user->name,
1173
      'subject' => $this->randomName(),
1174
      'body[value]' => $this->randomName(50),
1175
    );
1176
    $this->drupalPost('messages/new', $message, t('Send message'));
1177

    
1178
    $this->drupalLogin($user);
1179
    $this->assertText(t('New message'), t('Privatemsg new block title displayed.'));
1180

    
1181
    $this->assertText(t('You have a new message! Click here to read it.'), t('New message indication displayed.'));
1182
  }
1183
}
1184

    
1185
/**
1186
 * Tests for fields integration.
1187
 */
1188
class PrivatemsgTokenTestCase extends PrivatemsgBaseTestCase {
1189
  /**
1190
   * Implements of getInfo().
1191
   */
1192
  public static function getInfo() {
1193
    return array
1194
    (
1195
      'name' => t('Privatemsg tokens'),
1196
      'description' => t('Tests tokens in private messages.'),
1197
      'group' => t('Privatemsg'),
1198
      'dependencies' => array('token'),
1199
    );
1200
  }
1201

    
1202
  /**
1203
   * Implements setUp().
1204
   */
1205
  function setUp() {
1206
    parent::setUp('privatemsg', 'token');
1207
  }
1208

    
1209
  /**
1210
   * Basic token functionality.
1211
   */
1212
  function testBasicTokens() {
1213
    $author = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'use tokens in privatemsg'));
1214
    $recipient1 = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
1215
    $recipient2 = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
1216

    
1217
    $this->drupalLogin($author);
1218

    
1219
    // Enable preview button.
1220
    variable_set('privatemsg_display_preview_button', TRUE);
1221

    
1222
    // Create message with tokens and preview it.
1223
    $edit = array(
1224
      'recipient'   => $recipient1->name . ', ' . $recipient2->name,
1225
      'subject'     => 'Hey [privatemsg_message:recipient:name]',
1226
      'body[value]' => "Testing a few tokens.\n\nSubject: [privatemsg_message:subject]\n\nAuthor: [privatemsg_message:author:name]\n\nInvalid token: [privatemsg_message:invalid_token]",
1227
    );
1228
    $this->drupalPost('messages/new', $edit, t('Preview message'));
1229

    
1230
    // Make sure the tokens in subject and body are correctly displayed in the
1231
    // preview.
1232
    $this->assertText('Hey &lt; Token [privatemsg_message:recipient:name] &gt;', t('Token is displayed in subject.'));
1233
    $this->assertText('Subject: &lt; Token [privatemsg_message:subject] &gt;', t('Subject token is displayed.'));
1234
    $this->assertText('Author: &lt; Token [privatemsg_message:author:name] &gt;', t('Author token is displayed.'));
1235
    $this->assertText('Invalid token: &lt; INVALID TOKEN [privatemsg_message:invalid_token] &gt;', t('Invalid token is displayed.'));
1236
    $this->assertText(t('Note: Valid tokens will be replaced when a recipient is reading this message.'), t('Token notice is displayed.'));
1237

    
1238
    $this->drupalPost(NULL, array(), t('Send message'));
1239

    
1240
    // Make sure the tokens in subject and body are correctly displayed in the
1241
    // sent message.
1242
    $this->assertText('Hey &lt; Token [privatemsg_message:recipient:name] &gt;', t('Token is displayed in subject.'));
1243
    $this->assertText('Subject: &lt; Token [privatemsg_message:subject] &gt;', t('Subject token is displayed.'));
1244
    $this->assertText('Author: &lt; Token [privatemsg_message:author:name] &gt;', t('Author token is displayed.'));
1245
    $this->assertText('Invalid token: &lt; INVALID TOKEN [privatemsg_message:invalid_token] &gt;', t('Invalid token is displayed.'));
1246
    $this->assertText(t('Note: Valid tokens will be replaced when a recipient is reading this message.'), t('Token notice is displayed.'));
1247

    
1248
    // Log in as the first recipient, verify that he sees the message with the
1249
    // replacements.
1250
    $this->drupalLogin($recipient1);
1251
    $this->drupalGet('messages');
1252
    $this->clickLink('Hey ' . $recipient1->name);
1253

    
1254
    // Make sure the tokens in subject and body are correctly displayed in the
1255
    // message.
1256
    $this->assertText('Hey ' . $recipient1->name, t('Replaced token is displayed in subject.'));
1257
    $this->assertText('Subject: Hey ' . $recipient1->name, t('Subject token is replaced.'));
1258
    $this->assertText('Author: ' . $author->name, t('Author token is replaced.'));
1259
    $this->assertText('Invalid token: [privatemsg_message:invalid_token]', t('Invalid token is displayed.'));
1260
    $this->assertNoText(t('Note: Valid tokens will be replaced when a recipient is reading this message.'), t('Token notice is displayed.'));
1261

    
1262
    // Reply, verify that the message still shows the correct subject and
1263
    // tokens from this user are not replaced.
1264
    $reply = array(
1265
      'body[value]' => 'Response with a token [privatemsg_message:mid]',
1266
    );
1267
    $this->drupalPost(NULL, $reply, t('Send message'));
1268

    
1269
    $this->drupalLogin($recipient2);
1270
    $this->drupalGet('messages');
1271
    $this->clickLink('Hey ' . $recipient2->name);
1272

    
1273
    // Make sure the tokens in subject and body are correctly displayed in the
1274
    // message.
1275
    $this->assertText('Hey ' . $recipient2->name, t('Replaced token is displayed in subject.'));
1276
    $this->assertText('Subject: Hey ' . $recipient2->name, t('Subject token is replaced.'));
1277
    $this->assertText('Author: ' . $author->name, t('Author token is replaced.'));
1278
    $this->assertText('Invalid token: [privatemsg_message:invalid_token]', t('Invalid token is displayed.'));
1279
    $this->assertText('Response with a token [privatemsg_message:mid]', t('Token from recipient is not replaced.'));
1280
    $this->assertNoText(t('Note: Valid tokens will be replaced when a recipient is reading this message.'), t('Token notice is displayed.'));
1281

    
1282
    $this->drupalLogin($author);
1283
    $this->drupalGet('messages');
1284

    
1285
    // Assert that token is displayed in the subject for the author.
1286
    $this->assertText('Hey &lt; Token [privatemsg_message:recipient:name] &gt;', t('Token is displayed correctly in subject for author.'));
1287
  }
1288
}
1289

    
1290
/**
1291
 * Privatemsg API tests
1292
 */
1293
class PrivatemsgAPITestCase extends PrivatemsgBaseTestCase {
1294
  /**
1295
   * Implements getInfo().
1296
   */
1297
  public static function getInfo() {
1298
    return array(
1299
      // 'name' should start with what is being tested (menu item) followed by what about it
1300
      // is being tested (creation/deletion).
1301
      'name' => t('Privatemsg API functionality.'),
1302
      // 'description' should be one or more complete sentences that provide more details on what
1303
      // exactly is being tested.
1304
      'description' => t('Test sending, receiving, listing, deleting messages and other features via API.'),
1305
      // 'group' should be a logical grouping of test cases, like a category.  In most cases, that
1306
      // is the module the test case is for.
1307
      'group' => t('Privatemsg'),
1308
    );
1309
  }
1310

    
1311
  /**
1312
   * Implements setUp().
1313
   */
1314
  function setUp() {
1315
    parent::setUp('privatemsg');
1316

    
1317
    // Create the full html format.
1318
    $full_html_format = array(
1319
      'format' => 'full_html',
1320
      'name' => 'Full HTML',
1321
      'weight' => 1,
1322
      'filters' => array(
1323
        // URL filter.
1324
        'filter_url' => array(
1325
          'weight' => 0,
1326
          'status' => 1,
1327
        ),
1328
        // Line break filter.
1329
        'filter_autop' => array(
1330
          'weight' => 1,
1331
          'status' => 1,
1332
        ),
1333
        // HTML corrector filter.
1334
        'filter_htmlcorrector' => array(
1335
          'weight' => 10,
1336
          'status' => 1,
1337
        ),
1338
      ),
1339
    );
1340
    $full_html_format = (object) $full_html_format;
1341
    filter_format_save($full_html_format);
1342
    // Refresh permissions.
1343
    $this->checkPermissions(array(), TRUE);
1344
  }
1345

    
1346
  function testPrivatemsgApiNewThread() {
1347
    $author     = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
1348
    $recipient1  = $this->drupalCreateUser(array('read privatemsg'));
1349
    $recipient2  = $this->drupalCreateUser(array('read privatemsg'));
1350
    $recipient3  = $this->drupalCreateUser(array('read privatemsg'));
1351

    
1352
    // Reset user_access cache
1353
    user_access('', $author, TRUE);
1354

    
1355
    $resultok1 = privatemsg_new_thread(array($recipient1, $recipient2, $recipient3), 'normal message', 'Body text', array('author' => $author));
1356
    $this->assertTrue($resultok1['success'], 'Private message could be sent successfully');
1357

    
1358
    $message = $this->getMessageFromSubject('normal message');
1359
    $this->assertFalse(empty($message), 'Message was saved in database');
1360
    $this->assertEqual($message->author, $author->uid, 'Message was sent by author');
1361

    
1362
    $resultok2 = privatemsg_new_thread(array($recipient1, $recipient2, $recipient3), 'empty body', '', array('author' => $author));
1363
    $this->assertTrue($resultok2['success'], 'API allowed to send message without body');
1364

    
1365
    $resultf1 = privatemsg_new_thread(array($recipient1, $recipient2, $recipient3), '', 'No subject', array('author' => $author));
1366
    $this->assertEqual('A subject must be included with the message.', $resultf1['messages']['error'][0], 'API denied to send message without a subject');
1367

    
1368
    $resultf2 = privatemsg_new_thread(array(), 'no recipients', 'Body text', array('author' => $author));
1369
    $this->assertEqual('At least one valid recipient must be included with the message.', $resultf2['messages']['error'][0], 'API denied to send message without recipients');
1370
    $message = $this->getMessageFromSubject('no recipients');
1371
    $this->assertTrue(empty($message), 'Message was not saved in database');
1372

    
1373
    $resultf3 = privatemsg_new_thread(array($recipient1, $recipient2, $recipient3), 'not allowed', 'Body text', array('author' => $recipient1));
1374
    $errormessage = t('@user is not allowed to write messages.', array('@user' => $recipient1->name));
1375
    $this->assertEqual($errormessage, $resultf3['messages']['error'][0], 'API denied to send message from user without permission');
1376
    $message = $this->getMessageFromSubject('not allowed');
1377
    $this->assertTrue(empty($message), 'Message was not saved in database');
1378

    
1379
    // Test with an input format that the author is not allowed to use.
1380
    $resultf4 = privatemsg_new_thread(array($recipient1, $recipient2, $recipient3), 'input filter not allowed', 'Body text', array('author' => $author, 'format' => 'full_html'));
1381
    $errormessage = t('@user is not allowed to use the specified input format.', array('@user' => $author->name));
1382
    $this->assertEqual($errormessage, $resultf4['messages']['error'][0], t('User is not allowed to use the specified input format.'));
1383
    $message = $this->getMessageFromSubject('input filter not allowed');
1384
    $this->assertTrue(empty($message), 'Message was not saved in database');
1385

    
1386
    // Send a message through the api to the same user and check if it marked
1387
    // as new.
1388
    privatemsg_new_thread(array($author), $subject = $this->randomName(10), $this->randomString(20), array('author' => $author));
1389
    $this->drupalLogin($author);
1390
    $this->drupalGet('messages');
1391
    $this->clickLink($subject);
1392
    $this->assertText(t('New'), t('Message is marked as new'));
1393
  }
1394

    
1395
  function getMessageFromSubject($subject) {
1396
    return db_query("SELECT * FROM {pm_message} WHERE subject = :subject", array(':subject' => $subject))->fetchObject();
1397
  }
1398

    
1399
  function testPrivatemsgApiReply() {
1400
    $author     = $this->drupalCreateUser(array('write privatemsg'));
1401
    $recipient1  = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
1402
    $recipient2  = $this->drupalCreateUser(array('read privatemsg', 'write privatemsg'));
1403
    $recipient3  = $this->drupalCreateUser(array('read privatemsg'));
1404

    
1405
    // Reset user_access cache
1406
    user_access('', $author, TRUE);
1407

    
1408
    $resultok = privatemsg_new_thread(array($recipient2, $recipient1, $recipient3), 'test reply', 'body text', array('author' => $author));
1409
    $this->assertTrue($resultok['success'], 'Private message could be sent successfully');
1410

    
1411
    $thread_row = $this->getMessageFromSubject('test reply');
1412

    
1413
    $resultok = privatemsg_reply($thread_row->mid, 'Test Body', array('author' => $author));
1414
    $this->assertTrue($resultok['success'], 'Reply could be sent successfully');
1415

    
1416
    $resultok = privatemsg_reply($thread_row->mid, 'Test Body', array('author' => $recipient1));
1417
    $this->assertTrue($resultok['success'], 'Reply could be sent successfully');
1418

    
1419
    $resultf1 = privatemsg_reply($thread_row->mid, '', array('author' => $recipient2));
1420
    $this->assertFalse($resultf1['success'], 'API denied to send message without body.');
1421
    $this->assertEqual($resultf1['messages']['error'][0], t('A message must be included in your reply.'), 'Correct error returned when replying with an empty body.');
1422

    
1423
    $resultf2 = privatemsg_reply($thread_row->mid, 'Test Body', array('author' => $recipient3));
1424
    $errormessage = t('@user is not allowed to write messages.', array('@user' => $recipient3->name));
1425
    $this->assertEqual($errormessage, $resultf2['messages']['error'][0], 'API denied to send message from user without permission');
1426

    
1427
  }
1428

    
1429
  /**
1430
   * Test various use cases for privatemsg_get_link().
1431
   */
1432
  function testGetLink() {
1433
    $author      = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg'));
1434
    $recipient1  = $this->drupalCreateUser(array('read privatemsg'));
1435
    $recipient2  = $this->drupalCreateUser(array('read privatemsg'));
1436
    $recipient3  = $this->drupalCreateUser(array('read privatemsg', 'allow disabling privatemsg'));
1437
    $recipient4  = $this->drupalCreateUser();
1438

    
1439
    $this->drupalLogin($author);
1440

    
1441
    $this->drupalGet(privatemsg_get_link(array($recipient1)));
1442
    $this->assertFieldByName('recipient', $recipient1->name . ' [user]');
1443

    
1444
    $this->drupalGet(privatemsg_get_link(array($recipient1, $recipient2)));
1445
    $this->assertFieldByName('recipient', $recipient1->name . ' [user], ' . $recipient2->name . ' [user]');
1446

    
1447
    $this->drupalGet(privatemsg_get_link(array($recipient1, $recipient2), $author));
1448
    $this->assertFieldByName('recipient', $recipient1->name . ' [user], ' . $recipient2->name . ' [user]');
1449

    
1450
    $this->drupalGet(privatemsg_get_link(array($recipient1, $recipient2), $author, $subject = 'Str/"ang\\w3//'));
1451
    $this->assertFieldByName('recipient', $recipient1->name . ' [user], ' . $recipient2->name . ' [user]');
1452
    $this->assertFieldByName('subject', $subject);
1453

    
1454
    // Disable privatemsg for recipient 3.
1455
    db_insert('pm_disable')
1456
      ->fields(array(
1457
        'uid' => $recipient3->uid,
1458
      ))
1459
      ->execute();
1460
    $this->assertFalse(privatemsg_get_link(array($recipient3), $author));
1461

    
1462
    $this->drupalGet(privatemsg_get_link(array($recipient1, $recipient3), $author));
1463
    $this->assertFieldByName('recipient', $recipient1->name . ' [user]');
1464

    
1465
    // Disable links to self, verify that a link is only returned when the
1466
    // author is not the only recipient.
1467
    variable_set('privatemsg_display_link_self', FALSE);
1468

    
1469
    $this->assertFalse(privatemsg_get_link(array($author), $author));
1470

    
1471
    $this->drupalGet(privatemsg_get_link(array($recipient1, $author), $author));
1472
    $this->assertFieldByName('recipient', $recipient1->name . ' [user]');
1473

    
1474
    // Verify that link is not shown when recipient doesn't have read
1475
    // permission.
1476
    $this->assertFalse(privatemsg_get_link(array($recipient4), $author));
1477

    
1478
    $this->drupalGet(privatemsg_get_link(array($recipient1, $recipient4), $author));
1479
    $this->assertFieldByName('recipient', $recipient1->name . ' [user]');
1480

    
1481
    // Verify that link is not shown when author does not have write permission.
1482
    $this->drupalLogin($recipient1);
1483
    $this->assertFalse(privatemsg_get_link(array($author), $recipient1));
1484
  }
1485
}