diff --git a/lib/classes/event/base.php b/lib/classes/event/base.php index 531eb593e2d24..6a303f2cd6ec4 100644 --- a/lib/classes/event/base.php +++ b/lib/classes/event/base.php @@ -463,6 +463,9 @@ protected final function validate_before_trigger() { if (!$DB->get_manager()->table_exists($this->data['objecttable'])) { debugging('Unknown table specified in objecttable field', DEBUG_DEVELOPER); } + if (!isset($this->data['objectid'])) { + debugging('Event property objectid must be set when objecttable is defined', DEBUG_DEVELOPER); + } } } } diff --git a/lib/tests/event_test.php b/lib/tests/event_test.php index dcbec81428f5f..6e1093c3bcc7b 100644 --- a/lib/tests/event_test.php +++ b/lib/tests/event_test.php @@ -619,17 +619,21 @@ public function test_bad_events() { @$event->trigger(); $this->assertDebuggingCalled(); - $event = \core_tests\event\bad_event6::create(array('context'=>\context_system::instance())); + $event = \core_tests\event\bad_event6::create(array('objectid'=>1, 'context'=>\context_system::instance())); $event->trigger(); - $this->assertDebuggingCalled(); + $this->assertDebuggingCalled('Unknown table specified in objecttable field'); $event = \core_tests\event\bad_event7::create(array('objectid'=>1, 'context'=>\context_system::instance())); try { $event->trigger(); - $this->fail('Exception expected when $data contains objectid by objecttable not specified'); + $this->fail('Exception expected when $data contains objectid but objecttable not specified'); } catch (\moodle_exception $e) { $this->assertInstanceOf('\coding_exception', $e); } + + $event = \core_tests\event\bad_event8::create(array('context'=>\context_system::instance())); + $event->trigger(); + $this->assertDebuggingCalled('Event property objectid must be set when objecttable is defined'); } public function test_problematic_events() { diff --git a/lib/tests/fixtures/event_fixtures.php b/lib/tests/fixtures/event_fixtures.php index 01275ca9143bb..cce023d38b96f 100644 --- a/lib/tests/fixtures/event_fixtures.php +++ b/lib/tests/fixtures/event_fixtures.php @@ -174,6 +174,14 @@ protected function init() { } } +class bad_event8 extends \core\event\base { + protected function init() { + $this->data['crud'] = 'c'; + $this->data['level'] = self::LEVEL_OTHER; + $this->data['objecttable'] = 'user'; + } +} + class problematic_event1 extends \core\event\base { protected function init() { $this->data['crud'] = 'u';