/** * Trigger event. */ public final function trigger() { global $CFG; if ($this->restored) { throw new \coding_exception('Can not trigger restored event'); } if ($this->triggered or $this->dispatched) { throw new \coding_exception('Can not trigger event twice'); } $this->validate_before_trigger(); $this->triggered = true; if (isset($CFG->loglifetime) and $CFG->loglifetime != -1) { if ($data = $this->get_legacy_logdata()) { call_user_func_array('add_to_log', $data); } } if (PHPUNIT_TEST and \phpunit_util::is_redirecting_events()) { $this->dispatched = true; \phpunit_util::event_triggered($this); return; } \core\event\manager::dispatch($this); $this->dispatched = true; if ($legacyeventname = static::get_legacy_eventname()) { events_trigger_legacy($legacyeventname, $this->get_legacy_eventdata()); } }
/** * Trigger event. */ public final function trigger() { global $CFG; if ($this->restored) { throw new \coding_exception('Can not trigger restored event'); } if ($this->triggered or $this->dispatched) { throw new \coding_exception('Can not trigger event twice'); } $this->validate_before_trigger(); $this->triggered = true; if (isset($CFG->loglifetime) and $CFG->loglifetime != -1) { if ($data = $this->get_legacy_logdata()) { $manager = get_log_manager(); if (method_exists($manager, 'legacy_add_to_log')) { if (is_array($data[0])) { // Some events require several entries in 'log' table. foreach ($data as $d) { call_user_func_array(array($manager, 'legacy_add_to_log'), $d); } } else { call_user_func_array(array($manager, 'legacy_add_to_log'), $data); } } } } if (PHPUNIT_TEST and \phpunit_util::is_redirecting_events()) { $this->dispatched = true; \phpunit_util::event_triggered($this); return; } \core\event\manager::dispatch($this); $this->dispatched = true; if ($legacyeventname = static::get_legacy_eventname()) { events_trigger_legacy($legacyeventname, $this->get_legacy_eventdata()); } }
public function test_trigger_problems() { $event = \core_tests\event\unittest_executed::create(array('courseid' => 1, 'context' => \context_system::instance(), 'other' => array('sample' => 5, 'xx' => 10))); $event->trigger(); try { $event->trigger(); $this->fail('Exception expected on double trigger'); } catch (\moodle_exception $e) { $this->assertInstanceOf('coding_exception', $e); } $data = $event->get_data(); $restored = \core_tests\event\unittest_executed::restore($data, array()); $this->assertTrue($restored->is_triggered()); $this->assertTrue($restored->is_restored()); try { $restored->trigger(); $this->fail('Exception expected on triggering of restored event'); } catch (\moodle_exception $e) { $this->assertInstanceOf('coding_exception', $e); } $event = \core_tests\event\unittest_executed::create(array('courseid' => 1, 'context' => \context_system::instance(), 'other' => array('sample' => 5, 'xx' => 10))); try { \core\event\manager::dispatch($event); $this->fail('Exception expected on manual event dispatching'); } catch (\moodle_exception $e) { $this->assertInstanceOf('coding_exception', $e); } }