Esempio n. 1
0
 /**
  * Constructor for the backup controller class.
  *
  * @param int $type Type of the backup; One of backup::TYPE_1COURSE, TYPE_1SECTION, TYPE_1ACTIVITY
  * @param int $id The ID of the item to backup; e.g the course id
  * @param int $format The backup format to use; Most likely backup::FORMAT_MOODLE
  * @param bool $interactive Whether this backup will require user interaction; backup::INTERACTIVE_YES or INTERACTIVE_NO
  * @param int $mode One of backup::MODE_GENERAL, MODE_IMPORT, MODE_SAMESITE, MODE_HUB, MODE_AUTOMATED
  * @param int $userid The id of the user making the backup
  */
 public function __construct($type, $id, $format, $interactive, $mode, $userid)
 {
     $this->type = $type;
     $this->id = $id;
     $this->courseid = backup_controller_dbops::get_courseid_from_type_id($this->type, $this->id);
     $this->format = $format;
     $this->interactive = $interactive;
     $this->mode = $mode;
     $this->userid = $userid;
     // Apply some defaults
     $this->execution = backup::EXECUTION_INMEDIATE;
     $this->operation = backup::OPERATION_BACKUP;
     $this->executiontime = 0;
     $this->checksum = '';
     // Apply current backup version and release if necessary
     backup_controller_dbops::apply_version_and_release();
     // Check format and type are correct
     backup_check::check_format_and_type($this->format, $this->type);
     // Check id is correct
     backup_check::check_id($this->type, $this->id);
     // Check user is correct
     backup_check::check_user($this->userid);
     // Calculate unique $backupid
     $this->calculate_backupid();
     // Default logger chain (based on interactive/execution)
     $this->logger = backup_factory::get_logger_chain($this->interactive, $this->execution, $this->backupid);
     // By default there is no progress reporter. Interfaces that wish to
     // display progress must set it.
     $this->progress = new \core\progress\none();
     // Instantiate the output_controller singleton and active it if interactive and inmediate
     $oc = output_controller::get_instance();
     if ($this->interactive == backup::INTERACTIVE_YES && $this->execution == backup::EXECUTION_INMEDIATE) {
         $oc->set_active(true);
     }
     $this->log('instantiating backup controller', backup::LOG_INFO, $this->backupid);
     // Default destination chain (based on type/mode/execution)
     $this->destination = backup_factory::get_destination_chain($this->type, $this->id, $this->mode, $this->execution);
     // Set initial status
     $this->set_status(backup::STATUS_CREATED);
     // Load plan (based on type/format)
     $this->load_plan();
     // Apply all default settings (based on type/format/mode)
     $this->apply_defaults();
     // Perform all initial security checks and apply (2nd param) them to settings automatically
     backup_check::check_security($this, true);
     // Set status based on interactivity
     if ($this->interactive == backup::INTERACTIVE_YES) {
         $this->set_status(backup::STATUS_SETTING_UI);
     } else {
         $this->set_status(backup::STATUS_AWAITING);
     }
 }
Esempio n. 2
0
 public function test_backup_check()
 {
     // Check against existing course module/section course or fail
     $this->assertTrue(backup_check::check_id(backup::TYPE_1ACTIVITY, $this->moduleid));
     $this->assertTrue(backup_check::check_id(backup::TYPE_1SECTION, $this->sectionid));
     $this->assertTrue(backup_check::check_id(backup::TYPE_1COURSE, $this->courseid));
     $this->assertTrue(backup_check::check_user($this->userid));
     // Check against non-existing course module/section/course (0)
     try {
         backup_check::check_id(backup::TYPE_1ACTIVITY, 0);
         $this->assertTrue(false, 'backup_controller_exception expected');
     } catch (exception $e) {
         $this->assertTrue($e instanceof backup_controller_exception);
         $this->assertEquals($e->errorcode, 'backup_check_module_not_exists');
     }
     try {
         backup_check::check_id(backup::TYPE_1SECTION, 0);
         $this->assertTrue(false, 'backup_controller_exception expected');
     } catch (exception $e) {
         $this->assertTrue($e instanceof backup_controller_exception);
         $this->assertEquals($e->errorcode, 'backup_check_section_not_exists');
     }
     try {
         backup_check::check_id(backup::TYPE_1COURSE, 0);
         $this->assertTrue(false, 'backup_controller_exception expected');
     } catch (exception $e) {
         $this->assertTrue($e instanceof backup_controller_exception);
         $this->assertEquals($e->errorcode, 'backup_check_course_not_exists');
     }
     // Try wrong type
     try {
         backup_check::check_id(12345678, 0);
         $this->assertTrue(false, 'backup_controller_exception expected');
     } catch (exception $e) {
         $this->assertTrue($e instanceof backup_controller_exception);
         $this->assertEquals($e->errorcode, 'backup_check_incorrect_type');
     }
     // Test non-existing user
     $userid = 0;
     try {
         backup_check::check_user($userid);
         $this->assertTrue(false, 'backup_controller_exception expected');
     } catch (exception $e) {
         $this->assertTrue($e instanceof backup_controller_exception);
         $this->assertEquals($e->errorcode, 'backup_check_user_not_exists');
     }
     // Security check tests
     // Try to pass wrong controller
     try {
         backup_check::check_security(new stdclass(), true);
         $this->assertTrue(false, 'backup_controller_exception expected');
     } catch (exception $e) {
         $this->assertTrue($e instanceof backup_controller_exception);
         $this->assertEquals($e->errorcode, 'backup_check_security_requires_backup_controller');
     }
     // Pass correct controller, check must return true in any case with $apply enabled
     // and $bc must continue being mock_backup_controller
     $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
     $this->assertTrue(backup_check::check_security($bc, true));
     $this->assertTrue($bc instanceof backup_controller);
 }