aeb30fc8b53a1f80f6ea9296ed4f463ad7e7fe3e
[moodle.git] / lib / tests / messagelib_test.php
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/>.
17 /**
18  * Tests for messagelib.php.
19  *
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
24  */
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));
81     }
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;
108             }
109         }
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));
142     }
144     /**
145      * Is a particular message type in the list of message types.
146      * @param string $component
147      * @param string $name a message name.
148      * @param array $providers as returned by message_get_providers_for_user.
149      * @return bool whether the message type is present.
150      */
151     protected function message_type_present($component, $name, $providers) {
152         foreach ($providers as $provider) {
153             if ($provider->component == $component && $provider->name == $name) {
154                 return true;
155             }
156         }
157         return false;
158     }