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 backup;
  20  use backup_controller;
  21  use backup_controller_exception;
  22  use backup_plan;
  23  use backup_plan_exception;
  24  use base_plan;
  25  use base_plan_exception;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once (__DIR__.'/fixtures/plan_fixtures.php');
  30  
  31  /**
  32   * @package    core_backup
  33   * @category   test
  34   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class plan_test extends \advanced_testcase {
  38  
  39      protected $moduleid;  // course_modules id used for testing
  40      protected $sectionid; // course_sections id used for testing
  41      protected $courseid;  // course id used for testing
  42      protected $userid;      // user record used for testing
  43  
  44      protected function setUp(): void {
  45          global $DB, $CFG;
  46          parent::setUp();
  47  
  48          $this->resetAfterTest(true);
  49  
  50          $course = $this->getDataGenerator()->create_course();
  51          $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id), array('section'=>3));
  52          $coursemodule = $DB->get_record('course_modules', array('id'=>$page->cmid));
  53  
  54          $this->moduleid  = $coursemodule->id;
  55          $this->sectionid = $DB->get_field("course_sections", 'id', array("section"=>$coursemodule->section, "course"=>$course->id));
  56          $this->courseid  = $coursemodule->course;
  57          $this->userid = 2; // admin
  58  
  59          // Disable all loggers
  60          $CFG->backup_error_log_logger_level = backup::LOG_NONE;
  61          $CFG->backup_file_logger_level = backup::LOG_NONE;
  62          $CFG->backup_database_logger_level = backup::LOG_NONE;
  63          $CFG->backup_file_logger_level_extra = backup::LOG_NONE;
  64      }
  65  
  66      /**
  67       * test base_plan class
  68       */
  69      function test_base_plan() {
  70  
  71          // Instantiate
  72          $bp = new \mock_base_plan('name');
  73          $this->assertTrue($bp instanceof base_plan);
  74          $this->assertEquals($bp->get_name(), 'name');
  75          $this->assertTrue(is_array($bp->get_settings()));
  76          $this->assertEquals(count($bp->get_settings()), 0);
  77          $this->assertTrue(is_array($bp->get_tasks()));
  78          $this->assertEquals(count($bp->get_tasks()), 0);
  79      }
  80  
  81      /**
  82       * test backup_plan class
  83       */
  84      function test_backup_plan() {
  85  
  86          // We need one (non interactive) controller for instantiating plan
  87          $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
  88              backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
  89          // Instantiate one backup plan
  90          $bp = new backup_plan($bc);
  91          $this->assertTrue($bp instanceof backup_plan);
  92          $this->assertEquals($bp->get_name(), 'backup_plan');
  93  
  94          // Calculate checksum and check it
  95          $checksum = $bp->calculate_checksum();
  96          $this->assertTrue($bp->is_checksum_correct($checksum));
  97  
  98          $bc->destroy();
  99      }
 100  
 101      /**
 102       * wrong base_plan class tests
 103       */
 104      function test_base_plan_wrong() {
 105  
 106          // We need one (non interactive) controller for instantiating plan
 107          $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
 108              backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
 109          // Instantiate one backup plan
 110          $bp = new backup_plan($bc);
 111          // Add wrong task
 112          try {
 113              $bp->add_task(new \stdClass());
 114              $this->assertTrue(false, 'base_plan_exception expected');
 115          } catch (\Exception $e) {
 116              $this->assertTrue($e instanceof base_plan_exception);
 117              $this->assertEquals($e->errorcode, 'wrong_base_task_specified');
 118          }
 119      }
 120  
 121      /**
 122       * wrong backup_plan class tests
 123       */
 124      function test_backup_plan_wrong() {
 125  
 126          // Try to pass one wrong controller
 127          try {
 128              $bp = new backup_plan(new \stdClass());
 129              $this->assertTrue(false, 'backup_plan_exception expected');
 130          } catch (\Exception $e) {
 131              $this->assertTrue($e instanceof backup_plan_exception);
 132              $this->assertEquals($e->errorcode, 'wrong_backup_controller_specified');
 133          }
 134          try {
 135              $bp = new backup_plan(null);
 136              $this->assertTrue(false, 'backup_plan_exception expected');
 137          } catch (\Exception $e) {
 138              $this->assertTrue($e instanceof backup_plan_exception);
 139              $this->assertEquals($e->errorcode, 'wrong_backup_controller_specified');
 140          }
 141  
 142          // Try to build one non-existent format plan (when creating the controller)
 143          // We need one (non interactive) controller for instatiating plan
 144          try {
 145              $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, 'non_existing_format',
 146                  backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
 147              $this->assertTrue(false, 'backup_controller_exception expected');
 148          } catch (\Exception $e) {
 149              $this->assertTrue($e instanceof backup_controller_exception);
 150              $this->assertEquals($e->errorcode, 'backup_check_unsupported_format');
 151              $this->assertEquals($e->a, 'non_existing_format');
 152          }
 153      }
 154  }
 155