MDL-43440 unit testing: Use cmid in unit tests when it should be used.

instance is has been used instead of cmid.  This works as all id's are 1
in most cases, but not always.

accesslib_clear_all_caches_for_unit_testing was updated to clear the
$USER->access cache which would fail test when the wrong user's data was used.

Conflicts from extra tests in master:
	blocks/comments/tests/events_test.php
	mod/assign/submission/comments/tests/events_test.php
	mod/data/tests/lib_test.php
	mod/glossary/tests/events_test.php
This commit is contained in:
Russell Smith
2014-01-24 14:56:53 +11:00
parent b88bb98ab7
commit cac73cf1ec
15 changed files with 30 additions and 30 deletions
+1 -1
View File
@@ -233,7 +233,7 @@ class core_badges_badgeslib_testcase extends advanced_testcase {
$criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
$criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
$criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_ACTIVITY, 'badgeid' => $badge->id));
$criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'module_'.$this->module->id => $this->module->id));
$criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'module_'.$this->module->cmid => $this->module->cmid));
// Set completion for forum activity.
$c = new completion_info($this->course);
+2 -2
View File
@@ -223,7 +223,7 @@ class core_files_externallib_testcase extends advanced_testcase {
// Insert the information about the file.
$contentid = $DB->insert_record('data_content', $datacontent);
// Required information for uploading a file.
$context = context_module::instance($module->id);
$context = context_module::instance($module->cmid);
$usercontext = context_user::instance($USER->id);
$component = 'mod_data';
$filearea = 'content';
@@ -301,7 +301,7 @@ class core_files_externallib_testcase extends advanced_testcase {
$modified = 0;
// Context level and instance ID are used to determine what the context is.
$contextlevel = 'module';
$instanceid = $module->id;
$instanceid = $module->cmid;
$testfilelisting = core_files_external::get_files($nocontext, $component, $filearea, $itemid, '/', $filename, $modified, $contextlevel, $instanceid);
$this->assertEquals($testfilelisting, $testdata);
}
+1 -1
View File
@@ -52,7 +52,7 @@ class core_grade_edittreelib_testcase extends advanced_testcase {
$scale = $this->getDataGenerator()->create_scale();
$course = $this->getDataGenerator()->create_course();
$assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
$modulecontext = context_module::instance($assign->id);
$modulecontext = context_module::instance($assign->cmid);
// The generator returns a dummy object, lets get the real assign object.
$assign = new assign($modulecontext, false, false);
$cm = $assign->get_course_module();
+2 -2
View File
@@ -66,7 +66,7 @@ class core_grading_externallib_testcase extends externallib_advanced_testcase {
// Create a teacher and give them capabilities.
$coursecontext = context_course::instance($course->id);
$roleid = $this->assignUserCapability('moodle/course:viewparticipants', $coursecontext->id, 3);
$modulecontext = context_module::instance($cm->id);
$modulecontext = context_module::instance($cm->cmid);
$this->assignUserCapability('mod/assign:grade', $modulecontext->id, $roleid);
// Create the teacher's enrolment record.
@@ -146,7 +146,7 @@ class core_grading_externallib_testcase extends externallib_advanced_testcase {
$DB->insert_record('gradingform_rubric_levels', $rubriclevel2);
// Call the external function.
$cmids = array ($cm->id);
$cmids = array ($cm->cmid);
$areaname = 'submissions';
$result = core_grading_external::get_definitions($cmids, $areaname);
+5 -5
View File
@@ -60,7 +60,7 @@ class core_accesslib_testcase extends advanced_testcase {
$this->assertNotEmpty($ACCESSLIB_PRIVATE->rolepermissions);
$this->assertNotEmpty($ACCESSLIB_PRIVATE->rolepermissions);
$this->assertNotEmpty($ACCESSLIB_PRIVATE->accessdatabyuser);
accesslib_clear_all_caches(true);
accesslib_clear_all_caches_for_unit_testing();
$this->assertEmpty($ACCESSLIB_PRIVATE->rolepermissions);
$this->assertEmpty($ACCESSLIB_PRIVATE->rolepermissions);
$this->assertEmpty($ACCESSLIB_PRIVATE->dirtycontexts);
@@ -2095,7 +2095,7 @@ class core_accesslib_testcase extends advanced_testcase {
unassign_capability('moodle/site:accessallgroups', $allroles['teacher'], $frontpagecontext->id, true);
unset($rc);
accesslib_clear_all_caches(false); // Must be done after assign_capability().
accesslib_clear_all_caches_for_unit_testing(); // Must be done after assign_capability().
// Test role_assign(), role_unassign(), role_unassign_all() functions.
@@ -2112,7 +2112,7 @@ class core_accesslib_testcase extends advanced_testcase {
$this->assertEquals(0, $DB->count_records('role_assignments', array('contextid'=>$context->id)));
unset($context);
accesslib_clear_all_caches(false); // Just in case.
accesslib_clear_all_caches_for_unit_testing(); // Just in case.
// Test has_capability(), get_users_by_capability(), role_switch(), reload_all_capabilities() and friends functions.
@@ -2173,7 +2173,7 @@ class core_accesslib_testcase extends advanced_testcase {
assign_capability('mod/page:view', CAP_PREVENT, $allroles['guest'], $systemcontext, true);
accesslib_clear_all_caches(false); // Must be done after assign_capability().
accesslib_clear_all_caches_for_unit_testing(); /// Must be done after assign_capability().
// Extra tests for guests and not-logged-in users because they can not be verified by cross checking
// with get_users_by_capability() where they are ignored.
@@ -2296,7 +2296,7 @@ class core_accesslib_testcase extends advanced_testcase {
unset($permissions);
unset($roles);
accesslib_clear_all_caches(false); // Must be done after assign_capability().
accesslib_clear_all_caches_for_unit_testing(); // must be done after assign_capability().
// Test time - let's set up some real user, just in case the logic for USER affects the others...
$USER = $DB->get_record('user', array('id'=>$testusers[3]));
+1 -1
View File
@@ -812,7 +812,7 @@ class core_completionlib_testcase extends advanced_testcase {
$this->assertInstanceOf('\core\event\course_module_completion_updated', $event);
$this->assertEquals($forum->cmid, $event->get_record_snapshot('course_modules_completion', $event->objectid)->coursemoduleid);
$this->assertEquals($current, $event->get_record_snapshot('course_modules_completion', $event->objectid));
$this->assertEquals(context_module::instance($forum->id), $event->get_context());
$this->assertEquals(context_module::instance($forum->cmid), $event->get_context());
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals($this->user->id, $event->other['relateduserid']);
$this->assertInstanceOf('moodle_url', $event->get_url());
+3 -3
View File
@@ -93,7 +93,7 @@ class core_messagelib_testcase extends advanced_testcase {
// however mod_quiz doesn't have a data generator.
// Instead we're going to use backup notifications and give and take away the capability at various levels.
$assign = $this->getDataGenerator()->create_module('assign', array('course'=>$course->id));
$modulecontext = context_module::instance($assign->id);
$modulecontext = context_module::instance($assign->cmid);
// Create and enrol a teacher.
$teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'), '*', MUST_EXIST);
@@ -121,7 +121,7 @@ class core_messagelib_testcase extends advanced_testcase {
// They should now be able to see the backup message.
assign_capability('moodle/site:config', CAP_ALLOW, $teacherrole->id, $modulecontext->id, true);
accesslib_clear_all_caches_for_unit_testing();
$modulecontext = context_module::instance($assign->id);
$modulecontext = context_module::instance($assign->cmid);
$this->assertTrue(has_capability('moodle/site:config', $modulecontext));
$providers = message_get_providers_for_user($teacher->id);
@@ -132,7 +132,7 @@ class core_messagelib_testcase extends advanced_testcase {
// They should not be able to see the backup message.
assign_capability('moodle/site:config', CAP_PROHIBIT, $teacherrole->id, $coursecontext->id, true);
accesslib_clear_all_caches_for_unit_testing();
$modulecontext = context_module::instance($assign->id);
$modulecontext = context_module::instance($assign->cmid);
$this->assertFalse(has_capability('moodle/site:config', $modulecontext));
$providers = message_get_providers_for_user($teacher->id);
+2 -2
View File
@@ -66,7 +66,7 @@ class mod_assign_external_testcase extends externallib_advanced_testcase {
// Create a teacher and give them capabilities.
$context = context_course::instance($course->id);
$roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
$context = context_module::instance($assign->id);
$context = context_module::instance($assign->cmid);
$this->assignUserCapability('mod/assign:grade', $context->id, $roleid);
// Create the teacher's enrolment record.
@@ -163,7 +163,7 @@ class mod_assign_external_testcase extends externallib_advanced_testcase {
// Create the user and give them capabilities.
$context = context_course::instance($course1->id);
$roleid = $this->assignUserCapability('moodle/course:view', $context->id);
$context = context_module::instance($assign1->id);
$context = context_module::instance($assign1->cmid);
$this->assignUserCapability('mod/assign:view', $context->id, $roleid);
// Create the user enrolment record.
+3 -3
View File
@@ -65,7 +65,7 @@ class mod_book_events_testcase extends advanced_testcase {
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_book\event\chapter_created', $event);
$this->assertEquals(context_module::instance($book->id), $event->get_context());
$this->assertEquals(context_module::instance($book->cmid), $event->get_context());
$this->assertEquals($chapter->id, $event->objectid);
$expected = array($course->id, 'book', 'add chapter', 'view.php?id='.$book->cmid.'&chapterid='.$chapter->id,
$chapter->id, $book->cmid);
@@ -97,7 +97,7 @@ class mod_book_events_testcase extends advanced_testcase {
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_book\event\chapter_updated', $event);
$this->assertEquals(context_module::instance($book->id), $event->get_context());
$this->assertEquals(context_module::instance($book->cmid), $event->get_context());
$this->assertEquals($chapter->id, $event->objectid);
$expected = array($course->id, 'book', 'update chapter', 'view.php?id='.$book->cmid.'&chapterid='.$chapter->id,
$chapter->id, $book->cmid);
@@ -131,7 +131,7 @@ class mod_book_events_testcase extends advanced_testcase {
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_book\event\chapter_deleted', $event);
$this->assertEquals(context_module::instance($book->id), $event->get_context());
$this->assertEquals(context_module::instance($book->cmid), $event->get_context());
$this->assertEquals($chapter->id, $event->objectid);
$this->assertEquals($chapter, $event->get_record_snapshot('book_chapters', $chapter->id));
$this->assertEventLegacyLogData(array('1', 2, false), $event);
+2 -2
View File
@@ -84,7 +84,7 @@ class mod_choice_events_testcase extends advanced_testcase {
$this->assertCount(1, $events);
$this->assertInstanceOf('\mod_choice\event\answer_submitted', $events[0]);
$this->assertEquals($user->id, $events[0]->userid);
$this->assertEquals(context_module::instance($this->choice->id), $events[0]->get_context());
$this->assertEquals(context_module::instance($this->choice->cmid), $events[0]->get_context());
$this->assertEquals(1, $events[0]->other['choiceid']);
$this->assertEquals(3, $events[0]->other['optionid']);
$expected = array($this->course->id, "choice", "choose", 'view.php?id=' . $this->cm->id, $this->choice->id, $this->cm->id);
@@ -133,7 +133,7 @@ class mod_choice_events_testcase extends advanced_testcase {
$this->assertCount(1, $events);
$this->assertInstanceOf('\mod_choice\event\answer_updated', $events[0]);
$this->assertEquals($user->id, $events[0]->userid);
$this->assertEquals(context_module::instance($this->choice->id), $events[0]->get_context());
$this->assertEquals(context_module::instance($this->choice->cmid), $events[0]->get_context());
$this->assertEquals(1, $events[0]->other['choiceid']);
$this->assertEquals(3, $events[0]->other['optionid']);
$expected = array($this->course->id, "choice", "choose again", 'view.php?id=' . $this->cm->id,
+1 -1
View File
@@ -104,4 +104,4 @@ class data_lib_testcase extends advanced_testcase {
// Make sure the function returns true on a successful deletion.
$this->assertTrue($result);
}
}
}
+1 -1
View File
@@ -283,7 +283,7 @@ class mod_feedback_events_testcase extends advanced_testcase {
// Test legacy data.
$arr = array($this->eventcourse->id, 'feedback', 'submit', 'view.php?id=' . $this->eventcm->id, $this->eventfeedback->id,
$this->eventfeedback->id, $USER->id);
$this->eventcm->id, $USER->id);
$this->assertEventLegacyLogData($arr, $event);
// Test can_view().
+3 -3
View File
@@ -90,7 +90,7 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
$enrol->enrol_user($instance2, $user->id);
// Assign capabilities to view forums for forum 2.
$cm2 = get_coursemodule_from_id('forum', $forum2->id, 0, false, MUST_EXIST);
$cm2 = get_coursemodule_from_id('forum', $forum2->cmid, 0, false, MUST_EXIST);
$context2 = context_module::instance($cm2->id);
$newrole = create_role('Role 2', 'role2', 'Role 2 description');
$roleid2 = $this->assignUserCapability('mod/forum:viewdiscussion', $context2->id, $newrole);
@@ -256,13 +256,13 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
$enrol->enrol_user($instance2, $user1->id);
// Assign capabilities to view discussions for forum 2.
$cm = get_coursemodule_from_id('forum', $forum2->id, 0, false, MUST_EXIST);
$cm = get_coursemodule_from_id('forum', $forum2->cmid, 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
$newrole = create_role('Role 2', 'role2', 'Role 2 description');
$this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
// Assign capabilities to view discussions for forum 3.
$cm = get_coursemodule_from_id('forum', $forum3->id, 0, false, MUST_EXIST);
$cm = get_coursemodule_from_id('forum', $forum3->cmid, 0, false, MUST_EXIST);
$context = context_module::instance($cm->id);
$this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
+1 -1
View File
@@ -36,7 +36,7 @@ class mod_forum_lib_testcase extends advanced_testcase {
$this->setUser($user->id);
$fakepost = (object) array('id' => 123, 'message' => 'Yay!', 'discussion' => 100);
$cm = get_coursemodule_from_instance('forum', $forum->cmid);
$cm = get_coursemodule_from_instance('forum', $forum->id);
$fs = get_file_storage();
$dummy = (object) array(
+2 -2
View File
@@ -246,10 +246,10 @@ class core_repositorylib_testcase extends advanced_testcase {
$forumdata = new stdClass();
$forumdata->course = $course1->id;
$forumc1 = $this->getDataGenerator()->create_module('forum', $forumdata);
$forumc1context = context_module::instance($forumc1->id);
$forumc1context = context_module::instance($forumc1->cmid);
$forumdata->course = $course2->id;
$forumc2 = $this->getDataGenerator()->create_module('forum', $forumdata);
$forumc2context = context_module::instance($forumc2->id);
$forumc2context = context_module::instance($forumc2->cmid);
$blockdata = new stdClass();
$blockdata->parentcontextid = $course1context->id;