diff --git a/mod/imscp/lib.php b/mod/imscp/lib.php index 8824ebedfc3..fb5f825390e 100644 --- a/mod/imscp/lib.php +++ b/mod/imscp/lib.php @@ -104,9 +104,17 @@ function imscp_add_instance($data, $mform) { $context = context_module::instance($cmid); $imscp = $DB->get_record('imscp', array('id'=>$data->id), '*', MUST_EXIST); - if ($filename = $mform->get_new_filename('package')) { - if ($package = $mform->save_stored_file('package', $context->id, 'mod_imscp', 'backup', 1, '/', $filename)) { - // extract package content + if (!empty($data->package)) { + // Save uploaded files to 'backup' filearea. + $fs = get_file_storage(); + $fs->delete_area_files($context->id, 'mod_imscp', 'backup', 1); + file_save_draft_area_files($data->package, $context->id, 'mod_imscp', 'backup', + 1, array('subdirs' => 0, 'maxfiles' => 1)); + // Get filename of zip that was uploaded. + $files = $fs->get_area_files($context->id, 'mod_imscp', 'backup', 1, '', false); + if ($files) { + // Extract package content to 'content' filearea. + $package = reset($files); $packer = get_file_packer('application/zip'); $package->extract_to_storage($packer, $context->id, 'mod_imscp', 'content', 1, '/'); $structure = imscp_parse_structure($imscp, $context); @@ -139,7 +147,8 @@ function imscp_update_instance($data, $mform) { $context = context_module::instance($cmid); $imscp = $DB->get_record('imscp', array('id'=>$data->id), '*', MUST_EXIST); - if ($filename = $mform->get_new_filename('package')) { + if (!empty($data->package) && ($draftareainfo = file_get_draft_area_info($data->package)) && + $draftareainfo['filecount']) { $fs = get_file_storage(); $imscp->revision++; @@ -152,7 +161,10 @@ function imscp_update_instance($data, $mform) { $packages = array(); } - $package = $mform->save_stored_file('package', $context->id, 'mod_imscp', 'backup', $imscp->revision, '/', $filename); + file_save_draft_area_files($data->package, $context->id, 'mod_imscp', 'backup', + $imscp->revision, array('subdirs' => 0, 'maxfiles' => 1)); + $files = $fs->get_area_files($context->id, 'mod_imscp', 'backup', $imscp->revision, '', false); + $package = reset($files); // purge all extracted content $fs->delete_area_files($context->id, 'mod_imscp', 'content'); diff --git a/mod/imscp/tests/generator/lib.php b/mod/imscp/tests/generator/lib.php new file mode 100644 index 00000000000..8c869466538 --- /dev/null +++ b/mod/imscp/tests/generator/lib.php @@ -0,0 +1,71 @@ +. + +/** + * mod_imscp data generator. + * + * @package mod_imscp + * @category test + * @copyright 2013 Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * mod_imscp data generator class. + * + * @package mod_imscp + * @category test + * @copyright 2013 Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mod_imscp_generator extends testing_module_generator { + + public function create_instance($record = null, array $options = null) { + global $CFG, $USER; + + // Add default values for imscp. + $record = (array)$record + array( + 'package' => '', + 'packagepath' => $CFG->dirroot.'/mod/imscp/tests/packages/singlescobasic.zip', + 'keepold' => -1 + ); + + // The 'package' value corresponds to the draft file area ID. If not specified, create from packagepath. + if (empty($record['package'])) { + if (!isloggedin() || isguestuser()) { + throw new coding_exception('IMSCP generator requires a current user'); + } + if (!file_exists($record['packagepath'])) { + throw new coding_exception("File {$record['packagepath']} does not exist"); + } + $usercontext = context_user::instance($USER->id); + + // Pick a random context id for specified user. + $record['package'] = file_get_unused_draft_itemid(); + + // Add actual file there. + $filerecord = array('component' => 'user', 'filearea' => 'draft', + 'contextid' => $usercontext->id, 'itemid' => $record['package'], + 'filename' => basename($record['packagepath']), 'filepath' => '/'); + $fs = get_file_storage(); + $fs->create_file_from_pathname($filerecord, $record['packagepath']); + } + + return parent::create_instance($record, (array)$options); + } +} diff --git a/mod/imscp/tests/generator_test.php b/mod/imscp/tests/generator_test.php new file mode 100644 index 00000000000..fe1e8caa5e9 --- /dev/null +++ b/mod/imscp/tests/generator_test.php @@ -0,0 +1,76 @@ +. + +/** + * mod_imscp generator tests + * + * @package mod_imscp + * @category test + * @copyright 2013 Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Genarator tests class for mod_imscp. + * + * @package mod_imscp + * @category test + * @copyright 2013 Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mod_imscp_generator_testcase extends advanced_testcase { + + public function test_create_instance() { + global $DB, $CFG, $USER; + $this->resetAfterTest(); + $this->setAdminUser(); + + $course = $this->getDataGenerator()->create_course(); + + $this->assertFalse($DB->record_exists('imscp', array('course' => $course->id))); + $imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course)); + $records = $DB->get_records('imscp', array('course' => $course->id), 'id'); + $this->assertEquals(1, count($records)); + $this->assertTrue(array_key_exists($imscp->id, $records)); + + $params = array('course' => $course->id, 'name' => 'Another imscp'); + $imscp = $this->getDataGenerator()->create_module('imscp', $params); + $records = $DB->get_records('imscp', array('course' => $course->id), 'id'); + $this->assertEquals(2, count($records)); + $this->assertEquals('Another imscp', $records[$imscp->id]->name); + + // Examples of specifying the package file (do not validate anything, just check for exceptions). + // 1. As path to the file in filesystem: + $params = array( + 'course' => $course->id, + 'packagepath' => $CFG->dirroot.'/mod/imscp/tests/packages/singlescobasic.zip' + ); + $imscp = $this->getDataGenerator()->create_module('imscp', $params); + + // 2. As file draft area id: + $fs = get_file_storage(); + $params = array( + 'course' => $course->id, + 'package' => file_get_unused_draft_itemid() + ); + $usercontext = context_user::instance($USER->id); + $filerecord = array('component' => 'user', 'filearea' => 'draft', + 'contextid' => $usercontext->id, 'itemid' => $params['package'], + 'filename' => 'singlescobasic.zip', 'filepath' => '/'); + $fs->create_file_from_pathname($filerecord, $CFG->dirroot.'/mod/imscp/tests/packages/singlescobasic.zip'); + $imscp = $this->getDataGenerator()->create_module('imscp', $params); + } +}