MDL-58650 core_message: always use 'popup' processor for messages
[moodle.git] / lib / tests / message_test.php
CommitLineData
93975a6a
AA
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
26defined('MOODLE_INTERNAL') || die();
27
28global $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 */
38class 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();
cc350fd9 51 $message->courseid = SITEID;
93975a6a
AA
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';
0fe86bbd 64 $message->replyto = "random@example.com";
93975a6a
AA
65 $message->attachname = 'attachment';
66 $content = array('*' => array('header' => ' test ', 'footer' => ' test ')); // Extra content for all types of messages.
67 $message->set_additional_content('test', $content);
68
69 // Create a file instance.
70 $usercontext = context_user::instance($user->id);
71 $file = new stdClass;
72 $file->contextid = $usercontext->id;
73 $file->component = 'user';
74 $file->filearea = 'private';
75 $file->itemid = 0;
76 $file->filepath = '/';
77 $file->filename = '1.txt';
78 $file->source = 'test';
79
80 $fs = get_file_storage();
81 $file = $fs->create_file_from_string($file, 'file1 content');
82 $message->attachment = $file;
83
84 $stdclass = $message->get_eventobject_for_processor('test');
85
cc350fd9 86 $this->assertSame($message->courseid, $stdclass->courseid);
93975a6a
AA
87 $this->assertSame($message->component, $stdclass->component);
88 $this->assertSame($message->name, $stdclass->name);
89 $this->assertSame($message->userfrom, $stdclass->userfrom);
90 $this->assertSame($message->userto, $stdclass->userto);
91 $this->assertSame($message->subject, $stdclass->subject);
92 $this->assertSame(' test ' . $message->fullmessage . ' test ', $stdclass->fullmessage);
93 $this->assertSame(' test ' . $message->fullmessagehtml . ' test ', $stdclass->fullmessagehtml);
94 $this->assertSame(' test ' . $message->smallmessage . ' test ', $stdclass->smallmessage);
95 $this->assertSame($message->notification, $stdclass->notification);
96 $this->assertSame($message->contexturl, $stdclass->contexturl);
97 $this->assertSame($message->contexturlname, $stdclass->contexturlname);
98 $this->assertSame($message->replyto, $stdclass->replyto);
99 $this->assertSame($message->attachname, $stdclass->attachname);
100
101 // Extra content for fullmessage only.
102 $content = array('fullmessage' => array('header' => ' test ', 'footer' => ' test '));
103 $message->set_additional_content('test', $content);
104 $stdclass = $message->get_eventobject_for_processor('test');
105 $this->assertSame(' test ' . $message->fullmessage . ' test ', $stdclass->fullmessage);
106 $this->assertSame($message->fullmessagehtml, $stdclass->fullmessagehtml);
107 $this->assertSame($message->smallmessage, $stdclass->smallmessage);
108
109 // Extra content for fullmessagehtml and smallmessage only.
110 $content = array('fullmessagehtml' => array('header' => ' test ', 'footer' => ' test '),
111 'smallmessage' => array('header' => ' testsmall ', 'footer' => ' testsmall '));
112 $message->set_additional_content('test', $content);
113 $stdclass = $message->get_eventobject_for_processor('test');
114 $this->assertSame($message->fullmessage, $stdclass->fullmessage);
115 $this->assertSame(' test ' . $message->fullmessagehtml . ' test ', $stdclass->fullmessagehtml);
116 $this->assertSame(' testsmall ' . $message->smallmessage . ' testsmall ', $stdclass->smallmessage);
117
118 // Extra content for * and smallmessage.
119 $content = array('*' => array('header' => ' test ', 'footer' => ' test '),
120 'smallmessage' => array('header' => ' testsmall ', 'footer' => ' testsmall '));
121 $message->set_additional_content('test', $content);
122 $stdclass = $message->get_eventobject_for_processor('test');
123 $this->assertSame(' test ' . $message->fullmessage . ' test ', $stdclass->fullmessage);
124 $this->assertSame(' test ' . $message->fullmessagehtml . ' test ', $stdclass->fullmessagehtml);
125 $this->assertSame(' testsmall ' . ' test ' . $message->smallmessage . ' test ' . ' testsmall ', $stdclass->smallmessage);
126 }
127
128 /**
129 * Test sending messages as email works with the new class.
130 */
131 public function test_send_message() {
132 global $DB, $CFG;
133 $this->preventResetByRollback();
134 $this->resetAfterTest();
135
e0789335 136 $user1 = $this->getDataGenerator()->create_user(array('maildisplay' => 1));
93975a6a 137 $user2 = $this->getDataGenerator()->create_user();
e0789335 138 set_config('allowedemaildomains', 'example.com');
93975a6a
AA
139
140 // Test basic email processor.
141 $this->assertFileExists("$CFG->dirroot/message/output/email/version.php");
142 $this->assertFileExists("$CFG->dirroot/message/output/popup/version.php");
143
144 $DB->set_field_select('message_processors', 'enabled', 0, "name <> 'email'");
145 set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user2);
146
147 // Extra content for all types of messages.
148 $message = new \core\message\message();
cc350fd9 149 $message->courseid = 1;
93975a6a
AA
150 $message->component = 'moodle';
151 $message->name = 'instantmessage';
152 $message->userfrom = $user1;
153 $message->userto = $user2;
154 $message->subject = 'message subject 1';
155 $message->fullmessage = 'message body';
156 $message->fullmessageformat = FORMAT_MARKDOWN;
157 $message->fullmessagehtml = '<p>message body</p>';
158 $message->smallmessage = 'small message';
159 $message->notification = '0';
160 $content = array('*' => array('header' => ' test ', 'footer' => ' test '));
161 $message->set_additional_content('email', $content);
162
163 $sink = $this->redirectEmails();
164 $messageid = message_send($message);
165 $emails = $sink->get_messages();
166 $this->assertCount(1, $emails);
167 $email = reset($emails);
685daf1a 168 $recordexists = $DB->record_exists('message', array('id' => $messageid));
93975a6a
AA
169 $this->assertSame(true, $recordexists);
170 $this->assertSame($user1->email, $email->from);
171 $this->assertSame($user2->email, $email->to);
172 $this->assertSame($message->subject, $email->subject);
173 $this->assertNotEmpty($email->header);
174 $this->assertNotEmpty($email->body);
175 $this->assertRegExp('/test message body test/', $email->body);
176 $sink->clear();
177
cc350fd9
AD
178 // Test that event fired includes the courseid.
179 $eventsink = $this->redirectEvents();
180 $messageid = message_send($message);
181 $events = $eventsink->get_events();
182 $event = reset($events);
183 $this->assertEquals($message->courseid, $event->other['courseid']);
184 $eventsink->clear();
185 $sink->clear();
186
93975a6a
AA
187 // Extra content for small message only. Shouldn't show up in emails as we sent fullmessage and fullmessagehtml only in
188 // the emails.
189 $message = new \core\message\message();
cc350fd9 190 $message->courseid = 1;
93975a6a
AA
191 $message->component = 'moodle';
192 $message->name = 'instantmessage';
193 $message->userfrom = $user1;
194 $message->userto = $user2;
195 $message->subject = 'message subject 1';
196 $message->fullmessage = 'message body';
197 $message->fullmessageformat = FORMAT_MARKDOWN;
198 $message->fullmessagehtml = '<p>message body</p>';
199 $message->smallmessage = 'small message';
200 $message->notification = '0';
201 $content = array('smallmessage' => array('header' => ' test ', 'footer' => ' test '));
202 $message->set_additional_content('email', $content);
203
204 $messageid = message_send($message);
205 $emails = $sink->get_messages();
206 $this->assertCount(1, $emails);
207 $email = reset($emails);
685daf1a 208 $recordexists = $DB->record_exists('message', array('id' => $messageid));
93975a6a
AA
209 $this->assertSame(true, $recordexists);
210 $this->assertSame($user1->email, $email->from);
211 $this->assertSame($user2->email, $email->to);
212 $this->assertSame($message->subject, $email->subject);
213 $this->assertNotEmpty($email->header);
214 $this->assertNotEmpty($email->body);
215 $this->assertNotRegExp('/test message body test/', $email->body);
cc350fd9
AD
216
217 // Test that event fired includes the courseid.
218 $eventsink = $this->redirectEvents();
219 $messageid = message_send($message);
220 $events = $eventsink->get_events();
221 $event = reset($events);
222 $this->assertEquals($message->courseid, $event->other['courseid']);
223 $eventsink->close();
93975a6a
AA
224 $sink->close();
225 }
0fe86bbd 226}