2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Tests for moodle_url.
21 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
28 * Tests for moodle_url.
30 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class core_moodle_url_testcase
extends advanced_testcase
{
35 * Test basic moodle_url construction.
37 public function test_moodle_url_constructor() {
40 $url = new moodle_url('/index.php');
41 $this->assertSame($CFG->wwwroot
.'/index.php', $url->out());
43 $url = new moodle_url('/index.php', array());
44 $this->assertSame($CFG->wwwroot
.'/index.php', $url->out());
46 $url = new moodle_url('/index.php', array('id' => 2));
47 $this->assertSame($CFG->wwwroot
.'/index.php?id=2', $url->out());
49 $url = new moodle_url('/index.php', array('id' => 'two'));
50 $this->assertSame($CFG->wwwroot
.'/index.php?id=two', $url->out());
52 $url = new moodle_url('/index.php', array('id' => 1, 'cid' => '2'));
53 $this->assertSame($CFG->wwwroot
.'/index.php?id=1&cid=2', $url->out());
54 $this->assertSame($CFG->wwwroot
.'/index.php?id=1&cid=2', $url->out(false));
56 $url = new moodle_url('/index.php', null, 'test');
57 $this->assertSame($CFG->wwwroot
.'/index.php#test', $url->out());
59 $url = new moodle_url('/index.php', array('id' => 2), 'test');
60 $this->assertSame($CFG->wwwroot
.'/index.php?id=2#test', $url->out());
64 * Tests moodle_url::get_path().
66 public function test_moodle_url_get_path() {
67 $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
68 $this->assertSame('/my/file/is/here.txt', $url->get_path());
70 $url = new moodle_url('http://www.example.org/');
71 $this->assertSame('/', $url->get_path());
73 $url = new moodle_url('http://www.example.org/pluginfile.php/slash/arguments');
74 $this->assertSame('/pluginfile.php/slash/arguments', $url->get_path());
75 $this->assertSame('/pluginfile.php', $url->get_path(false));
78 public function test_moodle_url_round_trip() {
79 $strurl = 'http://moodle.org/course/view.php?id=5';
80 $url = new moodle_url($strurl);
81 $this->assertSame($strurl, $url->out(false));
83 $strurl = 'http://moodle.org/user/index.php?contextid=53&sifirst=M&silast=D';
84 $url = new moodle_url($strurl);
85 $this->assertSame($strurl, $url->out(false));
89 * Test Moodle URL objects created with a param with empty value.
91 public function test_moodle_url_empty_param_values() {
92 $strurl = 'http://moodle.org/course/view.php?id=0';
93 $url = new moodle_url($strurl, array('id' => 0));
94 $this->assertSame($strurl, $url->out(false));
96 $strurl = 'http://moodle.org/course/view.php?id';
97 $url = new moodle_url($strurl, array('id' => false));
98 $this->assertSame($strurl, $url->out(false));
100 $strurl = 'http://moodle.org/course/view.php?id';
101 $url = new moodle_url($strurl, array('id' => null));
102 $this->assertSame($strurl, $url->out(false));
104 $strurl = 'http://moodle.org/course/view.php?id';
105 $url = new moodle_url($strurl, array('id' => ''));
106 $this->assertSame($strurl, $url->out(false));
108 $strurl = 'http://moodle.org/course/view.php?id';
109 $url = new moodle_url($strurl);
110 $this->assertSame($strurl, $url->out(false));
114 * Test set good scheme on Moodle URL objects.
116 public function test_moodle_url_set_good_scheme() {
117 $url = new moodle_url('http://moodle.org/foo/bar');
118 $url->set_scheme('myscheme');
119 $this->assertSame('myscheme://moodle.org/foo/bar', $url->out());
123 * Test set bad scheme on Moodle URL objects.
125 * @expectedException coding_exception
127 public function test_moodle_url_set_bad_scheme() {
128 $url = new moodle_url('http://moodle.org/foo/bar');
129 $url->set_scheme('not a valid $ scheme');
132 public function test_moodle_url_round_trip_array_params() {
133 $strurl = 'http://example.com/?a%5B1%5D=1&a%5B2%5D=2';
134 $url = new moodle_url($strurl);
135 $this->assertSame($strurl, $url->out(false));
137 $url = new moodle_url('http://example.com/?a[1]=1&a[2]=2');
138 $this->assertSame($strurl, $url->out(false));
140 // For un-keyed array params, we expect 0..n keys to be returned.
141 $strurl = 'http://example.com/?a%5B0%5D=0&a%5B1%5D=1';
142 $url = new moodle_url('http://example.com/?a[]=0&a[]=1');
143 $this->assertSame($strurl, $url->out(false));
146 public function test_compare_url() {
147 $url1 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2));
148 $url2 = new moodle_url('index2.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
150 $this->assertFalse($url1->compare($url2, URL_MATCH_BASE
));
151 $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS
));
152 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT
));
154 $url2 = new moodle_url('index.php', array('var1' => 1, 'var3' => 3));
156 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE
));
157 $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS
));
158 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT
));
160 $url2 = new moodle_url('index.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
162 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE
));
163 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS
));
164 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT
));
166 $url2 = new moodle_url('index.php', array('var2' => 2, 'var1' => 1));
168 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE
));
169 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS
));
170 $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT
));
172 $url1->set_anchor('test');
173 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE
));
174 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS
));
175 $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT
));
177 $url2->set_anchor('test');
178 $this->assertTrue($url1->compare($url2, URL_MATCH_BASE
));
179 $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS
));
180 $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT
));
183 public function test_out_as_local_url() {
186 $url1 = new moodle_url('/lib/tests/weblib_test.php');
187 $this->assertSame('/lib/tests/weblib_test.php', $url1->out_as_local_url());
190 $httpswwwroot = str_replace("http://", "https://", $CFG->wwwroot
);
191 $url2 = new moodle_url($httpswwwroot.'/login/profile.php');
192 $this->assertSame('/login/profile.php', $url2->out_as_local_url());
194 // Test http url matching wwwroot.
195 $url3 = new moodle_url($CFG->wwwroot
);
196 $this->assertSame('', $url3->out_as_local_url());
198 // Test http url matching wwwroot ending with slash (/).
199 $url3 = new moodle_url($CFG->wwwroot
.'/');
200 $this->assertSame('/', $url3->out_as_local_url());
204 * @expectedException coding_exception
207 public function test_out_as_local_url_error() {
208 $url2 = new moodle_url('http://www.google.com/lib/tests/weblib_test.php');
209 $url2->out_as_local_url();
213 * You should get error with modified url
215 * @expectedException coding_exception
218 public function test_modified_url_out_as_local_url_error() {
221 $modifiedurl = $CFG->wwwroot
.'1';
222 $url3 = new moodle_url($modifiedurl.'/login/profile.php');
223 $url3->out_as_local_url();
227 * Try get local url from external https url and you should get error
229 * @expectedException coding_exception
231 public function test_https_out_as_local_url_error() {
232 $url4 = new moodle_url('https://www.google.com/lib/tests/weblib_test.php');
233 $url4->out_as_local_url();
236 public function test_moodle_url_get_scheme() {
237 // Should return the scheme only.
238 $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
239 $this->assertSame('http', $url->get_scheme());
241 // Should work for secure URLs.
242 $url = new moodle_url('https://www.example.org:447/my/file/is/here.txt?really=1');
243 $this->assertSame('https', $url->get_scheme());
245 // Should return an empty string if no scheme is specified.
246 $url = new moodle_url('www.example.org:447/my/file/is/here.txt?really=1');
247 $this->assertSame('', $url->get_scheme());
250 public function test_moodle_url_get_host() {
251 // Should return the host part only.
252 $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
253 $this->assertSame('www.example.org', $url->get_host());
256 public function test_moodle_url_get_port() {
257 // Should return the port if one provided.
258 $url = new moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
259 $this->assertSame(447, $url->get_port());
261 // Should return an empty string if port not specified.
262 $url = new moodle_url('http://www.example.org/some/path/here.php');
263 $this->assertSame('', $url->get_port());
267 * Test the make_pluginfile_url function.
269 * @dataProvider make_pluginfile_url_provider
270 * @param bool $slashargs
271 * @param array $args Args to be provided to make_pluginfile_url
272 * @param string $expected The expected result
274 public function test_make_pluginfile_url($slashargs, $args, $expected) {
277 $this->resetAfterTest();
279 $CFG->slasharguments
= $slashargs;
280 $url = call_user_func_array('moodle_url::make_pluginfile_url', $args);
281 $this->assertRegexp($expected, $url->out(true));
285 * Data provider for make_pluginfile_url tests.
289 public function make_pluginfile_url_provider() {
290 $baseurl = "https://www.example.com/moodle/pluginfile.php";
291 $tokenbaseurl = "https://www.example.com/moodle/tokenpluginfile.php";
293 'Standard with slashargs' => [
303 'expected' => "@{$baseurl}/1/mod_forum/posts/422/my/location/file.png@",
305 'Standard without slashargs' => [
306 'slashargs' => false,
315 'expected' => "@{$baseurl}\?file=%2F1%2Fmod_forum%2Fposts%2F422%2Fmy%2Flocation%2Ffile.png@",
317 'Token included with slashargs' => [
329 'expected' => "@{$tokenbaseurl}/[^/]*/1/mod_forum/posts/422/my/location/file.png@",
331 'Token included without slashargs' => [
332 'slashargs' => false,
343 'expected' => "@{$tokenbaseurl}\?file=%2F1%2Fmod_forum%2Fposts%2F422%2Fmy%2Flocation%2Ffile.png&token=[a-z0-9]*@",