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_choice
21
 * @copyright  2013 Adrian Greeve <adrian@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace mod_choice\event;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
global $CFG;
30
require_once($CFG->dirroot . '/mod/choice/lib.php');
31
 
32
/**
33
 * Events tests class.
34
 *
35
 * @package    mod_choice
36
 * @copyright  2013 Adrian Greeve <adrian@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
    /** @var choice_object */
41
    protected $choice;
42
 
43
    /** @var course_object */
44
    protected $course;
45
 
46
    /** @var cm_object Course module object. */
47
    protected $cm;
48
 
49
    /** @var context_object */
50
    protected $context;
51
 
52
    /**
53
     * Setup often used objects for the following tests.
54
     */
55
    protected function setUp(): void {
56
        global $DB;
1441 ariadna 57
        parent::setUp();
1 efrain 58
 
59
        $this->resetAfterTest();
60
 
61
        $this->course = $this->getDataGenerator()->create_course();
62
        $this->choice = $this->getDataGenerator()->create_module('choice', array('course' => $this->course->id));
63
        $this->cm = $DB->get_record('course_modules', array('id' => $this->choice->cmid));
64
        $this->context = \context_module::instance($this->choice->cmid);
65
    }
66
 
67
    /**
68
     * Test to ensure that event data is being stored correctly.
69
     */
11 efrain 70
    public function test_answer_created(): void {
1 efrain 71
        global $DB;
72
        // Generate user data.
73
        $user = $this->getDataGenerator()->create_user();
74
        $this->setUser($user);
75
 
76
        $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $this->choice->id)));
77
        // Redirect event.
78
        $sink = $this->redirectEvents();
79
        choice_user_submit_response($optionids[3], $this->choice, $user->id, $this->course, $this->cm);
80
        $events = $sink->get_events();
81
        $answer = $DB->get_record('choice_answers', ['userid' => $user->id, 'choiceid' => $this->choice->id]);
82
 
83
        // Data checking.
84
        $this->assertCount(1, $events);
85
        $this->assertInstanceOf('\mod_choice\event\answer_created', $events[0]);
86
        $this->assertEquals($user->id, $events[0]->userid);
87
        $this->assertEquals($user->id, $events[0]->relateduserid);
88
        $this->assertEquals(\context_module::instance($this->choice->cmid), $events[0]->get_context());
89
        $this->assertEquals($answer->id, $events[0]->objectid);
90
        $this->assertEquals($this->choice->id, $events[0]->other['choiceid']);
91
        $this->assertEquals($optionids[3], $events[0]->other['optionid']);
92
        $this->assertEventContextNotUsed($events[0]);
93
        $sink->close();
94
    }
95
 
96
    /**
97
     * Test to ensure that event data is being stored correctly.
98
     */
11 efrain 99
    public function test_answer_submitted_by_another_user(): void {
1 efrain 100
        global $DB, $USER;
101
        // Generate user data.
102
        $user = $this->getDataGenerator()->create_user();
103
 
104
        $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $this->choice->id)));
105
        // Redirect event.
106
        $sink = $this->redirectEvents();
107
        choice_user_submit_response($optionids[3], $this->choice, $user->id, $this->course, $this->cm);
108
        $events = $sink->get_events();
109
        $answer = $DB->get_record('choice_answers', ['userid' => $user->id, 'choiceid' => $this->choice->id]);
110
 
111
        // Data checking.
112
        $this->assertCount(1, $events);
113
        $this->assertInstanceOf('\mod_choice\event\answer_created', $events[0]);
114
        $this->assertEquals($USER->id, $events[0]->userid);
115
        $this->assertEquals($user->id, $events[0]->relateduserid);
116
        $this->assertEquals(\context_module::instance($this->choice->cmid), $events[0]->get_context());
117
        $this->assertEquals($answer->id, $events[0]->objectid);
118
        $this->assertEquals($this->choice->id, $events[0]->other['choiceid']);
119
        $this->assertEquals($optionids[3], $events[0]->other['optionid']);
120
        $this->assertEventContextNotUsed($events[0]);
