Merge branch 'wip-MDL-42400-master' of git://github.com/marinaglancy/moodle

This commit is contained in:
Damyon Wiese 2013-11-04 13:11:42 +08:00
commit d04bea30da
37 changed files with 1550 additions and 452 deletions

View File

@ -329,7 +329,7 @@ class tool_generator_course_backend extends tool_generator_backend {
$number = self::$parampages[$this->size];
$this->log('createpages', $number, true);
for ($i = 0; $i < $number; $i++) {
$record = array('course' => $this->course->id);
$record = array('course' => $this->course);
$options = array('section' => $this->get_target_section());
$pagegenerator->create_instance($record, $options);
$this->dot($i, $number);
@ -347,7 +347,7 @@ class tool_generator_course_backend extends tool_generator_backend {
// Create resource with default textfile only.
$resourcegenerator = $this->generator->get_plugin_generator('mod_resource');
$record = array('course' => $this->course->id,
$record = array('course' => $this->course,
'name' => get_string('smallfiles', 'tool_generator'));
$options = array('section' => 0);
$resource = $resourcegenerator->create_instance($record, $options);
@ -415,7 +415,7 @@ class tool_generator_course_backend extends tool_generator_backend {
$resourcegenerator = $this->generator->get_plugin_generator('mod_resource');
for ($i = 0; $i < $count; $i++) {
// Create resource.
$record = array('course' => $this->course->id,
$record = array('course' => $this->course,
'name' => get_string('bigfile', 'tool_generator', $i));
$options = array('section' => $this->get_target_section());
$resource = $resourcegenerator->create_instance($record, $options);
@ -458,7 +458,7 @@ class tool_generator_course_backend extends tool_generator_backend {
// Create empty forum.
$forumgenerator = $this->generator->get_plugin_generator('mod_forum');
$record = array('course' => $this->course->id,
$record = array('course' => $this->course,
'name' => get_string('pluginname', 'forum'));
$options = array('section' => 0);
$forum = $forumgenerator->create_instance($record, $options);

View File

@ -43,6 +43,8 @@ class core_badges_badgeslib_testcase extends advanced_testcase {
unset_config('noemailever');
$CFG->enablecompletion = true;
$user = $this->getDataGenerator()->create_user();
$fordb = new stdClass();
@ -69,7 +71,7 @@ class core_badges_badgeslib_testcase extends advanced_testcase {
$this->badgeid = $DB->insert_record('badge', $fordb, true);
// Create a course with activity and auto completion tracking.
$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);

View File

@ -3242,6 +3242,7 @@ function course_get_url($courseorid, $section = null, $options = array()) {
*
* @param object $module
* @return object the created module info
* @throws moodle_exception if user is not allowed to perform the action or module is not allowed in this course
*/
function create_module($moduleinfo) {
global $DB, $CFG;
@ -3263,9 +3264,6 @@ function create_module($moduleinfo) {
$course = $DB->get_record('course', array('id'=>$moduleinfo->course), '*', MUST_EXIST);
list($module, $context, $cw) = can_add_moduleinfo($course, $moduleinfo->modulename, $moduleinfo->section);
// Load module library.
include_modulelib($module->name);
// Add the module.
$moduleinfo->module = $module->id;
$moduleinfo = add_moduleinfo($moduleinfo, $course, null);
@ -3282,6 +3280,7 @@ function create_module($moduleinfo) {
*
* @param object $module
* @return object the updated module info
* @throws moodle_exception if current user is not allowed to update the module
*/
function update_module($moduleinfo) {
global $DB, $CFG;
@ -3297,9 +3296,6 @@ function update_module($moduleinfo) {
// Some checks (capaibility / existing instances).
list($cm, $context, $module, $data, $cw) = can_update_moduleinfo($cm);
// Load module library.
include_modulelib($module->name);
// Retrieve few information needed by update_moduleinfo.
$moduleinfo->modulename = $cm->modname;
if (!isset($moduleinfo->scale)) {

View File

@ -252,8 +252,6 @@ if (file_exists($modmoodleform)) {
print_error('noformdesc');
}
include_modulelib($module->name);
$mformclassname = 'mod_'.$module->name.'_mod_form';
$mform = new $mformclassname($data, $cw->section, $cm, $course);
$mform->set_data($data);

View File

@ -44,6 +44,9 @@ require_once($CFG->dirroot.'/course/lib.php');
function add_moduleinfo($moduleinfo, $course, $mform = null) {
global $DB, $CFG;
// Attempt to include module library before we make any changes to DB.
include_modulelib($moduleinfo->modulename);
$moduleinfo->course = $course->id;
$moduleinfo = set_moduleinfo_defaults($moduleinfo);
@ -51,10 +54,6 @@ function add_moduleinfo($moduleinfo, $course, $mform = null) {
$moduleinfo->groupmode = 0; // Do not set groupmode.
}
if (!course_allowed_module($course, $moduleinfo->modulename)) {
print_error('moduledisable', '', '', $moduleinfo->modulename);
}
// First add course_module record because we need the context.
$newcm = new stdClass();
$newcm->course = $course->id;
@ -62,6 +61,9 @@ function add_moduleinfo($moduleinfo, $course, $mform = null) {
$newcm->instance = 0; // Not known yet, will be updated later (this is similar to restore code).
$newcm->visible = $moduleinfo->visible;
$newcm->visibleold = $moduleinfo->visible;
if (isset($moduleinfo->cmidnumber)) {
$newcm->idnumber = $moduleinfo->cmidnumber;
}
$newcm->groupmode = $moduleinfo->groupmode;
$newcm->groupingid = $moduleinfo->groupingid;
$newcm->groupmembersonly = $moduleinfo->groupmembersonly;
@ -87,7 +89,8 @@ function add_moduleinfo($moduleinfo, $course, $mform = null) {
print_error('cannotaddcoursemodule');
}
if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true)) {
if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true) &&
isset($moduleinfo->introeditor)) {
$introeditor = $moduleinfo->introeditor;
unset($moduleinfo->introeditor);
$moduleinfo->intro = $introeditor['text'];
@ -103,9 +106,9 @@ function add_moduleinfo($moduleinfo, $course, $mform = null) {
$DB->delete_records('course_modules', array('id'=>$moduleinfo->coursemodule));
if (!is_number($returnfromfunc)) {
print_error('invalidfunction', '', course_get_url($course, $cw->section));
print_error('invalidfunction', '', course_get_url($course, $moduleinfo->section));
} else {
print_error('cannotaddnewmodule', '', course_get_url($course, $cw->section), $moduleinfo->modulename);
print_error('cannotaddnewmodule', '', course_get_url($course, $moduleinfo->section), $moduleinfo->modulename);
}
}
@ -126,15 +129,6 @@ function add_moduleinfo($moduleinfo, $course, $mform = null) {
// So we have to update one of them twice.
$sectionid = course_add_cm_to_section($course, $moduleinfo->coursemodule, $moduleinfo->section);
// Make sure visibility is set correctly (in particular in calendar).
// Note: allow them to set it even without moodle/course:activityvisibility.
set_coursemodule_visible($moduleinfo->coursemodule, $moduleinfo->visible);
if (isset($moduleinfo->cmidnumber)) { // Label.
// Set cm idnumber - uniqueness is already verified by form validation.
set_coursemodule_idnumber($moduleinfo->coursemodule, $moduleinfo->cmidnumber);
}
// Set up conditions.
if ($CFG->enableavailability) {
condition_info::update_cm_from_form((object)array('id'=>$moduleinfo->coursemodule), $moduleinfo, false);
@ -177,9 +171,11 @@ function edit_module_post_actions($moduleinfo, $course) {
global $CFG;
$modcontext = context_module::instance($moduleinfo->coursemodule);
$hasgrades = plugin_supports('mod', $moduleinfo->modulename, FEATURE_GRADE_HAS_GRADE, false);
$hasoutcomes = plugin_supports('mod', $moduleinfo->modulename, FEATURE_GRADE_OUTCOMES, true);
// Sync idnumber with grade_item.
if ($grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$moduleinfo->modulename,
if ($hasgrades && $grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$moduleinfo->modulename,
'iteminstance'=>$moduleinfo->instance, 'itemnumber'=>0, 'courseid'=>$course->id))) {
if ($grade_item->idnumber != $moduleinfo->cmidnumber) {
$grade_item->idnumber = $moduleinfo->cmidnumber;
@ -187,8 +183,12 @@ function edit_module_post_actions($moduleinfo, $course) {
}
}
$items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$moduleinfo->modulename,
if ($hasgrades) {
$items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$moduleinfo->modulename,
'iteminstance'=>$moduleinfo->instance, 'courseid'=>$course->id));
} else {
$items = array();
}
// Create parent category if requested and move to correct parent category.
if ($items and isset($moduleinfo->gradecat)) {
@ -213,7 +213,7 @@ function edit_module_post_actions($moduleinfo, $course) {
}
// Add outcomes if requested.
if ($outcomes = grade_outcome::fetch_all_available($course->id)) {
if ($hasoutcomes && $outcomes = grade_outcome::fetch_all_available($course->id)) {
$grade_items = array();
// Outcome grade_item.itemnumber start at 1000, there is nothing above outcomes.
@ -292,8 +292,10 @@ function edit_module_post_actions($moduleinfo, $course) {
$moduleinfo->showgradingmanagement = $showgradingmanagement;
}
rebuild_course_cache($course->id);
grade_regrade_final_grades($course->id);
rebuild_course_cache($course->id, true);
if ($hasgrades) {
grade_regrade_final_grades($course->id);
}
require_once($CFG->libdir.'/plagiarismlib.php');
plagiarism_save_form_elements($moduleinfo);
@ -308,7 +310,6 @@ function edit_module_post_actions($moduleinfo, $course) {
* @return object the completed module info
*/
function set_moduleinfo_defaults($moduleinfo) {
global $DB;
if (empty($moduleinfo->coursemodule)) {
// Add.
@ -361,6 +362,7 @@ function set_moduleinfo_defaults($moduleinfo) {
* @param object $modulename the module name
* @param object $section the section of the module
* @return array list containing module, context, course section.
* @throws moodle_exception if user is not allowed to perform the action or module is not allowed in this course
*/
function can_add_moduleinfo($course, $modulename, $section) {
global $DB;
@ -385,6 +387,7 @@ function can_add_moduleinfo($course, $modulename, $section) {
*
* @param object $cm course module
* @return array - list of course module, context, module, moduleinfo, and course section.
* @throws moodle_exception if user is not allowed to perform the action
*/
function can_update_moduleinfo($cm) {
global $DB;
@ -420,6 +423,9 @@ function can_update_moduleinfo($cm) {
function update_moduleinfo($cm, $moduleinfo, $course, $mform = null) {
global $DB, $CFG;
// Attempt to include module library before we make any changes to DB.
include_modulelib($moduleinfo->modulename);
$moduleinfo->course = $course->id;
$moduleinfo = set_moduleinfo_defaults($moduleinfo);
@ -515,6 +521,7 @@ function update_moduleinfo($cm, $moduleinfo, $course, $mform = null) {
* Include once the module lib file.
*
* @param string $modulename module name of the lib to include
* @throws moodle_exception if lib.php file for the module does not exist
*/
function include_modulelib($modulename) {
global $CFG;

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,145 @@ 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;
}
/**
* Generates a piece of content for the module.
* User is usually taken from global $USER variable.
* @param stdClass $instance object returned from create_instance() call
* @param stdClass|array $record
* @return stdClass generated object
* @throws coding_exception if function is not implemented by module
*/
public function create_content($instance, $record = array()) {
throw new coding_exception('Module generator for '.$this->get_modulename().' does not implement method create_content()');
}
}
/**

View File

@ -108,7 +108,7 @@ class core_test_generator_testcase extends advanced_testcase {
}
public function test_create_module() {
global $CFG, $SITE;
global $CFG, $SITE, $DB;
if (!file_exists("$CFG->dirroot/mod/page/")) {
$this->markTestSkipped('Can not find standard Page module');
}
@ -125,6 +125,191 @@ class core_test_generator_testcase extends advanced_testcase {
$this->assertNotEmpty($page);
$cm = get_coursemodule_from_instance('page', $page->id, $SITE->id, true);
$this->assertEquals(3, $cm->sectionnum);
// Prepare environment to generate modules with all possible options.
// Enable advanced functionality.
$CFG->enablecompletion = 1;
$CFG->enableavailability = 1;
$CFG->enablegroupmembersonly = 1;
$CFG->enableoutcomes = 1;
require_once($CFG->libdir.'/gradelib.php');
require_once($CFG->libdir.'/completionlib.php');
require_once($CFG->dirroot.'/rating/lib.php');
// Create a course with enabled completion.
$course = $generator->create_course(array('enablecompletion' => true));
// Create new grading category in this course.
$grade_category = new grade_category();
$grade_category->courseid = $course->id;
$grade_category->fullname = 'Grade category';
$grade_category->insert();
// Create group and grouping.
$group = $generator->create_group(array('courseid' => $course->id));
$grouping = $generator->create_grouping(array('courseid' => $course->id));
$generator->create_grouping_group(array('groupid' => $group->id, 'groupingid' => $grouping->id));
// Prepare arrays with properties that we can both use for creating modules and asserting the data in created modules.
// General properties.
$optionsgeneral = array(
'visible' => 0, // Note: 'visibleold' will always be set to the same value as 'visible'.
'section' => 3, // Note: section will be created if does not exist.
// Module supports FEATURE_IDNUMBER.
'cmidnumber' => 'IDNUM', // Note: alternatively can have key 'idnumber'.
// Module supports FEATURE_GROUPS;
'groupmode' => SEPARATEGROUPS, // Note: will be reset to 0 if course groupmodeforce is set.
// Module supports FEATURE_GROUPINGS or module supports FEATURE_GROUPMEMBERSONLY:
'groupingid' => $grouping->id,
// Module supports FEATURE_GROUPMEMBERSONLY:
'groupmembersonly' => 1,
);
// In case completion is enabled on site and for course every module can have manual completion.
$featurecompletionmanual = array(
'completion' => COMPLETION_TRACKING_MANUAL, // "Students can manually mark activity as completed."
'completionexpected' => time() + 7 * DAYSECS,
);
// Automatic completion is possible if module supports FEATURE_COMPLETION_TRACKS_VIEWS or FEATURE_GRADE_HAS_GRADE.
// Note: completionusegrade is stored in DB and can be found in cm_info as 'completiongradeitemnumber' - either NULL or 0.
// Note: module can have more autocompletion rules as defined in moodleform_mod::add_completion_rules().
$featurecompletionautomatic = array(
'completion' => COMPLETION_TRACKING_AUTOMATIC, // "Show activity as complete when conditions are met."
'completionview' => 1, // "Student must view this activity to complete it"
'completionusegrade' => 1, // "Student must receive a grade to complete this activity"
);
// Module supports FEATURE_RATE:
$featurerate = array(
'assessed' => RATING_AGGREGATE_AVERAGE, // "Aggregate type"
'scale' => 100, // Either max grade or negative number for scale id.
'ratingtime' => 1, // "Restrict ratings to items with dates in this range".
'assesstimestart' => time() - DAYSECS, // Note: Will be ignored if neither 'assessed' nor 'ratingtime' is set.
'assesstimefinish' => time() + DAYSECS, // Note: Will be ignored if neither 'assessed' nor 'ratingtime' is set.
);
// Module supports FEATURE_GRADE_HAS_GRADE:
$featuregrade = array(
'grade' => 10,
'gradecat' => $grade_category->id, // Note: if $CFG->enableoutcomes is set, this can be set to -1 to automatically create new grade category.
);
// Now let's create several modules with different options.
$m1 = $generator->create_module('assign',
array('course' => $course->id) +
$optionsgeneral);
$m2 = $generator->create_module('data',
array('course' => $course->id) +
$featurecompletionmanual +
$featurerate);
$m3 = $generator->create_module('assign',
array('course' => $course->id) +
$featurecompletionautomatic +
$featuregrade);
// We need id of the grading item for the second module to create availability dependency in the 3rd module.
$gradingitem = grade_item::fetch(array('courseid'=>$course->id, 'itemtype'=>'mod', 'itemmodule' => 'assign', 'iteminstance' => $m3->id));
// Now prepare options to create the 4th module which availability depends on other modules.
// Following options available if $CFG->enableavailability is set:
$optionsavailability = array(
'showavailability' => 1,
'availablefrom' => time() - WEEKSECS,
'availableuntil' => time() + WEEKSECS,
'conditiongradegroup' => array(
array(
'conditiongradeitemid' => $gradingitem->id,
'conditiongrademin' => 20,
'conditiongrademax' => 80,
)
),
'conditionfieldgroup' => array(
array(
'conditionfield' => 'address',
'conditionfieldoperator' => 'contains',
'conditionfieldvalue' => 'street',
)
),
'conditioncompletiongroup' => array(
array(
'conditionsourcecmid' => $m2->cmid,
'conditionrequiredcompletion' => 1
),
array(
'conditionsourcecmid' => $m3->cmid,
'conditionrequiredcompletion' => 1
)
)
);
// The same data for assertion (different format).
$optionsavailabilityassertion = array(
'conditionsgrade' => array(
$gradingitem->id => (object)array(
'min' => 20,
'max' => 80,
'name' => $gradingitem->itemname
)
),
'conditionsfield' => array(
'address' => (object)array(
'fieldname' => 'address',
'operator' => 'contains',
'value' => 'street'
)
),
'conditionscompletion' => array(
$m2->cmid => 1,
$m3->cmid => 1
)
);
// Create module with conditional availability.
$m4 = $generator->create_module('assign',
array('course' => $course->id) +
$optionsavailability
);
// Verifying that everything is generated correctly.
$modinfo = get_fast_modinfo($course->id);
$cm1 = $modinfo->cms[$m1->cmid];
$this->assertEquals($optionsgeneral['visible'], $cm1->visible);
$this->assertEquals($optionsgeneral['section'], $cm1->sectionnum); // Note difference in key.
$this->assertEquals($optionsgeneral['cmidnumber'], $cm1->idnumber); // Note difference in key.
$this->assertEquals($optionsgeneral['groupmode'], $cm1->groupmode);
$this->assertEquals($optionsgeneral['groupingid'], $cm1->groupingid);
$this->assertEquals($optionsgeneral['groupmembersonly'], $cm1->groupmembersonly);
$cm2 = $modinfo->cms[$m2->cmid];
$this->assertEquals($featurecompletionmanual['completion'], $cm2->completion);
$this->assertEquals($featurecompletionmanual['completionexpected'], $cm2->completionexpected);
$this->assertEquals(null, $cm2->completiongradeitemnumber);
// Rating info is stored in the module's table (in our test {data}).
$data = $DB->get_record('data', array('id' => $m2->id));
$this->assertEquals($featurerate['assessed'], $data->assessed);
$this->assertEquals($featurerate['scale'], $data->scale);
$this->assertEquals($featurerate['assesstimestart'], $data->assesstimestart);
$this->assertEquals($featurerate['assesstimefinish'], $data->assesstimefinish);
// No validation for 'ratingtime'. It is only used in to enable/disable assesstime* when adding module.
$cm3 = $modinfo->cms[$m3->cmid];
$this->assertEquals($featurecompletionautomatic['completion'], $cm3->completion);
$this->assertEquals($featurecompletionautomatic['completionview'], $cm3->completionview);
$this->assertEquals(0, $cm3->completiongradeitemnumber); // Zero instead of default null since 'completionusegrade' was set.
$gradingitem = grade_item::fetch(array('courseid'=>$course->id, 'itemtype'=>'mod', 'itemmodule' => 'assign', 'iteminstance' => $m3->id));
$this->assertEquals(0, $gradingitem->grademin);
$this->assertEquals($featuregrade['grade'], $gradingitem->grademax);
$this->assertEquals($featuregrade['gradecat'], $gradingitem->categoryid);
$cm4 = $modinfo->cms[$m4->cmid];
$this->assertEquals($optionsavailability['showavailability'], $cm4->showavailability);
$this->assertEquals($optionsavailability['availablefrom'], $cm4->availablefrom);
$this->assertEquals($optionsavailability['availableuntil'], $cm4->availableuntil);
$this->assertEquals($optionsavailabilityassertion['conditionsgrade'], $cm4->conditionsgrade);
$this->assertEquals($optionsavailabilityassertion['conditionsfield'], $cm4->conditionsfield);
$this->assertEquals($optionsavailabilityassertion['conditionscompletion'], $cm4->conditionscompletion);
}
public function test_create_block() {

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).

View File

@ -26,30 +26,10 @@ defined('MOODLE_INTERNAL') || die();
*/
class mod_assign_generator extends testing_module_generator {
/**
* Create new assign module instance
* @param array|stdClass $record
* @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 $CFG;
require_once("$CFG->dirroot/mod/assign/lib.php");
$this->instancecount++;
$i = $this->instancecount;
$record = (object)(array)$record;
$options = (array)$options;
if (empty($record->course)) {
throw new coding_exception('module generator requires $record->course');
}
$defaultsettings = array(
'name' => get_string('pluginname', 'assign').' '.$i,
'intro' => 'Test assign ' . $i,
'introformat' => FORMAT_MOODLE,
'alwaysshowdescription' => 1,
'submissiondrafts' => 1,
'requiresubmissionstatement' => 0,
@ -63,7 +43,6 @@ class mod_assign_generator extends testing_module_generator {
'requireallteammemberssubmit' => 0,
'teamsubmissiongroupingid' => 0,
'blindmarking' => 0,
'cmidnumber' => '',
'attemptreopenmethod' => 'none',
'maxattempts' => -1,
'markingworkflow' => 0,
@ -76,9 +55,6 @@ class mod_assign_generator extends testing_module_generator {
}
}
$record->coursemodule = $this->precreate_course_module($record->course, $options);
$id = assign_add_instance($record, null);
rebuild_course_cache($record->course, true);
return $this->post_add_instance($id, $record->coursemodule);
return parent::create_instance($record, (array)$options);
}
}

View File

@ -36,34 +36,9 @@ defined('MOODLE_INTERNAL') || die();
*/
class mod_assignment_generator extends testing_module_generator {
/**
* Create new assignment module instance
* @param array|stdClass $record
* @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 $CFG;
require_once("$CFG->dirroot/mod/assignment/locallib.php");
$this->instancecount++;
$i = $this->instancecount;
$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;
}
if (!isset($record->intro)) {
$record->intro = 'Test assignment '.$i;
}
if (!isset($record->introformat)) {
$record->introformat = FORMAT_MOODLE;
}
if (!isset($record->assignmenttype)) {
$record->assignmenttype = 'upload';
}
@ -73,14 +48,7 @@ class mod_assignment_generator extends testing_module_generator {
if (!isset($record->timedue)) {
$record->timedue = 0;
}
if (isset($options['idnumber'])) {
$record->cmidnumber = $options['idnumber'];
} else {
$record->cmidnumber = '';
}
$record->coursemodule = $this->precreate_course_module($record->course, $options);
$id = assignment_add_instance($record, null);
return $this->post_add_instance($id, $record->coursemodule);
return parent::create_instance($record, (array)$options);
}
}

View File

@ -50,34 +50,12 @@ class mod_book_generator extends testing_module_generator {
parent::reset();
}
/**
* Create new book module instance
* @param array|stdClass $record
* @param array $options
* @return stdClass activity record with extra cmid field
*/
public function create_instance($record = null, array $options = null) {
global $CFG;
require_once("$CFG->dirroot/mod/book/locallib.php");
$this->instancecount++;
$i = $this->instancecount;
$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', 'book') . ' ' . $i;
}
if (!isset($record->intro)) {
$record->intro = 'Test book ' . $i;
}
if (!isset($record->introformat)) {
$record->introformat = FORMAT_MOODLE;
}
if (!isset($record->numbering)) {
$record->numbering = BOOK_NUM_NUMBERS;
}
@ -85,9 +63,7 @@ class mod_book_generator extends testing_module_generator {
$record->customtitles = 0;
}
$record->coursemodule = $this->precreate_course_module($record->course, $options);
$id = book_add_instance($record, null);
return $this->post_add_instance($id, $record->coursemodule);
return parent::create_instance($record, (array)$options);
}
public function create_chapter($record = null, array $options = null) {
@ -144,4 +120,11 @@ class mod_book_generator extends testing_module_generator {
return $record;
}
public function create_content($instance, $record = array()) {
$record = (array)$record + array(
'bookid' => $instance->id
);
return $this->create_chapter($record);
}
}

View File

@ -66,6 +66,9 @@ class mod_book_generator_testcase extends advanced_testcase {
$this->assertEquals(2, $DB->count_records('book_chapters', array('bookid' => $book->id)));
$this->assertEquals('Oops', $DB->get_field_select('book_chapters', 'title', 'id = :id', array('id' => $chapter->id)));
$this->assertEquals('Yay!', $DB->get_field_select('book_chapters', 'content', 'id = :id', array('id' => $chapter->id)));
$chapter = $bookgenerator->create_content($book);
$this->assertEquals(3, $DB->count_records('book_chapters', array('bookid' => $book->id)));
}
}

View File

@ -50,34 +50,9 @@ class mod_chat_generator extends testing_module_generator {
parent::reset();
}
/**
* Create new chat module instance
* @param array|stdClass $record
* @param array $options
* @return stdClass activity record with extra cmid field
*/
public function create_instance($record = null, array $options = null) {
global $CFG;
require_once("$CFG->dirroot/mod/chat/lib.php");
$this->instancecount++;
$i = $this->instancecount;
$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', 'chat') . ' ' . $i;
}
if (!isset($record->intro)) {
$record->intro = 'Test chat ' . $i;
}
if (!isset($record->introformat)) {
$record->introformat = FORMAT_MOODLE;
}
if (!isset($record->keepdays)) {
$record->keepdays = 0;
}
@ -94,9 +69,7 @@ class mod_chat_generator extends testing_module_generator {
$record->timemodified = time();
}
$record->coursemodule = $this->precreate_course_module($record->course, $options);
$id = chat_add_instance($record);
return $this->post_add_instance($id, $record->coursemodule);
return parent::create_instance($record, (array)$options);
}
}

View File

@ -35,35 +35,9 @@ defined('MOODLE_INTERNAL') || die();
*/
class mod_choice_generator extends testing_module_generator {
/**
* Create new choice module instance
*
* @param array|stdClass $record
* @param array $options
* @return stdClass activity record with extra cmid field
*/
public function create_instance($record = null, array $options = null) {
global $CFG;
require_once("$CFG->dirroot/mod/choice/lib.php");
$this->instancecount++;
$i = $this->instancecount;
$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', 'choice') . ' ' . $i;
}
if (!isset($record->intro)) {
$record->intro = 'Test choice ' . $i;
}
if (!isset($record->introformat)) {
$record->introformat = FORMAT_MOODLE;
}
if (!isset($record->timemodified)) {
$record->timemodified = time();
}
@ -74,8 +48,6 @@ class mod_choice_generator extends testing_module_generator {
$record->option[] = 'Wine';
$record->option[] = 'Spirits';
}
$record->coursemodule = $this->precreate_course_module($record->course, $options);
$id = choice_add_instance($record);
return $this->post_add_instance($id, $record->coursemodule);
return parent::create_instance($record, (array)$options);
}
}

View File

@ -36,48 +36,16 @@ defined('MOODLE_INTERNAL') || die();
*/
class mod_data_generator extends testing_module_generator {
/**
* Create new data module instance
* @param array|stdClass $record
* @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 $CFG;
require_once("$CFG->dirroot/mod/data/locallib.php");
$this->instancecount++;
$i = $this->instancecount;
$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;
}
if (!isset($record->intro)) {
$record->intro = 'Test database '.$i;
}
if (!isset($record->introformat)) {
$record->introformat = FORMAT_MOODLE;
}
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 = '';
}
$record->coursemodule = $this->precreate_course_module($record->course, $options);
$id = data_add_instance($record, null);
return $this->post_add_instance($id, $record->coursemodule);
return parent::create_instance($record, (array)$options);
}
}

View File

@ -35,36 +35,11 @@ defined('MOODLE_INTERNAL') || die();
*/
class mod_feedback_generator extends testing_module_generator {
/**
* Create new feedback module instance
*
* @param array|stdClass $record
* @param array $options
* @throws coding_exception
* @return stdClass activity record with extra cmid field
*/
public function create_instance($record = null, array $options = null) {
global $CFG;
require_once("$CFG->dirroot/mod/feedback/lib.php");
$this->instancecount++;
$i = $this->instancecount;
require_once($CFG->dirroot.'/mod/feedback/lib.php');
$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', 'feedback') . ' ' . $i;
}
if (!isset($record->intro)) {
$record->intro = 'Test feedback ' . $i;
}
if (!isset($record->introformat)) {
$record->introformat = FORMAT_MOODLE;
}
if (!isset($record->anonymous)) {
$record->anonymous = FEEDBACK_ANONYMOUS_YES;
}
@ -105,9 +80,7 @@ class mod_feedback_generator extends testing_module_generator {
// Hack to bypass draft processing of feedback_add_instance.
$record->page_after_submit_editor['itemid'] = false;
$record->coursemodule = $this->precreate_course_module($record->course, $options);
$id = feedback_add_instance($record);
return $this->post_add_instance($id, $record->coursemodule);
return parent::create_instance($record, (array)$options);
}
}

View File

@ -58,34 +58,11 @@ class mod_forum_generator extends testing_module_generator {
parent::reset();
}
/**
* Create new forum module instance
* @param array|stdClass $record
* @param array $options
* @return stdClass activity record with extra cmid field
*/
public function create_instance($record = null, array $options = null) {
global $CFG;
require_once("$CFG->dirroot/mod/forum/locallib.php");
$this->instancecount++;
$i = $this->instancecount;
require_once($CFG->dirroot.'/mod/forum/lib.php');
$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;
}
if (!isset($record->intro)) {
$record->intro = 'Test forum '.$i;
}
if (!isset($record->introformat)) {
$record->introformat = FORMAT_MOODLE;
}
if (!isset($record->type)) {
$record->type = 'general';
}
@ -98,15 +75,8 @@ class mod_forum_generator extends testing_module_generator {
if (!isset($record->forcesubscribe)) {
$record->forcesubscribe = FORUM_CHOOSESUBSCRIBE;
}
if (isset($options['idnumber'])) {
$record->cmidnumber = $options['idnumber'];
} else {
$record->cmidnumber = '';
}
$record->coursemodule = $this->precreate_course_module($record->course, $options);
$id = forum_add_instance($record, null);
return $this->post_add_instance($id, $record->coursemodule);
return parent::create_instance($record, (array)$options);
}
/**
@ -238,4 +208,27 @@ class mod_forum_generator extends testing_module_generator {
return $record;
}
public function create_content($instance, $record = array()) {
global $USER, $DB;
$record = (array)$record + array(
'forum' => $instance->id,
'userid' => $USER->id,
'course' => $instance->course
);
if (empty($record['discussion']) && empty($record['parent'])) {
// Create discussion.
$discussion = $this->create_discussion($record);
$post = $DB->get_record('forum_posts', array('id' => $discussion->firstpost));
} else {
// Create post.
if (empty($record['parent'])) {
$record['parent'] = $DB->get_field('forum_discussions', 'firstpost', array('id' => $record['discussion']), MUST_EXIST);
} else if (empty($record['discussion'])) {
$record['discussion'] = $DB->get_field('forum_posts', 'discussion', array('id' => $record['parent']), MUST_EXIST);
}
$post = $this->create_post($record);
}
return $post;
}
}

View File

@ -146,4 +146,42 @@ class mod_forum_generator_testcase extends advanced_testcase {
$this->assertEquals(4, $DB->count_records_select('forum_posts', 'discussion = :discussion',
array('discussion' => $discussion->id)));
}
public function test_create_content() {
global $DB;
$this->resetAfterTest(true);
// Create a bunch of users
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
$user4 = self::getDataGenerator()->create_user();
$this->setAdminUser();
// Create course and forum.
$course = self::getDataGenerator()->create_course();
$forum = self::getDataGenerator()->create_module('forum', array('course' => $course));
$generator = self::getDataGenerator()->get_plugin_generator('mod_forum');
// This should create discussion.
$post1 = $generator->create_content($forum);
// This should create posts in the discussion.
$post2 = $generator->create_content($forum, array('parent' => $post1->id));
$post3 = $generator->create_content($forum, array('discussion' => $post1->discussion));
// This should create posts answering another post.
$post4 = $generator->create_content($forum, array('parent' => $post2->id));
$discussionrecords = $DB->get_records('forum_discussions', array('forum' => $forum->id));
$postrecords = $DB->get_records('forum_posts');
$postrecords2 = $DB->get_records('forum_posts', array('discussion' => $post1->discussion));
$this->assertEquals(1, count($discussionrecords));
$this->assertEquals(4, count($postrecords));
$this->assertEquals(4, count($postrecords2));
$this->assertEquals($post1->id, $discussionrecords[$post1->discussion]->firstpost);
$this->assertEquals($post1->id, $postrecords[$post2->id]->parent);
$this->assertEquals($post1->id, $postrecords[$post3->id]->parent);
$this->assertEquals($post2->id, $postrecords[$post4->id]->parent);
}
}

View File

@ -0,0 +1,111 @@
<?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_glossary data generator.
*
* @package mod_glossary
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* mod_glossary data generator class.
*
* @package mod_glossary
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_glossary_generator extends testing_module_generator {
/**
* @var int keep track of how many entries have been created.
*/
protected $entrycount = 0;
/**
* To be called from data reset code only,
* do not use in tests.
* @return void
*/
public function reset() {
$this->entrycount = 0;
parent::reset();
}
public function create_instance($record = null, array $options = null) {
global $CFG;
// Add default values for glossary.
$record = (array)$record + array(
'globalglossary' => 0,
'mainglossary' => 0,
'defaultapproval' => $CFG->glossary_defaultapproval,
'allowduplicatedentries' => $CFG->glossary_dupentries,
'allowcomments' => $CFG->glossary_allowcomments,
'usedynalink' => $CFG->glossary_linkbydefault,
'displayformat' => 'dictionary',
'approvaldisplayformat' => 'default',
'entbypage' => !empty($CFG->glossary_entbypage) ? $CFG->glossary_entbypage : 10,
'showalphabet' => 1,
'showall' => 1,
'showspecial' => 1,
'allowprintview' => 1,
'rsstype' => 0,
'rssarticles' => 0,
'grade' => 100,
'assessed' => 0,
);
return parent::create_instance($record, (array)$options);
}
public function create_content($glossary, $record = array()) {
global $DB, $USER, $CFG;
$this->entrycount++;
$now = time();
$record = (array)$record + array(
'glossaryid' => $glossary->id,
'timecreated' => $now,
'timemodified' => $now,
'userid' => $USER->id,
'concept' => 'Glossary entry '.$this->entrycount,
'definition' => 'Definition of glossary entry '.$this->entrycount,
'definitionformat' => FORMAT_MOODLE,
'definitiontrust' => 0,
'usedynalink' => $CFG->glossary_linkentries,
'casesensitive' => $CFG->glossary_casesensitive,
'fullmatch' => $CFG->glossary_fullmatch
);
if (!isset($record['teacherentry']) || !isset($record['approved'])) {
$context = context_module::instance($glossary->cmid);
if (!isset($record['teacherentry'])) {
$record['teacherentry'] = has_capability('mod/glossary:manageentries', $context, $record['userid']);
}
if (!isset($record['approved'])) {
$defaultapproval = $glossary->defaultapproval;
$record['approved'] = ($defaultapproval || has_capability('mod/glossary:approve', $context));
}
}
$id = $DB->insert_record('glossary_entries', $record);
return $DB->get_record('glossary_entries', array('id' => $id), '*', MUST_EXIST);
}
}

View File

@ -0,0 +1,73 @@
<?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_glossary generator tests
*
* @package mod_glossary
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Genarator tests class for mod_glossary.
*
* @package mod_glossary
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_glossary_generator_testcase extends advanced_testcase {
public function test_create_instance() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$this->assertFalse($DB->record_exists('glossary', array('course' => $course->id)));
$glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
$records = $DB->get_records('glossary', array('course' => $course->id), 'id');
$this->assertEquals(1, count($records));
$this->assertTrue(array_key_exists($glossary->id, $records));
$params = array('course' => $course->id, 'name' => 'Another glossary');
$glossary = $this->getDataGenerator()->create_module('glossary', $params);
$records = $DB->get_records('glossary', array('course' => $course->id), 'id');
$this->assertEquals(2, count($records));
$this->assertEquals('Another glossary', $records[$glossary->id]->name);
}
public function test_create_content() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
$glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
$entry1 = $glossarygenerator->create_content($glossary);
$entry2 = $glossarygenerator->create_content($glossary, array('concept' => 'Custom concept'));
$records = $DB->get_records('glossary_entries', array('glossaryid' => $glossary->id), 'id');
$this->assertEquals(2, count($records));
$this->assertEquals($entry1->id, $records[$entry1->id]->id);
$this->assertEquals($entry2->id, $records[$entry2->id]->id);
$this->assertEquals('Custom concept', $records[$entry2->id]->concept);
}
}

View File

@ -35,38 +35,5 @@ defined('MOODLE_INTERNAL') || die();
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_label_generator extends testing_module_generator {
/**
* Create new label module instance
* @param array|stdClass $record
* @param array $options
* @return stdClass activity record with extra cmid field
*/
public function create_instance($record = null, array $options = null) {
global $CFG;
require_once("$CFG->dirroot/mod/label/lib.php");
$this->instancecount++;
$i = $this->instancecount;
$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', 'label').' '.$i;
}
if (!isset($record->intro)) {
$record->intro = 'Test label '.$i;
}
if (!isset($record->introformat)) {
$record->introformat = FORMAT_MOODLE;
}
$record->coursemodule = $this->precreate_course_module($record->course, $options);
$id = label_add_instance($record, null);
return $this->post_add_instance($id, $record->coursemodule);
}
// No additional fields in label module.
}

View File

@ -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/>.
/**
* mod_lesson data generator.
*
* @package mod_lesson
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* mod_lesson data generator class.
*
* @package mod_lesson
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_lesson_generator extends testing_module_generator {
/**
* @var int keep track of how many pages have been created.
*/
protected $pagecount = 0;
/**
* To be called from data reset code only,
* do not use in tests.
* @return void
*/
public function reset() {
$this->pagecount = 0;
parent::reset();
}
public function create_instance($record = null, array $options = null) {
global $CFG;
// Add default values for lesson.
$record = (array)$record + array(
'progressbar' => 0,
'ongoing' => 0,
'displayleft' => 0,
'displayleftif' => 0,
'slideshow' => 0,
'maxanswers' => $CFG->lesson_maxanswers,
'feedback' => 0,
'activitylink' => 0,
'available' => 0,
'deadline' => 0,
'usepassword' => 0,
'password' => '',
'dependency' => 0,
'timespent' => 0,
'completed' => 0,
'gradebetterthan' => 0,
'modattempts' => 0,
'review' => 0,
'maxattempts' => 1,
'nextpagedefault' => $CFG->lesson_defaultnextpage,
'maxpages' => 0,
'practice' => 0,
'custom' => 1,
'retake' => 0,
'usemaxgrade' => 0,
'minquestions' => 0,
'grade' => 100,
);
if (!isset($record['mediafile'])) {
require_once($CFG->libdir.'/filelib.php');
$record['mediafile'] = file_get_unused_draft_itemid();
}
return parent::create_instance($record, (array)$options);
}
public function create_content($lesson, $record = array()) {
global $DB, $CFG;
require_once($CFG->dirroot.'/mod/lesson/locallib.php');
$now = time();
$this->pagecount++;
$record = (array)$record + array(
'lessonid' => $lesson->id,
'title' => 'Lesson page '.$this->pagecount,
'timecreated' => $now,
'qtype' => 20, // LESSON_PAGE_BRANCHTABLE
'pageid' => 0, // By default insert in the beginning.
);
if (!isset($record['contents_editor'])) {
$record['contents_editor'] = array(
'text' => 'Contents of lesson page '.$this->pagecount,
'format' => FORMAT_MOODLE,
'itemid' => 0,
);
}
$context = context_module::instance($lesson->cmid);
$page = lesson_page::create((object)$record, new lesson($lesson), $context, $CFG->maxbytes);
return $DB->get_record('lesson_pages', array('id' => $page->id), '*', MUST_EXIST);
}
}

View File

@ -0,0 +1,73 @@
<?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_lesson generator tests
*
* @package mod_lesson
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Genarator tests class for mod_lesson.
*
* @package mod_lesson
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_lesson_generator_testcase extends advanced_testcase {
public function test_create_instance() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$this->assertFalse($DB->record_exists('lesson', array('course' => $course->id)));
$lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course));
$records = $DB->get_records('lesson', array('course' => $course->id), 'id');
$this->assertEquals(1, count($records));
$this->assertTrue(array_key_exists($lesson->id, $records));
$params = array('course' => $course->id, 'name' => 'Another lesson');
$lesson = $this->getDataGenerator()->create_module('lesson', $params);
$records = $DB->get_records('lesson', array('course' => $course->id), 'id');
$this->assertEquals(2, count($records));
$this->assertEquals('Another lesson', $records[$lesson->id]->name);
}
public function test_create_content() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course));
$lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
$page1 = $lessongenerator->create_content($lesson);
$page2 = $lessongenerator->create_content($lesson, array('title' => 'Custom title'));
$records = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id), 'id');
$this->assertEquals(2, count($records));
$this->assertEquals($page1->id, $records[$page1->id]->id);
$this->assertEquals($page2->id, $records[$page2->id]->id);
$this->assertEquals('Custom title', $records[$page2->id]->title);
}
}

View File

@ -37,37 +37,9 @@ defined('MOODLE_INTERNAL') || die();
*/
class mod_lti_generator extends testing_module_generator {
/**
* Create new lti module instance
*
* @param array|stdClass $record
* @param array $options
* @throws coding_exception
* @return stdClass activity record with extra cmid field
*/
public function create_instance($record = null, array $options = null) {
global $CFG;
require_once("$CFG->dirroot/mod/lti/lib.php");
$this->instancecount++;
$i = $this->instancecount;
$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', 'lti').' '.$i;
}
if (!isset($record->intro)) {
$record->intro = 'Test lti '.$i;
}
if (!isset($record->introformat)) {
$record->introformat = FORMAT_MOODLE;
}
if (!isset($record->toolurl)) {
$record->toolurl = 'http://www.imsglobal.org/developers/LTI/test/v1p1/tool.php';
}
@ -89,13 +61,6 @@ class mod_lti_generator extends testing_module_generator {
if (!isset($record->instructorchoiceacceptgrades)) {
$record->instructorchoiceacceptgrades = 1;
}
if (isset($options['idnumber'])) {
$record->cmidnumber = $options['idnumber'];
} else {
$record->cmidnumber = '';
}
$record->coursemodule = $this->precreate_course_module($record->course, $options);
$id = lti_add_instance($record, null);
return $this->post_add_instance($id, $record->coursemodule);
return parent::create_instance($record, (array)$options);
}
}

View File

@ -36,34 +36,12 @@ defined('MOODLE_INTERNAL') || die();
*/
class mod_page_generator extends testing_module_generator {
/**
* Create new page module instance
* @param array|stdClass $record
* @param array $options
* @return stdClass activity record with extra cmid field
*/
public function create_instance($record = null, array $options = null) {
global $CFG;
require_once("$CFG->dirroot/mod/page/locallib.php");
$this->instancecount++;
$i = $this->instancecount;
require_once($CFG->dirroot . '/lib/resourcelib.php');
$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;
}
if (!isset($record->intro)) {
$record->intro = 'Test page '.$i;
}
if (!isset($record->introformat)) {
$record->introformat = FORMAT_MOODLE;
}
if (!isset($record->content)) {
$record->content = 'Test page content';
}
@ -73,17 +51,10 @@ class mod_page_generator extends testing_module_generator {
if (!isset($record->display)) {
$record->display = RESOURCELIB_DISPLAY_AUTO;
}
if (isset($options['idnumber'])) {
$record->cmidnumber = $options['idnumber'];
} else {
$record->cmidnumber = '';
}
if (!isset($record->printintro)) {
$record->printintro = 0;
}
$record->coursemodule = $this->precreate_course_module($record->course, $options);
$id = page_add_instance($record, null);
return $this->post_add_instance($id, $record->coursemodule);
return parent::create_instance($record, (array)$options);
}
}

View File

@ -25,38 +25,15 @@ defined('MOODLE_INTERNAL') || die();
*/
class mod_quiz_generator extends testing_module_generator {
/**
* Create new quiz module instance.
* @param array|stdClass $record
* @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 $CFG;
require_once("$CFG->dirroot/mod/quiz/locallib.php");
$this->instancecount++;
$i = $this->instancecount;
require_once($CFG->dirroot.'/mod/quiz/locallib.php');
$record = (object)(array)$record;
$options = (array)$options;
if (empty($record->course)) {
throw new coding_exception('module generator requires $record->course');
}
if (isset($options['idnumber'])) {
$record->cmidnumber = $options['idnumber'];
} else {
$record->cmidnumber = '';
}
$alwaysvisible = mod_quiz_display_options::DURING | mod_quiz_display_options::IMMEDIATELY_AFTER |
mod_quiz_display_options::LATER_WHILE_OPEN | mod_quiz_display_options::AFTER_CLOSE;
$defaultquizsettings = array(
'name' => get_string('pluginname', 'quiz').' '.$i,
'intro' => 'Test quiz ' . $i,
'introformat' => FORMAT_MOODLE,
'timeopen' => 0,
'timeclose' => 0,
'preferredbehaviour' => 'deferredfeedback',
@ -99,8 +76,6 @@ class mod_quiz_generator extends testing_module_generator {
}
}
$record->coursemodule = $this->precreate_course_module($record->course, $options);
$id = quiz_add_instance($record);
return $this->post_add_instance($id, $record->coursemodule);
return parent::create_instance($record, (array)$options);
}
}

View File

@ -38,45 +38,23 @@ 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
* @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, $USER;
require_once($CFG->dirroot . '/mod/resource/locallib.php');
// Count generated modules.
$this->instancecount++;
$i = $this->instancecount;
require_once($CFG->dirroot . '/lib/resourcelib.php');
// 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->printintro)) {
$record->printintro = 0;
}
@ -101,14 +79,12 @@ class mod_resource_generator extends testing_module_generator {
// Add actual file there.
$filerecord = array('component' => 'user', 'filearea' => 'draft',
'contextid' => $usercontext->id, 'itemid' => $record->files,
'filename' => 'resource' . $i . '.txt', 'filepath' => '/');
'filename' => 'resource' . ($this->instancecount+1) . '.txt', 'filepath' => '/');
$fs = get_file_storage();
$fs->create_file_from_string($filerecord, 'Test resource ' . $i . ' file');
$fs->create_file_from_string($filerecord, 'Test resource ' . ($this->instancecount+1) . ' 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);
return parent::create_instance($record, (array)$options);
}
}

View File

@ -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/>.
/**
* mod_survey data generator.
*
* @package mod_survey
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* mod_survey data generator class.
*
* @package mod_survey
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_survey_generator extends testing_module_generator {
/**
* Cached list of available templates.
* @var array
*/
private $templates = null;
public function reset() {
$this->templates = null;
parent::reset();
}
public function create_instance($record = null, array $options = null) {
global $DB;
if ($this->templates === null) {
$this->templates = $DB->get_records_menu('survey', array('template' => 0), 'name', 'id, name');
}
if (empty($this->templates)) {
throw new moodle_exception('cannotfindsurveytmpt', 'survey');
}
$record = (array)$record;
if (isset($record['template']) && !is_number($record['template'])) {
// Substitute template name with template id.
$record['template'] = array_search($record['template'], $this->templates);
}
if (isset($record['template']) && !array_key_exists($record['template'], $this->templates)) {
throw new moodle_exception('cannotfindsurveytmpt', 'survey');
}
// Add default values for survey.
if (!isset($record['template'])) {
reset($this->templates);
$record['template'] = key($this->templates);
}
return parent::create_instance($record, (array)$options);
}
}

View File

@ -0,0 +1,95 @@
<?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_survey generator tests
*
* @package mod_survey
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Genarator tests class for mod_survey.
*
* @package mod_survey
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_survey_generator_testcase extends advanced_testcase {
public function test_create_instance() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$this->assertFalse($DB->record_exists('survey', array('course' => $course->id)));
$survey = $this->getDataGenerator()->create_module('survey', array('course' => $course));
$records = $DB->get_records('survey', array('course' => $course->id), 'id');
$this->assertEquals(1, count($records));
$this->assertTrue(array_key_exists($survey->id, $records));
$params = array('course' => $course->id, 'name' => 'Another survey');
$survey = $this->getDataGenerator()->create_module('survey', $params);
$records = $DB->get_records('survey', array('course' => $course->id), 'id');
$this->assertEquals(2, count($records));
$this->assertEquals('Another survey', $records[$survey->id]->name);
}
public function test_create_instance_with_template() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$templates = $DB->get_records_menu('survey', array('template' => 0), 'name', 'id, name');
$firsttemplateid = key($templates);
// By default survey is created with the first available template.
$survey = $this->getDataGenerator()->create_module('survey', array('course' => $course));
$record = $DB->get_record('survey', array('id' => $survey->id));
$this->assertEquals($firsttemplateid, $record->template);
// Survey can be created specifying the template id.
$tmplid = array_search('ciqname', $templates);
$survey = $this->getDataGenerator()->create_module('survey', array('course' => $course,
'template' => $tmplid));
$record = $DB->get_record('survey', array('id' => $survey->id));
$this->assertEquals($tmplid, $record->template);
// Survey can be created specifying the template name instead of id.
$survey = $this->getDataGenerator()->create_module('survey', array('course' => $course,
'template' => 'collesaname'));
$record = $DB->get_record('survey', array('id' => $survey->id));
$this->assertEquals(array_search('collesaname', $templates), $record->template);
// Survey can not be created specifying non-existing template id or name.
try {
$this->getDataGenerator()->create_module('survey', array('course' => $course,
'template' => 87654));
$this->fail('Exception about non-existing numeric template is expected');
} catch (Exception $e) {}
try {
$this->getDataGenerator()->create_module('survey', array('course' => $course,
'template' => 'nonexistingcode'));
$this->fail('Exception about non-existing string template is expected');
} catch (Exception $e) {}
}
}

View File

@ -0,0 +1,50 @@
<?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_url data generator.
*
* @package mod_url
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* mod_url data generator class.
*
* @package mod_url
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_url_generator extends testing_module_generator {
public function create_instance($record = null, array $options = null) {
global $CFG;
require_once($CFG->libdir.'/resourcelib.php');
// Add default values for url.
$record = (array)$record + array(
'display' => RESOURCELIB_DISPLAY_AUTO,
'externalurl' => 'http://moodle.org/',
);
return parent::create_instance($record, (array)$options);
}
}

View File

@ -0,0 +1,55 @@
<?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_url generator tests
*
* @package mod_url
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Genarator tests class for mod_url.
*
* @package mod_url
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_url_generator_testcase extends advanced_testcase {
public function test_create_instance() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$this->assertFalse($DB->record_exists('url', array('course' => $course->id)));
$url = $this->getDataGenerator()->create_module('url', array('course' => $course));
$records = $DB->get_records('url', array('course' => $course->id), 'id');
$this->assertEquals(1, count($records));
$this->assertTrue(array_key_exists($url->id, $records));
$params = array('course' => $course->id, 'name' => 'Another url');
$url = $this->getDataGenerator()->create_module('url', $params);
$records = $DB->get_records('url', array('course' => $course->id), 'id');
$this->assertEquals(2, count($records));
$this->assertEquals('Another url', $records[$url->id]->name);
}
}

View File

@ -21,6 +21,7 @@ class wiki_parser_proxy {
}
$type = strtolower($type);
self::$parsers[$type] = null; // Reset the current parser because it may have other options.
if(self::create_parser_instance($type)) {
return self::$parsers[$type]->parse($string, $options);
}
@ -56,15 +57,11 @@ class wiki_parser_proxy {
private static function create_parser_instance($type) {
if(empty(self::$parsers[$type])) {
if(include(self::$basepath."markups/$type.php")) {
$class = strtolower($type)."_parser";
if(class_exists($class)) {
self::$parsers[$type] = new $class;
return true;
}
else {
return false;
}
include_once(self::$basepath."markups/$type.php");
$class = strtolower($type)."_parser";
if(class_exists($class)) {
self::$parsers[$type] = new $class;
return true;
}
else {
return false;

View File

@ -0,0 +1,123 @@
<?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_wiki data generator.
*
* @package mod_wiki
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* mod_wiki data generator class.
*
* @package mod_wiki
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_wiki_generator extends testing_module_generator {
/**
* @var int keep track of how many pages have been created.
*/
protected $pagecount = 0;
/**
* To be called from data reset code only,
* do not use in tests.
* @return void
*/
public function reset() {
$this->pagecount = 0;
parent::reset();
}
public function create_instance($record = null, array $options = null) {
// Add default values for wiki.
$record = (array)$record + array(
'wikimode' => 'collaborative',
'firstpagetitle' => 'Front page for wiki '.($this->instancecount+1),
'defaultformat' => 'html',
'forceformat' => 0
);
return parent::create_instance($record, (array)$options);
}
public function create_content($wiki, $record = array()) {
$record = (array)$record + array(
'wikiid' => $wiki->id
);
return $this->create_page($wiki, $record);
}
public function create_first_page($wiki, $record = array()) {
$record = (array)$record + array(
'title' => $wiki->firstpagetitle,
);
return $this->create_page($wiki, $record);
}
/**
* Generates a page in wiki.
*
* @param stdClass wiki object returned from create_instance (if known)
* @param stdClass|array $record data to insert as wiki entry.
* @return stdClass
* @throws coding_exception if neither $record->wikiid nor $wiki->id is specified
*/
public function create_page($wiki, $record = array()) {
global $CFG, $USER;
require_once($CFG->dirroot.'/mod/wiki/locallib.php');
$this->pagecount++;
$record = (array)$record + array(
'title' => 'wiki page '.$this->pagecount,
'wikiid' => $wiki->id,
'subwikiid' => 0,
'group' => 0,
'content' => 'Wiki page content '.$this->pagecount,
'format' => $wiki->defaultformat
);
if (empty($record['wikiid']) && empty($record['subwikiid'])) {
throw new coding_exception('wiki page generator requires either wikiid or subwikiid');
}
if (!$record['subwikiid']) {
if (!isset($record['userid'])) {
$record['userid'] = ($wiki->wikimode == 'individual') ? $USER->id : 0;
}
if ($subwiki = wiki_get_subwiki_by_group($record['wikiid'], $record['group'], $record['userid'])) {
$record['subwikiid'] = $subwiki->id;
} else {
$record['subwikiid'] = wiki_add_subwiki($record['wikiid'], $record['group'], $record['userid']);
}
}
if ($wikipage = wiki_get_page_by_title($record['subwikiid'], $record['title'])) {
$rv = wiki_save_page($wikipage, $record['content'], $USER->id);
return $rv['page'];
}
$pageid = wiki_create_page($record['subwikiid'], $record['title'], $record['format'], $USER->id);
$wikipage = wiki_get_page($pageid);
$rv = wiki_save_page($wikipage, $record['content'], $USER->id);
return $rv['page'];
}
}

View File

@ -0,0 +1,121 @@
<?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_wiki generator tests
*
* @package mod_wiki
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Genarator tests class for mod_wiki.
*
* @package mod_wiki
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_wiki_generator_testcase extends advanced_testcase {
public function test_create_instance() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$this->assertFalse($DB->record_exists('wiki', array('course' => $course->id)));
$wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course));
$records = $DB->get_records('wiki', array('course' => $course->id), 'id');
$this->assertEquals(1, count($records));
$this->assertTrue(array_key_exists($wiki->id, $records));
$params = array('course' => $course->id, 'name' => 'Another wiki');
$wiki = $this->getDataGenerator()->create_module('wiki', $params);
$records = $DB->get_records('wiki', array('course' => $course->id), 'id');
$this->assertEquals(2, count($records));
$this->assertEquals('Another wiki', $records[$wiki->id]->name);
}
public function test_create_content() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course));
$wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki');
$page1 = $wikigenerator->create_first_page($wiki);
$page2 = $wikigenerator->create_content($wiki);
$page3 = $wikigenerator->create_content($wiki, array('title' => 'Custom title'));
$subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id), 'id');
$this->assertEquals(1, count($subwikis));
$subwikiid = key($subwikis);
$records = $DB->get_records('wiki_pages', array('subwikiid' => $subwikiid), 'id');
$this->assertEquals(3, count($records));
$this->assertEquals($page1->id, $records[$page1->id]->id);
$this->assertEquals($page2->id, $records[$page2->id]->id);
$this->assertEquals($page3->id, $records[$page3->id]->id);
$this->assertEquals('Custom title', $records[$page3->id]->title);
}
public function test_create_content_individual() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$wiki = $this->getDataGenerator()->create_module('wiki',
array('course' => $course, 'wikimode' => 'individual'));
$wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki');
$page1 = $wikigenerator->create_first_page($wiki);
$page2 = $wikigenerator->create_content($wiki);
$page3 = $wikigenerator->create_content($wiki, array('title' => 'Custom title for admin'));
$subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id), 'id');
$this->assertEquals(1, count($subwikis));
$subwikiid = key($subwikis);
$records = $DB->get_records('wiki_pages', array('subwikiid' => $subwikiid), 'id');
$this->assertEquals(3, count($records));
$this->assertEquals($page1->id, $records[$page1->id]->id);
$this->assertEquals($page2->id, $records[$page2->id]->id);
$this->assertEquals($page3->id, $records[$page3->id]->id);
$this->assertEquals('Custom title for admin', $records[$page3->id]->title);
$user = $this->getDataGenerator()->create_user();
$role = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
$this->setUser($user);
$page1s = $wikigenerator->create_first_page($wiki);
$page2s = $wikigenerator->create_content($wiki);
$page3s = $wikigenerator->create_content($wiki, array('title' => 'Custom title for student'));
$subwikis = $DB->get_records('wiki_subwikis', array('wikiid' => $wiki->id), 'id');
$this->assertEquals(2, count($subwikis));
next($subwikis);
$subwikiid = key($subwikis);
$records = $DB->get_records('wiki_pages', array('subwikiid' => $subwikiid), 'id');
$this->assertEquals(3, count($records));
$this->assertEquals($page1s->id, $records[$page1s->id]->id);
$this->assertEquals($page2s->id, $records[$page2s->id]->id);
$this->assertEquals($page3s->id, $records[$page3s->id]->id);
$this->assertEquals('Custom title for student', $records[$page3s->id]->title);
}
}

View File

@ -0,0 +1,100 @@
<?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_workshop data generator.
*
* @package mod_workshop
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* mod_workshop data generator class.
*
* @package mod_workshop
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_workshop_generator extends testing_module_generator {
public function create_instance($record = null, array $options = null) {
global $CFG;
require_once($CFG->libdir.'/filelib.php');
$workshopconfig = get_config('workshop');
// Add default values for workshop.
$record = (array)$record + array(
'strategy' => $workshopconfig->strategy,
'grade' => $workshopconfig->grade,
'gradinggrade' => $workshopconfig->gradinggrade,
'gradedecimals' => $workshopconfig->gradedecimals,
'nattachments' => 1,
'maxbytes' => $workshopconfig->maxbytes,
'latesubmissions' => 0,
'useselfassessment' => 0,
'overallfeedbackmode' => 1,
'overallfeedbackfiles' => 0,
'overallfeedbackmaxbytes' => $workshopconfig->maxbytes,
'useexamples' => 0,
'examplesmode' => $workshopconfig->examplesmode,
'submissionstart' => 0,
'submissionend' => 0,
'phaseswitchassessment' => 0,
'assessmentstart' => 0,
'assessmentend' => 0,
);
if (!isset($record['gradecategory']) || !isset($record['gradinggradecategory'])) {
require_once($CFG->libdir.'/gradelib.php');
$courseid = is_object($record['course']) ? $record['course']->id : $record['course'];
$gradecategories = grade_get_categories_menu($courseid);
reset($gradecategories);
$defaultcategory = key($gradecategories);
$record += array(
'gradecategory' => $defaultcategory,
'gradinggradecategory' => $defaultcategory
);
}
if (!isset($record['instructauthorseditor'])) {
$record['instructauthorseditor'] = array(
'text' => 'Instructions for submission '.($this->instancecount+1),
'format' => FORMAT_MOODLE,
'itemid' => file_get_unused_draft_itemid()
);
}
if (!isset($record['instructreviewerseditor'])) {
$record['instructreviewerseditor'] = array(
'text' => 'Instructions for assessment '.($this->instancecount+1),
'format' => FORMAT_MOODLE,
'itemid' => file_get_unused_draft_itemid()
);
}
if (!isset($record['conclusioneditor'])) {
$record['conclusioneditor'] = array(
'text' => 'Conclusion '.($this->instancecount+1),
'format' => FORMAT_MOODLE,
'itemid' => file_get_unused_draft_itemid()
);
}
return parent::create_instance($record, (array)$options);
}
}

View File

@ -0,0 +1,55 @@
<?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_workshop generator tests
*
* @package mod_workshop
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Genarator tests class for mod_workshop.
*
* @package mod_workshop
* @category test
* @copyright 2013 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_workshop_generator_testcase extends advanced_testcase {
public function test_create_instance() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$this->assertFalse($DB->record_exists('workshop', array('course' => $course->id)));
$workshop = $this->getDataGenerator()->create_module('workshop', array('course' => $course));
$records = $DB->get_records('workshop', array('course' => $course->id), 'id');
$this->assertEquals(1, count($records));
$this->assertTrue(array_key_exists($workshop->id, $records));
$params = array('course' => $course->id, 'name' => 'Another workshop');
$workshop = $this->getDataGenerator()->create_module('workshop', $params);
$records = $DB->get_records('workshop', array('course' => $course->id), 'id');
$this->assertEquals(2, count($records));
$this->assertEquals('Another workshop', $records[$workshop->id]->name);
}
}