diff --git a/lib/phpunit/classes/advanced_testcase.php b/lib/phpunit/classes/advanced_testcase.php index 7ee75ebf374..1c0bd816cd9 100644 --- a/lib/phpunit/classes/advanced_testcase.php +++ b/lib/phpunit/classes/advanced_testcase.php @@ -307,6 +307,44 @@ abstract class advanced_testcase extends base_testcase { $this->resetDebugging(); } + /** + * Asserts how many times debugging has been called. + * + * @param int $expectedcount The expected number of times + * @param array $debugmessages Expected debugging messages, one for each expected message. + * @param array $debuglevels Expected debugging levels, one for each expected message. + * @param string $message + * @return void + */ + public function assertDebuggingCalledCount($expectedcount, $debugmessages = array(), $debuglevels = array(), $message = '') { + if (!is_int($expectedcount)) { + throw new coding_exception('assertDebuggingCalledCount $expectedcount argument should be an integer.'); + } + + $debugging = $this->getDebuggingMessages(); + $this->assertEquals($expectedcount, count($debugging), $message); + + if ($debugmessages) { + if (!is_array($debugmessages) || count($debugmessages) != $expectedcount) { + throw new coding_exception('assertDebuggingCalledCount $debugmessages should contain ' . $expectedcount . ' messages'); + } + foreach ($debugmessages as $key => $debugmessage) { + $this->assertSame($debugmessage, $debugging[$key]->message, $message); + } + } + + if ($debuglevels) { + if (!is_array($debuglevels) || count($debuglevels) != $expectedcount) { + throw new coding_exception('assertDebuggingCalledCount $debuglevels should contain ' . $expectedcount . ' messages'); + } + foreach ($debuglevels as $key => $debuglevel) { + $this->assertSame($debuglevel, $debugging[$key]->level, $message); + } + } + + $this->resetDebugging(); + } + /** * Call when no debugging() messages expected. * @param string $message diff --git a/lib/tests/event_test.php b/lib/tests/event_test.php index 55cdbf9acd5..f260cc61818 100644 --- a/lib/tests/event_test.php +++ b/lib/tests/event_test.php @@ -29,6 +29,8 @@ require_once(__DIR__.'/fixtures/event_fixtures.php'); class core_event_testcase extends advanced_testcase { + const DEBUGGING_MSG = 'Events API using $handlers array has been deprecated in favour of Events 2 API, please use it instead.'; + public function test_event_properties() { global $USER; @@ -591,6 +593,7 @@ class core_event_testcase extends advanced_testcase { $DB->delete_records('log', array()); events_update_definition('unittest'); + $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER); $DB->delete_records_select('events_handlers', "component <> 'unittest'"); events_get_handlers('reset'); $this->assertEquals(3, $DB->count_records('events_handlers')); @@ -601,10 +604,12 @@ class core_event_testcase extends advanced_testcase { $event1 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>5, 'xx'=>10))); $event1->trigger(); + $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER); $event2 = \core_tests\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>6, 'xx'=>11))); $event2->nest = true; $event2->trigger(); + $this->assertDebuggingCalledCount(2, array(self::DEBUGGING_MSG, self::DEBUGGING_MSG), array(DEBUG_DEVELOPER, DEBUG_DEVELOPER)); $this->assertSame( array('observe_all-5', 'observe_one-5', 'legacy_handler-0', 'observe_all-nesting-6', 'legacy_handler-0', 'observe_one-6', 'observe_all-666', 'observe_one-666', 'legacy_handler-0'), diff --git a/lib/tests/eventslib_test.php b/lib/tests/eventslib_test.php index 0428744a24f..c62d5cf2e4f 100644 --- a/lib/tests/eventslib_test.php +++ b/lib/tests/eventslib_test.php @@ -29,6 +29,8 @@ defined('MOODLE_INTERNAL') || die(); class core_eventslib_testcase extends advanced_testcase { + const DEBUGGING_MSG = 'Events API using $handlers array has been deprecated in favour of Events 2 API, please use it instead.'; + /** * Create temporary entries in the database for these tests. * These tests have to work no matter the data currently in the database @@ -40,7 +42,6 @@ class core_eventslib_testcase extends advanced_testcase { // Set global category settings to -1 (not force). eventslib_sample_function_handler('reset'); eventslib_sample_handler_class::static_method('reset'); - events_update_definition('unittest'); $this->resetAfterTest(); } @@ -51,6 +52,9 @@ class core_eventslib_testcase extends advanced_testcase { public function test_events_update_definition__install() { global $DB; + events_update_definition('unittest'); + $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER); + $dbcount = $DB->count_records('events_handlers', array('component'=>'unittest')); $handlers = array(); require(__DIR__.'/fixtures/events.php'); @@ -63,6 +67,9 @@ class core_eventslib_testcase extends advanced_testcase { public function test_events_update_definition__uninstall() { global $DB; + events_update_definition('unittest'); + $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER); + events_uninstall('unittest'); $this->assertEquals(0, $DB->count_records('events_handlers', array('component'=>'unittest')), 'All handlers should be uninstalled: %s'); } @@ -72,6 +79,10 @@ class core_eventslib_testcase extends advanced_testcase { */ public function test_events_update_definition__update() { global $DB; + + events_update_definition('unittest'); + $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER); + // First modify directly existing handler. $handler = $DB->get_record('events_handlers', array('component'=>'unittest', 'eventname'=>'test_instant')); @@ -82,6 +93,7 @@ class core_eventslib_testcase extends advanced_testcase { // Update the definition, it should revert the handler back. events_update_definition('unittest'); + $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER); $handler = $DB->get_record('events_handlers', array('component'=>'unittest', 'eventname'=>'test_instant')); $this->assertSame($handler->handlerfunction, $original, 'update should sync db with file definition: %s'); } @@ -90,15 +102,27 @@ class core_eventslib_testcase extends advanced_testcase { * Tests events_trigger_is_registered() function. */ public function test_events_is_registered() { + + events_update_definition('unittest'); + $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER); + $this->assertTrue(events_is_registered('test_instant', 'unittest')); + $this->assertDebuggingCalled('events_is_registered() has been deprecated along with all Events 1 API in favour of Events 2' . + ' API, please use it instead.', DEBUG_DEVELOPER); } /** * Tests events_trigger_legacy() function. */ public function test_events_trigger_legacy_instant() { + + events_update_definition('unittest'); + $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER); + $this->assertEquals(0, events_trigger_legacy('test_instant', 'ok')); + $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER); $this->assertEquals(0, events_trigger_legacy('test_instant', 'ok')); + $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER); $this->assertEquals(2, eventslib_sample_function_handler('status')); } @@ -106,9 +130,16 @@ class core_eventslib_testcase extends advanced_testcase { * Tests events_trigger_legacy() function. */ public function test_events_trigger__cron() { + + events_update_definition('unittest'); + $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER); + $this->assertEquals(0, events_trigger_legacy('test_cron', 'ok')); $this->assertEquals(0, eventslib_sample_handler_class::static_method('status')); events_cron('test_cron'); + // The events_cron one + one for each triggered event above (triggered in events_dispatch). + $this->assertDebuggingCalledCount(2, array(self::DEBUGGING_MSG, self::DEBUGGING_MSG), + array(DEBUG_DEVELOPER, DEBUG_DEVELOPER)); $this->assertEquals(1, eventslib_sample_handler_class::static_method('status')); } @@ -116,10 +147,20 @@ class core_eventslib_testcase extends advanced_testcase { * Tests events_pending_count() function. */ public function test_events_pending_count() { + + events_update_definition('unittest'); + $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER); + events_trigger_legacy('test_cron', 'ok'); + $this->assertDebuggingNotCalled(); events_trigger_legacy('test_cron', 'ok'); + $this->assertDebuggingNotCalled(); events_cron('test_cron'); + // The events_cron one + one for each triggered event above (triggered in events_dispatch). + $this->assertDebuggingCalledCount(3); $this->assertEquals(0, events_pending_count('test_cron'), 'all messages should be already dequeued: %s'); + $this->assertDebuggingCalled('events_pending_count() has been deprecated along with all Events 1 API in favour of Events 2' . + ' API, please use it instead.', DEBUG_DEVELOPER); } /** @@ -127,34 +168,64 @@ class core_eventslib_testcase extends advanced_testcase { */ public function test_events_trigger__failed_instant() { global $CFG; + + events_update_definition('unittest'); + $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER); + $olddebug = $CFG->debug; $this->assertEquals(1, events_trigger_legacy('test_instant', 'fail'), 'fail first event: %s'); + $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER); $this->assertEquals(1, events_trigger_legacy('test_instant', 'ok'), 'this one should fail too: %s'); + $this->assertDebuggingNotCalled(); $this->assertEquals(0, events_cron('test_instant'), 'all events should stay in queue: %s'); - $this->assertDebuggingCalled(); + // events_cron + one for each dispatched event. + $this->assertDebuggingCalledCount(3); $this->assertEquals(2, events_pending_count('test_instant'), 'two events should in queue: %s'); + $this->assertDebuggingCalled('events_pending_count() has been deprecated along with all Events 1 API in favour of Events 2' . + ' API, please use it instead.', DEBUG_DEVELOPER); + $this->assertEquals(0, eventslib_sample_function_handler('status'), 'verify no event dispatched yet: %s'); eventslib_sample_function_handler('ignorefail'); // Ignore "fail" eventdata from now on. $this->assertEquals(1, events_trigger_legacy('test_instant', 'ok'), 'this one should go to queue directly: %s'); + $this->assertDebuggingNotCalled(); + $this->assertEquals(3, events_pending_count('test_instant'), 'three events should in queue: %s'); + $this->assertDebuggingCalled('events_pending_count() has been deprecated along with all Events 1 API in favour of Events 2' . + ' API, please use it instead.', DEBUG_DEVELOPER); + $this->assertEquals(0, eventslib_sample_function_handler('status'), 'verify previous event was not dispatched: %s'); $this->assertEquals(3, events_cron('test_instant'), 'all events should be dispatched: %s'); + // events_cron + one for each dispatched event. + $this->assertDebuggingCalledCount(4); + $this->assertEquals(3, eventslib_sample_function_handler('status'), 'verify three events were dispatched: %s'); $this->assertEquals(0, events_pending_count('test_instant'), 'no events should in queue: %s'); + $this->assertDebuggingCalled('events_pending_count() has been deprecated along with all Events 1 API in favour of Events 2' . + ' API, please use it instead.', DEBUG_DEVELOPER); + $this->assertEquals(0, events_trigger_legacy('test_instant', 'ok'), 'this event should be dispatched immediately: %s'); + $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER); + $this->assertEquals(4, eventslib_sample_function_handler('status'), 'verify event was dispatched: %s'); $this->assertEquals(0, events_pending_count('test_instant'), 'no events should in queue: %s'); + $this->assertDebuggingCalled('events_pending_count() has been deprecated along with all Events 1 API in favour of Events 2' . + ' API, please use it instead.', DEBUG_DEVELOPER); } /** * Tests events_trigger() function. */ public function test_events_trigger_debugging() { + + events_update_definition('unittest'); + $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER); + $this->assertEquals(0, events_trigger('test_instant', 'ok')); - $this->assertDebuggingCalled(); + $debugmessages = array('events_trigger() is deprecated, please use new events instead', self::DEBUGGING_MSG); + $this->assertDebuggingCalledCount(2, $debugmessages, array(DEBUG_DEVELOPER, DEBUG_DEVELOPER)); } }