Exemplo n.º 1
0
 public function test_legacy()
 {
     global $DB;
     $this->resetAfterTest(true);
     $observers = array(array('eventname' => '\\core_tests\\event\\unittest_executed', 'callback' => '\\core_tests\\event\\unittest_observer::observe_one'), array('eventname' => '*', 'callback' => '\\core_tests\\event\\unittest_observer::observe_all', 'includefile' => null, 'internal' => 1, 'priority' => 9999));
     $DB->delete_records('log', array());
     events_update_definition('unittest');
     $DB->delete_records_select('events_handlers', "component <> 'unittest'");
     events_get_handlers('reset');
     $this->assertEquals(3, $DB->count_records('events_handlers'));
     set_config('loglifetime', 60 * 60 * 24 * 5);
     \core\event\manager::phpunit_replace_observers($observers);
     \core_tests\event\unittest_observer::reset();
     $event1 = \core_tests\event\unittest_executed::create(array('courseid' => 1, 'context' => \context_system::instance(), 'other' => array('sample' => 5, 'xx' => 10)));
     $event1->trigger();
     $event2 = \core_tests\event\unittest_executed::create(array('courseid' => 2, 'context' => \context_system::instance(), 'other' => array('sample' => 6, 'xx' => 11)));
     $event2->nest = true;
     $event2->trigger();
     $this->assertSame(array('observe_all-1', 'observe_one-1', 'legacy_handler-1', 'observe_all-nesting-2', 'legacy_handler-3', 'observe_one-2', 'observe_all-3', 'observe_one-3', 'legacy_handler-2'), \core_tests\event\unittest_observer::$info);
     $this->assertSame($event1, \core_tests\event\unittest_observer::$event[0]);
     $this->assertSame($event1, \core_tests\event\unittest_observer::$event[1]);
     $this->assertSame(array(1, 5), \core_tests\event\unittest_observer::$event[2]);
     $logs = $DB->get_records('log', array(), 'id ASC');
     $this->assertCount(3, $logs);
     $log = array_shift($logs);
     $this->assertEquals(1, $log->course);
     $this->assertSame('core_unittest', $log->module);
     $this->assertSame('view', $log->action);
     $log = array_shift($logs);
     $this->assertEquals(2, $log->course);
     $this->assertSame('core_unittest', $log->module);
     $this->assertSame('view', $log->action);
     $log = array_shift($logs);
     $this->assertEquals(3, $log->course);
     $this->assertSame('core_unittest', $log->module);
     $this->assertSame('view', $log->action);
 }
Exemplo n.º 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();
 }