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