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
 
1441 ariadna 17
use core_badges\tests\badges_testcase;
18
 
1 efrain 19
/**
20
 * Badge events tests class.
21
 *
22
 * @package    core_badges
23
 * @copyright  2015 onwards Simey Lameze <simey@moodle.com>
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
1441 ariadna 26
final class events_test extends badges_testcase {
1 efrain 27
    /**
28
     * Test badge awarded event.
29
     */
11 efrain 30
    public function test_badge_awarded(): void {
1 efrain 31
        $systemcontext = context_system::instance();
32
 
33
        $sink = $this->redirectEvents();
34
 
35
        $badge = new badge($this->badgeid);
36
        $badge->issue($this->user->id, true);
37
        $badge->is_issued($this->user->id);
38
        $events = $sink->get_events();
39
        $this->assertCount(1, $events);
40
        $event = reset($events);
41
        $this->assertInstanceOf('\core\event\badge_awarded', $event);
42
        $this->assertEquals($this->badgeid, $event->objectid);
43
        $this->assertEquals($this->user->id, $event->relateduserid);
44
        $this->assertEquals($systemcontext, $event->get_context());
45
 
46
        $sink->close();
47
    }
48
 
49
    /**
50
     * Test the badge created event.
51
     *
52
     * There is no external API for creating a badge, so the unit test will simply
53
     * create and trigger the event and ensure data is returned as expected.
54
     */
11 efrain 55
    public function test_badge_created(): void {
1 efrain 56
        $badge = new badge($this->badgeid);
57
        // Trigger an event: badge created.
58
        $eventparams = array(
59
            'userid' => $badge->usercreated,
60
            'objectid' => $badge->id,
61
            'context' => $badge->get_context(),
62
        );
63
 
64
        $event = \core\event\badge_created::create($eventparams);
65
        // Trigger and capture the event.
66
        $sink = $this->redirectEvents();
67
        $event->trigger();
68
        $events = $sink->get_events();
69
        $event = reset($events);
70
 
71
        // Check that the event data is valid.
72
        $this->assertInstanceOf('\core\event\badge_created', $event);
73
        $this->assertEquals($badge->usercreated, $event->userid);
74
        $this->assertEquals($badge->id, $event->objectid);
75
        $this->assertDebuggingNotCalled();
76
        $sink->close();
77
    }
78
 
79
    /**
80
     * Test the badge archived event.
81
     *
82
     */
11 efrain 83
    public function test_badge_archived(): void {
1 efrain 84
        $badge = new badge($this->badgeid);
85
        $sink = $this->redirectEvents();
86
 
87
        // Trigger and capture the event.
88
        $badge->delete(true);
89
        $events = $sink->get_events();
90
        $this->assertCount(2, $events);
91
        $event = $events[1];
92
 
93
        // Check that the event data is valid.
94
        $this->assertInstanceOf('\core\event\badge_archived', $event);
95
        $this->assertEquals($badge->id, $event->objectid);
96
        $this->assertDebuggingNotCalled();
97
        $sink->close();
98
    }
99
 
100
    /**
101
     * Test the badge updated event.
102
     *
103
     */
11 efrain 104
    public function test_badge_updated(): void {
1 efrain 105
        $badge = new badge($this->badgeid);
106
        $sink = $this->redirectEvents();
107
 
108
        // Trigger and capture the event.
109
        $badge->save();
110
        $events = $sink->get_events();
111
        $event = reset($events);
112
        $this->assertCount(1, $events);
113
 
114
        // Check that the event data is valid.
115
        $this->assertInstanceOf('\core\event\badge_updated', $event);
116
        $this->assertEquals($badge->id, $event->objectid);
117
        $this->assertDebuggingNotCalled();
118
        $sink->close();
1441 ariadna 119
    }
1 efrain 120
 
121
    /**
122
     * Test the badge deleted event.
123
     */
