public function test_archive_with_both_options()
 {
     global $CFG;
     $this->resetAfterTest();
     // Get backup packer.
     $packer = get_file_packer('application/vnd.moodle.backup');
     require_once $CFG->dirroot . '/lib/filestorage/tgz_packer.php';
     if (!tgz_packer::has_required_extension()) {
         $this->markTestSkipped('zlib not available');
         return;
     }
     // Set up basic archive contents.
     $files = array('1.txt' => array('frog'));
     // Create 2 archives (each with one file in) in default mode.
     $CFG->enabletgzbackups = false;
     $filefalse = $CFG->tempdir . '/false.mbz';
     $this->assertNotEmpty($packer->archive_to_pathname($files, $filefalse));
     $context = context_system::instance();
     $this->assertNotEmpty($storagefalse = $packer->archive_to_storage($files, $context->id, 'phpunit', 'data', 0, '/', 'false.mbz'));
     // Create 2 archives in tgz mode.
     $CFG->enabletgzbackups = true;
     $filetrue = $CFG->tempdir . '/true.mbz';
     $this->assertNotEmpty($packer->archive_to_pathname($files, $filetrue));
     $context = context_system::instance();
     $this->assertNotEmpty($storagetrue = $packer->archive_to_storage($files, $context->id, 'phpunit', 'data', 0, '/', 'false.mbz'));
     // Check the sizes are different (indicating different formats).
     $this->assertNotEquals(filesize($filefalse), filesize($filetrue));
     $this->assertNotEquals($storagefalse->get_filesize(), $storagetrue->get_filesize());
     // Extract files into storage and into filesystem from both formats.
     // (Note: the setting does not matter, but set to false just to check.)
     $CFG->enabletgzbackups = false;
     // Extract to path (zip).
     $packer->extract_to_pathname($filefalse, $CFG->tempdir);
     $onefile = $CFG->tempdir . '/1.txt';
     $this->assertEquals('frog', file_get_contents($onefile));
     unlink($onefile);
     // Extract to path (tgz).
     $packer->extract_to_pathname($filetrue, $CFG->tempdir);
     $onefile = $CFG->tempdir . '/1.txt';
     $this->assertEquals('frog', file_get_contents($onefile));
     unlink($onefile);
     // Extract to storage (zip).
     $packer->extract_to_storage($storagefalse, $context->id, 'phpunit', 'data', 1, '/');
     $fs = get_file_storage();
     $out = $fs->get_file($context->id, 'phpunit', 'data', 1, '/', '1.txt');
     $this->assertNotEmpty($out);
     $this->assertEquals('frog', $out->get_content());
     // Extract to storage (tgz).
     $packer->extract_to_storage($storagetrue, $context->id, 'phpunit', 'data', 2, '/');
     $out = $fs->get_file($context->id, 'phpunit', 'data', 2, '/', '1.txt');
     $this->assertNotEmpty($out);
     $this->assertEquals('frog', $out->get_content());
 }
 /**
  * Selects appropriate packer for existing archive depending on file contents.
  *
  * @param string|stored_file $archivefile full pathname of zip file or stored_file instance
  * @return file_packer Suitable packer
  */
 protected function get_packer_for_read_operation($archivefile)
 {
     global $CFG;
     require_once $CFG->dirroot . '/lib/filestorage/tgz_packer.php';
     if (tgz_packer::is_tgz_file($archivefile)) {
         return get_file_packer('application/x-gzip');
     } else {
         return get_file_packer('application/zip');
     }
 }
