Search moodle.org's
Developer Documentation

See Release Notes

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