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