Esempio n. 3
0
 public function test_is_tgz_file()
 {
     global $CFG;
     // Set up.
     $filelist = $this->prepare_file_list();
     $packer1 = get_file_packer('application/x-gzip');
     $packer2 = get_file_packer('application/zip');
     $archive2 = "{$CFG->tempdir}/archive.zip";
     // Archive in tgz and zip format.
     $context = context_system::instance();
     $archive1 = $packer1->archive_to_storage($filelist, $context->id, 'phpunit', 'test', 0, '/', 'archive.tgz', null, true, $this);
     $this->assertInstanceOf('stored_file', $archive1);
     $result = $packer2->archive_to_pathname($filelist, $archive2);
     $this->assertTrue($result);
     // Use is_tgz_file to detect which is which. First check is from storage,
     // second check is from filesystem.
     $this->assertTrue(tgz_packer::is_tgz_file($archive1));
     $this->assertFalse(tgz_packer::is_tgz_file($archive2));
 }
 /**
  * Checks if zlib is available. If not, marks current test as skipped.
  *
  * @return bool True if text should be skipped
  */
 protected function skip_because_missing_zlib()
 {
     global $CFG;
     require_once $CFG->dirroot . '/lib/filestorage/tgz_packer.php';
     if (!tgz_packer::has_required_extension()) {
         $this->markTestSkipped('zlib not available');
         return true;
     }
     return false;
 }
 /**
  * @see tgz_extractor_handler::tgz_end_file()
  */
 public function tgz_end_file($archivepath, $realpath)
 {
     // Place temp file into storage.
     $fs = get_file_storage();
     $filerecord = array('contextid' => $this->contextid, 'component' => $this->component, 'filearea' => $this->filearea, 'itemid' => $this->itemid);
     $filerecord['filepath'] = $this->pathbase . dirname($archivepath) . '/';
     $filerecord['filename'] = basename($archivepath);
     if ($this->userid) {
         $filerecord['userid'] = $this->userid;
     }
     // Delete existing file (if any) and create new one.
     tgz_packer::delete_existing_file_record($fs, $filerecord);
     $fs->create_file_from_pathname($filerecord, $this->tempfile);
     unlink($this->tempfile);
 }