11 efrain 124
    public function test_badge_deleted(): void {
1 efrain 125
        $badge = new badge($this->badgeid);
126
        $sink = $this->redirectEvents();
127
 
128
        // Trigger and capture the event.
129
        $badge->delete(false);
130
        $events = $sink->get_events();
131
        $event = reset($events);
132
        $this->assertCount(1, $events);
133
 
134
        // Check that the event data is valid.
135
        $this->assertInstanceOf('\core\event\badge_deleted', $event);
136
        $this->assertEquals($badge->id, $event->objectid);
137
        $this->assertDebuggingNotCalled();
138
        $sink->close();
139
    }
140
 
141
    /**
142
     * Test the badge duplicated event.
143
     *
144
     */
11 efrain 145
    public function test_badge_duplicated(): void {
1 efrain 146
        $badge = new badge($this->badgeid);
147
        $sink = $this->redirectEvents();
148
 
149
        // Trigger and capture the event.
150
        $newid = $badge->make_clone();
151
        $events = $sink->get_events();
152
        $event = reset($events);
153
        $this->assertCount(1, $events);
154
 
155
        // Check that the event data is valid.
156
        $this->assertInstanceOf('\core\event\badge_duplicated', $event);
157
        $this->assertEquals($newid, $event->objectid);
158
        $this->assertDebuggingNotCalled();
159
        $sink->close();
160
    }
161
 
162
    /**
163
     * Test the badge disabled event.
164
     *
165
     */
11 efrain 166
    public function test_badge_disabled(): void {
1 efrain 167
        $badge = new badge($this->badgeid);
168
        $sink = $this->redirectEvents();
169
 
170
        // Trigger and capture the event.
171
        $badge->set_status(BADGE_STATUS_INACTIVE);
172
        $events = $sink->get_events();
173
        $event = reset($events);
174
        $this->assertCount(2, $events);
175
        $event = $events[1];
176
 
177
        // Check that the event data is valid.
178
        $this->assertInstanceOf('\core\event\badge_disabled', $event);
179
        $this->assertEquals($badge->id, $event->objectid);
180
        $this->assertDebuggingNotCalled();
181
        $sink->close();
182
    }
183
 
184
    /**
185
     * Test the badge enabled event.
186
     *
187
     */
11 efrain 188
    public function test_badge_enabled(): void {
1 efrain 189
        $badge = new badge($this->badgeid);
190
        $sink = $this->redirectEvents();
191
 
192
        // Trigger and capture the event.
193
        $badge->set_status(BADGE_STATUS_ACTIVE);
194
        $events = $sink->get_events();
195
        $event = reset($events);
196
        $this->assertCount(2, $events);
197
        $event = $events[1];
198
 
199
        // Check that the event data is valid.
200
        $this->assertInstanceOf('\core\event\badge_enabled', $event);
201
        $this->assertEquals($badge->id, $event->objectid);
202
        $this->assertDebuggingNotCalled();
203
        $sink->close();
204
    }
205
 
206
    /**
207
     * Test the badge criteria created event.
208
     *
209
     * There is no external API for this, so the unit test will simply
210
     * create and trigger the event and ensure data is returned as expected.
211
     */
11 efrain 212
    public function test_badge_criteria_created(): void {
1 efrain 213
        $badge = new badge($this->badgeid);
214
 
215
        // Trigger and capture the event.
216
        $sink = $this->redirectEvents();
217
        $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
218
        $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
219
        $criteriaprofile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
220
        $params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address');
221
        $criteriaprofile->save($params);
222
        $events = $sink->get_events();
223
        $event = reset($events);
224
 
225
        // Check that the event data is valid.
226
        $this->assertCount(1, $events);
227
        $this->assertInstanceOf('\core\event\badge_criteria_created', $event);
228
        $this->assertEquals($criteriaprofile->id, $event->objectid);
229
        $this->assertEquals($criteriaprofile->badgeid, $event->other['badgeid']);
230
        $this->assertDebuggingNotCalled();
231
        $sink->close();
232
    }
233
 
234
    /**
235
     * Test the badge criteria updated event.
236
     *
237
     * There is no external API for this, so the unit test will simply
238
     * create and trigger the event and ensure data is returned as expected.
239
     */
11 efrain 240
    public function test_badge_criteria_updated(): void {
1 efrain 241
        $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
242
        $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
243
        $criteriaprofile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $this->badgeid));
