weekly release 4.5dev
[moodle.git] / badges / tests / events_test.php
blobc319e163cec73a0ac81661c9fa13b6293d0283fe
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/>.
16 /**
17 * Badge events tests.
19 * @package core_badges
20 * @copyright 2015 onwards Simey Lameze <simey@moodle.com>
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 defined('MOODLE_INTERNAL') || die();
24 global $CFG;
25 require_once($CFG->dirroot . '/badges/tests/badgeslib_test.php');
27 /**
28 * Badge events tests class.
30 * @package core_badges
31 * @copyright 2015 onwards Simey Lameze <simey@moodle.com>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class events_test extends badgeslib_test {
36 /**
37 * Test badge awarded event.
39 public function test_badge_awarded(): void {
41 $systemcontext = context_system::instance();
43 $sink = $this->redirectEvents();
45 $badge = new badge($this->badgeid);
46 $badge->issue($this->user->id, true);
47 $badge->is_issued($this->user->id);
48 $events = $sink->get_events();
49 $this->assertCount(1, $events);
50 $event = reset($events);
51 $this->assertInstanceOf('\core\event\badge_awarded', $event);
52 $this->assertEquals($this->badgeid, $event->objectid);
53 $this->assertEquals($this->user->id, $event->relateduserid);
54 $this->assertEquals($systemcontext, $event->get_context());
56 $sink->close();
59 /**
60 * Test the badge created event.
62 * There is no external API for creating a badge, so the unit test will simply
63 * create and trigger the event and ensure data is returned as expected.
65 public function test_badge_created(): void {
67 $badge = new badge($this->badgeid);
68 // Trigger an event: badge created.
69 $eventparams = array(
70 'userid' => $badge->usercreated,
71 'objectid' => $badge->id,
72 'context' => $badge->get_context(),
75 $event = \core\event\badge_created::create($eventparams);
76 // Trigger and capture the event.
77 $sink = $this->redirectEvents();
78 $event->trigger();
79 $events = $sink->get_events();
80 $event = reset($events);
82 // Check that the event data is valid.
83 $this->assertInstanceOf('\core\event\badge_created', $event);
84 $this->assertEquals($badge->usercreated, $event->userid);
85 $this->assertEquals($badge->id, $event->objectid);
86 $this->assertDebuggingNotCalled();
87 $sink->close();
91 /**
92 * Test the badge archived event.
95 public function test_badge_archived(): void {
96 $badge = new badge($this->badgeid);
97 $sink = $this->redirectEvents();
99 // Trigger and capture the event.
100 $badge->delete(true);
101 $events = $sink->get_events();
102 $this->assertCount(2, $events);
103 $event = $events[1];
105 // Check that the event data is valid.
106 $this->assertInstanceOf('\core\event\badge_archived', $event);
107 $this->assertEquals($badge->id, $event->objectid);
108 $this->assertDebuggingNotCalled();
109 $sink->close();
115 * Test the badge updated event.
118 public function test_badge_updated(): void {
119 $badge = new badge($this->badgeid);
120 $sink = $this->redirectEvents();
122 // Trigger and capture the event.
123 $badge->save();
124 $events = $sink->get_events();
125 $event = reset($events);
126 $this->assertCount(1, $events);
128 // Check that the event data is valid.
129 $this->assertInstanceOf('\core\event\badge_updated', $event);
130 $this->assertEquals($badge->id, $event->objectid);
131 $this->assertDebuggingNotCalled();
132 $sink->close();
136 * Test the badge deleted event.
138 public function test_badge_deleted(): void {
139 $badge = new badge($this->badgeid);
140 $sink = $this->redirectEvents();
142 // Trigger and capture the event.
143 $badge->delete(false);
144 $events = $sink->get_events();
145 $event = reset($events);
146 $this->assertCount(1, $events);
148 // Check that the event data is valid.
149 $this->assertInstanceOf('\core\event\badge_deleted', $event);
150 $this->assertEquals($badge->id, $event->objectid);
151 $this->assertDebuggingNotCalled();
152 $sink->close();
157 * Test the badge duplicated event.
160 public function test_badge_duplicated(): void {
161 $badge = new badge($this->badgeid);
162 $sink = $this->redirectEvents();
164 // Trigger and capture the event.
165 $newid = $badge->make_clone();
166 $events = $sink->get_events();
167 $event = reset($events);
168 $this->assertCount(1, $events);
170 // Check that the event data is valid.
171 $this->assertInstanceOf('\core\event\badge_duplicated', $event);
172 $this->assertEquals($newid, $event->objectid);
173 $this->assertDebuggingNotCalled();
174 $sink->close();
179 * Test the badge disabled event.
182 public function test_badge_disabled(): void {
183 $badge = new badge($this->badgeid);
184 $sink = $this->redirectEvents();
186 // Trigger and capture the event.
187 $badge->set_status(BADGE_STATUS_INACTIVE);
188 $events = $sink->get_events();
189 $event = reset($events);
190 $this->assertCount(2, $events);
191 $event = $events[1];
193 // Check that the event data is valid.
194 $this->assertInstanceOf('\core\event\badge_disabled', $event);
195 $this->assertEquals($badge->id, $event->objectid);
196 $this->assertDebuggingNotCalled();
197 $sink->close();
202 * Test the badge enabled event.
205 public function test_badge_enabled(): void {
206 $badge = new badge($this->badgeid);
207 $sink = $this->redirectEvents();
209 // Trigger and capture the event.
210 $badge->set_status(BADGE_STATUS_ACTIVE);
211 $events = $sink->get_events();
212 $event = reset($events);
213 $this->assertCount(2, $events);
214 $event = $events[1];
216 // Check that the event data is valid.
217 $this->assertInstanceOf('\core\event\badge_enabled', $event);
218 $this->assertEquals($badge->id, $event->objectid);
219 $this->assertDebuggingNotCalled();
220 $sink->close();
225 * Test the badge criteria created event.
227 * There is no external API for this, so the unit test will simply
228 * create and trigger the event and ensure data is returned as expected.
230 public function test_badge_criteria_created(): void {
232 $badge = new badge($this->badgeid);
234 // Trigger and capture the event.
235 $sink = $this->redirectEvents();
236 $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
237 $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
238 $criteriaprofile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
239 $params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address');
240 $criteriaprofile->save($params);
241 $events = $sink->get_events();
242 $event = reset($events);
244 // Check that the event data is valid.
245 $this->assertCount(1, $events);
246 $this->assertInstanceOf('\core\event\badge_criteria_created', $event);
247 $this->assertEquals($criteriaprofile->id, $event->objectid);
248 $this->assertEquals($criteriaprofile->badgeid, $event->other['badgeid']);
249 $this->assertDebuggingNotCalled();
250 $sink->close();
255 * Test the badge criteria updated event.
257 * There is no external API for this, so the unit test will simply
258 * create and trigger the event and ensure data is returned as expected.
260 public function test_badge_criteria_updated(): void {
262 $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
263 $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
264 $criteriaprofile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $this->badgeid));
265 $params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address');
266 $criteriaprofile->save($params);
267 $badge = new badge($this->badgeid);
269 // Trigger and capture the event.
270 $sink = $this->redirectEvents();
271 $criteria = $badge->criteria[BADGE_CRITERIA_TYPE_PROFILE];
272 $params2 = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address', 'id' => $criteria->id);
273 $criteria->save((array)$params2);
274 $events = $sink->get_events();
275 $event = reset($events);
277 // Check that the event data is valid.
278 $this->assertCount(1, $events);
279 $this->assertInstanceOf('\core\event\badge_criteria_updated', $event);
280 $this->assertEquals($criteria->id, $event->objectid);
281 $this->assertEquals($this->badgeid, $event->other['badgeid']);
282 $this->assertDebuggingNotCalled();
283 $sink->close();
288 * Test the badge criteria deleted event.
290 * There is no external API for this, so the unit test will simply
291 * create and trigger the event and ensure data is returned as expected.
293 public function test_badge_criteria_deleted(): void {
295 $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
296 $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
297 $badge = new badge($this->badgeid);
299 // Trigger and capture the event.
300 $sink = $this->redirectEvents();
301 $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->delete();
302 $events = $sink->get_events();
303 $event = reset($events);
305 // Check that the event data is valid.
306 $this->assertCount(1, $events);
307 $this->assertInstanceOf('\core\event\badge_criteria_deleted', $event);
308 $this->assertEquals($criteriaoverall->badgeid, $event->other['badgeid']);
309 $this->assertDebuggingNotCalled();
310 $sink->close();
315 * Test the badge viewed event.
317 * There is no external API for viewing a badge, so the unit test will simply
318 * create and trigger the event and ensure data is returned as expected.
320 public function test_badge_viewed(): void {
322 $badge = new badge($this->badgeid);
323 // Trigger an event: badge viewed.
324 $other = array('badgeid' => $badge->id, 'badgehash' => '12345678');
325 $eventparams = array(
326 'context' => $badge->get_context(),
327 'other' => $other,
330 $event = \core\event\badge_viewed::create($eventparams);
331 // Trigger and capture the event.
332 $sink = $this->redirectEvents();
333 $event->trigger();
334 $events = $sink->get_events();
335 $event = reset($events);
337 // Check that the event data is valid.
338 $this->assertInstanceOf('\core\event\badge_viewed', $event);
339 $this->assertEquals('12345678', $event->other['badgehash']);
340 $this->assertEquals($badge->id, $event->other['badgeid']);
341 $this->assertDebuggingNotCalled();
342 $sink->close();
347 * Test the badge listing viewed event.
349 * There is no external API for viewing a badge, so the unit test will simply
350 * create and trigger the event and ensure data is returned as expected.
352 public function test_badge_listing_viewed(): void {
354 // Trigger an event: badge listing viewed.
355 $context = context_system::instance();
356 $eventparams = array(
357 'context' => $context,
358 'other' => array('badgetype' => BADGE_TYPE_SITE)
361 $event = \core\event\badge_listing_viewed::create($eventparams);
362 // Trigger and capture the event.
363 $sink = $this->redirectEvents();
364 $event->trigger();
365 $events = $sink->get_events();
366 $event = reset($events);
368 // Check that the event data is valid.
369 $this->assertInstanceOf('\core\event\badge_listing_viewed', $event);
370 $this->assertEquals(BADGE_TYPE_SITE, $event->other['badgetype']);
371 $this->assertDebuggingNotCalled();
372 $sink->close();