05405305f4c582950a2fb75918ef27f68ba8cf18
[moodle.git] / lib / tests / modinfolib_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  * Unit tests for lib/modinfolib.php.
19  *
20  * @package    core
21  * @category   phpunit
22  * @copyright  2012 Andrew Davis
23  */
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->libdir . '/modinfolib.php');
29 require_once($CFG->libdir . '/conditionlib.php');
31 /**
32  * Unit tests for modinfolib.php
33  *
34  * @copyright 2012 Andrew Davis
35  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36  */
37 class core_modinfolib_testcase extends advanced_testcase {
38     public function test_section_info_properties() {
39         global $DB, $CFG;
41         $this->resetAfterTest();
42         $oldcfgenableavailability = $CFG->enableavailability;
43         $oldcfgenablecompletion = $CFG->enablecompletion;
44         set_config('enableavailability', 1);
45         set_config('enablecompletion', 1);
46         $this->setAdminUser();
48         // Generate the course and pre-requisite module.
49         $course = $this->getDataGenerator()->create_course(
50                 array('format' => 'topics',
51                     'numsections' => 3,
52                     'enablecompletion' => 1,
53                     'groupmode' => SEPARATEGROUPS,
54                     'forcegroupmode' => 0),
55                 array('createsections' => true));
56         $coursecontext = context_course::instance($course->id);
57         $prereqforum = $this->getDataGenerator()->create_module('forum',
58                 array('course' => $course->id),
59                 array('completion' => 1));
61         // Generate the module and add availability conditions.
62         $conditionscompletion = array($prereqforum->cmid => COMPLETION_COMPLETE);
63         $conditionsgrade = array(666 => (object)array('min' => 0.4, 'max' => null, 'name' => '!missing'));
64         $conditionsfield = array('email' => (object)array(
65             'fieldname' => 'email',
66             'operator' => 'contains',
67             'value' => 'test'
68         ));
69         $sectiondb = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 2));
70         $ci = new condition_info_section((object)array('id' => $sectiondb->id), CONDITION_MISSING_EVERYTHING);
71         foreach ($conditionscompletion as $cmid => $requiredcompletion) {
72             $ci->add_completion_condition($cmid, $requiredcompletion);
73         }
74         foreach ($conditionsgrade as $gradeid => $conditiongrade) {
75             $ci->add_grade_condition($gradeid, $conditiongrade->min, $conditiongrade->max, true);
76         }
77         foreach ($conditionsfield as $conditionfield) {
78             $ci->add_user_field_condition($conditionfield->fieldname, $conditionfield->operator, $conditionfield->value);
79         }
81         // Create and enrol a student.
82         $studentrole = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
83         $student = $this->getDataGenerator()->create_user();
84         role_assign($studentrole->id, $student->id, $coursecontext);
85         $enrolplugin = enrol_get_plugin('manual');
86         $enrolinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'));
87         $enrolplugin->enrol_user($enrolinstance, $student->id);
88         $this->setUser($student);
90         // Get modinfo.
91         $modinfo = get_fast_modinfo($course->id);
92         $si = $modinfo->get_section_info(2);
94         $this->assertEquals($sectiondb->id, $si->id);
95         $this->assertEquals($sectiondb->course, $si->course);
96         $this->assertEquals($sectiondb->section, $si->section);
97         $this->assertEquals($sectiondb->name, $si->name);
98         $this->assertEquals($sectiondb->visible, $si->visible);
99         $this->assertEquals($sectiondb->summary, $si->summary);
100         $this->assertEquals($sectiondb->summaryformat, $si->summaryformat);
101         $this->assertEquals($sectiondb->showavailability, $si->showavailability);
102         $this->assertEquals($sectiondb->availablefrom, $si->availablefrom);
103         $this->assertEquals($sectiondb->availableuntil, $si->availableuntil);
104         $this->assertEquals($sectiondb->groupingid, $si->groupingid);
105         $this->assertEquals($sectiondb->sequence, $si->sequence); // Since this section does not contain invalid modules.
106         $this->assertEquals($conditionscompletion, $si->conditionscompletion);
107         $this->assertEquals($conditionsgrade, $si->conditionsgrade);
108         $this->assertEquals($conditionsfield, $si->conditionsfield);
110         // Dynamic fields, just test that they can be retrieved (must be carefully tested in each activity type).
111         $this->assertEquals(0, $si->available);
112         $this->assertNotEmpty($si->availableinfo); // Lists all unmet availability conditions.
113         $this->assertEquals(0, $si->uservisible);
115         // Restore settings.
116         set_config('enableavailability', $oldcfgenableavailability);
117         set_config('enablecompletion', $oldcfgenablecompletion);
118     }
121     /**
122      * Test is_user_access_restricted_by_group()
123      *
124      * The underlying groups system is more thoroughly tested in lib/tests/grouplib_test.php
125      */
126     public function test_is_user_access_restricted_by_group() {
127         global $DB, $CFG, $USER;
129         $this->resetAfterTest();
131         // Create a course.
132         $course = $this->getDataGenerator()->create_course();
133         $coursecontext = context_course::instance($course->id);
135         // Create a mod_assign instance.
136         $assign = $this->getDataGenerator()->create_module('assign', array('course'=>$course->id));
137         $cm_info = get_fast_modinfo($course)->instances['assign'][$assign->id];
139         // Create and enrol a student.
140         // Enrolment is necessary for groups to work.
141         $studentrole = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
142         $student = $this->getDataGenerator()->create_user();
143         role_assign($studentrole->id, $student->id, $coursecontext);
144         $enrolplugin = enrol_get_plugin('manual');
145         $enrolplugin->add_instance($course);
146         $enrolinstances = enrol_get_instances($course->id, false);
147         foreach ($enrolinstances as $enrolinstance) {
148             if ($enrolinstance->enrol === 'manual') {
149                 break;
150             }
151         }
152         $enrolplugin->enrol_user($enrolinstance, $student->id);
154         // Switch to a student and reload the context info.
155         $this->setUser($student);
156         $cm_info = get_fast_modinfo($course)->instances['assign'][$assign->id];
158         // Create up a teacher.
159         $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'), '*', MUST_EXIST);
160         $teacher = $this->getDataGenerator()->create_user();
161         role_assign($teacherrole->id, $teacher->id, $coursecontext);
163         // Create 2 groupings.
164         $grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id, 'name' => 'grouping1'));
165         $grouping2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id, 'name' => 'grouping2'));
167         // Create 2 groups and put them in the groupings.
168         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id, 'idnumber' => 'group1'));
169         groups_assign_grouping($grouping1->id, $group1->id);
170         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id, 'idnumber' => 'group2'));
171         groups_assign_grouping($grouping2->id, $group2->id);
173         // If groups are disabled, the activity isn't restricted.
174         $CFG->enablegroupmembersonly = false;
175         $this->assertFalse($cm_info->is_user_access_restricted_by_group());
177         // Turn groups setting on.
178         $CFG->enablegroupmembersonly = true;
179         // Create a mod_assign instance with "group members only", the activity should not be restricted.
180         $assignnogroups = $this->getDataGenerator()->create_module('assign', array('course'=>$course->id),
181             array('groupmembersonly' => NOGROUPS));
182         $cm_info = get_fast_modinfo($course->id)->instances['assign'][$assignnogroups->id];
183         $this->assertFalse($cm_info->is_user_access_restricted_by_group());
185         // If "group members only" is on but user is in the wrong group, the activity is restricted.
186         $assignsepgroups = $this->getDataGenerator()->create_module('assign', array('course'=>$course->id),
187             array('groupmembersonly' => SEPARATEGROUPS, 'groupingid' => $grouping1->id));
188         $this->assertTrue(groups_add_member($group2, $USER));
189         get_fast_modinfo($course->id, 0, true);
190         $cm_info = get_fast_modinfo($course->id)->instances['assign'][$assignsepgroups->id];
191         $this->assertEquals($grouping1->id, $cm_info->groupingid);
192         $this->assertTrue($cm_info->is_user_access_restricted_by_group());
194         // If the user is in the required group, the activity isn't restricted.
195         groups_remove_member($group2, $USER);
196         $this->assertTrue(groups_add_member($group1, $USER));
197         get_fast_modinfo($course->id, 0, true);
198         $cm_info = get_fast_modinfo($course->id)->instances['assign'][$assignsepgroups->id];
199         $this->assertFalse($cm_info->is_user_access_restricted_by_group());
201         // Switch to a teacher and reload the context info.
202         $this->setUser($teacher);
203         $cm_info = get_fast_modinfo($course->id)->instances['assign'][$assignsepgroups->id];
205         // If the user isn't in the required group but has 'moodle/site:accessallgroups', the activity isn't restricted.
206         $this->assertTrue(has_capability('moodle/site:accessallgroups', $coursecontext));
207         $this->assertFalse($cm_info->is_user_access_restricted_by_group());
208     }
210     /**
211      * Test is_user_access_restricted_by_conditional_access()
212      *
213      * The underlying conditional access system is more thoroughly tested in lib/tests/conditionlib_test.php
214      */
215     public function test_is_user_access_restricted_by_conditional_access() {
216         global $DB, $CFG;
218         $this->resetAfterTest();
220         // Create a course.
221         $course = $this->getDataGenerator()->create_course();
222         // 1. Create an activity that is currently unavailable and hidden entirely (for students).
223         $assign1 = $this->getDataGenerator()->create_module('assign', array('course'=>$course->id),
224                 array('availablefrom' => time() + 10000, 'showavailability' => CONDITION_STUDENTVIEW_HIDE));
225         // 2. Create an activity that is currently available.
226         $assign2 = $this->getDataGenerator()->create_module('assign', array('course'=>$course->id));
227         // 3. Create an activity that is currently unavailable and set to be greyed out.
228         $assign3 = $this->getDataGenerator()->create_module('assign', array('course'=>$course->id),
229                 array('availablefrom' => time() + 10000, 'showavailability' => CONDITION_STUDENTVIEW_SHOW));
231         // Set up a teacher.
232         $coursecontext = context_course::instance($course->id);
233         $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'), '*', MUST_EXIST);
234         $teacher = $this->getDataGenerator()->create_user();
235         role_assign($teacherrole->id, $teacher->id, $coursecontext);
237         // If conditional availability is disabled the activity will always be unrestricted.
238         $CFG->enableavailability = false;
239         $cm_info = get_fast_modinfo($course)->instances['assign'][$assign1->id];
240         $this->assertFalse($cm_info->is_user_access_restricted_by_conditional_access());
242         // Turn on conditional availability and reset the get_fast_modinfo cache.
243         $CFG->enableavailability = true;
244         get_fast_modinfo($course, 0, true);
246         // The unavailable, hidden entirely activity should now be restricted.
247         $cm_info = get_fast_modinfo($course)->instances['assign'][$assign1->id];
248         $this->assertFalse($cm_info->available);
249         $this->assertEquals(CONDITION_STUDENTVIEW_HIDE, $cm_info->showavailability);
250         $this->assertTrue($cm_info->is_user_access_restricted_by_conditional_access());
252         // If the activity is available it should not be restricted.
253         $cm_info = get_fast_modinfo($course)->instances['assign'][$assign2->id];
254         $this->assertTrue($cm_info->available);
255         $this->assertFalse($cm_info->is_user_access_restricted_by_conditional_access());
257         // If the activity is unavailable and set to be greyed out it should not be restricted.
258         $cm_info = get_fast_modinfo($course)->instances['assign'][$assign3->id];
259         $this->assertFalse($cm_info->available);
260         $this->assertEquals(CONDITION_STUDENTVIEW_SHOW, $cm_info->showavailability);
261         $this->assertFalse($cm_info->is_user_access_restricted_by_conditional_access());
263         // If the activity is unavailable and set to be hidden entirely its restricted unless user has 'moodle/course:viewhiddenactivities'.
264         // Switch to a teacher and reload the context info.
265         $this->setUser($teacher);
266         $cm_info = get_fast_modinfo($course)->instances['assign'][$assign1->id];
267         $this->assertFalse($cm_info->available);
268         $this->assertEquals(CONDITION_STUDENTVIEW_HIDE, $cm_info->showavailability);
270         $this->assertTrue(has_capability('moodle/course:viewhiddenactivities', $coursecontext));
271         $this->assertFalse($cm_info->is_user_access_restricted_by_conditional_access());
272     }
274     public function test_is_user_access_restricted_by_capability() {
275         global $DB;
277         $this->resetAfterTest();
279         // Create a course and a mod_assign instance.
280         $course = $this->getDataGenerator()->create_course();
281         $assign = $this->getDataGenerator()->create_module('assign', array('course'=>$course->id));
283         // Create and enrol a student.
284         $coursecontext = context_course::instance($course->id);
285         $studentrole = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
286         $student = $this->getDataGenerator()->create_user();
287         role_assign($studentrole->id, $student->id, $coursecontext);
288         $enrolplugin = enrol_get_plugin('manual');
289         $enrolinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'));
290         $enrolplugin->enrol_user($enrolinstance, $student->id);
291         $this->setUser($student);
293         // Make sure student can see the module.
294         $cm = get_fast_modinfo($course->id)->instances['assign'][$assign->id];
295         $this->assertTrue($cm->uservisible);
296         $this->assertFalse($cm->is_user_access_restricted_by_capability());
298         // Prohibit student to view mod_assign for the course.
299         role_change_permission($studentrole->id, $coursecontext, 'mod/assign:view', CAP_PROHIBIT);
300         get_fast_modinfo($course->id, 0, true);
301         $cm = get_fast_modinfo($course->id)->instances['assign'][$assign->id];
302         $this->assertFalse($cm->uservisible);
303         $this->assertTrue($cm->is_user_access_restricted_by_capability());
305         // Restore permission to student to view mod_assign for the course.
306         role_change_permission($studentrole->id, $coursecontext, 'mod/assign:view', CAP_INHERIT);
307         get_fast_modinfo($course->id, 0, true);
308         $cm = get_fast_modinfo($course->id)->instances['assign'][$assign->id];
309         $this->assertTrue($cm->uservisible);
310         $this->assertFalse($cm->is_user_access_restricted_by_capability());
312         // Prohibit student to view mod_assign for the particular module.
313         role_change_permission($studentrole->id, context_module::instance($cm->id), 'mod/assign:view', CAP_PROHIBIT);
314         get_fast_modinfo($course->id, 0, true);
315         $cm = get_fast_modinfo($course->id)->instances['assign'][$assign->id];
316         $this->assertFalse($cm->uservisible);
317         $this->assertTrue($cm->is_user_access_restricted_by_capability());
319         // Check calling get_fast_modinfo() for different user:
320         $this->setAdminUser();
321         $cm = get_fast_modinfo($course->id)->instances['assign'][$assign->id];
322         $this->assertTrue($cm->uservisible);
323         $this->assertFalse($cm->is_user_access_restricted_by_capability());
324         $cm = get_fast_modinfo($course->id, $student->id)->instances['assign'][$assign->id];
325         $this->assertFalse($cm->uservisible);
326         $this->assertTrue($cm->is_user_access_restricted_by_capability());
327     }