Merge branch 'MDL-42592_master' of https://github.com/markn86/moodle
[moodle.git] / lib / tests / messagelib_test.php
blob4f95515dd44afc775b62a8f8c032cad060a91ec1
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.
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/>.
17 /**
18 * Tests for messagelib.php.
20 * @package core_message
21 * @category phpunit
22 * @copyright 2012 The Open Universtiy
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 class core_messagelib_testcase extends advanced_testcase {
30 public function test_message_get_providers_for_user() {
31 global $CFG, $DB;
33 $this->resetAfterTest();
35 $generator = $this->getDataGenerator();
37 // Create a course category and course.
38 $cat = $generator->create_category(array('parent' => 0));
39 $course = $generator->create_course(array('category' => $cat->id));
40 $quiz = $generator->create_module('quiz', array('course' => $course->id));
41 $user = $generator->create_user();
43 $coursecontext = context_course::instance($course->id);
44 $quizcontext = context_module::instance($quiz->cmid);
45 $frontpagecontext = context_course::instance(SITEID);
47 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
49 // The user is a student in a course, and has the capability for quiz
50 // confirmation emails in one quiz in that course.
51 role_assign($studentrole->id, $user->id, $coursecontext->id);
52 assign_capability('mod/quiz:emailconfirmsubmission', CAP_ALLOW, $studentrole->id, $quizcontext->id);
54 // Give this message type to the front page role.
55 assign_capability('mod/quiz:emailwarnoverdue', CAP_ALLOW, $CFG->defaultfrontpageroleid, $frontpagecontext->id);
57 $providers = message_get_providers_for_user($user->id);
58 $this->assertTrue($this->message_type_present('mod_forum', 'posts', $providers));
59 $this->assertTrue($this->message_type_present('mod_quiz', 'confirmation', $providers));
60 $this->assertTrue($this->message_type_present('mod_quiz', 'attempt_overdue', $providers));
61 $this->assertFalse($this->message_type_present('mod_quiz', 'submission', $providers));
63 // A user is a student in a different course, they should not get confirmation.
64 $course2 = $generator->create_course(array('category' => $cat->id));
65 $user2 = $generator->create_user();
66 $coursecontext2 = context_course::instance($course2->id);
67 role_assign($studentrole->id, $user2->id, $coursecontext2->id);
68 accesslib_clear_all_caches_for_unit_testing();
69 $providers = message_get_providers_for_user($user2->id);
70 $this->assertTrue($this->message_type_present('mod_forum', 'posts', $providers));
71 $this->assertFalse($this->message_type_present('mod_quiz', 'confirmation', $providers));
73 // Now remove the frontpage role id, and attempt_overdue message should go away.
74 unset_config('defaultfrontpageroleid');
75 accesslib_clear_all_caches_for_unit_testing();
77 $providers = message_get_providers_for_user($user->id);
78 $this->assertTrue($this->message_type_present('mod_quiz', 'confirmation', $providers));
79 $this->assertFalse($this->message_type_present('mod_quiz', 'attempt_overdue', $providers));
80 $this->assertFalse($this->message_type_present('mod_quiz', 'submission', $providers));
83 public function test_message_get_providers_for_user_more() {
84 global $DB;
86 $this->resetAfterTest();
88 // Create a course.
89 $course = $this->getDataGenerator()->create_course();
90 $coursecontext = context_course::instance($course->id);
92 // It would probably be better to use a quiz instance as it has capability controlled messages
93 // however mod_quiz doesn't have a data generator.
94 // Instead we're going to use backup notifications and give and take away the capability at various levels.
95 $assign = $this->getDataGenerator()->create_module('assign', array('course'=>$course->id));
96 $modulecontext = context_module::instance($assign->id);
98 // Create and enrol a teacher.
99 $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'), '*', MUST_EXIST);
100 $teacher = $this->getDataGenerator()->create_user();
101 role_assign($teacherrole->id, $teacher->id, $coursecontext);
102 $enrolplugin = enrol_get_plugin('manual');
103 $enrolplugin->add_instance($course);
104 $enrolinstances = enrol_get_instances($course->id, false);
105 foreach ($enrolinstances as $enrolinstance) {
106 if ($enrolinstance->enrol === 'manual') {
107 break;
110 $enrolplugin->enrol_user($enrolinstance, $teacher->id);
112 // Make the teacher the current user.
113 $this->setUser($teacher);
115 // Teacher shouldn't have the required capability so they shouldn't be able to see the backup message.
116 $this->assertFalse(has_capability('moodle/site:config', $modulecontext));
117 $providers = message_get_providers_for_user($teacher->id);
118 $this->assertFalse($this->message_type_present('moodle', 'backup', $providers));
120 // Give the user the required capability in an activity module.
121 // They should now be able to see the backup message.
122 assign_capability('moodle/site:config', CAP_ALLOW, $teacherrole->id, $modulecontext->id, true);
123 accesslib_clear_all_caches_for_unit_testing();
124 $modulecontext = context_module::instance($assign->id);
125 $this->assertTrue(has_capability('moodle/site:config', $modulecontext));
127 $providers = message_get_providers_for_user($teacher->id);
128 $this->assertTrue($this->message_type_present('moodle', 'backup', $providers));
130 // Prohibit the capability for the user at the course level.
131 // This overrules the CAP_ALLOW at the module level.
132 // They should not be able to see the backup message.
133 assign_capability('moodle/site:config', CAP_PROHIBIT, $teacherrole->id, $coursecontext->id, true);
134 accesslib_clear_all_caches_for_unit_testing();
135 $modulecontext = context_module::instance($assign->id);
136 $this->assertFalse(has_capability('moodle/site:config', $modulecontext));
138 $providers = message_get_providers_for_user($teacher->id);
139 // Actually, handling PROHIBITs would be too expensive. We do not
140 // care if users with PROHIBITs see a few more preferences than they should.
141 // $this->assertFalse($this->message_type_present('moodle', 'backup', $providers));
144 public function test_message_attachment_send() {
145 global $CFG;
146 $this->preventResetByRollback();
147 $this->resetAfterTest();
149 // Set config setting to allow attachments.
150 $CFG->allowattachments = true;
151 unset_config('noemailever');
153 $user = $this->getDataGenerator()->create_user();
154 $context = context_user::instance($user->id);
156 // Create a test file.
157 $fs = get_file_storage();
158 $filerecord = array(
159 'contextid' => $context->id,
160 'component' => 'core',
161 'filearea' => 'unittest',
162 'itemid' => 99999,
163 'filepath' => '/',
164 'filename' => 'emailtest.txt'
166 $file = $fs->create_file_from_string($filerecord, 'Test content');
168 $message = new stdClass();
169 $message->component = 'moodle';
170 $message->name = 'instantmessage';
171 $message->userfrom = get_admin();
172 $message->userto = $user;
173 $message->subject = 'message subject 1';
174 $message->fullmessage = 'message body';
175 $message->fullmessageformat = FORMAT_MARKDOWN;
176 $message->fullmessagehtml = '<p>message body</p>';
177 $message->smallmessage = 'small message';
178 $message->attachment = $file;
179 $message->attachname = 'emailtest.txt';
181 // Make sure we are redirecting emails.
182 $sink = $this->redirectEmails();
183 $this->assertTrue(phpunit_util::is_redirecting_phpmailer());
184 message_send($message);
186 // Get the email that we just sent.
187 $emails = $sink->get_messages();
188 $email = reset($emails);
189 $this->assertTrue(strpos($email->body, 'Content-Disposition: attachment;') !== false);
190 $this->assertTrue(strpos($email->body, 'emailtest.txt') !== false);
194 * Is a particular message type in the list of message types.
195 * @param string $component
196 * @param string $name a message name.
197 * @param array $providers as returned by message_get_providers_for_user.
198 * @return bool whether the message type is present.
200 protected function message_type_present($component, $name, $providers) {
201 foreach ($providers as $provider) {
202 if ($provider->component == $component && $provider->name == $name) {
203 return true;
206 return false;