mirror of
https://github.com/moodle/moodle.git
synced 2025-01-29 19:50:14 +01:00
MDL-42508 imspcp: add module generator
This commit is contained in:
parent
15cca13822
commit
490dca1a6f
@ -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');
|
||||
|
71
mod/imscp/tests/generator/lib.php
Normal file
71
mod/imscp/tests/generator/lib.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
76
mod/imscp/tests/generator_test.php
Normal file
76
mod/imscp/tests/generator_test.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user