/course/tests/courselib_test.php
PHP | 7022 lines | 5081 code | 872 blank | 1069 comment | 65 complexity | a297622f3cca4ec62c336ac19a97aab9 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
Large files files are truncated, but you can click here to view the full file
- <?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/>.
- /**
- * Course related unit tests
- *
- * @package core
- * @category phpunit
- * @copyright 2012 Petr Skoda {@link http://skodak.org}
- * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
- defined('MOODLE_INTERNAL') || die();
- global $CFG;
- require_once($CFG->dirroot . '/course/lib.php');
- require_once($CFG->dirroot . '/course/tests/fixtures/course_capability_assignment.php');
- require_once($CFG->dirroot . '/enrol/imsenterprise/tests/imsenterprise_test.php');
- class core_course_courselib_testcase extends advanced_testcase {
- /**
- * Set forum specific test values for calling create_module().
- *
- * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
- */
- private function forum_create_set_values(&$moduleinfo) {
- // Completion specific to forum - optional.
- $moduleinfo->completionposts = 3;
- $moduleinfo->completiondiscussions = 1;
- $moduleinfo->completionreplies = 2;
- // Specific values to the Forum module.
- $moduleinfo->forcesubscribe = FORUM_INITIALSUBSCRIBE;
- $moduleinfo->type = 'single';
- $moduleinfo->trackingtype = FORUM_TRACKING_FORCED;
- $moduleinfo->maxbytes = 10240;
- $moduleinfo->maxattachments = 2;
- // Post threshold for blocking - specific to forum.
- $moduleinfo->blockperiod = 60*60*24;
- $moduleinfo->blockafter = 10;
- $moduleinfo->warnafter = 5;
- // Grading of whole forum settings.
- $moduleinfo->grade_forum = 0;
- }
- /**
- * Execute test asserts on the saved DB data by create_module($forum).
- *
- * @param object $moduleinfo - the specific forum values that were used to create a forum.
- * @param object $dbmodinstance - the DB values of the created forum.
- */
- private function forum_create_run_asserts($moduleinfo, $dbmodinstance) {
- // Compare values specific to forums.
- $this->assertEquals($moduleinfo->forcesubscribe, $dbmodinstance->forcesubscribe);
- $this->assertEquals($moduleinfo->type, $dbmodinstance->type);
- $this->assertEquals($moduleinfo->assessed, $dbmodinstance->assessed);
- $this->assertEquals($moduleinfo->completionposts, $dbmodinstance->completionposts);
- $this->assertEquals($moduleinfo->completiondiscussions, $dbmodinstance->completiondiscussions);
- $this->assertEquals($moduleinfo->completionreplies, $dbmodinstance->completionreplies);
- $this->assertEquals($moduleinfo->scale, $dbmodinstance->scale);
- $this->assertEquals($moduleinfo->assesstimestart, $dbmodinstance->assesstimestart);
- $this->assertEquals($moduleinfo->assesstimefinish, $dbmodinstance->assesstimefinish);
- $this->assertEquals($moduleinfo->rsstype, $dbmodinstance->rsstype);
- $this->assertEquals($moduleinfo->rssarticles, $dbmodinstance->rssarticles);
- $this->assertEquals($moduleinfo->trackingtype, $dbmodinstance->trackingtype);
- $this->assertEquals($moduleinfo->maxbytes, $dbmodinstance->maxbytes);
- $this->assertEquals($moduleinfo->maxattachments, $dbmodinstance->maxattachments);
- $this->assertEquals($moduleinfo->blockperiod, $dbmodinstance->blockperiod);
- $this->assertEquals($moduleinfo->blockafter, $dbmodinstance->blockafter);
- $this->assertEquals($moduleinfo->warnafter, $dbmodinstance->warnafter);
- }
- /**
- * Set assign module specific test values for calling create_module().
- *
- * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
- */
- private function assign_create_set_values(&$moduleinfo) {
- // Specific values to the Assign module.
- $moduleinfo->alwaysshowdescription = true;
- $moduleinfo->submissiondrafts = true;
- $moduleinfo->requiresubmissionstatement = true;
- $moduleinfo->sendnotifications = true;
- $moduleinfo->sendlatenotifications = true;
- $moduleinfo->duedate = time() + (7 * 24 * 3600);
- $moduleinfo->cutoffdate = time() + (7 * 24 * 3600);
- $moduleinfo->gradingduedate = time() + (7 * 24 * 3600);
- $moduleinfo->allowsubmissionsfromdate = time();
- $moduleinfo->teamsubmission = true;
- $moduleinfo->requireallteammemberssubmit = true;
- $moduleinfo->teamsubmissiongroupingid = true;
- $moduleinfo->blindmarking = true;
- $moduleinfo->markingworkflow = true;
- $moduleinfo->markingallocation = true;
- $moduleinfo->assignsubmission_onlinetext_enabled = true;
- $moduleinfo->assignsubmission_file_enabled = true;
- $moduleinfo->assignsubmission_file_maxfiles = 1;
- $moduleinfo->assignsubmission_file_maxsizebytes = 1000000;
- $moduleinfo->assignsubmission_comments_enabled = true;
- $moduleinfo->assignfeedback_comments_enabled = true;
- $moduleinfo->assignfeedback_offline_enabled = true;
- $moduleinfo->assignfeedback_file_enabled = true;
- // Advanced grading.
- $gradingmethods = grading_manager::available_methods();
- $moduleinfo->advancedgradingmethod_submissions = current(array_keys($gradingmethods));
- }
- /**
- * Execute test asserts on the saved DB data by create_module($assign).
- *
- * @param object $moduleinfo - the specific assign module values that were used to create an assign module.
- * @param object $dbmodinstance - the DB values of the created assign module.
- */
- private function assign_create_run_asserts($moduleinfo, $dbmodinstance) {
- global $DB;
- $this->assertEquals($moduleinfo->alwaysshowdescription, $dbmodinstance->alwaysshowdescription);
- $this->assertEquals($moduleinfo->submissiondrafts, $dbmodinstance->submissiondrafts);
- $this->assertEquals($moduleinfo->requiresubmissionstatement, $dbmodinstance->requiresubmissionstatement);
- $this->assertEquals($moduleinfo->sendnotifications, $dbmodinstance->sendnotifications);
- $this->assertEquals($moduleinfo->duedate, $dbmodinstance->duedate);
- $this->assertEquals($moduleinfo->cutoffdate, $dbmodinstance->cutoffdate);
- $this->assertEquals($moduleinfo->allowsubmissionsfromdate, $dbmodinstance->allowsubmissionsfromdate);
- $this->assertEquals($moduleinfo->teamsubmission, $dbmodinstance->teamsubmission);
- $this->assertEquals($moduleinfo->requireallteammemberssubmit, $dbmodinstance->requireallteammemberssubmit);
- $this->assertEquals($moduleinfo->teamsubmissiongroupingid, $dbmodinstance->teamsubmissiongroupingid);
- $this->assertEquals($moduleinfo->blindmarking, $dbmodinstance->blindmarking);
- $this->assertEquals($moduleinfo->markingworkflow, $dbmodinstance->markingworkflow);
- $this->assertEquals($moduleinfo->markingallocation, $dbmodinstance->markingallocation);
- // The goal not being to fully test assign_add_instance() we'll stop here for the assign tests - to avoid too many DB queries.
- // Advanced grading.
- $cm = get_coursemodule_from_instance('assign', $dbmodinstance->id);
- $contextmodule = context_module::instance($cm->id);
- $advancedgradingmethod = $DB->get_record('grading_areas',
- array('contextid' => $contextmodule->id,
- 'activemethod' => $moduleinfo->advancedgradingmethod_submissions));
- $this->assertEquals($moduleinfo->advancedgradingmethod_submissions, $advancedgradingmethod);
- }
- /**
- * Run some asserts test for a specific module for the function create_module().
- *
- * The function has been created (and is called) for $this->test_create_module().
- * Note that the call to MODULE_create_set_values and MODULE_create_run_asserts are done after the common set values/run asserts.
- * So if you want, you can overwrite the default values/asserts in the respective functions.
- * @param string $modulename Name of the module ('forum', 'assign', 'book'...).
- */
- private function create_specific_module_test($modulename) {
- global $DB, $CFG;
- $this->resetAfterTest(true);
- $this->setAdminUser();
- // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
- require_once($CFG->dirroot.'/mod/'. $modulename .'/lib.php');
- // Enable avaibility.
- // If not enabled all conditional fields will be ignored.
- set_config('enableavailability', 1);
- // Enable course completion.
- // If not enabled all completion settings will be ignored.
- set_config('enablecompletion', COMPLETION_ENABLED);
- // Enable forum RSS feeds.
- set_config('enablerssfeeds', 1);
- set_config('forum_enablerssfeeds', 1);
- $course = $this->getDataGenerator()->create_course(array('numsections'=>1, 'enablecompletion' => COMPLETION_ENABLED),
- array('createsections'=>true));
- $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
- // Create assign module instance for test.
- $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
- $params['course'] = $course->id;
- $instance = $generator->create_instance($params);
- $assigncm = get_coursemodule_from_instance('assign', $instance->id);
- // Module test values.
- $moduleinfo = new stdClass();
- // Always mandatory generic values to any module.
- $moduleinfo->modulename = $modulename;
- $moduleinfo->section = 1; // This is the section number in the course. Not the section id in the database.
- $moduleinfo->course = $course->id;
- $moduleinfo->groupingid = $grouping->id;
- $moduleinfo->visible = true;
- $moduleinfo->visibleoncoursepage = true;
- // Sometimes optional generic values for some modules.
- $moduleinfo->name = 'My test module';
- $moduleinfo->showdescription = 1; // standard boolean
- require_once($CFG->libdir . '/gradelib.php');
- $gradecats = grade_get_categories_menu($moduleinfo->course, false);
- $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats
- $moduleinfo->gradecat = $gradecatid;
- $moduleinfo->groupmode = VISIBLEGROUPS;
- $moduleinfo->cmidnumber = 'idnumber_XXX';
- // Completion common to all module.
- $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC;
- $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED;
- $moduleinfo->completiongradeitemnumber = 1;
- $moduleinfo->completionexpected = time() + (7 * 24 * 3600);
- // Conditional activity.
- $moduleinfo->availability = '{"op":"&","showc":[true,true],"c":[' .
- '{"type":"date","d":">=","t":' . time() . '},' .
- '{"type":"date","d":"<","t":' . (time() + (7 * 24 * 3600)) . '}' .
- ']}';
- $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself.
- $moduleinfo->conditiongradegroup = array(array('conditiongradeitemid' => $coursegradeitem->id, 'conditiongrademin' => 10, 'conditiongrademax' => 80));
- $moduleinfo->conditionfieldgroup = array(array('conditionfield' => 'email', 'conditionfieldoperator' => \availability_profile\condition::OP_CONTAINS, 'conditionfieldvalue' => '@'));
- $moduleinfo->conditioncompletiongroup = array(array('conditionsourcecmid' => $assigncm->id, 'conditionrequiredcompletion' => COMPLETION_COMPLETE)); // "conditionsourcecmid == 0" => none
- // Grading and Advanced grading.
- require_once($CFG->dirroot . '/rating/lib.php');
- $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE;
- $moduleinfo->scale = 10; // Note: it could be minus (for specific course scale). It is a signed number.
- $moduleinfo->assesstimestart = time();
- $moduleinfo->assesstimefinish = time() + (7 * 24 * 3600);
- // RSS.
- $moduleinfo->rsstype = 2;
- $moduleinfo->rssarticles = 10;
- // Optional intro editor (depends of module).
- $draftid_editor = 0;
- file_prepare_draft_area($draftid_editor, null, null, null, null);
- $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
- // Following is the advanced grading method area called 'submissions' for the 'assign' module.
- if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
- $moduleinfo->grade = 100;
- }
- // Plagiarism form values.
- // No plagiarism plugin installed by default. Use this space to make your own test.
- // Values specific to the module.
- $modulesetvalues = $modulename.'_create_set_values';
- $this->$modulesetvalues($moduleinfo);
- // Create the module.
- $result = create_module($moduleinfo);
- // Retrieve the module info.
- $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance));
- $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance);
- // We passed the course section number to create_courses but $dbcm contain the section id.
- // We need to retrieve the db course section number.
- $section = $DB->get_record('course_sections', array('course' => $dbcm->course, 'id' => $dbcm->section));
- // Retrieve the grade item.
- $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course,
- 'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename));
- // Compare the values common to all module instances.
- $this->assertEquals($moduleinfo->modulename, $dbcm->modname);
- $this->assertEquals($moduleinfo->section, $section->section);
- $this->assertEquals($moduleinfo->course, $dbcm->course);
- $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
- $this->assertEquals($moduleinfo->visible, $dbcm->visible);
- $this->assertEquals($moduleinfo->completion, $dbcm->completion);
- $this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
- $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber);
- $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected);
- $this->assertEquals($moduleinfo->availability, $dbcm->availability);
- $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription);
- $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode);
- $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber);
- $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid);
- // Optional grade testing.
- if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
- $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade);
- }
- // Some optional (but quite common) to some module.
- $this->assertEquals($moduleinfo->name, $dbmodinstance->name);
- $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro);
- $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat);
- // Test specific to the module.
- $modulerunasserts = $modulename.'_create_run_asserts';
- $this->$modulerunasserts($moduleinfo, $dbmodinstance);
- return $moduleinfo;
- }
- /**
- * Create module associated blog and tags.
- *
- * @param object $course Course.
- * @param object $modulecontext The context of the module.
- */
- private function create_module_asscociated_blog($course, $modulecontext) {
- global $DB, $CFG;
- // Create default group.
- $group = new stdClass();
- $group->courseid = $course->id;
- $group->name = 'Group';
- $group->id = $DB->insert_record('groups', $group);
- // Create default user.
- $user = $this->getDataGenerator()->create_user(array(
- 'username' => 'testuser',
- 'firstname' => 'Firsname',
- 'lastname' => 'Lastname'
- ));
- // Create default post.
- $post = new stdClass();
- $post->userid = $user->id;
- $post->groupid = $group->id;
- $post->content = 'test post content text';
- $post->module = 'blog';
- $post->id = $DB->insert_record('post', $post);
- // Create default tag.
- $tag = $this->getDataGenerator()->create_tag(array('userid' => $user->id,
- 'rawname' => 'Testtagname', 'isstandard' => 1));
- // Apply the tag to the blog.
- $DB->insert_record('tag_instance', array('tagid' => $tag->id, 'itemtype' => 'user',
- 'component' => 'core', 'itemid' => $post->id, 'ordering' => 0));
- require_once($CFG->dirroot . '/blog/locallib.php');
- $blog = new blog_entry($post->id);
- $blog->add_association($modulecontext->id);
- return $blog;
- }
- /**
- * Test create_module() for multiple modules defined in the $modules array (first declaration of the function).
- */
- public function test_create_module() {
- // Add the module name you want to test here.
- // Create the match MODULENAME_create_set_values() and MODULENAME_create_run_asserts().
- $modules = array('forum', 'assign');
- // Run all tests.
- foreach ($modules as $modulename) {
- $this->create_specific_module_test($modulename);
- }
- }
- /**
- * Test update_module() for multiple modules defined in the $modules array (first declaration of the function).
- */
- public function test_update_module() {
- // Add the module name you want to test here.
- // Create the match MODULENAME_update_set_values() and MODULENAME_update_run_asserts().
- $modules = array('forum');
- // Run all tests.
- foreach ($modules as $modulename) {
- $this->update_specific_module_test($modulename);
- }
- }
- /**
- * Set forum specific test values for calling update_module().
- *
- * @param object $moduleinfo - the moduleinfo to add some specific values - passed in reference.
- */
- private function forum_update_set_values(&$moduleinfo) {
- // Completion specific to forum - optional.
- $moduleinfo->completionposts = 3;
- $moduleinfo->completiondiscussions = 1;
- $moduleinfo->completionreplies = 2;
- // Specific values to the Forum module.
- $moduleinfo->forcesubscribe = FORUM_INITIALSUBSCRIBE;
- $moduleinfo->type = 'single';
- $moduleinfo->trackingtype = FORUM_TRACKING_FORCED;
- $moduleinfo->maxbytes = 10240;
- $moduleinfo->maxattachments = 2;
- // Post threshold for blocking - specific to forum.
- $moduleinfo->blockperiod = 60*60*24;
- $moduleinfo->blockafter = 10;
- $moduleinfo->warnafter = 5;
- // Grading of whole forum settings.
- $moduleinfo->grade_forum = 0;
- }
- /**
- * Execute test asserts on the saved DB data by update_module($forum).
- *
- * @param object $moduleinfo - the specific forum values that were used to update a forum.
- * @param object $dbmodinstance - the DB values of the updated forum.
- */
- private function forum_update_run_asserts($moduleinfo, $dbmodinstance) {
- // Compare values specific to forums.
- $this->assertEquals($moduleinfo->forcesubscribe, $dbmodinstance->forcesubscribe);
- $this->assertEquals($moduleinfo->type, $dbmodinstance->type);
- $this->assertEquals($moduleinfo->assessed, $dbmodinstance->assessed);
- $this->assertEquals($moduleinfo->completionposts, $dbmodinstance->completionposts);
- $this->assertEquals($moduleinfo->completiondiscussions, $dbmodinstance->completiondiscussions);
- $this->assertEquals($moduleinfo->completionreplies, $dbmodinstance->completionreplies);
- $this->assertEquals($moduleinfo->scale, $dbmodinstance->scale);
- $this->assertEquals($moduleinfo->assesstimestart, $dbmodinstance->assesstimestart);
- $this->assertEquals($moduleinfo->assesstimefinish, $dbmodinstance->assesstimefinish);
- $this->assertEquals($moduleinfo->rsstype, $dbmodinstance->rsstype);
- $this->assertEquals($moduleinfo->rssarticles, $dbmodinstance->rssarticles);
- $this->assertEquals($moduleinfo->trackingtype, $dbmodinstance->trackingtype);
- $this->assertEquals($moduleinfo->maxbytes, $dbmodinstance->maxbytes);
- $this->assertEquals($moduleinfo->maxattachments, $dbmodinstance->maxattachments);
- $this->assertEquals($moduleinfo->blockperiod, $dbmodinstance->blockperiod);
- $this->assertEquals($moduleinfo->blockafter, $dbmodinstance->blockafter);
- $this->assertEquals($moduleinfo->warnafter, $dbmodinstance->warnafter);
- }
- /**
- * Test a specific type of module.
- *
- * @param string $modulename - the module name to test
- */
- private function update_specific_module_test($modulename) {
- global $DB, $CFG;
- $this->resetAfterTest(true);
- $this->setAdminUser();
- // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard.
- require_once($CFG->dirroot.'/mod/'. $modulename .'/lib.php');
- // Enable avaibility.
- // If not enabled all conditional fields will be ignored.
- set_config('enableavailability', 1);
- // Enable course completion.
- // If not enabled all completion settings will be ignored.
- set_config('enablecompletion', COMPLETION_ENABLED);
- // Enable forum RSS feeds.
- set_config('enablerssfeeds', 1);
- set_config('forum_enablerssfeeds', 1);
- $course = $this->getDataGenerator()->create_course(array('numsections'=>1, 'enablecompletion' => COMPLETION_ENABLED),
- array('createsections'=>true));
- $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id));
- // Create assign module instance for testing gradeitem.
- $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
- $params['course'] = $course->id;
- $instance = $generator->create_instance($params);
- $assigncm = get_coursemodule_from_instance('assign', $instance->id);
- // Create the test forum to update.
- $initvalues = new stdClass();
- $initvalues->introformat = FORMAT_HTML;
- $initvalues->course = $course->id;
- $forum = self::getDataGenerator()->create_module('forum', $initvalues);
- // Retrieve course module.
- $cm = get_coursemodule_from_instance('forum', $forum->id);
- // Module test values.
- $moduleinfo = new stdClass();
- // Always mandatory generic values to any module.
- $moduleinfo->coursemodule = $cm->id;
- $moduleinfo->modulename = $modulename;
- $moduleinfo->course = $course->id;
- $moduleinfo->groupingid = $grouping->id;
- $moduleinfo->visible = true;
- $moduleinfo->visibleoncoursepage = true;
- // Sometimes optional generic values for some modules.
- $moduleinfo->name = 'My test module';
- $moduleinfo->showdescription = 1; // standard boolean
- require_once($CFG->libdir . '/gradelib.php');
- $gradecats = grade_get_categories_menu($moduleinfo->course, false);
- $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats
- $moduleinfo->gradecat = $gradecatid;
- $moduleinfo->groupmode = VISIBLEGROUPS;
- $moduleinfo->cmidnumber = 'idnumber_XXX';
- // Completion common to all module.
- $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC;
- $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED;
- $moduleinfo->completiongradeitemnumber = 1;
- $moduleinfo->completionexpected = time() + (7 * 24 * 3600);
- $moduleinfo->completionunlocked = 1;
- // Conditional activity.
- $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself.
- $moduleinfo->availability = json_encode(\core_availability\tree::get_root_json(
- array(\availability_date\condition::get_json('>=', time()),
- \availability_date\condition::get_json('<', time() + (7 * 24 * 3600)),
- \availability_grade\condition::get_json($coursegradeitem->id, 10, 80),
- \availability_profile\condition::get_json(false, 'email', 'contains', '@'),
- \availability_completion\condition::get_json($assigncm->id, COMPLETION_COMPLETE)), '&'));
- // Grading and Advanced grading.
- require_once($CFG->dirroot . '/rating/lib.php');
- $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE;
- $moduleinfo->scale = 10; // Note: it could be minus (for specific course scale). It is a signed number.
- $moduleinfo->assesstimestart = time();
- $moduleinfo->assesstimefinish = time() + (7 * 24 * 3600);
- // RSS.
- $moduleinfo->rsstype = 2;
- $moduleinfo->rssarticles = 10;
- // Optional intro editor (depends of module).
- $draftid_editor = 0;
- file_prepare_draft_area($draftid_editor, null, null, null, null);
- $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor);
- // Following is the advanced grading method area called 'submissions' for the 'assign' module.
- if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
- $moduleinfo->grade = 100;
- }
- // Plagiarism form values.
- // No plagiarism plugin installed by default. Use this space to make your own test.
- // Values specific to the module.
- $modulesetvalues = $modulename.'_update_set_values';
- $this->$modulesetvalues($moduleinfo);
- // Create the module.
- $result = update_module($moduleinfo);
- // Retrieve the module info.
- $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance));
- $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance);
- // Retrieve the grade item.
- $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course,
- 'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename));
- // Compare the values common to all module instances.
- $this->assertEquals($moduleinfo->modulename, $dbcm->modname);
- $this->assertEquals($moduleinfo->course, $dbcm->course);
- $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid);
- $this->assertEquals($moduleinfo->visible, $dbcm->visible);
- $this->assertEquals($moduleinfo->completion, $dbcm->completion);
- $this->assertEquals($moduleinfo->completionview, $dbcm->completionview);
- $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber);
- $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected);
- $this->assertEquals($moduleinfo->availability, $dbcm->availability);
- $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription);
- $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode);
- $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber);
- $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid);
- // Optional grade testing.
- if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) {
- $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade);
- }
- // Some optional (but quite common) to some module.
- $this->assertEquals($moduleinfo->name, $dbmodinstance->name);
- $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro);
- $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat);
- // Test specific to the module.
- $modulerunasserts = $modulename.'_update_run_asserts';
- $this->$modulerunasserts($moduleinfo, $dbmodinstance);
- return $moduleinfo;
- }
- /**
- * Data provider for course_delete module
- *
- * @return array An array of arrays contain test data
- */
- public function provider_course_delete_module() {
- $data = array();
- $data['assign'] = array('assign', array('duedate' => time()));
- $data['quiz'] = array('quiz', array('duedate' => time()));
- return $data;
- }
- /**
- * Test the create_course function
- */
- public function test_create_course() {
- global $DB;
- $this->resetAfterTest(true);
- $defaultcategory = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
- $course = new stdClass();
- $course->fullname = 'Apu loves Unit Təsts';
- $course->shortname = 'Spread the lŭve';
- $course->idnumber = '123';
- $course->summary = 'Awesome!';
- $course->summaryformat = FORMAT_PLAIN;
- $course->format = 'topics';
- $course->newsitems = 0;
- $course->category = $defaultcategory;
- $original = (array) $course;
- $created = create_course($course);
- $context = context_course::instance($created->id);
- // Compare original and created.
- $this->assertEquals($original, array_intersect_key((array) $created, $original));
- // Ensure default section is created.
- $sectioncreated = $DB->record_exists('course_sections', array('course' => $created->id, 'section' => 0));
- $this->assertTrue($sectioncreated);
- // Ensure that the shortname isn't duplicated.
- try {
- $created = create_course($course);
- $this->fail('Exception expected');
- } catch (moodle_exception $e) {
- $this->assertSame(get_string('shortnametaken', 'error', $course->shortname), $e->getMessage());
- }
- // Ensure that the idnumber isn't duplicated.
- $course->shortname .= '1';
- try {
- $created = create_course($course);
- $this->fail('Exception expected');
- } catch (moodle_exception $e) {
- $this->assertSame(get_string('courseidnumbertaken', 'error', $course->idnumber), $e->getMessage());
- }
- }
- public function test_create_course_with_generator() {
- global $DB;
- $this->resetAfterTest(true);
- $course = $this->getDataGenerator()->create_course();
- // Ensure default section is created.
- $sectioncreated = $DB->record_exists('course_sections', array('course' => $course->id, 'section' => 0));
- $this->assertTrue($sectioncreated);
- }
- public function test_create_course_sections() {
- global $DB;
- $this->resetAfterTest(true);
- $numsections = 5;
- $course = $this->getDataGenerator()->create_course(
- array('shortname' => 'GrowingCourse',
- 'fullname' => 'Growing Course',
- 'numsections' => $numsections),
- array('createsections' => true));
- // Ensure all 6 (0-5) sections were created and course content cache works properly
- $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
- $this->assertEquals(range(0, $numsections), $sectionscreated);
- // this will do nothing, section already exists
- $this->assertFalse(course_create_sections_if_missing($course, $numsections));
- // this will create new section
- $this->assertTrue(course_create_sections_if_missing($course, $numsections + 1));
- // Ensure all 7 (0-6) sections were created and modinfo/sectioninfo cache works properly
- $sectionscreated = array_keys(get_fast_modinfo($course)->get_section_info_all());
- $this->assertEquals(range(0, $numsections + 1), $sectionscreated);
- }
- public function test_update_course() {
- global $DB;
- $this->resetAfterTest();
- $defaultcategory = $DB->get_field_select('course_categories', 'MIN(id)', 'parent = 0');
- $course = new stdClass();
- $course->fullname = 'Apu loves Unit Təsts';
- $course->shortname = 'test1';
- $course->idnumber = '1';
- $course->summary = 'Awesome!';
- $course->summaryformat = FORMAT_PLAIN;
- $course->format = 'topics';
- $course->newsitems = 0;
- $course->numsections = 5;
- $course->category = $defaultcategory;
- $created = create_course($course);
- // Ensure the checks only work on idnumber/shortname that are not already ours.
- update_course($created);
- $course->shortname = 'test2';
- $course->idnumber = '2';
- $created2 = create_course($course);
- // Test duplicate idnumber.
- $created2->idnumber = '1';
- try {
- update_course($created2);
- $this->fail('Expected exception when trying to update a course with duplicate idnumber');
- } catch (moodle_exception $e) {
- $this->assertEquals(get_string('courseidnumbertaken', 'error', $created2->idnumber), $e->getMessage());
- }
- // Test duplicate shortname.
- $created2->idnumber = '2';
- $created2->shortname = 'test1';
- try {
- update_course($created2);
- $this->fail('Expected exception when trying to update a course with a duplicate shortname');
- } catch (moodle_exception $e) {
- $this->assertEquals(get_string('shortnametaken', 'error', $created2->shortname), $e->getMessage());
- }
- }
- public function test_update_course_section_time_modified() {
- global $DB;
- $this->resetAfterTest();
- // Create the course with sections.
- $course = $this->getDataGenerator()->create_course(array('numsections' => 10), array('createsections' => true));
- $sections = $DB->get_records('course_sections', array('course' => $course->id));
- // Get the last section's time modified value.
- $section = array_pop($sections);
- $oldtimemodified = $section->timemodified;
- // Update the section.
- $this->waitForSecond(); // Ensuring that the section update occurs at a different timestamp.
- course_update_section($course, $section, array());
- // Check that the time has changed.
- $section = $DB->get_record('course_sections', array('id' => $section->id));
- $newtimemodified = $section->timemodified;
- $this->assertGreaterThan($oldtimemodified, $newtimemodified);
- }
- /**
- * Relative dates mode settings provider for course creation.
- */
- public function create_course_relative_dates_provider() {
- return [
- [0, 0, 0],
- [0, 1, 0],
- [1, 0, 0],
- [1, 1, 1],
- ];
- }
- /**
- * Test create_course by attempting to change the relative dates mode.
- *
- * @dataProvider create_course_relative_dates_provider
- * @param int $setting The value for the 'enablecourserelativedates' admin setting.
- * @param int $mode The value for the course's 'relativedatesmode' field.
- * @param int $expectedvalue The expected value of the 'relativedatesmode' field after course creation.
- */
- public function test_relative_dates_mode_for_course_creation($setting, $mode, $expectedvalue) {
- global $DB;
- $this->resetAfterTest();
- set_config('enablecourserelativedates', $setting);
- // Generate a course with relative dates mode set to $mode.
- $course = $this->getDataGenerator()->create_course(['relativedatesmode' => $mode]);
- // Verify that the relative dates match what's expected.
- $relativedatesmode = $DB->get_field('course', 'relativedatesmode', ['id' => $course->id]);
- $this->assertEquals($expectedvalue, $relativedatesmode);
- }
- /**
- * Test update_course by attempting to change the relative dates mode.
- */
- public function test_relative_dates_mode_for_course_update() {
- global $DB;
- $this->resetAfterTest();
- set_config('enablecourserelativedates', 1);
- // Generate a course with relative dates mode set to 1.
- $course = $this->getDataGenerator()->create_course(['relativedatesmode' => 1]);
- // Attempt to update the course with a changed relativedatesmode.
- $course->relativedatesmode = 0;
- update_course($course);
- // Verify that the relative dates mode has not changed.
- $relativedatesmode = $DB->get_field('course', 'relativedatesmode', ['id' => $course->id]);
- $this->assertEquals(1, $relativedatesmode);
- }
- public function test_course_add_cm_to_section() {
- global $DB;
- $this->resetAfterTest(true);
- // Create course with 1 section.
- $course = $this->getDataGenerator()->create_course(
- array('shortname' => 'GrowingCourse',
- 'fullname' => 'Growing Course',
- 'numsections' => 1),
- array('createsections' => true));
- // Trash modinfo.
- rebuild_course_cache($course->id, true);
- // Create some cms for testing.
- $cmids = array();
- for ($i=0; $i<4; $i++) {
- $cmids[$i] = $DB->insert_record('course_modules', array('course' => $course->id));
- }
- // Add it to section that exists.
- course_add_cm_to_section($course, $cmids[0], 1);
- // Check it got added to sequence.
- $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 1));
- $this->assertEquals($cmids[0], $sequence);
- // Add a second, this time using courseid variant of parameters.
- $coursecacherev = $DB->get_field('course', 'cacherev', array('id' => $course->id));
- course_add_cm_to_section($course->id, $cmids[1], 1);
- $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 1));
- $this->assertEquals($cmids[0] . ',' . $cmids[1], $sequence);
- // Check that modinfo cache was reset but not rebuilt (important for performance if calling repeatedly).
- $this->assertGreaterThan($coursecacherev, $DB->get_field('course', 'cacherev', array('id' => $course->id)));
- $this->assertEmpty(cache::make('core', 'coursemodinfo')->get($course->id));
- // Add one to section that doesn't exist (this might rebuild modinfo).
- course_add_cm_to_section($course, $cmids[2], 2);
- $this->assertEquals(3, $DB->count_records('course_sections', array('course' => $course->id)));
- $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 2));
- $this->assertEquals($cmids[2], $sequence);
- // Add using the 'before' option.
- course_add_cm_to_section($course, $cmids[3], 2, $cmids[2]);
- $this->assertEquals(3, $DB->count_records('course_sections', array('course' => $course->id)));
- $sequence = $DB->get_field('course_sections', 'sequence', array('course' => $course->id, 'section' => 2));
- $this->assertEquals($cmids[3] . ',' . $cmids[2], $sequence);
- }
- public function test_reorder_sections() {
- global $DB;
- $this->resetAfterTest(true);
- $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
- $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
- $oldsections = array();
- $sections = array();
- foreach ($DB->get_records('course_sections', array('course'=>$course->id), 'id') as $section) {
- $oldsections[$section->section] = $section->id;
- $sections[$section->id] = $section->section;
- }
- ksort($oldsections);
- $neworder = reorder_sections($sections, 2, 4);
- $neworder = array_keys($neworder);
- $this->assertEquals($oldsections[0], $neworder[0]);
- $this->assertEquals($oldsections[1], $neworder[1]);
- $this->assertEquals($oldsections[2], $neworder[4]);
- $this->assertEquals($oldsections[3], $neworder[2]);
- $this->assertEquals($oldsections[4], $neworder[3]);
- $this->assertEquals($oldsections[5], $neworder[5]);
- $this->assertEquals($oldsections[6], $neworder[6]);
- $neworder = reorder_sections($sections, 4, 2);
- $neworder = array_keys($neworder);
- $this->assertEquals($oldsections[0], $neworder[0]);
- $this->assertEquals($oldsections[1], $neworder[1]);
- $this->assertEquals($oldsections[2], $neworder[3]);
- $this->assertEquals($oldsections[3], $neworder[4]);
- $this->assertEquals($oldsections[4], $neworder[2]);
- $this->assertEquals($oldsections[5], $neworder[5]);
- $this->assertEquals($oldsections[6], $neworder[6]);
- $neworder = reorder_sections(1, 2, 4);
- $this->assertFalse($neworder);
- }
- public function test_move_section_down() {
- global $DB;
- $this->resetAfterTest(true);
- $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
- $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
- $oldsections = array();
- foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
- $oldsections[$section->section] = $section->id;
- }
- ksort($oldsections);
- // Test move section down..
- move_section_to($course, 2, 4);
- $sections = array();
- foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
- $sections[$section->section] = $section->id;
- }
- ksort($sections);
- $this->assertEquals($oldsections[0], $sections[0]);
- $this->assertEquals($oldsections[1], $sections[1]);
- $this->assertEquals($oldsections[2], $sections[4]);
- $this->assertEquals($oldsections[3], $sections[2]);
- $this->assertEquals($oldsections[4], $sections[3]);
- $this->assertEquals($oldsections[5], $sections[5]);
- $this->assertEquals($oldsections[6], $sections[6]);
- }
- public function test_move_section_up() {
- global $DB;
- $this->resetAfterTest(true);
- $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
- $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
- $oldsections = array();
- foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
- $oldsections[$section->section] = $section->id;
- }
- ksort($oldsections);
- // Test move section up..
- move_section_to($course, 6, 4);
- $sections = array();
- foreach ($DB->get_records('course_sections', array('course'=>$course->id)) as $section) {
- $sections[$section->section] = $section->id;
- }
- ksort($sections);
- $this->assertEquals($oldsections[0], $sections[0]);
- $this->assertEquals($oldsections[1], $sections[1]);
- $this->assertEquals($oldsections[2], $sections[2]);
- $this->assertEquals($oldsections[3], $sections[3]);
- $this->assertEquals($oldsections[4], $sections[5]);
- $this->assertEquals($oldsections[5], $sections[6]);
- $this->assertEquals($oldsections[6], $sections[4]);
- }
- public function test_move_section_marker() {
- global $DB;
- $this->resetAfterTest(true);
- $this->getDataGenerator()->create_course(array('numsections'=>5), array('createsections'=>true));
- $course = $this->getDataGenerator()->create_course(array('numsections'=>10), array('createsections'=>true));
- // Set course marker to the section we are going to move..
- course_set_marker($course->id, 2);
- // Verify that the course marker is set correctly.
- $course = $DB->get_record('course', array('id' => $course->id));
- $this->assertEquals(2, $course->marker);
- // Test move the marked section down..
- move_section_to($course, 2, 4);
- // Verify that the course marker has been moved along with the section..
- $course = $DB->get_record('course', array('id' => $course->id));
- $this->assertEquals(4, $course->marker);
- // Test move the marked section up..
- move_section_to($course, 4, 3);
- // Verify that the course marker has been moved along with the section..
- $course = $DB->get_record('course', array('id' => $course->id));
- $this->assertEquals(3, $course->marker);
- // Test moving a non-marked section above the marked section..
- move_section_to($course, 4, 2);
- // Verify that the course marker has been moved down to accomodate..
- $course = $DB->get_record('course', array('id' => $course->id));
- $this->assertEquals(4, $course->marker);
- // Test moving a non-marked section below the marked section..
- move_section_to($course, 3, 6);
- // Verify that the course marker has been up to accomodate..
- $course = $DB->get_record('course', array('id' => $course->id));
- $this->assertEquals(3, $course->marker);
- }
- public function test_course_can_delete_section() {
- global $DB;
- $this->resetAfterTest(true);
- $generator = $this->getDataGenerator();
- $courseweeks = $generator->create_course(
- array('numsections' => 5, 'format' => 'weeks'),
- array('createsections' => true));
- $assign1 = $generator->create_module('assign', array('course' => $courseweeks, 'section' => 1));
- $assign2 = $generator->create_module('assign', array('course' => $courseweeks, 'section' => 2));
- $coursetopics = $generator->create_course(
- array('numsections' => 5, 'format' => 'topics'),
- array('createsections' => true));
- $coursesingleactivity = $generator->create_course(
- array('format' => 'singleactivity'),
- array('createsections' => true));
- // Enrol student and teacher.
- $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
- $student = $generator->create_user();
- $teacher = $generator->create_user();
- $generator->enrol_user($student->id, $courseweeks->id, $roleids['student']);
- $generator->enrol_user($teacher->id, $courseweeks->id, $roleids['editingteacher']);
- $generator->enrol_user($student->id, $coursetopics->id, $roleids['student']);
- $generator->enrol_user($teacher->id, $coursetopics->id, $roleids['editingteacher']);
- $generator->enrol_user($student->id, $coursesingleactivity->id, $roleids['student']);
- $generator->enrol_user($teacher->id, $coursesingleactivity->id, $roleids['editingteacher']);
- // Teacher should be able to delete sections (except for 0) in topics and weeks format.
- $this->setUser($teacher);
- // For topics and weeks formats will return false for section 0 and true for any other section.
- $this->assertFalse(course_can_delete_section($courseweeks, 0));
- $this->assertTrue(course_can_delete_section($courseweeks, 1));
- $this->assertFalse(course_can_delete_section($coursetopics, 0));
- $this->assertTrue(course_can_delete_section($coursetopics, 1));
- // For singleactivity course format no section can be deleted.
- $this->assertFalse(course_can_delete_section($coursesingleactivity, 0));
- $this->assertFalse(course_can_delete_section($coursesingleactivity, 1));
- // Now let's revoke a capability from teacher to manage activity in section 1.
- $modulecontext = context_module::instance($assign1->cmid);
- assign_capability('moodle/course:manageactivities', CAP_PROHIBIT, $roleids['editingteacher'],
- $modulecontext);
- $this->assertFalse(course_can_delete_section($courseweeks, 1));
- $this->assertTrue(course_can_delete_section($courseweeks, 2));
- // Student does not have permissions to delete sections.
- $this->setUser($student);
- $this->assertFalse(course_can_delete_section($courseweeks, 1));
- $this->assertFalse(course_can_delete_section($coursetopics, 1));
- $this->assertFalse(course_can_delete_section($coursesingleactivity, 1));
- }
- public function test_course_delete_section() {
- global $DB;
- $this->resetAfterTest(true);
- $generator = $this->getDataGenerator();
- $course = $generator->create_course(array('numsections' => 6, 'format' => 'topics'),
- array('createsections' => true));
- $assign0 = $generator->create_module('assign', array('course' => $c…
Large files files are truncated, but you can click here to view the full file