Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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    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
24
 */
25
 
26
namespace mod_wiki\event;
27
 
28
defined('MOODLE_INTERNAL') || die();
29
global $CFG;
30
require_once($CFG->dirroot.'/mod/wiki/locallib.php');
31
/**
32
 * Events tests class.
33
 *
34
 * @package    mod_wiki
35
 * @category   phpunit
36
 * @copyright  2013 Rajesh Taneja <rajesh@moodle.com>
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
1441 ariadna 39
final class events_test extends \advanced_testcase {
1 efrain 40
    private $course;
41
    private $wiki;
42
    private $wikigenerator;
43
    private $student;
44
    private $teacher;
45
 
46
    /**
47
     * Setup test data.
48
     */
49
    public function setUp(): void {
50
        global $DB;
1441 ariadna 51
        parent::setUp();
1 efrain 52
 
53
        $this->resetAfterTest();
54
        // Create course and wiki.
55
        $this->course = $this->getDataGenerator()->create_course();
56
        $this->wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $this->course->id));
57
        $this->wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki');
58
 
59
        // Create student and teacher in course.
60
        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
61
        $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
62
        $this->student = $this->getDataGenerator()->create_user();
63
        $this->teacher = $this->getDataGenerator()->create_user();
64
        $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $studentrole->id);
65
        $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $teacherrole->id);
66
        $this->setAdminUser();
67
    }
68
 
69
    /**
70
     * Test comment_created event.
71
     */
11 efrain 72
    public function test_comment_created(): void {
1 efrain 73
        $this->setUp();
74
 
75
        $page = $this->wikigenerator->create_first_page($this->wiki);
76
        $context = \context_module::instance($this->wiki->cmid);
77
 
78
        // Triggering and capturing the event.
79
        $sink = $this->redirectEvents();
80
        wiki_add_comment($context, $page->id, 'Test comment', $this->wiki->defaultformat);
81
        $events = $sink->get_events();
82
        $this->assertCount(1, $events);
83
        $event = reset($events);
84
 
85
        // Checking that the event contains the expected values.
86
        $this->assertInstanceOf('\mod_wiki\event\comment_created', $event);
87
        $this->assertEquals($context, $event->get_context());
88
        $this->assertEquals($page->id, $event->other['itemid']);
89
        $this->assertEventContextNotUsed($event);
90
    }
91
 
92
    /**
93
     * Test comment_deleted event.
94
     */
11 efrain 95
    public function test_comment_deleted(): void {
1 efrain 96
        $this->setUp();
97
 
98
        $page = $this->wikigenerator->create_first_page($this->wiki);
99
        $context = \context_module::instance($this->wiki->cmid);
100
 
101
        // Add comment so we can delete it later.
102
        wiki_add_comment($context, $page->id, 'Test comment', 'html');
103
        $comment = wiki_get_comments($context->id, $page->id);
104
        $this->assertCount(1, $comment);
105
        $comment = array_shift($comment);
106
 
107
        // Triggering and capturing the event.
108
        $sink = $this->redirectEvents();
109
        wiki_delete_comment($comment->id, $context, $page->id);
110
        $events = $sink->get_events();
111
        $this->assertCount(1, $events);
112
        $event = reset($events);
113
 
114
        // Checking that the event contains the expected values.
115
        $this->assertInstanceOf('\mod_wiki\event\comment_deleted', $event);
116
        $this->assertEquals($context, $event->get_context());
117
        $this->assertEquals($page->id, $event->other['itemid']);
118
        $this->assertEventContextNotUsed($event);
119
    }
120
 
121
    /**
122
     * Test comment_viewed event.
123
     */
11 efrain 124
    public function test_comment_viewed(): void {
1 efrain 125
        // There is no proper API to call or trigger this event, so simulating event
126
        // to check if event returns the right information.
127
 
128
        $this->setUp();
129
        $page = $this->wikigenerator->create_first_page($this->wiki);
130
        $context = \context_module::instance($this->wiki->cmid);
131
 
132
        $params = array(
133
                'context' => $context,
134
                'objectid' => $page->id
135
                );
136
        $event = \mod_wiki\event\comments_viewed::create($params);
137
 
138
        // Triggering and capturing the event.
139
        $sink = $this->redirectEvents();
140
        $event->trigger();
141
        $events = $sink->get_events();
142
        $this->assertCount(1, $events);
143
        $event = reset($events);
144
 
145
        // Checking that the event contains the expected values.
146
        $this->assertInstanceOf('\mod_wiki\event\comments_viewed', $event);
147
        $this->assertEquals($context, $event->get_context());
148
    }
149
 
