From 605a8c336f1f2ad52ff2b751cb567d317ee3dcf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Tue, 9 Jul 2013 14:51:36 +0200 Subject: [PATCH] MDL-39846 add more magic and improve tests --- lib/classes/event/base.php | 28 +++++++++++++++++++--- lib/tests/event_test.php | 48 +++++++++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/lib/classes/event/base.php b/lib/classes/event/base.php index 87764c2a057..708813f39bb 100644 --- a/lib/classes/event/base.php +++ b/lib/classes/event/base.php @@ -520,9 +520,6 @@ abstract class base { /** * Magic getter for read only access. * - * Note: we must not allow modification of data from outside, - * after trigger() the data MUST NOT CHANGE!!! - * * @param string $name * @return mixed */ @@ -533,4 +530,29 @@ abstract class base { debugging("Accessing non-existent event property '$name'"); } + + /** + * Magic setter. + * + * Note: we must not allow modification of data from outside, + * after trigger() the data MUST NOT CHANGE!!! + * + * @param string $name + * @param mixed $value + * + * @throws \coding_exception + */ + public function __set($name, $value) { + throw new \coding_exception('Event properties must not be modified.'); + } + + /** + * Is data property set? + * + * @param string $name + * @return bool + */ + public function __isset($name) { + return isset($this->data[$name]); + } } diff --git a/lib/tests/event_test.php b/lib/tests/event_test.php index c6db5fe92b1..3745d69a2f0 100644 --- a/lib/tests/event_test.php +++ b/lib/tests/event_test.php @@ -29,6 +29,52 @@ require_once(__DIR__.'/fixtures/event_fixtures.php'); class core_event_testcase extends advanced_testcase { + public function test_event_properties() { + global $USER; + + $system = \context_system::instance(); + $event = \core_tests\event\unittest_executed::create(array('courseid'=>1, 'context'=>$system, 'objectid'=>5, 'extra'=>array('sample'=>null, 'xx'=>10))); + + $this->assertSame('\core_tests\event\unittest_executed', $event->eventname); + $this->assertSame('core_tests', $event->component); + $this->assertSame('executed', $event->action); + $this->assertSame('unittest', $event->object); + $this->assertSame(5, $event->objectid); + $this->assertSame('u', $event->crud); + $this->assertSame(10, $event->level); + + $this->assertSame($system, $event->get_context()); + $this->assertSame($system->id, $event->contextid); + $this->assertSame($system->contextlevel, $event->contextlevel); + $this->assertSame($system->instanceid, $event->contextinstanceid); + + $this->assertSame($USER->id, $event->userid); + $this->assertSame(1, $event->courseid); + + $this->assertNull($event->relateduserid); + $this->assertFalse(isset($event->relateduserid)); + + $this->assertSame(array('sample'=>null, 'xx'=>10), $event->extra); + $this->assertTrue(isset($event->extra['xx'])); + $this->assertFalse(isset($event->extra['sample'])); + + $this->assertLessThanOrEqual(time(), $event->timecreated); + + try { + $event->courseid = 2; + $this->fail('Exception expected on event modification'); + } catch (\moodle_exception $e) { + $this->assertInstanceOf('coding_exception', $e); + } + + try { + $event->xxxx = 1; + $this->fail('Exception expected on event modification'); + } catch (\moodle_exception $e) { + $this->assertInstanceOf('coding_exception', $e); + } + } + public function test_observers_parsing() { $observers = array( @@ -536,4 +582,4 @@ class core_event_testcase extends advanced_testcase { $this->assertEquals(1, $user->id); $this->assertSame('guest', $user->username); } - } +}