PageRenderTime 31ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/webservice/tests/events_test.php

http://github.com/moodle/moodle
PHP | 359 lines | 233 code | 59 blank | 67 comment | 1 complexity | 5ddb402a193a85d9966f8e0167343a98 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. * Unit tests for Web service events.
  18. *
  19. * @package webservice
  20. * @category phpunit
  21. * @copyright 2013 Frédéric Massart
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. /**
  26. * Unit tests for Web service events.
  27. *
  28. * @package webservice
  29. * @category phpunit
  30. * @copyright 2013 Frédéric Massart
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class core_webservice_events_testcase extends advanced_testcase {
  34. public function setUp() {
  35. $this->resetAfterTest();
  36. }
  37. public function test_function_called() {
  38. // The Web service API doesn't allow the testing of the events directly by
  39. // calling some functions which trigger the events, so what we are going here
  40. // is just checking that the event returns the expected information.
  41. $sink = $this->redirectEvents();
  42. $fakelogdata = array(1, 'B', true, null);
  43. $params = array(
  44. 'other' => array(
  45. 'function' => 'A function'
  46. )
  47. );
  48. $event = \core\event\webservice_function_called::create($params);
  49. $event->set_legacy_logdata($fakelogdata);
  50. $event->trigger();
  51. $events = $sink->get_events();
  52. $this->assertCount(1, $events);
  53. $event = reset($events);
  54. $this->assertEquals(context_system::instance(), $event->get_context());
  55. $this->assertEquals('A function', $event->other['function']);
  56. $this->assertEventLegacyLogData($fakelogdata, $event);
  57. $this->assertEventContextNotUsed($event);
  58. }
  59. public function test_login_failed() {
  60. // The Web service API doesn't allow the testing of the events directly by
  61. // calling some functions which trigger the events, so what we are going here
  62. // is just checking that the event returns the expected information.
  63. $sink = $this->redirectEvents();
  64. $fakelogdata = array(1, 'B', true, null);
  65. $params = array(
  66. 'other' => array(
  67. 'reason' => 'Unit Test',
  68. 'method' => 'Some method',
  69. 'tokenid' => '123'
  70. )
  71. );
  72. $event = \core\event\webservice_login_failed::create($params);
  73. $event->set_legacy_logdata($fakelogdata);
  74. $event->trigger();
  75. $events = $sink->get_events();
  76. $this->assertCount(1, $events);
  77. $event = reset($events);
  78. $this->assertEquals(context_system::instance(), $event->get_context());
  79. $this->assertEquals($params['other']['reason'], $event->other['reason']);
  80. $this->assertEquals($params['other']['method'], $event->other['method']);
  81. $this->assertEquals($params['other']['tokenid'], $event->other['tokenid']);
  82. $this->assertEventLegacyLogData($fakelogdata, $event);
  83. // We cannot set the token in the other properties.
  84. $params['other']['token'] = 'I should not be set';
  85. try {
  86. $event = \core\event\webservice_login_failed::create($params);
  87. $this->fail('The token cannot be allowed in \core\event\webservice_login_failed');
  88. } catch (coding_exception $e) {
  89. }
  90. $this->assertEventContextNotUsed($event);
  91. }
  92. public function test_service_created() {
  93. global $CFG, $DB;
  94. // The Web service API doesn't allow the testing of the events directly by
  95. // calling some functions which trigger the events, so what we are going here
  96. // is just checking that the event returns the expected information.
  97. $sink = $this->redirectEvents();
  98. // Creating a fake service.
  99. $service = (object) array(
  100. 'name' => 'Test',
  101. 'enabled' => 1,
  102. 'requiredcapability' => '',
  103. 'restrictedusers' => 0,
  104. 'component' => null,
  105. 'timecreated' => time(),
  106. 'timemodified' => time(),
  107. 'shortname' => null,
  108. 'downloadfiles' => 0,
  109. 'uploadfiles' => 0
  110. );
  111. $service->id = $DB->insert_record('external_services', $service);
  112. // Trigger the event.
  113. $params = array(
  114. 'objectid' => $service->id,
  115. );
  116. $event = \core\event\webservice_service_created::create($params);
  117. $event->add_record_snapshot('external_services', $service);
  118. $event->trigger();
  119. $events = $sink->get_events();
  120. $this->assertCount(1, $events);
  121. $event = reset($events);
  122. // Assert that the event contains the right information.
  123. $this->assertEquals(context_system::instance(), $event->get_context());
  124. $this->assertEquals($service->id, $event->objectid);
  125. $returnurl = $CFG->wwwroot . "/" . $CFG->admin . "/settings.php?section=externalservices";
  126. $expected = array(SITEID, 'webservice', 'add', $returnurl, get_string('addservice', 'webservice', $service));
  127. $this->assertEventLegacyLogData($expected, $event);
  128. $this->assertEventContextNotUsed($event);
  129. }
  130. public function test_service_updated() {
  131. global $CFG, $DB;
  132. // The Web service API doesn't allow the testing of the events directly by
  133. // calling some functions which trigger the events, so what we are going here
  134. // is just checking that the event returns the expected information.
  135. $sink = $this->redirectEvents();
  136. // Creating a fake service.
  137. $service = (object) array(
  138. 'name' => 'Test',
  139. 'enabled' => 1,
  140. 'requiredcapability' => '',
  141. 'restrictedusers' => 0,
  142. 'component' => null,
  143. 'timecreated' => time(),
  144. 'timemodified' => time(),
  145. 'shortname' => null,
  146. 'downloadfiles' => 0,
  147. 'uploadfiles' => 0
  148. );
  149. $service->id = $DB->insert_record('external_services', $service);
  150. // Trigger the event.
  151. $params = array(
  152. 'objectid' => $service->id,
  153. );
  154. $event = \core\event\webservice_service_updated::create($params);
  155. $event->add_record_snapshot('external_services', $service);
  156. $event->trigger();
  157. $events = $sink->get_events();
  158. $this->assertCount(1, $events);
  159. $event = reset($events);
  160. // Assert that the event contains the right information.
  161. $this->assertEquals(context_system::instance(), $event->get_context());
  162. $this->assertEquals($service->id, $event->objectid);
  163. $returnurl = $CFG->wwwroot . "/" . $CFG->admin . "/settings.php?section=externalservices";
  164. $expected = array(SITEID, 'webservice', 'edit', $returnurl, get_string('editservice', 'webservice', $service));
  165. $this->assertEventLegacyLogData($expected, $event);
  166. $this->assertEventContextNotUsed($event);
  167. }
  168. public function test_service_deleted() {
  169. global $CFG, $DB;
  170. // The Web service API doesn't allow the testing of the events directly by
  171. // calling some functions which trigger the events, so what we are going here
  172. // is just checking that the event returns the expected information.
  173. $sink = $this->redirectEvents();
  174. // Creating a fake service.
  175. $service = (object) array(
  176. 'name' => 'Test',
  177. 'enabled' => 1,
  178. 'requiredcapability' => '',
  179. 'restrictedusers' => 0,
  180. 'component' => null,
  181. 'timecreated' => time(),
  182. 'timemodified' => time(),
  183. 'shortname' => null,
  184. 'downloadfiles' => 0,
  185. 'uploadfiles' => 0
  186. );
  187. $service->id = $DB->insert_record('external_services', $service);
  188. // Trigger the event.
  189. $params = array(
  190. 'objectid' => $service->id,
  191. );
  192. $event = \core\event\webservice_service_deleted::create($params);
  193. $event->add_record_snapshot('external_services', $service);
  194. $event->trigger();
  195. $events = $sink->get_events();
  196. $this->assertCount(1, $events);
  197. $event = reset($events);
  198. // Assert that the event contains the right information.
  199. $this->assertEquals(context_system::instance(), $event->get_context());
  200. $this->assertEquals($service->id, $event->objectid);
  201. $returnurl = $CFG->wwwroot . "/" . $CFG->admin . "/settings.php?section=externalservices";
  202. $expected = array(SITEID, 'webservice', 'delete', $returnurl, get_string('deleteservice', 'webservice', $service));
  203. $this->assertEventLegacyLogData($expected, $event);
  204. $this->assertEventContextNotUsed($event);
  205. }
  206. public function test_service_user_added() {
  207. global $CFG;
  208. // The Web service API doesn't allow the testing of the events directly by
  209. // calling some functions which trigger the events, so what we are going here
  210. // is just checking that the event returns the expected information.
  211. $sink = $this->redirectEvents();
  212. $params = array(
  213. 'objectid' => 1,
  214. 'relateduserid' => 2
  215. );
  216. $event = \core\event\webservice_service_user_added::create($params);
  217. $event->trigger();
  218. $events = $sink->get_events();
  219. $this->assertCount(1, $events);
  220. $event = reset($events);
  221. $this->assertEquals(context_system::instance(), $event->get_context());
  222. $this->assertEquals(1, $event->objectid);
  223. $this->assertEquals(2, $event->relateduserid);
  224. $expected = array(SITEID, 'core', 'assign', $CFG->admin . '/webservice/service_users.php?id=' . $params['objectid'],
  225. 'add', '', $params['relateduserid']);
  226. $this->assertEventLegacyLogData($expected, $event);
  227. $this->assertEventContextNotUsed($event);
  228. }
  229. public function test_service_user_removed() {
  230. global $CFG;
  231. // The Web service API doesn't allow the testing of the events directly by
  232. // calling some functions which trigger the events, so what we are going here
  233. // is just checking that the event returns the expected information.
  234. $sink = $this->redirectEvents();
  235. $params = array(
  236. 'objectid' => 1,
  237. 'relateduserid' => 2
  238. );
  239. $event = \core\event\webservice_service_user_removed::create($params);
  240. $event->trigger();
  241. $events = $sink->get_events();
  242. $this->assertCount(1, $events);
  243. $event = reset($events);
  244. $this->assertEquals(context_system::instance(), $event->get_context());
  245. $this->assertEquals(1, $event->objectid);
  246. $this->assertEquals(2, $event->relateduserid);
  247. $expected = array(SITEID, 'core', 'assign', $CFG->admin . '/webservice/service_users.php?id=' . $params['objectid'],
  248. 'remove', '', $params['relateduserid']);
  249. $this->assertEventLegacyLogData($expected, $event);
  250. $this->assertEventContextNotUsed($event);
  251. }
  252. public function test_token_created() {
  253. // The Web service API doesn't allow the testing of the events directly by
  254. // calling some functions which trigger the events, so what we are going here
  255. // is just checking that the event returns the expected information.
  256. $sink = $this->redirectEvents();
  257. $params = array(
  258. 'objectid' => 1,
  259. 'relateduserid' => 2,
  260. 'other' => array(
  261. 'auto' => true
  262. )
  263. );
  264. $event = \core\event\webservice_token_created::create($params);
  265. $event->trigger();
  266. $events = $sink->get_events();
  267. $this->assertCount(1, $events);
  268. $event = reset($events);
  269. $this->assertEquals(context_system::instance(), $event->get_context());
  270. $this->assertEquals(1, $event->objectid);
  271. $this->assertEquals(2, $event->relateduserid);
  272. $expected = array(SITEID, 'webservice', 'automatically create user token', '' , 'User ID: ' . 2);
  273. $this->assertEventLegacyLogData($expected, $event);
  274. $this->assertEventContextNotUsed($event);
  275. }
  276. public function test_token_sent() {
  277. $user = $this->getDataGenerator()->create_user();
  278. $this->setUser($user);
  279. // The Web service API doesn't allow the testing of the events directly by
  280. // calling some functions which trigger the events, so what we are going here
  281. // is just checking that the event returns the expected information.
  282. $sink = $this->redirectEvents();
  283. $params = array(
  284. 'objectid' => 1,
  285. 'other' => array(
  286. 'auto' => true
  287. )
  288. );
  289. $event = \core\event\webservice_token_sent::create($params);
  290. $event->trigger();
  291. $events = $sink->get_events();
  292. $this->assertCount(1, $events);
  293. $event = reset($events);
  294. $this->assertEquals(context_system::instance(), $event->get_context());
  295. $this->assertEquals(1, $event->objectid);
  296. $expected = array(SITEID, 'webservice', 'sending requested user token', '' , 'User ID: ' . $user->id);
  297. $this->assertEventLegacyLogData($expected, $event);
  298. $this->assertEventContextNotUsed($event);
  299. }
  300. }