150
    /**
151
     * Test instances_list_viewed event.
152
     */
11 efrain 153
    public function test_course_module_instance_list_viewed(): void {
1 efrain 154
        // There is no proper API to call or trigger this event, so simulating event
155
        // to check if event returns the right information.
156
 
157
        $this->setUp();
158
        $context = \context_course::instance($this->course->id);
159
 
160
        $params = array('context' => $context);
161
        $event = \mod_wiki\event\course_module_instance_list_viewed::create($params);
162
 
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);
169
 
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
    }
174
 
175
    /**
176
     * Test course_module_viewed event.
177
     */
11 efrain 178
    public function test_course_module_viewed(): void {
1 efrain 179
        // There is no proper API to call or trigger this event, so simulating event
180
        // to check if event returns the right information.
181
 
182
        $this->setUp();
183
        $context = \context_module::instance($this->wiki->cmid);
184
 
185
        $params = array(
186
                'context' => $context,
187
                'objectid' => $this->wiki->id
188
                );
189
        $event = \mod_wiki\event\course_module_viewed::create($params);
190
 
191
        // Triggering and capturing the event.
192
        $sink = $this->redirectEvents();
193
        $event->trigger();
194
        $events = $sink->get_events();
195
        $this->assertCount(1, $events);
196
        $event = reset($events);
197
 
198
        // Checking that the event contains the expected values.
199
        $this->assertInstanceOf('\mod_wiki\event\course_module_viewed', $event);
200
        $this->assertEquals($context, $event->get_context());
201
        $this->assertEquals($this->wiki->id, $event->objectid);
202
    }
203
 
204
    /**
205
     * Test page_viewed event.
206
     */
11 efrain 207
    public function test_page_viewed(): void {
1 efrain 208
        // There is no proper API to call or trigger this event, so simulating event
209
        // to check if event returns the right information.
210
 
211
        $this->setUp();
212
 
213
        $page = $this->wikigenerator->create_first_page($this->wiki);
214
        $context = \context_module::instance($this->wiki->cmid);
215
 
216
        $params = array(
217
                'context' => $context,
218
                'objectid' => $page->id
219
                );
220
        $event = \mod_wiki\event\page_viewed::create($params);
221
 
222
        // Triggering and capturing the event.
223
        $sink = $this->redirectEvents();
224
        $event->trigger();
225
        $events = $sink->get_events();
226
        $this->assertCount(1, $events);
227
        $event = reset($events);
228
 
229
        // Checking that the event contains the expected values.
230
        $this->assertInstanceOf('\mod_wiki\event\page_viewed', $event);
231
        $this->assertEquals($context, $event->get_context());
232
        $this->assertEquals($page->id, $event->objectid);
233
    }
234
 
235
    /**
236
     * Test page_viewed event for prettypage view.
237
     */
11 efrain 238
    public function test_pretty_page_viewed(): void {
1 efrain 239
        // There is no proper API to call or trigger this event, so simulating event
240
        // to check if event returns the right information.
241
 
242
        $this->setUp();
243
 
244
        $page = $this->wikigenerator->create_first_page($this->wiki);
245
        $context = \context_module::instance($this->wiki->cmid);
246
 
247
        $params = array(
248
                'context' => $context,
249
                'objectid' => $page->id,
250
                'other' => array('prettyview' => true)
251
                );
252
        $event = \mod_wiki\event\page_viewed::create($params);
253
 
254
        // Triggering and capturing the event.
255
        $sink = $this->redirectEvents();
256
        $event->trigger();
257
        $events = $sink->get_events();
258
        $this->assertCount(1, $events);
259
        $event = reset($events);
260
 
261
        // Checking that the event contains the expected values.
262
        $this->assertInstanceOf('\mod_wiki\event\page_viewed', $event);
263
        $this->assertEquals($context, $event->get_context());
264
        $this->assertEquals($page->id, $event->objectid);
265
    }
266
 
267
    /**
268
     * Test page_created event.
269
     */
11 efrain 270
    public function test_page_created(): void {
1 efrain 271
        global $USER;
272
 
273
        $this->setUp();
274
 
275
        $context = \context_module::instance($this->wiki->cmid);
276
 
277
        // Triggering and capturing the event.
278
        $sink = $this->redirectEvents();
279
        $page = $this->wikigenerator->create_first_page($this->wiki);
280
        $events = $sink->get_events();
281
        $this->assertCount(2, $events);
282
        $event = reset($events);
283
 
284
        // Checking that the event contains the expected values.
285
        $this->assertInstanceOf('\mod_wiki\event\page_created', $event);
286
        $this->assertEquals($context, $event->get_context());
287
        $this->assertEquals($page->id, $event->objectid);
288
    }
