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.

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

   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 core_backup;
  18  
  19  use restore_log_rule;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  // Include all the needed stuff
  24  global $CFG;
  25  require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
  26  
  27  /**
  28   * Test the backup and restore of logs using rules.
  29   *
  30   * @package    core_backup
  31   * @category   test
  32   * @copyright  2015 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class restore_log_rule_test extends \basic_testcase {
  36  
  37      function test_process_keeps_log_unmodified() {
  38  
  39          // Prepare a tiny log entry.
  40          $originallog = new \stdClass();
  41          $originallog->url = 'original';
  42          $originallog->info = 'original';
  43          $log = clone($originallog);
  44  
  45          // Process it with a tiny log rule, only modifying url and info.
  46          $lr = new restore_log_rule('test', 'test', 'changed', 'changed');
  47          $result = $lr->process($log);
  48  
  49          // The log has been processed.
  50          $this->assertEquals('changed', $result->url);
  51          $this->assertEquals('changed', $result->info);
  52  
  53          // But the original log has been kept unmodified by the process() call.
  54          $this->assertEquals($originallog, $log);
  55      }
  56  
  57      public function test_build_regexp() {
  58          $original = 'Any (string) with [placeholders] like {this} and {this}. [end].';
  59          $expectation = '~Any \(string\) with (.*) like (.*) and (.*)\. (.*)\.~';
  60  
  61          $lr = new restore_log_rule('this', 'doesnt', 'matter', 'here');
  62          $class = new \ReflectionClass('restore_log_rule');
  63  
  64          $method = $class->getMethod('extract_tokens');
  65          $method->setAccessible(true);
  66          $tokens = $method->invoke($lr, $original);
  67  
  68          $method = $class->getMethod('build_regexp');
  69          $method->setAccessible(true);
  70          $this->assertSame($expectation, $method->invoke($lr, $original, $tokens));
  71      }
  72  }