Exemple #1
0
 /**
  * Get the directory of the object to restore.
  *
  * @return string|false|null subdirectory in $CFG->tempdir/backup/..., false when an error occured
  *                           and null when there is simply nothing.
  */
 protected function get_restore_content_dir()
 {
     $backupfile = null;
     $shortname = null;
     if (!empty($this->options['backupfile'])) {
         $backupfile = $this->options['backupfile'];
     } else {
         if (!empty($this->options['templatecourse']) || is_numeric($this->options['templatecourse'])) {
             $shortname = $this->options['templatecourse'];
         }
     }
     $errors = array();
     $dir = tool_uploadcourse_helper::get_restore_content_dir($backupfile, $shortname, $errors);
     if (!empty($errors)) {
         foreach ($errors as $key => $message) {
             $this->error($key, $message);
         }
         return false;
     } else {
         if ($dir === false) {
             // We want to return null when nothing was wrong, but nothing was found.
             $dir = null;
         }
     }
     if (empty($dir) && !empty($this->importoptions['restoredir'])) {
         $dir = $this->importoptions['restoredir'];
     }
     return $dir;
 }
Exemple #2
0
 public function test_get_restore_content_dir()
 {
     global $CFG;
     $this->resetAfterTest(true);
     $this->setAdminUser();
     $c1 = $this->getDataGenerator()->create_course();
     $c2 = $this->getDataGenerator()->create_course((object) array('shortname' => 'Yay'));
     // Creating backup file.
     $bc = new backup_controller(backup::TYPE_1COURSE, $c1->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2);
     $bc->execute_plan();
     $result = $bc->get_results();
     $this->assertTrue(isset($result['backup_destination']));
     $c1backupfile = $result['backup_destination']->copy_content_to_temp();
     $bc->destroy();
     unset($bc);
     // File logging is a mess, we can only try to rely on gc to close handles.
     // Creating backup file.
     $bc = new backup_controller(backup::TYPE_1COURSE, $c2->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2);
     $bc->execute_plan();
     $result = $bc->get_results();
     $this->assertTrue(isset($result['backup_destination']));
     $c2backupfile = $result['backup_destination']->copy_content_to_temp();
     $bc->destroy();
     unset($bc);
     // File logging is a mess, we can only try to rely on gc to close handles.
     $oldcfg = isset($CFG->keeptempdirectoriesonbackup) ? $CFG->keeptempdirectoriesonbackup : false;
     $CFG->keeptempdirectoriesonbackup = true;
     // Checking restore dir.
     $dir = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
     $bcinfo = backup_general_helper::get_backup_information($dir);
     $this->assertEquals($bcinfo->original_course_id, $c1->id);
     $this->assertEquals($bcinfo->original_course_fullname, $c1->fullname);
     // Do it again, it should be the same directory.
     $dir2 = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
     $this->assertEquals($dir, $dir2);
     // Get the second course.
     $dir = tool_uploadcourse_helper::get_restore_content_dir($c2backupfile, null);
     $bcinfo = backup_general_helper::get_backup_information($dir);
     $this->assertEquals($bcinfo->original_course_id, $c2->id);
     $this->assertEquals($bcinfo->original_course_fullname, $c2->fullname);
     // Checking with a shortname.
     $dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
     $bcinfo = backup_general_helper::get_backup_information($dir);
     $this->assertEquals($bcinfo->original_course_id, $c1->id);
     $this->assertEquals($bcinfo->original_course_fullname, $c1->fullname);
     // Do it again, it should be the same directory.
     $dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
     $this->assertEquals($dir, $dir2);
     // Get the second course.
     $dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c2->shortname);
     $bcinfo = backup_general_helper::get_backup_information($dir);
     $this->assertEquals($bcinfo->original_course_id, $c2->id);
     $this->assertEquals($bcinfo->original_course_fullname, $c2->fullname);
     // Get a course that does not exist.
     $errors = array();
     $dir = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors);
     $this->assertFalse($dir);
     $this->assertArrayHasKey('coursetorestorefromdoesnotexist', $errors);
     // Trying again without caching. $CFG->keeptempdirectoriesonbackup is required for caching.
     $CFG->keeptempdirectoriesonbackup = false;
     // Checking restore dir.
     $dir = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
     $dir2 = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
     $this->assertNotEquals($dir, $dir2);
     // Checking with a shortname.
     $dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
     $dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
     $this->assertNotEquals($dir, $dir2);
     // Get a course that does not exist.
     $errors = array();
     $dir = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors);
     $this->assertFalse($dir);
     $this->assertArrayHasKey('coursetorestorefromdoesnotexist', $errors);
     $dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors);
     $this->assertEquals($dir, $dir2);
     $CFG->keeptempdirectoriesonbackup = $oldcfg;
 }
Exemple #3
0
 /**
  * Get the directory of the object to restore.
  *
  * @return string subdirectory in $CFG->tempdir/backup/...
  */
 protected function get_restore_content_dir()
 {
     $backupfile = null;
     $shortname = null;
     if (!empty($this->restorefile)) {
         $backupfile = $this->restorefile;
     } else {
         if (!empty($this->templatecourse) || is_numeric($this->templatecourse)) {
             $shortname = $this->templatecourse;
         }
     }
     $dir = tool_uploadcourse_helper::get_restore_content_dir($backupfile, $shortname);
     return $dir;
 }