PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/tests/message_test.php

http://github.com/moodle/moodle
PHP | 269 lines | 192 code | 29 blank | 48 comment | 1 complexity | 5211dc4f36dad0e164e6c50038e046b5 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Test classes for \core\message\message.
  18. *
  19. * @package core_message
  20. * @category test
  21. * @copyright 2015 onwards Ankit Agarwal
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. global $CFG;
  26. /**
  27. * Test script for message class.
  28. *
  29. * @package core_message
  30. * @category test
  31. * @copyright 2015 onwards Ankit Agarwal
  32. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33. */
  34. class core_message_testcase extends advanced_testcase {
  35. /**
  36. * Test the method get_eventobject_for_processor().
  37. */
  38. public function test_get_eventobject_for_processor() {
  39. global $USER;
  40. $this->resetAfterTest();
  41. $this->setAdminUser();
  42. $user = $this->getDataGenerator()->create_user();
  43. $message = new \core\message\message();
  44. $message->courseid = SITEID;
  45. $message->component = 'moodle';
  46. $message->name = 'instantmessage';
  47. $message->userfrom = $USER;
  48. $message->userto = $user;
  49. $message->subject = 'message subject 1';
  50. $message->fullmessage = 'message body';
  51. $message->fullmessageformat = FORMAT_MARKDOWN;
  52. $message->fullmessagehtml = '<p>message body</p>';
  53. $message->smallmessage = 'small message';
  54. $message->notification = '0';
  55. $message->contexturl = 'http://GalaxyFarFarAway.com';
  56. $message->contexturlname = 'Context name';
  57. $message->replyto = "random@example.com";
  58. $message->replytoname = fullname($USER);
  59. $message->attachname = 'attachment';
  60. $content = array('*' => array('header' => ' test ', 'footer' => ' test ')); // Extra content for all types of messages.
  61. $message->set_additional_content('test', $content);
  62. // Create a file instance.
  63. $usercontext = context_user::instance($user->id);
  64. $file = new stdClass;
  65. $file->contextid = $usercontext->id;
  66. $file->component = 'user';
  67. $file->filearea = 'private';
  68. $file->itemid = 0;
  69. $file->filepath = '/';
  70. $file->filename = '1.txt';
  71. $file->source = 'test';
  72. $fs = get_file_storage();
  73. $file = $fs->create_file_from_string($file, 'file1 content');
  74. $message->attachment = $file;
  75. $stdclass = $message->get_eventobject_for_processor('test');
  76. $this->assertSame($message->courseid, $stdclass->courseid);
  77. $this->assertSame($message->component, $stdclass->component);
  78. $this->assertSame($message->name, $stdclass->name);
  79. $this->assertSame($message->userfrom, $stdclass->userfrom);
  80. $this->assertSame($message->userto, $stdclass->userto);
  81. $this->assertSame($message->subject, $stdclass->subject);
  82. $this->assertSame(' test ' . $message->fullmessage . ' test ', $stdclass->fullmessage);
  83. $this->assertSame(' test ' . $message->fullmessagehtml . ' test ', $stdclass->fullmessagehtml);
  84. $this->assertSame(' test ' . $message->smallmessage . ' test ', $stdclass->smallmessage);
  85. $this->assertSame($message->notification, $stdclass->notification);
  86. $this->assertSame($message->contexturl, $stdclass->contexturl);
  87. $this->assertSame($message->contexturlname, $stdclass->contexturlname);
  88. $this->assertSame($message->replyto, $stdclass->replyto);
  89. $this->assertSame($message->replytoname, $stdclass->replytoname);
  90. $this->assertSame($message->attachname, $stdclass->attachname);
  91. // Extra content for fullmessage only.
  92. $content = array('fullmessage' => array('header' => ' test ', 'footer' => ' test '));
  93. $message->set_additional_content('test', $content);
  94. $stdclass = $message->get_eventobject_for_processor('test');
  95. $this->assertSame(' test ' . $message->fullmessage . ' test ', $stdclass->fullmessage);
  96. $this->assertSame($message->fullmessagehtml, $stdclass->fullmessagehtml);
  97. $this->assertSame($message->smallmessage, $stdclass->smallmessage);
  98. // Extra content for fullmessagehtml and smallmessage only.
  99. $content = array('fullmessagehtml' => array('header' => ' test ', 'footer' => ' test '),
  100. 'smallmessage' => array('header' => ' testsmall ', 'footer' => ' testsmall '));
  101. $message->set_additional_content('test', $content);
  102. $stdclass = $message->get_eventobject_for_processor('test');
  103. $this->assertSame($message->fullmessage, $stdclass->fullmessage);
  104. $this->assertSame(' test ' . $message->fullmessagehtml . ' test ', $stdclass->fullmessagehtml);
  105. $this->assertSame(' testsmall ' . $message->smallmessage . ' testsmall ', $stdclass->smallmessage);
  106. // Extra content for * and smallmessage.
  107. $content = array('*' => array('header' => ' test ', 'footer' => ' test '),
  108. 'smallmessage' => array('header' => ' testsmall ', 'footer' => ' testsmall '));
  109. $message->set_additional_content('test', $content);
  110. $stdclass = $message->get_eventobject_for_processor('test');
  111. $this->assertSame(' test ' . $message->fullmessage . ' test ', $stdclass->fullmessage);
  112. $this->assertSame(' test ' . $message->fullmessagehtml . ' test ', $stdclass->fullmessagehtml);
  113. $this->assertSame(' testsmall ' . ' test ' . $message->smallmessage . ' test ' . ' testsmall ', $stdclass->smallmessage);
  114. }
  115. /**
  116. * Test sending messages as email works with the new class.
  117. */
  118. public function test_send_message() {
  119. global $DB, $CFG;
  120. $this->preventResetByRollback();
  121. $this->resetAfterTest();
  122. $user1 = $this->getDataGenerator()->create_user(array('maildisplay' => 1));
  123. $user2 = $this->getDataGenerator()->create_user();
  124. set_config('allowedemaildomains', 'example.com');
  125. // Test basic email processor.
  126. $this->assertFileExists("$CFG->dirroot/message/output/email/version.php");
  127. $this->assertFileExists("$CFG->dirroot/message/output/popup/version.php");
  128. $DB->set_field_select('message_processors', 'enabled', 0, "name <> 'email'");
  129. set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user2);
  130. // Extra content for all types of messages.
  131. $message = new \core\message\message();
  132. $message->courseid = 1;
  133. $message->component = 'moodle';
  134. $message->name = 'instantmessage';
  135. $message->userfrom = $user1;
  136. $message->userto = $user2;
  137. $message->subject = 'message subject 1';
  138. $message->fullmessage = 'message body';
  139. $message->fullmessageformat = FORMAT_MARKDOWN;
  140. $message->fullmessagehtml = '<p>message body</p>';
  141. $message->smallmessage = 'small message';
  142. $message->notification = '0';
  143. $content = array('*' => array('header' => ' test ', 'footer' => ' test '));
  144. $message->set_additional_content('email', $content);
  145. $sink = $this->redirectEmails();
  146. $messageid = message_send($message);
  147. $emails = $sink->get_messages();
  148. $this->assertCount(1, $emails);
  149. $email = reset($emails);
  150. $recordexists = $DB->record_exists('messages', array('id' => $messageid));
  151. $this->assertSame(true, $recordexists);
  152. $this->assertSame($user1->email, $email->from);
  153. $this->assertSame($user2->email, $email->to);
  154. $this->assertSame(get_string('unreadnewmessage', 'message', fullname($user1)), $email->subject);
  155. $this->assertNotEmpty($email->header);
  156. $this->assertNotEmpty($email->body);
  157. $this->assertRegExp('/test message body.*test/s', $email->body);
  158. $sink->clear();
  159. // Test that event fired includes the courseid.
  160. $eventsink = $this->redirectEvents();
  161. $messageid = message_send($message);
  162. $events = $eventsink->get_events();
  163. $event = reset($events);
  164. $this->assertEquals($message->courseid, $event->other['courseid']);
  165. $eventsink->clear();
  166. $sink->clear();
  167. // Extra content for small message only. Shouldn't show up in emails as we sent fullmessage and fullmessagehtml only in
  168. // the emails.
  169. $message = new \core\message\message();
  170. $message->courseid = 1;
  171. $message->component = 'moodle';
  172. $message->name = 'instantmessage';
  173. $message->userfrom = $user1;
  174. $message->userto = $user2;
  175. $message->subject = 'message subject 1';
  176. $message->fullmessage = 'message body';
  177. $message->fullmessageformat = FORMAT_MARKDOWN;
  178. $message->fullmessagehtml = '<p>message body</p>';
  179. $message->smallmessage = 'small message';
  180. $message->notification = '0';
  181. $content = array('smallmessage' => array('header' => ' test ', 'footer' => ' test '));
  182. $message->set_additional_content('email', $content);
  183. $messageid = message_send($message);
  184. $emails = $sink->get_messages();
  185. $this->assertCount(1, $emails);
  186. $email = reset($emails);
  187. $recordexists = $DB->record_exists('messages', array('id' => $messageid));
  188. $this->assertSame(true, $recordexists);
  189. $this->assertSame($user1->email, $email->from);
  190. $this->assertSame($user2->email, $email->to);
  191. $this->assertSame(get_string('unreadnewmessage', 'message', fullname($user1)), $email->subject);
  192. $this->assertNotEmpty($email->header);
  193. $this->assertNotEmpty($email->body);
  194. $this->assertNotRegExp('/test message body test/', $email->body);
  195. // Test that event fired includes the courseid.
  196. $eventsink = $this->redirectEvents();
  197. $messageid = message_send($message);
  198. $events = $eventsink->get_events();
  199. $event = reset($events);
  200. $this->assertEquals($message->courseid, $event->other['courseid']);
  201. $eventsink->close();
  202. $sink->close();
  203. }
  204. public function test_send_message_with_prefix() {
  205. global $DB, $CFG;
  206. $this->preventResetByRollback();
  207. $this->resetAfterTest();
  208. $user1 = $this->getDataGenerator()->create_user(array('maildisplay' => 1));
  209. $user2 = $this->getDataGenerator()->create_user();
  210. set_config('allowedemaildomains', 'example.com');
  211. set_config('emailsubjectprefix', '[Prefix Text]');
  212. // Test basic email processor.
  213. $this->assertFileExists("$CFG->dirroot/message/output/email/version.php");
  214. $this->assertFileExists("$CFG->dirroot/message/output/popup/version.php");
  215. $DB->set_field_select('message_processors', 'enabled', 0, "name <> 'email'");
  216. set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user2);
  217. // Check that prefix is ammended to the subject of the email.
  218. $message = new \core\message\message();
  219. $message->courseid = 1;
  220. $message->component = 'moodle';
  221. $message->name = 'instantmessage';
  222. $message->userfrom = $user1;
  223. $message->userto = $user2;
  224. $message->subject = get_string('unreadnewmessage', 'message', fullname($user1));
  225. $message->fullmessage = 'message body';
  226. $message->fullmessageformat = FORMAT_MARKDOWN;
  227. $message->fullmessagehtml = '<p>message body</p>';
  228. $message->smallmessage = 'small message';
  229. $message->notification = '0';
  230. $content = array('*' => array('header' => ' test ', 'footer' => ' test '));
  231. $message->set_additional_content('email', $content);
  232. $sink = $this->redirectEmails();
  233. $messageid = message_send($message);
  234. $emails = $sink->get_messages();
  235. $this->assertCount(1, $emails);
  236. $email = reset($emails);
  237. $this->assertSame('[Prefix Text] '. get_string('unreadnewmessage', 'message', fullname($user1)), $email->subject);
  238. $sink->clear();
  239. }
  240. }