Proyectos de Subversion Moodle

Rev

Rev 1 | | 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
 * Unit tests for lib.php
19
 *
20
 * @package    mod_glossary
21
 * @category   test
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_glossary\event;
27
 
28
/**
29
 * Unit tests for glossary events.
30
 *
31
 * @package   mod_glossary
32
 * @category  test
33
 * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
34
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class events_test extends \advanced_testcase {
37
 
38
    public function setUp(): void {
39
        $this->resetAfterTest();
40
    }
41
 
42
    /**
43
     * Test comment_created event.
44
     */
11 efrain 45
    public function test_comment_created(): void {
1 efrain 46
        global $CFG;
47
        require_once($CFG->dirroot . '/comment/lib.php');
48
 
49
        // Create a record for adding comment.
50
        $this->setAdminUser();
51
        $course = $this->getDataGenerator()->create_course();
52
        $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
53
        $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
54
 
55
        $entry = $glossarygenerator->create_content($glossary);
56
 
57
        $context = \context_module::instance($glossary->cmid);
58
        $cm = get_coursemodule_from_instance('glossary', $glossary->id, $course->id);
59
        $cmt = new \stdClass();
60
        $cmt->component = 'mod_glossary';
61
        $cmt->context = $context;
62
        $cmt->course = $course;
63
        $cmt->cm = $cm;
64
        $cmt->area = 'glossary_entry';
65
        $cmt->itemid = $entry->id;
66
        $cmt->showcount = true;
67
        $comment = new \comment($cmt);
68
 
69
        // Triggering and capturing the event.
70
        $sink = $this->redirectEvents();
71
        $comment->add('New comment');
72
        $events = $sink->get_events();
73
        $this->assertCount(1, $events);
74
        $event = reset($events);
75
 
76
        // Checking that the event contains the expected values.
77
        $this->assertInstanceOf('\mod_glossary\event\comment_created', $event);
78
        $this->assertEquals($context, $event->get_context());
79
        $url = new \moodle_url('/mod/glossary/view.php', array('id' => $glossary->cmid));
80
        $this->assertEquals($url, $event->get_url());
81
        $this->assertEventContextNotUsed($event);
82
    }
83
 
84
    /**
85
     * Test comment_deleted event.
86
     */
11 efrain 87
    public function test_comment_deleted(): void {
1 efrain 88
        global $CFG;
89
        require_once($CFG->dirroot . '/comment/lib.php');
90
 
91
        // Create a record for deleting comment.
92
        $this->setAdminUser();
93
        $course = $this->getDataGenerator()->create_course();
94
        $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
95
        $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
96
 
97
        $entry = $glossarygenerator->create_content($glossary);
98
 
99
        $context = \context_module::instance($glossary->cmid);
100
        $cm = get_coursemodule_from_instance('glossary', $glossary->id, $course->id);
101
        $cmt = new \stdClass();
102
        $cmt->component = 'mod_glossary';
103
        $cmt->context = $context;
104
        $cmt->course = $course;
105
        $cmt->cm = $cm;
106
        $cmt->area = 'glossary_entry';
107
        $cmt->itemid = $entry->id;
108
        $cmt->showcount = true;
109
        $comment = new \comment($cmt);
110
        $newcomment = $comment->add('New comment 1');
111
 
112
        // Triggering and capturing the event.
113
        $sink = $this->redirectEvents();
114
        $comment->delete($newcomment->id);
115
        $events = $sink->get_events();
116
        $this->assertCount(1, $events);
117
        $event = reset($events);
118
 
119
        // Checking that the event contains the expected values.
120
        $this->assertInstanceOf('\mod_glossary\event\comment_deleted', $event);
121
        $this->assertEquals($context, $event->get_context());
122
        $url = new \moodle_url('/mod/glossary/view.php', array('id' => $glossary->cmid));
123
        $this->assertEquals($url, $event->get_url());
124
        $this->assertEventContextNotUsed($event);
125
    }
126
 
11 efrain 127
    public function test_course_module_viewed(): void {
1 efrain 128
        global $DB;
129
        // There is no proper API to call to trigger this event, so what we are
130
        // doing here is simply making sure that the events returns the right information.
131
 
132
        $course = $this->getDataGenerator()->create_course();
133
        $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course->id));
134
 
135
        $dbcourse = $DB->get_record('course', array('id' => $course->id));
136
        $dbglossary = $DB->get_record('glossary', array('id' => $glossary->id));
137
        $context = \context_module::instance($glossary->cmid);
138
        $mode = 'letter';
