Merge branch 'MDL-73483-master' of https://github.com/dmitriim/moodle
[moodle.git] / lib / tests / moodle_url_test.php
blobeb33e6ba76a9fec5379a4f1f217a677f199af226
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.
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/>.
17 namespace core;
19 /**
20 * Tests for moodle_url.
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
27 class moodle_url_test extends \advanced_testcase {
28 /**
29 * Test basic moodle_url construction.
31 public function test_moodle_url_constructor() {
32 global $CFG;
34 $url = new \moodle_url('/index.php');
35 $this->assertSame($CFG->wwwroot.'/index.php', $url->out());
37 $url = new \moodle_url('/index.php', array());
38 $this->assertSame($CFG->wwwroot.'/index.php', $url->out());
40 $url = new \moodle_url('/index.php', array('id' => 2));
41 $this->assertSame($CFG->wwwroot.'/index.php?id=2', $url->out());
43 $url = new \moodle_url('/index.php', array('id' => 'two'));
44 $this->assertSame($CFG->wwwroot.'/index.php?id=two', $url->out());
46 $url = new \moodle_url('/index.php', array('id' => 1, 'cid' => '2'));
47 $this->assertSame($CFG->wwwroot.'/index.php?id=1&amp;cid=2', $url->out());
48 $this->assertSame($CFG->wwwroot.'/index.php?id=1&cid=2', $url->out(false));
50 $url = new \moodle_url('/index.php', null, 'test');
51 $this->assertSame($CFG->wwwroot.'/index.php#test', $url->out());
53 $url = new \moodle_url('/index.php', null, 'Long "Anchor"');
54 $this->assertSame($CFG->wwwroot . '/index.php#Long%20%22Anchor%22', $url->out());
56 $url = new \moodle_url('/index.php', array('id' => 2), 'test');
57 $this->assertSame($CFG->wwwroot.'/index.php?id=2#test', $url->out());
60 /**
61 * Tests \moodle_url::get_path().
63 public function test_moodle_url_get_path() {
64 $url = new \moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
65 $this->assertSame('/my/file/is/here.txt', $url->get_path());
67 $url = new \moodle_url('http://www.example.org/');
68 $this->assertSame('/', $url->get_path());
70 $url = new \moodle_url('http://www.example.org/pluginfile.php/slash/arguments');
71 $this->assertSame('/pluginfile.php/slash/arguments', $url->get_path());
72 $this->assertSame('/pluginfile.php', $url->get_path(false));
75 public function test_moodle_url_round_trip() {
76 $strurl = 'http://moodle.org/course/view.php?id=5';
77 $url = new \moodle_url($strurl);
78 $this->assertSame($strurl, $url->out(false));
80 $strurl = 'http://moodle.org/user/index.php?contextid=53&sifirst=M&silast=D';
81 $url = new \moodle_url($strurl);
82 $this->assertSame($strurl, $url->out(false));
85 /**
86 * Test Moodle URL objects created with a param with empty value.
88 public function test_moodle_url_empty_param_values() {
89 $strurl = 'http://moodle.org/course/view.php?id=0';
90 $url = new \moodle_url($strurl, array('id' => 0));
91 $this->assertSame($strurl, $url->out(false));
93 $strurl = 'http://moodle.org/course/view.php?id';
94 $url = new \moodle_url($strurl, array('id' => false));
95 $this->assertSame($strurl, $url->out(false));
97 $strurl = 'http://moodle.org/course/view.php?id';
98 $url = new \moodle_url($strurl, array('id' => null));
99 $this->assertSame($strurl, $url->out(false));
101 $strurl = 'http://moodle.org/course/view.php?id';
102 $url = new \moodle_url($strurl, array('id' => ''));
103 $this->assertSame($strurl, $url->out(false));
105 $strurl = 'http://moodle.org/course/view.php?id';
106 $url = new \moodle_url($strurl);
107 $this->assertSame($strurl, $url->out(false));
111 * Test set good scheme on Moodle URL objects.
113 public function test_moodle_url_set_good_scheme() {
114 $url = new \moodle_url('http://moodle.org/foo/bar');
115 $url->set_scheme('myscheme');
116 $this->assertSame('myscheme://moodle.org/foo/bar', $url->out());
120 * Test set bad scheme on Moodle URL objects.
122 public function test_moodle_url_set_bad_scheme() {
123 $url = new \moodle_url('http://moodle.org/foo/bar');
124 $this->expectException(\coding_exception::class);
125 $url->set_scheme('not a valid $ scheme');
128 public function test_moodle_url_round_trip_array_params() {
129 $strurl = 'http://example.com/?a%5B1%5D=1&a%5B2%5D=2';
130 $url = new \moodle_url($strurl);
131 $this->assertSame($strurl, $url->out(false));
133 $url = new \moodle_url('http://example.com/?a[1]=1&a[2]=2');
134 $this->assertSame($strurl, $url->out(false));
136 // For un-keyed array params, we expect 0..n keys to be returned.
137 $strurl = 'http://example.com/?a%5B0%5D=0&a%5B1%5D=1';
138 $url = new \moodle_url('http://example.com/?a[]=0&a[]=1');
139 $this->assertSame($strurl, $url->out(false));
143 * Test returning URL without parameters
145 public function test_out_omit_querystring(): void {
146 global $CFG;
148 $url = new \moodle_url('/index.php', ['id' => 2], 'Long "Anchor"');
150 $this->assertSame($CFG->wwwroot . '/index.php', $url->out_omit_querystring());
151 $this->assertSame($CFG->wwwroot . '/index.php#Long%20%22Anchor%22', $url->out_omit_querystring(true));
154 public function test_compare_url() {
155 $url1 = new \moodle_url('index.php', array('var1' => 1, 'var2' => 2));
156 $url2 = new \moodle_url('index2.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
158 $this->assertFalse($url1->compare($url2, URL_MATCH_BASE));
159 $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS));
160 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
162 $url2 = new \moodle_url('index.php', array('var1' => 1, 'var3' => 3));
164 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
165 $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS));
166 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
168 $url2 = new \moodle_url('index.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
170 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
171 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
172 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
174 $url2 = new \moodle_url('index.php', array('var2' => 2, 'var1' => 1));
176 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
177 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
178 $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT));
180 $url1->set_anchor('test');
181 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
182 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
183 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
185 $url2->set_anchor('test');
186 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
187 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
188 $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT));
191 public function test_out_as_local_url() {
192 global $CFG;
193 // Test http url.
194 $url1 = new \moodle_url('/lib/tests/weblib_test.php');
195 $this->assertSame('/lib/tests/weblib_test.php', $url1->out_as_local_url());
197 // Test https url.
198 $httpswwwroot = str_replace("http://", "https://", $CFG->wwwroot);
199 $url2 = new \moodle_url($httpswwwroot.'/login/profile.php');
200 $this->assertSame('/login/profile.php', $url2->out_as_local_url());
202 // Test http url matching wwwroot.
203 $url3 = new \moodle_url($CFG->wwwroot);
204 $this->assertSame('', $url3->out_as_local_url());
206 // Test http url matching wwwroot ending with slash (/).
207 $url3 = new \moodle_url($CFG->wwwroot.'/');
208 $this->assertSame('/', $url3->out_as_local_url());
211 public function test_out_as_local_url_error() {
212 $url2 = new \moodle_url('http://www.google.com/lib/tests/weblib_test.php');
213 $this->expectException(\coding_exception::class);
214 $url2->out_as_local_url();
218 * You should get error with modified url
220 public function test_modified_url_out_as_local_url_error() {
221 global $CFG;
223 $modifiedurl = $CFG->wwwroot.'1';
224 $url3 = new \moodle_url($modifiedurl.'/login/profile.php');
225 $this->expectException(\coding_exception::class);
226 $url3->out_as_local_url();
230 * Try get local url from external https url and you should get error
232 public function test_https_out_as_local_url_error() {
233 $url4 = new \moodle_url('https://www.google.com/lib/tests/weblib_test.php');
234 $this->expectException(\coding_exception::class);
235 $url4->out_as_local_url();
238 public function test_moodle_url_get_scheme() {
239 // Should return the scheme only.
240 $url = new \moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
241 $this->assertSame('http', $url->get_scheme());
243 // Should work for secure URLs.
244 $url = new \moodle_url('https://www.example.org:447/my/file/is/here.txt?really=1');
245 $this->assertSame('https', $url->get_scheme());
247 // Should return an empty string if no scheme is specified.
248 $url = new \moodle_url('www.example.org:447/my/file/is/here.txt?really=1');
249 $this->assertSame('', $url->get_scheme());
252 public function test_moodle_url_get_host() {
253 // Should return the host part only.
254 $url = new \moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
255 $this->assertSame('www.example.org', $url->get_host());
258 public function test_moodle_url_get_port() {
259 // Should return the port if one provided.
260 $url = new \moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
261 $this->assertSame(447, $url->get_port());
263 // Should return an empty string if port not specified.
264 $url = new \moodle_url('http://www.example.org/some/path/here.php');
265 $this->assertSame('', $url->get_port());
269 * Test exporting params for templates.
271 * @dataProvider moodle_url_export_params_for_template_provider
272 * @param string $url URL with params to test.
273 * @param array $expected The expected result.
275 public function test_moodle_url_export_params_for_template(string $url, array $expected) :void {
276 // Should return params in the URL.
277 $moodleurl = new \moodle_url($url);
278 $this->assertSame($expected, $moodleurl->export_params_for_template());
282 * Data provider for moodle_url_export_params_for_template tests.
284 * @return array[] the array of test data.
286 public function moodle_url_export_params_for_template_provider() :array {
287 $baseurl = "http://example.com";
288 return [
289 'With indexed array params' => [
290 'url' => "@{$baseurl}/?tags[0]=123&tags[1]=456",
291 'expected' => [
292 0 => ['name' => 'tags[0]', 'value' => '123'],
293 1 => ['name' => 'tags[1]', 'value' => '456']
296 'Without indexed array params' => [
297 'url' => "@{$baseurl}/?tags[]=123&tags[]=456",
298 'expected' => [
299 0 => ['name' => 'tags[0]', 'value' => '123'],
300 1 => ['name' => 'tags[1]', 'value' => '456']
303 'with no params' => [
304 'url' => "@{$baseurl}/",
305 'expected' => []
307 'with no array params' => [
308 'url' => "@{$baseurl}/?param1=1&param2=2&param3=3",
309 'expected' => [
310 0 => ['name' => 'param1', 'value' => '1'],
311 1 => ['name' => 'param2', 'value' => '2'],
312 2 => ['name' => 'param3', 'value' => '3'],
315 'array embedded with other params' => [
316 'url' => "@{$baseurl}/?param1=1&tags[0]=123&tags[1]=456&param2=2&param3=3",
317 'expected' => [
318 0 => ['name' => 'param1', 'value' => '1'],
319 1 => ['name' => 'tags[0]', 'value' => '123'],
320 2 => ['name' => 'tags[1]', 'value' => '456'],
321 3 => ['name' => 'param2', 'value' => '2'],
322 4 => ['name' => 'param3', 'value' => '3'],
325 'params with array at the end' => [
326 'url' => "@{$baseurl}/?param1=1&tags[]=123&tags[]=456",
327 'expected' => [
328 0 => ['name' => 'param1', 'value' => '1'],
329 1 => ['name' => 'tags[0]', 'value' => '123'],
330 2 => ['name' => 'tags[1]', 'value' => '456'],
337 * Test the make_pluginfile_url function.
339 * @dataProvider make_pluginfile_url_provider
340 * @param bool $slashargs
341 * @param array $args Args to be provided to make_pluginfile_url
342 * @param string $expected The expected result
344 public function test_make_pluginfile_url($slashargs, $args, $expected) {
345 global $CFG;
347 $this->resetAfterTest();
349 $CFG->slasharguments = $slashargs;
350 $url = call_user_func_array('\moodle_url::make_pluginfile_url', $args);
351 $this->assertMatchesRegularExpression($expected, $url->out(true));
355 * Data provider for make_pluginfile_url tests.
357 * @return array[]
359 public function make_pluginfile_url_provider() {
360 $baseurl = "https://www.example.com/moodle/pluginfile.php";
361 $tokenbaseurl = "https://www.example.com/moodle/tokenpluginfile.php";
362 return [
363 'Standard with slashargs' => [
364 'slashargs' => true,
365 'args' => [
367 'mod_forum',
368 'posts',
369 422,
370 '/my/location/',
371 'file.png',
373 'expected' => "@{$baseurl}/1/mod_forum/posts/422/my/location/file.png@",
375 'Standard without slashargs' => [
376 'slashargs' => false,
377 'args' => [
379 'mod_forum',
380 'posts',
381 422,
382 '/my/location/',
383 'file.png',
385 'expected' => "@{$baseurl}\?file=%2F1%2Fmod_forum%2Fposts%2F422%2Fmy%2Flocation%2Ffile.png@",
387 'Token included with slashargs' => [
388 'slashargs' => true,
389 'args' => [
391 'mod_forum',
392 'posts',
393 422,
394 '/my/location/',
395 'file.png',
396 false,
397 true,
399 'expected' => "@{$tokenbaseurl}/[^/]*/1/mod_forum/posts/422/my/location/file.png@",
401 'Token included without slashargs' => [
402 'slashargs' => false,
403 'args' => [
405 'mod_forum',
406 'posts',
407 422,
408 '/my/location/',
409 'file.png',
410 false,
411 true,
413 'expected' => "@{$tokenbaseurl}\?file=%2F1%2Fmod_forum%2Fposts%2F422%2Fmy%2Flocation%2Ffile.png&amp;token=[a-z0-9]*@",