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