Skip to content

Commit

Permalink
MDL-43736 Events: Updated unit test to test context not used in event
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajesh Taneja committed Jan 29, 2014
1 parent fdc729e commit 623a32e
Show file tree
Hide file tree
Showing 36 changed files with 210 additions and 13 deletions.
2 changes: 2 additions & 0 deletions blocks/comments/tests/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public function test_comment_created() {
$this->assertEquals($context, $event->get_context());
$url = new moodle_url('/mod/wiki/view.php', array('id' => $this->wiki->id));
$this->assertEquals($url, $event->get_url());
$this->assertEventContextNotUsed($event);
}

/**
Expand Down Expand Up @@ -178,5 +179,6 @@ public function test_comment_deleted() {
$this->assertEquals($context, $event->get_context());
$url = new moodle_url('/mod/wiki/view.php', array('id' => $this->wiki->id));
$this->assertEquals($url, $event->get_url());
$this->assertEventContextNotUsed($event);
}
}
7 changes: 7 additions & 0 deletions blog/tests/bloglib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public function test_blog_entry_created_event() {
$this->assertEventLegacyLogData($arr, $event);
$this->assertEquals("blog_entry_added", $event->get_legacy_eventname());
$this->assertEventLegacyData($blog, $event);
$this->assertEventContextNotUsed($event);
}

/**
Expand Down Expand Up @@ -215,6 +216,7 @@ public function test_blog_entry_updated_event() {
$this->assertEventLegacyData($blog, $event);
$arr = array (SITEID, 'blog', 'update', 'index.php?userid=' . $this->userid . '&entryid=' . $blog->id, $blog->subject);
$this->assertEventLegacyLogData($arr, $event);
$this->assertEventContextNotUsed($event);
}

/**
Expand Down Expand Up @@ -249,6 +251,7 @@ public function test_blog_entry_deleted_event() {
$blog->id);
$this->assertEventLegacyLogData($arr, $event);
$this->assertEventLegacyData($blog, $event);
$this->assertEventContextNotUsed($event);
}


Expand Down Expand Up @@ -301,6 +304,7 @@ public function test_blog_association_created_event() {
$arr = array(SITEID, 'blog', 'add association', 'index.php?userid=' . $this->userid . '&entryid=' . $blog->id,
$blog->subject, $this->cmid, $this->userid);
$this->assertEventLegacyLogData($arr, $event);
$this->assertEventContextNotUsed($event);
}

/**
Expand Down Expand Up @@ -381,6 +385,7 @@ public function test_blog_entries_viewed_event() {
$this->assertEquals($url, $event->get_url());
$arr = array(SITEID, 'blog', 'view', $url2->out(), 'view blog entry');
$this->assertEventLegacyLogData($arr, $event);
$this->assertEventContextNotUsed($event);
}

/**
Expand Down Expand Up @@ -416,6 +421,7 @@ public function test_blog_comment_created_event() {
$this->assertEquals($this->postid, $event->other['itemid']);
$url = new moodle_url('/blog/index.php', array('entryid' => $this->postid));
$this->assertEquals($url, $event->get_url());
$this->assertEventContextNotUsed($event);
}

/**
Expand Down Expand Up @@ -452,6 +458,7 @@ public function test_blog_comment_deleted_event() {
$this->assertEquals($this->postid, $event->other['itemid']);
$url = new moodle_url('/blog/index.php', array('entryid' => $this->postid));
$this->assertEquals($url, $event->get_url());
$this->assertEventContextNotUsed($event);
}
}

5 changes: 5 additions & 0 deletions cohort/tests/cohortlib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public function test_cohort_add_cohort_event() {
$this->assertEquals($cohort->contextid, $event->contextid);
$this->assertEquals($cohort, $event->get_record_snapshot('cohort', $id));
$this->assertEventLegacyData($cohort, $event);
$this->assertEventContextNotUsed($event);
}

public function test_cohort_update_cohort() {
Expand Down Expand Up @@ -176,6 +177,7 @@ public function test_cohort_update_cohort_event() {
$this->assertEquals($updatedcohort->contextid, $event->contextid);
$this->assertEquals($cohort, $event->get_record_snapshot('cohort', $id));
$this->assertEventLegacyData($cohort, $event);
$this->assertEventContextNotUsed($event);
}

public function test_cohort_delete_cohort() {
Expand Down Expand Up @@ -213,6 +215,7 @@ public function test_cohort_delete_cohort_event() {
$this->assertEquals($cohort->id, $event->objectid);
$this->assertEquals($cohort, $event->get_record_snapshot('cohort', $cohort->id));
$this->assertEventLegacyData($cohort, $event);
$this->assertEventContextNotUsed($event);
}

public function test_cohort_delete_category() {
Expand Down Expand Up @@ -270,6 +273,7 @@ public function test_cohort_add_member_event() {
$this->assertEquals($user->id, $event->relateduserid);
$this->assertEquals($USER->id, $event->userid);
$this->assertEventLegacyData((object) array('cohortid' => $cohort->id, 'userid' => $user->id), $event);
$this->assertEventContextNotUsed($event);
}

public function test_cohort_remove_member() {
Expand Down Expand Up @@ -313,6 +317,7 @@ public function test_cohort_remove_member_event() {
$this->assertEquals($user->id, $event->relateduserid);
$this->assertEquals($USER->id, $event->userid);
$this->assertEventLegacyData((object) array('cohortid' => $cohort->id, 'userid' => $user->id), $event);
$this->assertEventContextNotUsed($event);
}

public function test_cohort_is_member() {
Expand Down
10 changes: 9 additions & 1 deletion course/tests/courselib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,7 @@ public function test_course_created_event() {
// as they have already been validated in the previous steps. Here we only want to make sure that when the
// imsenterprise plugin creates a course an event is triggered.
$this->assertInstanceOf('\core\event\course_created', $event);
$this->assertEventContextNotUsed($event);
}

/**
Expand Down Expand Up @@ -1527,6 +1528,7 @@ public function test_course_updated_event() {
$this->assertEventLegacyData($movedcoursehidden, $event);
$expectedlog = array($movedcoursehidden->id, 'course', 'move', 'edit.php?id=' . $movedcoursehidden->id, $movedcoursehidden->id);
$this->assertEventLegacyLogData($expectedlog, $event);
$this->assertEventContextNotUsed($event);
}

/**
Expand Down Expand Up @@ -1572,6 +1574,7 @@ public function test_course_deleted_event() {
$this->assertEventLegacyData($course, $event);
$expectedlog = array(SITEID, 'course', 'delete', 'view.php?id=' . $course->id, $course->fullname . '(ID ' . $course->id . ')');
$this->assertEventLegacyLogData($expectedlog, $event);
$this->assertEventContextNotUsed($event);
}

/**
Expand Down Expand Up @@ -1613,6 +1616,7 @@ public function test_course_content_deleted_event() {
$course->context = $coursecontext;
$course->options = array();
$this->assertEventLegacyData($course, $event);
$this->assertEventContextNotUsed($event);
}

/**
Expand Down Expand Up @@ -1675,6 +1679,7 @@ public function test_course_category_deleted_event() {
$this->assertEventLegacyData($category2, $event);
$expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category2->name . '(ID ' . $category2->id . ')');
$this->assertEventLegacyLogData($expectedlog, $event);
$this->assertEventContextNotUsed($event);
}

/**
Expand Down Expand Up @@ -1740,6 +1745,7 @@ public function test_course_restored_event() {
'samesite' => $rc->is_samesite()
);
$this->assertEventLegacyData($legacydata, $event);
$this->assertEventContextNotUsed($event);

// Destroy the resource controller since we are done using it.
$rc->destroy();
Expand Down Expand Up @@ -1794,6 +1800,7 @@ public function test_course_section_updated_event() {
$sectionnum = $section->section;
$expectedlegacydata = array($course->id, "course", "editsection", 'editsection.php?id=' . $id, $sectionnum);
$this->assertEventLegacyLogData($expectedlegacydata, $event);
$this->assertEventContextNotUsed($event);
}

public function test_course_integrity_check() {
Expand Down Expand Up @@ -1953,6 +1960,7 @@ public function test_course_module_created_event() {

$arr = array($cm->course, "course", "add mod", "../mod/assign/view.php?id=$mod->id", "assign $cm->instance");
$this->assertEventLegacyLogData($arr, $event);
$this->assertEventContextNotUsed($event);

}

Expand Down Expand Up @@ -2058,7 +2066,7 @@ public function test_course_module_updated_event() {

$arr = array($cm->course, "course", "update mod", "../mod/forum/view.php?id=$mod->id", "forum $cm->instance");
$this->assertEventLegacyLogData($arr, $event);

$this->assertEventContextNotUsed($event);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions enrol/meta/tests/plugin_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ public function test_user_enrolment_created_observer() {
$expectedlegacyeventdata->enrol = 'meta';
$expectedlegacyeventdata->courseid = $course2->id;
$this->assertEventLegacyData($expectedlegacyeventdata, $event);
$this->assertEventContextNotUsed($event);
}

/**
Expand Down Expand Up @@ -507,6 +508,7 @@ public function test_user_enrolment_deleted_observer() {
$this->assertEquals(0, $DB->count_records('user_enrolments'));
$this->assertInstanceOf('\core\event\user_enrolment_deleted', $event);
$this->assertEquals('user_unenrolled', $event->get_legacy_eventname());
$this->assertEventContextNotUsed($event);
}

/**
Expand Down Expand Up @@ -546,5 +548,6 @@ public function test_user_enrolment_updated_observer() {
$expectedlegacyeventdata->enrol = 'meta';
$expectedlegacyeventdata->courseid = $course2->id;
$this->assertEventLegacyData($expectedlegacyeventdata, $event);
$this->assertEventContextNotUsed($event);
}
}
2 changes: 2 additions & 0 deletions enrol/tests/enrollib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ public function test_user_enrolment_created_event() {
$this->assertEventLegacyData($expectedlegacyeventdata, $event);
$expected = array($course1->id, 'course', 'enrol', '../enrol/users.php?id=' . $course1->id, $course1->id);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}

/**
Expand Down Expand Up @@ -359,5 +360,6 @@ public function test_user_enrolment_deleted_event() {
$this->assertEventLegacyData($expectedlegacyeventdata, $event);
$expected = array($course->id, 'course', 'unenrol', '../enrol/users.php?id=' . $course->id, $course->id);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}
}
28 changes: 28 additions & 0 deletions lib/phpunit/classes/advanced_testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,34 @@ public function assertEventLegacyLogData($expected, \core\event\base $event, $me
$this->assertEquals($expected, $legacydata, $message);
}

/**
* Assert that an event is not using event->contxet.
* While restoring context might not be valid and it should not be used by event url
* or description methods.
*
* @param \core\event\base $event the event object.
* @param string $message
* @return void
*/
public function assertEventContextNotUsed(\core\event\base $event, $message = '') {
// Save current event->context and set it to false.
$eventcontext = phpunit_event_mock::testable_get_event_context($event);
phpunit_event_mock::testable_set_event_context($event, false);
if ($message === '') {
$message = 'Event should not use context property of event in any method.';
}

// Test event methods should not use event->context.
$event->get_url();
$event->get_description();
$event->get_legacy_eventname();
phpunit_event_mock::testable_get_legacy_eventdata($event);
phpunit_event_mock::testable_get_legacy_logdata($event);

// Restore event->context.
phpunit_event_mock::testable_set_event_context($event, $eventcontext);
}