139
 
140
        $event = \mod_glossary\event\course_module_viewed::create(array(
141
            'objectid' => $dbglossary->id,
142
            'context' => $context,
143
            'other' => array('mode' => $mode)
144
        ));
145
 
146
        $event->add_record_snapshot('course', $dbcourse);
147
        $event->add_record_snapshot('glossary', $dbglossary);
148
 
149
        // Triggering and capturing the event.
150
        $sink = $this->redirectEvents();
151
        $event->trigger();
152
        $events = $sink->get_events();
153
        $this->assertCount(1, $events);
154
        $event = reset($events);
155
 
156
        // Checking that the event contains the expected values.
157
        $this->assertInstanceOf('\mod_glossary\event\course_module_viewed', $event);
158
        $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
159
        $this->assertEquals($glossary->cmid, $event->contextinstanceid);
160
        $this->assertEquals($glossary->id, $event->objectid);
161
        $this->assertEquals(new \moodle_url('/mod/glossary/view.php', array('id' => $glossary->cmid, 'mode' => $mode)), $event->get_url());
162
        $this->assertEventContextNotUsed($event);
163
    }
164
 
11 efrain 165
    public function test_course_module_instance_list_viewed(): void {
1 efrain 166
        // There is no proper API to call to trigger this event, so what we are
167
        // doing here is simply making sure that the events returns the right information.
168
 
169
        $course = $this->getDataGenerator()->create_course();
170
 
171
        $event = \mod_glossary\event\course_module_instance_list_viewed::create(array(
172
            'context' => \context_course::instance($course->id)
173
        ));
174
 
175
        // Triggering and capturing the event.
176
        $sink = $this->redirectEvents();
177
        $event->trigger();
178
        $events = $sink->get_events();
179
        $this->assertCount(1, $events);
180
        $event = reset($events);
181
 
182
        // Checking that the event contains the expected values.
183
        $this->assertInstanceOf('\mod_glossary\event\course_module_instance_list_viewed', $event);
184
        $this->assertEquals(CONTEXT_COURSE, $event->contextlevel);
185
        $this->assertEquals($course->id, $event->contextinstanceid);
186
        $this->assertEventContextNotUsed($event);
187
    }
188
 
11 efrain 189
    public function test_entry_created(): void {
1 efrain 190
        // There is no proper API to call to trigger this event, so what we are
191
        // doing here is simply making sure that the events returns the right information.
192
 
193
        $this->setAdminUser();
194
        $course = $this->getDataGenerator()->create_course();
195
        $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
196
        $context = \context_module::instance($glossary->cmid);
197
 
198
        $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
199
        $entry = $glossarygenerator->create_content($glossary);
200
 
201
        $eventparams = array(
202
            'context' => $context,
203
            'objectid' => $entry->id,
204
            'other' => array('concept' => $entry->concept)
205
        );
206
        $event = \mod_glossary\event\entry_created::create($eventparams);
207
        $event->add_record_snapshot('glossary_entries', $entry);
208
 
209
        $sink = $this->redirectEvents();
210
        $event->trigger();
211
        $events = $sink->get_events();
212
        $this->assertCount(1, $events);
213
        $event = reset($events);
214
 
215
        // Checking that the event contains the expected values.
216
        $this->assertInstanceOf('\mod_glossary\event\entry_created', $event);
217
        $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
218
        $this->assertEquals($glossary->cmid, $event->contextinstanceid);
219
        $this->assertEventContextNotUsed($event);
220
    }
221
 
11 efrain 222
    public function test_entry_updated(): void {
1 efrain 223
        // There is no proper API to call to trigger this event, so what we are
224
        // doing here is simply making sure that the events returns the right information.
225
 
226
        $this->setAdminUser();
227
        $course = $this->getDataGenerator()->create_course();
228
        $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
229
        $context = \context_module::instance($glossary->cmid);
230
 
231
        $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
232
        $entry = $glossarygenerator->create_content($glossary);
233
 
234
        $eventparams = array(
235
            'context' => $context,
236
            'objectid' => $entry->id,
237
            'other' => array('concept' => $entry->concept)
238
        );
239
        $event = \mod_glossary\event\entry_updated::create($eventparams);
240
        $event->add_record_snapshot('glossary_entries', $entry);
241
 
242
        $sink = $this->redirectEvents();
243
        $event->trigger();
244
        $events = $sink->get_events();
245
        $this->assertCount(1, $events);
246
        $event = reset($events);
247
 
248
        // Checking that the event contains the expected values.
249
        $this->assertInstanceOf('\mod_glossary\event\entry_updated', $event);
250
        $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
251
        $this->assertEquals($glossary->cmid, $event->contextinstanceid);
252
        $this->assertEventContextNotUsed($event);
253
    }
