MDL-67673 phpunit: Fix the return type of template methods
[moodle.git] / mod / wiki / tests / events_test.php
blob2714ab263ee01d311bbd7ce8e63954f2f2460709
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 mod_wiki
21 * @category phpunit
22 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->dirroot.'/mod/wiki/locallib.php');
29 /**
30 * Events tests class.
32 * @package mod_wiki
33 * @category phpunit
34 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class mod_wiki_events_testcase extends advanced_testcase {
38 private $course;
39 private $wiki;
40 private $wikigenerator;
41 private $student;
42 private $teacher;
44 /**
45 * Setup test data.
47 public function setUp(): void {
48 global $DB;
50 $this->resetAfterTest();
51 // Create course and wiki.
52 $this->course = $this->getDataGenerator()->create_course();
53 $this->wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $this->course->id));
54 $this->wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki');
56 // Create student and teacher in course.
57 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
58 $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
59 $this->student = $this->getDataGenerator()->create_user();
60 $this->teacher = $this->getDataGenerator()->create_user();
61 $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $studentrole->id);
62 $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $teacherrole->id);
63 $this->setAdminUser();
66 /**
67 * Test comment_created event.
69 public function test_comment_created() {
70 $this->setUp();
72 $page = $this->wikigenerator->create_first_page($this->wiki);
73 $context = context_module::instance($this->wiki->cmid);
75 // Triggering and capturing the event.
76 $sink = $this->redirectEvents();
77 wiki_add_comment($context, $page->id, 'Test comment', $this->wiki->defaultformat);
78 $events = $sink->get_events();
79 $this->assertCount(1, $events);
80 $event = reset($events);
82 // Checking that the event contains the expected values.
83 $this->assertInstanceOf('\mod_wiki\event\comment_created', $event);
84 $this->assertEquals($context, $event->get_context());
85 $this->assertEquals($page->id, $event->other['itemid']);
86 $this->assertEventContextNotUsed($event);
89 /**
90 * Test comment_deleted event.
92 public function test_comment_deleted() {
93 $this->setUp();
95 $page = $this->wikigenerator->create_first_page($this->wiki);
96 $context = context_module::instance($this->wiki->cmid);
98 // Add comment so we can delete it later.
99 wiki_add_comment($context, $page->id, 'Test comment', 'html');
100 $comment = wiki_get_comments($context->id, $page->id);
101 $this->assertCount(1, $comment);
102 $comment = array_shift($comment);
104 // Triggering and capturing the event.
105 $sink = $this->redirectEvents();
106 wiki_delete_comment($comment->id, $context, $page->id);
107 $events = $sink->get_events();
108 $this->assertCount(1, $events);
109 $event = reset($events);
111 // Checking that the event contains the expected values.
112 $this->assertInstanceOf('\mod_wiki\event\comment_deleted', $event);
113 $this->assertEquals($context, $event->get_context());
114 $this->assertEquals($page->id, $event->other['itemid']);
115 $this->assertEventContextNotUsed($event);
119 * Test comment_viewed event.
121 public function test_comment_viewed() {
122 // There is no proper API to call or trigger this event, so simulating event
123 // to check if event returns the right information.
125 $this->setUp();
126 $page = $this->wikigenerator->create_first_page($this->wiki);
127 $context = context_module::instance($this->wiki->cmid);
129 $params = array(
130 'context' => $context,
131 'objectid' => $page->id
133 $event = \mod_wiki\event\comments_viewed::create($params);
135 // Triggering and capturing the event.
136 $sink = $this->redirectEvents();
137 $event->trigger();
138 $events = $sink->get_events();
139 $this->assertCount(1, $events);
140 $event = reset($events);
142 // Checking that the event contains the expected values.
143 $this->assertInstanceOf('\mod_wiki\event\comments_viewed', $event);
144 $this->assertEquals($context, $event->get_context());
145 $expected = array($this->course->id, 'wiki', 'comments', 'comments.php?pageid=' . $page->id, $page->id, $this->wiki->cmid);
146 $this->assertEventLegacyLogData($expected, $event);
147 $this->assertEventContextNotUsed($event);
151 * Test instances_list_viewed event.
153 public function test_course_module_instance_list_viewed() {
154 // There is no proper API to call or trigger this event, so simulating event
155 // to check if event returns the right information.
157 $this->setUp();
158 $context = context_course::instance($this->course->id);
160 $params = array('context' => $context);
161 $event = \mod_wiki\event\course_module_instance_list_viewed::create($params);
163 // Triggering and capturing the event.
164 $sink = $this->redirectEvents();
165 $event->trigger();
166 $events = $sink->get_events();
167 $this->assertCount(1, $events);
168 $event = reset($events);
170 // Checking that the event contains the expected values.
171 $this->assertInstanceOf('\mod_wiki\event\course_module_instance_list_viewed', $event);
172 $this->assertEquals($context, $event->get_context());
173 $expected = array($this->course->id, 'wiki', 'view all', 'index.php?id=' . $this->course->id, '');
174 $this->assertEventLegacyLogData($expected, $event);
175 $this->assertEventContextNotUsed($event);
179 * Test course_module_viewed event.
181 public function test_course_module_viewed() {
182 // There is no proper API to call or trigger this event, so simulating event
183 // to check if event returns the right information.
185 $this->setUp();
186 $context = context_module::instance($this->wiki->cmid);
188 $params = array(
189 'context' => $context,
190 'objectid' => $this->wiki->id
192 $event = \mod_wiki\event\course_module_viewed::create($params);
194 // Triggering and capturing the event.
195 $sink = $this->redirectEvents();
196 $event->trigger();
197 $events = $sink->get_events();
198 $this->assertCount(1, $events);
199 $event = reset($events);
201 // Checking that the event contains the expected values.
202 $this->assertInstanceOf('\mod_wiki\event\course_module_viewed', $event);
203 $this->assertEquals($context, $event->get_context());
204 $this->assertEquals($this->wiki->id, $event->objectid);
205 $expected = array($this->course->id, 'wiki', 'view', 'view.php?id=' . $this->wiki->cmid,
206 $this->wiki->id, $this->wiki->cmid);
207 $this->assertEventLegacyLogData($expected, $event);
208 $this->assertEventContextNotUsed($event);
212 * Test page_viewed event.
214 public function test_page_viewed() {
215 // There is no proper API to call or trigger this event, so simulating event
216 // to check if event returns the right information.
218 $this->setUp();
220 $page = $this->wikigenerator->create_first_page($this->wiki);
221 $context = context_module::instance($this->wiki->cmid);
223 $params = array(
224 'context' => $context,
225 'objectid' => $page->id
227 $event = \mod_wiki\event\page_viewed::create($params);
229 // Triggering and capturing the event.
230 $sink = $this->redirectEvents();
231 $event->trigger();
232 $events = $sink->get_events();
233 $this->assertCount(1, $events);
234 $event = reset($events);
236 // Checking that the event contains the expected values.
237 $this->assertInstanceOf('\mod_wiki\event\page_viewed', $event);
238 $this->assertEquals($context, $event->get_context());
239 $this->assertEquals($page->id, $event->objectid);
240 $expected = array($this->course->id, 'wiki', 'view', 'view.php?pageid=' . $page->id, $page->id, $this->wiki->cmid);
241 $this->assertEventLegacyLogData($expected, $event);
242 $this->assertEventContextNotUsed($event);
246 * Test page_viewed event for prettypage view.
248 public function test_pretty_page_viewed() {
249 // There is no proper API to call or trigger this event, so simulating event
250 // to check if event returns the right information.
252 $this->setUp();
254 $page = $this->wikigenerator->create_first_page($this->wiki);
255 $context = context_module::instance($this->wiki->cmid);
257 $params = array(
258 'context' => $context,
259 'objectid' => $page->id,
260 'other' => array('prettyview' => true)
262 $event = \mod_wiki\event\page_viewed::create($params);
264 // Triggering and capturing the event.
265 $sink = $this->redirectEvents();
266 $event->trigger();
267 $events = $sink->get_events();
268 $this->assertCount(1, $events);
269 $event = reset($events);
271 // Checking that the event contains the expected values.
272 $this->assertInstanceOf('\mod_wiki\event\page_viewed', $event);
273 $this->assertEquals($context, $event->get_context());
274 $this->assertEquals($page->id, $event->objectid);
275 $expected = array($this->course->id, 'wiki', 'view', 'prettyview.php?pageid=' . $page->id, $page->id, $this->wiki->cmid);
276 $this->assertEventLegacyLogData($expected, $event);
277 $this->assertEventContextNotUsed($event);
281 * Test page_created event.
283 public function test_page_created() {
284 global $USER;
286 $this->setUp();
288 $context = context_module::instance($this->wiki->cmid);
290 // Triggering and capturing the event.
291 $sink = $this->redirectEvents();
292 $page = $this->wikigenerator->create_first_page($this->wiki);
293 $events = $sink->get_events();
294 $this->assertCount(2, $events);
295 $event = reset($events);
297 // Checking that the event contains the expected values.
298 $this->assertInstanceOf('\mod_wiki\event\page_created', $event);
299 $this->assertEquals($context, $event->get_context());
300 $this->assertEquals($page->id, $event->objectid);
301 $expected = array($this->course->id, 'wiki', 'add page',
302 'view.php?pageid=' . $page->id, $page->id, $this->wiki->cmid);
303 $this->assertEventLegacyLogData($expected, $event);
304 $this->assertEventContextNotUsed($event);
308 * Test page_deleted and page_version_deleted and page_locks_deleted event.
310 public function test_page_deleted() {
311 global $DB;
313 $this->setUp();
315 $page = $this->wikigenerator->create_first_page($this->wiki);
316 $context = context_module::instance($this->wiki->cmid);
317 $oldversions = $DB->get_records('wiki_versions', array('pageid' => $page->id));
318 $oldversion = array_shift($oldversions);
320 // Triggering and capturing the event.
321 $sink = $this->redirectEvents();
322 wiki_delete_pages($context, array($page->id));
323 $events = $sink->get_events();
324 $this->assertCount(4, $events);
325 $event = array_shift($events);
327 // Checking that the event contains the page_version_deleted event.
328 $this->assertInstanceOf('\mod_wiki\event\page_version_deleted', $event);
329 $this->assertEquals($context, $event->get_context());
330 $this->assertEquals($page->id, $event->other['pageid']);
331 $this->assertEquals($oldversion->id, $event->objectid);
332 $expected = array($this->course->id, 'wiki', 'admin', 'admin.php?pageid=' . $page->id, $page->id, $this->wiki->cmid);
333 $this->assertEventLegacyLogData($expected, $event);
335 // Checking that the event contains the page_deleted event.
336 $event = array_pop($events);
337 $this->assertInstanceOf('\mod_wiki\event\page_deleted', $event);
338 $this->assertEquals($context, $event->get_context());
339 $this->assertEquals($page->id, $event->objectid);
340 $expected = array($this->course->id, 'wiki', 'admin', 'admin.php?pageid=' . $page->id, $page->id, $this->wiki->cmid);
341 $this->assertEventLegacyLogData($expected, $event);
343 // Checking that the event contains the expected values.
344 $event = array_pop($events);
345 $this->assertInstanceOf('\mod_wiki\event\page_locks_deleted', $event);
346 $this->assertEquals($context, $event->get_context());
347 $this->assertEquals($page->id, $event->objectid);
348 $expected = array($this->course->id, 'wiki', 'overridelocks', 'view.php?pageid=' . $page->id, $page->id, $this->wiki->cmid);
349 $this->assertEventLegacyLogData($expected, $event);
351 // Delete all pages.
352 $page1 = $this->wikigenerator->create_first_page($this->wiki);
353 $page2 = $this->wikigenerator->create_content($this->wiki);
354 $page3 = $this->wikigenerator->create_content($this->wiki, array('title' => 'Custom title'));
356 // Triggering and capturing the event.
357 $sink = $this->redirectEvents();
358 wiki_delete_pages($context, array($page1->id, $page2->id));
359 $events = $sink->get_events();
360 $this->assertCount(8, $events);
361 $event = array_pop($events);
363 // Checking that the event contains the expected values.
364 $this->assertInstanceOf('\mod_wiki\event\page_deleted', $event);
365 $this->assertEquals($context, $event->get_context());
366 $this->assertEquals($page2->id, $event->objectid);
367 $expected = array($this->course->id, 'wiki', 'admin', 'admin.php?pageid=' . $page2->id, $page2->id, $this->wiki->cmid);
368 $this->assertEventLegacyLogData($expected, $event);
369 $this->assertEventContextNotUsed($event);
373 * Test page_updated event.
375 public function test_page_updated() {
376 global $USER;
378 $this->setUp();
380 $page = $this->wikigenerator->create_first_page($this->wiki);
381 $context = context_module::instance($this->wiki->cmid);
383 // Triggering and capturing the event.
384 $sink = $this->redirectEvents();
385 wiki_save_page($page, 'New content', $USER->id);
386 $events = $sink->get_events();
387 $this->assertCount(1, $events);
388 $event = reset($events);
390 // Checking that the event contains the expected values.
391 $this->assertInstanceOf('\mod_wiki\event\page_updated', $event);
392 $this->assertEquals($context, $event->get_context());
393 $this->assertEquals($page->id, $event->objectid);
394 $expected = array($this->course->id, 'wiki', 'edit',
395 'view.php?pageid=' . $page->id, $page->id, $this->wiki->cmid);
396 $this->assertEventLegacyLogData($expected, $event);
397 $this->assertEventContextNotUsed($event);
401 * Test page_diff_viewed event.
403 public function test_page_diff_viewed() {
404 // There is no proper API to call or trigger this event, so simulating event
405 // to check if event returns the right information.
407 $this->setUp();
409 $page = $this->wikigenerator->create_first_page($this->wiki);
410 $context = context_module::instance($this->wiki->cmid);
412 $params = array(
413 'context' => $context,
414 'objectid' => $page->id,
415 'other' => array(
416 'comparewith' => 1,
417 'compare' => 2
420 $event = \mod_wiki\event\page_diff_viewed::create($params);
422 // Triggering and capturing the event.
423 $sink = $this->redirectEvents();
424 $event->trigger();
425 $events = $sink->get_events();
426 $this->assertCount(1, $events);
427 $event = reset($events);
429 // Checking that the event contains the expected values.
430 $this->assertInstanceOf('\mod_wiki\event\page_diff_viewed', $event);
431 $this->assertEquals($context, $event->get_context());
432 $this->assertEquals($page->id, $event->objectid);
433 $expected = array($this->course->id, 'wiki', 'diff', 'diff.php?pageid=' . $page->id . '&comparewith=' .
434 1 . '&compare=' . 2, $page->id, $this->wiki->cmid);
435 $this->assertEventLegacyLogData($expected, $event);
436 $this->assertEventContextNotUsed($event);
440 * Test page_history_viewed event.
442 public function test_page_history_viewed() {
443 // There is no proper API to call or trigger this event, so simulating event
444 // to check if event returns the right information.
446 $this->setUp();
448 $page = $this->wikigenerator->create_first_page($this->wiki);
449 $context = context_module::instance($this->wiki->cmid);
451 $params = array(
452 'context' => $context,
453 'objectid' => $page->id
455 $event = \mod_wiki\event\page_history_viewed::create($params);
457 // Triggering and capturing the event.
458 $sink = $this->redirectEvents();
459 $event->trigger();
460 $events = $sink->get_events();
461 $this->assertCount(1, $events);
462 $event = reset($events);
464 // Checking that the event contains the expected values.
465 $this->assertInstanceOf('\mod_wiki\event\page_history_viewed', $event);
466 $this->assertEquals($context, $event->get_context());
467 $this->assertEquals($page->id, $event->objectid);
468 $expected = array($this->course->id, 'wiki', 'history', 'history.php?pageid=' . $page->id, $page->id, $this->wiki->cmid);
469 $this->assertEventLegacyLogData($expected, $event);
470 $this->assertEventContextNotUsed($event);
474 * Test page_map_viewed event.
476 public function test_page_map_viewed() {
477 // There is no proper API to call or trigger this event, so simulating event
478 // to check if event returns the right information.
480 $this->setUp();
482 $page = $this->wikigenerator->create_first_page($this->wiki);
483 $context = context_module::instance($this->wiki->cmid);
485 $params = array(
486 'context' => $context,
487 'objectid' => $page->id,
488 'other' => array(
489 'option' => 0
492 $event = \mod_wiki\event\page_map_viewed::create($params);
494 // Triggering and capturing the event.
495 $sink = $this->redirectEvents();
496 $event->trigger();
497 $events = $sink->get_events();
498 $this->assertCount(1, $events);
499 $event = reset($events);
501 // Checking that the event contains the expected values.
502 $this->assertInstanceOf('\mod_wiki\event\page_map_viewed', $event);
503 $this->assertEquals($context, $event->get_context());
504 $this->assertEquals($page->id, $event->objectid);
505 $this->assertEquals(0, $event->other['option']);
506 $expected = array($this->course->id, 'wiki', 'map', 'map.php?pageid=' . $page->id, $page->id, $this->wiki->cmid);
507 $this->assertEventLegacyLogData($expected, $event);
508 $this->assertEventContextNotUsed($event);
512 * Test page_version_viewed event.
514 public function test_page_version_viewed() {
515 // There is no proper API to call or trigger this event, so simulating event
516 // to check if event returns the right information.
518 $this->setUp();
520 $page = $this->wikigenerator->create_first_page($this->wiki);
521 $context = context_module::instance($this->wiki->cmid);
523 $params = array(
524 'context' => $context,
525 'objectid' => $page->id,
526 'other' => array(
527 'versionid' => 1
530 $event = \mod_wiki\event\page_version_viewed::create($params);
532 // Triggering and capturing the event.
533 $sink = $this->redirectEvents();
534 $event->trigger();
535 $events = $sink->get_events();
536 $this->assertCount(1, $events);
537 $event = reset($events);
539 // Checking that the event contains the expected values.
540 $this->assertInstanceOf('\mod_wiki\event\page_version_viewed', $event);
541 $this->assertEquals($context, $event->get_context());
542 $this->assertEquals($page->id, $event->objectid);
543 $this->assertEquals(1, $event->other['versionid']);
544 $expected = array($this->course->id, 'wiki', 'history', 'viewversion.php?pageid=' . $page->id . '&versionid=1',
545 $page->id, $this->wiki->cmid);
546 $this->assertEventLegacyLogData($expected, $event);
547 $this->assertEventContextNotUsed($event);
551 * Test page_version_restored event.
553 public function test_page_version_restored() {
554 $this->setUp();
556 $page = $this->wikigenerator->create_first_page($this->wiki);
557 $context = context_module::instance($this->wiki->cmid);
558 $version = wiki_get_current_version($page->id);
560 // Triggering and capturing the event.
561 $sink = $this->redirectEvents();
562 wiki_restore_page($page, $version, $context);
563 $events = $sink->get_events();
564 $this->assertCount(2, $events);
565 $event = array_pop($events);
567 // Checking that the event contains the expected values.
568 $this->assertInstanceOf('\mod_wiki\event\page_version_restored', $event);
569 $this->assertEquals($context, $event->get_context());
570 $this->assertEquals($version->id, $event->objectid);
571 $this->assertEquals($page->id, $event->other['pageid']);
572 $expected = array($this->course->id, 'wiki', 'restore', 'view.php?pageid=' . $page->id, $page->id, $this->wiki->cmid);
573 $this->assertEventLegacyLogData($expected, $event);
574 $this->assertEventContextNotUsed($event);