| 1 | 
           efrain | 
           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 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           29 | 
           final class url_test extends \advanced_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           30 | 
              | 
        
        
            | 
            | 
           31 | 
               /**
  | 
        
        
            | 
            | 
           32 | 
                * Test the parsing to host + path components.
  | 
        
        
            | 
            | 
           33 | 
                *
  | 
        
        
            | 
            | 
           34 | 
                * @dataProvider url_provider
  | 
        
        
           | 1441 | 
           ariadna | 
           35 | 
                * @param string $url The full URL string
  | 
        
        
           | 1 | 
           efrain | 
           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 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           40 | 
               public function test_parsing($urlstring, $host, $path, $exception): void {
  | 
        
        
           | 1 | 
           efrain | 
           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 | 
                */
  | 
        
        
           | 1441 | 
           ariadna | 
           58 | 
               public static function url_provider(): array {
  | 
        
        
           | 1 | 
           efrain | 
           59 | 
                   return [
  | 
        
        
            | 
            | 
           60 | 
                       'No path' => [
  | 
        
        
           | 1441 | 
           ariadna | 
           61 | 
                           'urlstring' => 'https://example.moodle.net',
  | 
        
        
           | 1 | 
           efrain | 
           62 | 
                           'host' => 'example.moodle.net',
  | 
        
        
            | 
            | 
           63 | 
                           'path' => null,
  | 
        
        
            | 
            | 
           64 | 
                           'exception' => false,
  | 
        
        
            | 
            | 
           65 | 
                       ],
  | 
        
        
            | 
            | 
           66 | 
                       'Slash path' => [
  | 
        
        
           | 1441 | 
           ariadna | 
           67 | 
                           'urlstring' => 'https://example.moodle.net/',
  | 
        
        
           | 1 | 
           efrain | 
           68 | 
                           'host' => 'example.moodle.net',
  | 
        
        
            | 
            | 
           69 | 
                           'path' => '/',
  | 
        
        
            | 
            | 
           70 | 
                           'exception' => false,
  | 
        
        
            | 
            | 
           71 | 
                       ],
  | 
        
        
            | 
            | 
           72 | 
                       'Path includes file and extension' => [
  | 
        
        
           | 1441 | 
           ariadna | 
           73 | 
                           'urlstring' => 'https://example.moodle.net/uploads/123456789/pic.png',
  | 
        
        
           | 1 | 
           efrain | 
           74 | 
                           'host' => 'example.moodle.net',
  | 
        
        
            | 
            | 
           75 | 
                           'path' => '/uploads/123456789/pic.png',
  | 
        
        
            | 
            | 
           76 | 
                           'exception' => false,
  | 
        
        
            | 
            | 
           77 | 
                       ],
  | 
        
        
            | 
            | 
           78 | 
                       'Path includes file, extension and params' => [
  | 
        
        
           | 1441 | 
           ariadna | 
           79 | 
                           'urlstring' => 'https://example.moodle.net/uploads/123456789/pic.png?option=1&option2=test',
  | 
        
        
           | 1 | 
           efrain | 
           80 | 
                           'host' => 'example.moodle.net',
  | 
        
        
            | 
            | 
           81 | 
                           'path' => '/uploads/123456789/pic.png',
  | 
        
        
            | 
            | 
           82 | 
                           'exception' => false,
  | 
        
        
            | 
            | 
           83 | 
                       ],
  | 
        
        
            | 
            | 
           84 | 
                       'Malformed - invalid' => [
  | 
        
        
           | 1441 | 
           ariadna | 
           85 | 
                           'urlstring' => 'invalid',
  | 
        
        
           | 1 | 
           efrain | 
           86 | 
                           'host' => null,
  | 
        
        
            | 
            | 
           87 | 
                           'path' => null,
  | 
        
        
            | 
            | 
           88 | 
                           'exception' => true,
  | 
        
        
            | 
            | 
           89 | 
                       ],
  | 
        
        
            | 
            | 
           90 | 
                       'Direct, non-encoded utf8 - invalid' => [
  | 
        
        
           | 1441 | 
           ariadna | 
           91 | 
                           'urlstring' => 'http://москва.рф/services/',
  | 
        
        
           | 1 | 
           efrain | 
           92 | 
                           'host' => 'москва.рф',
  | 
        
        
            | 
            | 
           93 | 
                           'path' => '/services/',
  | 
        
        
            | 
            | 
           94 | 
                           'exception' => true,
  | 
        
        
            | 
            | 
           95 | 
                       ],
  | 
        
        
            | 
            | 
           96 | 
                   ];
  | 
        
        
            | 
            | 
           97 | 
               }
  | 
        
        
            | 
            | 
           98 | 
           }
  |