Commit 623a32e5 authored by Rajesh Taneja's avatar Rajesh Taneja
Browse files

MDL-43736 Events: Updated unit test to test context not used in event

parent fdc729ea
......@@ -113,6 +113,7 @@ class block_comments_events_testcase extends advanced_testcase {
$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);
}
/**
......@@ -178,5 +179,6 @@ class block_comments_events_testcase extends advanced_testcase {
$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);
}
}
......@@ -183,6 +183,7 @@ class core_bloglib_testcase extends advanced_testcase {
$this->assertEventLegacyLogData($arr, $event);
$this->assertEquals("blog_entry_added", $event->get_legacy_eventname());
$this->assertEventLegacyData($blog, $event);
$this->assertEventContextNotUsed($event);
}
/**
......@@ -215,6 +216,7 @@ class core_bloglib_testcase extends advanced_testcase {
$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);
}
/**
......@@ -249,6 +251,7 @@ class core_bloglib_testcase extends advanced_testcase {
$blog->id);
$this->assertEventLegacyLogData($arr, $event);
$this->assertEventLegacyData($blog, $event);
$this->assertEventContextNotUsed($event);
}
......@@ -301,6 +304,7 @@ class core_bloglib_testcase extends advanced_testcase {
$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);
}
/**
......@@ -381,6 +385,7 @@ class core_bloglib_testcase extends advanced_testcase {
$this->assertEquals($url, $event->get_url());
$arr = array(SITEID, 'blog', 'view', $url2->out(), 'view blog entry');
$this->assertEventLegacyLogData($arr, $event);
$this->assertEventContextNotUsed($event);
}
/**
......@@ -416,6 +421,7 @@ class core_bloglib_testcase extends advanced_testcase {
$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);
}
/**
......@@ -452,6 +458,7 @@ class core_bloglib_testcase extends advanced_testcase {
$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);
}
}
......@@ -106,6 +106,7 @@ class core_cohort_cohortlib_testcase extends advanced_testcase {
$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() {
......@@ -176,6 +177,7 @@ class core_cohort_cohortlib_testcase extends advanced_testcase {
$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() {
......@@ -213,6 +215,7 @@ class core_cohort_cohortlib_testcase extends advanced_testcase {
$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() {
......@@ -270,6 +273,7 @@ class core_cohort_cohortlib_testcase extends advanced_testcase {
$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() {
......@@ -313,6 +317,7 @@ class core_cohort_cohortlib_testcase extends advanced_testcase {
$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() {
......
......@@ -1449,6 +1449,7 @@ class core_course_courselib_testcase extends advanced_testcase {
// 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);
}
/**
......@@ -1527,6 +1528,7 @@ class core_course_courselib_testcase extends advanced_testcase {
$this->assertEventLegacyData($movedcoursehidden, $event);
$expectedlog = array($movedcoursehidden->id, 'course', 'move', 'edit.php?id=' . $movedcoursehidden->id, $movedcoursehidden->id);
$this->assertEventLegacyLogData($expectedlog, $event);
$this->assertEventContextNotUsed($event);
}
/**
......@@ -1572,6 +1574,7 @@ class core_course_courselib_testcase extends advanced_testcase {
$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);
}
/**
......@@ -1613,6 +1616,7 @@ class core_course_courselib_testcase extends advanced_testcase {
$course->context = $coursecontext;
$course->options = array();
$this->assertEventLegacyData($course, $event);
$this->assertEventContextNotUsed($event);
}
/**
......@@ -1675,6 +1679,7 @@ class core_course_courselib_testcase extends advanced_testcase {
$this->assertEventLegacyData($category2, $event);
$expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category2->name . '(ID ' . $category2->id . ')');
$this->assertEventLegacyLogData($expectedlog, $event);
$this->assertEventContextNotUsed($event);
}
/**
......@@ -1740,6 +1745,7 @@ class core_course_courselib_testcase extends advanced_testcase {
'samesite' => $rc->is_samesite()
);
$this->assertEventLegacyData($legacydata, $event);
$this->assertEventContextNotUsed($event);
// Destroy the resource controller since we are done using it.
$rc->destroy();
......@@ -1794,6 +1800,7 @@ class core_course_courselib_testcase extends advanced_testcase {
$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() {
......@@ -1953,6 +1960,7 @@ class core_course_courselib_testcase extends advanced_testcase {
$arr = array($cm->course, "course", "add mod", "../mod/assign/view.php?id=$mod->id", "assign $cm->instance");
$this->assertEventLegacyLogData($arr, $event);
$this->assertEventContextNotUsed($event);
}
......@@ -2058,7 +2066,7 @@ class core_course_courselib_testcase extends advanced_testcase {
$arr = array($cm->course, "course", "update mod", "../mod/forum/view.php?id=$mod->id", "forum $cm->instance");
$this->assertEventLegacyLogData($arr, $event);
$this->assertEventContextNotUsed($event);
}
/**
......
......@@ -474,6 +474,7 @@ class enrol_meta_plugin_testcase extends advanced_testcase {
$expectedlegacyeventdata->enrol = 'meta';
$expectedlegacyeventdata->courseid = $course2->id;
$this->assertEventLegacyData($expectedlegacyeventdata, $event);
$this->assertEventContextNotUsed($event);
}
/**
......@@ -507,6 +508,7 @@ class enrol_meta_plugin_testcase extends advanced_testcase {
$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);
}
/**
......@@ -546,5 +548,6 @@ class enrol_meta_plugin_testcase extends advanced_testcase {
$expectedlegacyeventdata->enrol = 'meta';
$expectedlegacyeventdata->courseid = $course2->id;
$this->assertEventLegacyData($expectedlegacyeventdata, $event);
$this->assertEventContextNotUsed($event);
}
}
......@@ -318,6 +318,7 @@ class core_enrollib_testcase extends advanced_testcase {
$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);
}
/**
......@@ -359,5 +360,6 @@ class core_enrollib_testcase extends advanced_testcase {
$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);
}
}
......@@ -343,6 +343,34 @@ abstract class advanced_testcase extends PHPUnit_Framework_TestCase {
$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().
*
......
......@@ -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) {
......@@ -45,10 +47,31 @@ 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 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;
}
}
......@@ -75,6 +75,7 @@ class core_event_content_viewed_testcase extends advanced_testcase {
$result = $sink->get_events();
$event = $result[1];
$this->assertEventLegacyLogData(null, $event);
$this->assertEventContextNotUsed($event);
}
/**
......@@ -89,6 +90,7 @@ class core_event_content_viewed_testcase extends advanced_testcase {
$pageevent = \core_tests\event\content_viewed::create();
$pageevent->set_page_detail();
$pageevent->trigger();
$this->assertEventContextNotUsed($pageevent);
}
}
......@@ -57,6 +57,7 @@ class core_event_course_module_instance_list_viewed_testcase extends advanced_te
$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);
}
......
......@@ -64,6 +64,7 @@ class core_event_course_module_viewed_testcase extends advanced_testcase {
$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);
}
......
......@@ -749,4 +749,16 @@ class core_event_testcase extends advanced_testcase {
$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);
}
}
......@@ -51,6 +51,7 @@ class core_events_testcase extends advanced_testcase {
$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);
}
/**
......@@ -139,6 +140,7 @@ class core_events_testcase extends advanced_testcase {
$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);
}
/**
......@@ -170,5 +172,6 @@ class core_events_testcase extends advanced_testcase {
$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);
}
}
......@@ -258,3 +258,29 @@ class course_module_viewed extends \core\event\course_module_viewed {
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);
}
}
......@@ -1889,6 +1889,7 @@ class core_moodlelib_testcase extends advanced_testcase {
$this->assertSame($eventdata['other']['picture'], $user->picture);
$this->assertSame($eventdata['other']['mnethostid'], $user->mnethostid);
$this->assertEquals($user, $event->get_record_snapshot('user', $event->objectid));
$this->assertEventContextNotUsed($event);
// Try invalid params.
$record = new stdClass();
......@@ -2459,6 +2460,7 @@ class core_moodlelib_testcase extends advanced_testcase {
$this->assertEquals('user', $event->objecttable);
$this->assertEquals($user->id, $event->objectid);
$this->assertEquals(context_system::instance()->id, $event->contextid);
$this->assertEventContextNotUsed($event);
$user = $DB->get_record('user', array('id'=>$user->id));
......@@ -2504,6 +2506,7 @@ class core_moodlelib_testcase extends advanced_testcase {
$expectedlogdata = array(SITEID, 'user', 'logout', 'view.php?id='.$event->objectid.'&course='.SITEID, $event->objectid, 0,
$event->objectid);
$this->assertEventLegacyLogData($expectedlogdata, $event);
$this->assertEventContextNotUsed($event);
}
public function test_email_to_user() {
......@@ -2586,6 +2589,7 @@ class core_moodlelib_testcase extends advanced_testcase {
$this->assertEquals(context_user::instance($user->id), $event->get_context());
$expectedlogdata = array(SITEID, 'user', 'update', 'view.php?id='.$user->id, '');
$this->assertEventLegacyLogData($expectedlogdata, $event);
$this->assertEventContextNotUsed($event);
}
}
......
......@@ -75,6 +75,7 @@ class assignsubmission_comments_events_testcase extends mod_assign_base_testcase
$this->assertEquals($context, $event->get_context());
$url = new moodle_url('/mod/assign/view.php', array('id' => $submission->id));
$this->assertEquals($url, $event->get_url());
$this->assertEventContextNotUsed($event);
}
/**
......@@ -112,5 +113,6 @@ class assignsubmission_comments_events_testcase extends mod_assign_base_testcase
$this->assertEquals($context, $event->get_context());
$url = new moodle_url('/mod/assign/view.php', array('id' => $submission->id));
$this->assertEquals($url, $event->get_url());
$this->assertEventContextNotUsed($event);
}
}
......@@ -90,6 +90,7 @@ class assignsubmission_file_events_testcase extends advanced_testcase {
$expected->files = $files;
$expected->pathnamehashes = array($fi->get_pathnamehash(), $fi2->get_pathnamehash());
$this->assertEventLegacyData($expected, $event);
$this->assertEventContextNotUsed($event);
}
}
......@@ -70,6 +70,7 @@ class assignsubmission_onlinetext_events_testcase extends advanced_testcase {
$expected->userid = $user->id;
$expected->content = 'Submission text';
$this->assertEventLegacyData($expected, $event);
$this->assertEventContextNotUsed($event);
}
}
......@@ -70,6 +70,7 @@ class mod_book_events_testcase extends advanced_testcase {
$expected = array($course->id, 'book', 'add chapter', 'view.php?id='.$book->cmid.'&chapterid='.$chapter->id,
$chapter->id, $book->cmid);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}
public function test_chapter_updated() {
......@@ -102,6 +103,7 @@ class mod_book_events_testcase extends advanced_testcase {
$expected = array($course->id, 'book', 'update chapter', 'view.php?id='.$book->cmid.'&chapterid='.$chapter->id,
$chapter->id, $book->cmid);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}
public function test_chapter_deleted() {
......@@ -135,6 +137,7 @@ class mod_book_events_testcase extends advanced_testcase {
$this->assertEquals($chapter->id, $event->objectid);
$this->assertEquals($chapter, $event->get_record_snapshot('book_chapters', $chapter->id));
$this->assertEventLegacyLogData(array('1', 2, false), $event);
$this->assertEventContextNotUsed($event);
}
public function test_course_module_instance_list_viewed() {
......@@ -159,6 +162,7 @@ class mod_book_events_testcase extends advanced_testcase {
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$expected = array($course->id, 'book', 'view all', 'index.php?id='.$course->id, '');
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}
public function test_course_module_viewed() {
......@@ -187,6 +191,7 @@ class mod_book_events_testcase extends advanced_testcase {
$this->assertEquals($book->id, $event->objectid);
$expected = array($course->id, 'book', 'view', 'view.php?id=' . $book->cmid, $book->id, $book->cmid);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}
public function test_chapter_viewed() {
......@@ -219,6 +224,7 @@ class mod_book_events_testcase extends advanced_testcase {
$expected = array($course->id, 'book', 'view chapter', 'view.php?id=' . $book->cmid . '&chapterid=' .
$chapter->id, $chapter->id, $book->cmid);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}
}
......@@ -67,6 +67,7 @@ class booktool_exportimscp_events_testcase extends advanced_testcase {
$expected = array($course->id, 'book', 'exportimscp', 'tool/exportimscp/index.php?id=' . $book->cmid,
$book->id, $book->cmid);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment