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  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * @package    core_backup
  20   * @category   phpunit
  21   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  // Include all the needed stuff
  28  global $CFG;
  29  require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  30  
  31  
  32  /*
  33   * check tests (all)
  34   */
  35  class backup_check_testcase extends advanced_testcase {
  36  
  37      protected $moduleid;  // course_modules id used for testing
  38      protected $sectionid; // course_sections id used for testing
  39      protected $courseid;  // course id used for testing
  40      protected $userid;    // user record id
  41  
  42      protected function setUp(): void {
  43          global $DB, $CFG;
  44          parent::setUp();
  45  
  46          $this->resetAfterTest(true);
  47  
  48          $course = $this->getDataGenerator()->create_course(array(), array('createsections' => true));
  49          $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id), array('section'=>3));
  50          $coursemodule = $DB->get_record('course_modules', array('id'=>$page->cmid));
  51  
  52          $this->moduleid  = $coursemodule->id;
  53          $this->sectionid = $coursemodule->section;
  54          $this->courseid  = $coursemodule->course;
  55          $this->userid = 2; // admin
  56  
  57          $CFG->backup_error_log_logger_level = backup::LOG_NONE;
  58          $CFG->backup_output_indented_logger_level = backup::LOG_NONE;
  59          $CFG->backup_file_logger_level = backup::LOG_NONE;
  60          $CFG->backup_database_logger_level = backup::LOG_NONE;
  61          unset($CFG->backup_file_logger_extra);
  62          $CFG->backup_file_logger_level_extra = backup::LOG_NONE;
  63      }
  64  
  65      /*
  66       * test backup_check class
  67       */
  68      public function test_backup_check() {
  69  
  70          // Check against existing course module/section course or fail
  71          $this->assertTrue(backup_check::check_id(backup::TYPE_1ACTIVITY, $this->moduleid));
  72          $this->assertTrue(backup_check::check_id(backup::TYPE_1SECTION, $this->sectionid));
  73          $this->assertTrue(backup_check::check_id(backup::TYPE_1COURSE, $this->courseid));
  74          $this->assertTrue(backup_check::check_user($this->userid));
  75  
  76          // Check against non-existing course module/section/course (0)
  77          try {
  78              backup_check::check_id(backup::TYPE_1ACTIVITY, 0);
  79              $this->assertTrue(false, 'backup_controller_exception expected');
  80          } catch (exception $e) {
  81              $this->assertTrue($e instanceof backup_controller_exception);
  82              $this->assertEquals($e->errorcode, 'backup_check_module_not_exists');
  83          }
  84          try {
  85              backup_check::check_id(backup::TYPE_1SECTION, 0);
  86              $this->assertTrue(false, 'backup_controller_exception expected');
  87          } catch (exception $e) {
  88              $this->assertTrue($e instanceof backup_controller_exception);
  89              $this->assertEquals($e->errorcode, 'backup_check_section_not_exists');
  90          }
  91          try {
  92              backup_check::check_id(backup::TYPE_1COURSE, 0);
  93              $this->assertTrue(false, 'backup_controller_exception expected');
  94          } catch (exception $e) {
  95              $this->assertTrue($e instanceof backup_controller_exception);
  96              $this->assertEquals($e->errorcode, 'backup_check_course_not_exists');
  97          }
  98  
  99          // Try wrong type
 100          try {
 101              backup_check::check_id(12345678,0);
 102              $this->assertTrue(false, 'backup_controller_exception expected');
 103          } catch (exception $e) {
 104              $this->assertTrue($e instanceof backup_controller_exception);
 105              $this->assertEquals($e->errorcode, 'backup_check_incorrect_type');
 106          }
 107  
 108          // Test non-existing user
 109          $userid = 0;
 110          try {
 111              backup_check::check_user($userid);
 112              $this->assertTrue(false, 'backup_controller_exception expected');
 113          } catch (exception $e) {
 114              $this->assertTrue($e instanceof backup_controller_exception);
 115              $this->assertEquals($e->errorcode, 'backup_check_user_not_exists');
 116          }
 117  
 118          // Security check tests
 119          // Try to pass wrong controller
 120          try {
 121              backup_check::check_security(new stdclass(), true);
 122              $this->assertTrue(false, 'backup_controller_exception expected');
 123          } catch (exception $e) {
 124              $this->assertTrue($e instanceof backup_controller_exception);
 125              $this->assertEquals($e->errorcode, 'backup_check_security_requires_backup_controller');
 126          }
 127  
 128          // Pass correct controller, check must return true in any case with $apply enabled
 129          // and $bc must continue being mock_backup_controller
 130          $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
 131              backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
 132          $this->assertTrue(backup_check::check_security($bc, true));
 133          $this->assertTrue($bc instanceof backup_controller);
 134          $bc->destroy();
 135  
 136      }
 137  }