Newer
Older
Sascha Grossenbacher
committed
<?php
/**
* @file
* Test file for pm_email_notify.module
*/
class PrivatemsgEMailNotifyTestCase extends PrivatemsgBaseTestCase {
Sascha Grossenbacher
committed
protected $handledMails = 0;
/**
* Implements getInfo().
*/
function getInfo() {
return array
(
'name' => t('Privatemsg E-Mail notifications.'),
'description' => t('Test sending e-mail notifications according to the set configuration.'),
'group' => t('Privatemsg'),
);
}
/**
* Implements setUp().
*/
function setUp() {
parent::setUp('privatemsg', 'pm_email_notify', 'privatemsg_roles');
$this->author = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'write privatemsg to all roles'));
$this->recipient = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'set privatemsg e-mail notification level', 'change privatemsg e-mail notification for indirect messages'));
doitDave
committed
$this->recipient2 = $this->drupalCreateUser(array('read privatemsg'));
Sascha Grossenbacher
committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
$this->admin = $this->drupalCreateUser(array('write privatemsg', 'read privatemsg', 'administer privatemsg settings'));
$this->handledMails = 0;
}
/**
* Test Always configuration.
*/
function testAlwaysConfiguration() {
$this->drupalLogin($this->author);
// Send a new message.
$message = array(
'recipient' => $this->recipient->name . ', ' . $this->recipient2->name,
'subject' => $this->randomName(),
'body[value]' => $this->randomName(20),
);
$this->drupalPost('messages/new', $message, t('Send message'));
$message['thread_id'] = $this->getCurrentThreadId();
$this->verifyMails(array($this->recipient, $this->recipient2), $this->author, $message);
// Reply to the existing message.
$reply = array(
'body[value]' => $this->randomName(20),
);
$this->drupalPost(NULL, $reply, t('Send message'));
$message['body[value]'] = $reply['body[value]'];
$this->verifyMails(array($this->recipient, $this->recipient2), $this->author, $message);
// Send a message to all users.
$message = array(
'recipient' => 'authenticated user',
'subject' => $this->randomName(),
'body[value]' => $this->randomName(20),
);
$this->drupalPost('messages/new', $message, t('Send message'));
$message['thread_id'] = $this->getCurrentThreadId();
$recipients = array(user_load(1), $this->author, $this->recipient, $this->recipient2, $this->admin);
$this->verifyMails($recipients, $this->author, $message);
// Verify that the user with the necessary permission chan choose the exact
// notification level while the other can only opt out.
$this->drupalLogin($this->recipient);
$this->drupalGet('user/' . $this->recipient->uid . '/edit');
// Verify correct defaults.
$this->assertFieldByName('pm_email_notify_level', PM_EMAIL_NOTIFY_LEVEL_ALL, t('Notification level setting has the correct default value'));
$edit = array(
'pm_email_notify_level' => PM_EMAIL_NOTIFY_LEVEL_DISABLED,
);
$this->drupalPost(NULL, $edit, t('Save'));
// Send another message, only recipient two should get a notification.
$this->drupalLogin($this->author);
// Send a new message.
$message = array(
'recipient' => $this->recipient->name . ', ' . $this->recipient2->name,
'subject' => $this->randomName(),
'body[value]' => $this->randomName(20),
);
$this->drupalPost('messages/new', $message, t('Send message'));
$message['thread_id'] = $this->getCurrentThreadId();
$this->verifyMails(array($this->recipient2), $this->author, $message);
// Opt out of notifications with the other user.
$this->drupalLogin($this->recipient2);
$this->drupalGet('user/' . $this->recipient2->uid . '/edit');
// Verify correct defaults.
$this->assertFieldChecked('edit-pm-email-notify-level', t('Notification level is set to default.'));
$edit = array(
'pm_email_notify_level' => FALSE,
);
$this->drupalPost(NULL, $edit, t('Save'));
// Send another message, only recipient two should get a notification.
$this->drupalLogin($this->author);
// Send a new message.
$message = array(
'recipient' => $this->recipient->name . ', ' . $this->recipient2->name,
'subject' => $this->randomName(),
'body[value]' => $this->randomName(20),
);
$this->drupalPost('messages/new', $message, t('Send message'));
$this->getNewMails(0);
}
/**
* Test Always configuration.
*/
function testOnlyUserConfiguration() {
$this->drupalLogin($this->author);
// Send a message to all users.
$message = array(
'recipient' => 'authenticated user',
'subject' => $this->randomName(),
'body[value]' => $this->randomName(20),
);
$this->drupalPost('messages/new', $message, t('Send message'));
$message['thread_id'] = $this->getCurrentThreadId();
$recipients = array(user_load(1), $this->author, $this->recipient, $this->recipient2, $this->admin);
$this->verifyMails($recipients, $this->author, $message);
// Enable only user.
$this->drupalLogin($this->admin);
$edit = array(
'privatemsg_setting_email_notify_only_user' => TRUE,
);
$this->drupalPost('admin/config/messaging/privatemsg', $edit, t('Save configuration'));
$this->drupalLogin($this->author);
// No notifications should be sent now.
$message = array(
'recipient' => 'authenticated user',
'subject' => $this->randomName(),
'body[value]' => $this->randomName(20),
);
$this->drupalPost('messages/new', $message, t('Send message'));
$this->getNewMails(0);
// Verify that only the user with the necessary permission can change
// the default setting.
$this->drupalLogin($this->recipient);
$this->drupalGet('user/' . $this->recipient->uid . '/edit');
// Verify correct defaults.
$this->assertFieldChecked('edit-pm-email-only-user', t('Only user option has the correct default value'));
//$this->assertFieldByName('pm_email_notify_level', PM_EMAIL_NOTIFY_LEVEL_ALL, t('Notification level setting has the correct default value'));
$edit = array(
'pm_email_only_user' => FALSE,
);
$this->drupalPost(NULL, $edit, t('Save'));
$this->drupalLogin($this->recipient2);
$this->drupalGet('user/' . $this->recipient2->uid . '/edit');
// Verify correct defaults.
$this->assertNoFieldById('edit-pm-email-only-user', NULL, t('User is not allowed to customize noly user option.'));
//$this->assertNoText(t('Private messages'), t('Private messages fieldset is not displayed when there are no fields within it.'));
$this->drupalLogin($this->author);
// Send another message to all users, only $this->recipient should receive
// a notification.
$message = array(
'recipient' => 'authenticated user',
'subject' => $this->randomName(),
'body[value]' => $this->randomName(20),
);
$this->drupalPost('messages/new', $message, t('Send message'));
$message['thread_id'] = $this->getCurrentThreadId();
$recipients = array($this->recipient);
$this->verifyMails($recipients, $this->author, $message);
}
/**
* Test Always configuration.
*/
function testThreadConfiguration() {
// Configure to only send notifications for new messages.
$this->drupalLogin($this->admin);
$edit = array(
'privatemsg_setting_email_notify_level' => PM_EMAIL_NOTIFY_LEVEL_THREAD,
);
$this->drupalPost('admin/config/messaging/privatemsg', $edit, t('Save configuration'));
$this->drupalLogin($this->author);
// Send a new message.
$message = array(
'recipient' => $this->recipient->name . ', ' . $this->recipient2->name,
'subject' => $this->randomName(),
'body[value]' => $this->randomName(20),
);
$this->drupalPost('messages/new', $message, t('Send message'));
$message['thread_id'] = $this->getCurrentThreadId();
$this->verifyMails(array($this->recipient, $this->recipient2), $this->author, $message);
// Reply to the existing message.
$reply = array(
'body[value]' => $this->randomName(20),
);
$this->drupalPost(NULL, $reply, t('Send message'));
$message['body[value]'] = $reply['body[value]'];
$this->getNewMails(0);
// Send a message to all users.
$message = array(
'recipient' => 'authenticated user',
'subject' => $this->randomName(),
'body[value]' => $this->randomName(20),
);
$this->drupalPost('messages/new', $message, t('Send message'));
$message['thread_id'] = $this->getCurrentThreadId();
$recipients = array(user_load(1), $this->author, $this->recipient, $this->recipient2, $this->admin);
$this->verifyMails($recipients, $this->author, $message);
}
/**
* Test Always configuration.
*/
function testUnreadConfiguration() {
// Configure to only send notifications for new messages.
$this->drupalLogin($this->admin);
$edit = array(
'privatemsg_setting_email_notify_level' => PM_EMAIL_NOTIFY_LEVEL_UNREAD_ONCE,
);
$this->drupalPost('admin/config/messaging/privatemsg', $edit, t('Save configuration'));
$this->drupalLogin($this->author);
// Send a new message.
$message = array(
'recipient' => $this->recipient->name . ', ' . $this->recipient2->name,
'subject' => $this->randomName(),
'body[value]' => $this->randomName(20),
);
$this->drupalPost('messages/new', $message, t('Send message'));
$message['thread_id'] = $this->getCurrentThreadId();
$this->verifyMails(array($this->recipient, $this->recipient2), $this->author, $message);
// Reply to the existing message.
$reply = array(
'body[value]' => $this->randomName(20),
);
$this->drupalPost(NULL, $reply, t('Send message'));
$message['body[value]'] = $reply['body[value]'];
$this->getNewMails(0);
// Visit the thread with the first recipient.
$this->drupalLogin($this->recipient);
$this->drupalGet('messages');
$this->clickLink($message['subject']);
// Back to author, send another message.
$this->drupalLogin($this->author);
$this->drupalGet('messages/view/' . $message['thread_id']);
$reply = array(
'body[value]' => $this->randomName(20),
);
$this->drupalPost(NULL, $reply, t('Send message'));
$message['body[value]'] = $reply['body[value]'];
$this->verifyMails(array($this->recipient), $this->author, $message);
// Another reply, again, no notifications.
$reply = array(
'body[value]' => $this->randomName(20),
);
$this->drupalPost(NULL, $reply, t('Send message'));
$message['body[value]'] = $reply['body[value]'];
$this->getNewMails(0);
// Send a message to all users.
$message = array(
'recipient' => 'authenticated user',
'subject' => $this->randomName(),
'body[value]' => $this->randomName(20),
);
$this->drupalPost('messages/new', $message, t('Send message'));
$message['thread_id'] = $this->getCurrentThreadId();
$recipients = array(user_load(1), $this->author, $this->recipient, $this->recipient2, $this->admin);
$this->verifyMails($recipients, $this->author, $message);
}
/**
* Test that no mails are sent when
*/
function testNeverConfiguration() {
// Disable notifications by default.
$this->drupalLogin($this->admin);
$edit = array(
'privatemsg_setting_email_notify_level' => PM_EMAIL_NOTIFY_LEVEL_DISABLED,
);
$this->drupalPost('admin/config/messaging/privatemsg', $edit, t('Save configuration'));
$this->drupalLogin($this->author);
// Send a new message.
$message = array(
'recipient' => $this->recipient->name . ', ' . $this->recipient2->name,
'subject' => $this->randomName(),
'body[value]' => $this->randomName(20),
);
$this->drupalPost('messages/new', $message, t('Send message'));
$this->getNewMails(0);
// Send a message to all users.
$message = array(
'recipient' => 'authenticated user',
'subject' => $this->randomName(),
'body[value]' => $this->randomName(20),
);
$this->drupalPost('messages/new', $message, t('Send message'));
$this->getNewMails(0);
// Verify that only the user with the necessary permission enable email
// notifications.
$this->drupalLogin($this->recipient);
$this->drupalGet('user/' . $this->recipient->uid . '/edit');
// Verify correct defaults.
$this->assertFieldByName('pm_email_notify_level', PM_EMAIL_NOTIFY_LEVEL_ALL, t('Notification level setting has the correct default value'));
$edit = array(
'pm_email_notify_level' => PM_EMAIL_NOTIFY_LEVEL_ALL,
);
$this->drupalPost(NULL, $edit, t('Save'));
$this->drupalLogin($this->recipient2);
$this->drupalGet('user/' . $this->recipient2->uid . '/edit');
// Verify correct defaults.
$this->assertNoFieldById('edit-pm-email-notify-level', NULL, t('User is not allowed change email notification setting.'));
$this->assertNoText(t('Private messages'), t('Private messages fieldset is not displayed when there are no fields within it.'));
// Send a message to all users, only recipient should receive an email.
$this->drupalLogin($this->author);
$message = array(
'recipient' => 'authenticated user',
'subject' => $this->randomName(),
'body[value]' => $this->randomName(20),
);
$this->drupalPost('messages/new', $message, t('Send message'));
$message['thread_id'] = $this->getCurrentThreadId();
$recipients = array($this->recipient);
$this->verifyMails($recipients, $this->author, $message);
}
doitDave
committed
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/**
* Test the "author's email address as notification sender" feature.
*/
function testShowSenderMail() {
// Define a value for the "notification mails from" field.
$generic_sender = 'generic_sender@example.com';
// Test #1: Site wide setting is successfully stored (set to "show").
// Log in as admin and set the default to "show".
$this->drupalLogin($this->admin);
$edit = array(
'privatemsg_setting_show_sender_mail' => TRUE,
// Also set the generic sender address while we're here.
'pm_email_notify_from' => $generic_sender,
);
$this->drupalPost('admin/config/messaging/privatemsg', $edit, t('Save configuration'));
$this->drupalGet('admin/config/messaging/privatemsg');
// Verify correct defaults.
$this->assertFieldByName('privatemsg_setting_show_sender_mail', 1, "<em>Use sender's email address</em> value is now enabled in the form.");
// Test #2: User defaults match site-wide setting (currently "show").
// Log in as author and check for the custom field default.
$this->drupalLogin($this->author);
$this->drupalGet('user/' . $this->author->uid . '/edit');
$this->assertFieldByName('pm_show_sender_mail', 1, "User's default setting for <em>show my email address</em> is also enabled.");
// Test #3: User can override the site-wide setting (back to "hide")
// and his private mail address will be hidden.
$edit = array(
'pm_show_sender_mail' => FALSE,
);
$this->drupalPost(NULL, $edit, t('Save'));
$this->drupalGet('user/' . $this->author->uid . '/edit');
$this->assertFieldByName('pm_show_sender_mail', 0, "User successfully disabled his <em>show my email address</em> setting.");
// Let author send a test mail.
$message = array(
'recipient' => $this->recipient->name,
'subject' => $this->randomName(),
'body[value]' => $this->randomName(20),
);
$this->drupalPost('messages/new', $message, t('Send message'));
$message['thread_id'] = $this->getCurrentThreadId();
$recipients = array($this->recipient);
$this->verifyMails($recipients, $this->author, $message, $generic_sender);
// Test #4: User can change his setting to "show" and his private
// mail address will be used.
$this->drupalGet('user/' . $this->author->uid . '/edit');
$edit = array(
'pm_show_sender_mail' => TRUE,
);
$this->drupalPost(NULL, $edit, t('Save'));
// Let author send a test mail.
$message = array(
'recipient' => $this->recipient->name,
'subject' => $this->randomName(),
'body[value]' => $this->randomName(20),
);
$this->drupalPost('messages/new', $message, t('Send message'));
$message['thread_id'] = $this->getCurrentThreadId();
$recipients = array($this->recipient);
$this->verifyMails($recipients, $this->author, $message, $this->author->mail);
}
Sascha Grossenbacher
committed
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/**
* Extracts the current thread_id from the URl.
*
* @return
* Thread id of the current page.
*/
function getCurrentThreadId() {
preg_match('|messages\/view\/([0-9]+)|', $this->getURL(), $matches);
return $matches[1];
}
/**
* Return new mails.
*
* Returns all mails which were sent since the last call to this function.
*
* @param $expectedNumber
* Number of new mails that should be there.
*/
function getNewMails($expectedNumber) {
// Load all mails of the current test.
$mails = $this->drupalGetMails();
// Slice of the new ones.
$mails_slice = array_slice($mails, $this->handledMails);
// Update amount of already handled mails.
$this->handledMails = count($mails);
if ($expectedNumber !== FALSE) {
$this->assertEqual(count($mails_slice), $expectedNumber, t('@expected notification mails were sent.', array('@expected' => $expectedNumber)));
}
return $mails_slice;
}
/**
* Verify multiple notification mails
*
* @param $recipients
* Array of recipients in the order in which they received their mail.
* @param $author
* Author user object.
* @param $message
* Message array, containing the keys thread_id, body and subject.
doitDave
committed
* @param $expectedFrom
* The mail address expected to be the notification origin (optional).
Sascha Grossenbacher
committed
*/
doitDave
committed
function verifyMails($recipients, $author, $message, $expectedFrom = NULL) {
Sascha Grossenbacher
committed
drupal_static_reset('privatemsg_thread_load');
$mails = $this->getNewMails(count($recipients));
foreach ($recipients as $id => $recipient) {
doitDave
committed
$this->verifyMail($mails[$id], $recipient, $author, $message, $expectedFrom);
Sascha Grossenbacher
committed
}
}
/**
* Verify a notification mail.
*
* @param $mail
* Mail array as returned by DrupalWebTestCase::drupalGetMails().
* @param $recipient
* Recipient user object.
* @param $author
* Author user object.
* @param $message
* Message array, containing the keys thread_id, body and subject.
doitDave
committed
* @param $expectedFrom
* The mail address expected to be the notification origin (optional).
*
Sascha Grossenbacher
committed
*/
doitDave
committed
function verifyMail($mail, $recipient, $author, $message, $expectedFrom = NULL) {
Sascha Grossenbacher
committed
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
$this->assertEqual($mail['id'], 'pm_email_notify_notice', t('Correct notification id.'));
$this->assertEqual($mail['to'], $recipient->mail, t('Correct recipient e-mail address.'));
if (is_array($message) && isset($message['thread_id'])) {
$mid = db_query('SELECT MAX(mid) FROM {pm_index} WHERE thread_id = :thread_id', array(':thread_id' => $message['thread_id']))->fetchField();
$message = privatemsg_message_load($mid);
}
$data = array(
'privatemsg_message' => $message,
'privatemsg_recipient' => $recipient,
);
$options = array(
'language' => user_preferred_language($recipient),
// Don't sanitize output since this is used in an email, not a browser.
'sanitize' => FALSE,
// Custom token to avoid custom token handling.
'privatemsg-display-invalid' => FALSE,
);
$subject = trim(token_replace(pm_email_notify_text('subject', $options['language']), $data, $options));
$body = drupal_wrap_mail(drupal_html_to_text(trim(token_replace(pm_email_notify_text('body', $options['language']), $data, $options))));
$this->assertEqual($mail['subject'], $subject, t('Correct subject'));
$this->assertEqual($mail['body'], $body, t('Correct body'));
doitDave
committed
// Optionally verify the message's from address.
if (isset($expectedFrom)) {
$this->assertEqual($mail['from'], $expectedFrom, format_string( 'Mail sender was %sender, expected sender was %expected', array(
'%sender' => $mail['from'],
'%expected' => $expectedFrom,
)));
}
Sascha Grossenbacher
committed
}