MDL-42453 theme_base: Fixed Error in base/style/admin.css that breaks CSS validator.
[moodle.git] / webservice / tests / events_test.php
blob7152077703d8a19d80278f58634f40e6ee304dad
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.
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/>.
17 /**
18 * Unit tests for Web service events.
20 * @package webservice
21 * @category phpunit
22 * @copyright 2013 Frédéric Massart
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * Unit tests for Web service events.
31 * @package webservice
32 * @category phpunit
33 * @copyright 2013 Frédéric Massart
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class core_webservice_events_testcase extends advanced_testcase {
38 public function setUp() {
39 $this->resetAfterTest();
42 public function test_function_called() {
43 // The Web service API doesn't allow the testing of the events directly by
44 // calling some functions which trigger the events, so what we are going here
45 // is just checking that the event returns the expected information.
47 $sink = $this->redirectEvents();
49 $fakelogdata = array(1, 'B', true, null);
50 $params = array(
51 'other' => array(
52 'function' => 'A function'
55 $event = \core\event\webservice_function_called::create($params);
56 $event->set_legacy_logdata($fakelogdata);
57 $event->trigger();
59 $events = $sink->get_events();
60 $this->assertCount(1, $events);
61 $event = reset($events);
63 $this->assertEquals(context_system::instance(), $event->get_context());
64 $this->assertEquals('A function', $event->other['function']);
65 $this->assertEventLegacyLogData($fakelogdata, $event);
68 public function test_login_failed() {
69 // The Web service API doesn't allow the testing of the events directly by
70 // calling some functions which trigger the events, so what we are going here
71 // is just checking that the event returns the expected information.
73 $sink = $this->redirectEvents();
75 $fakelogdata = array(1, 'B', true, null);
76 $params = array(
77 'other' => array(
78 'reason' => 'Unit Test',
79 'method' => 'Some method',
80 'tokenid' => '123'
83 $event = \core\event\webservice_login_failed::create($params);
84 $event->set_legacy_logdata($fakelogdata);
85 $event->trigger();
87 $events = $sink->get_events();
88 $this->assertCount(1, $events);
89 $event = reset($events);
91 $this->assertEquals(context_system::instance(), $event->get_context());
92 $this->assertEquals($params['other']['reason'], $event->other['reason']);
93 $this->assertEquals($params['other']['method'], $event->other['method']);
94 $this->assertEquals($params['other']['tokenid'], $event->other['tokenid']);
95 $this->assertEventLegacyLogData($fakelogdata, $event);
97 // We cannot set the token in the other properties.
98 $params['other']['token'] = 'I should not be set';
99 try {
100 $event = \core\event\webservice_login_failed::create($params);
101 $this->fail('The token cannot be allowed in \core\event\webservice_login_failed');
102 } catch (coding_exception $e) {
106 public function test_service_created() {
107 global $CFG, $DB;
109 // The Web service API doesn't allow the testing of the events directly by
110 // calling some functions which trigger the events, so what we are going here
111 // is just checking that the event returns the expected information.
113 $sink = $this->redirectEvents();
115 // Creating a fake service.
116 $service = (object) array(
117 'name' => 'Test',
118 'enabled' => 1,
119 'requiredcapability' => '',
120 'restrictedusers' => 0,
121 'component' => null,
122 'timecreated' => time(),
123 'timemodified' => time(),
124 'shortname' => null,
125 'downloadfiles' => 0,
126 'uploadfiles' => 0
128 $service->id = $DB->insert_record('external_services', $service);
130 // Trigger the event.
131 $params = array(
132 'objectid' => $service->id,
134 $event = \core\event\webservice_service_created::create($params);
135 $event->add_record_snapshot('external_services', $service);
136 $event->trigger();
138 $events = $sink->get_events();
139 $this->assertCount(1, $events);
140 $event = reset($events);
142 // Assert that the event contains the right information.
143 $this->assertEquals(context_system::instance(), $event->get_context());
144 $this->assertEquals($service->id, $event->objectid);
145 $returnurl = $CFG->wwwroot . "/" . $CFG->admin . "/settings.php?section=externalservices";
146 $expected = array(SITEID, 'webservice', 'add', $returnurl, get_string('addservice', 'webservice', $service));
147 $this->assertEventLegacyLogData($expected, $event);
150 public function test_service_updated() {
151 global $CFG, $DB;
153 // The Web service API doesn't allow the testing of the events directly by
154 // calling some functions which trigger the events, so what we are going here
155 // is just checking that the event returns the expected information.
157 $sink = $this->redirectEvents();
159 // Creating a fake service.
160 $service = (object) array(
161 'name' => 'Test',
162 'enabled' => 1,
163 'requiredcapability' => '',
164 'restrictedusers' => 0,
165 'component' => null,
166 'timecreated' => time(),
167 'timemodified' => time(),
168 'shortname' => null,
169 'downloadfiles' => 0,
170 'uploadfiles' => 0
172 $service->id = $DB->insert_record('external_services', $service);
174 // Trigger the event.
175 $params = array(
176 'objectid' => $service->id,
178 $event = \core\event\webservice_service_updated::create($params);
179 $event->add_record_snapshot('external_services', $service);
180 $event->trigger();
182 $events = $sink->get_events();
183 $this->assertCount(1, $events);
184 $event = reset($events);
186 // Assert that the event contains the right information.
187 $this->assertEquals(context_system::instance(), $event->get_context());
188 $this->assertEquals($service->id, $event->objectid);
189 $returnurl = $CFG->wwwroot . "/" . $CFG->admin . "/settings.php?section=externalservices";
190 $expected = array(SITEID, 'webservice', 'edit', $returnurl, get_string('editservice', 'webservice', $service));
191 $this->assertEventLegacyLogData($expected, $event);
194 public function test_service_deleted() {
195 global $CFG, $DB;
197 // The Web service API doesn't allow the testing of the events directly by
198 // calling some functions which trigger the events, so what we are going here
199 // is just checking that the event returns the expected information.
201 $sink = $this->redirectEvents();
203 // Creating a fake service.
204 $service = (object) array(
205 'name' => 'Test',
206 'enabled' => 1,
207 'requiredcapability' => '',
208 'restrictedusers' => 0,
209 'component' => null,
210 'timecreated' => time(),
211 'timemodified' => time(),
212 'shortname' => null,
213 'downloadfiles' => 0,
214 'uploadfiles' => 0
216 $service->id = $DB->insert_record('external_services', $service);
218 // Trigger the event.
219 $params = array(
220 'objectid' => $service->id,
222 $event = \core\event\webservice_service_deleted::create($params);
223 $event->add_record_snapshot('external_services', $service);
224 $event->trigger();
226 $events = $sink->get_events();
227 $this->assertCount(1, $events);
228 $event = reset($events);
230 // Assert that the event contains the right information.
231 $this->assertEquals(context_system::instance(), $event->get_context());
232 $this->assertEquals($service->id, $event->objectid);
233 $returnurl = $CFG->wwwroot . "/" . $CFG->admin . "/settings.php?section=externalservices";
234 $expected = array(SITEID, 'webservice', 'delete', $returnurl, get_string('deleteservice', 'webservice', $service));
235 $this->assertEventLegacyLogData($expected, $event);
238 public function test_service_user_added() {
239 global $CFG;
241 // The Web service API doesn't allow the testing of the events directly by
242 // calling some functions which trigger the events, so what we are going here
243 // is just checking that the event returns the expected information.
245 $sink = $this->redirectEvents();
247 $params = array(
248 'objectid' => 1,
249 'relateduserid' => 2
251 $event = \core\event\webservice_service_user_added::create($params);
252 $event->trigger();
254 $events = $sink->get_events();
255 $this->assertCount(1, $events);
256 $event = reset($events);
258 $this->assertEquals(context_system::instance(), $event->get_context());
259 $this->assertEquals(1, $event->objectid);
260 $this->assertEquals(2, $event->relateduserid);
261 $expected = array(SITEID, 'core', 'assign', $CFG->admin . '/webservice/service_users.php?id=' . $params['objectid'],
262 'add', '', $params['relateduserid']);
263 $this->assertEventLegacyLogData($expected, $event);
266 public function test_service_user_removed() {
267 global $CFG;
269 // The Web service API doesn't allow the testing of the events directly by
270 // calling some functions which trigger the events, so what we are going here
271 // is just checking that the event returns the expected information.
273 $sink = $this->redirectEvents();
275 $params = array(
276 'objectid' => 1,
277 'relateduserid' => 2
279 $event = \core\event\webservice_service_user_removed::create($params);
280 $event->trigger();
282 $events = $sink->get_events();
283 $this->assertCount(1, $events);
284 $event = reset($events);
286 $this->assertEquals(context_system::instance(), $event->get_context());
287 $this->assertEquals(1, $event->objectid);
288 $this->assertEquals(2, $event->relateduserid);
289 $expected = array(SITEID, 'core', 'assign', $CFG->admin . '/webservice/service_users.php?id=' . $params['objectid'],
290 'remove', '', $params['relateduserid']);
291 $this->assertEventLegacyLogData($expected, $event);
294 public function test_token_created() {
295 // The Web service API doesn't allow the testing of the events directly by
296 // calling some functions which trigger the events, so what we are going here
297 // is just checking that the event returns the expected information.
299 $sink = $this->redirectEvents();
301 $params = array(
302 'objectid' => 1,
303 'relateduserid' => 2,
304 'other' => array(
305 'auto' => true
308 $event = \core\event\webservice_token_created::create($params);
309 $event->trigger();
311 $events = $sink->get_events();
312 $this->assertCount(1, $events);
313 $event = reset($events);
315 $this->assertEquals(context_system::instance(), $event->get_context());
316 $this->assertEquals(1, $event->objectid);
317 $this->assertEquals(2, $event->relateduserid);
318 $expected = array(SITEID, 'webservice', 'automatically create user token', '' , 'User ID: ' . 2);
319 $this->assertEventLegacyLogData($expected, $event);
322 public function test_token_sent() {
323 $user = $this->getDataGenerator()->create_user();
324 $this->setUser($user);
326 // The Web service API doesn't allow the testing of the events directly by
327 // calling some functions which trigger the events, so what we are going here
328 // is just checking that the event returns the expected information.
330 $sink = $this->redirectEvents();
332 $params = array(
333 'objectid' => 1,
334 'other' => array(
335 'auto' => true
338 $event = \core\event\webservice_token_sent::create($params);
339 $event->trigger();
341 $events = $sink->get_events();
342 $this->assertCount(1, $events);
343 $event = reset($events);
345 $this->assertEquals(context_system::instance(), $event->get_context());
346 $this->assertEquals(1, $event->objectid);
347 $expected = array(SITEID, 'webservice', 'sending requested user token', '' , 'User ID: ' . $user->id);
348 $this->assertEventLegacyLogData($expected, $event);