MDL-35762 fix deprecated use of get_fast_modinfo() reset.
[moodle.git] / lib / tests / modinfolib_test.php
CommitLineData
69632d42
AD
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/>.
16
17/**
18 * Unit tests for lib/modinfolib.php.
19 *
20 * @package core
21 * @category phpunit
22 * @copyright 2012 Andrew Davis
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27global $CFG;
28require_once($CFG->libdir . '/modinfolib.php');
29require_once($CFG->libdir . '/conditionlib.php');
30
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 */
37class modinfolib_testcase extends advanced_testcase {
38
39 /**
40 * Test is_user_access_restricted_by_group()
41 *
42 * The underlying groups system is more thoroughly tested in lib/tests/grouplib_test.php
43 */
44 public function test_is_user_access_restricted_by_group() {
45 global $DB, $CFG, $USER;
46
47 $this->resetAfterTest(true);
48
49 // Create a course
50 $course = $this->getDataGenerator()->create_course();
51 $coursecontext = context_course::instance($course->id);
52
53 // Create a mod_assign instance
54 $assign = $this->getDataGenerator()->create_module('assign', array('course'=>$course->id));
55 $cm_info = get_fast_modinfo($course)->instances['assign'][$assign->id];
56
57 // Create and enrol a student
58 // Enrolment is necessary for groups to work
59 $studentrole = $DB->get_record('role', array('shortname'=>'student'), '*', MUST_EXIST);
60 $student = $this->getDataGenerator()->create_user();
61 role_assign($studentrole->id, $student->id, $coursecontext);
62 $enrolplugin = enrol_get_plugin('manual');
63 $enrolplugin->add_instance($course);
64 $enrolinstances = enrol_get_instances($course->id, false);
65 foreach ($enrolinstances as $enrolinstance) {
66 if ($enrolinstance->enrol === 'manual') {
67 break;
68 }
69 }
70 $enrolplugin->enrol_user($enrolinstance, $student->id);
71
72 // Switch to a student and reload the context info
73 $this->setUser($student);
74 $cm_info = $this->refresh_cm_info($course, $assign);
75
76 // Create up a teacher
77 $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'), '*', MUST_EXIST);
78 $teacher = $this->getDataGenerator()->create_user();
79 role_assign($teacherrole->id, $teacher->id, $coursecontext);
80
81 // Create 2 groupings
82 $grouping1 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id, 'name' => 'grouping1'));
83 $grouping2 = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id, 'name' => 'grouping2'));
84
85 // Create 2 groups and put them in the groupings
86 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id, 'idnumber' => 'group1'));
87 groups_assign_grouping($grouping1->id, $group1->id);
88 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id, 'idnumber' => 'group2'));
89 groups_assign_grouping($grouping2->id, $group2->id);
90
91 // If groups are disabled, the activity isn't restricted.
92 $CFG->enablegroupmembersonly = false;
93 $this->assertFalse($cm_info->is_user_access_restricted_by_group());
94
95 // If groups are on but "group members only" is off, the activity isn't restricted.
96 $CFG->enablegroupmembersonly = true;
97 $cm_info->groupmembersonly = NOGROUPS;
98 $this->assertFalse($cm_info->is_user_access_restricted_by_group());
99
100 // If "group members only" is on but user is in the wrong group, the activity is restricted.
101 $cm_info->groupmembersonly = SEPARATEGROUPS;
102 $cm_info->groupingid = $grouping1->id;
103 $this->assertTrue(groups_add_member($group2, $USER));
104 $this->assertTrue($cm_info->is_user_access_restricted_by_group());
105
106 // If the user is in the required group, the activity isn't restricted.
107 groups_remove_member($group2, $USER);
108 $this->assertTrue(groups_add_member($group1, $USER));
109 $cm_info = $this->refresh_cm_info($course, $assign);
110 $this->assertFalse($cm_info->is_user_access_restricted_by_group());
111
112 // Switch to a teacher and reload the context info
113 $this->setUser($teacher);
114 $cm_info = $this->refresh_cm_info($course, $assign);
115
116 // If the user isn't in the required group but has 'moodle/site:accessallgroups', the activity isn't restricted.
117 $this->assertTrue(has_capability('moodle/site:accessallgroups', $coursecontext));
118 $this->assertFalse($cm_info->is_user_access_restricted_by_group());
119 }
120
121 /**
122 * Test is_user_access_restricted_by_conditional_access()
123 *
124 * The underlying conditional access system is more thoroughly tested in lib/tests/conditionlib_test.php
125 */
126 public function test_is_user_access_restricted_by_conditional_access() {
127 global $DB, $CFG;
128
129 $this->resetAfterTest(true);
130
131 // Create a course and a mod_assign instance
132 $course = $this->getDataGenerator()->create_course();
133 $assign = $this->getDataGenerator()->create_module('assign', array('course'=>$course->id));
134 $cm_info = get_fast_modinfo($course)->instances['assign'][$assign->id];
135
136 // Set up a teacher
137 $coursecontext = context_course::instance($course->id);
138 $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'), '*', MUST_EXIST);
139 $teacher = $this->getDataGenerator()->create_user();
140 role_assign($teacherrole->id, $teacher->id, $coursecontext);
141
142 // Mark the activity as unavailable (due to unmet conditions)
143 // Testing of the code that normally turns this flag on and off is done in conditionlib_test.php
144 $cm_info->available = false;
145 // Set the activity to be hidden entirely if it is unavailable to the user
146 $cm_info->showavailability = CONDITION_STUDENTVIEW_HIDE;
147
148 // If conditional availability is disabled the activity will always be unrestricted
149 $CFG->enableavailability = false;
150 $this->assertFalse($cm_info->is_user_access_restricted_by_conditional_access());
151
152 // Turn on conditional availability
153 $CFG->enableavailability = true;
154
155 // The unavailable, hidden entirely activity should now be restricted
156 $this->assertTrue($cm_info->is_user_access_restricted_by_conditional_access());
157
158 // If the activity is available it should not be restricted
159 $cm_info->available = true;
160 $this->assertFalse($cm_info->is_user_access_restricted_by_conditional_access());
161
162 // If the activity is unavailable and set to be greyed out it should not be restricted
163 $cm_info->available = false;
164 $cm_info->showavailability = CONDITION_STUDENTVIEW_SHOW;
165 $this->assertFalse($cm_info->is_user_access_restricted_by_conditional_access());
166
167 // If the activity is unavailable and set to be hidden entirely its restricted unless user has 'moodle/course:viewhiddenactivities'
168 $cm_info->available = false;
169 $cm_info->showavailability = CONDITION_STUDENTVIEW_HIDE;
170
171 // Switch to a teacher and reload the context info
172 $this->setUser($teacher);
173 $cm_info = $this->refresh_cm_info($course, $assign);
174
175 $this->assertTrue(has_capability('moodle/course:viewhiddenactivities', $coursecontext));
176 $this->assertFalse($cm_info->is_user_access_restricted_by_conditional_access());
177 }
178
179 private function refresh_cm_info($course, $assign) {
278714a0 180 get_fast_modinfo(0, 0, true);
69632d42
AD
181 return get_fast_modinfo($course)->instances['assign'][$assign->id];
182 }
183}