/**
* Stores current time as the base for assertTimeCurrent().
*
Expand Down
23 changes: 23 additions & 0 deletions lib/phpunit/classes/event_mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ abstract class phpunit_event_mock extends \core\event\base {

/**
* Returns the log data of the event.
*
* @param \core\event\base $event event to get legacy eventdata from.
* @return array
*/
public static function testable_get_legacy_eventdata($event) {
Expand All @@ -45,10 +47,31 @@ public static function testable_get_legacy_eventdata($event) {

/**
* Returns the log data of the event.
*
* @param \core\event\base $event event to get legacy logdata from.
* @return array
*/
public static function testable_get_legacy_logdata($event) {
return $event->get_legacy_logdata();
}

/**
* Returns event context.
*
* @param \core\event\base $event event to get context for.
* @return context event context
*/
public static function testable_get_event_context($event) {
return $event->context;
}

/**
* Sets event context.
*
* @param \core\event\base $event event to set context for.
* @param context $context context to set.
*/
public static function testable_set_event_context($event, $context) {
$event->context = $context;
}
}
2 changes: 2 additions & 0 deletions lib/tests/event_content_viewed_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function test_event_attributes() {
$result = $sink->get_events();
$event = $result[1];
$this->assertEventLegacyLogData(null, $event);
$this->assertEventContextNotUsed($event);
}

/**
Expand All @@ -89,6 +90,7 @@ public function test_event_context_exception() {
$pageevent = \core_tests\event\content_viewed::create();
$pageevent->set_page_detail();
$pageevent->trigger();
$this->assertEventContextNotUsed($pageevent);
}
}

1 change: 1 addition & 0 deletions lib/tests/event_course_module_instance_list_viewed.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function test_event_attributes() {
$this->assertEventLegacyLogData($legacydata, $event);
$url = new moodle_url('/mod/unittests/index.php', array('id' => $course->id));
$this->assertEquals($url, $event->get_url());
$this->assertEventContextNotUsed($event);

}

Expand Down
1 change: 1 addition & 0 deletions lib/tests/event_course_module_viewed.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function test_event_attributes() {
$this->assertSame('feedback', $event->objecttable);
$url = new moodle_url('/mod/feedback/view.php', array('id' => $cm->id));
$this->assertEquals($url, $event->get_url());
$this->assertEventContextNotUsed($event);

}

Expand Down
12 changes: 12 additions & 0 deletions lib/tests/event_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -749,4 +749,16 @@ public function test_iteration() {

$this->assertSame($event->get_data(), $data);
}

/**
* @expectedException PHPUnit_Framework_Error_Notice
*/
public function test_context_not_used() {
$event = \core_tests\event\context_used_in_event::create(array('courseid' => 1, 'other' => array('sample' => 1, 'xx' => 10)));
$this->assertEventContextNotUsed($event);

$eventcontext = phpunit_event_mock::testable_get_event_context($event);
phpunit_event_mock::testable_set_event_context($event, null);
$this->assertEventContextNotUsed($event);
}
}
3 changes: 3 additions & 0 deletions lib/tests/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function test_course_category_created() {
$this->assertEquals(context_coursecat::instance($category->id), $event->get_context());
$expected = array(SITEID, 'category', 'add', 'editcategory.php?id=' . $category->id, $category->id);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}

