Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
/**
18
 * Unit tests for Web service events.
19
 *
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
24
 */
25
 
26
namespace core_webservice\event;
27
 
28
/**
29
 * Unit tests for Web service events.
30
 *
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
35
 */
1441 ariadna 36
final class events_test extends \advanced_testcase {
1 efrain 37
 
38
    public function setUp(): void {
1441 ariadna 39
        parent::setUp();
1 efrain 40
        $this->resetAfterTest();
41
    }
42
 
11 efrain 43
    public function test_function_called(): void {
1 efrain 44
        // The Web service API doesn't allow the testing of the events directly by
45
        // calling some functions which trigger the events, so what we are going here
46
        // is just checking that the event returns the expected information.
47
 
48
        $sink = $this->redirectEvents();
49
 
50
        $params = array(
51
            'other' => array(
52
                'function' => 'A function'
53
            )
54
        );
55
        $event = \core\event\webservice_function_called::create($params);
56
        $event->trigger();
57
 
58
        $events = $sink->get_events();
59
        $this->assertCount(1, $events);
60
        $event = reset($events);
61
 
62
        $this->assertEquals(\context_system::instance(), $event->get_context());
63
        $this->assertEquals('A function', $event->other['function']);
64
        $this->assertEventContextNotUsed($event);
65
    }
66
 
11 efrain 67
    public function test_login_failed(): void {
1 efrain 68
        // The Web service API doesn't allow the testing of the events directly by
69
        // calling some functions which trigger the events, so what we are going here
70
        // is just checking that the event returns the expected information.
71
 
72
        $sink = $this->redirectEvents();
73
 
74
        $params = array(
75
            'other' => array(
76
                'reason' => 'Unit Test',
77
                'method' => 'Some method',
78
                'tokenid' => '123'
79
            )
80
        );
81
        $event = \core\event\webservice_login_failed::create($params);
82
        $event->trigger();
83
 
84
        $events = $sink->get_events();
85
        $this->assertCount(1, $events);
86
        $event = reset($events);
87
 
88
        $this->assertEquals(\context_system::instance(), $event->get_context());
89
        $this->assertEquals($params['other']['reason'], $event->other['reason']);
90
        $this->assertEquals($params['other']['method'], $event->other['method']);
91
        $this->assertEquals($params['other']['tokenid'], $event->other['tokenid']);
92
 
93
        // We cannot set the token in the other properties.
94
        $params['other']['token'] = 'I should not be set';
95
        try {
96
            $event = \core\event\webservice_login_failed::create($params);
97
            $this->fail('The token cannot be allowed in \core\event\webservice_login_failed');
98
        } catch (\coding_exception $e) {
99
        }
100
        $this->assertEventContextNotUsed($event);
101
    }
102
 
11 efrain 103
    public function test_service_created(): void {
1 efrain 104
        global $CFG, $DB;
105
 
106
        // The Web service API doesn't allow the testing of the events directly by
107
        // calling some functions which trigger the events, so what we are going here
108
        // is just checking that the event returns the expected information.
109
 
110
        $sink = $this->redirectEvents();
111
 
112
        // Creating a fake service.
113
        $service = (object) array(
114
            'name' => 'Test',
115
            'enabled' => 1,
116
            'requiredcapability' => '',
117
            'restrictedusers' => 0,
118
            'component' => null,
119
            'timecreated' => time(),
120
            'timemodified' => time(),
121
            'shortname' => null,
122
            'downloadfiles' => 0,
123
            'uploadfiles' => 0
124
        );
125
        $service->id = $DB->insert_record('external_services', $service);
126
 
127
        // Trigger the event.
128
        $params = array(
129
            'objectid' => $service->id,
130
        );
131
        $event = \core\event\webservice_service_created::create($params);
132
        $event->add_record_snapshot('external_services', $service);
133
        $event->trigger();
134
 
135
        $events = $sink->get_events();
136
        $this->assertCount(1, $events);
137
        $event = reset($events);
138
 
139
        // Assert that the event contains the right information.
140
        $this->assertEquals(\context_system::instance(), $event->get_context());
141
        $this->assertEquals($service->id, $event->objectid);
142
        $this->assertEventContextNotUsed($event);
143
    }
144
 
11 efrain 145
    public function test_service_updated(): void {
1 efrain 146
        global $CFG, $DB;
147
 
148
        // The Web service API doesn't allow the testing of the events directly by
149
        // calling some functions which trigger the events, so what we are going here
150
        // is just checking that the event returns the expected information.
151
 
152
        $sink = $this->redirectEvents();
153
 
154
        // Creating a fake service.
155
        $service = (object) array(
156
            'name' => 'Test',
157
            'enabled' => 1,
158
            'requiredcapability' => '',
159
            'restrictedusers' => 0,
160
            'component' => null,
161
            'timecreated' => time(),
162
            'timemodified' => time(),
163
            'shortname' => null,
164
            'downloadfiles' => 0,
165
            'uploadfiles' => 0
166
        );
167
        $service->id = $DB->insert_record('external_services', $service);
168
 
169
        // Trigger the event.
170
        $params = array(
171
            'objectid' => $service->id,
172
        );
173
        $event = \core\event\webservice_service_updated::create($params);
174
        $event->add_record_snapshot('external_services', $service);
175
        $event->trigger();
176
 
177
        $events = $sink->get_events();
178
        $this->assertCount(1, $events);
179
        $event = reset($events);
180
 
181
        // Assert that the event contains the right information.
182
        $this->assertEquals(\context_system::instance(), $event->get_context());
183
        $this->assertEquals($service->id, $event->objectid);
184
        $this->assertEventContextNotUsed($event);
185
    }
186
 
11 efrain 187
    public function test_service_deleted(): void {
1 efrain 188
        global $CFG, $DB;
189
 
190
        // The Web service API doesn't allow the testing of the events directly by
191
        // calling some functions which trigger the events, so what we are going here
192
        // is just checking that the event returns the expected information.
193
 
194
        $sink = $this->redirectEvents();
195
 
196
        // Creating a fake service.
197
        $service = (object) array(
198
            'name' => 'Test',
199
            'enabled' => 1,
200
            'requiredcapability' => '',
201
            'restrictedusers' => 0,
202
            'component' => null,
203
            'timecreated' => time(),
204
            'timemodified' => time(),
205
            'shortname' => null,
206
            'downloadfiles' => 0,
207
            'uploadfiles' => 0
208
        );
209
        $service->id = $DB->insert_record('external_services', $service);
210
 
211
        // Trigger the event.
212
        $params = array(
213
            'objectid' => $service->id,
214
        );
215
        $event = \core\event\webservice_service_deleted::create($params);
216
        $event->add_record_snapshot('external_services', $service);
217
        $event->trigger();
218
 
219
        $events = $sink->get_events();
220
        $this->assertCount(1, $events);
221
        $event = reset($events);
222
 
223
        // Assert that the event contains the right information.
224
        $this->assertEquals(\context_system::instance(), $event->get_context());
225
        $this->assertEquals($service->id, $event->objectid);
226
        $this->assertEventContextNotUsed($event);
227
    }
228
 
11 efrain 229
    public function test_service_user_added(): void {
1 efrain 230
        global $CFG;
231
 
232
        // The Web service API doesn't allow the testing of the events directly by
233
        // calling some functions which trigger the events, so what we are going here
234
        // is just checking that the event returns the expected information.
235
 
236
        $sink = $this->redirectEvents();
237
 
238
        $params = array(
239
            'objectid' => 1,
240
            'relateduserid' => 2
241
        );
242
        $event = \core\event\webservice_service_user_added::create($params);
243
        $event->trigger();
244
 
245
        $events = $sink->get_events();
246
        $this->assertCount(1, $events);
247
        $event = reset($events);
248
 
249
        $this->assertEquals(\context_system::instance(), $event->get_context());
250
        $this->assertEquals(1, $event->objectid);
251
        $this->assertEquals(2, $event->relateduserid);
252
        $this->assertEventContextNotUsed($event);
253
    }
254
 
11 efrain 255
    public function test_service_user_removed(): void {
1 efrain 256
        global $CFG;
257
 
258
        // The Web service API doesn't allow the testing of the events directly by
259
        // calling some functions which trigger the events, so what we are going here
260
        // is just checking that the event returns the expected information.
261
 
262
        $sink = $this->redirectEvents();
263
 
264
        $params = array(
265
            'objectid' => 1,
266
            'relateduserid' => 2
267
        );
268
        $event = \core\event\webservice_service_user_removed::create($params);
269
        $event->trigger();
270
 
271
        $events = $sink->get_events();
272
        $this->assertCount(1, $events);
273
        $event = reset($events);
274
 
275
        $this->assertEquals(\context_system::instance(), $event->get_context());
276
        $this->assertEquals(1, $event->objectid);
277
        $this->assertEquals(2, $event->relateduserid);
278
        $this->assertEventContextNotUsed($event);
279
    }
280
 
11 efrain 281
    public function test_token_created(): void {
1 efrain 282
        // The Web service API doesn't allow the testing of the events directly by
283
        // calling some functions which trigger the events, so what we are going here
284
        // is just checking that the event returns the expected information.
285
 
286
        $sink = $this->redirectEvents();
287
 
288
        $params = array(
289
            'objectid' => 1,
290
            'relateduserid' => 2,
291
            'other' => array(
292
                'auto' => true
293
            )
294
        );
295
        $event = \core\event\webservice_token_created::create($params);
296
        $event->trigger();
297
 
298
        $events = $sink->get_events();
299
        $this->assertCount(1, $events);
300
        $event = reset($events);
301
 
302
        $this->assertEquals(\context_system::instance(), $event->get_context());
303
        $this->assertEquals(1, $event->objectid);
304
        $this->assertEquals(2, $event->relateduserid);
305
        $this->assertEventContextNotUsed($event);
306
    }
307
 
11 efrain 308
    public function test_token_sent(): void {
1 efrain 309
        $user = $this->getDataGenerator()->create_user();
310
        $this->setUser($user);
311
 
312
        // The Web service API doesn't allow the testing of the events directly by
313
        // calling some functions which trigger the events, so what we are going here
314
        // is just checking that the event returns the expected information.
315
 
316
        $sink = $this->redirectEvents();
317
 
318
        $params = array(
319
            'objectid' => 1,
320
            'other' => array(
321
                'auto' => true
322
            )
323
        );
324
        $event = \core\event\webservice_token_sent::create($params);
325
        $event->trigger();
326
 
327
        $events = $sink->get_events();
328
        $this->assertCount(1, $events);
329
        $event = reset($events);
330
 
331
        $this->assertEquals(\context_system::instance(), $event->get_context());
332
        $this->assertEquals(1, $event->objectid);
333
        $this->assertEventContextNotUsed($event);
334
    }
335
}