/**
  * 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');
     }
 }
 /**
  * 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');
     }
 }
Exemple #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));
 }
 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));
 }