dab7a6402e5a3efe5eb3cfd8e74f21fbe74c205a
[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  * @copyright  2012 The Open Universtiy
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
27 class messagelib_testcase extends advanced_testcase {
29     public function test_message_get_providers_for_user() {
30         global $CFG, $DB;
32         $this->resetAfterTest(true);
34         $generator = $this->getDataGenerator();
36         // Create a course category and course
37         $cat = $generator->create_category(array('parent' => 0));
38         $course = $generator->create_course(array('category' => $cat->id));
39         $quiz = $generator->create_module('quiz', array('course' => $course->id));
40         $user = $generator->create_user();
42         $coursecontext = context_course::instance($course->id);
43         $quizcontext = context_module::instance($quiz->cmid);
44         $frontpagecontext = context_course::instance(SITEID);
46         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
48         // The user is a student in a course, and has the capability for quiz
49         // confirmation emails in one quiz in that course.
50         role_assign($studentrole->id, $user->id, $coursecontext->id);
51         assign_capability('mod/quiz:emailconfirmsubmission', CAP_ALLOW, $studentrole->id, $quizcontext->id);
53         // Give this message type to the front page role.
54         assign_capability('mod/quiz:emailwarnoverdue', CAP_ALLOW, $CFG->defaultfrontpageroleid, $frontpagecontext->id);
56         $providers = message_get_providers_for_user($user->id);
57         $this->assertTrue($this->message_type_present('mod_forum', 'posts', $providers));
58         $this->assertTrue($this->message_type_present('mod_quiz', 'confirmation', $providers));
59         $this->assertTrue($this->message_type_present('mod_quiz', 'attempt_overdue', $providers));
60         $this->assertFalse($this->message_type_present('mod_quiz', 'submission', $providers));
62         // A user is a student in a different course, they should not get confirmation.
63         $course2 = $generator->create_course(array('category' => $cat->id));
64         $user2 = $generator->create_user();
65         $coursecontext2 = context_course::instance($course2->id);
66         role_assign($studentrole->id, $user2->id, $coursecontext2->id);
67         accesslib_clear_all_caches_for_unit_testing();
68         $providers = message_get_providers_for_user($user2->id);
69         $this->assertTrue($this->message_type_present('mod_forum', 'posts', $providers));
70         $this->assertFalse($this->message_type_present('mod_quiz', 'confirmation', $providers));
72         // Now remove the frontpage role id, and attempt_overdue message should go away.
73         unset_config('defaultfrontpageroleid');
74         accesslib_clear_all_caches_for_unit_testing();
76         $providers = message_get_providers_for_user($user->id);
77         $this->assertTrue($this->message_type_present('mod_quiz', 'confirmation', $providers));
78         $this->assertFalse($this->message_type_present('mod_quiz', 'attempt_overdue', $providers));
79         $this->assertFalse($this->message_type_present('mod_quiz', 'submission', $providers));
80     }
82     public function test_message_get_providers_for_user_more() {
83         global $DB;
85         $this->resetAfterTest(true);
87         // Create a course
88         $course = $this->getDataGenerator()->create_course();
89         $coursecontext = context_course::instance($course->id);
91         // It would probably be better to use a quiz instance as it has capability controlled messages
92         // however mod_quiz doesn't have a data generator
93         // Instead we're going to use backup notifications and give and take away the capability at various levels
94         $assignment = $this->getDataGenerator()->create_module('assignment', array('course'=>$course->id));
95         $modulecontext = context_module::instance($assignment->id);
97         // Create and enrol a teacher
98         $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'), '*', MUST_EXIST);
99         $teacher = $this->getDataGenerator()->create_user();
100         role_assign($teacherrole->id, $teacher->id, $coursecontext);
101         $enrolplugin = enrol_get_plugin('manual');
102         $enrolplugin->add_instance($course);
103         $enrolinstances = enrol_get_instances($course->id, false);
104         foreach ($enrolinstances as $enrolinstance) {
105             if ($enrolinstance->enrol === 'manual') {
106                 break;
107             }
108         }
109         $enrolplugin->enrol_user($enrolinstance, $teacher->id);
111         // Make the teacher the current user
112         $this->setUser($teacher);
114         // Teacher shouldn't have the required capability so they shouldn't be able to see the backup message
115         $this->assertFalse(has_capability('moodle/site:config', $modulecontext));
116         $providers = message_get_providers_for_user($teacher->id);
117         $this->assertFalse($this->message_type_present('moodle', 'backup', $providers));
119         // Give the user the required capability in an activity module
120         // They should now be able to see the backup message
121         assign_capability('moodle/site:config', CAP_ALLOW, $teacherrole->id, $modulecontext->id, true);
122         accesslib_clear_all_caches_for_unit_testing();
123         $modulecontext = context_module::instance($assignment->id);
124         $this->assertTrue(has_capability('moodle/site:config', $modulecontext));
126         $providers = message_get_providers_for_user($teacher->id);
127         $this->assertTrue($this->message_type_present('moodle', 'backup', $providers));
129         // Prohibit the capability for the user at the course level
130         // This overrules the CAP_ALLOW at the module level
131         // They should not be able to see the backup message
132         assign_capability('moodle/site:config', CAP_PROHIBIT, $teacherrole->id, $coursecontext->id, true);
133         accesslib_clear_all_caches_for_unit_testing();
134         $modulecontext = context_module::instance($assignment->id);
135         $this->assertFalse(has_capability('moodle/site:config', $modulecontext));
137         $providers = message_get_providers_for_user($teacher->id);
138         // Actually, handling PROHIBITs would be too expensive. We do not
139         // care if users with PROHIBITs see a few more preferences than they should.
140         // $this->assertFalse($this->message_type_present('moodle', 'backup', $providers));
141     }
143     /**
144      * Is a particular message type in the list of message types.
145      * @param string $name a message name.
146      * @param array $providers as returned by message_get_providers_for_user.
147      * @return bool whether the message type is present.
148      */
149     protected function message_type_present($component, $name, $providers) {
150         foreach ($providers as $provider) {
151             if ($provider->component == $component && $provider->name == $name) {
152                 return true;
153             }
154         }
155         return false;
156     }