Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-41852 Resource: Does not have generator

  • Loading branch information...
commit 5b15ad907ac8d0dd1e9ba40ae5e9a0b381c5ca30 1 parent 444433b
@sammarshallou sammarshallou authored dmonllao committed
View
117 mod/resource/tests/generator/lib.php
@@ -0,0 +1,117 @@
+<?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/>.
+
+/**
+ * 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);
+ }
+}
View
75 mod/resource/tests/generator_test.php
@@ -0,0 +1,75 @@
+<?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/>.
+
+/**
+ * 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));
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.