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