See Release Notes
Long Term Support Release
Differences Between: [Versions 310 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 and 403]
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 namespace tool_usertours; 18 19 /** 20 * Tests for role filter. 21 * 22 * @package tool_usertours 23 * @copyright 2016 Andrew Nicols <andrew@nicols.co.uk> 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 25 */ 26 class role_filter_test extends \advanced_testcase { 27 28 /** 29 * @var $course Test course 30 */ 31 protected $course; 32 33 /** 34 * @var $student Test student 35 */ 36 protected $student; 37 38 /** 39 * @var $teacher Test teacher 40 */ 41 protected $teacher; 42 43 /** 44 * @var $editingteacher Test editor 45 */ 46 protected $editingteacher; 47 48 /** 49 * @var $roles List of all roles 50 */ 51 protected $roles; 52 53 public function setUp(): void { 54 global $DB; 55 56 $this->resetAfterTest(true); 57 $generator = $this->getDataGenerator(); 58 59 $this->course = $generator->create_course(); 60 $this->roles = $DB->get_records_menu('role', [], null, 'shortname, id'); 61 $this->testroles = ['student', 'teacher', 'editingteacher']; 62 63 foreach ($this->testroles as $role) { 64 $user = $this->$role = $generator->create_user(); 65 $generator->enrol_user($user->id, $this->course->id, $this->roles[$role]); 66 } 67 } 68 69 /** 70 * Test the filter_matches function when any is set. 71 */ 72 public function test_filter_matches_any() { 73 $context = \context_course::instance($this->course->id); 74 75 // Note: No need to persist this tour. 76 $tour = new \tool_usertours\tour(); 77 $tour->set_filter_values('role', []); 78 79 // Note: The role filter does not use the context. 80 foreach ($this->testroles as $role) { 81 $this->setUser($this->$role); 82 $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context)); 83 } 84 85 // The admin should always be able to view too. 86 $this->setAdminUser(); 87 $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context)); 88 } 89 90 /** 91 * Test the filter_matches function when one role is set. 92 */ 93 public function test_filter_matches_single_role() { 94 $context = \context_course::instance($this->course->id); 95 96 $roles = [ 97 'student', 98 ]; 99 100 // Note: No need to persist this tour. 101 $tour = new \tool_usertours\tour(); 102 $tour->set_filter_values('role', $roles); 103 104 // Note: The role filter does not use the context. 105 foreach ($this->testroles as $role) { 106 $this->setUser($this->$role); 107 if ($role === 'student') { 108 $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context)); 109 } else { 110 $this->assertFalse(\tool_usertours\local\filter\role::filter_matches($tour, $context)); 111 } 112 } 113 114 // The admin can't view this one either. 115 $this->setAdminUser(); 116 $this->assertFalse(\tool_usertours\local\filter\role::filter_matches($tour, $context)); 117 } 118 119 /** 120 * Test the filter_matches function when multiple roles are set. 121 */ 122 public function test_filter_matches_multiple_role() { 123 $context = \context_course::instance($this->course->id); 124 125 $roles = [ 126 'teacher', 127 'editingteacher', 128 ]; 129 130 // Note: No need to persist this tour. 131 $tour = new \tool_usertours\tour(); 132 $tour->set_filter_values('role', $roles); 133 134 // Note: The role filter does not use the context. 135 foreach ($this->testroles as $role) { 136 $this->setUser($this->$role); 137 if ($role === 'student') { 138 $this->assertFalse(\tool_usertours\local\filter\role::filter_matches($tour, $context)); 139 } else { 140 $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context)); 141 } 142 } 143 144 // The admin can't view this one either. 145 $this->setAdminUser(); 146 $this->assertFalse(\tool_usertours\local\filter\role::filter_matches($tour, $context)); 147 } 148 149 /** 150 * Test the filter_matches function when one user has multiple roles. 151 */ 152 public function test_filter_matches_multiple_role_one_user() { 153 $context = \context_course::instance($this->course->id); 154 155 $roles = [ 156 'student', 157 ]; 158 159 $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->roles['teacher']); 160 161 // Note: No need to persist this tour. 162 $tour = new \tool_usertours\tour(); 163 $tour->set_filter_values('role', $roles); 164 165 166 // Note: The role filter does not use the context. 167 foreach ($this->testroles as $role) { 168 $this->setUser($this->$role); 169 if ($role === 'student') { 170 $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context)); 171 } else { 172 $this->assertFalse(\tool_usertours\local\filter\role::filter_matches($tour, $context)); 173 } 174 } 175 176 // The admin can't view this one either. 177 $this->setAdminUser(); 178 $this->assertFalse(\tool_usertours\local\filter\role::filter_matches($tour, $context)); 179 } 180 181 /** 182 * Test the filter_matches function when it is targetted at an admin. 183 */ 184 public function test_filter_matches_multiple_role_only_admin() { 185 $context = \context_course::instance($this->course->id); 186 187 $roles = [ 188 \tool_usertours\local\filter\role::ROLE_SITEADMIN, 189 ]; 190 191 $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->roles['teacher']); 192 193 // Note: No need to persist this tour. 194 $tour = new \tool_usertours\tour(); 195 $tour->set_filter_values('role', $roles); 196 197 198 // Note: The role filter does not use the context. 199 foreach ($this->testroles as $role) { 200 $this->setUser($this->$role); 201 $this->assertFalse(\tool_usertours\local\filter\role::filter_matches($tour, $context)); 202 } 203 204 // The admin can view this one because it's only aimed at them. 205 $this->setAdminUser(); 206 $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context)); 207 } 208 209 /** 210 * Test the filter_matches function when multiple roles are set, including an admin user. 211 */ 212 public function test_filter_matches_multiple_role_including_admin() { 213 $context = \context_course::instance($this->course->id); 214 215 $roles = [ 216 \tool_usertours\local\filter\role::ROLE_SITEADMIN, 217 'teacher', 218 'editingteacher', 219 ]; 220 221 // Note: No need to persist this tour. 222 $tour = new \tool_usertours\tour(); 223 $tour->set_filter_values('role', $roles); 224 225 // Note: The role filter does not use the context. 226 foreach ($this->testroles as $role) { 227 $this->setUser($this->$role); 228 if ($role === 'student') { 229 $this->assertFalse(\tool_usertours\local\filter\role::filter_matches($tour, $context)); 230 } else { 231 $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context)); 232 } 233 } 234 235 // The admin can view this one because it's only aimed at them. 236 $this->setAdminUser(); 237 $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context)); 238 } 239 240 /** 241 * Test the filter_matches function when an admin user has multiple roles. 242 */ 243 public function test_filter_matches_multiple_role_admin_user() { 244 global $USER; 245 246 $context = \context_course::instance($this->course->id); 247 248 $roles = [ 249 \tool_usertours\local\filter\role::ROLE_SITEADMIN, 250 ]; 251 252 $this->setAdminUser(); 253 $this->getDataGenerator()->enrol_user($USER->id, $this->course->id, $this->roles['student']); 254 255 // Note: No need to persist this tour. 256 $tour = new \tool_usertours\tour(); 257 $tour->set_filter_values('role', $roles); 258 259 // The admin can view this one because it's only aimed at them. 260 $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context)); 261 } 262 263 /** 264 * Test that the get_filter_options function does not include the guest roles. 265 */ 266 public function test_get_filter_options_no_guest_roles() { 267 create_role('Test Role', 'testrole', 'This is a test role', 'guest'); 268 269 $allroles = role_get_names(null, ROLENAME_ALIAS); 270 $options = \tool_usertours\local\filter\role::get_filter_options(); 271 272 foreach ($allroles as $role) { 273 $hasrole = isset($options[$role->shortname]); 274 if ($role->archetype === 'guest') { 275 $this->assertFalse($hasrole); 276 } else { 277 $this->assertTrue($hasrole); 278 } 279 } 280 } 281 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body