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   * task tests (all)
  31   */
  32  class backup_task_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_task class
  63       */
  64      function test_base_task() {
  65  
  66          $bp = new mock_base_plan('planname'); // We need one plan
  67          // Instantiate
  68          $bt = new mock_base_task('taskname', $bp);
  69          $this->assertTrue($bt instanceof base_task);
  70          $this->assertEquals($bt->get_name(), 'taskname');
  71          $this->assertTrue(is_array($bt->get_settings()));
  72          $this->assertEquals(count($bt->get_settings()), 0);
  73          $this->assertTrue(is_array($bt->get_steps()));
  74          $this->assertEquals(count($bt->get_steps()), 0);
  75      }
  76  
  77      /**
  78       * test backup_task class
  79       */
  80      function test_backup_task() {
  81  
  82          // We need one (non interactive) controller for instatiating plan
  83          $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
  84              backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
  85          // We need one plan
  86          $bp = new backup_plan($bc);
  87          // Instantiate task
  88          $bt = new mock_backup_task('taskname', $bp);
  89          $this->assertTrue($bt instanceof backup_task);
  90          $this->assertEquals($bt->get_name(), 'taskname');
  91  
  92          // Calculate checksum and check it
  93          $checksum = $bt->calculate_checksum();
  94          $this->assertTrue($bt->is_checksum_correct($checksum));
  95  
  96          $bc->destroy();
  97      }
  98  
  99      /**
 100       * wrong base_task class tests
 101       */
 102      function test_base_task_wrong() {
 103  
 104          // Try to pass one wrong plan
 105          try {
 106              $bt = new mock_base_task('tasktest', new stdclass());
 107              $this->assertTrue(false, 'base_task_exception expected');
 108          } catch (exception $e) {
 109              $this->assertTrue($e instanceof base_task_exception);
 110              $this->assertEquals($e->errorcode, 'wrong_base_plan_specified');
 111          }
 112  
 113          // Add wrong step to task
 114          $bp = new mock_base_plan('planname'); // We need one plan
 115          // Instantiate
 116          $bt = new mock_base_task('taskname', $bp);
 117          try {
 118              $bt->add_step(new stdclass());
 119              $this->assertTrue(false, 'base_task_exception expected');
 120          } catch (exception $e) {
 121              $this->assertTrue($e instanceof base_task_exception);
 122              $this->assertEquals($e->errorcode, 'wrong_base_step_specified');
 123          }
 124  
 125      }
 126  
 127      /**
 128       * wrong backup_task class tests
 129       */
 130      function test_backup_task_wrong() {
 131  
 132          // Try to pass one wrong plan
 133          try {
 134              $bt = new mock_backup_task('tasktest', new stdclass());
 135              $this->assertTrue(false, 'backup_task_exception expected');
 136          } catch (exception $e) {
 137              $this->assertTrue($e instanceof backup_task_exception);
 138              $this->assertEquals($e->errorcode, 'wrong_backup_plan_specified');
 139          }
 140      }
 141  }