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 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   * Tests for restore_structure_parser_processor class.
  19   *
  20   * @package     core_backup
  21   * @category    test
  22   * @copyright   2017 Dmitrii Metelkin (dmitriim@catalyst-au.net)
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  
  30  require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  31  require_once($CFG->dirroot . '/backup/util/helper/restore_structure_parser_processor.class.php');
  32  
  33  /**
  34   * Tests for restore_structure_parser_processor class.
  35   *
  36   * @package core_backup
  37   * @copyright 2017 Dmitrii Metelkin (dmitriim@catalyst-au.net)
  38   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class restore_structure_parser_processor_test extends advanced_testcase {
  41  
  42      /**
  43       * Initial set up.
  44       */
  45      public function setUp(): void {
  46          parent::setUp();
  47  
  48          $this->resetAfterTest(true);
  49      }
  50  
  51      /**
  52       * Data provider for ::test_process_cdata.
  53       *
  54       * @return array
  55       */
  56      public function process_cdata_data_provider() {
  57          return array(
  58              array(null, null, true),
  59              array("$@NULL@$", null, true),
  60              array("$@NULL@$ ", "$@NULL@$ ", true),
  61              array(1, 1, true),
  62              array(" ", " ", true),
  63              array("1", "1", true),
  64              array("$@FILEPHP@$1.jpg", "$@FILEPHP@$1.jpg", true),
  65              array(
  66                  "http://test.test/$@SLASH@$",
  67                  "http://test.test/$@SLASH@$",
  68                  true
  69              ),
  70              array(
  71                  "<a href='$@FILEPHP@$1.jpg'>Image</a>",
  72                  "<a href='http://test.test/file.php/11.jpg'>Image</a>",
  73                  true
  74              ),
  75              array(
  76                  "<a href='$@FILEPHP@$$@SLASH@$1.jpg'>Image</a>",
  77                  "<a href='http://test.test/file.php/1/1.jpg'>Image</a>",
  78                  true
  79              ),
  80              array(
  81                  "<a href='$@FILEPHP@$$@SLASH@$$@SLASH@$1.jpg'>Image</a>",
  82                  "<a href='http://test.test/file.php/1//1.jpg'>Image</a>",
  83                  true
  84              ),
  85              array(
  86                  "<a href='$@FILEPHP@$1.jpg'>Image</a>",
  87                  "<a href='http://test.test/file.php?file=%2F11.jpg'>Image</a>",
  88                  false
  89              ),
  90              array(
  91                  "<a href='$@FILEPHP@$$@SLASH@$1.jpg'>Image</a>",
  92                  "<a href='http://test.test/file.php?file=%2F1%2F1.jpg'>Image</a>",
  93                  false
  94              ),
  95              array(
  96                  "<a href='$@FILEPHP@$$@SLASH@$$@SLASH@$1.jpg'>Image</a>",
  97                  "<a href='http://test.test/file.php?file=%2F1%2F%2F1.jpg'>Image</a>",
  98                  false
  99              ),
 100              array(
 101                  "<a href='$@FILEPHP@$$@SLASH@$1.jpg$@FORCEDOWNLOAD@$'>Image</a>",
 102                  "<a href='http://test.test/file.php/1/1.jpg?forcedownload=1'>Image</a>",
 103                  true
 104              ),
 105              array(
 106                  "<a href='$@FILEPHP@$$@SLASH@$1.jpg$@FORCEDOWNLOAD@$'>Image</a>",
 107                  "<a href='http://test.test/file.php?file=%2F1%2F1.jpg&amp;forcedownload=1'>Image</a>",
 108                  false
 109              ),
 110              array(
 111                  "<iframe src='$@H5PEMBED@$?url=testurl'></iframe>",
 112                  "<iframe src='http://test.test/h5p/embed.php?url=testurl'></iframe>",
 113                  true
 114              ),
 115          );
 116      }
 117  
 118      /**
 119       * Test that restore_structure_parser_processor replaces $@FILEPHP@$ to correct file php links.
 120       *
 121       * @dataProvider process_cdata_data_provider
 122       * @param string $content Testing content.
 123       * @param string $expected Expected result.
 124       * @param bool $slasharguments A value for $CFG->slasharguments setting.
 125       */
 126      public function test_process_cdata($content, $expected, $slasharguments) {
 127          global $CFG;
 128  
 129          $CFG->slasharguments = $slasharguments;
 130          $CFG->wwwroot = 'http://test.test';
 131  
 132          $processor = new restore_structure_parser_processor(1, 1);
 133  
 134          $this->assertEquals($expected, $processor->process_cdata($content));
 135      }
 136  
 137  }