[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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->component = 'moodle'; 52 $message->name = 'instantmessage'; 53 $message->userfrom = $USER; 54 $message->userto = $user; 55 $message->subject = 'message subject 1'; 56 $message->fullmessage = 'message body'; 57 $message->fullmessageformat = FORMAT_MARKDOWN; 58 $message->fullmessagehtml = '<p>message body</p>'; 59 $message->smallmessage = 'small message'; 60 $message->notification = '0'; 61 $message->contexturl = 'http://GalaxyFarFarAway.com'; 62 $message->contexturlname = 'Context name'; 63 $message->replyto = "random@example.com"; 64 $message->attachname = 'attachment'; 65 $content = array('*' => array('header' => ' test ', 'footer' => ' test ')); // Extra content for all types of messages. 66 $message->set_additional_content('test', $content); 67 68 // Create a file instance. 69 $usercontext = context_user::instance($user->id); 70 $file = new stdClass; 71 $file->contextid = $usercontext->id; 72 $file->component = 'user'; 73 $file->filearea = 'private'; 74 $file->itemid = 0; 75 $file->filepath = '/'; 76 $file->filename = '1.txt'; 77 $file->source = 'test'; 78 79 $fs = get_file_storage(); 80 $file = $fs->create_file_from_string($file, 'file1 content'); 81 $message->attachment = $file; 82 83 $stdclass = $message->get_eventobject_for_processor('test'); 84 85 $this->assertSame($message->component, $stdclass->component); 86 $this->assertSame($message->name, $stdclass->name); 87 $this->assertSame($message->userfrom, $stdclass->userfrom); 88 $this->assertSame($message->userto, $stdclass->userto); 89 $this->assertSame($message->subject, $stdclass->subject); 90 $this->assertSame(' test ' . $message->fullmessage . ' test ', $stdclass->fullmessage); 91 $this->assertSame(' test ' . $message->fullmessagehtml . ' test ', $stdclass->fullmessagehtml); 92 $this->assertSame(' test ' . $message->smallmessage . ' test ', $stdclass->smallmessage); 93 $this->assertSame($message->notification, $stdclass->notification); 94 $this->assertSame($message->contexturl, $stdclass->contexturl); 95 $this->assertSame($message->contexturlname, $stdclass->contexturlname); 96 $this->assertSame($message->replyto, $stdclass->replyto); 97 $this->assertSame($message->attachname, $stdclass->attachname); 98 99 // Extra content for fullmessage only. 100 $content = array('fullmessage' => array('header' => ' test ', 'footer' => ' test ')); 101 $message->set_additional_content('test', $content); 102 $stdclass = $message->get_eventobject_for_processor('test'); 103 $this->assertSame(' test ' . $message->fullmessage . ' test ', $stdclass->fullmessage); 104 $this->assertSame($message->fullmessagehtml, $stdclass->fullmessagehtml); 105 $this->assertSame($message->smallmessage, $stdclass->smallmessage); 106 107 // Extra content for fullmessagehtml and smallmessage only. 108 $content = array('fullmessagehtml' => array('header' => ' test ', 'footer' => ' test '), 109 'smallmessage' => array('header' => ' testsmall ', 'footer' => ' testsmall ')); 110 $message->set_additional_content('test', $content); 111 $stdclass = $message->get_eventobject_for_processor('test'); 112 $this->assertSame($message->fullmessage, $stdclass->fullmessage); 113 $this->assertSame(' test ' . $message->fullmessagehtml . ' test ', $stdclass->fullmessagehtml); 114 $this->assertSame(' testsmall ' . $message->smallmessage . ' testsmall ', $stdclass->smallmessage); 115 116 // Extra content for * and smallmessage. 117 $content = array('*' => array('header' => ' test ', 'footer' => ' test '), 118 'smallmessage' => array('header' => ' testsmall ', 'footer' => ' testsmall ')); 119 $message->set_additional_content('test', $content); 120 $stdclass = $message->get_eventobject_for_processor('test'); 121 $this->assertSame(' test ' . $message->fullmessage . ' test ', $stdclass->fullmessage); 122 $this->assertSame(' test ' . $message->fullmessagehtml . ' test ', $stdclass->fullmessagehtml); 123 $this->assertSame(' testsmall ' . ' test ' . $message->smallmessage . ' test ' . ' testsmall ', $stdclass->smallmessage); 124 } 125 126 /** 127 * Test sending messages as email works with the new class. 128 */ 129 public function test_send_message() { 130 global $DB, $CFG; 131 $this->preventResetByRollback(); 132 $this->resetAfterTest(); 133 134 $user1 = $this->getDataGenerator()->create_user(); 135 $user2 = $this->getDataGenerator()->create_user(); 136 137 // Test basic email processor. 138 $this->assertFileExists("$CFG->dirroot/message/output/email/version.php"); 139 $this->assertFileExists("$CFG->dirroot/message/output/popup/version.php"); 140 141 $DB->set_field_select('message_processors', 'enabled', 0, "name <> 'email'"); 142 set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user2); 143 144 // Extra content for all types of messages. 145 $message = new \core\message\message(); 146 $message->component = 'moodle'; 147 $message->name = 'instantmessage'; 148 $message->userfrom = $user1; 149 $message->userto = $user2; 150 $message->subject = 'message subject 1'; 151 $message->fullmessage = 'message body'; 152 $message->fullmessageformat = FORMAT_MARKDOWN; 153 $message->fullmessagehtml = '<p>message body</p>'; 154 $message->smallmessage = 'small message'; 155 $message->notification = '0'; 156 $content = array('*' => array('header' => ' test ', 'footer' => ' test ')); 157 $message->set_additional_content('email', $content); 158 159 $sink = $this->redirectEmails(); 160 $messageid = message_send($message); 161 $emails = $sink->get_messages(); 162 $this->assertCount(1, $emails); 163 $email = reset($emails); 164 $recordexists = $DB->record_exists('message_read', array('id' => $messageid)); 165 $this->assertSame(true, $recordexists); 166 $this->assertSame($user1->email, $email->from); 167 $this->assertSame($user2->email, $email->to); 168 $this->assertSame($message->subject, $email->subject); 169 $this->assertNotEmpty($email->header); 170 $this->assertNotEmpty($email->body); 171 $this->assertRegExp('/test message body test/', $email->body); 172 $sink->clear(); 173 174 // Extra content for small message only. Shouldn't show up in emails as we sent fullmessage and fullmessagehtml only in 175 // the emails. 176 $message = new \core\message\message(); 177 $message->component = 'moodle'; 178 $message->name = 'instantmessage'; 179 $message->userfrom = $user1; 180 $message->userto = $user2; 181 $message->subject = 'message subject 1'; 182 $message->fullmessage = 'message body'; 183 $message->fullmessageformat = FORMAT_MARKDOWN; 184 $message->fullmessagehtml = '<p>message body</p>'; 185 $message->smallmessage = 'small message'; 186 $message->notification = '0'; 187 $content = array('smallmessage' => array('header' => ' test ', 'footer' => ' test ')); 188 $message->set_additional_content('email', $content); 189 190 $messageid = message_send($message); 191 $emails = $sink->get_messages(); 192 $this->assertCount(1, $emails); 193 $email = reset($emails); 194 $recordexists = $DB->record_exists('message_read', array('id' => $messageid)); 195 $this->assertSame(true, $recordexists); 196 $this->assertSame($user1->email, $email->from); 197 $this->assertSame($user2->email, $email->to); 198 $this->assertSame($message->subject, $email->subject); 199 $this->assertNotEmpty($email->header); 200 $this->assertNotEmpty($email->body); 201 $this->assertNotRegExp('/test message body test/', $email->body); 202 $sink->close(); 203 } 204 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |