See Release Notes
Long Term Support Release
Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]
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 core; 18 19 /** 20 * Tests for moodle_url. 21 * 22 * @package core 23 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk> 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 25 * @covers \moodle_url 26 */ 27 class moodle_url_test extends \advanced_testcase { 28 /** 29 * Test basic moodle_url construction. 30 */ 31 public function test_moodle_url_constructor() { 32 global $CFG; 33 34 $url = new \moodle_url('/index.php'); 35 $this->assertSame($CFG->wwwroot.'/index.php', $url->out()); 36 37 $url = new \moodle_url('/index.php', array()); 38 $this->assertSame($CFG->wwwroot.'/index.php', $url->out()); 39 40 $url = new \moodle_url('/index.php', array('id' => 2)); 41 $this->assertSame($CFG->wwwroot.'/index.php?id=2', $url->out()); 42 43 $url = new \moodle_url('/index.php', array('id' => 'two')); 44 $this->assertSame($CFG->wwwroot.'/index.php?id=two', $url->out()); 45 46 $url = new \moodle_url('/index.php', array('id' => 1, 'cid' => '2')); 47 $this->assertSame($CFG->wwwroot.'/index.php?id=1&cid=2', $url->out()); 48 $this->assertSame($CFG->wwwroot.'/index.php?id=1&cid=2', $url->out(false)); 49 50 $url = new \moodle_url('/index.php', null, 'test'); 51 $this->assertSame($CFG->wwwroot.'/index.php#test', $url->out()); 52 53 $url = new \moodle_url('/index.php', array('id' => 2), 'test'); 54 $this->assertSame($CFG->wwwroot.'/index.php?id=2#test', $url->out()); 55 } 56 57 /** 58 * Tests \moodle_url::get_path(). 59 */ 60 public function test_moodle_url_get_path() { 61 $url = new \moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1'); 62 $this->assertSame('/my/file/is/here.txt', $url->get_path()); 63 64 $url = new \moodle_url('http://www.example.org/'); 65 $this->assertSame('/', $url->get_path()); 66 67 $url = new \moodle_url('http://www.example.org/pluginfile.php/slash/arguments'); 68 $this->assertSame('/pluginfile.php/slash/arguments', $url->get_path()); 69 $this->assertSame('/pluginfile.php', $url->get_path(false)); 70 } 71 72 public function test_moodle_url_round_trip() { 73 $strurl = 'http://moodle.org/course/view.php?id=5'; 74 $url = new \moodle_url($strurl); 75 $this->assertSame($strurl, $url->out(false)); 76 77 $strurl = 'http://moodle.org/user/index.php?contextid=53&sifirst=M&silast=D'; 78 $url = new \moodle_url($strurl); 79 $this->assertSame($strurl, $url->out(false)); 80 } 81 82 /** 83 * Test Moodle URL objects created with a param with empty value. 84 */ 85 public function test_moodle_url_empty_param_values() { 86 $strurl = 'http://moodle.org/course/view.php?id=0'; 87 $url = new \moodle_url($strurl, array('id' => 0)); 88 $this->assertSame($strurl, $url->out(false)); 89 90 $strurl = 'http://moodle.org/course/view.php?id'; 91 $url = new \moodle_url($strurl, array('id' => false)); 92 $this->assertSame($strurl, $url->out(false)); 93 94 $strurl = 'http://moodle.org/course/view.php?id'; 95 $url = new \moodle_url($strurl, array('id' => null)); 96 $this->assertSame($strurl, $url->out(false)); 97 98 $strurl = 'http://moodle.org/course/view.php?id'; 99 $url = new \moodle_url($strurl, array('id' => '')); 100 $this->assertSame($strurl, $url->out(false)); 101 102 $strurl = 'http://moodle.org/course/view.php?id'; 103 $url = new \moodle_url($strurl); 104 $this->assertSame($strurl, $url->out(false)); 105 } 106 107 /** 108 * Test set good scheme on Moodle URL objects. 109 */ 110 public function test_moodle_url_set_good_scheme() { 111 $url = new \moodle_url('http://moodle.org/foo/bar'); 112 $url->set_scheme('myscheme'); 113 $this->assertSame('myscheme://moodle.org/foo/bar', $url->out()); 114 } 115 116 /** 117 * Test set bad scheme on Moodle URL objects. 118 */ 119 public function test_moodle_url_set_bad_scheme() { 120 $url = new \moodle_url('http://moodle.org/foo/bar'); 121 $this->expectException(\coding_exception::class); 122 $url->set_scheme('not a valid $ scheme'); 123 } 124 125 public function test_moodle_url_round_trip_array_params() { 126 $strurl = 'http://example.com/?a%5B1%5D=1&a%5B2%5D=2'; 127 $url = new \moodle_url($strurl); 128 $this->assertSame($strurl, $url->out(false)); 129 130 $url = new \moodle_url('http://example.com/?a[1]=1&a[2]=2'); 131 $this->assertSame($strurl, $url->out(false)); 132 133 // For un-keyed array params, we expect 0..n keys to be returned. 134 $strurl = 'http://example.com/?a%5B0%5D=0&a%5B1%5D=1'; 135 $url = new \moodle_url('http://example.com/?a[]=0&a[]=1'); 136 $this->assertSame($strurl, $url->out(false)); 137 } 138 139 public function test_compare_url() { 140 $url1 = new \moodle_url('index.php', array('var1' => 1, 'var2' => 2)); 141 $url2 = new \moodle_url('index2.php', array('var1' => 1, 'var2' => 2, 'var3' => 3)); 142 143 $this->assertFalse($url1->compare($url2, URL_MATCH_BASE)); 144 $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS)); 145 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT)); 146 147 $url2 = new \moodle_url('index.php', array('var1' => 1, 'var3' => 3)); 148 149 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE)); 150 $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS)); 151 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT)); 152 153 $url2 = new \moodle_url('index.php', array('var1' => 1, 'var2' => 2, 'var3' => 3)); 154 155 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE)); 156 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS)); 157 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT)); 158 159 $url2 = new \moodle_url('index.php', array('var2' => 2, 'var1' => 1)); 160 161 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE)); 162 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS)); 163 $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT)); 164 165 $url1->set_anchor('test'); 166 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE)); 167 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS)); 168 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT)); 169 170 $url2->set_anchor('test'); 171 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE)); 172 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS)); 173 $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT)); 174 } 175 176 public function test_out_as_local_url() { 177 global $CFG; 178 // Test http url. 179 $url1 = new \moodle_url('/lib/tests/weblib_test.php'); 180 $this->assertSame('/lib/tests/weblib_test.php', $url1->out_as_local_url()); 181 182 // Test https url. 183 $httpswwwroot = str_replace("http://", "https://", $CFG->wwwroot); 184 $url2 = new \moodle_url($httpswwwroot.'/login/profile.php'); 185 $this->assertSame('/login/profile.php', $url2->out_as_local_url()); 186 187 // Test http url matching wwwroot. 188 $url3 = new \moodle_url($CFG->wwwroot); 189 $this->assertSame('', $url3->out_as_local_url()); 190 191 // Test http url matching wwwroot ending with slash (/). 192 $url3 = new \moodle_url($CFG->wwwroot.'/'); 193 $this->assertSame('/', $url3->out_as_local_url()); 194 } 195 196 public function test_out_as_local_url_error() { 197 $url2 = new \moodle_url('http://www.google.com/lib/tests/weblib_test.php'); 198 $this->expectException(\coding_exception::class); 199 $url2->out_as_local_url(); 200 } 201 202 /** 203 * You should get error with modified url 204 */ 205 public function test_modified_url_out_as_local_url_error() { 206 global $CFG; 207 208 $modifiedurl = $CFG->wwwroot.'1'; 209 $url3 = new \moodle_url($modifiedurl.'/login/profile.php'); 210 $this->expectException(\coding_exception::class); 211 $url3->out_as_local_url(); 212 } 213 214 /** 215 * Try get local url from external https url and you should get error 216 */ 217 public function test_https_out_as_local_url_error() { 218 $url4 = new \moodle_url('https://www.google.com/lib/tests/weblib_test.php'); 219 $this->expectException(\coding_exception::class); 220 $url4->out_as_local_url(); 221 } 222 223 public function test_moodle_url_get_scheme() { 224 // Should return the scheme only. 225 $url = new \moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1'); 226 $this->assertSame('http', $url->get_scheme()); 227 228 // Should work for secure URLs. 229 $url = new \moodle_url('https://www.example.org:447/my/file/is/here.txt?really=1'); 230 $this->assertSame('https', $url->get_scheme()); 231 232 // Should return an empty string if no scheme is specified. 233 $url = new \moodle_url('www.example.org:447/my/file/is/here.txt?really=1'); 234 $this->assertSame('', $url->get_scheme()); 235 } 236 237 public function test_moodle_url_get_host() { 238 // Should return the host part only. 239 $url = new \moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1'); 240 $this->assertSame('www.example.org', $url->get_host()); 241 } 242 243 public function test_moodle_url_get_port() { 244 // Should return the port if one provided. 245 $url = new \moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1'); 246 $this->assertSame(447, $url->get_port()); 247 248 // Should return an empty string if port not specified. 249 $url = new \moodle_url('http://www.example.org/some/path/here.php'); 250 $this->assertSame('', $url->get_port()); 251 } 252 253 /** 254 * Test exporting params for templates. 255 * 256 * @dataProvider moodle_url_export_params_for_template_provider 257 * @param string $url URL with params to test. 258 * @param array $expected The expected result. 259 */ 260 public function test_moodle_url_export_params_for_template(string $url, array $expected) :void { 261 // Should return params in the URL. 262 $moodleurl = new \moodle_url($url); 263 $this->assertSame($expected, $moodleurl->export_params_for_template()); 264 } 265 266 /** 267 * Data provider for moodle_url_export_params_for_template tests. 268 * 269 * @return array[] the array of test data. 270 */ 271 public function moodle_url_export_params_for_template_provider() :array { 272 $baseurl = "http://example.com"; 273 return [ 274 'With indexed array params' => [ 275 'url' => "@{$baseurl}/?tags[0]=123&tags[1]=456", 276 'expected' => [ 277 0 => ['name' => 'tags[0]', 'value' => '123'], 278 1 => ['name' => 'tags[1]', 'value' => '456'] 279 ] 280 ], 281 'Without indexed array params' => [ 282 'url' => "@{$baseurl}/?tags[]=123&tags[]=456", 283 'expected' => [ 284 0 => ['name' => 'tags[0]', 'value' => '123'], 285 1 => ['name' => 'tags[1]', 'value' => '456'] 286 ] 287 ], 288 'with no params' => [ 289 'url' => "@{$baseurl}/", 290 'expected' => [] 291 ], 292 'with no array params' => [ 293 'url' => "@{$baseurl}/?param1=1¶m2=2¶m3=3", 294 'expected' => [ 295 0 => ['name' => 'param1', 'value' => '1'], 296 1 => ['name' => 'param2', 'value' => '2'], 297 2 => ['name' => 'param3', 'value' => '3'], 298 ] 299 ], 300 'array embedded with other params' => [ 301 'url' => "@{$baseurl}/?param1=1&tags[0]=123&tags[1]=456¶m2=2¶m3=3", 302 'expected' => [ 303 0 => ['name' => 'param1', 'value' => '1'], 304 1 => ['name' => 'tags[0]', 'value' => '123'], 305 2 => ['name' => 'tags[1]', 'value' => '456'], 306 3 => ['name' => 'param2', 'value' => '2'], 307 4 => ['name' => 'param3', 'value' => '3'], 308 ] 309 ], 310 'params with array at the end' => [ 311 'url' => "@{$baseurl}/?param1=1&tags[]=123&tags[]=456", 312 'expected' => [ 313 0 => ['name' => 'param1', 'value' => '1'], 314 1 => ['name' => 'tags[0]', 'value' => '123'], 315 2 => ['name' => 'tags[1]', 'value' => '456'], 316 ] 317 ], 318 ]; 319 } 320 321 /** 322 * Test the make_pluginfile_url function. 323 * 324 * @dataProvider make_pluginfile_url_provider 325 * @param bool $slashargs 326 * @param array $args Args to be provided to make_pluginfile_url 327 * @param string $expected The expected result 328 */ 329 public function test_make_pluginfile_url($slashargs, $args, $expected) { 330 global $CFG; 331 332 $this->resetAfterTest(); 333 334 $CFG->slasharguments = $slashargs; 335 $url = call_user_func_array('\moodle_url::make_pluginfile_url', $args); 336 $this->assertMatchesRegularExpression($expected, $url->out(true)); 337 } 338 339 /** 340 * Data provider for make_pluginfile_url tests. 341 * 342 * @return array[] 343 */ 344 public function make_pluginfile_url_provider() { 345 $baseurl = "https://www.example.com/moodle/pluginfile.php"; 346 $tokenbaseurl = "https://www.example.com/moodle/tokenpluginfile.php"; 347 return [ 348 'Standard with slashargs' => [ 349 'slashargs' => true, 350 'args' => [ 351 1, 352 'mod_forum', 353 'posts', 354 422, 355 '/my/location/', 356 'file.png', 357 ], 358 'expected' => "@{$baseurl}/1/mod_forum/posts/422/my/location/file.png@", 359 ], 360 'Standard without slashargs' => [ 361 'slashargs' => false, 362 'args' => [ 363 1, 364 'mod_forum', 365 'posts', 366 422, 367 '/my/location/', 368 'file.png', 369 ], 370 'expected' => "@{$baseurl}\?file=%2F1%2Fmod_forum%2Fposts%2F422%2Fmy%2Flocation%2Ffile.png@", 371 ], 372 'Token included with slashargs' => [ 373 'slashargs' => true, 374 'args' => [ 375 1, 376 'mod_forum', 377 'posts', 378 422, 379 '/my/location/', 380 'file.png', 381 false, 382 true, 383 ], 384 'expected' => "@{$tokenbaseurl}/[^/]*/1/mod_forum/posts/422/my/location/file.png@", 385 ], 386 'Token included without slashargs' => [ 387 'slashargs' => false, 388 'args' => [ 389 1, 390 'mod_forum', 391 'posts', 392 422, 393 '/my/location/', 394 'file.png', 395 false, 396 true, 397 ], 398 'expected' => "@{$tokenbaseurl}\?file=%2F1%2Fmod_forum%2Fposts%2F422%2Fmy%2Flocation%2Ffile.png&token=[a-z0-9]*@", 399 ], 400 ]; 401 } 402 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body