121
        $sink->close();
122
    }
123
 
124
    /**
125
     * Test to ensure that multiple choice data is being stored correctly.
126
     */
11 efrain 127
    public function test_answer_created_multiple(): void {
1 efrain 128
        global $DB;
129
 
130
        // Generate user data.
131
        $user = $this->getDataGenerator()->create_user();
132
        $this->setUser($user);
133
 
134
        // Create multiple choice.
135
        $choice = $this->getDataGenerator()->create_module('choice', array('course' => $this->course->id,
136
            'allowmultiple' => 1));
137
        $cm = $DB->get_record('course_modules', array('id' => $choice->cmid));
138
        $context = \context_module::instance($choice->cmid);
139
 
140
        $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $choice->id)));
141
        $submittedoptionids = array($optionids[1], $optionids[3]);
142
 
143
        // Redirect event.
144
        $sink = $this->redirectEvents();
145
        choice_user_submit_response($submittedoptionids, $choice, $user->id, $this->course, $cm);
146
        $events = $sink->get_events();
147
        $answers = $DB->get_records('choice_answers', ['userid' => $user->id, 'choiceid' => $choice->id], 'id');
148
        $answers = array_values($answers);
149
 
150
        // Data checking.
151
        $this->assertCount(2, $events);
152
        $this->assertInstanceOf('\mod_choice\event\answer_created', $events[0]);
153
        $this->assertEquals($user->id, $events[0]->userid);
154
        $this->assertEquals($user->id, $events[0]->relateduserid);
155
        $this->assertEquals(\context_module::instance($choice->cmid), $events[0]->get_context());
156
        $this->assertEquals($answers[0]->id, $events[0]->objectid);
157
        $this->assertEquals($choice->id, $events[0]->other['choiceid']);
158
        $this->assertEquals($optionids[1], $events[0]->other['optionid']);
159
        $this->assertEventContextNotUsed($events[0]);
160
 
161
        $this->assertInstanceOf('\mod_choice\event\answer_created', $events[1]);
162
        $this->assertEquals($user->id, $events[1]->userid);
163
        $this->assertEquals($user->id, $events[1]->relateduserid);
164
        $this->assertEquals(\context_module::instance($choice->cmid), $events[1]->get_context());
165
        $this->assertEquals($answers[1]->id, $events[1]->objectid);
166
        $this->assertEquals($choice->id, $events[1]->other['choiceid']);
167
        $this->assertEquals($optionids[3], $events[1]->other['optionid']);
168
        $this->assertEventContextNotUsed($events[1]);
169
        $sink->close();
170
    }
171
 
172
    /**
173
     * Test custom validations.
174
     */
11 efrain 175
    public function test_answer_created_other_exception(): void {
1 efrain 176
        // Generate user data.
177
        $user = $this->getDataGenerator()->create_user();
178
 
179
        $eventdata = array();
180
        $eventdata['context'] = $this->context;
181
        $eventdata['objectid'] = 2;
182
        $eventdata['userid'] = $user->id;
183
        $eventdata['courseid'] = $this->course->id;
184
        $eventdata['other'] = array();
185
 
186
        // Make sure content identifier is always set.
187
        $this->expectException(\coding_exception::class);
188
        $event = \mod_choice\event\answer_created::create($eventdata);
189
        $event->trigger();
190
        $this->assertEventContextNotUsed($event);
191
    }
192
 
193
    /**
194
     * Test to ensure that event data is being stored correctly.
195
     */
11 efrain 196
    public function test_answer_updated(): void {
1 efrain 197
        global $DB;
198
        // Generate user data.
199
        $user = $this->getDataGenerator()->create_user();
200
        $this->setUser($user);
201
 
202
        $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $this->choice->id)));
203
 
204
        // Create the first answer.
205
        choice_user_submit_response($optionids[2], $this->choice, $user->id, $this->course, $this->cm);
206
        $oldanswer = $DB->get_record('choice_answers', ['userid' => $user->id, 'choiceid' => $this->choice->id]);
207
 
208
        // Redirect event.
209
        $sink = $this->redirectEvents();
