mimemail_rules.test 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * @file
  4. * Functionality tests for the Rules integration in the Mime Mail module.
  5. *
  6. * @ingroup mimemail
  7. */
  8. /**
  9. * Tests the Rules integration.
  10. */
  11. class MimeMailRulesTestCase extends DrupalWebTestCase {
  12. protected $adminUser;
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Rules integration',
  16. 'description' => 'Test the Rules integration.',
  17. 'group' => 'Mime Mail',
  18. );
  19. }
  20. public function setUp() {
  21. parent::setUp('mailsystem', 'locale', 'entity', 'entity_token', 'rules', 'mimemail');
  22. // Create and login user.
  23. $this->adminUser = $this->drupalCreateUser(array(
  24. 'access administration pages',
  25. 'edit mimemail user settings',
  26. 'administer languages',
  27. 'administer rules',
  28. 'bypass rules access',
  29. 'access rules debug',
  30. ));
  31. $this->drupalLogin($this->adminUser);
  32. // Enable another language too.
  33. foreach (array('de', 'it') as $langcode) {
  34. $edit = array();
  35. $edit['langcode'] = $langcode;
  36. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  37. }
  38. // Make sure we are not using a stale list.
  39. drupal_static_reset('language_list');
  40. }
  41. /**
  42. * Create rule with "mimemail" action and fire it.
  43. */
  44. public function testMailToUserAction() {
  45. $settings = array(
  46. 'key' => 'mail-key-' . $this->randomName(),
  47. 'to' => $this->randomName() . '@example.com',
  48. 'from' => $this->randomName() . '@example.com',
  49. 'subject' => $this->randomName(),
  50. 'body' => $this->randomName(60) . '<div></div><br /><hr>',
  51. 'plaintext' => $this->randomName(30) . '<div></div><br /><hr>',
  52. );
  53. // Set no language for the mail and check if the system default is used.
  54. $rule = rule();
  55. $rule->action('mimemail', array(
  56. 'key' => $settings['key'],
  57. 'to' => $settings['to'],
  58. 'from_mail' => $settings['from'],
  59. 'subject' => $settings['subject'],
  60. 'body' => $settings['body'],
  61. 'plaintext' => $settings['plaintext'],
  62. 'language' => '',
  63. ))->save();
  64. $rule->execute();
  65. $mails = $this->drupalGetMails(array('key' => $settings['key']));
  66. $this->assertEqual(count($mails), 1);
  67. $mail = reset($mails);
  68. $this->assertEqual($mail['to'], $settings['to']);
  69. $this->assertEqual($mail['from'], $settings['from']);
  70. $this->assertEqual($mail['subject'], $settings['subject']);
  71. $this->assertEqual($mail['params']['context']['body'], $settings['body']);
  72. $this->assertEqual($mail['params']['plaintext'], $settings['plaintext']);
  73. $this->assertEqual($mail['language']->language, language_default('language'));
  74. // Explicitly set another language for the mail.
  75. $rule_action = $rule->elementMap()->lookup(3);
  76. unset($rule_action->settings['language:select']);
  77. $rule_action->settings['language'] = 'de';
  78. $rule_action->settings['key'] = $settings['key'];
  79. $rule->save();
  80. $rule->execute();
  81. $mails = $this->drupalGetMails(array('key' => $settings['key']));
  82. $this->assertEqual(count($mails), 2);
  83. $mail = end($mails);
  84. $this->assertEqual($mail['language']->language, 'de');
  85. }
  86. /**
  87. * Create rule with "mimemail_to_users_of_role" action and fire it.
  88. */
  89. public function testMailToUsersOfRoleAction() {
  90. $languages = language_list();
  91. // Add more users and roles.
  92. $users = array(
  93. $this->randomName() . '@example.com' => 'en',
  94. $this->randomName() . '@example.com' => 'de',
  95. $this->randomName() . '@example.com' => 'it',
  96. $this->randomName() . '@example.com' => '',
  97. $this->randomName() . '@example.com' => 'invalid',
  98. );
  99. $mimemail_role = $this->drupalCreateRole(array());
  100. foreach ($users as $email => $language) {
  101. $user = $this->drupalCreateUser(array(
  102. 'access administration pages',
  103. ));
  104. $user->language = $language;
  105. $user->mail = $email;
  106. $user->roles[$mimemail_role] = $mimemail_role;
  107. user_save($user);
  108. }
  109. $settings = array(
  110. 'key' => 'mail-key-' . $this->randomName(),
  111. 'from' => $this->randomName() . '@example.com',
  112. 'subject' => $this->randomName(),
  113. 'body' => $this->randomName(60) . '<div></div><br /><hr>',
  114. 'plaintext' => $this->randomName(30) . '<div></div><br /><hr>',
  115. );
  116. // Rest the collected mails.
  117. variable_set('drupal_test_email_collector', array());
  118. // Send mails to all users of a role and respect the language of the users.
  119. // Don't enforce a specific language as fallback use the system default.
  120. $rule = rule();
  121. $rule->action('mimemail_to_users_of_role', array(
  122. 'key' => $settings['key'],
  123. 'from_mail' => $settings['from'],
  124. 'subject' => $settings['subject'],
  125. 'body' => $settings['body'],
  126. 'plaintext' => $settings['plaintext'],
  127. 'roles' => array($mimemail_role => $mimemail_role),
  128. 'active' => TRUE,
  129. 'language_user' => TRUE,
  130. 'language' => '',
  131. ));
  132. $rule->save();
  133. $rule->execute();
  134. $mails = $this->drupalGetMails(array('key' => $settings['key']));
  135. $this->assertEqual(count($mails), count($users));
  136. $mail = reset($mails);
  137. $this->assertEqual($mail['from'], $settings['from']);
  138. $this->assertEqual($mail['subject'], $settings['subject']);
  139. $this->assertEqual($mail['params']['context']['body'], $settings['body']);
  140. $this->assertEqual($mail['params']['plaintext'], $settings['plaintext']);
  141. foreach ($mails as $mail) {
  142. // If the user hasn't a proper language the system default has to be used
  143. // if the rules action doesn't provide a language to use.
  144. $user_language = (!empty($languages[$users[$mail['to']]])) ? $users[$mail['to']] : language_default('language');
  145. $this->assertEqual($mail['language']->language, $user_language);
  146. }
  147. // Rest the collected mails.
  148. variable_set('drupal_test_email_collector', array());
  149. // Send mails to all users of a role and respect the language of the users.
  150. // Enforce German as fallback language if an user doesn't have a language.
  151. $rule->elementMap()->lookup(3)->settings['language'] = 'de';
  152. $rule->save();
  153. $rule->execute();
  154. $mails = $this->drupalGetMails(array('key' => $settings['key']));
  155. $this->assertEqual(count($mails), count($users));
  156. foreach ($mails as $mail) {
  157. // If the user hasn't a proper language the language set in the rules
  158. // action has to be used.
  159. $user_language = (!empty($languages[$users[$mail['to']]])) ? $users[$mail['to']] : 'de';
  160. $this->assertEqual($mail['language']->language, $user_language);
  161. }
  162. // Rest the collected mails.
  163. variable_set('drupal_test_email_collector', array());
  164. // Send mails to all users of a role but use the same language for all.
  165. $rule->elementMap()->lookup(3)->settings['language_user'] = FALSE;
  166. $rule->elementMap()->lookup(3)->settings['language'] = 'it';
  167. $rule->save();
  168. $rule->execute();
  169. $mails = $this->drupalGetMails(array('key' => $settings['key']));
  170. foreach ($mails as $mail) {
  171. $this->assertEqual($mail['language']->language, 'it');
  172. }
  173. // Rest the collected mails.
  174. variable_set('drupal_test_email_collector', array());
  175. // Send mails to all users of a role except deactivated users.
  176. // Disable one of the users.
  177. reset($users);
  178. $user = user_load_by_mail(key($users));
  179. $user->status = 0;
  180. user_save($user);
  181. $rule->execute();
  182. $mails = $this->drupalGetMails(array('key' => $settings['key']));
  183. $this->assertEqual(count($mails), count($users) - 1);
  184. // Rest the collected mails.
  185. variable_set('drupal_test_email_collector', array());
  186. // Send mails to all users, also to deactivated ones.
  187. $rule->elementMap()->lookup(3)->settings['active'] = FALSE;
  188. $rule->save();
  189. $rule->execute();
  190. $mails = $this->drupalGetMails(array('key' => $settings['key']));
  191. // One user is disabled but it should be ignored.
  192. $this->assertEqual(count($mails), count($users));
  193. }
  194. }