diff --git a/mod/resource/tests/generator/lib.php b/mod/resource/tests/generator/lib.php new file mode 100644 index 00000000000..4be97bdfcaf --- /dev/null +++ b/mod/resource/tests/generator/lib.php @@ -0,0 +1,117 @@ +. + +/** + * Data generator. + * + * @package mod_resource + * @copyright 2013 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + + +/** + * Resource module data generator class. + * + * @package mod_resource + * @copyright 2013 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mod_resource_generator extends testing_module_generator { + + /** + * Creates new resource module instance. By default it contains a short + * text file. + * + * @param array|stdClass $record Resource module record, as from form + * @param array $options Standard options about how to create it + * @return stdClass Activity record, with extra cmid field + */ + public function create_instance($record = null, array $options = null) { + global $CFG, $USER; + require_once($CFG->dirroot . '/mod/resource/locallib.php'); + + // Count generated modules. + $this->instancecount++; + $i = $this->instancecount; + + // Ensure the record can be modified without affecting calling code. + $record = (object)(array)$record; + $options = (array)$options; + + // Course is required. + if (empty($record->course)) { + throw new coding_exception('module generator requires $record->course'); + } + + // Fill in optional values if not specified. + if (!isset($record->name)) { + $record->name = get_string('pluginname', 'resource') . ' ' . $i; + } + if (!isset($record->intro)) { + $record->intro = 'Test resource ' . $i; + } + if (!isset($record->introformat)) { + $record->introformat = FORMAT_MOODLE; + } + if (!isset($record->display)) { + $record->display = RESOURCELIB_DISPLAY_AUTO; + } + if (isset($options['idnumber'])) { + $record->cmidnumber = $options['idnumber']; + } else { + $record->cmidnumber = ''; + } + if (!isset($record->printheading)) { + $record->printheading = 1; + } + if (!isset($record->printintro)) { + $record->printintro = 0; + } + if (!isset($record->showsize)) { + $record->showsize = 0; + } + if (!isset($record->showtype)) { + $record->showtype = 0; + } + + // The 'files' value corresponds to the draft file area ID. If not + // specified, create a default file. + if (!isset($record->files)) { + if (empty($USER->username) || $USER->username === 'guest') { + throw new coding_exception('resource generator requires a current user'); + } + $usercontext = context_user::instance($USER->id); + + // Pick a random context id for specified user. + $record->files = file_get_unused_draft_itemid(); + + // Add actual file there. + $filerecord = array('component' => 'user', 'filearea' => 'draft', + 'contextid' => $usercontext->id, 'itemid' => $record->files, + 'filename' => 'resource' . $i . '.txt', 'filepath' => '/'); + $fs = get_file_storage(); + $fs->create_file_from_string($filerecord, 'Test resource ' . $i . ' file'); + } + + // Do work to actually add the instance. + $record->coursemodule = $this->precreate_course_module($record->course, $options); + $id = resource_add_instance($record, null); + return $this->post_add_instance($id, $record->coursemodule); + } +} diff --git a/mod/resource/tests/generator_test.php b/mod/resource/tests/generator_test.php new file mode 100644 index 00000000000..bd1821dbc22 --- /dev/null +++ b/mod/resource/tests/generator_test.php @@ -0,0 +1,75 @@ +. + +/** + * PHPUnit data generator tests. + * + * @package mod_resource + * @category phpunit + * @copyright 2013 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + + +/** + * PHPUnit data generator testcase. + * + * @package mod_resource + * @category phpunit + * @copyright 2013 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mod_resource_generator_testcase extends advanced_testcase { + public function test_generator() { + global $DB, $SITE; + + $this->resetAfterTest(true); + + // Must be a non-guest user to create resources. + $this->setAdminUser(); + + // There are 0 resources initially. + $this->assertEquals(0, $DB->count_records('resource')); + + // Create the generator object and do standard checks. + $generator = $this->getDataGenerator()->get_plugin_generator('mod_resource'); + $this->assertInstanceOf('mod_resource_generator', $generator); + $this->assertEquals('resource', $generator->get_modulename()); + + // Create three instances in the site course. + $generator->create_instance(array('course' => $SITE->id)); + $generator->create_instance(array('course' => $SITE->id)); + $resource = $generator->create_instance(array('course' => $SITE->id)); + $this->assertEquals(3, $DB->count_records('resource')); + + // Check the course-module is correct. + $cm = get_coursemodule_from_instance('resource', $resource->id); + $this->assertEquals($resource->id, $cm->instance); + $this->assertEquals('resource', $cm->modname); + $this->assertEquals($SITE->id, $cm->course); + + // Check the context is correct. + $context = context_module::instance($cm->id); + $this->assertEquals($resource->cmid, $context->instanceid); + + // Check that generated resource module contains a file. + $fs = get_file_storage(); + $files = $fs->get_area_files($context->id, 'mod_resource', 'content', false, '', false); + $this->assertEquals(1, count($files)); + } +}