pm_email_notify.test

  1. 7-2 pm_email_notify/pm_email_notify.test
  2. 6-2 pm_email_notify/pm_email_notify.test

Test file for pm_email_notify.module

File

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

Classes