MDL-42400 generator: module generators call add_moduleinfo instead of direct inserts

This commit is contained in:
Marina Glancy 2013-10-18 13:31:40 +11:00
parent b4b7587294
commit 7fbe33fcf1
18 changed files with 194 additions and 409 deletions

View file

@ -70,6 +70,12 @@ abstract class testing_module_generator extends component_generator_base {
/**
* Create course module and link it to course
*
* Since 2.6 it is recommended to use function add_moduleinfo() to create a module.
*
* @deprecated since 2.6
* @see testing_module_generator::create_instance()
*
* @param integer $courseid
* @param array $options section, visible
* @return integer $cm instance id
@ -110,6 +116,12 @@ abstract class testing_module_generator extends component_generator_base {
/**
* Called after *_add_instance()
*
* Since 2.6 it is recommended to use function add_moduleinfo() to create a module.
*
* @deprecated since 2.6
* @see testing_module_generator::create_instance()
*
* @param int $id
* @param int $cmid
* @return stdClass module instance
@ -130,12 +142,133 @@ abstract class testing_module_generator extends component_generator_base {
}
/**
* Create a test module
* @param array|stdClass $record
* @param array $options
* @return stdClass activity record
* Merges together arguments $record and $options and fills default module
* fields that are shared by all module types
*
* @param object|array $record fields (different from defaults) for this module
* @param null|array $options for backward-compatibility this may include fields from course_modules
* table. They are merged into $record
* @throws coding_exception if $record->course is not specified
*/
abstract public function create_instance($record = null, array $options = null);
protected function prepare_moduleinfo_record($record, $options) {
global $DB;
// Make sure we don't modify the original object.
$moduleinfo = (object)(array)$record;
if (empty($moduleinfo->course)) {
throw new coding_exception('module generator requires $record->course');
}
$moduleinfo->modulename = $this->get_modulename();
$moduleinfo->module = $DB->get_field('modules', 'id', array('name' => $moduleinfo->modulename));
// Allow idnumber to be set as either $options['idnumber'] or $moduleinfo->cmidnumber or $moduleinfo->idnumber.
// The actual field name is 'idnumber' but add_moduleinfo() expects 'cmidnumber'.
if (isset($options['idnumber'])) {
$moduleinfo->cmidnumber = $options['idnumber'];
} else if (!isset($moduleinfo->cmidnumber) && isset($moduleinfo->idnumber)) {
$moduleinfo->cmidnumber = $moduleinfo->idnumber;
}
// These are the fields from table 'course_modules' in 2.6 when the second
// argument $options is being deprecated.
// List excludes fields: instance (does not exist yet), course, module and idnumber (set above)
$easymergefields = array('section', 'added', 'score', 'indent',
'visible', 'visibleold', 'groupmode', 'groupingid', 'groupmembersonly',
'completion', 'completiongradeitemnumber', 'completionview', 'completionexpected',
'availablefrom', 'availableuntil', 'showavailability', 'showdescription');
foreach ($easymergefields as $key) {
if (isset($options[$key])) {
$moduleinfo->$key = $options[$key];
}
}
// Set default values. Note that visibleold and completiongradeitemnumber are not used when creating a module.
$defaults = array(
'section' => 0,
'visible' => 1,
'cmidnumber' => '',
'groupmode' => 0,
'groupingid' => 0,
'groupmembersonly' => 0,
'showavailability' => 0,
'availablefrom' => 0,
'availableuntil' => 0,
'completion' => 0,
'completionview' => 0,
'completionexpected' => 0,
'conditiongradegroup' => array(),
'conditionfieldgroup' => array(),
'conditioncompletiongroup' => array()
);
foreach ($defaults as $key => $value) {
if (!isset($moduleinfo->$key)) {
$moduleinfo->$key = $value;
}
}
return $moduleinfo;
}
/**
* Creates an instance of the module for testing purposes.
*
* Module type will be taken from the class name. Each module type may overwrite
* this function to add other default values used by it.
*
* @param array|stdClass $record data for module being generated. Requires 'course' key
* (an id or the full object). Also can have any fields from add module form.
* @param null|array $options general options for course module. Since 2.6 it is
* possible to omit this argument by merging options into $record
* @return stdClass record from module-defined table with additional field
* cmid (corresponding id in course_modules table)
*/
public function create_instance($record = null, array $options = null) {
global $CFG, $DB;
require_once($CFG->dirroot.'/course/modlib.php');
$this->instancecount++;
// Merge options into record and add default values.
$record = $this->prepare_moduleinfo_record($record, $options);
// Retrieve the course record.
if (!empty($record->course->id)) {
$course = $record->course;
$record->course = $record->course->id;
} else {
$course = get_course($record->course);
}
// Fill the name and intro with default values (if missing).
if (empty($record->name)) {
$record->name = get_string('pluginname', $this->get_modulename()).' '.$this->instancecount;
}
if (empty($record->introeditor) && empty($record->intro)) {
$record->intro = 'Test '.$this->get_modulename().' ' . $this->instancecount;
}
if (empty($record->introeditor) && empty($record->introformat)) {
$record->introformat = FORMAT_MOODLE;
}
// Before Moodle 2.6 it was possible to create a module with completion tracking when
// it is not setup for course and/or site-wide. Display debugging message so it is
// easier to trace an error in unittests.
if ($record->completion && empty($CFG->enablecompletion)) {
debugging('Did you forget to set $CFG->enablecompletion before generating module with completion tracking?', DEBUG_DEVELOPER);
}
if ($record->completion && empty($course->enablecompletion)) {
debugging('Did you forget to enable completion tracking for the course before generating module with completion tracking?', DEBUG_DEVELOPER);
}
// Add the module to the course.
$moduleinfo = add_moduleinfo($record, $course, $mform = null);
// Prepare object to return with additional field cmid.
$instance = $DB->get_record($this->get_modulename(), array('id' => $moduleinfo->instance), '*', MUST_EXIST);
$instance->cmid = $moduleinfo->coursemodule;
return $instance;
}
}
/**

View file

@ -53,8 +53,11 @@ class core_completionlib_testcase extends advanced_testcase {
$this->resetAfterTest();
// Enable completion before creating modules, otherwise the completion data is not written in DB.
$CFG->enablecompletion = true;
// Create a course with activities.
$this->course = $this->getDataGenerator()->create_course();
$this->course = $this->getDataGenerator()->create_course(array('enablecompletion' => true));
$this->user = $this->getDataGenerator()->create_user();
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->assertNotEmpty($studentrole);
@ -718,10 +721,14 @@ class core_completionlib_testcase extends advanced_testcase {
}
public function test_get_activities() {
global $CFG;
$this->resetAfterTest();
// Enable completion before creating modules, otherwise the completion data is not written in DB.
$CFG->enablecompletion = true;
// Create a course with mixed auto completion data.
$course = $this->getDataGenerator()->create_course();
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => true));
$completionauto = array('completion' => COMPLETION_TRACKING_AUTOMATIC);
$completionmanual = array('completion' => COMPLETION_TRACKING_MANUAL);
$completionnone = array('completion' => COMPLETION_TRACKING_NONE);
@ -734,7 +741,7 @@ class core_completionlib_testcase extends advanced_testcase {
$data2 = $this->getDataGenerator()->create_module('data', array('course' => $course->id), $completionnone);
// Create data in another course to make sure it's not considered.
$course2 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course(array('enablecompletion' => true));
$c2forum = $this->getDataGenerator()->create_module('forum', array('course' => $course2->id), $completionauto);
$c2page = $this->getDataGenerator()->create_module('page', array('course' => $course2->id), $completionmanual);
$c2data = $this->getDataGenerator()->create_module('data', array('course' => $course2->id), $completionnone);
@ -755,11 +762,15 @@ class core_completionlib_testcase extends advanced_testcase {
}
public function test_has_activities() {
global $CFG;
$this->resetAfterTest();
// Enable completion before creating modules, otherwise the completion data is not written in DB.
$CFG->enablecompletion = true;
// Create a course with mixed auto completion data.
$course = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => true));
$course2 = $this->getDataGenerator()->create_course(array('enablecompletion' => true));
$completionauto = array('completion' => COMPLETION_TRACKING_AUTOMATIC);
$completionnone = array('completion' => COMPLETION_TRACKING_NONE);
$c1forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id), $completionauto);
@ -776,9 +787,10 @@ class core_completionlib_testcase extends advanced_testcase {
* Test course module completion update event.
*/
public function test_course_module_completion_updated_event() {
global $USER;
global $USER, $CFG;
$this->setup_data();
$this->setAdminUser();
$completionauto = array('completion' => COMPLETION_TRACKING_AUTOMATIC);

View file

@ -77,6 +77,8 @@ class core_modinfolib_testcase extends advanced_testcase {
foreach ($conditionsfield as $conditionfield) {
$ci->add_user_field_condition($conditionfield->fieldname, $conditionfield->operator, $conditionfield->value);
}
// Direct calls to condition_info_section methods do not reset the course cache. Do it manually.
rebuild_course_cache($course->id, true);
// Create and enrol a student.
$studentrole = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
@ -164,6 +166,8 @@ class core_modinfolib_testcase extends advanced_testcase {
foreach ($conditionsfield as $conditionfield) {
$ci->add_user_field_condition($conditionfield->fieldname, $conditionfield->operator, $conditionfield->value);
}
// Direct access to condition_info functions does not reset course cache, do it manually.
rebuild_course_cache($course->id, true);
// Retrieve all related records from DB.
$assigndb = $DB->get_record('assign', array('id' => $assign->id));
@ -542,6 +546,9 @@ class core_modinfolib_testcase extends advanced_testcase {
$this->resetAfterTest();
// Enable conditional availability before creating modules, otherwise the condition data is not written in DB.
$CFG->enableavailability = true;
// Create a course.
$course = $this->getDataGenerator()->create_course();
// 1. Create an activity that is currently unavailable and hidden entirely (for students).