254
 
11 efrain 255
    public function test_entry_deleted(): void {
1 efrain 256
        global $DB;
257
        // There is no proper API to call to trigger this event, so what we are
258
        // doing here is simply making sure that the events returns the right information.
259
 
260
        $this->setAdminUser();
261
        $course = $this->getDataGenerator()->create_course();
262
        $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
263
        $context = \context_module::instance($glossary->cmid);
264
        $prevmode = 'view';
265
        $hook = 'ALL';
266
 
267
        $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
268
        $entry = $glossarygenerator->create_content($glossary);
269
 
270
        $DB->delete_records('glossary_entries', array('id' => $entry->id));
271
 
272
        $eventparams = array(
273
            'context' => $context,
274
            'objectid' => $entry->id,
275
            'other' => array(
276
                'mode' => $prevmode,
277
                'hook' => $hook,
278
                'concept' => $entry->concept
279
            )
280
        );
281
        $event = \mod_glossary\event\entry_deleted::create($eventparams);
282
        $event->add_record_snapshot('glossary_entries', $entry);
283
 
284
        $sink = $this->redirectEvents();
285
        $event->trigger();
286
        $events = $sink->get_events();
287
        $this->assertCount(1, $events);
288
        $event = reset($events);
289
 
290
        // Checking that the event contains the expected values.
291
        $this->assertInstanceOf('\mod_glossary\event\entry_deleted', $event);
292
        $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
293
        $this->assertEquals($glossary->cmid, $event->contextinstanceid);
294
        $this->assertEventContextNotUsed($event);
295
    }
296
 
11 efrain 297
    public function test_category_created(): void {
1 efrain 298
        global $DB;
299
        // There is no proper API to call to trigger this event, so what we are
300
        // doing here is simply making sure that the events returns the right information.
301
 
302
        $this->setAdminUser();
303
        $course = $this->getDataGenerator()->create_course();
304
        $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
305
        $context = \context_module::instance($glossary->cmid);
306
 
307
        // Create category and trigger event.
308
        $category = new \stdClass();
309
        $category->name = 'New category';
310
        $category->usedynalink = 0;
311
        $category->id = $DB->insert_record('glossary_categories', $category);
312
 
313
        $event = \mod_glossary\event\category_created::create(array(
314
            'context' => $context,
315
            'objectid' => $category->id
316
        ));
317
 
318
        $sink = $this->redirectEvents();
319
        $event->trigger();
320
        $events = $sink->get_events();
321
        $this->assertCount(1, $events);
322
        $event = reset($events);
323
 
324
        // Checking that the event contains the expected values.
325
        $this->assertInstanceOf('\mod_glossary\event\category_created', $event);
326
        $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
327
        $this->assertEquals($glossary->cmid, $event->contextinstanceid);
328
        $this->assertEventContextNotUsed($event);
329
 
330
        // Update category and trigger event.
331
        $category->name = 'Updated category';
332
        $DB->update_record('glossary_categories', $category);
333
 
334
        $event = \mod_glossary\event\category_updated::create(array(
335
            'context' => $context,
336
            'objectid' => $category->id
337
        ));
338
 
339
        $sink = $this->redirectEvents();
340
        $event->trigger();
341
        $events = $sink->get_events();
342
        $this->assertCount(1, $events);
343
        $event = reset($events);
344
 
345
        // Checking that the event contains the expected values.
346
        $this->assertInstanceOf('\mod_glossary\event\category_updated', $event);
347
        $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
348
        $this->assertEquals($glossary->cmid, $event->contextinstanceid);
349
        $this->assertEventContextNotUsed($event);
350
 
351
 
352
        // Delete category and trigger event.
353
        $category = $DB->get_record('glossary_categories', array('id' => $category->id));
354
        $DB->delete_records('glossary_categories', array('id' => $category->id));
355
 
356
        $event = \mod_glossary\event\category_deleted::create(array(
357
            'context' => $context,
358
            'objectid' => $category->id
359
        ));
360
        $event->add_record_snapshot('glossary_categories', $category);
361
 
362
        $sink = $this->redirectEvents();
363
        $event->trigger();
364
        $events = $sink->get_events();
365
        $this->assertCount(1, $events);
366
        $event = reset($events);
367
 
368
        // Checking that the event contains the expected values.
369
        $this->assertInstanceOf('\mod_glossary\event\category_deleted', $event);
370
        $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
371
        $this->assertEquals($glossary->cmid, $event->contextinstanceid);
372
        $this->assertEventContextNotUsed($event);
373
    }
