[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/backup/controller/tests/ -> controller_test.php (source)

   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  // Include all the needed stuff
  27  global $CFG;
  28  require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  29  require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
  30  
  31  /*
  32   * controller tests (all)
  33   */
  34  class core_backup_controller_testcase extends advanced_testcase {
  35  
  36      protected $moduleid;  // course_modules id used for testing
  37      protected $sectionid; // course_sections id used for testing
  38      protected $courseid;  // course id used for testing
  39      protected $userid;    // user used if for testing
  40  
  41      protected function setUp() {
  42          global $DB, $CFG;
  43  
  44          $this->resetAfterTest(true);
  45  
  46          $course = $this->getDataGenerator()->create_course();
  47          $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id), array('section'=>3));
  48          $coursemodule = $DB->get_record('course_modules', array('id'=>$page->cmid));
  49  
  50          $this->moduleid  = $coursemodule->id;
  51          $this->sectionid = $DB->get_field("course_sections", 'id', array("section"=>$coursemodule->section, "course"=>$course->id));
  52          $this->courseid  = $coursemodule->course;
  53          $this->userid = 2; // admin
  54  
  55          // Disable all loggers
  56          $CFG->backup_error_log_logger_level = backup::LOG_NONE;
  57          $CFG->backup_output_indented_logger_level = backup::LOG_NONE;
  58          $CFG->backup_file_logger_level = backup::LOG_NONE;
  59          $CFG->backup_database_logger_level = backup::LOG_NONE;
  60          $CFG->backup_file_logger_level_extra = backup::LOG_NONE;
  61      }
  62  
  63      /*
  64       * test base_setting class
  65       */
  66      public function test_backup_controller() {
  67          // Instantiate non interactive backup_controller
  68          $bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
  69              backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
  70          $this->assertTrue($bc instanceof backup_controller);
  71          $this->assertEquals($bc->get_status(), backup::STATUS_AWAITING);
  72          // Instantiate interactive backup_controller
  73          $bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
  74              backup::INTERACTIVE_YES, backup::MODE_GENERAL, $this->userid);
  75          $this->assertTrue($bc instanceof backup_controller);
  76          $this->assertEquals($bc->get_status(), backup::STATUS_SETTING_UI);
  77          $this->assertEquals(strlen($bc->get_backupid()), 32); // is one md5
  78  
  79          // Save and load one backup controller to check everything is in place
  80          $bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
  81              backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
  82          $recid = $bc->save_controller();
  83          $newbc = mock_backup_controller::load_controller($bc->get_backupid());
  84          $this->assertTrue($newbc instanceof backup_controller); // This means checksum and load worked ok
  85      }
  86  
  87      public function test_backup_controller_include_files() {
  88          // A MODE_GENERAL controller - this should include files
  89          $bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
  90              backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
  91          $this->assertEquals($bc->get_include_files(), 1);
  92  
  93  
  94          // The MODE_IMPORT and MODE_SAMESITE should not include files in the backup.
  95          // A MODE_IMPORT controller
  96          $bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
  97              backup::INTERACTIVE_NO, backup::MODE_IMPORT, $this->userid);
  98          $this->assertEquals($bc->get_include_files(), 0);
  99  
 100          // A MODE_SAMESITE controller
 101          $bc = new mock_backup_controller(backup::TYPE_1COURSE, $this->courseid, backup::FORMAT_MOODLE,
 102              backup::INTERACTIVE_NO, backup::MODE_IMPORT, $this->userid);
 103          $this->assertEquals($bc->get_include_files(), 0);
 104      }
 105  
 106      /**
 107       * Tests the restore_controller.
 108       */
 109      public function test_restore_controller_is_executing() {
 110          global $CFG;
 111  
 112          // Make a backup.
 113          check_dir_exists($CFG->tempdir . '/backup');
 114          $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
 115              backup::INTERACTIVE_NO, backup::MODE_IMPORT, $this->userid);
 116          $backupid = $bc->get_backupid();
 117          $bc->execute_plan();
 118          $bc->destroy();
 119  
 120          // The progress class will get called during restore, so we can use that
 121          // to check the executing flag is true.
 122          $progress = new core_backup_progress_restore_is_executing();
 123  
 124          // Set up restore.
 125          $rc = new restore_controller($backupid, $this->courseid,
 126                  backup::INTERACTIVE_NO, backup::MODE_SAMESITE, $this->userid,
 127                  backup::TARGET_EXISTING_ADDING);
 128          $this->assertTrue($rc->execute_precheck());
 129  
 130          // Check restore is NOT executing.
 131          $this->assertFalse(restore_controller::is_executing());
 132  
 133          // Execute restore.
 134          $rc->set_progress($progress);
 135          $rc->execute_plan();
 136  
 137          // Check restore is NOT executing afterward either.
 138          $this->assertFalse(restore_controller::is_executing());
 139          $rc->destroy();
 140  
 141          // During restore, check that executing was true.
 142          $this->assertTrue(count($progress->executing) > 0);
 143          $alltrue = true;
 144          foreach ($progress->executing as $executing) {
 145              if (!$executing) {
 146                  $alltrue = false;
 147                  break;
 148              }
 149          }
 150          $this->assertTrue($alltrue);
 151      }
 152  }
 153  
 154  
 155  /**
 156   * Progress class that records the result of restore_controller::is_executing calls.
 157   *
 158   * @package core_backup
 159   * @copyright 2014 The Open University
 160   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 161   */
 162  class core_backup_progress_restore_is_executing extends \core\progress\base {
 163      /** @var array Array of results from calling function */
 164      public $executing = array();
 165  
 166      public function update_progress() {
 167          $this->executing[] = restore_controller::is_executing();
 168      }
 169  }
 170  
 171  
 172  /*
 173   * helper extended @backup_controller class that makes some methods public for testing
 174   */
 175  class mock_backup_controller extends backup_controller {
 176  
 177      public function save_controller($includeobj = true, $cleanobj = false) {
 178          parent::save_controller($includeobj, $cleanobj);
 179      }
 180  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1