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 backup;
  27  use backup_controller;
  28  use backup_controller_dbops;
  29  use backup_controller_exception;
  30  use backup_dbops_exception;
  31  
  32  defined('MOODLE_INTERNAL') || die();
  33  
  34  // Include all the needed stuff
  35  global $CFG;
  36  require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  37  
  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 backup_dbops_test extends \advanced_testcase {
  45  
  46      protected $moduleid;  // course_modules id used for testing
  47      protected $sectionid; // course_sections id used for testing
  48      protected $courseid;  // course id used for testing
  49      protected $userid;      // user record used for testing
  50  
  51      protected function setUp(): void {
  52          global $DB, $CFG;
  53          parent::setUp();
  54  
  55          $this->resetAfterTest(true);
  56  
  57          $course = $this->getDataGenerator()->create_course();
  58          $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id), array('section'=>3));
  59          $coursemodule = $DB->get_record('course_modules', array('id'=>$page->cmid));
  60  
  61          $this->moduleid  = $page->cmid;
  62          $this->sectionid = $DB->get_field("course_sections", 'id', array("section"=>$coursemodule->section, "course"=>$course->id));
  63          $this->courseid  = $coursemodule->course;
  64          $this->userid = 2; // admin
  65  
  66          $CFG->backup_error_log_logger_level = backup::LOG_NONE;
  67          $CFG->backup_output_indented_logger_level = backup::LOG_NONE;
  68          $CFG->backup_file_logger_level = backup::LOG_NONE;
  69          $CFG->backup_database_logger_level = backup::LOG_NONE;
  70          unset($CFG->backup_file_logger_extra);
  71          $CFG->backup_file_logger_level_extra = backup::LOG_NONE;
  72      }
  73  
  74      /*
  75       * test backup_ops class
  76       */
  77      function test_backup_dbops() {
  78          // Nothing to do here, abstract class + exception, will be tested by the rest
  79      }
  80  
  81      /*
  82       * test backup_controller_dbops class
  83       */
  84      function test_backup_controller_dbops() {
  85          global $DB;
  86  
  87          $dbman = $DB->get_manager(); // Going to use some database_manager services for testing
  88  
  89          // Instantiate non interactive backup_controller
  90          $bc = new mock_backup_controller4dbops(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
  91              backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
  92          $this->assertTrue($bc instanceof backup_controller);
  93          // Calculate checksum
  94          $checksum = $bc->calculate_checksum();
  95          $this->assertEquals(strlen($checksum), 32); // is one md5
  96  
  97          // save controller
  98          $recid = backup_controller_dbops::save_controller($bc, $checksum);
  99          $this->assertNotEmpty($recid);
 100          // save it again (should cause update to happen)
 101          $recid2 = backup_controller_dbops::save_controller($bc, $checksum);
 102          $this->assertNotEmpty($recid2);
 103          $this->assertEquals($recid, $recid2); // Same record in both save operations
 104  
 105          // Try incorrect checksum
 106          $bc = new mock_backup_controller4dbops(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
 107              backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
 108          $checksum = $bc->calculate_checksum();
 109          try {
 110              $recid = backup_controller_dbops::save_controller($bc, 'lalala');
 111              $this->assertTrue(false, 'backup_dbops_exception expected');
 112          } catch (\Exception $e) {
 113              $this->assertTrue($e instanceof backup_dbops_exception);
 114              $this->assertEquals($e->errorcode, 'backup_controller_dbops_saving_checksum_mismatch');
 115          }
 116  
 117          // Try to save non backup_controller object
 118          $bc = new \stdClass();
 119          try {
 120              $recid = backup_controller_dbops::save_controller($bc, 'lalala');
 121              $this->assertTrue(false, 'backup_controller_exception expected');
 122          } catch (\Exception $e) {
 123              $this->assertTrue($e instanceof backup_controller_exception);
 124              $this->assertEquals($e->errorcode, 'backup_controller_expected');
 125          }
 126  
 127          // save and load controller (by backupid). Then compare
 128          $bc = new mock_backup_controller4dbops(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
 129              backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
 130          $checksum = $bc->calculate_checksum(); // Calculate checksum
 131          $backupid = $bc->get_backupid();
 132          $this->assertEquals(strlen($backupid), 32); // is one md5
 133          $recid = backup_controller_dbops::save_controller($bc, $checksum); // save controller
 134          $newbc = backup_controller_dbops::load_controller($backupid); // load controller
 135          $this->assertTrue($newbc instanceof backup_controller);
 136          $newchecksum = $newbc->calculate_checksum();
 137          $this->assertEquals($newchecksum, $checksum);
 138  
 139          // try to load non-existing controller
 140          try {
 141              $bc = backup_controller_dbops::load_controller('1234567890');
 142              $this->assertTrue(false, 'backup_dbops_exception expected');
 143          } catch (\Exception $e) {
 144              $this->assertTrue($e instanceof backup_dbops_exception);
 145              $this->assertEquals($e->errorcode, 'backup_controller_dbops_nonexisting');
 146          }
 147  
 148          // backup_ids_temp table tests
 149          // If, for any reason table exists, drop it
 150          if ($dbman->table_exists('backup_ids_temp')) {
 151              $dbman->drop_table(new xmldb_table('backup_ids_temp'));
 152          }
 153          // Check backup_ids_temp table doesn't exist
 154          $this->assertFalse($dbman->table_exists('backup_ids_temp'));
 155          // Create and check it exists
 156          backup_controller_dbops::create_backup_ids_temp_table('testingid');
 157          $this->assertTrue($dbman->table_exists('backup_ids_temp'));
 158          // Drop and check it doesn't exists anymore
 159          backup_controller_dbops::drop_backup_ids_temp_table('testingid');
 160          $this->assertFalse($dbman->table_exists('backup_ids_temp'));
 161  
 162          // Test encoding/decoding of backup_ids_temp,backup_files_temp encode/decode functions.
 163          // We need to handle both objects and data elements.
 164          $object = new \stdClass();
 165          $object->item1 = 10;
 166          $object->item2 = 'a String';
 167          $testarray = array($object, 10, null, 'string', array('a' => 'b', 1 => 1));
 168          foreach ($testarray as $item) {
 169              $encoded = backup_controller_dbops::encode_backup_temp_info($item);
 170              $decoded = backup_controller_dbops::decode_backup_temp_info($encoded);
 171              $this->assertEquals($item, $decoded);
 172          }
 173      }
 174  
 175      /**
 176       * Check backup_includes_files
 177       */
 178      function test_backup_controller_dbops_includes_files() {
 179          global $DB;
 180  
 181          $dbman = $DB->get_manager(); // Going to use some database_manager services for testing
 182  
 183          // A MODE_GENERAL controller - this should include files
 184          $bc = new mock_backup_controller4dbops(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
 185              backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
 186          $this->assertEquals(backup_controller_dbops::backup_includes_files($bc->get_backupid()), 1);
 187  
 188          // A MODE_IMPORT controller - should not include files
 189          $bc = new mock_backup_controller4dbops(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
 190              backup::INTERACTIVE_NO, backup::MODE_IMPORT, $this->userid);
 191          $this->assertEquals(backup_controller_dbops::backup_includes_files($bc->get_backupid()), 0);
 192  
 193          // A MODE_SAMESITE controller - should not include files
 194          $bc = new mock_backup_controller4dbops(backup::TYPE_1COURSE, $this->courseid, backup::FORMAT_MOODLE,
 195              backup::INTERACTIVE_NO, backup::MODE_SAMESITE, $this->userid);
 196          $this->assertEquals(backup_controller_dbops::backup_includes_files($bc->get_backupid()), 0);
 197      }
 198  }
 199  
 200  class mock_backup_controller4dbops extends backup_controller {
 201  
 202      /**
 203       * Change standard behavior so the checksum is also stored and not onlt calculated
 204       */
 205      public function calculate_checksum() {
 206          $this->checksum = parent::calculate_checksum();
 207          return $this->checksum;
 208      }
 209  }