Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   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 repository.
  19   *
  20   * @package   repository_url
  21   * @copyright 2014 John Okely
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace repository_url;
  25  
  26  defined('MOODLE_INTERNAL') || die;
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/repository/url/lib.php');
  30  
  31  
  32  /**
  33   * URL repository test case.
  34   *
  35   * @copyright 2014 John Okely
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class lib_test extends \advanced_testcase {
  39  
  40      /**
  41       * Check that the url escaper performs as expected
  42       */
  43      public function test_escape_url() {
  44          $this->resetAfterTest();
  45  
  46          $repoid = $this->getDataGenerator()->create_repository('url')->id;
  47  
  48          $conversions = array(
  49                  'http://example.com/test_file.png' => 'http://example.com/test_file.png',
  50                  'http://example.com/test%20file.png' => 'http://example.com/test%20file.png',
  51                  'http://example.com/test file.png' => 'http://example.com/test%20file.png',
  52                  'http://example.com/test file.png?query=string+test&more=string+tests' =>
  53                      'http://example.com/test%20file.png?query=string+test&more=string+tests',
  54                  'http://example.com/?tag=<p>' => 'http://example.com/?tag=%3Cp%3E',
  55                  'http://example.com/"quoted".txt' => 'http://example.com/%22quoted%22.txt',
  56                  'http://example.com/\'quoted\'.txt' => 'http://example.com/%27quoted%27.txt',
  57                  '' => ''
  58              );
  59  
  60          foreach ($conversions as $input => $expected) {
  61              // The constructor uses a optional_param, so we need to hack $_GET.
  62              $_GET['file'] = $input;
  63              $repository = new \repository_url($repoid);
  64              $this->assertSame($expected, $repository->file_url);
  65          }
  66  
  67          $exceptions = array(
  68                  '%' => true,
  69                  '!' => true,
  70                  '!https://download.moodle.org/unittest/test.jpg' => true,
  71                  'https://download.moodle.org/unittest/test.jpg' => false
  72              );
  73  
  74          foreach ($exceptions as $input => $expected) {
  75              $caughtexception = false;
  76              try {
  77                  // The constructor uses a optional_param, so we need to hack $_GET.
  78                  $_GET['file'] = $input;
  79                  $repository = new \repository_url($repoid);
  80                  $repository->get_listing();
  81              } catch (\repository_exception $e) {
  82                  if ($e->errorcode == 'validfiletype') {
  83                      $caughtexception = true;
  84                  }
  85              }
  86              $this->assertSame($expected, $caughtexception);
  87          }
  88  
  89          unset($_GET['file']);
  90      }
  91  
  92  }