MDL-41218 Object section_info calculates expensive data on demand only
[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 = $this->refresh_cm_info($course, $assign);
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         // If groups are on but "group members only" is off, the activity isn't restricted.
178         $CFG->enablegroupmembersonly = true;
179         $cm_info->groupmembersonly = NOGROUPS;
180         $this->assertFalse($cm_info->is_user_access_restricted_by_group());
182         // If "group members only" is on but user is in the wrong group, the activity is restricted.
183         $cm_info->groupmembersonly = SEPARATEGROUPS;
184         $cm_info->groupingid = $grouping1->id;
185         $this->assertTrue(groups_add_member($group2, $USER));
186         $this->assertTrue($cm_info->is_user_access_restricted_by_group());
188         // If the user is in the required group, the activity isn't restricted.
189         groups_remove_member($group2, $USER);
190         $this->assertTrue(groups_add_member($group1, $USER));
191         $cm_info = $this->refresh_cm_info($course, $assign);
192         $this->assertFalse($cm_info->is_user_access_restricted_by_group());
194         // Switch to a teacher and reload the context info.
195         $this->setUser($teacher);
196         $cm_info = $this->refresh_cm_info($course, $assign);
198         // If the user isn't in the required group but has 'moodle/site:accessallgroups', the activity isn't restricted.
199         $this->assertTrue(has_capability('moodle/site:accessallgroups', $coursecontext));
200         $this->assertFalse($cm_info->is_user_access_restricted_by_group());
201     }
203     /**
204      * Test is_user_access_restricted_by_conditional_access()
205      *
206      * The underlying conditional access system is more thoroughly tested in lib/tests/conditionlib_test.php
207      */
208     public function test_is_user_access_restricted_by_conditional_access() {
209         global $DB, $CFG;
211         $this->resetAfterTest();
213         // Create a course and a mod_assign instance.
214         $course = $this->getDataGenerator()->create_course();
215         $assign = $this->getDataGenerator()->create_module('assign', array('course'=>$course->id));
216         $cm_info = get_fast_modinfo($course)->instances['assign'][$assign->id];
218         // Set up a teacher.
219         $coursecontext = context_course::instance($course->id);
220         $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'), '*', MUST_EXIST);
221         $teacher = $this->getDataGenerator()->create_user();
222         role_assign($teacherrole->id, $teacher->id, $coursecontext);
224         // Mark the activity as unavailable (due to unmet conditions).
225         // Testing of the code that normally turns this flag on and off is done in conditionlib_test.php.
226         $cm_info->available = false;
227         // Set the activity to be hidden entirely if it is unavailable to the user.
228         $cm_info->showavailability = CONDITION_STUDENTVIEW_HIDE;
230         // If conditional availability is disabled the activity will always be unrestricted.
231         $CFG->enableavailability = false;
232         $this->assertFalse($cm_info->is_user_access_restricted_by_conditional_access());
234         // Turn on conditional availability.
235         $CFG->enableavailability = true;
237         // The unavailable, hidden entirely activity should now be restricted.
238         $this->assertTrue($cm_info->is_user_access_restricted_by_conditional_access());
240         // If the activity is available it should not be restricted.
241         $cm_info->available = true;
242         $this->assertFalse($cm_info->is_user_access_restricted_by_conditional_access());
244         // If the activity is unavailable and set to be greyed out it should not be restricted.
245         $cm_info->available = false;
246         $cm_info->showavailability = CONDITION_STUDENTVIEW_SHOW;
247         $this->assertFalse($cm_info->is_user_access_restricted_by_conditional_access());
249         // If the activity is unavailable and set to be hidden entirely its restricted unless user has 'moodle/course:viewhiddenactivities'.
250         $cm_info->available = false;
251         $cm_info->showavailability = CONDITION_STUDENTVIEW_HIDE;
253         // Switch to a teacher and reload the context info.
254         $this->setUser($teacher);
255         $cm_info = $this->refresh_cm_info($course, $assign);
257         $this->assertTrue(has_capability('moodle/course:viewhiddenactivities', $coursecontext));
258         $this->assertFalse($cm_info->is_user_access_restricted_by_conditional_access());
259     }
261     public function test_is_user_access_restricted_by_capability() {
262         global $DB;
264         $this->resetAfterTest();
266         // Create a course and a mod_assign instance.
267         $course = $this->getDataGenerator()->create_course();
268         $assign = $this->getDataGenerator()->create_module('assign', array('course'=>$course->id));
270         // Create and enrol a student.
271         $coursecontext = context_course::instance($course->id);
272         $studentrole = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
273         $student = $this->getDataGenerator()->create_user();
274         role_assign($studentrole->id, $student->id, $coursecontext);
275         $enrolplugin = enrol_get_plugin('manual');
276         $enrolinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'));
277         $enrolplugin->enrol_user($enrolinstance, $student->id);
278         $this->setUser($student);
280         // Make sure student can see the module.
281         $cm = get_fast_modinfo($course->id)->instances['assign'][$assign->id];
282         $this->assertTrue($cm->uservisible);
283         $this->assertFalse($cm->is_user_access_restricted_by_capability());
285         // Prohibit student to view mod_assign for the course.
286         role_change_permission($studentrole->id, $coursecontext, 'mod/assign:view', CAP_PROHIBIT);
287         get_fast_modinfo($course->id, 0, true);
288         $cm = get_fast_modinfo($course->id)->instances['assign'][$assign->id];
289         $this->assertFalse($cm->uservisible);
290         $this->assertTrue($cm->is_user_access_restricted_by_capability());
292         // Restore permission to student to view mod_assign for the course.
293         role_change_permission($studentrole->id, $coursecontext, 'mod/assign:view', CAP_INHERIT);
294         get_fast_modinfo($course->id, 0, true);
295         $cm = get_fast_modinfo($course->id)->instances['assign'][$assign->id];
296         $this->assertTrue($cm->uservisible);
297         $this->assertFalse($cm->is_user_access_restricted_by_capability());
299         // Prohibit student to view mod_assign for the particular module.
300         role_change_permission($studentrole->id, context_module::instance($cm->id), 'mod/assign:view', CAP_PROHIBIT);
301         get_fast_modinfo($course->id, 0, true);
302         $cm = get_fast_modinfo($course->id)->instances['assign'][$assign->id];
303         $this->assertFalse($cm->uservisible);
304         $this->assertTrue($cm->is_user_access_restricted_by_capability());
306         // Check calling get_fast_modinfo() for different user:
307         $this->setAdminUser();
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());
311         $cm = get_fast_modinfo($course->id, $student->id)->instances['assign'][$assign->id];
312         $this->assertFalse($cm->uservisible);
313         $this->assertTrue($cm->is_user_access_restricted_by_capability());
314     }
316     private function refresh_cm_info($course, $assign) {
317         get_fast_modinfo(0, 0, true);
318         return get_fast_modinfo($course)->instances['assign'][$assign->id];
319     }