Merge branch 'MDL-56022_m32v3' of https://github.com/sbourget/moodle
[moodle.git] / tag / tests / events_test.php
blobed3d3ec3353927543e4f411cd237196520f5aaa6
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 * Events tests.
20 * @package core_tag
21 * @category test
22 * @copyright 2014 Mark Nelson <markn@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
31 // Used to create a wiki page to tag.
32 require_once($CFG->dirroot . '/mod/wiki/locallib.php');
34 class core_tag_events_testcase extends advanced_testcase {
36 /**
37 * Test set up.
39 * This is executed before running any test in this file.
41 public function setUp() {
42 $this->resetAfterTest();
45 /**
46 * Test the tag updated event.
48 public function test_tag_updated() {
49 $this->setAdminUser();
51 // Save the system context.
52 $systemcontext = context_system::instance();
54 // Create a tag we are going to update.
55 $tag = $this->getDataGenerator()->create_tag();
57 // Store the name before we change it.
58 $oldname = $tag->name;
60 // Trigger and capture the event when renaming a tag.
61 $sink = $this->redirectEvents();
62 tag_rename($tag->id, 'newname');
63 $this->assertDebuggingCalled();
64 // Update the tag's name since we have renamed it.
65 $tag->name = 'newname';
66 $events = $sink->get_events();
67 $event = reset($events);
69 // Check that the event data is valid.
70 $this->assertInstanceOf('\core\event\tag_updated', $event);
71 $this->assertEquals($systemcontext, $event->get_context());
72 $expected = array(SITEID, 'tag', 'update', 'index.php?id=' . $tag->id, $oldname . '->'. $tag->name);
73 $this->assertEventLegacyLogData($expected, $event);
75 // Trigger and capture the event when setting the type of a tag.
76 $sink = $this->redirectEvents();
77 tag_type_set($tag->id, 'official');
78 $this->assertDebuggingCalled();
79 $events = $sink->get_events();
80 $event = reset($events);
82 // Check that the event data is valid.
83 $this->assertInstanceOf('\core\event\tag_updated', $event);
84 $this->assertEquals($systemcontext, $event->get_context());
85 $expected = array(0, 'tag', 'update', 'index.php?id=' . $tag->id, $tag->name);
86 $this->assertEventLegacyLogData($expected, $event);
88 // Trigger and capture the event for setting the description of a tag.
89 $sink = $this->redirectEvents();
90 tag_description_set($tag->id, 'description', FORMAT_MOODLE);
91 $this->assertDebuggingCalled();
92 $events = $sink->get_events();
93 $event = reset($events);
95 // Check that the event data is valid.
96 $this->assertInstanceOf('\core\event\tag_updated', $event);
97 $this->assertEquals($systemcontext, $event->get_context());
98 $expected = array(0, 'tag', 'update', 'index.php?id=' . $tag->id, $tag->name);
99 $this->assertEventLegacyLogData($expected, $event);
103 * Test the tag added event.
105 public function test_tag_added() {
106 global $DB;
108 // Create a course to tag.
109 $course = $this->getDataGenerator()->create_course();
111 // Trigger and capture the event for tagging a course.
112 $sink = $this->redirectEvents();
113 core_tag_tag::set_item_tags('core', 'course', $course->id, context_course::instance($course->id), array('A tag'));
114 $events = $sink->get_events();
115 $event = $events[1];
117 // Check that the tag was added to the course and that the event data is valid.
118 $this->assertEquals(1, $DB->count_records('tag_instance', array('component' => 'core')));
119 $this->assertInstanceOf('\core\event\tag_added', $event);
120 $this->assertEquals(context_course::instance($course->id), $event->get_context());
121 $expected = array($course->id, 'coursetags', 'add', 'tag/search.php?query=A+tag', 'Course tagged');
122 $this->assertEventLegacyLogData($expected, $event);
124 // Create a question to tag.
125 $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
126 $cat = $questiongenerator->create_question_category();
127 $question = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
129 // Trigger and capture the event for tagging a question.
130 $this->assertEquals(1, $DB->count_records('tag_instance'));
131 $sink = $this->redirectEvents();
132 core_tag_tag::set_item_tags('core_question', 'question', $question->id,
133 context::instance_by_id($cat->contextid), array('A tag'));
134 $events = $sink->get_events();
135 $event = reset($events);
137 // Check that the tag was added to the question and the event data is valid.
138 $this->assertEquals(1, $DB->count_records('tag_instance', array('component' => 'core')));
139 $this->assertInstanceOf('\core\event\tag_added', $event);
140 $this->assertEquals(context_system::instance(), $event->get_context());
141 $expected = null;
142 $this->assertEventLegacyLogData($expected, $event);
146 * Test the tag removed event.
148 public function test_tag_removed() {
149 global $DB;
151 $this->setAdminUser();
153 // Create a course to tag.
154 $course = $this->getDataGenerator()->create_course();
156 // Create a wiki page to tag.
157 $wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki');
158 $wiki = $wikigenerator->create_instance(array('course' => $course->id));
159 $subwikiid = wiki_add_subwiki($wiki->id, 0);
160 $wikipageid = wiki_create_page($subwikiid, 'Title', FORMAT_HTML, '2');
162 // Create the tag.
163 $tag = $this->getDataGenerator()->create_tag();
165 // Assign a tag to a course.
166 tag_assign('course', $course->id, $tag->id, 1, 2, 'core', context_course::instance($course->id)->id);
167 $this->assertDebuggingCalled();
169 // Trigger and capture the event for untagging a course.
170 $sink = $this->redirectEvents();
171 coursetag_delete_keyword($tag->id, 2, $course->id);
172 $this->assertDebuggingCalled();
173 $events = $sink->get_events();
174 $event = reset($events);
176 // Check that the tag was removed from the course and the event data is valid.
177 $this->assertEquals(0, $DB->count_records('tag_instance'));
178 $this->assertInstanceOf('\core\event\tag_removed', $event);
179 $this->assertEquals(context_course::instance($course->id), $event->get_context());
181 // Create the tag.
182 $tag = $this->getDataGenerator()->create_tag();
184 // Assign a tag to a wiki this time.
185 tag_assign('wiki_pages', $wikipageid, $tag->id, 1, 2, 'mod_wiki', context_module::instance($wiki->cmid)->id);
186 $this->assertDebuggingCalled();
188 // Trigger and capture the event for deleting this tag instance.
189 $sink = $this->redirectEvents();
190 tag_delete_instance('wiki_pages', $wikipageid, $tag->id);
191 $this->assertDebuggingCalled();
192 $events = $sink->get_events();
193 $event = reset($events);
195 // Check that tag was removed from the wiki page and the event data is valid.
196 $this->assertEquals(0, $DB->count_records('tag_instance'));
197 $this->assertInstanceOf('\core\event\tag_removed', $event);
198 $this->assertEquals(context_module::instance($wiki->cmid), $event->get_context());
200 // Create a tag again - the other would have been deleted since there were no more instances associated with it.
201 $tag = $this->getDataGenerator()->create_tag();
203 // Assign a tag to the wiki again.
204 tag_assign('wiki_pages', $wikipageid, $tag->id, 1, 2, 'mod_wiki', context_module::instance($wiki->cmid)->id);
205 $this->assertDebuggingCalled();
207 // Now we want to delete this tag, and because there is only one tag instance
208 // associated with it, it should get deleted as well.
209 $sink = $this->redirectEvents();
210 tag_delete($tag->id);
211 $this->assertDebuggingCalled();
212 $events = $sink->get_events();
213 $event = reset($events);
215 // Check that tag was removed from the wiki page and the event data is valid.
216 $this->assertEquals(0, $DB->count_records('tag_instance'));
217 $this->assertInstanceOf('\core\event\tag_removed', $event);
218 $this->assertEquals(context_module::instance($wiki->cmid), $event->get_context());
220 // Create a tag again - the other would have been deleted since there were no more instances associated with it.
221 $tag = $this->getDataGenerator()->create_tag();
223 // Assign a tag to the wiki again.
224 tag_assign('wiki_pages', $wikipageid, $tag->id, 1, 2, 'mod_wiki', context_module::instance($wiki->cmid)->id);
225 $this->assertDebuggingCalled();
227 // Delete all tag instances for this wiki instance.
228 $sink = $this->redirectEvents();
229 core_tag_tag::delete_instances('mod_wiki', 'wiki_pages', context_module::instance($wiki->cmid)->id);
230 $events = $sink->get_events();
231 $event = reset($events);
233 // Check that tag was removed from the wiki page and the event data is valid.
234 $this->assertEquals(0, $DB->count_records('tag_instance'));
235 $this->assertInstanceOf('\core\event\tag_removed', $event);
236 $this->assertEquals(context_module::instance($wiki->cmid), $event->get_context());
238 // Create another wiki.
239 $wiki2 = $wikigenerator->create_instance(array('course' => $course->id));
240 $subwikiid2 = wiki_add_subwiki($wiki2->id, 0);
241 $wikipageid2 = wiki_create_page($subwikiid2, 'Title', FORMAT_HTML, '2');
243 // Assign a tag to both wiki pages.
244 tag_assign('wiki_pages', $wikipageid, $tag->id, 1, 2, 'mod_wiki', context_module::instance($wiki->cmid)->id);
245 $this->assertDebuggingCalled();
246 tag_assign('wiki_pages', $wikipageid2, $tag->id, 1, 2, 'mod_wiki', context_module::instance($wiki2->cmid)->id);
247 $this->assertDebuggingCalled();
249 // Now remove all tag_instances associated with all wikis.
250 $sink = $this->redirectEvents();
251 core_tag_tag::delete_instances('mod_wiki');
252 $events = $sink->get_events();
254 // There will be two events - one for each wiki instance removed.
255 $this->assertCount(2, $events);
256 $contexts = [context_module::instance($wiki->cmid), context_module::instance($wiki2->cmid)];
257 $this->assertNotEquals($events[0]->contextid, $events[1]->contextid);
259 // Check that the tags were removed from the wiki pages.
260 $this->assertEquals(0, $DB->count_records('tag_instance'));
262 // Check the first event data is valid.
263 $this->assertInstanceOf('\core\event\tag_removed', $events[0]);
264 $this->assertContains($events[0]->get_context(), $contexts);
266 // Check that the second event data is valid.
267 $this->assertInstanceOf('\core\event\tag_removed', $events[1]);
268 $this->assertContains($events[1]->get_context(), $contexts);
272 * Test the tag flagged event.
274 public function test_tag_flagged() {
275 global $DB;
277 $this->setAdminUser();
279 // Create tags we are going to flag.
280 $tag = $this->getDataGenerator()->create_tag();
281 $tag2 = $this->getDataGenerator()->create_tag();
283 // Trigger and capture the event for setting the flag of a tag.
284 $sink = $this->redirectEvents();
285 tag_set_flag($tag->id);
286 $this->assertDebuggingCalled();
287 $events = $sink->get_events();
288 $event = reset($events);
290 // Check that the flag was updated.
291 $tag = $DB->get_record('tag', array('id' => $tag->id));
292 $this->assertEquals(1, $tag->flag);
294 // Check that the event data is valid.
295 $this->assertInstanceOf('\core\event\tag_flagged', $event);
296 $this->assertEquals(context_system::instance(), $event->get_context());
297 $expected = array(SITEID, 'tag', 'flag', 'index.php?id=' . $tag->id, $tag->id, '', '2');
298 $this->assertEventLegacyLogData($expected, $event);
300 // Unset the flag for both (though by default tag2 should have been created with 0 already).
301 tag_unset_flag(array($tag->id, $tag2->id));
302 $this->assertDebuggingCalled();
304 // Trigger and capture the event for setting the flag for multiple tags.
305 $sink = $this->redirectEvents();
306 tag_set_flag(array($tag->id, $tag2->id));
307 $this->assertDebuggingCalled();
308 $events = $sink->get_events();
310 // Check that the flags were updated.
311 $tag = $DB->get_record('tag', array('id' => $tag->id));
312 $this->assertEquals(1, $tag->flag);
313 $tag2 = $DB->get_record('tag', array('id' => $tag2->id));
314 $this->assertEquals(1, $tag2->flag);
316 // Confirm the events.
317 $event = $events[0];
318 $this->assertInstanceOf('\core\event\tag_flagged', $event);
319 $this->assertEquals(context_system::instance(), $event->get_context());
320 $expected = array(SITEID, 'tag', 'flag', 'index.php?id=' . $tag->id, $tag->id, '', '2');
321 $this->assertEventLegacyLogData($expected, $event);
323 $event = $events[1];
324 $this->assertInstanceOf('\core\event\tag_flagged', $event);
325 $this->assertEquals(context_system::instance(), $event->get_context());
326 $expected = array(SITEID, 'tag', 'flag', 'index.php?id=' . $tag2->id, $tag2->id, '', '2');
327 $this->assertEventLegacyLogData($expected, $event);
331 * Test the tag unflagged event.
333 public function test_tag_unflagged() {
334 global $DB;
336 $this->setAdminUser();
338 // Create tags we are going to unflag.
339 $tag = $this->getDataGenerator()->create_tag();
340 $tag2 = $this->getDataGenerator()->create_tag();
342 // Flag it.
343 tag_set_flag($tag->id);
344 $this->assertDebuggingCalled();
346 // Trigger and capture the event for unsetting the flag of a tag.
347 $sink = $this->redirectEvents();
348 tag_unset_flag($tag->id);
349 $this->assertDebuggingCalled();
350 $events = $sink->get_events();
351 $event = reset($events);
353 // Check that the flag was updated.
354 $tag = $DB->get_record('tag', array('id' => $tag->id));
355 $this->assertEquals(0, $tag->flag);
357 // Check that the event data is valid.
358 $this->assertInstanceOf('\core\event\tag_unflagged', $event);
359 $this->assertEquals(context_system::instance(), $event->get_context());
361 // Set the flag back for both.
362 tag_set_flag(array($tag->id, $tag2->id));
363 $this->assertDebuggingCalled();
365 // Trigger and capture the event for unsetting the flag for multiple tags.
366 $sink = $this->redirectEvents();
367 tag_unset_flag(array($tag->id, $tag2->id));
368 $this->assertDebuggingCalled();
369 $events = $sink->get_events();
371 // Check that the flags were updated.
372 $tag = $DB->get_record('tag', array('id' => $tag->id));
373 $this->assertEquals(0, $tag->flag);
374 $tag2 = $DB->get_record('tag', array('id' => $tag2->id));
375 $this->assertEquals(0, $tag2->flag);
377 // Confirm the events.
378 $event = $events[0];
379 $this->assertInstanceOf('\core\event\tag_unflagged', $event);
380 $this->assertEquals(context_system::instance(), $event->get_context());
382 $event = $events[1];
383 $this->assertInstanceOf('\core\event\tag_unflagged', $event);
384 $this->assertEquals(context_system::instance(), $event->get_context());
388 * Test the tag deleted event
390 public function test_tag_deleted() {
391 global $DB;
393 $this->setAdminUser();
395 // Create a course and a user.
396 $course = $this->getDataGenerator()->create_course();
397 $user = $this->getDataGenerator()->create_user();
399 // Create tag we are going to delete.
400 $tag = $this->getDataGenerator()->create_tag();
402 // Trigger and capture the event for deleting a tag.
403 $sink = $this->redirectEvents();
404 tag_delete($tag->id);
405 $this->assertDebuggingCalled();
406 $events = $sink->get_events();
407 $event = reset($events);
409 // Check that the tag was deleted and the event data is valid.
410 $this->assertEquals(0, $DB->count_records('tag'));
411 $this->assertInstanceOf('\core\event\tag_deleted', $event);
412 $this->assertEquals(context_system::instance(), $event->get_context());
414 // Create two tags we are going to delete to ensure passing multiple tags work.
415 $tag = $this->getDataGenerator()->create_tag();
416 $tag2 = $this->getDataGenerator()->create_tag();
418 // Trigger and capture the events for deleting multiple tags.
419 $sink = $this->redirectEvents();
420 tag_delete(array($tag->id, $tag2->id));
421 $this->assertDebuggingCalled();
422 $events = $sink->get_events();
424 // Check that the tags were deleted and the events data is valid.
425 $this->assertEquals(0, $DB->count_records('tag'));
426 foreach ($events as $event) {
427 $this->assertInstanceOf('\core\event\tag_deleted', $event);
428 $this->assertEquals(context_system::instance(), $event->get_context());
431 // Add a tag instance to a course.
432 core_tag_tag::add_item_tag('core', 'course', $course->id, context_course::instance($course->id), 'cat', $user->id);
434 // Trigger and capture the event for deleting a personal tag for a user for a course.
435 $sink = $this->redirectEvents();
436 core_tag_tag::remove_item_tag('core', 'course', $course->id, 'cat', $user->id);
437 $events = $sink->get_events();
438 $event = $events[1];
440 // Check that the tag was deleted and the event data is valid.
441 $this->assertEquals(0, $DB->count_records('tag'));
442 $this->assertInstanceOf('\core\event\tag_deleted', $event);
443 $this->assertEquals(context_system::instance(), $event->get_context());
445 // Add the tag instance to the course again as it was deleted.
446 core_tag_tag::add_item_tag('core', 'course', $course->id, context_course::instance($course->id), 'dog', $user->id);
448 // Trigger and capture the event for deleting all tags in a course.
449 $sink = $this->redirectEvents();
450 core_tag_tag::remove_all_item_tags('core', 'course', $course->id);
451 $events = $sink->get_events();
452 $event = $events[1];
454 // Check that the tag was deleted and the event data is valid.
455 $this->assertEquals(0, $DB->count_records('tag'));
456 $this->assertInstanceOf('\core\event\tag_deleted', $event);
457 $this->assertEquals(context_system::instance(), $event->get_context());
459 // Add multiple tag instances now and check that it still works.
460 core_tag_tag::set_item_tags('core', 'course', $course->id, context_course::instance($course->id),
461 array('fish', 'hamster'), $user->id);
463 // Trigger and capture the event for deleting all tags in a course.
464 $sink = $this->redirectEvents();
465 core_tag_tag::remove_all_item_tags('core', 'course', $course->id);
466 $events = $sink->get_events();
467 $events = array($events[1], $events[3]);
469 // Check that the tags were deleted and the events data is valid.
470 $this->assertEquals(0, $DB->count_records('tag'));
471 foreach ($events as $event) {
472 $this->assertInstanceOf('\core\event\tag_deleted', $event);
473 $this->assertEquals(context_system::instance(), $event->get_context());
478 * Test the tag deleted event while calling deprecated functions.
479 * Remove the test when the functions are removed.
481 public function test_tag_deleted_deprecated() {
482 global $DB;
484 $this->setAdminUser();
486 // Create a course.
487 $course = $this->getDataGenerator()->create_course();
489 // Create another tag to delete.
490 $tag = $this->getDataGenerator()->create_tag();
492 // Add a tag instance to a course.
493 tag_assign('course', $course->id, $tag->id, 0, 2, 'core', context_course::instance($course->id)->id);
494 $this->assertDebuggingCalled();
496 // Trigger and capture the event for deleting a personal tag for a user for a course.
497 $sink = $this->redirectEvents();
498 coursetag_delete_keyword($tag->id, 2, $course->id);
499 $this->assertDebuggingCalled();
500 $events = $sink->get_events();
501 $event = $events[1];
503 // Check that the tag was deleted and the event data is valid.
504 $this->assertEquals(0, $DB->count_records('tag'));
505 $this->assertInstanceOf('\core\event\tag_deleted', $event);
506 $this->assertEquals(context_system::instance(), $event->get_context());
508 // Create a new tag we are going to delete.
509 $tag = $this->getDataGenerator()->create_tag();
511 // Add the tag instance to the course again as it was deleted.
512 tag_assign('course', $course->id, $tag->id, 0, 2, 'core', context_course::instance($course->id)->id);
513 $this->assertDebuggingCalled();
515 // Trigger and capture the event for deleting all tags in a course.
516 $sink = $this->redirectEvents();
517 coursetag_delete_course_tags($course->id);
518 $this->assertDebuggingCalled();
519 $events = $sink->get_events();
520 $event = $events[1];
522 // Check that the tag was deleted and the event data is valid.
523 $this->assertEquals(0, $DB->count_records('tag'));
524 $this->assertInstanceOf('\core\event\tag_deleted', $event);
525 $this->assertEquals(context_system::instance(), $event->get_context());
527 // Create two tags we are going to delete to ensure passing multiple tags work.
528 $tag = $this->getDataGenerator()->create_tag();
529 $tag2 = $this->getDataGenerator()->create_tag();
531 // Add multiple tag instances now and check that it still works.
532 tag_assign('course', $course->id, $tag->id, 0, 2, 'core', context_course::instance($course->id)->id);
533 $this->assertDebuggingCalled();
534 tag_assign('course', $course->id, $tag2->id, 0, 2, 'core', context_course::instance($course->id)->id);
535 $this->assertDebuggingCalled();
537 // Trigger and capture the event for deleting all tags in a course.
538 $sink = $this->redirectEvents();
539 coursetag_delete_course_tags($course->id);
540 $this->assertDebuggingCalled();
541 $events = $sink->get_events();
542 $events = array($events[1], $events[3]);
544 // Check that the tags were deleted and the events data is valid.
545 $this->assertEquals(0, $DB->count_records('tag'));
546 foreach ($events as $event) {
547 $this->assertInstanceOf('\core\event\tag_deleted', $event);
548 $this->assertEquals(context_system::instance(), $event->get_context());
553 * Test the tag created event.
555 public function test_tag_created() {
556 global $DB;
558 // Trigger and capture the event for creating a tag.
559 $sink = $this->redirectEvents();
560 core_tag_tag::create_if_missing(core_tag_area::get_collection('core', 'course'),
561 array('A really awesome tag!'));
562 $events = $sink->get_events();
563 $event = reset($events);
565 // Check that the tag was created and the event data is valid.
566 $this->assertEquals(1, $DB->count_records('tag'));
567 $this->assertInstanceOf('\core\event\tag_created', $event);
568 $this->assertEquals(context_system::instance(), $event->get_context());