forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents_test.php
485 lines (390 loc) · 20.3 KB
/
events_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Events tests.
*
* @package core_tag
* @category test
* @copyright 2014 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
// Used to create a wiki page to tag.
require_once($CFG->dirroot . '/mod/wiki/locallib.php');
class core_tag_events_testcase extends advanced_testcase {
/**
* Test set up.
*
* This is executed before running any test in this file.
*/
public function setUp(): void {
$this->resetAfterTest();
}
/**
* Test the tag updated event.
*/
public function test_tag_updated() {
$this->setAdminUser();
// Save the system context.
$systemcontext = context_system::instance();
// Create a tag we are going to update.
$tag = $this->getDataGenerator()->create_tag();
// Store the name before we change it.
$oldname = $tag->name;
// Trigger and capture the event when renaming a tag.
$sink = $this->redirectEvents();
core_tag_tag::get($tag->id, '*')->update(array('rawname' => 'newname'));
// Update the tag's name since we have renamed it.
$tag->name = 'newname';
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
$this->assertInstanceOf('\core\event\tag_updated', $event);
$this->assertEquals($systemcontext, $event->get_context());
$expected = array(SITEID, 'tag', 'update', 'index.php?id=' . $tag->id, $oldname . '->'. $tag->name);
$this->assertEventLegacyLogData($expected, $event);
// Trigger and capture the event when setting the type of a tag.
$sink = $this->redirectEvents();
core_tag_tag::get($tag->id, '*')->update(array('isstandard' => 1));
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
$this->assertInstanceOf('\core\event\tag_updated', $event);
$this->assertEquals($systemcontext, $event->get_context());
$expected = array(0, 'tag', 'update', 'index.php?id=' . $tag->id, $tag->name);
$this->assertEventLegacyLogData($expected, $event);
// Trigger and capture the event for setting the description of a tag.
$sink = $this->redirectEvents();
core_tag_tag::get($tag->id, '*')->update(
array('description' => 'description', 'descriptionformat' => FORMAT_MOODLE));
$events = $sink->get_events();
$event = reset($events);
// Check that the event data is valid.
$this->assertInstanceOf('\core\event\tag_updated', $event);
$this->assertEquals($systemcontext, $event->get_context());
$expected = array(0, 'tag', 'update', 'index.php?id=' . $tag->id, $tag->name);
$this->assertEventLegacyLogData($expected, $event);
}
/**
* Test the tag added event.
*/
public function test_tag_added() {
global $DB;
// Create a course to tag.
$course = $this->getDataGenerator()->create_course();
// Trigger and capture the event for tagging a course.
$sink = $this->redirectEvents();
core_tag_tag::set_item_tags('core', 'course', $course->id, context_course::instance($course->id), array('A tag'));
$events = $sink->get_events();
$event = $events[1];
// Check that the tag was added to the course and that the event data is valid.
$this->assertEquals(1, $DB->count_records('tag_instance', array('component' => 'core')));
$this->assertInstanceOf('\core\event\tag_added', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
$expected = array($course->id, 'coursetags', 'add', 'tag/search.php?query=A+tag', 'Course tagged');
$this->assertEventLegacyLogData($expected, $event);
// Create a question to tag.
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $questiongenerator->create_question_category();
$question = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
// Trigger and capture the event for tagging a question.
$this->assertEquals(1, $DB->count_records('tag_instance'));
$sink = $this->redirectEvents();
core_tag_tag::set_item_tags('core_question', 'question', $question->id,
context::instance_by_id($cat->contextid), array('A tag'));
$events = $sink->get_events();
$event = reset($events);
// Check that the tag was added to the question and the event data is valid.
$this->assertEquals(1, $DB->count_records('tag_instance', array('component' => 'core')));
$this->assertInstanceOf('\core\event\tag_added', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
$expected = null;
$this->assertEventLegacyLogData($expected, $event);
}
/**
* Test the tag removed event.
*/
public function test_tag_removed() {
global $DB;
$this->setAdminUser();
// Create a course to tag.
$course = $this->getDataGenerator()->create_course();
// Create a wiki page to tag.
$wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki');
$wiki = $wikigenerator->create_instance(array('course' => $course->id));
$subwikiid = wiki_add_subwiki($wiki->id, 0);
$wikipageid = wiki_create_page($subwikiid, 'Title', FORMAT_HTML, '2');
// Create the tag.
$tag = $this->getDataGenerator()->create_tag();
// Assign a tag to a course.
core_tag_tag::add_item_tag('core', 'course', $course->id, context_course::instance($course->id), $tag->rawname);
// Trigger and capture the event for untagging a course.
$sink = $this->redirectEvents();
core_tag_tag::remove_item_tag('core', 'course', $course->id, $tag->rawname);
$events = $sink->get_events();
$event = reset($events);
// Check that the tag was removed from the course and the event data is valid.
$this->assertEquals(0, $DB->count_records('tag_instance'));
$this->assertInstanceOf('\core\event\tag_removed', $event);
$this->assertEquals(context_course::instance($course->id), $event->get_context());
// Create the tag.
$tag = $this->getDataGenerator()->create_tag();
// Assign a tag to a wiki this time.
core_tag_tag::add_item_tag('mod_wiki', 'wiki_pages', $wikipageid, context_module::instance($wiki->cmid), $tag->rawname);
// Trigger and capture the event for deleting this tag instance.
$sink = $this->redirectEvents();
core_tag_tag::remove_item_tag('mod_wiki', 'wiki_pages', $wikipageid, $tag->rawname);
$events = $sink->get_events();
$event = reset($events);
// Check that tag was removed from the wiki page and the event data is valid.
$this->assertEquals(0, $DB->count_records('tag_instance'));
$this->assertInstanceOf('\core\event\tag_removed', $event);
$this->assertEquals(context_module::instance($wiki->cmid), $event->get_context());
// Create a tag again - the other would have been deleted since there were no more instances associated with it.
$tag = $this->getDataGenerator()->create_tag();
// Assign a tag to the wiki again.
core_tag_tag::add_item_tag('mod_wiki', 'wiki_pages', $wikipageid, context_module::instance($wiki->cmid), $tag->rawname);
// Now we want to delete this tag, and because there is only one tag instance
// associated with it, it should get deleted as well.
$sink = $this->redirectEvents();
core_tag_tag::delete_tags($tag->id);
$events = $sink->get_events();
$event = reset($events);
// Check that tag was removed from the wiki page and the event data is valid.
$this->assertEquals(0, $DB->count_records('tag_instance'));
$this->assertInstanceOf('\core\event\tag_removed', $event);
$this->assertEquals(context_module::instance($wiki->cmid), $event->get_context());
// Create a tag again - the other would have been deleted since there were no more instances associated with it.
$tag = $this->getDataGenerator()->create_tag();
// Assign a tag to the wiki again.
core_tag_tag::add_item_tag('mod_wiki', 'wiki_pages', $wikipageid, context_module::instance($wiki->cmid), $tag->rawname);
// Delete all tag instances for this wiki instance.
$sink = $this->redirectEvents();
core_tag_tag::delete_instances('mod_wiki', 'wiki_pages', context_module::instance($wiki->cmid)->id);
$events = $sink->get_events();
$event = reset($events);
// Check that tag was removed from the wiki page and the event data is valid.
$this->assertEquals(0, $DB->count_records('tag_instance'));
$this->assertInstanceOf('\core\event\tag_removed', $event);
$this->assertEquals(context_module::instance($wiki->cmid), $event->get_context());
// Create another wiki.
$wiki2 = $wikigenerator->create_instance(array('course' => $course->id));
$subwikiid2 = wiki_add_subwiki($wiki2->id, 0);
$wikipageid2 = wiki_create_page($subwikiid2, 'Title', FORMAT_HTML, '2');
// Assign a tag to both wiki pages.
core_tag_tag::add_item_tag('mod_wiki', 'wiki_pages', $wikipageid, context_module::instance($wiki->cmid), $tag->rawname);
core_tag_tag::add_item_tag('mod_wiki', 'wiki_pages', $wikipageid2, context_module::instance($wiki2->cmid), $tag->rawname);
// Now remove all tag_instances associated with all wikis.
$sink = $this->redirectEvents();
core_tag_tag::delete_instances('mod_wiki');
$events = $sink->get_events();
// There will be two events - one for each wiki instance removed.
$this->assertCount(2, $events);
$contexts = [context_module::instance($wiki->cmid), context_module::instance($wiki2->cmid)];
$this->assertNotEquals($events[0]->contextid, $events[1]->contextid);
// Check that the tags were removed from the wiki pages.
$this->assertEquals(0, $DB->count_records('tag_instance'));
// Check the first event data is valid.
$this->assertInstanceOf('\core\event\tag_removed', $events[0]);
$this->assertContains($events[0]->get_context(), $contexts);
// Check that the second event data is valid.
$this->assertInstanceOf('\core\event\tag_removed', $events[1]);
$this->assertContains($events[1]->get_context(), $contexts);
}
/**
* Test the tag flagged event.
*/
public function test_tag_flagged() {
global $DB;
$this->setAdminUser();
// Create tags we are going to flag.
$tag = $this->getDataGenerator()->create_tag();
$tag2 = $this->getDataGenerator()->create_tag();
$tags = array($tag, $tag2);
// Trigger and capture the event for setting the flag of a tag.
$sink = $this->redirectEvents();
core_tag_tag::get($tag->id, '*')->flag();
$events = $sink->get_events();
$event = reset($events);
// Check that the flag was updated.
$tag = $DB->get_record('tag', array('id' => $tag->id));
$this->assertEquals(1, $tag->flag);
// Check that the event data is valid.
$this->assertInstanceOf('\core\event\tag_flagged', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
$expected = array(SITEID, 'tag', 'flag', 'index.php?id=' . $tag->id, $tag->id, '', '2');
$this->assertEventLegacyLogData($expected, $event);
// Unset the flag for both (though by default tag2 should have been created with 0 already).
foreach ($tags as $t) {
core_tag_tag::get($t->id, '*')->reset_flag();
}
// Trigger and capture the event for setting the flag for multiple tags.
$sink = $this->redirectEvents();
foreach ($tags as $t) {
core_tag_tag::get($t->id, '*')->flag();
}
$events = $sink->get_events();
// Check that the flags were updated.
$tag = $DB->get_record('tag', array('id' => $tag->id));
$this->assertEquals(1, $tag->flag);
$tag2 = $DB->get_record('tag', array('id' => $tag2->id));
$this->assertEquals(1, $tag2->flag);
// Confirm the events.
$event = $events[0];
$this->assertInstanceOf('\core\event\tag_flagged', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
$expected = array(SITEID, 'tag', 'flag', 'index.php?id=' . $tag->id, $tag->id, '', '2');
$this->assertEventLegacyLogData($expected, $event);
$event = $events[1];
$this->assertInstanceOf('\core\event\tag_flagged', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
$expected = array(SITEID, 'tag', 'flag', 'index.php?id=' . $tag2->id, $tag2->id, '', '2');
$this->assertEventLegacyLogData($expected, $event);
}
/**
* Test the tag unflagged event.
*/
public function test_tag_unflagged() {
global $DB;
$this->setAdminUser();
// Create tags we are going to unflag.
$tag = $this->getDataGenerator()->create_tag();
$tag2 = $this->getDataGenerator()->create_tag();
$tags = array($tag, $tag2);
// Flag it.
core_tag_tag::get($tag->id, '*')->flag();
// Trigger and capture the event for unsetting the flag of a tag.
$sink = $this->redirectEvents();
core_tag_tag::get($tag->id, '*')->reset_flag();
$events = $sink->get_events();
$event = reset($events);
// Check that the flag was updated.
$tag = $DB->get_record('tag', array('id' => $tag->id));
$this->assertEquals(0, $tag->flag);
// Check that the event data is valid.
$this->assertInstanceOf('\core\event\tag_unflagged', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
// Set the flag back for both.
foreach ($tags as $t) {
core_tag_tag::get($t->id, '*')->flag();
}
// Trigger and capture the event for unsetting the flag for multiple tags.
$sink = $this->redirectEvents();
foreach ($tags as $t) {
core_tag_tag::get($t->id, '*')->reset_flag();
}
$events = $sink->get_events();
// Check that the flags were updated.
$tag = $DB->get_record('tag', array('id' => $tag->id));
$this->assertEquals(0, $tag->flag);
$tag2 = $DB->get_record('tag', array('id' => $tag2->id));
$this->assertEquals(0, $tag2->flag);
// Confirm the events.
$event = $events[0];
$this->assertInstanceOf('\core\event\tag_unflagged', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
$event = $events[1];
$this->assertInstanceOf('\core\event\tag_unflagged', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
}
/**
* Test the tag deleted event
*/
public function test_tag_deleted() {
global $DB;
$this->setAdminUser();
// Create a course and a user.
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_user();
// Create tag we are going to delete.
$tag = $this->getDataGenerator()->create_tag();
// Trigger and capture the event for deleting a tag.
$sink = $this->redirectEvents();
core_tag_tag::delete_tags($tag->id);
$events = $sink->get_events();
$event = reset($events);
// Check that the tag was deleted and the event data is valid.
$this->assertEquals(0, $DB->count_records('tag'));
$this->assertInstanceOf('\core\event\tag_deleted', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
// Create two tags we are going to delete to ensure passing multiple tags work.
$tag = $this->getDataGenerator()->create_tag();
$tag2 = $this->getDataGenerator()->create_tag();
// Trigger and capture the events for deleting multiple tags.
$sink = $this->redirectEvents();
core_tag_tag::delete_tags(array($tag->id, $tag2->id));
$events = $sink->get_events();
// Check that the tags were deleted and the events data is valid.
$this->assertEquals(0, $DB->count_records('tag'));
foreach ($events as $event) {
$this->assertInstanceOf('\core\event\tag_deleted', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
}
// Add a tag instance to a course.
core_tag_tag::add_item_tag('core', 'course', $course->id, context_course::instance($course->id), 'cat', $user->id);
// Trigger and capture the event for deleting a personal tag for a user for a course.
$sink = $this->redirectEvents();
core_tag_tag::remove_item_tag('core', 'course', $course->id, 'cat', $user->id);
$events = $sink->get_events();
$event = $events[1];
// Check that the tag was deleted and the event data is valid.
$this->assertEquals(0, $DB->count_records('tag'));
$this->assertInstanceOf('\core\event\tag_deleted', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
// Add the tag instance to the course again as it was deleted.
core_tag_tag::add_item_tag('core', 'course', $course->id, context_course::instance($course->id), 'dog', $user->id);
// Trigger and capture the event for deleting all tags in a course.
$sink = $this->redirectEvents();
core_tag_tag::remove_all_item_tags('core', 'course', $course->id);
$events = $sink->get_events();
$event = $events[1];
// Check that the tag was deleted and the event data is valid.
$this->assertEquals(0, $DB->count_records('tag'));
$this->assertInstanceOf('\core\event\tag_deleted', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
// Add multiple tag instances now and check that it still works.
core_tag_tag::set_item_tags('core', 'course', $course->id, context_course::instance($course->id),
array('fish', 'hamster'), $user->id);
// Trigger and capture the event for deleting all tags in a course.
$sink = $this->redirectEvents();
core_tag_tag::remove_all_item_tags('core', 'course', $course->id);
$events = $sink->get_events();
$events = array($events[1], $events[3]);
// Check that the tags were deleted and the events data is valid.
$this->assertEquals(0, $DB->count_records('tag'));
foreach ($events as $event) {
$this->assertInstanceOf('\core\event\tag_deleted', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
}
}
/**
* Test the tag created event.
*/
public function test_tag_created() {
global $DB;
// Trigger and capture the event for creating a tag.
$sink = $this->redirectEvents();
core_tag_tag::create_if_missing(core_tag_area::get_collection('core', 'course'),
array('A really awesome tag!'));
$events = $sink->get_events();
$event = reset($events);
// Check that the tag was created and the event data is valid.
$this->assertEquals(1, $DB->count_records('tag'));
$this->assertInstanceOf('\core\event\tag_created', $event);
$this->assertEquals(context_system::instance(), $event->get_context());
}
}