mimemail_rules.test 8.0 KB

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