289
 
290
    /**
291
     * Test page_deleted and page_version_deleted and page_locks_deleted event.
292
     */
11 efrain 293
    public function test_page_deleted(): void {
1 efrain 294
        global $DB;
295
 
296
        $this->setUp();
297
 
298
        $page = $this->wikigenerator->create_first_page($this->wiki);
299
        $context = \context_module::instance($this->wiki->cmid);
300
        $oldversions = $DB->get_records('wiki_versions', array('pageid' => $page->id));
301
        $oldversion = array_shift($oldversions);
302
 
303
        // Triggering and capturing the event.
304
        $sink = $this->redirectEvents();
305
        wiki_delete_pages($context, array($page->id));
306
        $events = $sink->get_events();
307
        $this->assertCount(4, $events);
308
        $event = array_shift($events);
309
 
310
        // Checking that the event contains the page_version_deleted event.
311
        $this->assertInstanceOf('\mod_wiki\event\page_version_deleted', $event);
312
        $this->assertEquals($context, $event->get_context());
313
        $this->assertEquals($page->id, $event->other['pageid']);
314
        $this->assertEquals($oldversion->id, $event->objectid);
315
 
316
        // Checking that the event contains the page_deleted event.
317
        $event = array_pop($events);
318
        $this->assertInstanceOf('\mod_wiki\event\page_deleted', $event);
319
        $this->assertEquals($context, $event->get_context());
320
        $this->assertEquals($page->id, $event->objectid);
321
 
322
        // Checking that the event contains the expected values.
323
        $event = array_pop($events);
324
        $this->assertInstanceOf('\mod_wiki\event\page_locks_deleted', $event);
325
        $this->assertEquals($context, $event->get_context());
326
        $this->assertEquals($page->id, $event->objectid);
327
 
328
        // Delete all pages.
329
        $page1 = $this->wikigenerator->create_first_page($this->wiki);
330
        $page2 = $this->wikigenerator->create_content($this->wiki);
331
        $page3 = $this->wikigenerator->create_content($this->wiki, array('title' => 'Custom title'));
332
 
333
        // Triggering and capturing the event.
334
        $sink = $this->redirectEvents();
335
        wiki_delete_pages($context, array($page1->id, $page2->id));
336
        $events = $sink->get_events();
337
        $this->assertCount(8, $events);
338
        $event = array_pop($events);
339
 
340
        // Checking that the event contains the expected values.
341
        $this->assertInstanceOf('\mod_wiki\event\page_deleted', $event);
342
        $this->assertEquals($context, $event->get_context());
343
        $this->assertEquals($page2->id, $event->objectid);
344
    }
345
 
346
    /**
347
     * Test page_updated event.
348
     */
11 efrain 349
    public function test_page_updated(): void {
1 efrain 350
        global $USER;
351
 
352
        $this->setUp();
353
 
354
        $page = $this->wikigenerator->create_first_page($this->wiki);
355
        $context = \context_module::instance($this->wiki->cmid);
356
 
357
        // Triggering and capturing the event.
358
        $sink = $this->redirectEvents();
359
        wiki_save_page($page, 'New content', $USER->id);
360
        $events = $sink->get_events();
361
        $this->assertCount(1, $events);
362
        $event = reset($events);
363
 
364
        // Checking that the event contains the expected values.
365
        $this->assertInstanceOf('\mod_wiki\event\page_updated', $event);
366
        $this->assertEquals($context, $event->get_context());
367
        $this->assertEquals($page->id, $event->objectid);
368
    }
369
 
370
    /**
371
     * Test page_diff_viewed event.
372
     */
11 efrain 373
    public function test_page_diff_viewed(): void {
1 efrain 374
        // There is no proper API to call or trigger this event, so simulating event
375
        // to check if event returns the right information.
376
 
377
        $this->setUp();
378
 
379
        $page = $this->wikigenerator->create_first_page($this->wiki);
380
        $context = \context_module::instance($this->wiki->cmid);
381
 
382
        $params = array(
383
                'context' => $context,
384
                'objectid' => $page->id,
385
                'other' => array(
386
                    'comparewith' => 1,
387
                    'compare' => 2
388
                    )
389
                );
390
        $event = \mod_wiki\event\page_diff_viewed::create($params);
391
 
392
        // Triggering and capturing the event.
393
        $sink = $this->redirectEvents();
394
        $event->trigger();
395
        $events = $sink->get_events();
396
        $this->assertCount(1, $events);
397
        $event = reset($events);
398
 
399
        // Checking that the event contains the expected values.
400
        $this->assertInstanceOf('\mod_wiki\event\page_diff_viewed', $event);
401
        $this->assertEquals($context, $event->get_context());
402
        $this->assertEquals($page->id, $event->objectid);
403
    }
