Skip to content

Commit

Permalink
MDL-43440 unit testing: Use cmid in unit tests when it should be used.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mr-russ committed Jan 24, 2014
1 parent 780cf0a commit 74b63ea
Show file tree
Hide file tree
Showing 17 changed files with 44 additions and 44 deletions.
2 changes: 1 addition & 1 deletion badges/tests/badgeslib_test.php
Expand Up @@ -233,7 +233,7 @@ public function test_badges_observer_course_module_criteria_review() {
$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);
Expand Down
8 changes: 4 additions & 4 deletions blocks/comments/tests/events_test.php
Expand Up @@ -88,7 +88,7 @@ public function test_comment_created() {
$this->assertEquals($url, $event->get_url());

// Comments when block is on module (wiki) page.
$context = context_module::instance($this->wiki->id);
$context = context_module::instance($this->wiki->cmid);
$args = new stdClass;
$args->context = $context;
$args->course = $this->course;
Expand All @@ -111,7 +111,7 @@ public function test_comment_created() {
// Checking that the event contains the expected values.
$this->assertInstanceOf('\block_comments\event\comment_created', $event);
$this->assertEquals($context, $event->get_context());
$url = new moodle_url('/mod/wiki/view.php', array('id' => $this->wiki->id));
$url = new moodle_url('/mod/wiki/view.php', array('id' => $this->wiki->cmid));
$this->assertEquals($url, $event->get_url());
}

Expand Down Expand Up @@ -152,7 +152,7 @@ public function test_comment_deleted() {
$this->assertEquals($url, $event->get_url());

// Comments when block is on module (wiki) page.
$context = context_module::instance($this->wiki->id);
$context = context_module::instance($this->wiki->cmid);
$args = new stdClass;
$args->context = $context;
$args->course = $this->course;
Expand All @@ -176,7 +176,7 @@ public function test_comment_deleted() {
// Checking that the event contains the expected values.
$this->assertInstanceOf('\block_comments\event\comment_deleted', $event);
$this->assertEquals($context, $event->get_context());
$url = new moodle_url('/mod/wiki/view.php', array('id' => $this->wiki->id));
$url = new moodle_url('/mod/wiki/view.php', array('id' => $this->wiki->cmid));
$this->assertEquals($url, $event->get_url());
}
}
4 changes: 2 additions & 2 deletions files/tests/externallib_test.php
Expand Up @@ -223,7 +223,7 @@ public function test_get_files() {
// 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';
Expand Down Expand Up @@ -301,7 +301,7 @@ public function test_get_files() {
$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);
}
Expand Down
4 changes: 2 additions & 2 deletions grade/tests/externallib_test.php
Expand Up @@ -66,7 +66,7 @@ public function test_get_definitions() {
// 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.
Expand Down Expand Up @@ -146,7 +146,7 @@ public function test_get_definitions() {
$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);

Expand Down
10 changes: 5 additions & 5 deletions lib/tests/accesslib_test.php
Expand Up @@ -60,7 +60,7 @@ public function test_accesslib_clear_all_caches() {
$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);
Expand Down Expand Up @@ -2095,7 +2095,7 @@ public function test_permission_evaluation() {
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.
Expand All @@ -2112,7 +2112,7 @@ public function test_permission_evaluation() {
$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.
Expand Down Expand Up @@ -2173,7 +2173,7 @@ public function test_permission_evaluation() {

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.
Expand Down Expand Up @@ -2296,7 +2296,7 @@ public function test_permission_evaluation() {
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]));
Expand Down
2 changes: 1 addition & 1 deletion lib/tests/completionlib_test.php
Expand Up @@ -812,7 +812,7 @@ public function test_course_module_completion_updated_event() {
$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());
Expand Down
4 changes: 2 additions & 2 deletions mod/assign/submission/comments/tests/events_test.php
Expand Up @@ -73,7 +73,7 @@ public function test_comment_created() {
// Checking that the event contains the expected values.
$this->assertInstanceOf('\assignsubmission_comments\event\comment_created', $event);
$this->assertEquals($context, $event->get_context());
$url = new moodle_url('/mod/assign/view.php', array('id' => $submission->id));
$url = new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id));
$this->assertEquals($url, $event->get_url());
}

Expand Down Expand Up @@ -110,7 +110,7 @@ public function test_comment_deleted() {
// Checking that the event contains the expected values.
$this->assertInstanceOf('\assignsubmission_comments\event\comment_deleted', $event);
$this->assertEquals($context, $event->get_context());
$url = new moodle_url('/mod/assign/view.php', array('id' => $submission->id));
$url = new moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id));
$this->assertEquals($url, $event->get_url());
}
}
2 changes: 1 addition & 1 deletion mod/assign/tests/externallib_test.php
Expand Up @@ -163,7 +163,7 @@ public function test_get_assignments() {
// 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.
Expand Down
6 changes: 3 additions & 3 deletions mod/book/tests/events_test.php
Expand Up @@ -65,7 +65,7 @@ public function test_chapter_created() {

// 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);
Expand Down Expand Up @@ -97,7 +97,7 @@ public function test_chapter_updated() {

// 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);
Expand Down Expand Up @@ -131,7 +131,7 @@ public function test_chapter_deleted() {

// 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);
Expand Down
10 changes: 5 additions & 5 deletions mod/choice/tests/events_test.php
Expand Up @@ -65,7 +65,7 @@ protected function setup() {
$this->course = $this->getDataGenerator()->create_course();
$this->choice = $this->getDataGenerator()->create_module('choice', array('course' => $this->course->id));
$this->cm = $DB->get_record('course_modules', array('id' => $this->choice->cmid));
$this->context = context_module::instance($this->choice->id);
$this->context = context_module::instance($this->choice->cmid);
}

/**
Expand All @@ -84,7 +84,7 @@ public function test_answer_submitted() {
$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);
Expand Down Expand Up @@ -133,7 +133,7 @@ public function test_answer_updated() {
$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,
Expand Down Expand Up @@ -192,7 +192,7 @@ public function test_report_viewed() {
$this->assertCount(1, $event);
$this->assertInstanceOf('\mod_choice\event\report_viewed', $event[0]);
$this->assertEquals($USER->id, $event[0]->userid);
$this->assertEquals(context_module::instance($this->choice->id), $event[0]->get_context());
$this->assertEquals(context_module::instance($this->choice->cmid), $event[0]->get_context());
$expected = array($this->course->id, "choice", "report", 'report.php?id=' . $this->context->instanceid,
$this->choice->id, $this->context->instanceid);
$this->assertEventLegacyLogData($expected, $event[0]);
Expand Down Expand Up @@ -226,7 +226,7 @@ public function test_course_module_viewed() {
$this->assertCount(1, $event);
$this->assertInstanceOf('\mod_choice\event\course_module_viewed', $event[0]);
$this->assertEquals($USER->id, $event[0]->userid);
$this->assertEquals(context_module::instance($this->choice->id), $event[0]->get_context());
$this->assertEquals(context_module::instance($this->choice->cmid), $event[0]->get_context());
$expected = array($this->course->id, "choice", "view", 'view.php?id=' . $this->context->instanceid,
$this->choice->id, $this->context->instanceid);
$this->assertEventLegacyLogData($expected, $event[0]);
Expand Down
4 changes: 2 additions & 2 deletions mod/data/tests/lib_test.php
Expand Up @@ -142,7 +142,7 @@ public function test_data_comment_created_event() {
$contentid = $DB->insert_record('data_content', $datacontent);
$cm = get_coursemodule_from_instance('data', $module->id, $course->id);

$context = context_module::instance($module->id);
$context = context_module::instance($module->cmid);
$cmt = new stdClass();
$cmt->context = $context;
$cmt->course = $course;
Expand Down Expand Up @@ -204,7 +204,7 @@ public function test_data_comment_deleted_event() {
$contentid = $DB->insert_record('data_content', $datacontent);
$cm = get_coursemodule_from_instance('data', $module->id, $course->id);

$context = context_module::instance($module->id);
$context = context_module::instance($module->cmid);
$cmt = new stdClass();
$cmt->context = $context;
$cmt->course = $course;
Expand Down
2 changes: 1 addition & 1 deletion mod/feedback/tests/events_test.php
Expand Up @@ -283,7 +283,7 @@ public function test_response_submitted_event() {

// 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().
Expand Down
6 changes: 3 additions & 3 deletions mod/forum/tests/externallib_test.php
Expand Up @@ -90,7 +90,7 @@ public function test_mod_forum_get_forums_by_courses() {
$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);
Expand Down Expand Up @@ -256,13 +256,13 @@ public function test_mod_forum_get_forum_discussions() {
$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);

Expand Down
2 changes: 1 addition & 1 deletion mod/forum/tests/lib_test.php
Expand Up @@ -36,7 +36,7 @@ public function test_forum_trigger_content_uploaded_event() {

$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(
Expand Down
4 changes: 2 additions & 2 deletions mod/glossary/tests/events_test.php
Expand Up @@ -52,7 +52,7 @@ public function test_comment_created() {

$entry = $glossarygenerator->create_content($glossary);

$context = context_module::instance($glossary->id);
$context = context_module::instance($glossary->cmid);
$cm = get_coursemodule_from_instance('data', $glossary->id, $course->id);
$cmt = new stdClass();
$cmt->component = 'mod_glossary';
Expand Down Expand Up @@ -95,7 +95,7 @@ public function test_comment_deleted() {

$entry = $glossarygenerator->create_content($glossary);

$context = context_module::instance($glossary->id);
$context = context_module::instance($glossary->cmid);
$cm = get_coursemodule_from_instance('data', $glossary->id, $course->id);
$cmt = new stdClass();
$cmt->component = 'mod_glossary';
Expand Down
2 changes: 1 addition & 1 deletion mod/scorm/tests/event_test.php
Expand Up @@ -78,7 +78,7 @@ public function test_attempt_deleted_event() {
$this->assertEquals(4, $event->other['attemptid']);
$this->assertEquals(2, $event->relateduserid);
$expected = array($this->eventcourse->id, 'scorm', 'delete attempts', 'report.php?id=' . $this->eventcm->id,
4, $this->eventscorm->id);
4, $this->eventcm->id);
$this->assertEventLegacyLogData($expected, $events[0]);

// Test event validations.
Expand Down

0 comments on commit 74b63ea

Please sign in to comment.