2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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();
30 require_once($CFG->dirroot . '/tag/lib.php');
31 require_once($CFG->dirroot . '/tag/coursetagslib.php');
33 class core_tag_events_testcase extends advanced_testcase {
38 * This is executed before running any test in this file.
40 public function setUp() {
41 $this->resetAfterTest();
45 * Test the tag updated event.
47 public function test_tag_updated() {
48 $this->setAdminUser();
50 // Save the system context.
51 $systemcontext = context_system::instance();
53 // Create a tag we are going to update.
54 $tag = $this->getDataGenerator()->create_tag();
56 // Store the name before we change it.
57 $oldname = $tag->name;
59 // Trigger and capture the event when renaming a tag.
60 $sink = $this->redirectEvents();
61 tag_rename($tag->id, 'newname');
62 // Update the tag's name since we have renamed it.
63 $tag->name = 'newname';
64 $events = $sink->get_events();
65 $event = reset($events);
67 // Check that the event data is valid.
68 $this->assertInstanceOf('\core\event\tag_updated', $event);
69 $this->assertEquals($systemcontext, $event->get_context());
70 $expected = array(SITEID, 'tag', 'update', 'index.php?id=' . $tag->id, $oldname . '->'. $tag->name);
71 $this->assertEventLegacyLogData($expected, $event);
73 // Trigger and capture the event when setting the type of a tag.
74 $sink = $this->redirectEvents();
75 tag_type_set($tag->id, 'official');
76 $events = $sink->get_events();
77 $event = reset($events);
79 // Check that the event data is valid.
80 $this->assertInstanceOf('\core\event\tag_updated', $event);
81 $this->assertEquals($systemcontext, $event->get_context());
82 $expected = array(0, 'tag', 'update', 'index.php?id=' . $tag->id, $tag->name);
83 $this->assertEventLegacyLogData($expected, $event);
85 // Trigger and capture the event for setting the description of a tag.
86 $sink = $this->redirectEvents();
87 tag_description_set($tag->id, 'description', FORMAT_MOODLE);
88 $events = $sink->get_events();
89 $event = reset($events);
91 // Check that the event data is valid.
92 $this->assertInstanceOf('\core\event\tag_updated', $event);
93 $this->assertEquals($systemcontext, $event->get_context());
94 $expected = array(0, 'tag', 'update', 'index.php?id=' . $tag->id, $tag->name);
95 $this->assertEventLegacyLogData($expected, $event);
99 * Test the item tagged event.
101 public function test_item_tagged() {
104 // Create a course to tag.
105 $course = $this->getDataGenerator()->create_course();
107 // Trigger and capture the event for tagging a course.
108 $sink = $this->redirectEvents();
109 tag_set('course', $course->id, array('A tag'), 'core', context_course::instance($course->id)->id);
110 $events = $sink->get_events();
111 $event = reset($events);
113 // Check that the course was tagged and that the event data is valid.
114 $this->assertEquals(1, $DB->count_records('tag_instance', array('component' => 'core')));
115 $this->assertInstanceOf('\core\event\item_tagged', $event);
116 $this->assertEquals(context_course::instance($course->id), $event->get_context());
117 $expected = array($course->id, 'coursetags', 'add', 'tag/search.php?query=A+tag', 'Course tagged');
118 $this->assertEventLegacyLogData($expected, $event);
120 // Create a question to tag.
121 $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
122 $cat = $questiongenerator->create_question_category();
123 $question = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
125 // Trigger and capture the event for tagging a question.
126 $this->assertEquals(1, $DB->count_records('tag_instance'));
127 $sink = $this->redirectEvents();
128 tag_set('question', $question->id, array('A tag'), 'core_question', $cat->contextid);
129 $events = $sink->get_events();
130 $event = reset($events);
132 // Check that the question was tagged and the event data is valid.
133 $this->assertEquals(1, $DB->count_records('tag_instance', array('component' => 'core')));
134 $this->assertInstanceOf('\core\event\item_tagged', $event);
135 $this->assertEquals(context_system::instance(), $event->get_context());
137 $this->assertEventLegacyLogData($expected, $event);
141 * Test the tag flagged event.
143 public function test_tag_flagged() {
146 $this->setAdminUser();
148 // Create tags we are going to flag.
149 $tag = $this->getDataGenerator()->create_tag();
150 $tag2 = $this->getDataGenerator()->create_tag();
152 // Trigger and capture the event for setting the flag of a tag.
153 $sink = $this->redirectEvents();
154 tag_set_flag($tag->id);
155 $events = $sink->get_events();
156 $event = reset($events);
158 // Check that the flag was updated.
159 $tag = $DB->get_record('tag', array('id' => $tag->id));
160 $this->assertEquals(1, $tag->flag);
162 // Check that the event data is valid.
163 $this->assertInstanceOf('\core\event\tag_flagged', $event);
164 $this->assertEquals(context_system::instance(), $event->get_context());
165 $expected = array(SITEID, 'tag', 'flag', 'index.php?id=' . $tag->id, $tag->id, '', '2');
166 $this->assertEventLegacyLogData($expected, $event);
168 // Unset the flag for both (though by default tag2 should have been created with 0 already).
169 tag_unset_flag(array($tag->id, $tag2->id));
171 // Trigger and capture the event for setting the flag for multiple tags.
172 $sink = $this->redirectEvents();
173 tag_set_flag(array($tag->id, $tag2->id));
174 $events = $sink->get_events();
176 // Check that the flags were updated.
177 $tag = $DB->get_record('tag', array('id' => $tag->id));
178 $this->assertEquals(1, $tag->flag);
179 $tag2 = $DB->get_record('tag', array('id' => $tag2->id));
180 $this->assertEquals(1, $tag2->flag);
182 // Confirm the events.
184 $this->assertInstanceOf('\core\event\tag_flagged', $event);
185 $this->assertEquals(context_system::instance(), $event->get_context());
186 $expected = array(SITEID, 'tag', 'flag', 'index.php?id=' . $tag->id, $tag->id, '', '2');
187 $this->assertEventLegacyLogData($expected, $event);
190 $this->assertInstanceOf('\core\event\tag_flagged', $event);
191 $this->assertEquals(context_system::instance(), $event->get_context());
192 $expected = array(SITEID, 'tag', 'flag', 'index.php?id=' . $tag2->id, $tag2->id, '', '2');
193 $this->assertEventLegacyLogData($expected, $event);
197 * Test the tag unflagged event.
199 public function test_tag_unflagged() {
202 $this->setAdminUser();
204 // Create tags we are going to unflag.
205 $tag = $this->getDataGenerator()->create_tag();
206 $tag2 = $this->getDataGenerator()->create_tag();
209 tag_set_flag($tag->id);
211 // Trigger and capture the event for unsetting the flag of a tag.
212 $sink = $this->redirectEvents();
213 tag_unset_flag($tag->id);
214 $events = $sink->get_events();
215 $event = reset($events);
217 // Check that the flag was updated.
218 $tag = $DB->get_record('tag', array('id' => $tag->id));
219 $this->assertEquals(0, $tag->flag);
221 // Check that the event data is valid.
222 $this->assertInstanceOf('\core\event\tag_unflagged', $event);
223 $this->assertEquals(context_system::instance(), $event->get_context());
225 // Set the flag back for both.
226 tag_set_flag(array($tag->id, $tag2->id));
228 // Trigger and capture the event for unsetting the flag for multiple tags.
229 $sink = $this->redirectEvents();
230 tag_unset_flag(array($tag->id, $tag2->id));
231 $events = $sink->get_events();
233 // Check that the flags were updated.
234 $tag = $DB->get_record('tag', array('id' => $tag->id));
235 $this->assertEquals(0, $tag->flag);
236 $tag2 = $DB->get_record('tag', array('id' => $tag2->id));
237 $this->assertEquals(0, $tag2->flag);
239 // Confirm the events.
241 $this->assertInstanceOf('\core\event\tag_unflagged', $event);
242 $this->assertEquals(context_system::instance(), $event->get_context());
245 $this->assertInstanceOf('\core\event\tag_unflagged', $event);
246 $this->assertEquals(context_system::instance(), $event->get_context());
250 * Test the tag deleted event.
252 public function test_tag_deleted() {
255 $this->setAdminUser();
258 $course = $this->getDataGenerator()->create_course();
260 // Create tag we are going to delete.
261 $tag = $this->getDataGenerator()->create_tag();
263 // Trigger and capture the event for deleting a tag.
264 $sink = $this->redirectEvents();
265 tag_delete($tag->id);
266 $events = $sink->get_events();
267 $event = reset($events);
269 // Check that the tag was deleted and the event data is valid.
270 $this->assertEquals(0, $DB->count_records('tag'));
271 $this->assertInstanceOf('\core\event\tag_deleted', $event);
272 $this->assertEquals(context_system::instance(), $event->get_context());
274 // Create two tags we are going to delete to ensure passing multiple tags work.
275 $tag = $this->getDataGenerator()->create_tag();
276 $tag2 = $this->getDataGenerator()->create_tag();
278 // Trigger and capture the events for deleting multiple tags.
279 $sink = $this->redirectEvents();
280 tag_delete(array($tag->id, $tag2->id));
281 $events = $sink->get_events();
283 // Check that the tags were deleted and the events data is valid.
284 $this->assertEquals(0, $DB->count_records('tag'));
285 foreach ($events as $event) {
286 $this->assertInstanceOf('\core\event\tag_deleted', $event);
287 $this->assertEquals(context_system::instance(), $event->get_context());
290 // Create another tag to delete.
291 $tag = $this->getDataGenerator()->create_tag();
293 // Add a tag instance to a course.
294 tag_assign('course', $course->id, $tag->id, 0, 2, 'course', context_course::instance($course->id)->id);
296 // Trigger and capture the event for deleting a personal tag for a user for a course.
297 $sink = $this->redirectEvents();
298 coursetag_delete_keyword($tag->id, 2, $course->id);
299 $events = $sink->get_events();
300 $event = reset($events);
302 // Check that the tag was deleted and the event data is valid.
303 $this->assertEquals(0, $DB->count_records('tag'));
304 $this->assertInstanceOf('\core\event\tag_deleted', $event);
305 $this->assertEquals(context_system::instance(), $event->get_context());
307 // Create a new tag we are going to delete.
308 $tag = $this->getDataGenerator()->create_tag();
310 // Add the tag instance to the course again as it was deleted.
311 tag_assign('course', $course->id, $tag->id, 0, 2, 'course', context_course::instance($course->id)->id);
313 // Trigger and capture the event for deleting all tags in a course.
314 $sink = $this->redirectEvents();
315 coursetag_delete_course_tags($course->id);
316 $events = $sink->get_events();
317 $event = reset($events);
319 // Check that the tag was deleted and the event data is valid.
320 $this->assertEquals(0, $DB->count_records('tag'));
321 $this->assertInstanceOf('\core\event\tag_deleted', $event);
322 $this->assertEquals(context_system::instance(), $event->get_context());
324 // Create two tags we are going to delete to ensure passing multiple tags work.
325 $tag = $this->getDataGenerator()->create_tag();
326 $tag2 = $this->getDataGenerator()->create_tag();
328 // Add multiple tag instances now and check that it still works.
329 tag_assign('course', $course->id, $tag->id, 0, 2, 'course', context_course::instance($course->id)->id);
330 tag_assign('course', $course->id, $tag2->id, 0, 2, 'course', context_course::instance($course->id)->id);
332 // Trigger and capture the event for deleting all tags in a course.
333 $sink = $this->redirectEvents();
334 coursetag_delete_course_tags($course->id);
335 $events = $sink->get_events();
337 // Check that the tags were deleted and the events data is valid.
338 $this->assertEquals(0, $DB->count_records('tag'));
339 foreach ($events as $event) {
340 $this->assertInstanceOf('\core\event\tag_deleted', $event);
341 $this->assertEquals(context_system::instance(), $event->get_context());