404
 
405
    /**
406
     * Test page_history_viewed event.
407
     */
11 efrain 408
    public function test_page_history_viewed(): void {
1 efrain 409
        // There is no proper API to call or trigger this event, so simulating event
410
        // to check if event returns the right information.
411
 
412
        $this->setUp();
413
 
414
        $page = $this->wikigenerator->create_first_page($this->wiki);
415
        $context = \context_module::instance($this->wiki->cmid);
416
 
417
        $params = array(
418
                'context' => $context,
419
                'objectid' => $page->id
420
                );
421
        $event = \mod_wiki\event\page_history_viewed::create($params);
422
 
423
        // Triggering and capturing the event.
424
        $sink = $this->redirectEvents();
425
        $event->trigger();
426
        $events = $sink->get_events();
427
        $this->assertCount(1, $events);
428
        $event = reset($events);
429
 
430
        // Checking that the event contains the expected values.
431
        $this->assertInstanceOf('\mod_wiki\event\page_history_viewed', $event);
432
        $this->assertEquals($context, $event->get_context());
433
        $this->assertEquals($page->id, $event->objectid);
434
    }
435
 
436
    /**
437
     * Test page_map_viewed event.
438
     */
11 efrain 439
    public function test_page_map_viewed(): void {
1 efrain 440
        // There is no proper API to call or trigger this event, so simulating event
441
        // to check if event returns the right information.
442
 
443
        $this->setUp();
444
 
445
        $page = $this->wikigenerator->create_first_page($this->wiki);
446
        $context = \context_module::instance($this->wiki->cmid);
447
 
448
        $params = array(
449
                'context' => $context,
450
                'objectid' => $page->id,
451
                'other' => array(
452
                    'option' => 0
453
                    )
454
                );
455
        $event = \mod_wiki\event\page_map_viewed::create($params);
456
 
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);
463
 
464
        // Checking that the event contains the expected values.
465
        $this->assertInstanceOf('\mod_wiki\event\page_map_viewed', $event);
466
        $this->assertEquals($context, $event->get_context());
467
        $this->assertEquals($page->id, $event->objectid);
468
        $this->assertEquals(0, $event->other['option']);
469
    }
470
 
471
    /**
472
     * Test page_version_viewed event.
473
     */
11 efrain 474
    public function test_page_version_viewed(): void {
1 efrain 475
        // There is no proper API to call or trigger this event, so simulating event
476
        // to check if event returns the right information.
477
 
478
        $this->setUp();
479
 
480
        $page = $this->wikigenerator->create_first_page($this->wiki);
481
        $context = \context_module::instance($this->wiki->cmid);
482
 
483
        $params = array(
484
                'context' => $context,
485
                'objectid' => $page->id,
486
                'other' => array(
487
                    'versionid' => 1
488
                    )
489
                );
490
        $event = \mod_wiki\event\page_version_viewed::create($params);
491
 
492
        // Triggering and capturing the event.
493
        $sink = $this->redirectEvents();
494
        $event->trigger();
495
        $events = $sink->get_events();
496
        $this->assertCount(1, $events);
497
        $event = reset($events);
498
 
499
        // Checking that the event contains the expected values.
500
        $this->assertInstanceOf('\mod_wiki\event\page_version_viewed', $event);
501
        $this->assertEquals($context, $event->get_context());
502
        $this->assertEquals($page->id, $event->objectid);
503
        $this->assertEquals(1, $event->other['versionid']);
504
    }
505
 
506
    /**
507
     * Test page_version_restored event.
508
     */
11 efrain 509
    public function test_page_version_restored(): void {
1 efrain 510
        $this->setUp();
511
 
512
        $page = $this->wikigenerator->create_first_page($this->wiki);
513
        $context = \context_module::instance($this->wiki->cmid);
514
        $version = wiki_get_current_version($page->id);
515
 
516
        // Triggering and capturing the event.
517
        $sink = $this->redirectEvents();
518
        wiki_restore_page($page, $version, $context);
519
        $events = $sink->get_events();
520
        $this->assertCount(2, $events);
521
        $event = array_pop($events);
522
 
523
        // Checking that the event contains the expected values.
524
        $this->assertInstanceOf('\mod_wiki\event\page_version_restored', $event);
525
        $this->assertEquals($context, $event->get_context());
526
        $this->assertEquals($version->id, $event->objectid);
527
        $this->assertEquals($page->id, $event->other['pageid']);
528
    }
529
}