4c4400222f2df1b5f9d8fcc206f50bc2927792b2
[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         $sink = $this->redirectEvents();
382         $assign->testable_process_set_batch_marking_workflow_state($this->students[0]->id, ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW);
384         $events = $sink->get_events();
385         $this->assertCount(1, $events);
386         $event = reset($events);
387         $this->assertInstanceOf('\mod_assign\event\workflow_state_updated', $event);
388         $this->assertEquals($assign->get_context(), $event->get_context());
389         $this->assertEquals($assign->get_instance()->id, $event->objectid);
390         $this->assertEquals($this->students[0]->id, $event->relateduserid);
391         $this->assertEquals($this->editingteachers[0]->id, $event->userid);
392         $this->assertEquals(ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW, $event->other['newstate']);
393         $expected = array(
394             $assign->get_course()->id,
395             'assign',
396             'set marking workflow state',
397             'view.php?id=' . $assign->get_course_module()->id,
398             get_string('setmarkingworkflowstateforlog', 'assign', array('id' => $this->students[0]->id,
399                 'fullname' => fullname($this->students[0]), 'state' => ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW)),
400             $assign->get_course_module()->id
401         );
402         $this->assertEventLegacyLogData($expected, $event);
403         $sink->close();
405         // Revert to defaults.
406         $this->editingteachers[0]->ignoresesskey = false;
407     }
409     public function test_submission_duplicated() {
410         $this->setUser($this->students[0]);
412         $assign = $this->create_instance();
413         $submission1 = $assign->get_user_submission($this->students[0]->id, true, 0);
414         $submission2 = $assign->get_user_submission($this->students[0]->id, true, 1);
415         $submission2->status = ASSIGN_SUBMISSION_STATUS_REOPENED;
416         $assign->testable_update_submission($submission2, $this->students[0]->id, time(), $assign->get_instance()->teamsubmission);
418         $sink = $this->redirectEvents();
419         $notices = null;
420         $assign->copy_previous_attempt($notices);
422         $events = $sink->get_events();
423         $this->assertCount(1, $events);
424         $event = reset($events);
425         $this->assertInstanceOf('\mod_assign\event\submission_duplicated', $event);
426         $this->assertEquals($assign->get_context(), $event->get_context());
427         $this->assertEquals($submission2->id, $event->objectid);
428         $this->assertEquals($this->students[0]->id, $event->userid);
429         $submission2->status = ASSIGN_SUBMISSION_STATUS_DRAFT;
430         $expected = array(
431             $assign->get_course()->id,
432             'assign',
433             'submissioncopied',
434             'view.php?id=' . $assign->get_course_module()->id,
435             $assign->testable_format_submission_for_log($submission2),
436             $assign->get_course_module()->id
437         );
438         $this->assertEventLegacyLogData($expected, $event);
439         $sink->close();
440     }
442     public function test_submission_unlocked() {
443         $this->editingteachers[0]->ignoresesskey = true;
444         $this->setUser($this->editingteachers[0]);
446         $assign = $this->create_instance();
447         $sink = $this->redirectEvents();
449         $assign->unlock_submission($this->students[0]->id);
451         $events = $sink->get_events();
452         $this->assertCount(1, $events);
453         $event = reset($events);
454         $this->assertInstanceOf('\mod_assign\event\submission_unlocked', $event);
455         $this->assertEquals($assign->get_context(), $event->get_context());
456         $this->assertEquals($assign->get_instance()->id, $event->objectid);
457         $this->assertEquals($this->students[0]->id, $event->relateduserid);
458         $expected = array(
459             $assign->get_course()->id,
460             'assign',
461             'unlock submission',
462             'view.php?id=' . $assign->get_course_module()->id,
463             get_string('unlocksubmissionforstudent', 'assign', array('id' => $this->students[0]->id,
464                 'fullname' => fullname($this->students[0]))),
465             $assign->get_course_module()->id
466         );
467         $this->assertEventLegacyLogData($expected, $event);
468         $sink->close();
470         // Revert to defaults.
471         $this->editingteachers[0]->ignoresesskey = false;
472     }
474     public function test_submission_graded() {
475         $this->setUser($this->editingteachers[0]);
476         $assign = $this->create_instance();
478         // Test apply_grade_to_user.
479         $sink = $this->redirectEvents();
481         $data = new stdClass();
482         $data->grade = '50.0';
483         $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
484         $grade = $assign->get_user_grade($this->students[0]->id, false, 0);
486         $events = $sink->get_events();
487         $this->assertCount(1, $events);
488         $event = reset($events);
489         $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
490         $this->assertEquals($assign->get_context(), $event->get_context());
491         $this->assertEquals($grade->id, $event->objectid);
492         $this->assertEquals($this->students[0]->id, $event->relateduserid);
493         $expected = array(
494             $assign->get_course()->id,
495             'assign',
496             'grade submission',
497             'view.php?id=' . $assign->get_course_module()->id,
498             $assign->format_grade_for_log($grade),
499             $assign->get_course_module()->id
500         );
501         $this->assertEventLegacyLogData($expected, $event);
502         $sink->close();
504         // Test process_save_quick_grades.
505         $sink = $this->redirectEvents();
507         $data = array(
508             'grademodified_' . $this->students[0]->id => time(),
509             'quickgrade_' . $this->students[0]->id => '60.0'
510         );
511         $assign->testable_process_save_quick_grades($data);
512         $grade = $assign->get_user_grade($this->students[0]->id, false);
513         $this->assertEquals('60.0', $grade->grade);
515         $events = $sink->get_events();
516         $this->assertCount(1, $events);
517         $event = reset($events);
518         $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
519         $this->assertEquals($assign->get_context(), $event->get_context());
520         $this->assertEquals($grade->id, $event->objectid);
521         $this->assertEquals($this->students[0]->id, $event->relateduserid);
522         $expected = array(
523             $assign->get_course()->id,
524             'assign',
525             'grade submission',
526             'view.php?id=' . $assign->get_course_module()->id,
527             $assign->format_grade_for_log($grade),
528             $assign->get_course_module()->id
529         );
530         $this->assertEventLegacyLogData($expected, $event);
531         $sink->close();
533         // Test update_grade.
534         $sink = $this->redirectEvents();
535         $data = clone($grade);
536         $data->grade = '50.0';
537         $assign->update_grade($data);
538         $grade = $assign->get_user_grade($this->students[0]->id, false, 0);
539         $this->assertEquals('50.0', $grade->grade);
540         $events = $sink->get_events();
542         $this->assertCount(1, $events);
543         $event = reset($events);
544         $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
545         $this->assertEquals($assign->get_context(), $event->get_context());
546         $this->assertEquals($grade->id, $event->objectid);
547         $this->assertEquals($this->students[0]->id, $event->relateduserid);
548         $expected = array(
549             $assign->get_course()->id,
550             'assign',
551             'grade submission',
552             'view.php?id=' . $assign->get_course_module()->id,
553             $assign->format_grade_for_log($grade),
554             $assign->get_course_module()->id
555         );
556         $this->assertEventLegacyLogData($expected, $event);
557         $sink->close();
558     }
560     /**
561      * Test the submission_viewed event.
562      */
563     public function test_submission_viewed() {
564         global $PAGE;
566         $this->setUser($this->editingteachers[0]);
568         $assign = $this->create_instance();
569         $submission = $assign->get_user_submission($this->students[0]->id, true);
571         // We need to set the URL in order to view the submission.
572         $PAGE->set_url('/a_url');
573         // A hack - these variables are used by the view_plugin_content function to
574         // determine what we actually want to view - would usually be set in URL.
575         global $_POST;
576         $_POST['plugin'] = 'comments';
577         $_POST['sid'] = $submission->id;
579         // Trigger and capture the event.
580         $sink = $this->redirectEvents();
581         $assign->view('viewpluginassignsubmission');
582         $events = $sink->get_events();
583         $this->assertCount(1, $events);
584         $event = reset($events);
586         // Check that the event contains the expected values.
587         $this->assertInstanceOf('\mod_assign\event\submission_viewed', $event);
588         $this->assertEquals($assign->get_context(), $event->get_context());
589         $this->assertEquals($submission->id, $event->objectid);
590         $expected = array(
591             $assign->get_course()->id,
592             'assign',
593             'view submission',
594             'view.php?id=' . $assign->get_course_module()->id,
595             get_string('viewsubmissionforuser', 'assign', $this->students[0]->id),
596             $assign->get_course_module()->id
597         );
598         $this->assertEventLegacyLogData($expected, $event);
599         $this->assertEventContextNotUsed($event);
600     }
602     /**
603      * Test the feedback_viewed event.
604      */
605     public function test_feedback_viewed() {
606         global $DB, $PAGE;
608         $this->setUser($this->editingteachers[0]);
610         $assign = $this->create_instance();
611         $submission = $assign->get_user_submission($this->students[0]->id, true);
613         // Insert a grade for this submission.
614         $grade = new stdClass();
615         $grade->assignment = 1;
616         $grade->userid = $this->students[0]->id;
617         $gradeid = $DB->insert_record('assign_grades', $grade);
619         // We need to set the URL in order to view the feedback.
620         $PAGE->set_url('/a_url');
621         // A hack - these variables are used by the view_plugin_content function to
622         // determine what we actually want to view - would usually be set in URL.
623         global $_POST;
624         $_POST['plugin'] = 'comments';
625         $_POST['gid'] = $gradeid;
626         $_POST['sid'] = $submission->id;
628         // Trigger and capture the event.
629         $sink = $this->redirectEvents();
630         $assign->view('viewpluginassignfeedback');
631         $events = $sink->get_events();
632         $this->assertCount(1, $events);
633         $event = reset($events);
635         // Check that the event contains the expected values.
636         $this->assertInstanceOf('\mod_assign\event\feedback_viewed', $event);
637         $this->assertEquals($assign->get_context(), $event->get_context());
638         $this->assertEquals($gradeid, $event->objectid);
639         $expected = array(
640             $assign->get_course()->id,
641             'assign',
642             'view feedback',
643             'view.php?id=' . $assign->get_course_module()->id,
644             get_string('viewfeedbackforuser', 'assign', $this->students[0]->id),
645             $assign->get_course_module()->id
646         );
647         $this->assertEventLegacyLogData($expected, $event);
648         $this->assertEventContextNotUsed($event);
649     }
651     /**
652      * Test the grading_form_viewed event.
653      */
654     public function test_grading_form_viewed() {
655         global $PAGE;
657         $this->setUser($this->editingteachers[0]);
659         $assign = $this->create_instance();
661         // We need to set the URL in order to view the feedback.
662         $PAGE->set_url('/a_url');
663         // A hack - this variable is used by the view_single_grade_page function.
664         global $_POST;
665         $_POST['rownum'] = 1;
666         $_POST['userid'] = $this->students[0]->id;
668         // Trigger and capture the event.
669         $sink = $this->redirectEvents();
670         $assign->view('grade');
671         $events = $sink->get_events();
672         $this->assertCount(1, $events);
673         $event = reset($events);
675         // Check that the event contains the expected values.
676         $this->assertInstanceOf('\mod_assign\event\grading_form_viewed', $event);
677         $this->assertEquals($assign->get_context(), $event->get_context());
678         $expected = array(
679             $assign->get_course()->id,
680             'assign',
681             'view grading form',
682             'view.php?id=' . $assign->get_course_module()->id,
683             get_string('viewgradingformforstudent', 'assign', array('id' => $this->students[0]->id,
684                 'fullname' => fullname($this->students[0]))),
685             $assign->get_course_module()->id
686         );
687         $this->assertEventLegacyLogData($expected, $event);
688         $this->assertEventContextNotUsed($event);
689     }
691     /**
692      * Test the grading_table_viewed event.
693      */
694     public function test_grading_table_viewed() {
695         global $PAGE;
697         $this->setUser($this->editingteachers[0]);
699         $assign = $this->create_instance();
701         // We need to set the URL in order to view the feedback.
702         $PAGE->set_url('/a_url');
703         // A hack - this variable is used by the view_single_grade_page function.
704         global $_POST;
705         $_POST['rownum'] = 1;
706         $_POST['userid'] = $this->students[0]->id;
708         // Trigger and capture the event.
709         $sink = $this->redirectEvents();
710         $assign->view('grading');
711         $events = $sink->get_events();
712         $this->assertCount(1, $events);
713         $event = reset($events);
715         // Check that the event contains the expected values.
716         $this->assertInstanceOf('\mod_assign\event\grading_table_viewed', $event);
717         $this->assertEquals($assign->get_context(), $event->get_context());
718         $expected = array(
719             $assign->get_course()->id,
720             'assign',
721             'view submission grading table',
722             'view.php?id=' . $assign->get_course_module()->id,
723             get_string('viewsubmissiongradingtable', 'assign'),
724             $assign->get_course_module()->id
725         );
726         $this->assertEventLegacyLogData($expected, $event);
727         $this->assertEventContextNotUsed($event);
728     }
730     /**
731      * Test the submission_form_viewed event.
732      */
733     public function test_submission_form_viewed() {
734         global $PAGE;
736         $this->setUser($this->students[0]);
738         $assign = $this->create_instance();
740         // We need to set the URL in order to view the submission form.
741         $PAGE->set_url('/a_url');
743         // Trigger and capture the event.
744         $sink = $this->redirectEvents();
745         $assign->view('editsubmission');
746         $events = $sink->get_events();
747         $this->assertCount(1, $events);
748         $event = reset($events);
750         // Check that the event contains the expected values.
751         $this->assertInstanceOf('\mod_assign\event\submission_form_viewed', $event);
752         $this->assertEquals($assign->get_context(), $event->get_context());
753         $expected = array(
754             $assign->get_course()->id,
755             'assign',
756             'view submit assignment form',
757             'view.php?id=' . $assign->get_course_module()->id,
758             get_string('editsubmission', 'assign'),
759             $assign->get_course_module()->id
760         );
761         $this->assertEventLegacyLogData($expected, $event);
762         $this->assertEventContextNotUsed($event);
763     }
765     /**
766      * Test the submission_form_viewed event.
767      */
768     public function test_submission_confirmation_form_viewed() {
769         global $PAGE;
771         $this->setUser($this->students[0]);
773         $assign = $this->create_instance();
775         // We need to set the URL in order to view the submission form.
776         $PAGE->set_url('/a_url');
778         // Trigger and capture the event.
779         $sink = $this->redirectEvents();
780         $assign->view('submit');
781         $events = $sink->get_events();
782         $this->assertCount(1, $events);
783         $event = reset($events);
785         // Check that the event contains the expected values.
786         $this->assertInstanceOf('\mod_assign\event\submission_confirmation_form_viewed', $event);
787         $this->assertEquals($assign->get_context(), $event->get_context());
788         $expected = array(
789             $assign->get_course()->id,
790             'assign',
791             'view confirm submit assignment form',
792             'view.php?id=' . $assign->get_course_module()->id,
793             get_string('viewownsubmissionform', 'assign'),
794             $assign->get_course_module()->id
795         );
796         $this->assertEventLegacyLogData($expected, $event);
797         $this->assertEventContextNotUsed($event);
798     }
800     /**
801      * Test the reveal_identities_confirmation_page_viewed event.
802      */
803     public function test_reveal_identities_confirmation_page_viewed() {
804         global $PAGE;
806         // Set to the admin user so we have the permission to reveal identities.
807         $this->setAdminUser();
809         $assign = $this->create_instance();
811         // We need to set the URL in order to view the submission form.
812         $PAGE->set_url('/a_url');
814         // Trigger and capture the event.
815         $sink = $this->redirectEvents();
816         $assign->view('revealidentities');
817         $events = $sink->get_events();
818         $this->assertCount(1, $events);
819         $event = reset($events);
821         // Check that the event contains the expected values.
822         $this->assertInstanceOf('\mod_assign\event\reveal_identities_confirmation_page_viewed', $event);
823         $this->assertEquals($assign->get_context(), $event->get_context());
824         $expected = array(
825             $assign->get_course()->id,
826             'assign',
827             'view',
828             'view.php?id=' . $assign->get_course_module()->id,
829             get_string('viewrevealidentitiesconfirm', 'assign'),
830             $assign->get_course_module()->id
831         );
832         $this->assertEventLegacyLogData($expected, $event);
833         $this->assertEventContextNotUsed($event);
834     }
836     /**
837      * Test the statement_accepted event.
838      */
839     public function test_statement_accepted() {
840         // We want to be a student so we can submit assignments.
841         $this->setUser($this->students[0]);
843         // We do not want to send any messages to the student during the PHPUNIT test.
844         set_config('submissionreceipts', false, 'assign');
846         $assign = $this->create_instance();
848         // Create the data we want to pass to the submit_for_grading function.
849         $data = new stdClass();
850         $data->submissionstatement = 'We are the Borg. You will be assimilated. Resistance is futile. - do you agree
851             to these terms?';
853         // Trigger and capture the event.
854         $sink = $this->redirectEvents();
855         $assign->submit_for_grading($data, array());
856         $events = $sink->get_events();
857         $event = reset($events);
859         // Check that the event contains the expected values.
860         $this->assertInstanceOf('\mod_assign\event\statement_accepted', $event);
861         $this->assertEquals($assign->get_context(), $event->get_context());
862         $expected = array(
863             $assign->get_course()->id,
864             'assign',
865             'submission statement accepted',
866             'view.php?id=' . $assign->get_course_module()->id,
867             get_string('submissionstatementacceptedlog',
868                 'mod_assign',
869                 fullname($this->students[0])),
870             $assign->get_course_module()->id
871         );
872         $this->assertEventLegacyLogData($expected, $event);
873         $this->assertEventContextNotUsed($event);
875         // Enable the online text submission plugin.
876         $submissionplugins = $assign->get_submission_plugins();
877         foreach ($submissionplugins as $plugin) {
878             if ($plugin->get_type() === 'onlinetext') {
879                 $plugin->enable();
880                 break;
881             }
882         }
884         // Create the data we want to pass to the save_submission function.
885         $data = new stdClass();
886         $data->onlinetext_editor = array(
887             'text' => 'Online text',
888             'format' => FORMAT_HTML,
889             'itemid' => file_get_unused_draft_itemid()
890         );
891         $data->submissionstatement = 'We are the Borg. You will be assimilated. Resistance is futile. - do you agree
892             to these terms?';
894         // Trigger and capture the event.
895         $sink = $this->redirectEvents();
896         $assign->save_submission($data, $notices);
897         $events = $sink->get_events();
898         $event = $events[2];
900         // Check that the event contains the expected values.
901         $this->assertInstanceOf('\mod_assign\event\statement_accepted', $event);
902         $this->assertEquals($assign->get_context(), $event->get_context());
903         $this->assertEventLegacyLogData($expected, $event);
904         $this->assertEventContextNotUsed($event);
905     }
907     /**
908      * Test the batch_set_workflow_state_viewed event.
909      */
910     public function test_batch_set_workflow_state_viewed() {
911         $assign = $this->create_instance();
913         // Trigger and capture the event.
914         $sink = $this->redirectEvents();
915         $assign->testable_view_batch_set_workflow_state();
916         $events = $sink->get_events();
917         $event = reset($events);
919         // Check that the event contains the expected values.
920         $this->assertInstanceOf('\mod_assign\event\batch_set_workflow_state_viewed', $event);
921         $this->assertEquals($assign->get_context(), $event->get_context());
922         $expected = array(
923             $assign->get_course()->id,
924             'assign',
925             'view batch set marking workflow state',
926             'view.php?id=' . $assign->get_course_module()->id,
927             get_string('viewbatchsetmarkingworkflowstate', 'assign'),
928             $assign->get_course_module()->id
929         );
930         $this->assertEventLegacyLogData($expected, $event);
931         $this->assertEventContextNotUsed($event);
932     }
934     /**
935      * Test the batch_set_marker_allocation_viewed event.
936      */
937     public function test_batch_set_marker_allocation_viewed() {
938         $assign = $this->create_instance();
940         // Trigger and capture the event.
941         $sink = $this->redirectEvents();
942         $assign->testable_view_batch_markingallocation();
943         $events = $sink->get_events();
944         $event = reset($events);
946         // Check that the event contains the expected values.
947         $this->assertInstanceOf('\mod_assign\event\batch_set_marker_allocation_viewed', $event);
948         $this->assertEquals($assign->get_context(), $event->get_context());
949         $expected = array(
950             $assign->get_course()->id,
951             'assign',
952             'view batch set marker allocation',
953             'view.php?id=' . $assign->get_course_module()->id,
954             get_string('viewbatchmarkingallocation', 'assign'),
955             $assign->get_course_module()->id
956         );
957         $this->assertEventLegacyLogData($expected, $event);
958         $this->assertEventContextNotUsed($event);
959     }