Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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   */
  26  class moodle_url_test extends \advanced_testcase {
  27      /**
  28       * Test basic moodle_url construction.
  29       */
  30      public function test_moodle_url_constructor() {
  31          global $CFG;
  32  
  33          $url = new \moodle_url('/index.php');
  34          $this->assertSame($CFG->wwwroot.'/index.php', $url->out());
  35  
  36          $url = new \moodle_url('/index.php', array());
  37          $this->assertSame($CFG->wwwroot.'/index.php', $url->out());
  38  
  39          $url = new \moodle_url('/index.php', array('id' => 2));
  40          $this->assertSame($CFG->wwwroot.'/index.php?id=2', $url->out());
  41  
  42          $url = new \moodle_url('/index.php', array('id' => 'two'));
  43          $this->assertSame($CFG->wwwroot.'/index.php?id=two', $url->out());
  44  
  45          $url = new \moodle_url('/index.php', array('id' => 1, 'cid' => '2'));
  46          $this->assertSame($CFG->wwwroot.'/index.php?id=1&amp;cid=2', $url->out());
  47          $this->assertSame($CFG->wwwroot.'/index.php?id=1&cid=2', $url->out(false));
  48  
  49          $url = new \moodle_url('/index.php', null, 'test');
  50          $this->assertSame($CFG->wwwroot.'/index.php#test', $url->out());
  51  
  52          $url = new \moodle_url('/index.php', array('id' => 2), 'test');
  53          $this->assertSame($CFG->wwwroot.'/index.php?id=2#test', $url->out());
  54      }
  55  
  56      /**
  57       * Tests \moodle_url::get_path().
  58       */
  59      public function test_moodle_url_get_path() {
  60          $url = new \moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
  61          $this->assertSame('/my/file/is/here.txt', $url->get_path());
  62  
  63          $url = new \moodle_url('http://www.example.org/');
  64          $this->assertSame('/', $url->get_path());
  65  
  66          $url = new \moodle_url('http://www.example.org/pluginfile.php/slash/arguments');
  67          $this->assertSame('/pluginfile.php/slash/arguments', $url->get_path());
  68          $this->assertSame('/pluginfile.php', $url->get_path(false));
  69      }
  70  
  71      public function test_moodle_url_round_trip() {
  72          $strurl = 'http://moodle.org/course/view.php?id=5';
  73          $url = new \moodle_url($strurl);
  74          $this->assertSame($strurl, $url->out(false));
  75  
  76          $strurl = 'http://moodle.org/user/index.php?contextid=53&sifirst=M&silast=D';
  77          $url = new \moodle_url($strurl);
  78          $this->assertSame($strurl, $url->out(false));
  79      }
  80  
  81      /**
  82       * Test Moodle URL objects created with a param with empty value.
  83       */
  84      public function test_moodle_url_empty_param_values() {
  85          $strurl = 'http://moodle.org/course/view.php?id=0';
  86          $url = new \moodle_url($strurl, array('id' => 0));
  87          $this->assertSame($strurl, $url->out(false));
  88  
  89          $strurl = 'http://moodle.org/course/view.php?id';
  90          $url = new \moodle_url($strurl, array('id' => false));
  91          $this->assertSame($strurl, $url->out(false));
  92  
  93          $strurl = 'http://moodle.org/course/view.php?id';
  94          $url = new \moodle_url($strurl, array('id' => null));
  95          $this->assertSame($strurl, $url->out(false));
  96  
  97          $strurl = 'http://moodle.org/course/view.php?id';
  98          $url = new \moodle_url($strurl, array('id' => ''));
  99          $this->assertSame($strurl, $url->out(false));
 100  
 101          $strurl = 'http://moodle.org/course/view.php?id';
 102          $url = new \moodle_url($strurl);
 103          $this->assertSame($strurl, $url->out(false));
 104      }
 105  
 106      /**
 107       * Test set good scheme on Moodle URL objects.
 108       */
 109      public function test_moodle_url_set_good_scheme() {
 110          $url = new \moodle_url('http://moodle.org/foo/bar');
 111          $url->set_scheme('myscheme');
 112          $this->assertSame('myscheme://moodle.org/foo/bar', $url->out());
 113      }
 114  
 115      /**
 116       * Test set bad scheme on Moodle URL objects.
 117       */
 118      public function test_moodle_url_set_bad_scheme() {
 119          $url = new \moodle_url('http://moodle.org/foo/bar');
 120          $this->expectException(\coding_exception::class);
 121          $url->set_scheme('not a valid $ scheme');
 122      }
 123  
 124      public function test_moodle_url_round_trip_array_params() {
 125          $strurl = 'http://example.com/?a%5B1%5D=1&a%5B2%5D=2';
 126          $url = new \moodle_url($strurl);
 127          $this->assertSame($strurl, $url->out(false));
 128  
 129          $url = new \moodle_url('http://example.com/?a[1]=1&a[2]=2');
 130          $this->assertSame($strurl, $url->out(false));
 131  
 132          // For un-keyed array params, we expect 0..n keys to be returned.
 133          $strurl = 'http://example.com/?a%5B0%5D=0&a%5B1%5D=1';
 134          $url = new \moodle_url('http://example.com/?a[]=0&a[]=1');
 135          $this->assertSame($strurl, $url->out(false));
 136      }
 137  
 138      public function test_compare_url() {
 139          $url1 = new \moodle_url('index.php', array('var1' => 1, 'var2' => 2));
 140          $url2 = new \moodle_url('index2.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
 141  
 142          $this->assertFalse($url1->compare($url2, URL_MATCH_BASE));
 143          $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS));
 144          $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
 145  
 146          $url2 = new \moodle_url('index.php', array('var1' => 1, 'var3' => 3));
 147  
 148          $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
 149          $this->assertFalse($url1->compare($url2, URL_MATCH_PARAMS));
 150          $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
 151  
 152          $url2 = new \moodle_url('index.php', array('var1' => 1, 'var2' => 2, 'var3' => 3));
 153  
 154          $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
 155          $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
 156          $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
 157  
 158          $url2 = new \moodle_url('index.php', array('var2' => 2, 'var1' => 1));
 159  
 160          $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
 161          $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
 162          $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT));
 163  
 164          $url1->set_anchor('test');
 165          $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
 166          $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
 167          $this->assertFalse($url1->compare($url2, URL_MATCH_EXACT));
 168  
 169          $url2->set_anchor('test');
 170          $this->assertTrue($url1->compare($url2, URL_MATCH_BASE));
 171          $this->assertTrue($url1->compare($url2, URL_MATCH_PARAMS));
 172          $this->assertTrue($url1->compare($url2, URL_MATCH_EXACT));
 173      }
 174  
 175      public function test_out_as_local_url() {
 176          global $CFG;
 177          // Test http url.
 178          $url1 = new \moodle_url('/lib/tests/weblib_test.php');
 179          $this->assertSame('/lib/tests/weblib_test.php', $url1->out_as_local_url());
 180  
 181          // Test https url.
 182          $httpswwwroot = str_replace("http://", "https://", $CFG->wwwroot);
 183          $url2 = new \moodle_url($httpswwwroot.'/login/profile.php');
 184          $this->assertSame('/login/profile.php', $url2->out_as_local_url());
 185  
 186          // Test http url matching wwwroot.
 187          $url3 = new \moodle_url($CFG->wwwroot);
 188          $this->assertSame('', $url3->out_as_local_url());
 189  
 190          // Test http url matching wwwroot ending with slash (/).
 191          $url3 = new \moodle_url($CFG->wwwroot.'/');
 192          $this->assertSame('/', $url3->out_as_local_url());
 193      }
 194  
 195      public function test_out_as_local_url_error() {
 196          $url2 = new \moodle_url('http://www.google.com/lib/tests/weblib_test.php');
 197          $this->expectException(\coding_exception::class);
 198          $url2->out_as_local_url();
 199      }
 200  
 201      /**
 202       * You should get error with modified url
 203       */
 204      public function test_modified_url_out_as_local_url_error() {
 205          global $CFG;
 206  
 207          $modifiedurl = $CFG->wwwroot.'1';
 208          $url3 = new \moodle_url($modifiedurl.'/login/profile.php');
 209          $this->expectException(\coding_exception::class);
 210          $url3->out_as_local_url();
 211      }
 212  
 213      /**
 214       * Try get local url from external https url and you should get error
 215       */
 216      public function test_https_out_as_local_url_error() {
 217          $url4 = new \moodle_url('https://www.google.com/lib/tests/weblib_test.php');
 218          $this->expectException(\coding_exception::class);
 219          $url4->out_as_local_url();
 220      }
 221  
 222      public function test_moodle_url_get_scheme() {
 223          // Should return the scheme only.
 224          $url = new \moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
 225          $this->assertSame('http', $url->get_scheme());
 226  
 227          // Should work for secure URLs.
 228          $url = new \moodle_url('https://www.example.org:447/my/file/is/here.txt?really=1');
 229          $this->assertSame('https', $url->get_scheme());
 230  
 231          // Should return an empty string if no scheme is specified.
 232          $url = new \moodle_url('www.example.org:447/my/file/is/here.txt?really=1');
 233          $this->assertSame('', $url->get_scheme());
 234      }
 235  
 236      public function test_moodle_url_get_host() {
 237          // Should return the host part only.
 238          $url = new \moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
 239          $this->assertSame('www.example.org', $url->get_host());
 240      }
 241  
 242      public function test_moodle_url_get_port() {
 243          // Should return the port if one provided.
 244          $url = new \moodle_url('http://www.example.org:447/my/file/is/here.txt?really=1');
 245          $this->assertSame(447, $url->get_port());
 246  
 247          // Should return an empty string if port not specified.
 248          $url = new \moodle_url('http://www.example.org/some/path/here.php');
 249          $this->assertSame('', $url->get_port());
 250      }
 251  
 252      /**
 253       * Test the make_pluginfile_url function.
 254       *
 255       * @dataProvider make_pluginfile_url_provider
 256       * @param   bool    $slashargs
 257       * @param   array   $args Args to be provided to make_pluginfile_url
 258       * @param   string  $expected The expected result
 259       */
 260      public function test_make_pluginfile_url($slashargs, $args, $expected) {
 261          global $CFG;
 262  
 263          $this->resetAfterTest();
 264  
 265          $CFG->slasharguments = $slashargs;
 266          $url = call_user_func_array('\moodle_url::make_pluginfile_url', $args);
 267          $this->assertMatchesRegularExpression($expected, $url->out(true));
 268      }
 269  
 270      /**
 271       * Data provider for make_pluginfile_url tests.
 272       *
 273       * @return  array[]
 274       */
 275      public function make_pluginfile_url_provider() {
 276          $baseurl = "https://www.example.com/moodle/pluginfile.php";
 277          $tokenbaseurl = "https://www.example.com/moodle/tokenpluginfile.php";
 278          return [
 279              'Standard with slashargs' => [
 280                  'slashargs' => true,
 281                  'args' => [
 282                      1,
 283                      'mod_forum',
 284                      'posts',
 285                      422,
 286                      '/my/location/',
 287                      'file.png',
 288                  ],
 289                  'expected' => "@{$baseurl}/1/mod_forum/posts/422/my/location/file.png@",
 290              ],
 291              'Standard without slashargs' => [
 292                  'slashargs' => false,
 293                  'args' => [
 294                      1,
 295                      'mod_forum',
 296                      'posts',
 297                      422,
 298                      '/my/location/',
 299                      'file.png',
 300                  ],
 301                  'expected' => "@{$baseurl}\?file=%2F1%2Fmod_forum%2Fposts%2F422%2Fmy%2Flocation%2Ffile.png@",
 302              ],
 303              'Token included with slashargs' => [
 304                  'slashargs' => true,
 305                  'args' => [
 306                      1,
 307                      'mod_forum',
 308                      'posts',
 309                      422,
 310                      '/my/location/',
 311                      'file.png',
 312                      false,
 313                      true,
 314                  ],
 315                  'expected' => "@{$tokenbaseurl}/[^/]*/1/mod_forum/posts/422/my/location/file.png@",
 316              ],
 317              'Token included without slashargs' => [
 318                  'slashargs' => false,
 319                  'args' => [
 320                      1,
 321                      'mod_forum',
 322                      'posts',
 323                      422,
 324                      '/my/location/',
 325                      'file.png',
 326                      false,
 327                      true,
 328                  ],
 329                  'expected' => "@{$tokenbaseurl}\?file=%2F1%2Fmod_forum%2Fposts%2F422%2Fmy%2Flocation%2Ffile.png&amp;token=[a-z0-9]*@",
 330              ],
 331          ];
 332      }
 333  }