PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/tests/eventslib_test.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 303 lines | 172 code | 61 blank | 70 comment | 33 complexity | a439a308ebb06fe51b63964f28b4418a MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Tests events subsystem.
  18. *
  19. * @package core_event
  20. * @subpackage phpunit
  21. * @copyright 2007 onwards Martin Dougiamas (http://dougiamas.com)
  22. * @author Petr Skoda {@link http://skodak.org}
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. class core_eventslib_testcase extends advanced_testcase {
  27. const DEBUGGING_MSG = 'Events API using $handlers array has been deprecated in favour of Events 2 API, please use it instead.';
  28. /**
  29. * Create temporary entries in the database for these tests.
  30. * These tests have to work no matter the data currently in the database
  31. * (meaning they should run on a brand new site). This means several items of
  32. * data have to be artificially inseminated (:-) in the DB.
  33. */
  34. protected function setUp() {
  35. parent::setUp();
  36. // Set global category settings to -1 (not force).
  37. eventslib_sample_function_handler('reset');
  38. eventslib_sample_handler_class::static_method('reset');
  39. $this->resetAfterTest();
  40. }
  41. /**
  42. * Tests the installation of event handlers from file
  43. */
  44. public function test_events_update_definition__install() {
  45. global $DB;
  46. events_update_definition('unittest');
  47. $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
  48. $dbcount = $DB->count_records('events_handlers', array('component'=>'unittest'));
  49. $handlers = array();
  50. require(__DIR__.'/fixtures/events.php');
  51. $this->assertCount($dbcount, $handlers, 'Equal number of handlers in file and db: %s');
  52. }
  53. /**
  54. * Tests the uninstallation of event handlers from file.
  55. */
  56. public function test_events_update_definition__uninstall() {
  57. global $DB;
  58. events_update_definition('unittest');
  59. $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
  60. events_uninstall('unittest');
  61. $this->assertEquals(0, $DB->count_records('events_handlers', array('component'=>'unittest')), 'All handlers should be uninstalled: %s');
  62. }
  63. /**
  64. * Tests the update of event handlers from file.
  65. */
  66. public function test_events_update_definition__update() {
  67. global $DB;
  68. events_update_definition('unittest');
  69. $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
  70. // First modify directly existing handler.
  71. $handler = $DB->get_record('events_handlers', array('component'=>'unittest', 'eventname'=>'test_instant'));
  72. $original = $handler->handlerfunction;
  73. // Change handler in db.
  74. $DB->set_field('events_handlers', 'handlerfunction', serialize('some_other_function_handler'), array('id'=>$handler->id));
  75. // Update the definition, it should revert the handler back.
  76. events_update_definition('unittest');
  77. $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
  78. $handler = $DB->get_record('events_handlers', array('component'=>'unittest', 'eventname'=>'test_instant'));
  79. $this->assertSame($handler->handlerfunction, $original, 'update should sync db with file definition: %s');
  80. }
  81. /**
  82. * Tests events_trigger_is_registered() function.
  83. */
  84. public function test_events_is_registered() {
  85. events_update_definition('unittest');
  86. $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
  87. $this->assertTrue(events_is_registered('test_instant', 'unittest'));
  88. $this->assertDebuggingCalled('events_is_registered() has been deprecated along with all Events 1 API in favour of Events 2' .
  89. ' API, please use it instead.', DEBUG_DEVELOPER);
  90. }
  91. /**
  92. * Tests events_trigger_legacy() function.
  93. */
  94. public function test_events_trigger_legacy_instant() {
  95. events_update_definition('unittest');
  96. $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
  97. $this->assertEquals(0, events_trigger_legacy('test_instant', 'ok'));
  98. $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
  99. $this->assertEquals(0, events_trigger_legacy('test_instant', 'ok'));
  100. $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
  101. $this->assertEquals(2, eventslib_sample_function_handler('status'));
  102. }
  103. /**
  104. * Tests events_trigger_legacy() function.
  105. */
  106. public function test_events_trigger__cron() {
  107. events_update_definition('unittest');
  108. $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
  109. $this->assertEquals(0, events_trigger_legacy('test_cron', 'ok'));
  110. $this->assertEquals(0, eventslib_sample_handler_class::static_method('status'));
  111. events_cron('test_cron');
  112. // The events_cron one + one for each triggered event above (triggered in events_dispatch).
  113. $this->assertDebuggingCalledCount(2, array(self::DEBUGGING_MSG, self::DEBUGGING_MSG),
  114. array(DEBUG_DEVELOPER, DEBUG_DEVELOPER));
  115. $this->assertEquals(1, eventslib_sample_handler_class::static_method('status'));
  116. }
  117. /**
  118. * Tests events_pending_count() function.
  119. */
  120. public function test_events_pending_count() {
  121. events_update_definition('unittest');
  122. $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
  123. events_trigger_legacy('test_cron', 'ok');
  124. $this->assertDebuggingNotCalled();
  125. events_trigger_legacy('test_cron', 'ok');
  126. $this->assertDebuggingNotCalled();
  127. events_cron('test_cron');
  128. // The events_cron one + one for each triggered event above (triggered in events_dispatch).
  129. $this->assertDebuggingCalledCount(3);
  130. $this->assertEquals(0, events_pending_count('test_cron'), 'all messages should be already dequeued: %s');
  131. $this->assertDebuggingCalled('events_pending_count() has been deprecated along with all Events 1 API in favour of Events 2' .
  132. ' API, please use it instead.', DEBUG_DEVELOPER);
  133. }
  134. /**
  135. * Tests events_trigger_legacy() function when instant handler fails.
  136. */
  137. public function test_events_trigger__failed_instant() {
  138. global $CFG;
  139. events_update_definition('unittest');
  140. $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
  141. $olddebug = $CFG->debug;
  142. $this->assertEquals(1, events_trigger_legacy('test_instant', 'fail'), 'fail first event: %s');
  143. $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
  144. $this->assertEquals(1, events_trigger_legacy('test_instant', 'ok'), 'this one should fail too: %s');
  145. $this->assertDebuggingNotCalled();
  146. $this->assertEquals(0, events_cron('test_instant'), 'all events should stay in queue: %s');
  147. // events_cron + one for each dispatched event.
  148. $this->assertDebuggingCalledCount(3);
  149. $this->assertEquals(2, events_pending_count('test_instant'), 'two events should in queue: %s');
  150. $this->assertDebuggingCalled('events_pending_count() has been deprecated along with all Events 1 API in favour of Events 2' .
  151. ' API, please use it instead.', DEBUG_DEVELOPER);
  152. $this->assertEquals(0, eventslib_sample_function_handler('status'), 'verify no event dispatched yet: %s');
  153. eventslib_sample_function_handler('ignorefail'); // Ignore "fail" eventdata from now on.
  154. $this->assertEquals(1, events_trigger_legacy('test_instant', 'ok'), 'this one should go to queue directly: %s');
  155. $this->assertDebuggingNotCalled();
  156. $this->assertEquals(3, events_pending_count('test_instant'), 'three events should in queue: %s');
  157. $this->assertDebuggingCalled('events_pending_count() has been deprecated along with all Events 1 API in favour of Events 2' .
  158. ' API, please use it instead.', DEBUG_DEVELOPER);
  159. $this->assertEquals(0, eventslib_sample_function_handler('status'), 'verify previous event was not dispatched: %s');
  160. $this->assertEquals(3, events_cron('test_instant'), 'all events should be dispatched: %s');
  161. // events_cron + one for each dispatched event.
  162. $this->assertDebuggingCalledCount(4);
  163. $this->assertEquals(3, eventslib_sample_function_handler('status'), 'verify three events were dispatched: %s');
  164. $this->assertEquals(0, events_pending_count('test_instant'), 'no events should in queue: %s');
  165. $this->assertDebuggingCalled('events_pending_count() has been deprecated along with all Events 1 API in favour of Events 2' .
  166. ' API, please use it instead.', DEBUG_DEVELOPER);
  167. $this->assertEquals(0, events_trigger_legacy('test_instant', 'ok'), 'this event should be dispatched immediately: %s');
  168. $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
  169. $this->assertEquals(4, eventslib_sample_function_handler('status'), 'verify event was dispatched: %s');
  170. $this->assertEquals(0, events_pending_count('test_instant'), 'no events should in queue: %s');
  171. $this->assertDebuggingCalled('events_pending_count() has been deprecated along with all Events 1 API in favour of Events 2' .
  172. ' API, please use it instead.', DEBUG_DEVELOPER);
  173. }
  174. /**
  175. * Tests events_trigger() function.
  176. */
  177. public function test_events_trigger_debugging() {
  178. events_update_definition('unittest');
  179. $this->assertDebuggingCalled(self::DEBUGGING_MSG, DEBUG_DEVELOPER);
  180. $this->assertEquals(0, events_trigger('test_instant', 'ok'));
  181. $debugmessages = array('events_trigger() is deprecated, please use new events instead', self::DEBUGGING_MSG);
  182. $this->assertDebuggingCalledCount(2, $debugmessages, array(DEBUG_DEVELOPER, DEBUG_DEVELOPER));
  183. }
  184. }
  185. /**
  186. * Test handler function.
  187. */
  188. function eventslib_sample_function_handler($eventdata) {
  189. static $called = 0;
  190. static $ignorefail = false;
  191. if ($eventdata == 'status') {
  192. return $called;
  193. } else if ($eventdata == 'reset') {
  194. $called = 0;
  195. $ignorefail = false;
  196. return;
  197. } else if ($eventdata == 'fail') {
  198. if ($ignorefail) {
  199. $called++;
  200. return true;
  201. } else {
  202. return false;
  203. }
  204. } else if ($eventdata == 'ignorefail') {
  205. $ignorefail = true;
  206. return;
  207. } else if ($eventdata == 'ok') {
  208. $called++;
  209. return true;
  210. }
  211. print_error('invalideventdata', '', '', $eventdata);
  212. }
  213. /**
  214. * Test handler class with static method.
  215. */
  216. class eventslib_sample_handler_class {
  217. public static function static_method($eventdata) {
  218. static $called = 0;
  219. static $ignorefail = false;
  220. if ($eventdata == 'status') {
  221. return $called;
  222. } else if ($eventdata == 'reset') {
  223. $called = 0;
  224. $ignorefail = false;
  225. return;
  226. } else if ($eventdata == 'fail') {
  227. if ($ignorefail) {
  228. $called++;
  229. return true;
  230. } else {
  231. return false;
  232. }
  233. } else if ($eventdata == 'ignorefail') {
  234. $ignorefail = true;
  235. return;
  236. } else if ($eventdata == 'ok') {
  237. $called++;
  238. return true;
  239. }
  240. print_error('invalideventdata', '', '', $eventdata);
  241. }
  242. }