210
        // Now choose a different answer.
211
        choice_user_submit_response($optionids[3], $this->choice, $user->id, $this->course, $this->cm);
212
        $newanswer = $DB->get_record('choice_answers', ['userid' => $user->id, 'choiceid' => $this->choice->id]);
213
 
214
        $events = $sink->get_events();
215
 
216
        // Data checking.
217
        $this->assertCount(2, $events);
218
        $this->assertInstanceOf('\mod_choice\event\answer_deleted', $events[0]);
219
        $this->assertEquals($user->id, $events[0]->userid);
220
        $this->assertEquals(\context_module::instance($this->choice->cmid), $events[0]->get_context());
221
        $this->assertEquals($oldanswer->id, $events[0]->objectid);
222
        $this->assertEquals($this->choice->id, $events[0]->other['choiceid']);
223
        $this->assertEquals($optionids[2], $events[0]->other['optionid']);
224
        $this->assertEventContextNotUsed($events[0]);
225
 
226
        $this->assertInstanceOf('\mod_choice\event\answer_created', $events[1]);
227
        $this->assertEquals($user->id, $events[1]->userid);
228
        $this->assertEquals(\context_module::instance($this->choice->cmid), $events[1]->get_context());
229
        $this->assertEquals($newanswer->id, $events[1]->objectid);
230
        $this->assertEquals($this->choice->id, $events[1]->other['choiceid']);
231
        $this->assertEquals($optionids[3], $events[1]->other['optionid']);
232
        $this->assertEventContextNotUsed($events[1]);
233
 
234
        $sink->close();
235
    }
236
 
237
    /**
238
     * Test to ensure that event data is being stored correctly.
239
     */
11 efrain 240
    public function test_answer_deleted(): void {
1 efrain 241
        global $DB, $USER;
242
        // Generate user data.
243
        $user = $this->getDataGenerator()->create_user();
244
 
245
        $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $this->choice->id)));
246
 
247
        // Create the first answer.
248
        choice_user_submit_response($optionids[2], $this->choice, $user->id, $this->course, $this->cm);
249
        // Get the users response.
250
        $answer = $DB->get_record('choice_answers', array('userid' => $user->id, 'choiceid' => $this->choice->id),
251
            '*', $strictness = IGNORE_MULTIPLE);
252
 
253
        // Redirect event.
254
        $sink = $this->redirectEvents();
255
        // Now delete the answer.
256
        choice_delete_responses(array($answer->id), $this->choice, $this->cm, $this->course);
257
 
258
        // Get our event event.
259
        $events = $sink->get_events();
260
        $event = reset($events);
261
 
262
        // Data checking.
263
        $this->assertInstanceOf('\mod_choice\event\answer_deleted', $event);
264
        $this->assertEquals($USER->id, $event->userid);
265
        $this->assertEquals($user->id, $event->relateduserid);
266
        $this->assertEquals(\context_module::instance($this->choice->cmid), $event->get_context());
267
        $this->assertEquals($this->choice->id, $event->other['choiceid']);
268
        $this->assertEquals($answer->optionid, $event->other['optionid']);
269
        $this->assertEventContextNotUsed($event);
270
        $sink->close();
271
    }
272
 
273
    /**
274
     * Test to ensure that event data is being stored correctly.
275
     */
11 efrain 276
    public function test_report_viewed(): void {
1 efrain 277
        global $USER;
278
 
279
        $this->resetAfterTest();
280
 
281
        // Generate user data.
282
        $this->setAdminUser();
283
 
284
        $eventdata = array();
285
        $eventdata['objectid'] = $this->choice->id;
286
        $eventdata['context'] = $this->context;
287
        $eventdata['courseid'] = $this->course->id;
288
        $eventdata['other']['content'] = 'choicereportcontentviewed';
289
 
290
        // This is fired in a page view so we can't run this through a function.
291
        $event = \mod_choice\event\report_viewed::create($eventdata);
292
 
293
        // Redirect event.
294
        $sink = $this->redirectEvents();
295
        $event->trigger();
296
        $event = $sink->get_events();
297
 
298
        // Data checking.
299
        $this->assertCount(1, $event);
300
        $this->assertInstanceOf('\mod_choice\event\report_viewed', $event[0]);
301
        $this->assertEquals($USER->id, $event[0]->userid);
302
        $this->assertEquals(\context_module::instance($this->choice->cmid), $event[0]->get_context());
303
        $sink->close();
304
    }
