MDL-32400 improve module generators

Module generators are using standard *_add_instance() methods which helps with testing, it also updates grades and calendar events if used.
This commit is contained in:
Petr Skoda 2012-04-15 14:07:21 +02:00
parent 322cf28415
commit 6b04fdc0b6
13 changed files with 177 additions and 81 deletions

View File

@ -262,6 +262,9 @@ class grade_base_testcase extends advanced_testcase {
protected function load_grade_items() {
global $DB;
// purge all items created by module generators
$DB->delete_records('grade_items', array('itemtype'=>'mod'));
$course_category = grade_category::fetch_course_category($this->course->id);
// id = 0

View File

@ -505,20 +505,20 @@ abstract class phpunit_module_generator {
/**
* Create course module and link it to course
* @param stdClass $instance
* @param int $courseid
* @param array $options: section, visible
* @return stdClass $cm instance
* @return int $cm instance id
*/
protected function create_course_module(stdClass $instance, array $options) {
protected function precreate_course_module($courseid, array $options) {
global $DB, $CFG;
require_once("$CFG->dirroot/course/lib.php");
$modulename = $this->get_modulename();
$cm = new stdClass();
$cm->course = $instance->course;
$cm->course = $courseid;
$cm->module = $DB->get_field('modules', 'id', array('name'=>$modulename));
$cm->instance = $instance->id;
$cm->instance = 0;
$cm->section = isset($options['section']) ? $options['section'] : 0;
$cm->idnumber = isset($options['idnumber']) ? $options['idnumber'] : 0;
$cm->added = time();
@ -539,11 +539,28 @@ abstract class phpunit_module_generator {
add_mod_to_section($cm);
$cm = get_coursemodule_from_id($modulename, $cm->id, $cm->course, true, MUST_EXIST);
return $cm->id;
}
/**
* Called after *_add_instance()
* @param int $id
* @param int $cmid
* @return stdClass module instance
*/
protected function post_add_instance($id, $cmid) {
global $DB;
$DB->set_field('course_modules', 'instance', $id, array('id'=>$cmid));
$instance = $DB->get_record($this->get_modulename(), array('id'=>$id), '*', MUST_EXIST);
$cm = get_coursemodule_from_id($this->get_modulename(), $cmid, $instance->course, true, MUST_EXIST);
context_module::instance($cm->id);
return $cm;
$instance->cmid = $cm->id;
return $instance;
}
/**

View File

@ -2708,8 +2708,12 @@ function assignment_update_instance($assignment){
* Adds an assignment instance
*
* This is done by calling the add_instance() method of the assignment type class
*
* @param stdClass $assignment
* @param mod_assignment_mod_form $mform
* @return int intance id
*/
function assignment_add_instance($assignment) {
function assignment_add_instance($assignment, $mform = null) {
global $CFG;
$assignment->assignmenttype = clean_param($assignment->assignmenttype, PARAM_PLUGIN);

View File

@ -39,11 +39,11 @@ class mod_assignment_generator extends phpunit_module_generator {
/**
* Create new assignment module instance
* @param array|stdClass $record
* @param array $options
* @param array $options (mostly course_module properties)
* @return stdClass activity record with extra cmid field
*/
public function create_instance($record = null, array $options = null) {
global $DB, $CFG;
global $CFG;
require_once("$CFG->dirroot/mod/assignment/locallib.php");
$this->instancecount++;
@ -52,6 +52,9 @@ class mod_assignment_generator extends phpunit_module_generator {
$record = (object)(array)$record;
$options = (array)$options;
if (empty($record->course)) {
throw new coding_exception('module generator requires $record->course');
}
if (!isset($record->name)) {
$record->name = get_string('pluginname', 'assignment').' '.$i;
}
@ -67,15 +70,17 @@ class mod_assignment_generator extends phpunit_module_generator {
if (!isset($record->grade)) {
$record->grade = 100;
}
$record->timemodified = time();
if (!isset($record->timedue)) {
$record->timedue = 0;
}
if (isset($options['idnumber'])) {
$record->cmidnumber = $options['idnumber'];
} else {
$record->cmidnumber = '';
}
$id = $DB->insert_record('assignment', $record);
$instance = $DB->get_record('assignment', array('id'=>$id), '*', MUST_EXIST);
$cm = $this->create_course_module($instance, $options);
$instance->cmid = $cm->id;
return $instance;
$record->coursemodule = $this->precreate_course_module($record->course, $options);
$id = assignment_add_instance($record, null);
return $this->post_add_instance($id, $record->coursemodule);
}
}

View File

@ -36,28 +36,42 @@ defined('MOODLE_INTERNAL') || die();
*/
class mod_assignment_generator_testcase extends advanced_testcase {
public function test_generator() {
global $DB, $SITE;
global $DB;
$this->resetAfterTest(true);
$this->assertEquals(0, $DB->count_records('assignment'));
$course = $this->getDataGenerator()->create_course();
/** @var mod_assignment_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('mod_assignment');
$this->assertInstanceOf('mod_assignment_generator', $generator);
$this->assertEquals('assignment', $generator->get_modulename());
$generator->create_instance(array('course'=>$SITE->id));
$generator->create_instance(array('course'=>$SITE->id));
$assignment = $generator->create_instance(array('course'=>$SITE->id));
$generator->create_instance(array('course'=>$course->id, 'grade'=>0));
$generator->create_instance(array('course'=>$course->id, 'grade'=>0));
$assignment = $generator->create_instance(array('course'=>$course->id, 'grade'=>100));
$this->assertEquals(3, $DB->count_records('assignment'));
$cm = get_coursemodule_from_instance('assignment', $assignment->id);
$this->assertEquals($assignment->id, $cm->instance);
$this->assertEquals('assignment', $cm->modname);
$this->assertEquals($SITE->id, $cm->course);
$this->assertEquals($course->id, $cm->course);
$context = context_module::instance($cm->id);
$this->assertEquals($assignment->cmid, $context->instanceid);
// test gradebook integration using low level DB access - DO NOT USE IN PLUGIN CODE!
$gitem = $DB->get_record('grade_items', array('courseid'=>$course->id, 'itemtype'=>'mod', 'itemmodule'=>'assignment', 'iteminstance'=>$assignment->id));
$this->assertNotEmpty($gitem);
$this->assertEquals(100, $gitem->grademax);
$this->assertEquals(0, $gitem->grademin);
$this->assertEquals(GRADE_TYPE_VALUE, $gitem->gradetype);
// test eventslib integration
$this->setUser(2); // admin
$generator->create_instance(array('course'=>$course->id, 'timedue'=>(time()+60*60+24)));
$this->setUser(0);
}
}

View File

@ -829,11 +829,11 @@ function data_tags_check($dataid, $template) {
/**
* Adds an instance of a data
*
* @global object
* @param object $data
* @return $int
* @param stdClass $data
* @param mod_data_mod_form $mform
* @return int intance id
*/
function data_add_instance($data) {
function data_add_instance($data, $mform = null) {
global $DB;
if (empty($data->assessed)) {

View File

@ -39,11 +39,12 @@ class mod_data_generator extends phpunit_module_generator {
/**
* Create new data module instance
* @param array|stdClass $record
* @param array $options
* @param array $options (mostly course_module properties)
* @return stdClass activity record with extra cmid field
*/
public function create_instance($record = null, array $options = null) {
global $DB;
global $CFG;
require_once("$CFG->dirroot/mod/data/locallib.php");
$this->instancecount++;
$i = $this->instancecount;
@ -51,6 +52,9 @@ class mod_data_generator extends phpunit_module_generator {
$record = (object)(array)$record;
$options = (array)$options;
if (empty($record->course)) {
throw new coding_exception('module generator requires $record->course');
}
if (!isset($record->name)) {
$record->name = get_string('pluginname', 'data').' '.$i;
}
@ -60,15 +64,20 @@ class mod_data_generator extends phpunit_module_generator {
if (!isset($record->introformat)) {
$record->introformat = FORMAT_MOODLE;
}
$record->timemodified = time();
if (!isset($record->assessed)) {
$record->assessed = 0;
}
if (!isset($record->scale)) {
$record->scale = 0;
}
if (isset($options['idnumber'])) {
$record->cmidnumber = $options['idnumber'];
} else {
$record->cmidnumber = '';
}
$id = $DB->insert_record('data', $record);
$instance = $DB->get_record('data', array('id'=>$id), '*', MUST_EXIST);
$cm = $this->create_course_module($instance, $options);
$instance->cmid = $cm->id;
return $instance;
$record->coursemodule = $this->precreate_course_module($record->course, $options);
$id = data_add_instance($record, null);
return $this->post_add_instance($id, $record->coursemodule);
}
}

View File

@ -36,28 +36,39 @@ defined('MOODLE_INTERNAL') || die();
*/
class mod_data_generator_testcase extends advanced_testcase {
public function test_generator() {
global $DB, $SITE;
global $DB;
$this->resetAfterTest(true);
$this->assertEquals(0, $DB->count_records('data'));
$course = $this->getDataGenerator()->create_course();
/** @var mod_data_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
$this->assertInstanceOf('mod_data_generator', $generator);
$this->assertEquals('data', $generator->get_modulename());
$generator->create_instance(array('course'=>$SITE->id));
$generator->create_instance(array('course'=>$SITE->id));
$data = $generator->create_instance(array('course'=>$SITE->id));
$generator->create_instance(array('course'=>$course->id));
$generator->create_instance(array('course'=>$course->id));
$data = $generator->create_instance(array('course'=>$course->id));
$this->assertEquals(3, $DB->count_records('data'));
$cm = get_coursemodule_from_instance('data', $data->id);
$this->assertEquals($data->id, $cm->instance);
$this->assertEquals('data', $cm->modname);
$this->assertEquals($SITE->id, $cm->course);
$this->assertEquals($course->id, $cm->course);
$context = context_module::instance($cm->id);
$this->assertEquals($data->cmid, $context->instanceid);
// test gradebook integration using low level DB access - DO NOT USE IN PLUGIN CODE!
$data = $generator->create_instance(array('course'=>$course->id, 'assessed'=>1, 'scale'=>100));
$gitem = $DB->get_record('grade_items', array('courseid'=>$course->id, 'itemtype'=>'mod', 'itemmodule'=>'data', 'iteminstance'=>$data->id));
$this->assertNotEmpty($gitem);
$this->assertEquals(100, $gitem->grademax);
$this->assertEquals(0, $gitem->grademin);
$this->assertEquals(GRADE_TYPE_VALUE, $gitem->gradetype);
}
}

View File

@ -52,12 +52,11 @@ define('FORUM_TRACKING_ON', 2);
* will create a new instance and return the id number
* of the new instance.
*
* @global object
* @global object
* @param object $forum add forum instance (with magic quotes)
* @param stdClass $forum add forum instance
* @param mod_forum_mod_form $mform
* @return int intance id
*/
function forum_add_instance($forum, $mform) {
function forum_add_instance($forum, $mform = null) {
global $CFG, $DB;
$forum->timemodified = time();

View File

@ -43,7 +43,7 @@ class mod_forum_generator extends phpunit_module_generator {
* @return stdClass activity record with extra cmid field
*/
public function create_instance($record = null, array $options = null) {
global $DB, $CFG;
global $CFG;
require_once("$CFG->dirroot/mod/forum/locallib.php");
$this->instancecount++;
@ -52,6 +52,9 @@ class mod_forum_generator extends phpunit_module_generator {
$record = (object)(array)$record;
$options = (array)$options;
if (empty($record->course)) {
throw new coding_exception('module generator requires $record->course');
}
if (!isset($record->name)) {
$record->name = get_string('pluginname', 'forum').' '.$i;
}
@ -61,15 +64,26 @@ class mod_forum_generator extends phpunit_module_generator {
if (!isset($record->introformat)) {
$record->introformat = FORMAT_MOODLE;
}
$record->timemodified = time();
if (!isset($record->type)) {
$record->type = 'general';
}
if (!isset($record->assessed)) {
$record->assessed = 0;
}
if (!isset($record->scale)) {
$record->scale = 0;
}
if (!isset($record->forcesubscribe)) {
$record->forcesubscribe = FORUM_CHOOSESUBSCRIBE;
}
if (isset($options['idnumber'])) {
$record->cmidnumber = $options['idnumber'];
} else {
$record->cmidnumber = '';
}
$id = $DB->insert_record('forum', $record);
$instance = $DB->get_record('forum', array('id'=>$id), '*', MUST_EXIST);
$cm = $this->create_course_module($instance, $options);
$instance->cmid = $cm->id;
return $instance;
$record->coursemodule = $this->precreate_course_module($record->course, $options);
$id = forum_add_instance($record, null);
return $this->post_add_instance($id, $record->coursemodule);
}
}

View File

@ -36,28 +36,38 @@ defined('MOODLE_INTERNAL') || die();
*/
class mod_forum_generator_testcase extends advanced_testcase {
public function test_generator() {
global $DB, $SITE;
global $DB;
$this->resetAfterTest(true);
$this->assertEquals(0, $DB->count_records('forum'));
$course = $this->getDataGenerator()->create_course();
/** @var mod_forum_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('mod_forum');
$this->assertInstanceOf('mod_forum_generator', $generator);
$this->assertEquals('forum', $generator->get_modulename());
$generator->create_instance(array('course'=>$SITE->id));
$generator->create_instance(array('course'=>$SITE->id));
$forum = $generator->create_instance(array('course'=>$SITE->id));
$generator->create_instance(array('course'=>$course->id));
$generator->create_instance(array('course'=>$course->id));
$forum = $generator->create_instance(array('course'=>$course->id));
$this->assertEquals(3, $DB->count_records('forum'));
$cm = get_coursemodule_from_instance('forum', $forum->id);
$this->assertEquals($forum->id, $cm->instance);
$this->assertEquals('forum', $cm->modname);
$this->assertEquals($SITE->id, $cm->course);
$this->assertEquals($course->id, $cm->course);
$context = context_module::instance($cm->id);
$this->assertEquals($forum->cmid, $context->instanceid);
// test gradebook integration using low level DB access - DO NOT USE IN PLUGIN CODE!
$forum = $generator->create_instance(array('course'=>$course->id, 'assessed'=>1, 'scale'=>100));
$gitem = $DB->get_record('grade_items', array('courseid'=>$course->id, 'itemtype'=>'mod', 'itemmodule'=>'forum', 'iteminstance'=>$forum->id));
$this->assertNotEmpty($gitem);
$this->assertEquals(100, $gitem->grademax);
$this->assertEquals(0, $gitem->grademin);
$this->assertEquals(GRADE_TYPE_VALUE, $gitem->gradetype);
}
}

View File

@ -81,16 +81,15 @@ function page_get_post_actions() {
/**
* Add page instance.
* @param object $data
* @param object $mform
* @param stdClass $data
* @param mod_page_mod_form $mform
* @return int new page instance id
*/
function page_add_instance($data, $mform) {
function page_add_instance($data, $mform = null) {
global $CFG, $DB;
require_once("$CFG->libdir/resourcelib.php");
$cmid = $data->coursemodule;
$draftitemid = $data->page['itemid'];
$cmid = $data->coursemodule;
$data->timemodified = time();
$displayoptions = array();
@ -102,8 +101,10 @@ function page_add_instance($data, $mform) {
$displayoptions['printintro'] = $data->printintro;
$data->displayoptions = serialize($displayoptions);
$data->content = $data->page['text'];
$data->contentformat = $data->page['format'];
if ($mform) {
$data->content = $data->page['text'];
$data->contentformat = $data->page['format'];
}
$data->id = $DB->insert_record('page', $data);
@ -111,7 +112,8 @@ function page_add_instance($data, $mform) {
$DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));
$context = get_context_instance(CONTEXT_MODULE, $cmid);
if ($draftitemid) {
if ($mform and !empty($data->page['itemid'])) {
$draftitemid = $data->page['itemid'];
$data->content = file_save_draft_area_files($draftitemid, $context->id, 'mod_page', 'content', 0, page_get_editor_options($context), $data->content);
$DB->update_record('page', $data);
}

View File

@ -43,7 +43,7 @@ class mod_page_generator extends phpunit_module_generator {
* @return stdClass activity record with extra cmid field
*/
public function create_instance($record = null, array $options = null) {
global $DB, $CFG;
global $CFG;
require_once("$CFG->dirroot/mod/page/locallib.php");
$this->instancecount++;
@ -52,6 +52,9 @@ class mod_page_generator extends phpunit_module_generator {
$record = (object)(array)$record;
$options = (array)$options;
if (empty($record->course)) {
throw new coding_exception('module generator requires $record->course');
}
if (!isset($record->name)) {
$record->name = get_string('pluginname', 'page').' '.$i;
}
@ -70,15 +73,20 @@ class mod_page_generator extends phpunit_module_generator {
if (!isset($record->display)) {
$record->display = RESOURCELIB_DISPLAY_AUTO;
}
$record->timemodified = time();
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;
}
$id = $DB->insert_record('page', $record);
$instance = $DB->get_record('page', array('id'=>$id), '*', MUST_EXIST);
$cm = $this->create_course_module($instance, $options);
$instance->cmid = $cm->id;
return $instance;
$record->coursemodule = $this->precreate_course_module($record->course, $options);
$id = page_add_instance($record, null);
return $this->post_add_instance($id, $record->coursemodule);
}
}