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 manager. 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 global $CFG; 28 require_once($CFG->libdir . '/formslib.php'); 29 require_once (__DIR__ . '/helper_trait.php'); 30 31 /** 32 * Tests for step. 33 * 34 * @package tool_usertours 35 * @copyright 2016 Andrew Nicols <andrew@nicols.co.uk> 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 */ 38 class tool_usertours_manager_testcase extends advanced_testcase { 39 // There are shared helpers for these tests in the helper trait. 40 use tool_usertours_helper_trait; 41 42 /** 43 * @var moodle_database 44 */ 45 protected $db; 46 47 /** 48 * Setup to store the DB reference. 49 */ 50 public function setUp() { 51 global $DB; 52 53 $this->db = $DB; 54 } 55 56 /** 57 * Tear down to restore the original DB reference. 58 */ 59 public function tearDown() { 60 global $DB; 61 62 $DB = $this->db; 63 } 64 65 /** 66 * Helper to mock the database. 67 * 68 * @return moodle_database 69 */ 70 public function mock_database() { 71 global $DB; 72 73 $DB = $this->getMockBuilder('moodle_database')->getMock(); 74 75 return $DB; 76 } 77 78 /** 79 * Data provider to ensure that all modification actions require the session key. 80 * 81 * @return array 82 */ 83 public function sesskey_required_provider() { 84 $tourid = rand(1, 100); 85 $stepid = rand(1, 100); 86 87 return [ 88 'Tour removal' => [ 89 'delete_tour', 90 [$tourid], 91 ], 92 'Step removal' => [ 93 'delete_step', 94 [$stepid], 95 ], 96 'Tour visibility' => [ 97 'show_hide_tour', 98 [$tourid, true], 99 ], 100 'Move step' => [ 101 'move_step', 102 [$stepid], 103 ], 104 ]; 105 } 106 107 /** 108 * Ensure that all modification actions require the session key. 109 * 110 * @dataProvider sesskey_required_provider 111 * @param string $function The function to test 112 * @param array $arguments The arguments to pass with it 113 */ 114 public function test_sesskey_required($function, $arguments) { 115 $manager = new \tool_usertours\manager(); 116 117 $rc = new \ReflectionClass('\tool_usertours\manager'); 118 $rcm = $rc->getMethod($function); 119 $rcm->setAccessible(true); 120 121 $this->expectException('moodle_exception'); 122 $rcm->invokeArgs($manager, $arguments); 123 } 124 125 /** 126 * Data provider for test_move_tour 127 * 128 * @return array 129 */ 130 public function move_tour_provider() { 131 $alltours = [ 132 ['name' => 'Tour 1'], 133 ['name' => 'Tour 2'], 134 ['name' => 'Tour 3'], 135 ]; 136 137 return [ 138 'Move up' => [ 139 $alltours, 140 'Tour 2', 141 \tool_usertours\helper::MOVE_UP, 142 0, 143 ], 144 'Move down' => [ 145 $alltours, 146 'Tour 2', 147 \tool_usertours\helper::MOVE_DOWN, 148 2, 149 ], 150 'Move up (first)' => [ 151 $alltours, 152 'Tour 1', 153 \tool_usertours\helper::MOVE_UP, 154 0, 155 ], 156 'Move down (last)' => [ 157 $alltours, 158 'Tour 3', 159 \tool_usertours\helper::MOVE_DOWN, 160 2, 161 ], 162 ]; 163 } 164 165 /** 166 * Test moving tours (changing sortorder) 167 * 168 * @dataProvider move_tour_provider 169 * 170 * @param array $alltours 171 * @param string $movetourname 172 * @param int $direction 173 * @param int $expectedsortorder 174 * @return void 175 */ 176 public function test_move_tour($alltours, $movetourname, $direction, $expectedsortorder) { 177 global $DB; 178 179 $this->resetAfterTest(); 180 181 // Clear out existing tours so ours are the only ones, otherwise we can't predict the sortorder. 182 $DB->delete_records('tool_usertours_tours'); 183 184 foreach ($alltours as $tourconfig) { 185 $this->helper_create_tour((object) $tourconfig); 186 } 187 188 // Load our tour to move. 189 $record = $DB->get_record('tool_usertours_tours', ['name' => $movetourname]); 190 $tour = \tool_usertours\tour::load_from_record($record); 191 192 // Call protected method via reflection. 193 $class = new ReflectionClass(\tool_usertours\manager::class); 194 $method = $class->getMethod('_move_tour'); 195 $method->setAccessible(true); 196 $method->invokeArgs(null, [$tour, $direction]); 197 198 // Assert expected sortorder. 199 $this->assertEquals($expectedsortorder, $tour->get_sortorder()); 200 } 201 202 /** 203 * Data Provider for get_matching_tours tests. 204 * 205 * @return array 206 */ 207 public function get_matching_tours_provider() { 208 global $CFG; 209 210 $alltours = [ 211 [ 212 'pathmatch' => '/my/%', 213 'enabled' => false, 214 'name' => 'Failure', 215 'description' => '', 216 'configdata' => '', 217 ], 218 [ 219 'pathmatch' => '/my/%', 220 'enabled' => true, 221 'name' => 'My tour enabled', 222 'description' => '', 223 'configdata' => '', 224 ], 225 [ 226 'pathmatch' => '/my/%', 227 'enabled' => false, 228 'name' => 'Failure', 229 'description' => '', 230 'configdata' => '', 231 ], 232 [ 233 'pathmatch' => '/course/?id=%foo=bar', 234 'enabled' => false, 235 'name' => 'Failure', 236 'description' => '', 237 'configdata' => '', 238 ], 239 [ 240 'pathmatch' => '/course/?id=%foo=bar', 241 'enabled' => true, 242 'name' => 'course tour with additional params enabled', 243 'description' => '', 244 'configdata' => '', 245 ], 246 [ 247 'pathmatch' => '/course/?id=%foo=bar', 248 'enabled' => false, 249 'name' => 'Failure', 250 'description' => '', 251 'configdata' => '', 252 ], 253 [ 254 'pathmatch' => '/course/?id=%', 255 'enabled' => false, 256 'name' => 'Failure', 257 'description' => '', 258 'configdata' => '', 259 ], 260 [ 261 'pathmatch' => '/course/?id=%', 262 'enabled' => true, 263 'name' => 'course tour enabled', 264 'description' => '', 265 'configdata' => '', 266 ], 267 [ 268 'pathmatch' => '/course/?id=%', 269 'enabled' => false, 270 'name' => 'Failure', 271 'description' => '', 272 'configdata' => '', 273 ], 274 ]; 275 276 return [ 277 'No matches found' => [ 278 $alltours, 279 $CFG->wwwroot . '/some/invalid/value', 280 null, 281 ], 282 'Never return a disabled tour' => [ 283 $alltours, 284 $CFG->wwwroot . '/my/index.php', 285 'My tour enabled', 286 ], 287 'My not course' => [ 288 $alltours, 289 $CFG->wwwroot . '/my/index.php', 290 'My tour enabled', 291 ], 292 'My with params' => [ 293 $alltours, 294 $CFG->wwwroot . '/my/index.php?id=42', 295 'My tour enabled', 296 ], 297 'Course with params' => [ 298 $alltours, 299 $CFG->wwwroot . '/course/?id=42', 300 'course tour enabled', 301 ], 302 'Course with params and trailing content' => [ 303 $alltours, 304 $CFG->wwwroot . '/course/?id=42&foo=bar', 305 'course tour with additional params enabled', 306 ], 307 ]; 308 } 309 310 /** 311 * Tests for the get_matching_tours function. 312 * 313 * @dataProvider get_matching_tours_provider 314 * @param array $alltours The list of tours to insert 315 * @param string $url The URL to test 316 * @param string $expected The name of the expected matching tour 317 */ 318 public function test_get_matching_tours($alltours, $url, $expected) { 319 $this->resetAfterTest(); 320 321 foreach ($alltours as $tourconfig) { 322 $tour = $this->helper_create_tour((object) $tourconfig); 323 $this->helper_create_step((object) ['tourid' => $tour->get_id()]); 324 } 325 326 $match = \tool_usertours\manager::get_matching_tours(new moodle_url($url)); 327 if ($expected === null) { 328 $this->assertNull($match); 329 } else { 330 $this->assertNotNull($match); 331 $this->assertEquals($expected, $match->get_name()); 332 } 333 } 334 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body