/**
Expand Down Expand Up @@ -139,6 +140,7 @@ public function test_course_category_updated() {
$this->assertEquals(context_coursecat::instance($category2->id), $event->get_context());
$expected = array(SITEID, 'category', 'show', 'editcategory.php?id=' . $category2->id, $category2->id);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}

/**
Expand Down Expand Up @@ -170,5 +172,6 @@ public function test_email_failed() {
$this->assertEquals(context_system::instance(), $event->get_context());
$expected = array(SITEID, 'library', 'mailer', qualified_me(), 'ERROR: The email failed to send!');
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}
}
26 changes: 26 additions & 0 deletions lib/tests/fixtures/event_fixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,29 @@ protected function init() {
class course_module_viewed_noinit extends \core\event\course_module_viewed {
}

/**
* Event to test context used in event functions
*/
class context_used_in_event extends \core\event\base {
public function get_description() {
return $this->context->instanceid . " Description";
}

protected function init() {
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_PARTICIPATING;
$this->context = \context_system::instance();
}

public function get_url() {
return new \moodle_url('/somepath/somefile.php', array('id' => $this->context->instanceid));
}

protected function get_legacy_eventdata() {
return array($this->data['courseid'], $this->context->instanceid);
}

protected function get_legacy_logdata() {
return array($this->data['courseid'], 'core_unittest', 'view', 'unittest.php?id=' . $this->context->instanceid);
}
}
Loading

0 comments on commit 623a32e

Please sign in to comment.