Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]
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 /** 18 * Test classes for \core\message\message. 19 * 20 * @package core_message 21 * @category test 22 * @copyright 2015 onwards Ankit Agarwal 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 global $CFG; 29 30 /** 31 * Test script for message class. 32 * 33 * @package core_message 34 * @category test 35 * @copyright 2015 onwards Ankit Agarwal 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 */ 38 class core_message_testcase extends advanced_testcase { 39 40 /** 41 * Test the method get_eventobject_for_processor(). 42 */ 43 public function test_get_eventobject_for_processor() { 44 global $USER; 45 $this->resetAfterTest(); 46 $this->setAdminUser(); 47 48 $user = $this->getDataGenerator()->create_user(); 49 50 $message = new \core\message\message(); 51 $message->courseid = SITEID; 52 $message->component = 'moodle'; 53 $message->name = 'instantmessage'; 54 $message->userfrom = $USER; 55 $message->userto = $user; 56 $message->subject = 'message subject 1'; 57 $message->fullmessage = 'message body'; 58 $message->fullmessageformat = FORMAT_MARKDOWN; 59 $message->fullmessagehtml = '<p>message body</p>'; 60 $message->smallmessage = 'small message'; 61 $message->notification = '0'; 62 $message->contexturl = 'http://GalaxyFarFarAway.com'; 63 $message->contexturlname = 'Context name'; 64 $message->replyto = "random@example.com"; 65 $message->replytoname = fullname($USER); 66 $message->attachname = 'attachment'; 67 $content = array('*' => array('header' => ' test ', 'footer' => ' test ')); // Extra content for all types of messages. 68 $message->set_additional_content('test', $content); 69 70 // Create a file instance. 71 $usercontext = context_user::instance($user->id); 72 $file = new stdClass; 73 $file->contextid = $usercontext->id; 74 $file->component = 'user'; 75 $file->filearea = 'private'; 76 $file->itemid = 0; 77 $file->filepath = '/'; 78 $file->filename = '1.txt'; 79 $file->source = 'test'; 80 81 $fs = get_file_storage(); 82 $file = $fs->create_file_from_string($file, 'file1 content'); 83 $message->attachment = $file; 84 85 $stdclass = $message->get_eventobject_for_processor('test'); 86 87 $this->assertSame($message->courseid, $stdclass->courseid); 88 $this->assertSame($message->component, $stdclass->component); 89 $this->assertSame($message->name, $stdclass->name); 90 $this->assertSame($message->userfrom, $stdclass->userfrom); 91 $this->assertSame($message->userto, $stdclass->userto); 92 $this->assertSame($message->subject, $stdclass->subject); 93 $this->assertSame(' test ' . $message->fullmessage . ' test ', $stdclass->fullmessage); 94 $this->assertSame(' test ' . $message->fullmessagehtml . ' test ', $stdclass->fullmessagehtml); 95 $this->assertSame(' test ' . $message->smallmessage . ' test ', $stdclass->smallmessage); 96 $this->assertSame($message->notification, $stdclass->notification); 97 $this->assertSame($message->contexturl, $stdclass->contexturl); 98 $this->assertSame($message->contexturlname, $stdclass->contexturlname); 99 $this->assertSame($message->replyto, $stdclass->replyto); 100 $this->assertSame($message->replytoname, $stdclass->replytoname); 101 $this->assertSame($message->attachname, $stdclass->attachname); 102 103 // Extra content for fullmessage only. 104 $content = array('fullmessage' => array('header' => ' test ', 'footer' => ' test ')); 105 $message->set_additional_content('test', $content); 106 $stdclass = $message->get_eventobject_for_processor('test'); 107 $this->assertSame(' test ' . $message->fullmessage . ' test ', $stdclass->fullmessage); 108 $this->assertSame($message->fullmessagehtml, $stdclass->fullmessagehtml); 109 $this->assertSame($message->smallmessage, $stdclass->smallmessage); 110 111 // Extra content for fullmessagehtml and smallmessage only. 112 $content = array('fullmessagehtml' => array('header' => ' test ', 'footer' => ' test '), 113 'smallmessage' => array('header' => ' testsmall ', 'footer' => ' testsmall ')); 114 $message->set_additional_content('test', $content); 115 $stdclass = $message->get_eventobject_for_processor('test'); 116 $this->assertSame($message->fullmessage, $stdclass->fullmessage); 117 $this->assertSame(' test ' . $message->fullmessagehtml . ' test ', $stdclass->fullmessagehtml); 118 $this->assertSame(' testsmall ' . $message->smallmessage . ' testsmall ', $stdclass->smallmessage); 119 120 // Extra content for * and smallmessage. 121 $content = array('*' => array('header' => ' test ', 'footer' => ' test '), 122 'smallmessage' => array('header' => ' testsmall ', 'footer' => ' testsmall ')); 123 $message->set_additional_content('test', $content); 124 $stdclass = $message->get_eventobject_for_processor('test'); 125 $this->assertSame(' test ' . $message->fullmessage . ' test ', $stdclass->fullmessage); 126 $this->assertSame(' test ' . $message->fullmessagehtml . ' test ', $stdclass->fullmessagehtml); 127 $this->assertSame(' testsmall ' . ' test ' . $message->smallmessage . ' test ' . ' testsmall ', $stdclass->smallmessage); 128 } 129 130 /** 131 * Test sending messages as email works with the new class. 132 */ 133 public function test_send_message() { 134 global $DB, $CFG; 135 $this->preventResetByRollback(); 136 $this->resetAfterTest(); 137 138 $user1 = $this->getDataGenerator()->create_user(array('maildisplay' => 1)); 139 $user2 = $this->getDataGenerator()->create_user(); 140 set_config('allowedemaildomains', 'example.com'); 141 142 // Test basic email processor. 143 $this->assertFileExists("$CFG->dirroot/message/output/email/version.php"); 144 $this->assertFileExists("$CFG->dirroot/message/output/popup/version.php"); 145 146 $DB->set_field_select('message_processors', 'enabled', 0, "name <> 'email'"); 147 set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user2); 148 149 // Extra content for all types of messages. 150 $message = new \core\message\message(); 151 $message->courseid = 1; 152 $message->component = 'moodle'; 153 $message->name = 'instantmessage'; 154 $message->userfrom = $user1; 155 $message->userto = $user2; 156 $message->subject = 'message subject 1'; 157 $message->fullmessage = 'message body'; 158 $message->fullmessageformat = FORMAT_MARKDOWN; 159 $message->fullmessagehtml = '<p>message body</p>'; 160 $message->smallmessage = 'small message'; 161 $message->notification = '0'; 162 $content = array('*' => array('header' => ' test ', 'footer' => ' test ')); 163 $message->set_additional_content('email', $content); 164 165 $sink = $this->redirectEmails(); 166 $messageid = message_send($message); 167 $emails = $sink->get_messages(); 168 $this->assertCount(1, $emails); 169 $email = reset($emails); 170 $recordexists = $DB->record_exists('messages', array('id' => $messageid)); 171 $this->assertSame(true, $recordexists); 172 $this->assertSame($user1->email, $email->from); 173 $this->assertSame($user2->email, $email->to); 174 $this->assertSame(get_string('unreadnewmessage', 'message', fullname($user1)), $email->subject); 175 $this->assertNotEmpty($email->header); 176 $this->assertNotEmpty($email->body); 177 $this->assertRegExp('/test.*message body.*test/s', $email->body); 178 $sink->clear(); 179 180 // Test that event fired includes the courseid. 181 $eventsink = $this->redirectEvents(); 182 $messageid = message_send($message); 183 $events = $eventsink->get_events(); 184 $event = reset($events); 185 $this->assertEquals($message->courseid, $event->other['courseid']); 186 $eventsink->clear(); 187 $sink->clear(); 188 189 // Extra content for small message only. Shouldn't show up in emails as we sent fullmessage and fullmessagehtml only in 190 // the emails. 191 $message = new \core\message\message(); 192 $message->courseid = 1; 193 $message->component = 'moodle'; 194 $message->name = 'instantmessage'; 195 $message->userfrom = $user1; 196 $message->userto = $user2; 197 $message->subject = 'message subject 1'; 198 $message->fullmessage = 'message body'; 199 $message->fullmessageformat = FORMAT_MARKDOWN; 200 $message->fullmessagehtml = '<p>message body</p>'; 201 $message->smallmessage = 'small message'; 202 $message->notification = '0'; 203 $content = array('smallmessage' => array('header' => ' test ', 'footer' => ' test ')); 204 $message->set_additional_content('email', $content); 205 206 $messageid = message_send($message); 207 $emails = $sink->get_messages(); 208 $this->assertCount(1, $emails); 209 $email = reset($emails); 210 $recordexists = $DB->record_exists('messages', array('id' => $messageid)); 211 $this->assertSame(true, $recordexists); 212 $this->assertSame($user1->email, $email->from); 213 $this->assertSame($user2->email, $email->to); 214 $this->assertSame(get_string('unreadnewmessage', 'message', fullname($user1)), $email->subject); 215 $this->assertNotEmpty($email->header); 216 $this->assertNotEmpty($email->body); 217 $this->assertNotRegExp('/test.*message body test/', $email->body); 218 219 // Test that event fired includes the courseid. 220 $eventsink = $this->redirectEvents(); 221 $messageid = message_send($message); 222 $events = $eventsink->get_events(); 223 $event = reset($events); 224 $this->assertEquals($message->courseid, $event->other['courseid']); 225 $eventsink->close(); 226 $sink->close(); 227 } 228 229 public function test_send_message_with_prefix() { 230 global $DB, $CFG; 231 $this->preventResetByRollback(); 232 $this->resetAfterTest(); 233 234 $user1 = $this->getDataGenerator()->create_user(array('maildisplay' => 1)); 235 $user2 = $this->getDataGenerator()->create_user(); 236 set_config('allowedemaildomains', 'example.com'); 237 set_config('emailsubjectprefix', '[Prefix Text]'); 238 239 // Test basic email processor. 240 $this->assertFileExists("$CFG->dirroot/message/output/email/version.php"); 241 $this->assertFileExists("$CFG->dirroot/message/output/popup/version.php"); 242 243 $DB->set_field_select('message_processors', 'enabled', 0, "name <> 'email'"); 244 set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user2); 245 246 // Check that prefix is ammended to the subject of the email. 247 $message = new \core\message\message(); 248 $message->courseid = 1; 249 $message->component = 'moodle'; 250 $message->name = 'instantmessage'; 251 $message->userfrom = $user1; 252 $message->userto = $user2; 253 $message->subject = get_string('unreadnewmessage', 'message', fullname($user1)); 254 $message->fullmessage = 'message body'; 255 $message->fullmessageformat = FORMAT_MARKDOWN; 256 $message->fullmessagehtml = '<p>message body</p>'; 257 $message->smallmessage = 'small message'; 258 $message->notification = '0'; 259 $content = array('*' => array('header' => ' test ', 'footer' => ' test ')); 260 $message->set_additional_content('email', $content); 261 $sink = $this->redirectEmails(); 262 $messageid = message_send($message); 263 $emails = $sink->get_messages(); 264 $this->assertCount(1, $emails); 265 $email = reset($emails); 266 $this->assertSame('[Prefix Text] '. get_string('unreadnewmessage', 'message', fullname($user1)), $email->subject); 267 $sink->clear(); 268 } 269 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body