From 300dbc66f26afdc0b4a59919e8929e99eef75df7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Mon, 15 Jul 2013 09:23:45 +0200 Subject: [PATCH] MDL-39846 prevent adding of snapshots after event trigger --- lib/classes/event/base.php | 6 ++++++ lib/tests/event_test.php | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/classes/event/base.php b/lib/classes/event/base.php index dc2ce74aba0..6a73c1637bd 100644 --- a/lib/classes/event/base.php +++ b/lib/classes/event/base.php @@ -508,10 +508,16 @@ abstract class base implements \IteratorAggregate { * * @param string $tablename * @param \stdClass $record + * + * @throws \coding_exception if used after ::trigger() */ public function add_record_snapshot($tablename, $record) { global $DB; + if ($this->triggered) { + throw new \coding_exception('It is not possible to add snapshots after triggering of events'); + } + // NOTE: this might use some kind of MUC cache, // hopefully we will not run out of memory here... if (debugging('', DEBUG_DEVELOPER)) { // This should be replaced by new $CFG->slowdebug flag if introduced. diff --git a/lib/tests/event_test.php b/lib/tests/event_test.php index faba7230711..3c69163161e 100644 --- a/lib/tests/event_test.php +++ b/lib/tests/event_test.php @@ -639,6 +639,15 @@ class core_event_testcase extends advanced_testcase { $user = $event->get_record_snapshot('user', 1); $this->assertEquals(1, $user->id); $this->assertSame('guest', $user->username); + + $event->add_record_snapshot('course', $course1); + $event->trigger(); + try { + $event->add_record_snapshot('course', $course1); + $this->fail('Updating of snapshots after trigger is not ok');; + } catch (\moodle_exception $e) { + $this->assertInstanceOf('\coding_exception', $e); + } } public function test_iteration() {