Merge branch 'wip-MDL-41179-master' of git://github.com/marinaglancy/moodle
[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 */
e2262d70 37class core_modinfolib_testcase extends advanced_testcase {
69632d42
AD
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
e2262d70 47 $this->resetAfterTest();
69632d42 48
e2262d70 49 // Create a course.
69632d42
AD
50 $course = $this->getDataGenerator()->create_course();
51 $coursecontext = context_course::instance($course->id);
52
e2262d70 53 // Create a mod_assign instance.
69632d42
AD
54 $assign = $this->getDataGenerator()->create_module('assign', array('course'=>$course->id));
55 $cm_info = get_fast_modinfo($course)->instances['assign'][$assign->id];
56
e2262d70
PS
57 // Create and enrol a student.
58 // Enrolment is necessary for groups to work.
69632d42
AD
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
e2262d70 72 // Switch to a student and reload the context info.
69632d42
AD
73 $this->setUser($student);
74 $cm_info = $this->refresh_cm_info($course, $assign);
75
e2262d70 76 // Create up a teacher.
69632d42
AD
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
e2262d70 81 // Create 2 groupings.
69632d42
AD
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
e2262d70 85 // Create 2 groups and put them in the groupings.
69632d42
AD
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
e2262d70 112 // Switch to a teacher and reload the context info.
69632d42
AD
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
e2262d70 129 $this->resetAfterTest();
69632d42 130
e2262d70 131 // Create a course and a mod_assign instance.
69632d42
AD
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
e2262d70 136 // Set up a teacher.
69632d42
AD
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
e2262d70
PS
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.
69632d42 144 $cm_info->available = false;
e2262d70 145 // Set the activity to be hidden entirely if it is unavailable to the user.
69632d42
AD
146 $cm_info->showavailability = CONDITION_STUDENTVIEW_HIDE;
147
e2262d70 148 // If conditional availability is disabled the activity will always be unrestricted.
69632d42
AD
149 $CFG->enableavailability = false;
150 $this->assertFalse($cm_info->is_user_access_restricted_by_conditional_access());
151
e2262d70 152 // Turn on conditional availability.
69632d42
AD
153 $CFG->enableavailability = true;
154
e2262d70 155 // The unavailable, hidden entirely activity should now be restricted.
69632d42
AD
156 $this->assertTrue($cm_info->is_user_access_restricted_by_conditional_access());
157
e2262d70 158 // If the activity is available it should not be restricted.
69632d42
AD
159 $cm_info->available = true;
160 $this->assertFalse($cm_info->is_user_access_restricted_by_conditional_access());
161
e2262d70 162 // If the activity is unavailable and set to be greyed out it should not be restricted.
69632d42
AD
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
e2262d70 167 // If the activity is unavailable and set to be hidden entirely its restricted unless user has 'moodle/course:viewhiddenactivities'.
69632d42
AD
168 $cm_info->available = false;
169 $cm_info->showavailability = CONDITION_STUDENTVIEW_HIDE;
086f5f9e 170
e2262d70 171 // Switch to a teacher and reload the context info.
69632d42
AD
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 }
086f5f9e 178
3e439915
MG
179 public function test_is_user_access_restricted_by_capability() {
180 global $DB;
181
182 $this->resetAfterTest();
183
184 // Create a course and a mod_assign instance.
185 $course = $this->getDataGenerator()->create_course();
186 $assign = $this->getDataGenerator()->create_module('assign', array('course'=>$course->id));
187
188 // Create and enrol a student.
189 $coursecontext = context_course::instance($course->id);
190 $studentrole = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
191 $student = $this->getDataGenerator()->create_user();
192 role_assign($studentrole->id, $student->id, $coursecontext);
193 $enrolplugin = enrol_get_plugin('manual');
194 $enrolinstance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'));
195 $enrolplugin->enrol_user($enrolinstance, $student->id);
196 $this->setUser($student);
197
198 // Make sure student can see the module.
199 $cm = get_fast_modinfo($course->id)->instances['assign'][$assign->id];
200 $this->assertTrue($cm->uservisible);
201 $this->assertFalse($cm->is_user_access_restricted_by_capability());
202
203 // Prohibit student to view mod_assign for the course.
204 role_change_permission($studentrole->id, $coursecontext, 'mod/assign:view', CAP_PROHIBIT);
205 get_fast_modinfo($course->id, 0, true);
206 $cm = get_fast_modinfo($course->id)->instances['assign'][$assign->id];
207 $this->assertFalse($cm->uservisible);
208 $this->assertTrue($cm->is_user_access_restricted_by_capability());
209
210 // Restore permission to student to view mod_assign for the course.
211 role_change_permission($studentrole->id, $coursecontext, 'mod/assign:view', CAP_INHERIT);
212 get_fast_modinfo($course->id, 0, true);
213 $cm = get_fast_modinfo($course->id)->instances['assign'][$assign->id];
214 $this->assertTrue($cm->uservisible);
215 $this->assertFalse($cm->is_user_access_restricted_by_capability());
216
217 // Prohibit student to view mod_assign for the particular module.
218 role_change_permission($studentrole->id, context_module::instance($cm->id), 'mod/assign:view', CAP_PROHIBIT);
219 get_fast_modinfo($course->id, 0, true);
220 $cm = get_fast_modinfo($course->id)->instances['assign'][$assign->id];
221 $this->assertFalse($cm->uservisible);
222 $this->assertTrue($cm->is_user_access_restricted_by_capability());
223
224 // Check calling get_fast_modinfo() for different user:
225 $this->setAdminUser();
226 $cm = get_fast_modinfo($course->id)->instances['assign'][$assign->id];
227 $this->assertTrue($cm->uservisible);
228 $this->assertFalse($cm->is_user_access_restricted_by_capability());
229 $cm = get_fast_modinfo($course->id, $student->id)->instances['assign'][$assign->id];
230 $this->assertFalse($cm->uservisible);
231 $this->assertTrue($cm->is_user_access_restricted_by_capability());
232 }
233
69632d42 234 private function refresh_cm_info($course, $assign) {
278714a0 235 get_fast_modinfo(0, 0, true);
69632d42
AD
236 return get_fast_modinfo($course)->instances['assign'][$assign->id];
237 }
238}