Merge branch 'MDL-40255_M25' of git://github.com/lazydaisy/moodle into MOODLE_25_STABLE
[moodle.git] / lib / tests / modinfolib_test.php
blobf5f65119066c3c06517962833bad3008704386ba
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.
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.
20 * @package core
21 * @category phpunit
22 * @copyright 2012 Andrew Davis
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
34 * @copyright 2012 Andrew Davis
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class modinfolib_testcase extends advanced_testcase {
39 /**
40 * Test is_user_access_restricted_by_group()
42 * The underlying groups system is more thoroughly tested in lib/tests/grouplib_test.php
44 public function test_is_user_access_restricted_by_group() {
45 global $DB, $CFG, $USER;
47 $this->resetAfterTest(true);
49 // Create a course
50 $course = $this->getDataGenerator()->create_course();
51 $coursecontext = context_course::instance($course->id);
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];
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;
70 $enrolplugin->enrol_user($enrolinstance, $student->id);
72 // Switch to a student and reload the context info
73 $this->setUser($student);
74 $cm_info = $this->refresh_cm_info($course, $assign);
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);
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'));
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);
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());
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());
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());
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());
112 // Switch to a teacher and reload the context info
113 $this->setUser($teacher);
114 $cm_info = $this->refresh_cm_info($course, $assign);
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());
122 * Test is_user_access_restricted_by_conditional_access()
124 * The underlying conditional access system is more thoroughly tested in lib/tests/conditionlib_test.php
126 public function test_is_user_access_restricted_by_conditional_access() {
127 global $DB, $CFG;
129 $this->resetAfterTest(true);
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];
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);
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;
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());
152 // Turn on conditional availability
153 $CFG->enableavailability = true;
155 // The unavailable, hidden entirely activity should now be restricted
156 $this->assertTrue($cm_info->is_user_access_restricted_by_conditional_access());
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());
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());
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;
171 // Switch to a teacher and reload the context info
172 $this->setUser($teacher);
173 $cm_info = $this->refresh_cm_info($course, $assign);
175 $this->assertTrue(has_capability('moodle/course:viewhiddenactivities', $coursecontext));
176 $this->assertFalse($cm_info->is_user_access_restricted_by_conditional_access());
179 private function refresh_cm_info($course, $assign) {
180 get_fast_modinfo(0, 0, true);
181 return get_fast_modinfo($course)->instances['assign'][$assign->id];