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   * @package    core_backup
  19   * @category   test
  20   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  namespace core_backup;
  25  
  26  use restore_decode_rule;
  27  use restore_decode_rule_exception;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  // Include all the needed stuff
  32  global $CFG;
  33  require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
  34  
  35  
  36  /**
  37   * Restore_decode tests (both rule and content)
  38   *
  39   * @package    core_backup
  40   * @category   test
  41   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class decode_test extends \basic_testcase {
  45  
  46      /**
  47       * test restore_decode_rule class
  48       */
  49      function test_restore_decode_rule() {
  50  
  51          // Test various incorrect constructors
  52          try {
  53              $dr = new restore_decode_rule('28 HJH', '/index.php', array());
  54              $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
  55          } catch (\Exception $e) {
  56              $this->assertTrue($e instanceof restore_decode_rule_exception);
  57              $this->assertEquals($e->errorcode, 'decode_rule_incorrect_name');
  58              $this->assertEquals($e->a, '28 HJH');
  59          }
  60  
  61          try {
  62              $dr = new restore_decode_rule('HJHJhH', '/index.php', array());
  63              $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
  64          } catch (\Exception $e) {
  65              $this->assertTrue($e instanceof restore_decode_rule_exception);
  66              $this->assertEquals($e->errorcode, 'decode_rule_incorrect_name');
  67              $this->assertEquals($e->a, 'HJHJhH');
  68          }
  69  
  70          try {
  71              $dr = new restore_decode_rule('', '/index.php', array());
  72              $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
  73          } catch (\Exception $e) {
  74              $this->assertTrue($e instanceof restore_decode_rule_exception);
  75              $this->assertEquals($e->errorcode, 'decode_rule_incorrect_name');
  76              $this->assertEquals($e->a, '');
  77          }
  78  
  79          try {
  80              $dr = new restore_decode_rule('TESTRULE', 'index.php', array());
  81              $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
  82          } catch (\Exception $e) {
  83              $this->assertTrue($e instanceof restore_decode_rule_exception);
  84              $this->assertEquals($e->errorcode, 'decode_rule_incorrect_urltemplate');
  85              $this->assertEquals($e->a, 'index.php');
  86          }
  87  
  88          try {
  89              $dr = new restore_decode_rule('TESTRULE', '', array());
  90              $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
  91          } catch (\Exception $e) {
  92              $this->assertTrue($e instanceof restore_decode_rule_exception);
  93              $this->assertEquals($e->errorcode, 'decode_rule_incorrect_urltemplate');
  94              $this->assertEquals($e->a, '');
  95          }
  96  
  97          try {
  98              $dr = new restore_decode_rule('TESTRULE', '/course/view.php?id=$1&c=$2$3', array('test1', 'test2'));
  99              $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
 100          } catch (\Exception $e) {
 101              $this->assertTrue($e instanceof restore_decode_rule_exception);
 102              $this->assertEquals($e->errorcode, 'decode_rule_mappings_incorrect_count');
 103              $this->assertEquals($e->a->placeholders, 3);
 104              $this->assertEquals($e->a->mappings, 2);
 105          }
 106  
 107          try {
 108              $dr = new restore_decode_rule('TESTRULE', '/course/view.php?id=$5&c=$4$1', array('test1', 'test2', 'test3'));
 109              $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
 110          } catch (\Exception $e) {
 111              $this->assertTrue($e instanceof restore_decode_rule_exception);
 112              $this->assertEquals($e->errorcode, 'decode_rule_nonconsecutive_placeholders');
 113              $this->assertEquals($e->a, '1, 4, 5');
 114          }
 115  
 116          try {
 117              $dr = new restore_decode_rule('TESTRULE', '/course/view.php?id=$0&c=$3$2', array('test1', 'test2', 'test3'));
 118              $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
 119          } catch (\Exception $e) {
 120              $this->assertTrue($e instanceof restore_decode_rule_exception);
 121              $this->assertEquals($e->errorcode, 'decode_rule_nonconsecutive_placeholders');
 122              $this->assertEquals($e->a, '0, 2, 3');
 123          }
 124  
 125          try {
 126              $dr = new restore_decode_rule('TESTRULE', '/course/view.php?id=$1&c=$3$3', array('test1', 'test2', 'test3'));
 127              $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
 128          } catch (\Exception $e) {
 129              $this->assertTrue($e instanceof restore_decode_rule_exception);
 130              $this->assertEquals($e->errorcode, 'decode_rule_duplicate_placeholders');
 131              $this->assertEquals($e->a, '1, 3, 3');
 132          }
 133  
 134          // Provide some example content and test the regexp is calculated ok
 135          $content    = '$@TESTRULE*22*33*44@$';
 136          $linkname   = 'TESTRULE';
 137          $urltemplate= '/course/view.php?id=$1&c=$3$2';
 138          $mappings   = array('test1', 'test2', 'test3');
 139          $result     = '1/course/view.php?id=44&c=8866';
 140          $dr = new mock_restore_decode_rule($linkname, $urltemplate, $mappings);
 141          $this->assertEquals($dr->decode($content), $result);
 142  
 143          $content    = '$@TESTRULE*22*33*44@$ñ$@TESTRULE*22*33*44@$';
 144          $linkname   = 'TESTRULE';
 145          $urltemplate= '/course/view.php?id=$1&c=$3$2';
 146          $mappings   = array('test1', 'test2', 'test3');
 147          $result     = '1/course/view.php?id=44&c=8866ñ1/course/view.php?id=44&c=8866';
 148          $dr = new mock_restore_decode_rule($linkname, $urltemplate, $mappings);
 149          $this->assertEquals($dr->decode($content), $result);
 150  
 151          $content    = 'ñ$@TESTRULE*22*0*44@$ñ$@TESTRULE*22*33*44@$ñ';
 152          $linkname   = 'TESTRULE';
 153          $urltemplate= '/course/view.php?id=$1&c=$3$2';
 154          $mappings   = array('test1', 'test2', 'test3');
 155          $result     = 'ñ0/course/view.php?id=22&c=440ñ1/course/view.php?id=44&c=8866ñ';
 156          $dr = new mock_restore_decode_rule($linkname, $urltemplate, $mappings);
 157          $this->assertEquals($dr->decode($content), $result);
 158      }
 159  
 160      /**
 161       * test restore_decode_content class
 162       */
 163      function test_restore_decode_content() {
 164          // TODO: restore_decode_content tests
 165      }
 166  
 167      /**
 168       * test restore_decode_processor class
 169       */
 170      function test_restore_decode_processor() {
 171          // TODO: restore_decode_processor tests
 172      }
 173  }
 174  
 175  /**
 176   * Mockup restore_decode_rule for testing purposes
 177   */
 178  class mock_restore_decode_rule extends restore_decode_rule {
 179  
 180      /**
 181       * Originally protected, make it public
 182       */
 183      public function get_calculated_regexp() {
 184          return parent::get_calculated_regexp();
 185      }
 186  
 187      /**
 188       * Simply map each itemid by its double
 189       */
 190      protected function get_mapping($itemname, $itemid) {
 191          return $itemid * 2;
 192      }
 193  
 194      /**
 195       * Simply prefix with '0' non-mapped results and with '1' mapped ones
 196       */
 197      protected function apply_modifications($toreplace, $mappingsok) {
 198          return ($mappingsok ? '1' : '0') . $toreplace;
 199      }
 200  }