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
 * Contains the event tests for the module assign.
19
 *
20
 * @package   mod_assign
21
 * @copyright 2014 Adrian Greeve <adrian@moodle.com>
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace mod_assign\event;
26
 
27
use mod_assign_test_generator;
28
 
29
defined('MOODLE_INTERNAL') || die();
30
 
31
global $CFG;
32
require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
33
require_once($CFG->dirroot . '/mod/assign/tests/fixtures/event_mod_assign_fixtures.php');
34
require_once($CFG->dirroot . '/mod/assign/locallib.php');
35
 
36
/**
37
 * Contains the event tests for the module assign.
38
 *
39
 * @package   mod_assign
40
 * @copyright 2014 Adrian Greeve <adrian@moodle.com>
41
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42
 */
1441 ariadna 43
final class events_test extends \advanced_testcase {
1 efrain 44
    // Use the generator helper.
45
    use mod_assign_test_generator;
46
 
47
    /**
48
     * Basic tests for the submission_created() abstract class.
49
     */
11 efrain 50
    public function test_base_event(): void {
1 efrain 51
        $this->resetAfterTest();
52
 
53
        $course = $this->getDataGenerator()->create_course();
54
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
55
        $instance = $generator->create_instance(array('course' => $course->id));
56
        $modcontext = \context_module::instance($instance->cmid);
57
 
58
        $data = array(
59
            'context' => $modcontext,
60
        );
61
 
62
        $event = \mod_assign_unittests\event\nothing_happened::create($data);
63
        $assign = $event->get_assign();
64
        $this->assertDebuggingCalled();
65
        $this->assertInstanceOf('assign', $assign);
66
 
67
        $event = \mod_assign_unittests\event\nothing_happened::create($data);
68
        $event->set_assign($assign);
69
        $assign2 = $event->get_assign();
70
        $this->assertDebuggingNotCalled();
71
        $this->assertSame($assign, $assign2);
72
    }
73
 
74
    /**
75
     * Basic tests for the submission_created() abstract class.
76
     */
11 efrain 77
    public function test_submission_created(): void {
1 efrain 78
        $this->resetAfterTest();
79
 
80
        $course = $this->getDataGenerator()->create_course();
81
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
82
        $instance = $generator->create_instance(array('course' => $course->id));
83
        $modcontext = \context_module::instance($instance->cmid);
84
 
85
        // Standard Event parameters.
86
        $params = array(
87
            'context' => $modcontext,
88
            'courseid' => $course->id
89
        );
90
 
91
        $eventinfo = $params;
92
        $eventinfo['other'] = array(
93
            'submissionid' => '17',
94
            'submissionattempt' => 0,
95
            'submissionstatus' => 'submitted'
96
        );
97
 
98
        $sink = $this->redirectEvents();
99
        $event = \mod_assign_unittests\event\submission_created::create($eventinfo);
100
        $event->trigger();
101
        $result = $sink->get_events();
102
        $event = reset($result);
103
        $sink->close();
104
 
105
        $this->assertEquals($modcontext->id, $event->contextid);
106
        $this->assertEquals($course->id, $event->courseid);
107
 
108
        // Check that an error occurs when teamsubmission is not set.
109
        try {
110
            \mod_assign_unittests\event\submission_created::create($params);
111
            $this->fail('Other must contain the key submissionid.');
112
        } catch (\Exception $e) {
113
            $this->assertInstanceOf('coding_exception', $e);
114
        }
115
        // Check that the submission status debugging is fired.
116
        $subinfo = $params;
117
        $subinfo['other'] = array('submissionid' => '23');
118
        try {
119
            \mod_assign_unittests\event\submission_created::create($subinfo);
120
            $this->fail('Other must contain the key submissionattempt.');
121
        } catch (\Exception $e) {
122
            $this->assertInstanceOf('coding_exception', $e);
123
        }
124
 
125
        $subinfo['other'] = array('submissionattempt' => '0');
126
        try {
127
            \mod_assign_unittests\event\submission_created::create($subinfo);
128
            $this->fail('Other must contain the key submissionstatus.');
129
        } catch (\Exception $e) {
130
            $this->assertInstanceOf('coding_exception', $e);
131
        }
132
    }
133
 
134
    /**
135
     * Basic tests for the submission_updated() abstract class.
136
     */
11 efrain 137
    public function test_submission_updated(): void {
1 efrain 138
        $this->resetAfterTest();
139
 
140
        $course = $this->getDataGenerator()->create_course();
141
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
142
        $instance = $generator->create_instance(array('course' => $course->id));
143
        $modcontext = \context_module::instance($instance->cmid);
144
 
145
        // Standard Event parameters.
146
        $params = array(
147
            'context' => $modcontext,
148
            'courseid' => $course->id
149
        );
150
 
151
        $eventinfo = $params;
152
        $eventinfo['other'] = array(
153
            'submissionid' => '17',
154
            'submissionattempt' => 0,
155
            'submissionstatus' => 'submitted'
156
        );
157
 
158
        $sink = $this->redirectEvents();
159
        $event = \mod_assign_unittests\event\submission_updated::create($eventinfo);
160
        $event->trigger();
161
        $result = $sink->get_events();
162
        $event = reset($result);
163
        $sink->close();
164
 
165
        $this->assertEquals($modcontext->id, $event->contextid);
166
        $this->assertEquals($course->id, $event->courseid);
167
 
168
        // Check that an error occurs when teamsubmission is not set.
169
        try {
170
            \mod_assign_unittests\event\submission_created::create($params);
171
            $this->fail('Other must contain the key submissionid.');
172
        } catch (\Exception $e) {
173
            $this->assertInstanceOf('coding_exception', $e);
174
        }
175
        // Check that the submission status debugging is fired.
176
        $subinfo = $params;
177
        $subinfo['other'] = array('submissionid' => '23');
178
        try {
179
            \mod_assign_unittests\event\submission_created::create($subinfo);
180
            $this->fail('Other must contain the key submissionattempt.');
181
        } catch (\Exception $e) {
182
            $this->assertInstanceOf('coding_exception', $e);
183
        }
184
 
185
        $subinfo['other'] = array('submissionattempt' => '0');
186
        try {
187
            \mod_assign_unittests\event\submission_created::create($subinfo);
188
            $this->fail('Other must contain the key submissionstatus.');
189
        } catch (\Exception $e) {
190
            $this->assertInstanceOf('coding_exception', $e);
191
        }
192
    }
193
 
194
    /**
195
     * Test submission_removed event.
196
     *
197
     * @covers \mod_assign\event\submission_removed
198
     */
11 efrain 199
    public function test_submission_removed(): void {
1 efrain 200
        $this->resetAfterTest();
201
 
202
        $course = $this->getDataGenerator()->create_course();
203
        $this->getDataGenerator()->create_and_enrol($course, 'teacher');
204
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
205
 
206
        $assign = $this->create_instance($course);
207
        $this->add_submission($student, $assign);
208
        $submission = $assign->get_user_submission($student->id, 0);
209
 
210
        $sink = $this->redirectEvents();
211
        $assign->remove_submission($student->id);
212
        $events = $sink->get_events();
213
        $this->assertCount(2, $events);
214
        $event = $events[0];
215
        $this->assertInstanceOf('mod_assign\event\submission_removed', $event);
216
        $this->assertEquals($assign->get_context(), $event->get_context());
217
        $this->assertEquals($submission->id, $event->objectid);
218
        $this->assertEquals($student->id, $event->relateduserid);
219
        $this->assertEquals($submission->id, $event->other['submissionid']);
220
        $this->assertEquals(0, $event->other['submissionattempt']);
221
        $this->assertEquals(ASSIGN_SUBMISSION_STATUS_NEW, $event->other['submissionstatus']);
222
        $this->assertEquals(0, $event->other['groupid']);
223
        $this->assertEquals(null, $event->other['groupname']);
224
        $sink->close();
225
    }
226
 
227
    /**
228
     * Test submission_removed event when a team submission is removed.
229
     *
230
     * @covers \mod_assign\event\submission_removed
231
     */
11 efrain 232
    public function test_team_submission_removed(): void {
1 efrain 233
        $this->resetAfterTest();
234
 
235
        $course = $this->getDataGenerator()->create_course();
236
        $this->getDataGenerator()->create_and_enrol($course, 'teacher');
237
        $group = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
238
 
239
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
240
        groups_add_member($group, $student);
241
 
242
        $otherstudent = $this->getDataGenerator()->create_and_enrol($course, 'student');
243
        groups_add_member($group, $otherstudent);
244
 
245
        $assign = $this->create_instance($course, [
246
            'teamsubmission' => 1,
247
        ]);
248
        $this->add_submission($student, $assign);
249
        $submission = $assign->get_group_submission($student->id, 0, true);
250
 
251
        $sink = $this->redirectEvents();
252
        $assign->remove_submission($student->id);
253
        $events = $sink->get_events();
254
        $this->assertCount(2, $events);
255
        $event = $events[0];
256
        $this->assertInstanceOf('mod_assign\event\submission_removed', $event);
257
        $this->assertEquals($assign->get_context(), $event->get_context());
258
        $this->assertEquals($submission->id, $event->objectid);
259
        $this->assertEquals(null, $event->relateduserid);
260
        $this->assertEquals($submission->id, $event->other['submissionid']);
261
        $this->assertEquals(0, $event->other['submissionattempt']);
262
        $this->assertEquals(ASSIGN_SUBMISSION_STATUS_NEW, $event->other['submissionstatus']);
263
        $this->assertEquals($group->id, $event->other['groupid']);
264
        $this->assertEquals($group->name, $event->other['groupname']);
265
        $sink->close();
266
    }
267
 
1441 ariadna 268
    /**
269
     * Test event creation for save_user_extension().
270
     *
271
     * @covers \assign::save_user_extension
272
     */
11 efrain 273
    public function test_extension_granted(): void {
1441 ariadna 274
        global $DB, $CFG;
275
        require_once($CFG->dirroot.'/calendar/lib.php');
276
 
1 efrain 277
        $this->resetAfterTest();
278
 
279
        $course = $this->getDataGenerator()->create_course();
280
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
281
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
282
 
283
        $this->setUser($teacher);
284
 
285
        $now = time();
286
        $tomorrow = $now + DAYSECS;
287
        $yesterday = $now - DAYSECS;
288
 
289
        $assign = $this->create_instance($course, [
290
            'duedate' => $yesterday,
291
            'cutoffdate' => $yesterday,
292
        ]);
293
        $sink = $this->redirectEvents();
294
 
295
        $assign->testable_save_user_extension($student->id, $tomorrow);
296
 
297
        $events = $sink->get_events();
1441 ariadna 298
 
299
        // Event for extension granted and extension due date.
300
        $this->assertCount(2, $events);
301
 
302
        $grantedevent = $events[0];
303
        $this->assertInstanceOf('\mod_assign\event\extension_granted', $grantedevent);
304
        $this->assertEquals($assign->get_context(), $grantedevent->get_context());
305
        $this->assertEquals($assign->get_instance()->id, $grantedevent->objectid);
306
        $this->assertEquals($student->id, $grantedevent->relateduserid);
307
 
308
        $calendarevent = $events[1];
309
        $this->assertInstanceOf('\core\event\calendar_event_created', $calendarevent);
310
 
311
        // Check that the calendar event is deleted if extension is revoked.
312
        $assign->testable_save_user_extension($student->id, '');
313
 
314
        $isexist = $DB->record_exists('event', [
315
            'userid' => $student->id,
316
            'eventtype' => ASSIGN_EVENT_TYPE_EXTENSION,
317
            'modulename' => 'assign',
318
            'instance' => $assign->get_course_module()->id,
319
        ]);
320
        $this->assertFalse($isexist);
321
 
1 efrain 322
        $sink->close();
323
    }
324
 
11 efrain 325
    public function test_submission_locked(): void {
1 efrain 326
        $this->resetAfterTest();
327
 
328
        $course = $this->getDataGenerator()->create_course();
329
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
330
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
331
 
332
        $teacher->ignoresesskey = true;
333
        $this->setUser($teacher);
334
 
335
        $assign = $this->create_instance($course);
336
        $sink = $this->redirectEvents();
337
 
338
        $assign->lock_submission($student->id);
339
 
340
        $events = $sink->get_events();
341
        $this->assertCount(1, $events);
342
        $event = reset($events);
343
        $this->assertInstanceOf('\mod_assign\event\submission_locked', $event);
344
        $this->assertEquals($assign->get_context(), $event->get_context());
345
        $this->assertEquals($assign->get_instance()->id, $event->objectid);
346
        $this->assertEquals($student->id, $event->relateduserid);
347
        $sink->close();
348
    }
349
 
11 efrain 350
    public function test_identities_revealed(): void {
1 efrain 351
        $this->resetAfterTest();
352
 
353
        $course = $this->getDataGenerator()->create_course();
354
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
355
 
356
        $teacher->ignoresesskey = true;
357
        $this->setUser($teacher);
358
 
359
        $assign = $this->create_instance($course, ['blindmarking' => 1]);
360
        $sink = $this->redirectEvents();
361
 
362
        $assign->reveal_identities();
363
 
364
        $events = $sink->get_events();
365
        $eventscount = 0;
366
 
367
        foreach ($events as $event) {
368
            if ($event instanceof \mod_assign\event\identities_revealed) {
369
                $eventscount++;
370
                $this->assertInstanceOf('\mod_assign\event\identities_revealed', $event);
371
                $this->assertEquals($assign->get_context(), $event->get_context());
372
                $this->assertEquals($assign->get_instance()->id, $event->objectid);
373
            }
374
        }
375
 
376
        $this->assertEquals(1, $eventscount);
377
        $sink->close();
378
    }
379
 
380
    /**
381
     * Test the submission_status_viewed event.
382
     */
11 efrain 383
    public function test_submission_status_viewed(): void {
1 efrain 384
        global $PAGE;
385
        $this->resetAfterTest();
386
 
387
        $course = $this->getDataGenerator()->create_course();
388
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
389
 
390
        $this->setUser($teacher);
391
 
392
        $assign = $this->create_instance($course);
393
 
394
        // We need to set the URL in order to view the feedback.
395
        $PAGE->set_url('/a_url');
396
 
397
        // Trigger and capture the event.
398
        $sink = $this->redirectEvents();
399
        $assign->view();
400
        $events = $sink->get_events();
401
        $this->assertCount(1, $events);
402
        $event = reset($events);
403
 
404
        // Check that the event contains the expected values.
405
        $this->assertInstanceOf('\mod_assign\event\submission_status_viewed', $event);
406
        $this->assertEquals($assign->get_context(), $event->get_context());
407
    }
408
 
409
    /**
410
     * Test submission_status_updated event when a submission is updated.
411
     *
412
     * @covers \mod_assign\event\submission_status_updated
413
     */
11 efrain 414
    public function test_submission_status_updated_on_update(): void {
1 efrain 415
        $this->resetAfterTest();
416
 
417
        $course = $this->getDataGenerator()->create_course();
418
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
419
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
420
 
421
        $this->setUser($teacher);
422
 
423
        $assign = $this->create_instance($course);
424
        $submission = $assign->get_user_submission($student->id, true);
425
        $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
426
        $assign->testable_update_submission($submission, $student->id, true, false);
427
 
428
        $sink = $this->redirectEvents();
429
        $assign->revert_to_draft($student->id);
430
 
431
        $events = $sink->get_events();
432
        $this->assertCount(2, $events);
433
        $event = $events[1];
434
        $this->assertInstanceOf('\mod_assign\event\submission_status_updated', $event);
435
        $this->assertEquals($assign->get_context(), $event->get_context());
436
        $this->assertEquals($submission->id, $event->objectid);
437
        $this->assertEquals($student->id, $event->relateduserid);
438
        $this->assertEquals(ASSIGN_SUBMISSION_STATUS_DRAFT, $event->other['newstatus']);
439
        $sink->close();
440
    }
441
 
442
    /**
443
     * Test submission_status_updated event when a submission is removed.
444
     *
445
     * @covers \mod_assign\event\submission_status_updated
446
     */
11 efrain 447
    public function test_submission_status_updated_on_remove(): void {
1 efrain 448
        $this->resetAfterTest();
449
 
450
        $course = $this->getDataGenerator()->create_course();
451
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
452
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
453
 
454
        $assign = $this->create_instance($course);
455
        $this->add_submission($student, $assign);
456
        $submission = $assign->get_user_submission($student->id, false);
457
 
458
        $sink = $this->redirectEvents();
459
        $assign->remove_submission($student->id);
460
 
461
        $events = $sink->get_events();
462
        $this->assertCount(2, $events);
463
 
464
        $event = $events[1];
465
        $this->assertInstanceOf('\mod_assign\event\submission_status_updated', $event);
466
        $this->assertEquals($assign->get_context(), $event->get_context());
467
        $this->assertEquals($submission->id, $event->objectid);
468
        $this->assertEquals($student->id, $event->relateduserid);
469
        $this->assertEquals(ASSIGN_SUBMISSION_STATUS_NEW, $event->other['newstatus']);
470
        $sink->close();
471
    }
472
 
473
    /**
474
     * Test submission_status_updated event when a team submission is removed.
475
     *
476
     * @covers \mod_assign\event\submission_status_updated
477
     */
11 efrain 478
    public function test_team_submission_status_updated_on_remove(): void {
1 efrain 479
        $this->resetAfterTest();
480
 
481
        $course = $this->getDataGenerator()->create_course();
482
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
483
        $group = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
484
 
485
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
486
        groups_add_member($group, $student);
487
 
488
        $otherstudent = $this->getDataGenerator()->create_and_enrol($course, 'student');
489
        groups_add_member($group, $otherstudent);
490
 
491
        $assign = $this->create_instance($course, [
492
            'teamsubmission' => 1,
493
        ]);
494
        $this->add_submission($student, $assign);
495
        $submission = $assign->get_group_submission($student->id, 0, false);
496
 
497
        $sink = $this->redirectEvents();
498
        $assign->remove_submission($student->id);
499
 
500
        $events = $sink->get_events();
501
        $this->assertCount(2, $events);
502
 
503
        $event = $events[1];
504
        $this->assertInstanceOf('\mod_assign\event\submission_status_updated', $event);
505
        $this->assertEquals($assign->get_context(), $event->get_context());
506
        $this->assertEquals($submission->id, $event->objectid);
507
        $this->assertEquals(null, $event->relateduserid);
508
        $this->assertEquals(ASSIGN_SUBMISSION_STATUS_NEW, $event->other['newstatus']);
509
        $sink->close();
510
    }
511
 
11 efrain 512
    public function test_marker_updated(): void {
1 efrain 513
        $this->resetAfterTest();
514
 
515
        $course = $this->getDataGenerator()->create_course();
516
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
517
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
518
 
519
        $teacher->ignoresesskey = true;
520
        $this->setUser($teacher);
521
 
522
        $assign = $this->create_instance($course);
523
 
524
        $sink = $this->redirectEvents();
525
        $assign->testable_process_set_batch_marking_allocation($student->id, $teacher->id);
526
 
527
        $events = $sink->get_events();
528
        $this->assertCount(1, $events);
529
        $event = reset($events);
530
        $this->assertInstanceOf('\mod_assign\event\marker_updated', $event);
531
        $this->assertEquals($assign->get_context(), $event->get_context());
532
        $this->assertEquals($assign->get_instance()->id, $event->objectid);
533
        $this->assertEquals($student->id, $event->relateduserid);
534
        $this->assertEquals($teacher->id, $event->userid);
535
        $this->assertEquals($teacher->id, $event->other['markerid']);
536
        $sink->close();
537
    }
538
 
11 efrain 539
    public function test_workflow_state_updated(): void {
1 efrain 540
        $this->resetAfterTest();
541
 
542
        $course = $this->getDataGenerator()->create_course();
543
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
544
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
545
 
546
        $teacher->ignoresesskey = true;
547
        $this->setUser($teacher);
548
 
549
        $assign = $this->create_instance($course);
550
 
551
        // Test process_set_batch_marking_workflow_state.
552
        $sink = $this->redirectEvents();
553
        $assign->testable_process_set_batch_marking_workflow_state($student->id, ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW);
554
 
555
        $events = $sink->get_events();
556
        $eventcount = 0;
557
        foreach ($events as $event) {
558
            if ($event instanceof \mod_assign\event\submission_graded) {
559
                $eventcount++;
560
                $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
561
                $this->assertEquals($assign->get_context(), $event->get_context());
562
            }
563
            if ($event instanceof \mod_assign\event\workflow_state_updated) {
564
                $eventcount++;
565
                $this->assertInstanceOf('\mod_assign\event\workflow_state_updated', $event);
566
                $this->assertEquals($assign->get_context(), $event->get_context());
567
                $this->assertEquals($assign->get_instance()->id, $event->objectid);
568
                $this->assertEquals($student->id, $event->relateduserid);
569
                $this->assertEquals($teacher->id, $event->userid);
570
                $this->assertEquals(ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW, $event->other['newstate']);
571
            }
572
        }
573
        $this->assertEquals(2, $eventcount);
574
        $sink->close();
575
 
576
        // Test setting workflow state in apply_grade_to_user.
577
        $sink = $this->redirectEvents();
578
        $data = new \stdClass();
579
        $data->grade = '50.0';
580
        $data->workflowstate = 'readyforrelease';
581
        $assign->testable_apply_grade_to_user($data, $student->id, 0);
582
 
583
        $events = $sink->get_events();
584
        $this->assertCount(4, $events);
585
        $event = reset($events);
586
        $this->assertInstanceOf('\mod_assign\event\workflow_state_updated', $event);
587
        $this->assertEquals($assign->get_context(), $event->get_context());
588
        $this->assertEquals($assign->get_instance()->id, $event->objectid);
589
        $this->assertEquals($student->id, $event->relateduserid);
590
        $this->assertEquals($teacher->id, $event->userid);
591
        $this->assertEquals(ASSIGN_MARKING_WORKFLOW_STATE_READYFORRELEASE, $event->other['newstate']);
592
        $sink->close();
593
 
594
        // Test setting workflow state in process_save_quick_grades.
595
        $sink = $this->redirectEvents();
596
 
597
        $data = array(
598
            'grademodified_' . $student->id => time(),
599
            'gradeattempt_' . $student->id => '',
600
            'quickgrade_' . $student->id => '60.0',
601
            'quickgrade_' . $student->id . '_workflowstate' => 'inmarking'
602
        );
603
        $assign->testable_process_save_quick_grades($data);
604
 
605
        $events = $sink->get_events();
606
        $this->assertCount(4, $events);
607
        $event = reset($events);
608
        $this->assertInstanceOf('\mod_assign\event\workflow_state_updated', $event);
609
        $this->assertEquals($assign->get_context(), $event->get_context());
610
        $this->assertEquals($assign->get_instance()->id, $event->objectid);
611
        $this->assertEquals($student->id, $event->relateduserid);
612
        $this->assertEquals($teacher->id, $event->userid);
613
        $this->assertEquals(ASSIGN_MARKING_WORKFLOW_STATE_INMARKING, $event->other['newstate']);
614
        $sink->close();
615
    }
616
 
11 efrain 617
    public function test_submission_duplicated(): void {
1 efrain 618
        $this->resetAfterTest();
619
 
620
        $course = $this->getDataGenerator()->create_course();
621
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
622
 
623
        $this->setUser($student);
624
 
625
        $assign = $this->create_instance($course);
626
        $submission1 = $assign->get_user_submission($student->id, true, 0);
627
        $submission2 = $assign->get_user_submission($student->id, true, 1);
628
        $submission2->status = ASSIGN_SUBMISSION_STATUS_REOPENED;
629
        $assign->testable_update_submission($submission2, $student->id, time(), $assign->get_instance()->teamsubmission);
630
 
631
        $sink = $this->redirectEvents();
632
        $notices = null;
633
        $assign->copy_previous_attempt($notices);
634
 
635
        $events = $sink->get_events();
636
        $this->assertCount(1, $events);
637
        $event = reset($events);
638
        $this->assertInstanceOf('\mod_assign\event\submission_duplicated', $event);
639
        $this->assertEquals($assign->get_context(), $event->get_context());
640
        $this->assertEquals($submission2->id, $event->objectid);
641
        $this->assertEquals($student->id, $event->userid);
642
        $submission2->status = ASSIGN_SUBMISSION_STATUS_DRAFT;
643
        $sink->close();
644
    }
645
 
11 efrain 646
    public function test_submission_unlocked(): void {
1 efrain 647
        $this->resetAfterTest();
648
 
649
        $course = $this->getDataGenerator()->create_course();
650
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
651
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
652
 
653
        $teacher->ignoresesskey = true;
654
        $this->setUser($teacher);
655
 
656
        $assign = $this->create_instance($course);
657
        $sink = $this->redirectEvents();
658
 
659
        $assign->unlock_submission($student->id);
660
 
661
        $events = $sink->get_events();
662
        $this->assertCount(1, $events);
663
        $event = reset($events);
664
        $this->assertInstanceOf('\mod_assign\event\submission_unlocked', $event);
665
        $this->assertEquals($assign->get_context(), $event->get_context());
666
        $this->assertEquals($assign->get_instance()->id, $event->objectid);
667
        $this->assertEquals($student->id, $event->relateduserid);
668
        $sink->close();
669
    }
670
 
11 efrain 671
    public function test_submission_graded(): void {
1 efrain 672
        $this->resetAfterTest();
673
 
674
        $course = $this->getDataGenerator()->create_course();
675
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
676
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
677
 
678
        $teacher->ignoresesskey = true;
679
        $this->setUser($teacher);
680
 
681
        $assign = $this->create_instance($course);
682
 
683
        // Test apply_grade_to_user.
684
        $sink = $this->redirectEvents();
685
 
686
        $data = new \stdClass();
687
        $data->grade = '50.0';
688
        $assign->testable_apply_grade_to_user($data, $student->id, 0);
689
        $grade = $assign->get_user_grade($student->id, false, 0);
690
 
691
        $events = $sink->get_events();
692
        $this->assertCount(3, $events);
693
        $event = $events[2];
694
        $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
695
        $this->assertEquals($assign->get_context(), $event->get_context());
696
        $this->assertEquals($grade->id, $event->objectid);
697
        $this->assertEquals($student->id, $event->relateduserid);
698
        $sink->close();
699
 
700
        // Test process_save_quick_grades.
701
        $sink = $this->redirectEvents();
702
 
703
        $grade = $assign->get_user_grade($student->id, false);
704
        $data = array(
705
            'grademodified_' . $student->id => time(),
706
            'gradeattempt_' . $student->id => $grade->attemptnumber,
707
            'quickgrade_' . $student->id => '60.0'
708
        );
709
        $assign->testable_process_save_quick_grades($data);
710
        $grade = $assign->get_user_grade($student->id, false);
711
        $this->assertEquals(60.0, $grade->grade);
712
 
713
        $events = $sink->get_events();
714
        $this->assertCount(3, $events);
715
        $event = $events[2];
716
        $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
717
        $this->assertEquals($assign->get_context(), $event->get_context());
718
        $this->assertEquals($grade->id, $event->objectid);
719
        $this->assertEquals($student->id, $event->relateduserid);
720
        $sink->close();
721
 
722
        // Test update_grade.
723
        $sink = $this->redirectEvents();
724
        $data = clone($grade);
725
        $data->grade = '50.0';
726
        $assign->update_grade($data);
727
        $grade = $assign->get_user_grade($student->id, false, 0);
728
        $this->assertEquals(50.0, $grade->grade);
729
        $events = $sink->get_events();
730
 
731
        $this->assertCount(3, $events);
732
        $event = $events[2];
733
        $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
734
        $this->assertEquals($assign->get_context(), $event->get_context());
735
        $this->assertEquals($grade->id, $event->objectid);
736
        $this->assertEquals($student->id, $event->relateduserid);
737
        $sink->close();
738
    }
739
 
740
    /**
741
     * Test the submission_viewed event.
742
     */
11 efrain 743
    public function test_submission_viewed(): void {
1 efrain 744
        global $PAGE;
745
 
746
        $this->resetAfterTest();
747
 
748
        $course = $this->getDataGenerator()->create_course();
749
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
750
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
751
 
752
        $this->setUser($teacher);
753
 
754
        $assign = $this->create_instance($course);
755
        $submission = $assign->get_user_submission($student->id, true);
756
 
757
        // We need to set the URL in order to view the submission.
758
        $PAGE->set_url('/a_url');
759
        // A hack - these variables are used by the view_plugin_content function to
760
        // determine what we actually want to view - would usually be set in URL.
761
        global $_POST;
762
        $_POST['plugin'] = 'comments';
763
        $_POST['sid'] = $submission->id;
764
 
765
        // Trigger and capture the event.
766
        $sink = $this->redirectEvents();
767
        $assign->view('viewpluginassignsubmission');
768
        $events = $sink->get_events();
769
        $this->assertCount(1, $events);
770
        $event = reset($events);
771
 
772
        // Check that the event contains the expected values.
773
        $this->assertInstanceOf('\mod_assign\event\submission_viewed', $event);
774
        $this->assertEquals($assign->get_context(), $event->get_context());
775
        $this->assertEquals($submission->id, $event->objectid);
776
    }
777
 
778
    /**
779
     * Test the feedback_viewed event.
780
     */
11 efrain 781
    public function test_feedback_viewed(): void {
1 efrain 782
        global $DB, $PAGE;
783
 
784
        $this->resetAfterTest();
785
 
786
        $course = $this->getDataGenerator()->create_course();
787
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
788
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
789
 
790
        $this->setUser($teacher);
791
 
792
        $assign = $this->create_instance($course);
793
        $submission = $assign->get_user_submission($student->id, true);
794
 
795
        // Insert a grade for this submission.
796
        $grade = new \stdClass();
797
        $grade->assignment = $assign->get_instance()->id;
798
        $grade->userid = $student->id;
799
        $gradeid = $DB->insert_record('assign_grades', $grade);
800
 
801
        // We need to set the URL in order to view the feedback.
802
        $PAGE->set_url('/a_url');
803
        // A hack - these variables are used by the view_plugin_content function to
804
        // determine what we actually want to view - would usually be set in URL.
805
        global $_POST;
806
        $_POST['plugin'] = 'comments';
807
        $_POST['gid'] = $gradeid;
808
        $_POST['sid'] = $submission->id;
809
 
810
        // Trigger and capture the event.
811
        $sink = $this->redirectEvents();
812
        $assign->view('viewpluginassignfeedback');
813
        $events = $sink->get_events();
814
        $this->assertCount(1, $events);
815
        $event = reset($events);
816
 
817
        // Check that the event contains the expected values.
818
        $this->assertInstanceOf('\mod_assign\event\feedback_viewed', $event);
819
        $this->assertEquals($assign->get_context(), $event->get_context());
820
        $this->assertEquals($gradeid, $event->objectid);
821
    }
822
 
823
    /**
824
     * Test the grading_form_viewed event.
825
     */
11 efrain 826
    public function test_grading_form_viewed(): void {
1 efrain 827
        global $PAGE;
828
 
829
        $this->resetAfterTest();
830
 
831
        $course = $this->getDataGenerator()->create_course();
832
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
833
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
834
 
835
        $this->setUser($teacher);
836
 
837
        $assign = $this->create_instance($course);
838
 
839
        // We need to set the URL in order to view the feedback.
840
        $PAGE->set_url('/a_url');
841
        // A hack - this variable is used by the view_single_grade_page function.
842
        global $_POST;
843
        $_POST['rownum'] = 1;
844
        $_POST['userid'] = $student->id;
845
 
846
        // Trigger and capture the event.
847
        $sink = $this->redirectEvents();
848
        $assign->view('grade');
849
        $events = $sink->get_events();
850
        $this->assertCount(1, $events);
851
        $event = reset($events);
852
 
853
        // Check that the event contains the expected values.
854
        $this->assertInstanceOf('\mod_assign\event\grading_form_viewed', $event);
855
        $this->assertEquals($assign->get_context(), $event->get_context());
856
    }
857
 
858
    /**
859
     * Test the grading_table_viewed event.
860
     */
11 efrain 861
    public function test_grading_table_viewed(): void {
1 efrain 862
        global $PAGE;
863
 
864
        $this->resetAfterTest();
865
 
866
        $course = $this->getDataGenerator()->create_course();
867
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
868
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
869
 
870
        $this->setUser($teacher);
871
 
872
        $assign = $this->create_instance($course);
873
 
874
        // We need to set the URL in order to view the feedback.
875
        $PAGE->set_url('/a_url');
876
        // A hack - this variable is used by the view_single_grade_page function.
877
        global $_POST;
878
        $_POST['rownum'] = 1;
879
        $_POST['userid'] = $student->id;
880
 
881
        // Trigger and capture the event.
882
        $sink = $this->redirectEvents();
883
        $assign->view('grading');
884
        $events = $sink->get_events();
885
        $this->assertCount(1, $events);
886
        $event = reset($events);
887
 
888
        // Check that the event contains the expected values.
889
        $this->assertInstanceOf('\mod_assign\event\grading_table_viewed', $event);
890
        $this->assertEquals($assign->get_context(), $event->get_context());
891
        $this->assertEventContextNotUsed($event);
892
    }
893
 
894
    /**
895
     * Test the submission_form_viewed event.
896
     */
11 efrain 897
    public function test_submission_form_viewed(): void {
1 efrain 898
        global $PAGE;
899
 
900
        $this->resetAfterTest();
901
 
902
        $course = $this->getDataGenerator()->create_course();
903
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
904
 
905
        $this->setUser($student);
906
 
907
        $assign = $this->create_instance($course);
908
 
909
        // We need to set the URL in order to view the submission form.
910
        $PAGE->set_url('/a_url');
911
 
912
        // Trigger and capture the event.
913
        $sink = $this->redirectEvents();
914
        $assign->view('editsubmission');
915
        $events = $sink->get_events();
916
        $this->assertCount(1, $events);
917
        $event = reset($events);
918
 
919
        // Check that the event contains the expected values.
920
        $this->assertInstanceOf('\mod_assign\event\submission_form_viewed', $event);
921
        $this->assertEquals($assign->get_context(), $event->get_context());
922
        $this->assertEventContextNotUsed($event);
923
    }
924
 
925
    /**
926
     * Test the submission_form_viewed event.
927
     */
11 efrain 928
    public function test_submission_confirmation_form_viewed(): void {
1 efrain 929
        global $PAGE;
930
 
931
        $this->resetAfterTest();
932
 
933
        $course = $this->getDataGenerator()->create_course();
934
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
935
 
936
        $this->setUser($student);
937
 
938
        $assign = $this->create_instance($course);
939
 
940
        // We need to set the URL in order to view the submission form.
941
        $PAGE->set_url('/a_url');
942
 
943
        // Trigger and capture the event.
944
        $sink = $this->redirectEvents();
945
        $assign->view('submit');
946
        $events = $sink->get_events();
947
        $this->assertCount(1, $events);
948
        $event = reset($events);
949
 
950
        // Check that the event contains the expected values.
951
        $this->assertInstanceOf('\mod_assign\event\submission_confirmation_form_viewed', $event);
952
        $this->assertEquals($assign->get_context(), $event->get_context());
953
        $this->assertEventContextNotUsed($event);
954
    }
955
 
956
    /**
957
     * Test the reveal_identities_confirmation_page_viewed event.
958
     */
11 efrain 959
    public function test_reveal_identities_confirmation_page_viewed(): void {
1 efrain 960
        global $PAGE;
961
        $this->resetAfterTest();
962
 
963
        // Set to the admin user so we have the permission to reveal identities.
964
        $this->setAdminUser();
965
 
966
        $course = $this->getDataGenerator()->create_course();
967
        $assign = $this->create_instance($course);
968
 
969
        // We need to set the URL in order to view the submission form.
970
        $PAGE->set_url('/a_url');
971
 
972
        // Trigger and capture the event.
973
        $sink = $this->redirectEvents();
974
        $assign->view('revealidentities');
975
        $events = $sink->get_events();
976
        $this->assertCount(1, $events);
977
        $event = reset($events);
978
 
979
        // Check that the event contains the expected values.
980
        $this->assertInstanceOf('\mod_assign\event\reveal_identities_confirmation_page_viewed', $event);
981
        $this->assertEquals($assign->get_context(), $event->get_context());
982
        $this->assertEventContextNotUsed($event);
983
    }
984
 
985
    /**
986
     * Test the statement_accepted event.
987
     */
11 efrain 988
    public function test_statement_accepted(): void {
1 efrain 989
        // We want to be a student so we can submit assignments.
990
        $this->resetAfterTest();
991
 
992
        $course = $this->getDataGenerator()->create_course();
993
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
994
 
995
        $this->setUser($student);
996
 
997
        // We do not want to send any messages to the student during the PHPUNIT test.
998
        set_config('submissionreceipts', false, 'assign');
999
 
1000
        $assign = $this->create_instance($course);
1001
 
1002
        // Create the data we want to pass to the submit_for_grading function.
1003
        $data = new \stdClass();
1004
        $data->submissionstatement = 'We are the Borg. You will be assimilated. Resistance is futile. - do you agree
1005
            to these terms?';
1006
 
1007
        // Trigger and capture the event.
1008
        $sink = $this->redirectEvents();
1009
        $assign->submit_for_grading($data, array());
1010
        $events = $sink->get_events();
1011
        $event = reset($events);
1012
 
1013
        // Check that the event contains the expected values.
1014
        $this->assertInstanceOf('\mod_assign\event\statement_accepted', $event);
1015
        $this->assertEquals($assign->get_context(), $event->get_context());
1016
        $this->assertEventContextNotUsed($event);
1017
 
1018
        // Enable the online text submission plugin.
1019
        $submissionplugins = $assign->get_submission_plugins();
1020
        foreach ($submissionplugins as $plugin) {
1021
            if ($plugin->get_type() === 'onlinetext') {
1022
                $plugin->enable();
1023
                break;
1024
            }
1025
        }
1026
 
1027
        // Create the data we want to pass to the save_submission function.
1028
        $data = new \stdClass();
1029
        $data->onlinetext_editor = array(
1030
            'text' => 'Online text',
1031
            'format' => FORMAT_HTML,
1032
            'itemid' => file_get_unused_draft_itemid()
1033
        );
1034
        $data->submissionstatement = 'We are the Borg. You will be assimilated. Resistance is futile. - do you agree
1035
            to these terms?';
1036
 
1037
        // Trigger and capture the event.
1038
        $sink = $this->redirectEvents();
1039
        $assign->save_submission($data, $notices);
1040
        $events = $sink->get_events();
1041
        $event = $events[2];
1042
 
1043
        // Check that the event contains the expected values.
1044
        $this->assertInstanceOf('\mod_assign\event\statement_accepted', $event);
1045
        $this->assertEquals($assign->get_context(), $event->get_context());
1046
        $this->assertEventContextNotUsed($event);
1047
    }
1048
 
1049
    /**
1050
     * Test the batch_set_workflow_state_viewed event.
1051
     */
11 efrain 1052
    public function test_batch_set_workflow_state_viewed(): void {
1 efrain 1053
        $this->resetAfterTest();
1054
 
1055
        $course = $this->getDataGenerator()->create_course();
1056
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
1057
        $assign = $this->create_instance($course);
1058
 
1059
        // Trigger and capture the event.
1060
        $sink = $this->redirectEvents();
1061
        $assign->testable_view_batch_set_workflow_state($student->id);
1062
        $events = $sink->get_events();
1063
        $event = reset($events);
1064
 
1065
        // Check that the event contains the expected values.
1066
        $this->assertInstanceOf('\mod_assign\event\batch_set_workflow_state_viewed', $event);
1067
        $this->assertEquals($assign->get_context(), $event->get_context());
1068
        $this->assertEventContextNotUsed($event);
1069
    }
1070
 
1071
    /**
1072
     * Test the batch_set_marker_allocation_viewed event.
1073
     */
11 efrain 1074
    public function test_batch_set_marker_allocation_viewed(): void {
1 efrain 1075
        $this->resetAfterTest();
1076
 
1077
        $course = $this->getDataGenerator()->create_course();
1078
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
1079
        $assign = $this->create_instance($course);
1080
 
1081
        // Trigger and capture the event.
1082
        $sink = $this->redirectEvents();
1083
        $assign->testable_view_batch_markingallocation($student->id);
1084
        $events = $sink->get_events();
1085
        $event = reset($events);
1086
 
1087
        // Check that the event contains the expected values.
1088
        $this->assertInstanceOf('\mod_assign\event\batch_set_marker_allocation_viewed', $event);
1089
        $this->assertEquals($assign->get_context(), $event->get_context());
1090
        $this->assertEventContextNotUsed($event);
1091
    }
1092
 
1093
    /**
1094
     * Test the user override created event.
1095
     *
1096
     * There is no external API for creating a user override, so the unit test will simply
1097
     * create and trigger the event and ensure the event data is returned as expected.
1098
     */
11 efrain 1099
    public function test_user_override_created(): void {
1 efrain 1100
        $this->resetAfterTest();
1101
 
1102
        $course = $this->getDataGenerator()->create_course();
1103
        $assign = $this->getDataGenerator()->get_plugin_generator('mod_assign')->create_instance(['course' => $course->id]);
1104
 
1105
        $params = array(
1106
            'objectid' => 1,
1107
            'relateduserid' => 2,
1108
            'context' => \context_module::instance($assign->cmid),
1109
            'other' => array(
1110
                'assignid' => $assign->id
1111
            )
1112
        );
1113
        $event = \mod_assign\event\user_override_created::create($params);
1114
 
1115
        // Trigger and capture the event.
1116
        $sink = $this->redirectEvents();
1117
        $event->trigger();
1118
        $events = $sink->get_events();
1119
        $event = reset($events);
1120
 
1121
        // Check that the event data is valid.
1122
        $this->assertInstanceOf('\mod_assign\event\user_override_created', $event);
1123
        $this->assertEquals(\context_module::instance($assign->cmid), $event->get_context());
1124
        $this->assertEventContextNotUsed($event);
1125
    }
1126
 
1127
    /**
1128
     * Test the group override created event.
1129
     *
1130
     * There is no external API for creating a group override, so the unit test will simply
1131
     * create and trigger the event and ensure the event data is returned as expected.
1132
     */
11 efrain 1133
    public function test_group_override_created(): void {
1 efrain 1134
        $this->resetAfterTest();
1135
 
1136
        $course = $this->getDataGenerator()->create_course();
1137
        $assign = $this->getDataGenerator()->get_plugin_generator('mod_assign')->create_instance(['course' => $course->id]);
1138
 
1139
        $params = array(
1140
            'objectid' => 1,
1141
            'context' => \context_module::instance($assign->cmid),
1142
            'other' => array(
1143
                'assignid' => $assign->id,
1144
                'groupid' => 2
1145
            )
1146
        );
1147
        $event = \mod_assign\event\group_override_created::create($params);
1148
 
1149
        // Trigger and capture the event.
1150
        $sink = $this->redirectEvents();
1151
        $event->trigger();
1152
        $events = $sink->get_events();
1153
        $event = reset($events);
1154
 
1155
        // Check that the event data is valid.
1156
        $this->assertInstanceOf('\mod_assign\event\group_override_created', $event);
1157
        $this->assertEquals(\context_module::instance($assign->cmid), $event->get_context());
1158
        $this->assertEventContextNotUsed($event);
1159
    }
1160
 
1161
    /**
1162
     * Test the user override updated event.
1163
     *
1164
     * There is no external API for updating a user override, so the unit test will simply
1165
     * create and trigger the event and ensure the event data is returned as expected.
1166
     */
11 efrain 1167
    public function test_user_override_updated(): void {
1 efrain 1168
        $this->resetAfterTest();
1169
 
1170
        $course = $this->getDataGenerator()->create_course();
1171
        $assign = $this->getDataGenerator()->get_plugin_generator('mod_assign')->create_instance(['course' => $course->id]);
1172
 
1173
        $params = array(
1174
            'objectid' => 1,
1175
            'relateduserid' => 2,
1176
            'context' => \context_module::instance($assign->cmid),
1177
            'other' => array(
1178
                'assignid' => $assign->id
1179
            )
1180
        );
1181
        $event = \mod_assign\event\user_override_updated::create($params);
1182
 
1183
        // Trigger and capture the event.
1184
        $sink = $this->redirectEvents();
1185
        $event->trigger();
1186
        $events = $sink->get_events();
1187
        $event = reset($events);
1188
 
1189
        // Check that the event data is valid.
1190
        $this->assertInstanceOf('\mod_assign\event\user_override_updated', $event);
1191
        $this->assertEquals(\context_module::instance($assign->cmid), $event->get_context());
1192
        $this->assertEventContextNotUsed($event);
1193
    }
1194
 
1195
    /**
1196
     * Test the group override updated event.
1197
     *
1198
     * There is no external API for updating a group override, so the unit test will simply
1199
     * create and trigger the event and ensure the event data is returned as expected.
1200
     */
11 efrain 1201
    public function test_group_override_updated(): void {
1 efrain 1202
        $this->resetAfterTest();
1203
 
1204
        $course = $this->getDataGenerator()->create_course();
1205
        $assign = $this->getDataGenerator()->get_plugin_generator('mod_assign')->create_instance(['course' => $course->id]);
1206
 
1207
        $params = array(
1208
            'objectid' => 1,
1209
            'context' => \context_module::instance($assign->cmid),
1210
            'other' => array(
1211
                'assignid' => $assign->id,
1212
                'groupid' => 2
1213
            )
1214
        );
1215
        $event = \mod_assign\event\group_override_updated::create($params);
1216
 
1217
        // Trigger and capture the event.
1218
        $sink = $this->redirectEvents();
1219
        $event->trigger();
1220
        $events = $sink->get_events();
1221
        $event = reset($events);
1222
 
1223
        // Check that the event data is valid.
1224
        $this->assertInstanceOf('\mod_assign\event\group_override_updated', $event);
1225
        $this->assertEquals(\context_module::instance($assign->cmid), $event->get_context());
1226
        $this->assertEventContextNotUsed($event);
1227
    }
1228
 
1229
    /**
1230
     * Test the user override deleted event.
1231
     */
11 efrain 1232
    public function test_user_override_deleted(): void {
1 efrain 1233
        global $DB;
1234
        $this->resetAfterTest();
1235
 
1236
        $course = $this->getDataGenerator()->create_course();
1237
        $assigninstance = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
1238
        $cm = get_coursemodule_from_instance('assign', $assigninstance->id, $course->id);
1239
        $context = \context_module::instance($cm->id);
1240
        $assign = new \assign($context, $cm, $course);
1241
 
1242
        // Create an override.
1243
        $override = new \stdClass();
1244
        $override->assign = $assigninstance->id;
1245
        $override->userid = 2;
1246
        $override->id = $DB->insert_record('assign_overrides', $override);
1247
 
1248
        // Trigger and capture the event.
1249
        $sink = $this->redirectEvents();
1250
        $assign->delete_override($override->id);
1251
        $events = $sink->get_events();
1252
        $event = reset($events);
1253
 
1254
        // Check that the event data is valid.
1255
        $this->assertInstanceOf('\mod_assign\event\user_override_deleted', $event);
1256
        $this->assertEquals(\context_module::instance($cm->id), $event->get_context());
1257
        $this->assertEventContextNotUsed($event);
1258
    }
1259
 
1260
    /**
1261
     * Test the group override deleted event.
1262
     */
11 efrain 1263
    public function test_group_override_deleted(): void {
1 efrain 1264
        global $DB;
1265
        $this->resetAfterTest();
1266
 
1267
        $course = $this->getDataGenerator()->create_course();
1268
        $assigninstance = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
1269
        $cm = get_coursemodule_from_instance('assign', $assigninstance->id, $course->id);
1270
        $context = \context_module::instance($cm->id);
1271
        $assign = new \assign($context, $cm, $course);
1272
 
1273
        // Create an override.
1274
        $override = new \stdClass();
1275
        $override->assign = $assigninstance->id;
1276
        $override->groupid = 2;
1277
        $override->id = $DB->insert_record('assign_overrides', $override);
1278
 
1279
        // Trigger and capture the event.
1280
        $sink = $this->redirectEvents();
1281
        $assign->delete_override($override->id);
1282
        $events = $sink->get_events();
1283
        $event = reset($events);
1284
 
1285
        // Check that the event data is valid.
1286
        $this->assertInstanceOf('\mod_assign\event\group_override_deleted', $event);
1287
        $this->assertEquals(\context_module::instance($cm->id), $event->get_context());
1288
        $this->assertEventContextNotUsed($event);
1289
    }
1290
 
1291
    /**
1292
     * Test the course module viewed event.
1293
     */
11 efrain 1294
    public function test_course_module_viewed(): void {
1 efrain 1295
        $this->resetAfterTest();
1296
 
1297
        $course = $this->getDataGenerator()->create_course();
1298
        $assign = $this->create_instance($course);
1299
 
1300
        $context = $assign->get_context();
1301
 
1302
        $params = array(
1303
            'context' => $context,
1304
            'objectid' => $assign->get_instance()->id
1305
        );
1306
 
1307
        $event = \mod_assign\event\course_module_viewed::create($params);
1308
 
1309
        // Trigger and capture the event.
1310
        $sink = $this->redirectEvents();
1311
        $event->trigger();
1312
        $events = $sink->get_events();
1313
        $this->assertCount(1, $events);
1314
        $event = reset($events);
1315
 
1316
        // Check that the event contains the expected values.
1317
        $this->assertInstanceOf('\mod_assign\event\course_module_viewed', $event);
1318
        $this->assertEquals($context, $event->get_context());
1319
    }
1320
 
1321
    /**
1322
     * Test that all events generated with blindmarking enabled are anonymous
1323
     */
11 efrain 1324
    public function test_anonymous_events(): void {
1 efrain 1325
        $this->resetAfterTest();
1326
 
1327
        $course = $this->getDataGenerator()->create_course();
1328
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
1329
        $student1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
1330
        $student2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
1331
 
1332
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
1333
        $instance = $generator->create_instance(array('course' => $course->id, 'blindmarking' => 1));
1334
 
1335
        $cm = get_coursemodule_from_instance('assign', $instance->id, $course->id);
1336
        $context = \context_module::instance($cm->id);
1337
        $assign = new \assign($context, $cm, $course);
1338
 
1339
        $this->setUser($teacher);
1340
        $sink = $this->redirectEvents();
1341
 
1342
        $assign->lock_submission($student1->id);
1343
 
1344
        $events = $sink->get_events();
1345
        $event = reset($events);
1346
 
1347
        $this->assertTrue((bool)$event->anonymous);
1348
 
1349
        $assign->reveal_identities();
1350
        $sink = $this->redirectEvents();
1351
        $assign->lock_submission($student2->id);
1352
 
1353
        $events = $sink->get_events();
1354
        $event = reset($events);
1355
 
1356
        $this->assertFalse((bool)$event->anonymous);
1357
    }
1358
 
1359
}