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