305
 
306
    /**
307
     * Test to ensure that event data is being stored correctly.
308
     */
11 efrain 309
    public function test_report_downloaded(): void {
1 efrain 310
        global $USER;
311
 
312
        $this->resetAfterTest();
313
 
314
        // Generate user data.
315
        $this->setAdminUser();
316
 
317
        $eventdata = array();
318
        $eventdata['context'] = $this->context;
319
        $eventdata['courseid'] = $this->course->id;
320
        $eventdata['other']['content'] = 'choicereportcontentviewed';
321
        $eventdata['other']['format'] = 'csv';
322
        $eventdata['other']['choiceid'] = $this->choice->id;
323
 
324
        // This is fired in a page view so we can't run this through a function.
325
        $event = \mod_choice\event\report_downloaded::create($eventdata);
326
 
327
        // Redirect event.
328
        $sink = $this->redirectEvents();
329
        $event->trigger();
330
        $event = $sink->get_events();
331
 
332
        // Data checking.
333
        $this->assertCount(1, $event);
334
        $this->assertInstanceOf('\mod_choice\event\report_downloaded', $event[0]);
335
        $this->assertEquals($USER->id, $event[0]->userid);
336
        $this->assertEquals(\context_module::instance($this->choice->cmid), $event[0]->get_context());
337
        $this->assertEquals('csv', $event[0]->other['format']);
338
        $this->assertEquals($this->choice->id, $event[0]->other['choiceid']);
339
        $this->assertEventContextNotUsed($event[0]);
340
        $sink->close();
341
    }
342
 
343
    /**
344
     * Test to ensure that event data is being stored correctly.
345
     */
11 efrain 346
    public function test_course_module_viewed(): void {
1 efrain 347
        global $USER;
348
 
349
        // Generate user data.
350
        $this->setAdminUser();
351
 
352
        $eventdata = array();
353
        $eventdata['objectid'] = $this->choice->id;
354
        $eventdata['context'] = $this->context;
355
        $eventdata['courseid'] = $this->course->id;
356
        $eventdata['other']['content'] = 'pageresourceview';
357
 
358
        // This is fired in a page view so we can't run this through a function.
359
        $event = \mod_choice\event\course_module_viewed::create($eventdata);
360
 
361
        // Redirect event.
362
        $sink = $this->redirectEvents();
363
        $event->trigger();
364
        $event = $sink->get_events();
365
 
366
        // Data checking.
367
        $this->assertCount(1, $event);
368
        $this->assertInstanceOf('\mod_choice\event\course_module_viewed', $event[0]);
369
        $this->assertEquals($USER->id, $event[0]->userid);
370
        $this->assertEquals(\context_module::instance($this->choice->cmid), $event[0]->get_context());
371
        $sink->close();
372
    }
373
 
374
    /**
375
     * Test to ensure that event data is being stored correctly.
376
     */
11 efrain 377
    public function test_course_module_instance_list_viewed_viewed(): void {
1 efrain 378
        global $USER;
379
 
380
        // Not much can be tested here as the event is only triggered on a page load,
381
        // let's just check that the event contains the expected basic information.
382
        $this->setAdminUser();
383
 
384
        $params = array('context' => \context_course::instance($this->course->id));
385
        $event = \mod_choice\event\course_module_instance_list_viewed::create($params);
386
        $sink = $this->redirectEvents();
387
        $event->trigger();
388
        $events = $sink->get_events();
389
        $event = reset($events);
390
        $this->assertInstanceOf('\mod_choice\event\course_module_instance_list_viewed', $event);
391
        $this->assertEquals($USER->id, $event->userid);
392
        $this->assertEquals(\context_course::instance($this->course->id), $event->get_context());
393
    }
394
}