From 5dffc067da26f3e345efb1cbb6fb2f9f8a6d8f35 Mon Sep 17 00:00:00 2001 From: Yuliya Bozhko Date: Mon, 19 Aug 2013 08:44:02 +1200 Subject: [PATCH] MDL-41228 phpunit: Reset message processors list in unit tests --- lib/phpunit/classes/util.php | 1 + lib/phpunit/tests/advanced_test.php | 27 +++++++++++++++++++++++++++ message/lib.php | 6 +++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/phpunit/classes/util.php b/lib/phpunit/classes/util.php index 85cdaed9278..2f6456ef441 100644 --- a/lib/phpunit/classes/util.php +++ b/lib/phpunit/classes/util.php @@ -187,6 +187,7 @@ class phpunit_util extends testing_util { reset_text_filters_cache(true); events_get_handlers('reset'); textlib::reset_caches(); + get_message_processors(false, true); if (class_exists('repository')) { repository::reset_caches(); } diff --git a/lib/phpunit/tests/advanced_test.php b/lib/phpunit/tests/advanced_test.php index 61f5a30f951..4c331de6bd9 100644 --- a/lib/phpunit/tests/advanced_test.php +++ b/lib/phpunit/tests/advanced_test.php @@ -340,6 +340,33 @@ class core_phpunit_advanced_testcase extends advanced_testcase { $this->assertTrue($DB->record_exists('user', array('username'=>'onemore'))); } + public function test_message_processors_reset() { + global $DB; + + $this->resetAfterTest(true); + + // Get all processors first. + $processors1 = get_message_processors(); + + // Add a new message processor and get all processors again. + $processor = new stdClass(); + $processor->name = 'test_processor'; + $processor->enabled = 1; + $DB->insert_record('message_processors', $processor); + + $processors2 = get_message_processors(); + + // Assert that new processor still haven't been added to the list. + $this->assertSame($processors1, $processors2); + + // Reset message processors data. + $processors3 = get_message_processors(false, true); + // Now, list of processors should not be the same any more, + // And we should have one more message processor in the list. + $this->assertNotSame($processors1, $processors3); + $this->assertEquals(count($processors1) + 1, count($processors3)); + } + public function test_message_redirection() { global $DB; diff --git a/message/lib.php b/message/lib.php index bd642ce697d..95b44e3b163 100644 --- a/message/lib.php +++ b/message/lib.php @@ -2280,12 +2280,16 @@ function message_print_heading($title, $colspan=3) { * system configuration * * @param bool $ready only return ready-to-use processors + * @param bool $reset Reset list of message processors (used in unit tests) * @return mixed $processors array of objects containing information on message processors */ -function get_message_processors($ready = false) { +function get_message_processors($ready = false, $reset = false) { global $DB, $CFG; static $processors; + if ($reset) { + $processors = array(); + } if (empty($processors)) { // Get all processors, ensure the name column is the first so it will be the array key