Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

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