374
 
11 efrain 375
    public function test_entry_approved(): void {
1 efrain 376
        global $DB;
377
        // There is no proper API to call to trigger this event, so what we are
378
        // doing here is simply making sure that the events returns the right information.
379
 
380
        $this->setAdminUser();
381
        $course = $this->getDataGenerator()->create_course();
382
        $student = $this->getDataGenerator()->create_user();
383
        $rolestudent = $DB->get_record('role', array('shortname' => 'student'));
384
        $this->getDataGenerator()->enrol_user($student->id, $course->id, $rolestudent->id);
385
        $teacher = $this->getDataGenerator()->create_user();
386
        $roleteacher = $DB->get_record('role', array('shortname' => 'teacher'));
387
        $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $roleteacher->id);
388
 
389
        $this->setUser($teacher);
390
        $glossary = $this->getDataGenerator()->create_module('glossary',
391
                array('course' => $course, 'defaultapproval' => 0));
392
        $context = \context_module::instance($glossary->cmid);
393
 
394
        $this->setUser($student);
395
        $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
396
        $entry = $glossarygenerator->create_content($glossary);
397
        $this->assertEquals(0, $entry->approved);
398
 
399
        // Approve entry, trigger and validate event.
400
        $this->setUser($teacher);
401
        $newentry = new \stdClass();
402
        $newentry->id           = $entry->id;
403
        $newentry->approved     = true;
404
        $newentry->timemodified = time();
405
        $DB->update_record("glossary_entries", $newentry);
406
        $params = array(
407
            'context' => $context,
408
            'objectid' => $entry->id
409
        );
410
        $event = \mod_glossary\event\entry_approved::create($params);
411
 
412
        $sink = $this->redirectEvents();
413
        $event->trigger();
414
        $events = $sink->get_events();
415
        $this->assertCount(1, $events);
416
        $event = reset($events);
417
 
418
        // Checking that the event contains the expected values.
419
        $this->assertInstanceOf('\mod_glossary\event\entry_approved', $event);
420
        $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
421
        $this->assertEquals($glossary->cmid, $event->contextinstanceid);
422
        $this->assertEventContextNotUsed($event);
423
 
424
 
425
        // Disapprove entry, trigger and validate event.
426
        $this->setUser($teacher);
427
        $newentry = new \stdClass();
428
        $newentry->id           = $entry->id;
429
        $newentry->approved     = false;
430
        $newentry->timemodified = time();
431
        $DB->update_record("glossary_entries", $newentry);
432
        $params = array(
433
            'context' => $context,
434
            'objectid' => $entry->id
435
        );
436
        $event = \mod_glossary\event\entry_disapproved::create($params);
437
 
438
        $sink = $this->redirectEvents();
439
        $event->trigger();
440
        $events = $sink->get_events();
441
        $this->assertCount(1, $events);
442
        $event = reset($events);
443
 
444
        // Checking that the event contains the expected values.
445
        $this->assertInstanceOf('\mod_glossary\event\entry_disapproved', $event);
446
        $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
447
        $this->assertEquals($glossary->cmid, $event->contextinstanceid);
448
        $this->assertEventContextNotUsed($event);
449
    }
450
 
11 efrain 451
    public function test_entry_viewed(): void {
1 efrain 452
        // There is no proper API to call to trigger this event, so what we are
453
        // doing here is simply making sure that the events returns the right information.
454
 
455
        $this->setAdminUser();
456
        $course = $this->getDataGenerator()->create_course();
457
        $glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
458
        $context = \context_module::instance($glossary->cmid);
459
 
460
        $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
461
        $entry = $glossarygenerator->create_content($glossary);
462
 
463
        $event = \mod_glossary\event\entry_viewed::create(array(
464
            'objectid' => $entry->id,
465
            'context' => $context
466
        ));
467
 
468
        $sink = $this->redirectEvents();
469
        $event->trigger();
470
        $events = $sink->get_events();
471
        $this->assertCount(1, $events);
472
        $event = reset($events);
473
 
474
        // Checking that the event contains the expected values.
475
        $this->assertInstanceOf('\mod_glossary\event\entry_viewed', $event);
476
        $this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
477
        $this->assertEquals($glossary->cmid, $event->contextinstanceid);
478
        $this->assertEventContextNotUsed($event);
479
    }
480
}