14d68ecd6e031ab1ae4e29e4960fd49b889c45fc
[moodle.git] / mod / assign / tests / events_test.php
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/>.
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  */
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
29 require_once($CFG->dirroot . '/mod/assign/tests/fixtures/event_mod_assign_fixtures.php');
31 /**
32  * Contains the event tests for the module assign.
33  *
34  * @package   mod_assign
35  * @copyright 2014 Adrian Greeve <adrian@moodle.com>
36  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37  */
38 class assign_events_testcase extends mod_assign_base_testcase {
40     /**
41      * Basic tests for the submission_created() abstract class.
42      */
43     public function test_base_event() {
44         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
45         $instance = $generator->create_instance(array('course' => $this->course->id));
46         $modcontext = context_module::instance($instance->cmid);
48         $data = array(
49             'context' => $modcontext,
50         );
51         /** @var \mod_assign_unittests\event\nothing_happened $event */
52         $event = \mod_assign_unittests\event\nothing_happened::create($data);
53         $assign = $event->get_assign();
54         $this->assertDebuggingCalled();
55         $this->assertInstanceOf('assign', $assign);
57         $event = \mod_assign_unittests\event\nothing_happened::create($data);
58         $event->set_assign($assign);
59         $assign2 = $event->get_assign();
60         $this->assertDebuggingNotCalled();
61         $this->assertSame($assign, $assign2);
62     }
64     /**
65      * Basic tests for the submission_created() abstract class.
66      */
67     public function test_submission_created() {
68         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
69         $instance = $generator->create_instance(array('course' => $this->course->id));
70         $modcontext = context_module::instance($instance->cmid);
72         // Standard Event parameters.
73         $params = array(
74             'context' => $modcontext,
75             'courseid' => $this->course->id
76         );
78         $eventinfo = $params;
79         $eventinfo['other'] = array(
80             'submissionid' => '17',
81             'submissionattempt' => 0,
82             'submissionstatus' => 'submitted'
83         );
85         $sink = $this->redirectEvents();
86         $event = \mod_assign_unittests\event\submission_created::create($eventinfo);
87         $event->trigger();
88         $result = $sink->get_events();
89         $event = reset($result);
90         $sink->close();
92         $this->assertEquals($modcontext->id, $event->contextid);
93         $this->assertEquals($this->course->id, $event->courseid);
95         // Check that an error occurs when teamsubmission is not set.
96         try {
97             \mod_assign_unittests\event\submission_created::create($params);
98             $this->fail('Other must contain the key submissionid.');
99         } catch (Exception $e) {
100             $this->assertInstanceOf('coding_exception', $e);
101         }
102         // Check that the submission status debugging is fired.
103         $subinfo = $params;
104         $subinfo['other'] = array('submissionid' => '23');
105         try {
106             \mod_assign_unittests\event\submission_created::create($subinfo);
107             $this->fail('Other must contain the key submissionattempt.');
108         } catch (Exception $e) {
109             $this->assertInstanceOf('coding_exception', $e);
110         }
112         $subinfo['other'] = array('submissionattempt' => '0');
113         try {
114             \mod_assign_unittests\event\submission_created::create($subinfo);
115             $this->fail('Other must contain the key submissionstatus.');
116         } catch (Exception $e) {
117             $this->assertInstanceOf('coding_exception', $e);
118         }
119     }
121     /**
122      * Basic tests for the submission_updated() abstract class.
123      */
124     public function test_submission_updated() {
125         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
126         $instance = $generator->create_instance(array('course' => $this->course->id));
127         $modcontext = context_module::instance($instance->cmid);
129         // Standard Event parameters.
130         $params = array(
131             'context' => $modcontext,
132             'courseid' => $this->course->id
133         );
135         $eventinfo = $params;
136         $eventinfo['other'] = array(
137             'submissionid' => '17',
138             'submissionattempt' => 0,
139             'submissionstatus' => 'submitted'
140         );
142         $sink = $this->redirectEvents();
143         $event = \mod_assign_unittests\event\submission_updated::create($eventinfo);
144         $event->trigger();
145         $result = $sink->get_events();
146         $event = reset($result);
147         $sink->close();
149         $this->assertEquals($modcontext->id, $event->contextid);
150         $this->assertEquals($this->course->id, $event->courseid);
152         // Check that an error occurs when teamsubmission is not set.
153         try {
154             \mod_assign_unittests\event\submission_created::create($params);
155             $this->fail('Other must contain the key submissionid.');
156         } catch (Exception $e) {
157             $this->assertInstanceOf('coding_exception', $e);
158         }
159         // Check that the submission status debugging is fired.
160         $subinfo = $params;
161         $subinfo['other'] = array('submissionid' => '23');
162         try {
163             \mod_assign_unittests\event\submission_created::create($subinfo);
164             $this->fail('Other must contain the key submissionattempt.');
165         } catch (Exception $e) {
166             $this->assertInstanceOf('coding_exception', $e);
167         }
169         $subinfo['other'] = array('submissionattempt' => '0');
170         try {
171             \mod_assign_unittests\event\submission_created::create($subinfo);
172             $this->fail('Other must contain the key submissionstatus.');
173         } catch (Exception $e) {
174             $this->assertInstanceOf('coding_exception', $e);
175         }
176     }
178     public function test_extension_granted() {
179         $this->setUser($this->editingteachers[0]);
181         $tomorrow = time() + 24*60*60;
182         $yesterday = time() - 24*60*60;
184         $assign = $this->create_instance(array('duedate' => $yesterday, 'cutoffdate' => $yesterday));
185         $sink = $this->redirectEvents();
187         $assign->testable_save_user_extension($this->students[0]->id, $tomorrow);
189         $events = $sink->get_events();
190         $this->assertCount(1, $events);
191         $event = reset($events);
192         $this->assertInstanceOf('\mod_assign\event\extension_granted', $event);
193         $this->assertEquals($assign->get_context(), $event->get_context());
194         $this->assertEquals($assign->get_instance()->id, $event->objectid);
195         $this->assertEquals($this->students[0]->id, $event->relateduserid);
196         $expected = array(
197             $assign->get_course()->id,
198             'assign',
199             'grant extension',
200             'view.php?id=' . $assign->get_course_module()->id,
201             $this->students[0]->id,
202             $assign->get_course_module()->id
203         );
204         $this->assertEventLegacyLogData($expected, $event);
205         $sink->close();
206     }
208     public function test_submission_locked() {
209         $this->editingteachers[0]->ignoresesskey = true;
210         $this->setUser($this->editingteachers[0]);
212         $assign = $this->create_instance();
213         $sink = $this->redirectEvents();
215         $assign->lock_submission($this->students[0]->id);
217         $events = $sink->get_events();
218         $this->assertCount(1, $events);
219         $event = reset($events);
220         $this->assertInstanceOf('\mod_assign\event\submission_locked', $event);
221         $this->assertEquals($assign->get_context(), $event->get_context());
222         $this->assertEquals($assign->get_instance()->id, $event->objectid);
223         $this->assertEquals($this->students[0]->id, $event->relateduserid);
224         $expected = array(
225             $assign->get_course()->id,
226             'assign',
227             'lock submission',
228             'view.php?id=' . $assign->get_course_module()->id,
229             get_string('locksubmissionforstudent', 'assign', array('id' => $this->students[0]->id,
230                 'fullname' => fullname($this->students[0]))),
231             $assign->get_course_module()->id
232         );
233         $this->assertEventLegacyLogData($expected, $event);
234         $sink->close();
236         // Revert to defaults.
237         $this->editingteachers[0]->ignoresesskey = false;
238     }
240     public function test_identities_revealed() {
241         $this->editingteachers[0]->ignoresesskey = true;
242         $this->setUser($this->editingteachers[0]);
244         $assign = $this->create_instance(array('blindmarking'=>1));
245         $sink = $this->redirectEvents();
247         $assign->reveal_identities();
249         $events = $sink->get_events();
250         $this->assertCount(1, $events);
251         $event = reset($events);
252         $this->assertInstanceOf('\mod_assign\event\identities_revealed', $event);
253         $this->assertEquals($assign->get_context(), $event->get_context());
254         $this->assertEquals($assign->get_instance()->id, $event->objectid);
255         $expected = array(
256             $assign->get_course()->id,
257             'assign',
258             'reveal identities',
259             'view.php?id=' . $assign->get_course_module()->id,
260             get_string('revealidentities', 'assign'),
261             $assign->get_course_module()->id
262         );
263         $this->assertEventLegacyLogData($expected, $event);
264         $sink->close();
266         // Revert to defaults.
267         $this->editingteachers[0]->ignoresesskey = false;
268     }
270     /**
271      * Test the submission_status_viewed event.
272      */
273     public function test_submission_status_viewed() {
274         global $PAGE;
276         $this->setUser($this->editingteachers[0]);
278         $assign = $this->create_instance();
280         // We need to set the URL in order to view the feedback.
281         $PAGE->set_url('/a_url');
283         // Trigger and capture the event.
284         $sink = $this->redirectEvents();
285         $assign->view();
286         $events = $sink->get_events();
287         $this->assertCount(1, $events);
288         $event = reset($events);
290         // Check that the event contains the expected values.
291         $this->assertInstanceOf('\mod_assign\event\submission_status_viewed', $event);
292         $this->assertEquals($assign->get_context(), $event->get_context());
293         $expected = array(
294             $assign->get_course()->id,
295             'assign',
296             'view',
297             'view.php?id=' . $assign->get_course_module()->id,
298             get_string('viewownsubmissionstatus', 'assign'),
299             $assign->get_course_module()->id
300         );
301         $this->assertEventLegacyLogData($expected, $event);
302         $this->assertEventContextNotUsed($event);
303     }
305     public function test_submission_status_updated() {
306         $this->editingteachers[0]->ignoresesskey = true;
307         $this->setUser($this->editingteachers[0]);
309         $assign = $this->create_instance();
310         $submission = $assign->get_user_submission($this->students[0]->id, true);
311         $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
312         $assign->testable_update_submission($submission, $this->students[0]->id, true, false);
314         $sink = $this->redirectEvents();
315         $assign->revert_to_draft($this->students[0]->id);
317         $events = $sink->get_events();
318         $this->assertCount(2, $events);
319         $event = $events[1];
320         $this->assertInstanceOf('\mod_assign\event\submission_status_updated', $event);
321         $this->assertEquals($assign->get_context(), $event->get_context());
322         $this->assertEquals($submission->id, $event->objectid);
323         $this->assertEquals($this->students[0]->id, $event->relateduserid);
324         $this->assertEquals(ASSIGN_SUBMISSION_STATUS_DRAFT, $event->other['newstatus']);
325         $expected = array(
326             $assign->get_course()->id,
327             'assign',
328             'revert submission to draft',
329             'view.php?id=' . $assign->get_course_module()->id,
330             get_string('reverttodraftforstudent', 'assign', array('id' => $this->students[0]->id,
331                 'fullname' => fullname($this->students[0]))),
332             $assign->get_course_module()->id
333         );
334         $this->assertEventLegacyLogData($expected, $event);
335         $sink->close();
337         // Revert to defaults.
338         $this->editingteachers[0]->ignoresesskey = false;
339     }
341     public function test_marker_updated() {
342         $this->editingteachers[0]->ignoresesskey = true;
343         $this->setUser($this->editingteachers[0]);
345         $assign = $this->create_instance();
347         $sink = $this->redirectEvents();
348         $assign->testable_process_set_batch_marking_allocation($this->students[0]->id, $this->teachers[0]->id);
350         $events = $sink->get_events();
351         $this->assertCount(1, $events);
352         $event = reset($events);
353         $this->assertInstanceOf('\mod_assign\event\marker_updated', $event);
354         $this->assertEquals($assign->get_context(), $event->get_context());
355         $this->assertEquals($assign->get_instance()->id, $event->objectid);
356         $this->assertEquals($this->students[0]->id, $event->relateduserid);
357         $this->assertEquals($this->editingteachers[0]->id, $event->userid);
358         $this->assertEquals($this->teachers[0]->id, $event->other['markerid']);
359         $expected = array(
360             $assign->get_course()->id,
361             'assign',
362             'set marking allocation',
363             'view.php?id=' . $assign->get_course_module()->id,
364             get_string('setmarkerallocationforlog', 'assign', array('id' => $this->students[0]->id,
365                 'fullname' => fullname($this->students[0]), 'marker' => fullname($this->teachers[0]))),
366             $assign->get_course_module()->id
367         );
368         $this->assertEventLegacyLogData($expected, $event);
369         $sink->close();
371         // Revert to defaults.
372         $this->editingteachers[0]->ignoresesskey = false;
373     }
375     public function test_workflow_state_updated() {
376         $this->editingteachers[0]->ignoresesskey = true;
377         $this->setUser($this->editingteachers[0]);
379         $assign = $this->create_instance();
381         // Test process_set_batch_marking_workflow_state.
382         $sink = $this->redirectEvents();
383         $assign->testable_process_set_batch_marking_workflow_state($this->students[0]->id, ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW);
385         $events = $sink->get_events();
386         $this->assertCount(1, $events);
387         $event = reset($events);
388         $this->assertInstanceOf('\mod_assign\event\workflow_state_updated', $event);
389         $this->assertEquals($assign->get_context(), $event->get_context());
390         $this->assertEquals($assign->get_instance()->id, $event->objectid);
391         $this->assertEquals($this->students[0]->id, $event->relateduserid);
392         $this->assertEquals($this->editingteachers[0]->id, $event->userid);
393         $this->assertEquals(ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW, $event->other['newstate']);
394         $expected = array(
395             $assign->get_course()->id,
396             'assign',
397             'set marking workflow state',
398             'view.php?id=' . $assign->get_course_module()->id,
399             get_string('setmarkingworkflowstateforlog', 'assign', array('id' => $this->students[0]->id,
400                 'fullname' => fullname($this->students[0]), 'state' => ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW)),
401             $assign->get_course_module()->id
402         );
403         $this->assertEventLegacyLogData($expected, $event);
404         $sink->close();
406         // Test setting workflow state in apply_grade_to_user.
407         $sink = $this->redirectEvents();
408         $data = new stdClass();
409         $data->grade = '50.0';
410         $data->workflowstate = 'readyforrelease';
411         $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
413         $events = $sink->get_events();
414         $this->assertCount(4, $events);
415         $event = reset($events);
416         $this->assertInstanceOf('\mod_assign\event\workflow_state_updated', $event);
417         $this->assertEquals($assign->get_context(), $event->get_context());
418         $this->assertEquals($assign->get_instance()->id, $event->objectid);
419         $this->assertEquals($this->students[0]->id, $event->relateduserid);
420         $this->assertEquals($this->editingteachers[0]->id, $event->userid);
421         $this->assertEquals(ASSIGN_MARKING_WORKFLOW_STATE_READYFORRELEASE, $event->other['newstate']);
422         $expected = array(
423             $assign->get_course()->id,
424             'assign',
425             'set marking workflow state',
426             'view.php?id=' . $assign->get_course_module()->id,
427             get_string('setmarkingworkflowstateforlog', 'assign', array('id' => $this->students[0]->id,
428                 'fullname' => fullname($this->students[0]), 'state' => ASSIGN_MARKING_WORKFLOW_STATE_READYFORRELEASE)),
429             $assign->get_course_module()->id
430         );
431         $this->assertEventLegacyLogData($expected, $event);
432         $sink->close();
434         // Test setting workflow state in process_save_quick_grades.
435         $sink = $this->redirectEvents();
437         $data = array(
438             'grademodified_' . $this->students[0]->id => time(),
439             'quickgrade_' . $this->students[0]->id => '60.0',
440             'quickgrade_' . $this->students[0]->id . '_workflowstate' => 'inmarking'
441         );
442         $assign->testable_process_save_quick_grades($data);
444         $events = $sink->get_events();
445         $this->assertCount(4, $events);
446         $event = reset($events);
447         $this->assertInstanceOf('\mod_assign\event\workflow_state_updated', $event);
448         $this->assertEquals($assign->get_context(), $event->get_context());
449         $this->assertEquals($assign->get_instance()->id, $event->objectid);
450         $this->assertEquals($this->students[0]->id, $event->relateduserid);
451         $this->assertEquals($this->editingteachers[0]->id, $event->userid);
452         $this->assertEquals(ASSIGN_MARKING_WORKFLOW_STATE_INMARKING, $event->other['newstate']);
453         $expected = array(
454             $assign->get_course()->id,
455             'assign',
456             'set marking workflow state',
457             'view.php?id=' . $assign->get_course_module()->id,
458             get_string('setmarkingworkflowstateforlog', 'assign', array('id' => $this->students[0]->id,
459                 'fullname' => fullname($this->students[0]), 'state' => ASSIGN_MARKING_WORKFLOW_STATE_INMARKING)),
460             $assign->get_course_module()->id
461         );
462         $this->assertEventLegacyLogData($expected, $event);
463         $sink->close();
465         // Revert to defaults.
466         $this->editingteachers[0]->ignoresesskey = false;
467     }
469     public function test_submission_duplicated() {
470         $this->setUser($this->students[0]);
472         $assign = $this->create_instance();
473         $submission1 = $assign->get_user_submission($this->students[0]->id, true, 0);
474         $submission2 = $assign->get_user_submission($this->students[0]->id, true, 1);
475         $submission2->status = ASSIGN_SUBMISSION_STATUS_REOPENED;
476         $assign->testable_update_submission($submission2, $this->students[0]->id, time(), $assign->get_instance()->teamsubmission);
478         $sink = $this->redirectEvents();
479         $notices = null;
480         $assign->copy_previous_attempt($notices);
482         $events = $sink->get_events();
483         $this->assertCount(1, $events);
484         $event = reset($events);
485         $this->assertInstanceOf('\mod_assign\event\submission_duplicated', $event);
486         $this->assertEquals($assign->get_context(), $event->get_context());
487         $this->assertEquals($submission2->id, $event->objectid);
488         $this->assertEquals($this->students[0]->id, $event->userid);
489         $submission2->status = ASSIGN_SUBMISSION_STATUS_DRAFT;
490         $expected = array(
491             $assign->get_course()->id,
492             'assign',
493             'submissioncopied',
494             'view.php?id=' . $assign->get_course_module()->id,
495             $assign->testable_format_submission_for_log($submission2),
496             $assign->get_course_module()->id
497         );
498         $this->assertEventLegacyLogData($expected, $event);
499         $sink->close();
500     }
502     public function test_submission_unlocked() {
503         $this->editingteachers[0]->ignoresesskey = true;
504         $this->setUser($this->editingteachers[0]);
506         $assign = $this->create_instance();
507         $sink = $this->redirectEvents();
509         $assign->unlock_submission($this->students[0]->id);
511         $events = $sink->get_events();
512         $this->assertCount(1, $events);
513         $event = reset($events);
514         $this->assertInstanceOf('\mod_assign\event\submission_unlocked', $event);
515         $this->assertEquals($assign->get_context(), $event->get_context());
516         $this->assertEquals($assign->get_instance()->id, $event->objectid);
517         $this->assertEquals($this->students[0]->id, $event->relateduserid);
518         $expected = array(
519             $assign->get_course()->id,
520             'assign',
521             'unlock submission',
522             'view.php?id=' . $assign->get_course_module()->id,
523             get_string('unlocksubmissionforstudent', 'assign', array('id' => $this->students[0]->id,
524                 'fullname' => fullname($this->students[0]))),
525             $assign->get_course_module()->id
526         );
527         $this->assertEventLegacyLogData($expected, $event);
528         $sink->close();
530         // Revert to defaults.
531         $this->editingteachers[0]->ignoresesskey = false;
532     }
534     public function test_submission_graded() {
535         $this->editingteachers[0]->ignoresesskey = true;
536         $this->setUser($this->editingteachers[0]);
537         $assign = $this->create_instance();
539         // Test apply_grade_to_user.
540         $sink = $this->redirectEvents();
542         $data = new stdClass();
543         $data->grade = '50.0';
544         $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
545         $grade = $assign->get_user_grade($this->students[0]->id, false, 0);
547         $events = $sink->get_events();
548         $this->assertCount(3, $events);
549         $event = $events[2];
550         $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
551         $this->assertEquals($assign->get_context(), $event->get_context());
552         $this->assertEquals($grade->id, $event->objectid);
553         $this->assertEquals($this->students[0]->id, $event->relateduserid);
554         $expected = array(
555             $assign->get_course()->id,
556             'assign',
557             'grade submission',
558             'view.php?id=' . $assign->get_course_module()->id,
559             $assign->format_grade_for_log($grade),
560             $assign->get_course_module()->id
561         );
562         $this->assertEventLegacyLogData($expected, $event);
563         $sink->close();
565         // Test process_save_quick_grades.
566         $sink = $this->redirectEvents();
568         $data = array(
569             'grademodified_' . $this->students[0]->id => time(),
570             'quickgrade_' . $this->students[0]->id => '60.0'
571         );
572         $assign->testable_process_save_quick_grades($data);
573         $grade = $assign->get_user_grade($this->students[0]->id, false);
574         $this->assertEquals('60.0', $grade->grade);
576         $events = $sink->get_events();
577         $this->assertCount(3, $events);
578         $event = $events[2];
579         $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
580         $this->assertEquals($assign->get_context(), $event->get_context());
581         $this->assertEquals($grade->id, $event->objectid);
582         $this->assertEquals($this->students[0]->id, $event->relateduserid);
583         $expected = array(
584             $assign->get_course()->id,
585             'assign',
586             'grade submission',
587             'view.php?id=' . $assign->get_course_module()->id,
588             $assign->format_grade_for_log($grade),
589             $assign->get_course_module()->id
590         );
591         $this->assertEventLegacyLogData($expected, $event);
592         $sink->close();
594         // Test update_grade.
595         $sink = $this->redirectEvents();
596         $data = clone($grade);
597         $data->grade = '50.0';
598         $assign->update_grade($data);
599         $grade = $assign->get_user_grade($this->students[0]->id, false, 0);
600         $this->assertEquals('50.0', $grade->grade);
601         $events = $sink->get_events();
603         $this->assertCount(3, $events);
604         $event = $events[2];
605         $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
606         $this->assertEquals($assign->get_context(), $event->get_context());
607         $this->assertEquals($grade->id, $event->objectid);
608         $this->assertEquals($this->students[0]->id, $event->relateduserid);
609         $expected = array(
610             $assign->get_course()->id,
611             'assign',
612             'grade submission',
613             'view.php?id=' . $assign->get_course_module()->id,
614             $assign->format_grade_for_log($grade),
615             $assign->get_course_module()->id
616         );
617         $this->assertEventLegacyLogData($expected, $event);
618         $sink->close();
619         // Revert to defaults.
620         $this->editingteachers[0]->ignoresesskey = false;
621     }
623     /**
624      * Test the submission_viewed event.
625      */
626     public function test_submission_viewed() {
627         global $PAGE;
629         $this->setUser($this->editingteachers[0]);
631         $assign = $this->create_instance();
632         $submission = $assign->get_user_submission($this->students[0]->id, true);
634         // We need to set the URL in order to view the submission.
635         $PAGE->set_url('/a_url');
636         // A hack - these variables are used by the view_plugin_content function to
637         // determine what we actually want to view - would usually be set in URL.
638         global $_POST;
639         $_POST['plugin'] = 'comments';
640         $_POST['sid'] = $submission->id;
642         // Trigger and capture the event.
643         $sink = $this->redirectEvents();
644         $assign->view('viewpluginassignsubmission');
645         $events = $sink->get_events();
646         $this->assertCount(1, $events);
647         $event = reset($events);
649         // Check that the event contains the expected values.
650         $this->assertInstanceOf('\mod_assign\event\submission_viewed', $event);
651         $this->assertEquals($assign->get_context(), $event->get_context());
652         $this->assertEquals($submission->id, $event->objectid);
653         $expected = array(
654             $assign->get_course()->id,
655             'assign',
656             'view submission',
657             'view.php?id=' . $assign->get_course_module()->id,
658             get_string('viewsubmissionforuser', 'assign', $this->students[0]->id),
659             $assign->get_course_module()->id
660         );
661         $this->assertEventLegacyLogData($expected, $event);
662         $this->assertEventContextNotUsed($event);
663     }
665     /**
666      * Test the feedback_viewed event.
667      */
668     public function test_feedback_viewed() {
669         global $DB, $PAGE;
671         $this->setUser($this->editingteachers[0]);
673         $assign = $this->create_instance();
674         $submission = $assign->get_user_submission($this->students[0]->id, true);
676         // Insert a grade for this submission.
677         $grade = new stdClass();
678         $grade->assignment = $assign->get_instance()->id;
679         $grade->userid = $this->students[0]->id;
680         $gradeid = $DB->insert_record('assign_grades', $grade);
682         // We need to set the URL in order to view the feedback.
683         $PAGE->set_url('/a_url');
684         // A hack - these variables are used by the view_plugin_content function to
685         // determine what we actually want to view - would usually be set in URL.
686         global $_POST;
687         $_POST['plugin'] = 'comments';
688         $_POST['gid'] = $gradeid;
689         $_POST['sid'] = $submission->id;
691         // Trigger and capture the event.
692         $sink = $this->redirectEvents();
693         $assign->view('viewpluginassignfeedback');
694         $events = $sink->get_events();
695         $this->assertCount(1, $events);
696         $event = reset($events);
698         // Check that the event contains the expected values.
699         $this->assertInstanceOf('\mod_assign\event\feedback_viewed', $event);
700         $this->assertEquals($assign->get_context(), $event->get_context());
701         $this->assertEquals($gradeid, $event->objectid);
702         $expected = array(
703             $assign->get_course()->id,
704             'assign',
705             'view feedback',
706             'view.php?id=' . $assign->get_course_module()->id,
707             get_string('viewfeedbackforuser', 'assign', $this->students[0]->id),
708             $assign->get_course_module()->id
709         );
710         $this->assertEventLegacyLogData($expected, $event);
711         $this->assertEventContextNotUsed($event);
712     }
714     /**
715      * Test the grading_form_viewed event.
716      */
717     public function test_grading_form_viewed() {
718         global $PAGE;
720         $this->setUser($this->editingteachers[0]);
722         $assign = $this->create_instance();
724         // We need to set the URL in order to view the feedback.
725         $PAGE->set_url('/a_url');
726         // A hack - this variable is used by the view_single_grade_page function.
727         global $_POST;
728         $_POST['rownum'] = 1;
729         $_POST['userid'] = $this->students[0]->id;
731         // Trigger and capture the event.
732         $sink = $this->redirectEvents();
733         $assign->view('grade');
734         $events = $sink->get_events();
735         $this->assertCount(1, $events);
736         $event = reset($events);
738         // Check that the event contains the expected values.
739         $this->assertInstanceOf('\mod_assign\event\grading_form_viewed', $event);
740         $this->assertEquals($assign->get_context(), $event->get_context());
741         $expected = array(
742             $assign->get_course()->id,
743             'assign',
744             'view grading form',
745             'view.php?id=' . $assign->get_course_module()->id,
746             get_string('viewgradingformforstudent', 'assign', array('id' => $this->students[0]->id,
747                 'fullname' => fullname($this->students[0]))),
748             $assign->get_course_module()->id
749         );
750         $this->assertEventLegacyLogData($expected, $event);
751         $this->assertEventContextNotUsed($event);
752     }
754     /**
755      * Test the grading_table_viewed event.
756      */
757     public function test_grading_table_viewed() {
758         global $PAGE;
760         $this->setUser($this->editingteachers[0]);
762         $assign = $this->create_instance();
764         // We need to set the URL in order to view the feedback.
765         $PAGE->set_url('/a_url');
766         // A hack - this variable is used by the view_single_grade_page function.
767         global $_POST;
768         $_POST['rownum'] = 1;
769         $_POST['userid'] = $this->students[0]->id;
771         // Trigger and capture the event.
772         $sink = $this->redirectEvents();
773         $assign->view('grading');
774         $events = $sink->get_events();
775         $this->assertCount(1, $events);
776         $event = reset($events);
778         // Check that the event contains the expected values.
779         $this->assertInstanceOf('\mod_assign\event\grading_table_viewed', $event);
780         $this->assertEquals($assign->get_context(), $event->get_context());
781         $expected = array(
782             $assign->get_course()->id,
783             'assign',
784             'view submission grading table',
785             'view.php?id=' . $assign->get_course_module()->id,
786             get_string('viewsubmissiongradingtable', 'assign'),
787             $assign->get_course_module()->id
788         );
789         $this->assertEventLegacyLogData($expected, $event);
790         $this->assertEventContextNotUsed($event);
791     }
793     /**
794      * Test the submission_form_viewed event.
795      */
796     public function test_submission_form_viewed() {
797         global $PAGE;
799         $this->setUser($this->students[0]);
801         $assign = $this->create_instance();
803         // We need to set the URL in order to view the submission form.
804         $PAGE->set_url('/a_url');
806         // Trigger and capture the event.
807         $sink = $this->redirectEvents();
808         $assign->view('editsubmission');
809         $events = $sink->get_events();
810         $this->assertCount(1, $events);
811         $event = reset($events);
813         // Check that the event contains the expected values.
814         $this->assertInstanceOf('\mod_assign\event\submission_form_viewed', $event);
815         $this->assertEquals($assign->get_context(), $event->get_context());
816         $expected = array(
817             $assign->get_course()->id,
818             'assign',
819             'view submit assignment form',
820             'view.php?id=' . $assign->get_course_module()->id,
821             get_string('editsubmission', 'assign'),
822             $assign->get_course_module()->id
823         );
824         $this->assertEventLegacyLogData($expected, $event);
825         $this->assertEventContextNotUsed($event);
826     }
828     /**
829      * Test the submission_form_viewed event.
830      */
831     public function test_submission_confirmation_form_viewed() {
832         global $PAGE;
834         $this->setUser($this->students[0]);
836         $assign = $this->create_instance();
838         // We need to set the URL in order to view the submission form.
839         $PAGE->set_url('/a_url');
841         // Trigger and capture the event.
842         $sink = $this->redirectEvents();
843         $assign->view('submit');
844         $events = $sink->get_events();
845         $this->assertCount(1, $events);
846         $event = reset($events);
848         // Check that the event contains the expected values.
849         $this->assertInstanceOf('\mod_assign\event\submission_confirmation_form_viewed', $event);
850         $this->assertEquals($assign->get_context(), $event->get_context());
851         $expected = array(
852             $assign->get_course()->id,
853             'assign',
854             'view confirm submit assignment form',
855             'view.php?id=' . $assign->get_course_module()->id,
856             get_string('viewownsubmissionform', 'assign'),
857             $assign->get_course_module()->id
858         );
859         $this->assertEventLegacyLogData($expected, $event);
860         $this->assertEventContextNotUsed($event);
861     }
863     /**
864      * Test the reveal_identities_confirmation_page_viewed event.
865      */
866     public function test_reveal_identities_confirmation_page_viewed() {
867         global $PAGE;
869         // Set to the admin user so we have the permission to reveal identities.
870         $this->setAdminUser();
872         $assign = $this->create_instance();
874         // We need to set the URL in order to view the submission form.
875         $PAGE->set_url('/a_url');
877         // Trigger and capture the event.
878         $sink = $this->redirectEvents();
879         $assign->view('revealidentities');
880         $events = $sink->get_events();
881         $this->assertCount(1, $events);
882         $event = reset($events);
884         // Check that the event contains the expected values.
885         $this->assertInstanceOf('\mod_assign\event\reveal_identities_confirmation_page_viewed', $event);
886         $this->assertEquals($assign->get_context(), $event->get_context());
887         $expected = array(
888             $assign->get_course()->id,
889             'assign',
890             'view',
891             'view.php?id=' . $assign->get_course_module()->id,
892             get_string('viewrevealidentitiesconfirm', 'assign'),
893             $assign->get_course_module()->id
894         );
895         $this->assertEventLegacyLogData($expected, $event);
896         $this->assertEventContextNotUsed($event);
897     }
899     /**
900      * Test the statement_accepted event.
901      */
902     public function test_statement_accepted() {
903         // We want to be a student so we can submit assignments.
904         $this->setUser($this->students[0]);
906         // We do not want to send any messages to the student during the PHPUNIT test.
907         set_config('submissionreceipts', false, 'assign');
909         $assign = $this->create_instance();
911         // Create the data we want to pass to the submit_for_grading function.
912         $data = new stdClass();
913         $data->submissionstatement = 'We are the Borg. You will be assimilated. Resistance is futile. - do you agree
914             to these terms?';
916         // Trigger and capture the event.
917         $sink = $this->redirectEvents();
918         $assign->submit_for_grading($data, array());
919         $events = $sink->get_events();
920         $event = reset($events);
922         // Check that the event contains the expected values.
923         $this->assertInstanceOf('\mod_assign\event\statement_accepted', $event);
924         $this->assertEquals($assign->get_context(), $event->get_context());
925         $expected = array(
926             $assign->get_course()->id,
927             'assign',
928             'submission statement accepted',
929             'view.php?id=' . $assign->get_course_module()->id,
930             get_string('submissionstatementacceptedlog',
931                 'mod_assign',
932                 fullname($this->students[0])),
933             $assign->get_course_module()->id
934         );
935         $this->assertEventLegacyLogData($expected, $event);
936         $this->assertEventContextNotUsed($event);
938         // Enable the online text submission plugin.
939         $submissionplugins = $assign->get_submission_plugins();
940         foreach ($submissionplugins as $plugin) {
941             if ($plugin->get_type() === 'onlinetext') {
942                 $plugin->enable();
943                 break;
944             }
945         }
947         // Create the data we want to pass to the save_submission function.
948         $data = new stdClass();
949         $data->onlinetext_editor = array(
950             'text' => 'Online text',
951             'format' => FORMAT_HTML,
952             'itemid' => file_get_unused_draft_itemid()
953         );
954         $data->submissionstatement = 'We are the Borg. You will be assimilated. Resistance is futile. - do you agree
955             to these terms?';
957         // Trigger and capture the event.
958         $sink = $this->redirectEvents();
959         $assign->save_submission($data, $notices);
960         $events = $sink->get_events();
961         $event = $events[2];
963         // Check that the event contains the expected values.
964         $this->assertInstanceOf('\mod_assign\event\statement_accepted', $event);
965         $this->assertEquals($assign->get_context(), $event->get_context());
966         $this->assertEventLegacyLogData($expected, $event);
967         $this->assertEventContextNotUsed($event);
968     }
970     /**
971      * Test the batch_set_workflow_state_viewed event.
972      */
973     public function test_batch_set_workflow_state_viewed() {
974         $assign = $this->create_instance();
976         // Trigger and capture the event.
977         $sink = $this->redirectEvents();
978         $assign->testable_view_batch_set_workflow_state($this->students[0]->id);
979         $events = $sink->get_events();
980         $event = reset($events);
982         // Check that the event contains the expected values.
983         $this->assertInstanceOf('\mod_assign\event\batch_set_workflow_state_viewed', $event);
984         $this->assertEquals($assign->get_context(), $event->get_context());
985         $expected = array(
986             $assign->get_course()->id,
987             'assign',
988             'view batch set marking workflow state',
989             'view.php?id=' . $assign->get_course_module()->id,
990             get_string('viewbatchsetmarkingworkflowstate', 'assign'),
991             $assign->get_course_module()->id
992         );
993         $this->assertEventLegacyLogData($expected, $event);
994         $this->assertEventContextNotUsed($event);
995     }
997     /**
998      * Test the batch_set_marker_allocation_viewed event.
999      */
1000     public function test_batch_set_marker_allocation_viewed() {
1001         $assign = $this->create_instance();
1003         // Trigger and capture the event.
1004         $sink = $this->redirectEvents();
1005         $assign->testable_view_batch_markingallocation($this->students[0]->id);
1006         $events = $sink->get_events();
1007         $event = reset($events);
1009         // Check that the event contains the expected values.
1010         $this->assertInstanceOf('\mod_assign\event\batch_set_marker_allocation_viewed', $event);
1011         $this->assertEquals($assign->get_context(), $event->get_context());
1012         $expected = array(
1013             $assign->get_course()->id,
1014             'assign',
1015             'view batch set marker allocation',
1016             'view.php?id=' . $assign->get_course_module()->id,
1017             get_string('viewbatchmarkingallocation', 'assign'),
1018             $assign->get_course_module()->id
1019         );
1020         $this->assertEventLegacyLogData($expected, $event);
1021         $this->assertEventContextNotUsed($event);
1022     }