MDL-39846 introduce new objecttable property

This commit is contained in:
Petr Škoda
2013-07-19 08:43:29 +02:00
parent 5fef139cd9
commit a85258cac8
5 changed files with 45 additions and 3 deletions
+15 -3
View File
@@ -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');
}
}
}
}
+1
View File
@@ -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;
+1
View File
@@ -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;
+12
View File
@@ -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() {
+16
View File
@@ -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';