244
        $params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address');
245
        $criteriaprofile->save($params);
246
        $badge = new badge($this->badgeid);
247
 
248
        // Trigger and capture the event.
249
        $sink = $this->redirectEvents();
250
        $criteria = $badge->criteria[BADGE_CRITERIA_TYPE_PROFILE];
251
        $params2 = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address', 'id' => $criteria->id);
252
        $criteria->save((array)$params2);
253
        $events = $sink->get_events();
254
        $event = reset($events);
255
 
256
        // Check that the event data is valid.
257
        $this->assertCount(1, $events);
258
        $this->assertInstanceOf('\core\event\badge_criteria_updated', $event);
259
        $this->assertEquals($criteria->id, $event->objectid);
260
        $this->assertEquals($this->badgeid, $event->other['badgeid']);
261
        $this->assertDebuggingNotCalled();
262
        $sink->close();
263
    }
264
 
265
    /**
266
     * Test the badge criteria deleted event.
267
     *
268
     * There is no external API for this, so the unit test will simply
269
     * create and trigger the event and ensure data is returned as expected.
270
     */
11 efrain 271
    public function test_badge_criteria_deleted(): void {
1 efrain 272
        $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
273
        $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
274
        $badge = new badge($this->badgeid);
275
 
276
        // Trigger and capture the event.
277
        $sink = $this->redirectEvents();
278
        $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->delete();
279
        $events = $sink->get_events();
280
        $event = reset($events);
281
 
282
        // Check that the event data is valid.
283
        $this->assertCount(1, $events);
284
        $this->assertInstanceOf('\core\event\badge_criteria_deleted', $event);
285
        $this->assertEquals($criteriaoverall->badgeid, $event->other['badgeid']);
286
        $this->assertDebuggingNotCalled();
287
        $sink->close();
288
    }
289
 
290
    /**
291
     * Test the badge viewed event.
292
     *
293
     * There is no external API for viewing a badge, so the unit test will simply
294
     * create and trigger the event and ensure data is returned as expected.
295
     */
11 efrain 296
    public function test_badge_viewed(): void {
1 efrain 297
        $badge = new badge($this->badgeid);
298
        // Trigger an event: badge viewed.
299
        $other = array('badgeid' => $badge->id, 'badgehash' => '12345678');
300
        $eventparams = array(
301
            'context' => $badge->get_context(),
302
            'other' => $other,
303
        );
304
 
305
        $event = \core\event\badge_viewed::create($eventparams);
306
        // Trigger and capture the event.
307
        $sink = $this->redirectEvents();
308
        $event->trigger();
309
        $events = $sink->get_events();
310
        $event = reset($events);
311
 
312
        // Check that the event data is valid.
313
        $this->assertInstanceOf('\core\event\badge_viewed', $event);
314
        $this->assertEquals('12345678', $event->other['badgehash']);
315
        $this->assertEquals($badge->id, $event->other['badgeid']);
316
        $this->assertDebuggingNotCalled();
317
        $sink->close();
318
    }
319
 
320
    /**
321
     * Test the badge listing viewed event.
322
     *
323
     * There is no external API for viewing a badge, so the unit test will simply
324
     * create and trigger the event and ensure data is returned as expected.
325
     */
11 efrain 326
    public function test_badge_listing_viewed(): void {
1 efrain 327
        // Trigger an event: badge listing viewed.
328
        $context = context_system::instance();
329
        $eventparams = array(
330
            'context' => $context,
331
            'other' => array('badgetype' => BADGE_TYPE_SITE)
332
        );
333
 
334
        $event = \core\event\badge_listing_viewed::create($eventparams);
335
        // Trigger and capture the event.
336
        $sink = $this->redirectEvents();
337
        $event->trigger();
338
        $events = $sink->get_events();
339
        $event = reset($events);
340
 
341
        // Check that the event data is valid.
342
        $this->assertInstanceOf('\core\event\badge_listing_viewed', $event);
343
        $this->assertEquals(BADGE_TYPE_SITE, $event->other['badgetype']);
344
        $this->assertDebuggingNotCalled();
345
        $sink->close();
346
    }
347
}