Esempio n. 6
0
 /**
  * Selects appropriate packer for existing archive depending on file contents.
  *
  * @param string|stored_file $archivefile full pathname of zip file or stored_file instance
  * @return file_packer Suitable packer
  * @throws moodle_exception If the file cannot be restored because of missing zlib
  */
 protected function get_packer_for_read_operation($archivefile)
 {
     global $CFG;
     require_once $CFG->dirroot . '/lib/filestorage/tgz_packer.php';
     if (tgz_packer::is_tgz_file($archivefile)) {
         if (tgz_packer::has_required_extension()) {
             return get_file_packer('application/x-gzip');
         } else {
             throw new moodle_exception('errortgznozlib', 'backup');
         }
     } else {
         return get_file_packer('application/zip');
     }
 }
 public function test_backup_restore()
 {
     // TODO this test does not check if userids are correctly mapped
     global $CFG, $DB;
     core_php_time_limit::raise();
     // Set to admin user.
     $this->setAdminUser();
     $gen_mod = new mod_ratingallocate_generated_module($this);
     $course1 = $gen_mod->course;
     // Create backup file and save it to the backup location.
     $bc = new backup_controller(backup::TYPE_1ACTIVITY, $gen_mod->mod_db->cmid, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2);
     $bc->execute_plan();
     $results = $bc->get_results();
     $file = $results['backup_destination'];
     //TODO: Necessary to ensure backward compatibility
     if (tgz_packer::is_tgz_file($file)) {
         $fp = get_file_packer('application/x-gzip');
     } else {
         $fp = get_file_packer();
     }
     $filepath = $CFG->dataroot . '/temp/backup/test-restore-course';
     $file->extract_to_pathname($fp, $filepath);
     $bc->destroy();
     unset($bc);
     // Create a course that we are going to restore the other course to.
     $course2 = $this->getDataGenerator()->create_course();
     // Now restore the course.
     $rc = new restore_controller('test-restore-course', $course2->id, backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2, backup::TARGET_NEW_COURSE);
     $rc->execute_precheck();
     $rc->execute_plan();
     $unset_values = function ($elem1, $elem2, $varname) {
         $this->assertNotEquals($elem1->{$varname}, $elem2->{$varname});
         $result = array($elem1->{$varname}, $elem2->{$varname});
         unset($elem1->{$varname});
         unset($elem2->{$varname});
         return $result;
     };
     $ratingallocate1 = $DB->get_record(this_db\ratingallocate::TABLE, array(this_db\ratingallocate::COURSE => $course1->id));
     $ratingallocate2 = $DB->get_record(this_db\ratingallocate::TABLE, array(this_db\ratingallocate::COURSE => $course2->id));
     list($rating_id1, $rating_id2) = $unset_values($ratingallocate1, $ratingallocate2, this_db\ratingallocate::ID);
     $unset_values($ratingallocate1, $ratingallocate2, this_db\ratingallocate::COURSE);
     $this->assertEquals($ratingallocate1, $ratingallocate2);
     $choices1 = $DB->get_records(this_db\ratingallocate_choices::TABLE, array(this_db\ratingallocate_choices::RATINGALLOCATEID => $rating_id1), this_db\ratingallocate_choices::TITLE);
     $choices2 = $DB->get_records(this_db\ratingallocate_choices::TABLE, array(this_db\ratingallocate_choices::RATINGALLOCATEID => $rating_id2), this_db\ratingallocate_choices::TITLE);
     $this->assertCount(2, $choices1);
     $this->assertCount(2, array_values($choices2));
     $choice2_copy = $choices2;
     foreach ($choices1 as $choice1) {
         //work with copies
         $choice2 = json_decode(json_encode(array_shift($choice2_copy)));
         $choice1 = json_decode(json_encode($choice1));
         list($choiceid1, $choiceid2) = $unset_values($choice1, $choice2, this_db\ratingallocate_choices::ID);
         $unset_values($choice1, $choice2, this_db\ratingallocate_choices::RATINGALLOCATEID);
         $this->assertEquals($choice1, $choice2);
         // compare ratings for this choice
         $ratings1 = array_values($DB->get_records(this_db\ratingallocate_ratings::TABLE, array(this_db\ratingallocate_ratings::CHOICEID => $choiceid1), this_db\ratingallocate_ratings::USERID));
         $ratings2 = array_values($DB->get_records(this_db\ratingallocate_ratings::TABLE, array(this_db\ratingallocate_ratings::CHOICEID => $choiceid2), this_db\ratingallocate_ratings::USERID));
         $this->assertEquals(count($ratings1), count($ratings2));
         $ratings2_copy = $ratings2;
         foreach ($ratings1 as $rating1) {
             $rating2 = json_decode(json_encode(array_shift($ratings2_copy)));
             $rating1 = json_decode(json_encode($rating1));
             $unset_values($rating1, $rating2, this_db\ratingallocate_ratings::CHOICEID);
             $unset_values($rating1, $rating2, this_db\ratingallocate_ratings::ID);
             $this->assertEquals($rating1, $rating2);
         }
     }
     // compare allocations
     $allocations1 = $DB->get_records(this_db\ratingallocate_allocations::TABLE, array(this_db\ratingallocate_allocations::RATINGALLOCATEID => $rating_id1), this_db\ratingallocate_allocations::USERID);
     $allocations2 = $DB->get_records(this_db\ratingallocate_allocations::TABLE, array(this_db\ratingallocate_allocations::RATINGALLOCATEID => $rating_id2), this_db\ratingallocate_allocations::USERID);
     // number of allocations is equal
     //$this->assertCount(count($allocations1), $allocations2);
     $this->assertCount(count($gen_mod->allocations), $allocations2);
     // create function that can be used to replace
     $map_allocation_to_choice_title = function (&$alloc, $choices) {
         $alloc->{'choice_title'} = $choices[$alloc->{this_db\ratingallocate_allocations::CHOICEID}]->{this_db\ratingallocate_choices::TITLE};
     };
     // compare allocations in detail!
     $alloc2 = reset($allocations2);
     foreach ($allocations1 as &$alloc1) {
         $map_allocation_to_choice_title($alloc1, $choices1);
         $map_allocation_to_choice_title($alloc2, $choices2);
         $unset_values($alloc1, $alloc2, this_db\ratingallocate_allocations::RATINGALLOCATEID);
         $unset_values($alloc1, $alloc2, this_db\ratingallocate_allocations::CHOICEID);
         $unset_values($alloc1, $alloc2, this_db\ratingallocate_allocations::ID);
         $alloc2 = next($allocations2);
     }
     $this->assertEquals(array_values($allocations1), array_values($allocations2));
 }