Permalink
Browse files

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

  • Loading branch information...
1 parent fdc729e commit 623a32e50d64b27d1463e0e1d2ee10211489ef2d Rajesh Taneja committed Jan 21, 2014
Showing with 210 additions and 13 deletions.
  1. +2 −0 blocks/comments/tests/events_test.php
  2. +7 −0 blog/tests/bloglib_test.php
  3. +5 −0 cohort/tests/cohortlib_test.php
  4. +9 −1 course/tests/courselib_test.php
  5. +3 −0 enrol/meta/tests/plugin_test.php
  6. +2 −0 enrol/tests/enrollib_test.php
  7. +28 −0 lib/phpunit/classes/advanced_testcase.php
  8. +23 −0 lib/phpunit/classes/event_mock.php
  9. +2 −0 lib/tests/event_content_viewed_test.php
  10. +1 −0 lib/tests/event_course_module_instance_list_viewed.php
  11. +1 −0 lib/tests/event_course_module_viewed.php
  12. +12 −0 lib/tests/event_test.php
  13. +3 −0 lib/tests/events_test.php
  14. +26 −0 lib/tests/fixtures/event_fixtures.php
  15. +4 −0 lib/tests/moodlelib_test.php
  16. +2 −0 mod/assign/submission/comments/tests/events_test.php
  17. +1 −0 mod/assign/submission/file/tests/events_test.php
  18. +1 −0 mod/assign/submission/onlinetext/tests/events_test.php
  19. +6 −0 mod/book/tests/events_test.php
  20. +1 −0 mod/book/tool/exportimscp/tests/events_test.php
  21. +1 −0 mod/book/tool/importhtml/tests/locallib_test.php
  22. +2 −0 mod/book/tool/print/tests/events_test.php
  23. +3 −0 mod/chat/tests/events_test.php
  24. +9 −9 mod/choice/tests/events_test.php
  25. +2 −0 mod/data/tests/lib_test.php
  26. +3 −1 mod/feedback/tests/events_test.php
  27. +1 −0 mod/folder/tests/events_test.php
  28. +1 −0 mod/forum/tests/lib_test.php
  29. +2 −0 mod/glossary/tests/events_test.php
  30. +6 −0 mod/lesson/tests/events_test.php
  31. +4 −0 mod/quiz/tests/events_test.php
  32. +2 −0 mod/resource/tests/events_test.php
  33. +8 −0 mod/scorm/tests/event_test.php
  34. +15 −0 mod/wiki/tests/events_test.php
  35. +3 −2 notes/tests/events_test.php
  36. +9 −0 webservice/tests/events_test.php
@@ -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);
}
/**
@@ -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);
}
}
@@ -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);
}
/**
@@ -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);
}
/**
@@ -249,6 +251,7 @@ public function test_blog_entry_deleted_event() {
$blog->id);
$this->assertEventLegacyLogData($arr, $event);
$this->assertEventLegacyData($blog, $event);
+ $this->assertEventContextNotUsed($event);
}
@@ -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);
}
/**
@@ -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);
}
/**
@@ -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);
}
/**
@@ -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);
}
}
@@ -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() {
@@ -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() {
@@ -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() {
@@ -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() {
@@ -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() {
@@ -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);
}
/**
@@ -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);
}
/**
@@ -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);
}
/**
@@ -1613,6 +1616,7 @@ public function test_course_content_deleted_event() {
$course->context = $coursecontext;
$course->options = array();
$this->assertEventLegacyData($course, $event);
+ $this->assertEventContextNotUsed($event);
}
/**
@@ -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);
}
/**
@@ -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();
@@ -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() {
@@ -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);
}
@@ -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);
}
/**
@@ -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);
}
/**
@@ -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);
}
/**
@@ -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);
}
}
@@ -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);
}
/**
@@ -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);
}
}
@@ -344,6 +344,34 @@ public function assertEventLegacyLogData($expected, \core\event\base $event, $me
}
/**
+ * 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().
*
* Note: this is called automatically before calling individual test methods.
@@ -37,6 +37,8 @@
/**
* 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 @@ 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;
+ }
}
@@ -75,6 +75,7 @@ public function test_event_attributes() {
$result = $sink->get_events();
$event = $result[1];
$this->assertEventLegacyLogData(null, $event);
+ $this->assertEventContextNotUsed($event);
}
/**
@@ -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);
}
}
@@ -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);
}
@@ -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);
}
View
@@ -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);
+ }
}
@@ -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);
}
/**
@@ -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);
}
/**
@@ -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);
}
}
@@ -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);
+ }
+}
Oops, something went wrong.

0 comments on commit 623a32e

Please sign in to comment.