Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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   * Unit tests for the url class.
  19   *
  20   * @package    tool_moodlenet
  21   * @category   test
  22   * @copyright  2020 Jake Dallimore <jrhdallimore@gmail.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  namespace tool_moodlenet\local\tests;
  26  
  27  use tool_moodlenet\local\url;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  /**
  32   * Class tool_moodlenet_url_testcase, providing test cases for the url class.
  33   */
  34  class tool_moodlenet_url_testcase extends \advanced_testcase {
  35  
  36      /**
  37       * Test the parsing to host + path components.
  38       *
  39       * @dataProvider url_provider
  40       * @param string $urlstring The full URL string
  41       * @param string $host the expected host component of the URL.
  42       * @param string $path the expected path component of the URL.
  43       * @param bool $exception whether or not an exception is expected during construction.
  44       */
  45      public function test_parsing($urlstring, $host, $path, $exception) {
  46          if ($exception) {
  47              $this->expectException(\coding_exception::class);
  48              $url = new url($urlstring);
  49              return;
  50          }
  51  
  52          $url = new url($urlstring);
  53          $this->assertEquals($urlstring, $url->get_value());
  54          $this->assertEquals($host, $url->get_host());
  55          $this->assertEquals($path, $url->get_path());
  56      }
  57  
  58      /**
  59       * Data provider.
  60       *
  61       * @return array
  62       */
  63      public function url_provider() {
  64          return [
  65              'No path' => [
  66                  'url' => 'https://example.moodle.net',
  67                  'host' => 'example.moodle.net',
  68                  'path' => null,
  69                  'exception' => false,
  70              ],
  71              'Slash path' => [
  72                  'url' => 'https://example.moodle.net/',
  73                  'host' => 'example.moodle.net',
  74                  'path' => '/',
  75                  'exception' => false,
  76              ],
  77              'Path includes file and extension' => [
  78                  'url' => 'https://example.moodle.net/uploads/123456789/pic.png',
  79                  'host' => 'example.moodle.net',
  80                  'path' => '/uploads/123456789/pic.png',
  81                  'exception' => false,
  82              ],
  83              'Path includes file, extension and params' => [
  84                  'url' => 'https://example.moodle.net/uploads/123456789/pic.png?option=1&option2=test',
  85                  'host' => 'example.moodle.net',
  86                  'path' => '/uploads/123456789/pic.png',
  87                  'exception' => false,
  88              ],
  89              'Malformed - invalid' => [
  90                  'url' => 'invalid',
  91                  'host' => null,
  92                  'path' => null,
  93                  'exception' => true,
  94              ],
  95              'Direct, non-encoded utf8 - invalid' => [
  96                  'url' => 'http://москва.рф/services/',
  97                  'host' => 'москва.рф',
  98                  'path' => '/services/',
  99                  'exception' => true,
 100              ],
 101          ];
 102      }
 103  }