Пример #1
0
 /**
  * Test custom validations of the event.
  */
 public function test_event_validations()
 {
     // Make sure objecttable and object id is always set.
     try {
         \core_tests\event\course_module_viewed_noinit::create(array('contextid' => 1, 'courseid' => 2, 'objectid' => 3));
     } catch (coding_exception $e) {
         $this->assertContains("course_module_viewed event must define objectid and object table.", $e->getMessage());
     }
     try {
         \core_tests\event\course_module_viewed::create(array('contextid' => 1, 'courseid' => 2));
     } catch (coding_exception $e) {
         $this->assertContains("course_module_viewed event must define objectid and object table.", $e->getMessage());
     }
 }
Пример #2
0
 /**
  * This tests the internal method of \core\event\manager::get_observing_classes.
  *
  * What we are testing is if we can subscribe to a parent event class, instead of only
  * the base event class or the final, implemented event class.  This enables us to subscribe
  * to things like all course module view events, all comment created events, etc.
  */
 public function test_observe_parent_event()
 {
     $this->resetAfterTest();
     // Ensure this has been reset prior to using it.
     \core_tests\event\unittest_observer::reset();
     $course = $this->getDataGenerator()->create_course();
     $feed = $this->getDataGenerator()->create_module('feedback', ['course' => $course->id]);
     $context = context_module::instance($feed->cmid);
     $data = ['context' => $context, 'courseid' => $course->id, 'objectid' => $feed->id];
     // This assertion ensures that basic observe use case did not break.
     \core\event\manager::phpunit_replace_observers([['eventname' => '\\core_tests\\event\\course_module_viewed', 'callback' => ['\\core_tests\\event\\unittest_observer', 'observe_all_alt']]]);
     $pageevent = \core_tests\event\course_module_viewed::create($data);
     $pageevent->trigger();
     $this->assertSame(['observe_all_alt'], \core_tests\event\unittest_observer::$info, 'Error observing triggered event');
     \core_tests\event\unittest_observer::reset();
     // This assertion tests that we can observe an abstract (parent) class instead of the implemented class.
     \core\event\manager::phpunit_replace_observers([['eventname' => '\\core\\event\\course_module_viewed', 'callback' => ['\\core_tests\\event\\unittest_observer', 'observe_all_alt']]]);
     $pageevent = \core_tests\event\course_module_viewed::create($data);
     $pageevent->trigger();
     $this->assertSame(['observe_all_alt'], \core_tests\event\unittest_observer::$info, 'Error observing parent class event');
     \core_tests\event\unittest_observer::reset();
 }