From a85258cac820c888caf42ba00a2d25425b22b8ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Sat, 13 Jul 2013 11:20:45 +0200 Subject: [PATCH] MDL-39846 introduce new objecttable property --- lib/classes/event/base.php | 18 +++++++++++++++--- lib/classes/event/role_assigned.php | 1 + lib/classes/event/role_unassigned.php | 1 + lib/tests/event_test.php | 12 ++++++++++++ lib/tests/fixtures/event_fixtures.php | 16 ++++++++++++++++ 5 files changed, 45 insertions(+), 3 deletions(-) diff --git a/lib/classes/event/base.php b/lib/classes/event/base.php index 22036375bba..4e272c78ae8 100644 --- a/lib/classes/event/base.php +++ b/lib/classes/event/base.php @@ -36,6 +36,7 @@ namespace core\event; * @property-read string $component Full frankenstyle component name * @property-read string $action what happened * @property-read string $object what/who was object of the action (usually similar to database table name) + * @property-read string $objecttable name of database table where is object record stored * @property-read int $objectid optional id of the object * @property-read string $crud letter indicating event type * @property-read int $level log level (number between 1 and 100) @@ -78,7 +79,7 @@ abstract class base { /** @var array list of event properties */ private static $fields = array( - 'eventname', 'component', 'action', 'object', 'objectid', 'crud', 'level', 'contextid', + 'eventname', 'component', 'action', 'object', 'objecttable', 'objectid', 'crud', 'level', 'contextid', 'contextlevel', 'contextinstanceid', 'userid', 'courseid', 'relateduserid', 'other', 'timecreated'); @@ -177,7 +178,7 @@ abstract class base { // Warn developers if they do something wrong. if (debugging('', DEBUG_DEVELOPER)) { static $automatickeys = array('eventname', 'component', 'action', 'object', 'timecreated'); - static $initkeys = array('crud', 'level'); + static $initkeys = array('crud', 'level', 'objecttable'); foreach ($data as $key => $ignored) { if ($key === 'context') { @@ -204,6 +205,7 @@ abstract class base { * Set all required data properties: * 1/ crud - letter [crud] * 2/ level - number 1...100 + * 3/ objecttable - name of database table if objectid specified * * TODO: MDL-37658 * @@ -377,14 +379,19 @@ abstract class base { * @throws \coding_exception */ protected final function validate_before_trigger() { + global $DB; + if (empty($this->data['crud'])) { throw new \coding_exception('crud must be specified in init() method of each method'); } if (empty($this->data['level'])) { throw new \coding_exception('level must be specified in init() method of each method'); } + if (!empty($this->data['objectid']) and empty($this->data['objecttable'])) { + throw new \coding_exception('objecttable must be specified in init() method if objectid present'); + } - if (debugging('', DEBUG_DEVELOPER)) { + if (debugging('', DEBUG_DEVELOPER)) { // This should be replaced by new $CFG->slowdebug flag if introduced. // Ideally these should be coding exceptions, but we need to skip these for performance reasons // on production servers. @@ -413,6 +420,11 @@ abstract class base { if ($this->data['relateduserid'] and !is_number($this->data['relateduserid'])) { debugging('Event property relateduserid must be a number'); } + if ($this->data['objecttable']) { + if (!$DB->get_manager()->table_exists($this->data['objecttable'])) { + debugging('Unknown table specified in objecttable field'); + } + } } } diff --git a/lib/classes/event/role_assigned.php b/lib/classes/event/role_assigned.php index df25e60f82b..d6652e03b9b 100644 --- a/lib/classes/event/role_assigned.php +++ b/lib/classes/event/role_assigned.php @@ -26,6 +26,7 @@ namespace core\event; class role_assigned extends base { protected function init() { + $this->data['objecttable'] = 'role'; $this->data['crud'] = 'c'; // TODO: MDL-37658 set level $this->data['level'] = 50; diff --git a/lib/classes/event/role_unassigned.php b/lib/classes/event/role_unassigned.php index eaf59c0f8a5..59da07de90d 100644 --- a/lib/classes/event/role_unassigned.php +++ b/lib/classes/event/role_unassigned.php @@ -26,6 +26,7 @@ namespace core\event; class role_unassigned extends base { protected function init() { + $this->data['objecttable'] = 'role'; $this->data['crud'] = 'd'; // TODO: MDL-37658 set level $this->data['level'] = 50; diff --git a/lib/tests/event_test.php b/lib/tests/event_test.php index d0fd5a10c0e..6273cef3693 100644 --- a/lib/tests/event_test.php +++ b/lib/tests/event_test.php @@ -533,6 +533,18 @@ class core_event_testcase extends advanced_testcase { $event = \core_tests\event\bad_event5::create(); @$event->trigger(); $this->assertDebuggingCalled(); + + $event = \core_tests\event\bad_event6::create(); + $event->trigger(); + $this->assertDebuggingCalled(); + + $event = \core_tests\event\bad_event7::create(array('objectid'=>1)); + try { + $event->trigger(); + $this->fail('Exception expected when $data contains objectid by objecttable not specified'); + } catch (\moodle_exception $e) { + $this->assertInstanceOf('\coding_exception', $e); + } } public function test_problematic_events() { diff --git a/lib/tests/fixtures/event_fixtures.php b/lib/tests/fixtures/event_fixtures.php index 6c49751f9d4..368e9cafc38 100644 --- a/lib/tests/fixtures/event_fixtures.php +++ b/lib/tests/fixtures/event_fixtures.php @@ -140,6 +140,22 @@ class bad_event5 extends \core\event\base { } } +class bad_event6 extends \core\event\base { + protected function init() { + $this->data['crud'] = 'c'; + $this->data['level'] = 10; + $this->data['objecttable'] = 'xxx_xxx_xx'; + } +} + +class bad_event7 extends \core\event\base { + protected function init() { + $this->data['crud'] = 'c'; + $this->data['level'] = 10; + $this->data['objecttable'] = null; + } +} + class problematic_event1 extends \core\event\base { protected function init() { $this->data['crud'] = 'u';