e73cb8e4e981dfa41ffbb51e43eac9d69d498aa0
[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_submission_created() {
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         // Standard Event parameters.
49         $params = array(
50             'context' => $modcontext,
51             'courseid' => $this->course->id
52         );
54         $eventinfo = $params;
55         $eventinfo['other'] = array(
56             'submissionid' => '17',
57             'submissionattempt' => 0,
58             'submissionstatus' => 'submitted'
59         );
61         $sink = $this->redirectEvents();
62         $event = \mod_assign_unittests\event\submission_created::create($eventinfo);
63         $event->trigger();
64         $result = $sink->get_events();
65         $event = reset($result);
66         $sink->close();
68         $this->assertEquals($modcontext->id, $event->contextid);
69         $this->assertEquals($this->course->id, $event->courseid);
71         // Check that an error occurs when teamsubmission is not set.
72         try {
73             \mod_assign_unittests\event\submission_created::create($params);
74             $this->fail('Other must contain the key submissionid.');
75         } catch (Exception $e) {
76             $this->assertInstanceOf('coding_exception', $e);
77         }
78         // Check that the submission status debugging is fired.
79         $subinfo = $params;
80         $subinfo['other'] = array('submissionid' => '23');
81         try {
82             \mod_assign_unittests\event\submission_created::create($subinfo);
83             $this->fail('Other must contain the key submissionattempt.');
84         } catch (Exception $e) {
85             $this->assertInstanceOf('coding_exception', $e);
86         }
88         $subinfo['other'] = array('submissionattempt' => '0');
89         try {
90             \mod_assign_unittests\event\submission_created::create($subinfo);
91             $this->fail('Other must contain the key submissionstatus.');
92         } catch (Exception $e) {
93             $this->assertInstanceOf('coding_exception', $e);
94         }
95     }
97     /**
98      * Basic tests for the submission_updated() abstract class.
99      */
100     public function test_submission_updated() {
101         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
102         $instance = $generator->create_instance(array('course' => $this->course->id));
103         $modcontext = context_module::instance($instance->cmid);
105         // Standard Event parameters.
106         $params = array(
107             'context' => $modcontext,
108             'courseid' => $this->course->id
109         );
111         $eventinfo = $params;
112         $eventinfo['other'] = array(
113             'submissionid' => '17',
114             'submissionattempt' => 0,
115             'submissionstatus' => 'submitted'
116         );
118         $sink = $this->redirectEvents();
119         $event = \mod_assign_unittests\event\submission_updated::create($eventinfo);
120         $event->trigger();
121         $result = $sink->get_events();
122         $event = reset($result);
123         $sink->close();
125         $this->assertEquals($modcontext->id, $event->contextid);
126         $this->assertEquals($this->course->id, $event->courseid);
128         // Check that an error occurs when teamsubmission is not set.
129         try {
130             \mod_assign_unittests\event\submission_created::create($params);
131             $this->fail('Other must contain the key submissionid.');
132         } catch (Exception $e) {
133             $this->assertInstanceOf('coding_exception', $e);
134         }
135         // Check that the submission status debugging is fired.
136         $subinfo = $params;
137         $subinfo['other'] = array('submissionid' => '23');
138         try {
139             \mod_assign_unittests\event\submission_created::create($subinfo);
140             $this->fail('Other must contain the key submissionattempt.');
141         } catch (Exception $e) {
142             $this->assertInstanceOf('coding_exception', $e);
143         }
145         $subinfo['other'] = array('submissionattempt' => '0');
146         try {
147             \mod_assign_unittests\event\submission_created::create($subinfo);
148             $this->fail('Other must contain the key submissionstatus.');
149         } catch (Exception $e) {
150             $this->assertInstanceOf('coding_exception', $e);
151         }
152     }
154     public function test_extension_granted() {
155         $this->setUser($this->editingteachers[0]);
157         $tomorrow = time() + 24*60*60;
158         $yesterday = time() - 24*60*60;
160         $assign = $this->create_instance(array('duedate' => $yesterday, 'cutoffdate' => $yesterday));
161         $sink = $this->redirectEvents();
163         $assign->testable_save_user_extension($this->students[0]->id, $tomorrow);
165         $events = $sink->get_events();
166         $this->assertCount(1, $events);
167         $event = reset($events);
168         $this->assertInstanceOf('\mod_assign\event\extension_granted', $event);
169         $this->assertEquals($assign->get_context(), $event->get_context());
170         $this->assertEquals($assign->get_instance()->id, $event->objectid);
171         $this->assertEquals($this->students[0]->id, $event->relateduserid);
172         $expected = array(
173             $assign->get_course()->id,
174             'assign',
175             'grant extension',
176             'view.php?id=' . $assign->get_course_module()->id,
177             $this->students[0]->id,
178             $assign->get_course_module()->id
179         );
180         $this->assertEventLegacyLogData($expected, $event);
181         $sink->close();
182     }
184     public function test_submission_locked() {
185         $this->editingteachers[0]->ignoresesskey = true;
186         $this->setUser($this->editingteachers[0]);
188         $assign = $this->create_instance();
189         $sink = $this->redirectEvents();
191         $assign->lock_submission($this->students[0]->id);
193         $events = $sink->get_events();
194         $this->assertCount(1, $events);
195         $event = reset($events);
196         $this->assertInstanceOf('\mod_assign\event\submission_locked', $event);
197         $this->assertEquals($assign->get_context(), $event->get_context());
198         $this->assertEquals($assign->get_instance()->id, $event->objectid);
199         $this->assertEquals($this->students[0]->id, $event->relateduserid);
200         $expected = array(
201             $assign->get_course()->id,
202             'assign',
203             'lock submission',
204             'view.php?id=' . $assign->get_course_module()->id,
205             get_string('locksubmissionforstudent', 'assign', array('id' => $this->students[0]->id,
206                 'fullname' => fullname($this->students[0]))),
207             $assign->get_course_module()->id
208         );
209         $this->assertEventLegacyLogData($expected, $event);
210         $sink->close();
212         // Revert to defaults.
213         $this->editingteachers[0]->ignoresesskey = false;
214     }
216     public function test_identities_revealed() {
217         $this->editingteachers[0]->ignoresesskey = true;
218         $this->setUser($this->editingteachers[0]);
220         $assign = $this->create_instance(array('blindmarking'=>1));
221         $sink = $this->redirectEvents();
223         $assign->reveal_identities();
225         $events = $sink->get_events();
226         $this->assertCount(1, $events);
227         $event = reset($events);
228         $this->assertInstanceOf('\mod_assign\event\identities_revealed', $event);
229         $this->assertEquals($assign->get_context(), $event->get_context());
230         $this->assertEquals($assign->get_instance()->id, $event->objectid);
231         $expected = array(
232             $assign->get_course()->id,
233             'assign',
234             'reveal identities',
235             'view.php?id=' . $assign->get_course_module()->id,
236             get_string('revealidentities', 'assign'),
237             $assign->get_course_module()->id
238         );
239         $this->assertEventLegacyLogData($expected, $event);
240         $sink->close();
242         // Revert to defaults.
243         $this->editingteachers[0]->ignoresesskey = false;
244     }
246     /**
247      * Test the submission_status_viewed event.
248      */
249     public function test_submission_status_viewed() {
250         global $PAGE;
252         $this->setUser($this->editingteachers[0]);
254         $assign = $this->create_instance();
256         // We need to set the URL in order to view the feedback.
257         $PAGE->set_url('/a_url');
259         // Trigger and capture the event.
260         $sink = $this->redirectEvents();
261         $assign->view();
262         $events = $sink->get_events();
263         $this->assertCount(1, $events);
264         $event = reset($events);
266         // Check that the event contains the expected values.
267         $this->assertInstanceOf('\mod_assign\event\submission_status_viewed', $event);
268         $this->assertEquals($assign->get_context(), $event->get_context());
269         $expected = array(
270             $assign->get_course()->id,
271             'assign',
272             'view',
273             'view.php?id=' . $assign->get_course_module()->id,
274             get_string('viewownsubmissionstatus', 'assign'),
275             $assign->get_course_module()->id
276         );
277         $this->assertEventLegacyLogData($expected, $event);
278         $this->assertEventContextNotUsed($event);
279     }
281     public function test_submission_status_updated() {
282         $this->editingteachers[0]->ignoresesskey = true;
283         $this->setUser($this->editingteachers[0]);
285         $assign = $this->create_instance();
286         $submission = $assign->get_user_submission($this->students[0]->id, true);
287         $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
288         $assign->testable_update_submission($submission, $this->students[0]->id, true, false);
290         $sink = $this->redirectEvents();
291         $assign->revert_to_draft($this->students[0]->id);
293         $events = $sink->get_events();
294         $this->assertCount(1, $events);
295         $event = reset($events);
296         $this->assertInstanceOf('\mod_assign\event\submission_status_updated', $event);
297         $this->assertEquals($assign->get_context(), $event->get_context());
298         $this->assertEquals($submission->id, $event->objectid);
299         $this->assertEquals($this->students[0]->id, $event->relateduserid);
300         $this->assertEquals(ASSIGN_SUBMISSION_STATUS_DRAFT, $event->other['newstatus']);
301         $expected = array(
302             $assign->get_course()->id,
303             'assign',
304             'revert submission to draft',
305             'view.php?id=' . $assign->get_course_module()->id,
306             get_string('reverttodraftforstudent', 'assign', array('id' => $this->students[0]->id,
307                 'fullname' => fullname($this->students[0]))),
308             $assign->get_course_module()->id
309         );
310         $this->assertEventLegacyLogData($expected, $event);
311         $sink->close();
313         // Revert to defaults.
314         $this->editingteachers[0]->ignoresesskey = false;
315     }
317     public function test_marker_updated() {
318         $this->editingteachers[0]->ignoresesskey = true;
319         $this->setUser($this->editingteachers[0]);
321         $assign = $this->create_instance();
323         $sink = $this->redirectEvents();
324         $assign->testable_process_set_batch_marking_allocation($this->students[0]->id, $this->teachers[0]->id);
326         $events = $sink->get_events();
327         $this->assertCount(1, $events);
328         $event = reset($events);
329         $this->assertInstanceOf('\mod_assign\event\marker_updated', $event);
330         $this->assertEquals($assign->get_context(), $event->get_context());
331         $this->assertEquals($assign->get_instance()->id, $event->objectid);
332         $this->assertEquals($this->students[0]->id, $event->relateduserid);
333         $this->assertEquals($this->editingteachers[0]->id, $event->userid);
334         $this->assertEquals($this->teachers[0]->id, $event->other['markerid']);
335         $expected = array(
336             $assign->get_course()->id,
337             'assign',
338             'set marking allocation',
339             'view.php?id=' . $assign->get_course_module()->id,
340             get_string('setmarkerallocationforlog', 'assign', array('id' => $this->students[0]->id,
341                 'fullname' => fullname($this->students[0]), 'marker' => fullname($this->teachers[0]))),
342             $assign->get_course_module()->id
343         );
344         $this->assertEventLegacyLogData($expected, $event);
345         $sink->close();
347         // Revert to defaults.
348         $this->editingteachers[0]->ignoresesskey = false;
349     }
351     public function test_workflow_state_updated() {
352         $this->editingteachers[0]->ignoresesskey = true;
353         $this->setUser($this->editingteachers[0]);
355         $assign = $this->create_instance();
357         $sink = $this->redirectEvents();
358         $assign->testable_process_set_batch_marking_workflow_state($this->students[0]->id, ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW);
360         $events = $sink->get_events();
361         $this->assertCount(1, $events);
362         $event = reset($events);
363         $this->assertInstanceOf('\mod_assign\event\workflow_state_updated', $event);
364         $this->assertEquals($assign->get_context(), $event->get_context());
365         $this->assertEquals($assign->get_instance()->id, $event->objectid);
366         $this->assertEquals($this->students[0]->id, $event->relateduserid);
367         $this->assertEquals($this->editingteachers[0]->id, $event->userid);
368         $this->assertEquals(ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW, $event->other['newstate']);
369         $expected = array(
370             $assign->get_course()->id,
371             'assign',
372             'set marking workflow state',
373             'view.php?id=' . $assign->get_course_module()->id,
374             get_string('setmarkingworkflowstateforlog', 'assign', array('id' => $this->students[0]->id,
375                 'fullname' => fullname($this->students[0]), 'state' => ASSIGN_MARKING_WORKFLOW_STATE_INREVIEW)),
376             $assign->get_course_module()->id
377         );
378         $this->assertEventLegacyLogData($expected, $event);
379         $sink->close();
381         // Revert to defaults.
382         $this->editingteachers[0]->ignoresesskey = false;
383     }
385     public function test_submission_duplicated() {
386         $this->setUser($this->students[0]);
388         $assign = $this->create_instance();
389         $submission1 = $assign->get_user_submission($this->students[0]->id, true, 0);
390         $submission2 = $assign->get_user_submission($this->students[0]->id, true, 1);
391         $submission2->status = ASSIGN_SUBMISSION_STATUS_REOPENED;
392         $assign->testable_update_submission($submission2, $this->students[0]->id, time(), $assign->get_instance()->teamsubmission);
394         $sink = $this->redirectEvents();
395         $notices = null;
396         $assign->copy_previous_attempt($notices);
398         $events = $sink->get_events();
399         $this->assertCount(1, $events);
400         $event = reset($events);
401         $this->assertInstanceOf('\mod_assign\event\submission_duplicated', $event);
402         $this->assertEquals($assign->get_context(), $event->get_context());
403         $this->assertEquals($submission2->id, $event->objectid);
404         $this->assertEquals($this->students[0]->id, $event->userid);
405         $submission2->status = ASSIGN_SUBMISSION_STATUS_DRAFT;
406         $expected = array(
407             $assign->get_course()->id,
408             'assign',
409             'submissioncopied',
410             'view.php?id=' . $assign->get_course_module()->id,
411             $assign->testable_format_submission_for_log($submission2),
412             $assign->get_course_module()->id
413         );
414         $this->assertEventLegacyLogData($expected, $event);
415         $sink->close();
416     }
418     public function test_submission_unlocked() {
419         $this->editingteachers[0]->ignoresesskey = true;
420         $this->setUser($this->editingteachers[0]);
422         $assign = $this->create_instance();
423         $sink = $this->redirectEvents();
425         $assign->unlock_submission($this->students[0]->id);
427         $events = $sink->get_events();
428         $this->assertCount(1, $events);
429         $event = reset($events);
430         $this->assertInstanceOf('\mod_assign\event\submission_unlocked', $event);
431         $this->assertEquals($assign->get_context(), $event->get_context());
432         $this->assertEquals($assign->get_instance()->id, $event->objectid);
433         $this->assertEquals($this->students[0]->id, $event->relateduserid);
434         $expected = array(
435             $assign->get_course()->id,
436             'assign',
437             'unlock submission',
438             'view.php?id=' . $assign->get_course_module()->id,
439             get_string('unlocksubmissionforstudent', 'assign', array('id' => $this->students[0]->id,
440                 'fullname' => fullname($this->students[0]))),
441             $assign->get_course_module()->id
442         );
443         $this->assertEventLegacyLogData($expected, $event);
444         $sink->close();
446         // Revert to defaults.
447         $this->editingteachers[0]->ignoresesskey = false;
448     }
450     public function test_submission_graded() {
451         $this->setUser($this->editingteachers[0]);
452         $assign = $this->create_instance();
454         // Test apply_grade_to_user.
455         $sink = $this->redirectEvents();
457         $data = new stdClass();
458         $data->grade = '50.0';
459         $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
460         $grade = $assign->get_user_grade($this->students[0]->id, false, 0);
462         $events = $sink->get_events();
463         $this->assertCount(1, $events);
464         $event = reset($events);
465         $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
466         $this->assertEquals($assign->get_context(), $event->get_context());
467         $this->assertEquals($grade->id, $event->objectid);
468         $this->assertEquals($this->students[0]->id, $event->relateduserid);
469         $expected = array(
470             $assign->get_course()->id,
471             'assign',
472             'grade submission',
473             'view.php?id=' . $assign->get_course_module()->id,
474             $assign->format_grade_for_log($grade),
475             $assign->get_course_module()->id
476         );
477         $this->assertEventLegacyLogData($expected, $event);
478         $sink->close();
480         // Test process_save_quick_grades.
481         $sink = $this->redirectEvents();
483         $data = array(
484             'grademodified_' . $this->students[0]->id => time(),
485             'quickgrade_' . $this->students[0]->id => '60.0'
486         );
487         $assign->testable_process_save_quick_grades($data);
488         $grade = $assign->get_user_grade($this->students[0]->id, false);
489         $this->assertEquals('60.0', $grade->grade);
491         $events = $sink->get_events();
492         $this->assertCount(1, $events);
493         $event = reset($events);
494         $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
495         $this->assertEquals($assign->get_context(), $event->get_context());
496         $this->assertEquals($grade->id, $event->objectid);
497         $this->assertEquals($this->students[0]->id, $event->relateduserid);
498         $expected = array(
499             $assign->get_course()->id,
500             'assign',
501             'grade submission',
502             'view.php?id=' . $assign->get_course_module()->id,
503             $assign->format_grade_for_log($grade),
504             $assign->get_course_module()->id
505         );
506         $this->assertEventLegacyLogData($expected, $event);
507         $sink->close();
508     }
510     /**
511      * Test the submission_viewed event.
512      */
513     public function test_submission_viewed() {
514         global $PAGE;
516         $this->setUser($this->editingteachers[0]);
518         $assign = $this->create_instance();
519         $submission = $assign->get_user_submission($this->students[0]->id, true);
521         // We need to set the URL in order to view the submission.
522         $PAGE->set_url('/a_url');
523         // A hack - these variables are used by the view_plugin_content function to
524         // determine what we actually want to view - would usually be set in URL.
525         global $_POST;
526         $_POST['plugin'] = 'comments';
527         $_POST['sid'] = $submission->id;
529         // Trigger and capture the event.
530         $sink = $this->redirectEvents();
531         $assign->view('viewpluginassignsubmission');
532         $events = $sink->get_events();
533         $this->assertCount(1, $events);
534         $event = reset($events);
536         // Check that the event contains the expected values.
537         $this->assertInstanceOf('\mod_assign\event\submission_viewed', $event);
538         $this->assertEquals($assign->get_context(), $event->get_context());
539         $this->assertEquals($submission->id, $event->objectid);
540         $expected = array(
541             $assign->get_course()->id,
542             'assign',
543             'view submission',
544             'view.php?id=' . $assign->get_course_module()->id,
545             get_string('viewsubmissionforuser', 'assign', $this->students[0]->id),
546             $assign->get_course_module()->id
547         );
548         $this->assertEventLegacyLogData($expected, $event);
549         $this->assertEventContextNotUsed($event);
550     }
552     /**
553      * Test the feedback_viewed event.
554      */
555     public function test_feedback_viewed() {
556         global $DB, $PAGE;
558         $this->setUser($this->editingteachers[0]);
560         $assign = $this->create_instance();
561         $submission = $assign->get_user_submission($this->students[0]->id, true);
563         // Insert a grade for this submission.
564         $grade = new stdClass();
565         $grade->assignment = 1;
566         $grade->userid = $this->students[0]->id;
567         $gradeid = $DB->insert_record('assign_grades', $grade);
569         // We need to set the URL in order to view the feedback.
570         $PAGE->set_url('/a_url');
571         // A hack - these variables are used by the view_plugin_content function to
572         // determine what we actually want to view - would usually be set in URL.
573         global $_POST;
574         $_POST['plugin'] = 'comments';
575         $_POST['gid'] = $gradeid;
576         $_POST['sid'] = $submission->id;
578         // Trigger and capture the event.
579         $sink = $this->redirectEvents();
580         $assign->view('viewpluginassignfeedback');
581         $events = $sink->get_events();
582         $this->assertCount(1, $events);
583         $event = reset($events);
585         // Check that the event contains the expected values.
586         $this->assertInstanceOf('\mod_assign\event\feedback_viewed', $event);
587         $this->assertEquals($assign->get_context(), $event->get_context());
588         $this->assertEquals($gradeid, $event->objectid);
589         $expected = array(
590             $assign->get_course()->id,
591             'assign',
592             'view feedback',
593             'view.php?id=' . $assign->get_course_module()->id,
594             get_string('viewfeedbackforuser', 'assign', $this->students[0]->id),
595             $assign->get_course_module()->id
596         );
597         $this->assertEventLegacyLogData($expected, $event);
598         $this->assertEventContextNotUsed($event);
599     }
601     /**
602      * Test the grading_form_viewed event.
603      */
604     public function test_grading_form_viewed() {
605         global $PAGE;
607         $this->setUser($this->editingteachers[0]);
609         $assign = $this->create_instance();
611         // We need to set the URL in order to view the feedback.
612         $PAGE->set_url('/a_url');
613         // A hack - this variable is used by the view_single_grade_page function.
614         global $_POST;
615         $_POST['rownum'] = 1;
616         $_POST['userid'] = $this->students[0]->id;
618         // Trigger and capture the event.
619         $sink = $this->redirectEvents();
620         $assign->view('grade');
621         $events = $sink->get_events();
622         $this->assertCount(1, $events);
623         $event = reset($events);
625         // Check that the event contains the expected values.
626         $this->assertInstanceOf('\mod_assign\event\grading_form_viewed', $event);
627         $this->assertEquals($assign->get_context(), $event->get_context());
628         $expected = array(
629             $assign->get_course()->id,
630             'assign',
631             'view grading form',
632             'view.php?id=' . $assign->get_course_module()->id,
633             get_string('viewgradingformforstudent', 'assign', array('id' => $this->students[0]->id,
634                 'fullname' => fullname($this->students[0]))),
635             $assign->get_course_module()->id
636         );
637         $this->assertEventLegacyLogData($expected, $event);
638         $this->assertEventContextNotUsed($event);
639     }
641     /**
642      * Test the grading_table_viewed event.
643      */
644     public function test_grading_table_viewed() {
645         global $PAGE;
647         $this->setUser($this->editingteachers[0]);
649         $assign = $this->create_instance();
651         // We need to set the URL in order to view the feedback.
652         $PAGE->set_url('/a_url');
653         // A hack - this variable is used by the view_single_grade_page function.
654         global $_POST;
655         $_POST['rownum'] = 1;
656         $_POST['userid'] = $this->students[0]->id;
658         // Trigger and capture the event.
659         $sink = $this->redirectEvents();
660         $assign->view('grading');
661         $events = $sink->get_events();
662         $this->assertCount(1, $events);
663         $event = reset($events);
665         // Check that the event contains the expected values.
666         $this->assertInstanceOf('\mod_assign\event\grading_table_viewed', $event);
667         $this->assertEquals($assign->get_context(), $event->get_context());
668         $expected = array(
669             $assign->get_course()->id,
670             'assign',
671             'view submission grading table',
672             'view.php?id=' . $assign->get_course_module()->id,
673             get_string('viewsubmissiongradingtable', 'assign'),
674             $assign->get_course_module()->id
675         );
676         $this->assertEventLegacyLogData($expected, $event);
677         $this->assertEventContextNotUsed($event);
678     }
680     /**
681      * Test the submission_form_viewed event.
682      */
683     public function test_submission_form_viewed() {
684         global $PAGE;
686         $this->setUser($this->students[0]);
688         $assign = $this->create_instance();
690         // We need to set the URL in order to view the submission form.
691         $PAGE->set_url('/a_url');
693         // Trigger and capture the event.
694         $sink = $this->redirectEvents();
695         $assign->view('editsubmission');
696         $events = $sink->get_events();
697         $this->assertCount(1, $events);
698         $event = reset($events);
700         // Check that the event contains the expected values.
701         $this->assertInstanceOf('\mod_assign\event\submission_form_viewed', $event);
702         $this->assertEquals($assign->get_context(), $event->get_context());
703         $expected = array(
704             $assign->get_course()->id,
705             'assign',
706             'view submit assignment form',
707             'view.php?id=' . $assign->get_course_module()->id,
708             get_string('editsubmission', 'assign'),
709             $assign->get_course_module()->id
710         );
711         $this->assertEventLegacyLogData($expected, $event);
712         $this->assertEventContextNotUsed($event);
713     }
715     /**
716      * Test the submission_form_viewed event.
717      */
718     public function test_submission_confirmation_form_viewed() {
719         global $PAGE;
721         $this->setUser($this->students[0]);
723         $assign = $this->create_instance();
725         // We need to set the URL in order to view the submission form.
726         $PAGE->set_url('/a_url');
728         // Trigger and capture the event.
729         $sink = $this->redirectEvents();
730         $assign->view('submit');
731         $events = $sink->get_events();
732         $this->assertCount(1, $events);
733         $event = reset($events);
735         // Check that the event contains the expected values.
736         $this->assertInstanceOf('\mod_assign\event\submission_confirmation_form_viewed', $event);
737         $this->assertEquals($assign->get_context(), $event->get_context());
738         $expected = array(
739             $assign->get_course()->id,
740             'assign',
741             'view confirm submit assignment form',
742             'view.php?id=' . $assign->get_course_module()->id,
743             get_string('viewownsubmissionform', 'assign'),
744             $assign->get_course_module()->id
745         );
746         $this->assertEventLegacyLogData($expected, $event);
747         $this->assertEventContextNotUsed($event);
748     }
750     /**
751      * Test the reveal_identities_confirmation_page_viewed event.
752      */
753     public function test_reveal_identities_confirmation_page_viewed() {
754         global $PAGE;
756         // Set to the admin user so we have the permission to reveal identities.
757         $this->setAdminUser();
759         $assign = $this->create_instance();
761         // We need to set the URL in order to view the submission form.
762         $PAGE->set_url('/a_url');
764         // Trigger and capture the event.
765         $sink = $this->redirectEvents();
766         $assign->view('revealidentities');
767         $events = $sink->get_events();
768         $this->assertCount(1, $events);
769         $event = reset($events);
771         // Check that the event contains the expected values.
772         $this->assertInstanceOf('\mod_assign\event\reveal_identities_confirmation_page_viewed', $event);
773         $this->assertEquals($assign->get_context(), $event->get_context());
774         $expected = array(
775             $assign->get_course()->id,
776             'assign',
777             'view',
778             'view.php?id=' . $assign->get_course_module()->id,
779             get_string('viewrevealidentitiesconfirm', 'assign'),
780             $assign->get_course_module()->id
781         );
782         $this->assertEventLegacyLogData($expected, $event);
783         $this->assertEventContextNotUsed($event);
784     }
786     /**
787      * Test the statement_accepted event.
788      */
789     public function test_statement_accepted() {
790         // We want to be a student so we can submit assignments.
791         $this->setUser($this->students[0]);
793         // We do not want to send any messages to the student during the PHPUNIT test.
794         set_config('submissionreceipts', false, 'assign');
796         $assign = $this->create_instance();
798         // Create the data we want to pass to the submit_for_grading function.
799         $data = new stdClass();
800         $data->submissionstatement = 'We are the Borg. You will be assimilated. Resistance is futile. - do you agree
801             to these terms?';
803         // Trigger and capture the event.
804         $sink = $this->redirectEvents();
805         $assign->submit_for_grading($data, array());
806         $events = $sink->get_events();
807         $event = reset($events);
809         // Check that the event contains the expected values.
810         $this->assertInstanceOf('\mod_assign\event\statement_accepted', $event);
811         $this->assertEquals($assign->get_context(), $event->get_context());
812         $expected = array(
813             $assign->get_course()->id,
814             'assign',
815             'submission statement accepted',
816             'view.php?id=' . $assign->get_course_module()->id,
817             get_string('submissionstatementacceptedlog',
818                 'mod_assign',
819                 fullname($this->students[0])),
820             $assign->get_course_module()->id
821         );
822         $this->assertEventLegacyLogData($expected, $event);
823         $this->assertEventContextNotUsed($event);
825         // Enable the online text submission plugin.
826         $submissionplugins = $assign->get_submission_plugins();
827         foreach ($submissionplugins as $plugin) {
828             if ($plugin->get_type() === 'onlinetext') {
829                 $plugin->enable();
830                 break;
831             }
832         }
834         // Create the data we want to pass to the save_submission function.
835         $data = new stdClass();
836         $data->onlinetext_editor = array(
837             'text' => 'Online text',
838             'format' => FORMAT_HTML,
839             'itemid' => file_get_unused_draft_itemid()
840         );
841         $data->submissionstatement = 'We are the Borg. You will be assimilated. Resistance is futile. - do you agree
842             to these terms?';
844         // Trigger and capture the event.
845         $sink = $this->redirectEvents();
846         $assign->save_submission($data, $notices);
847         $events = $sink->get_events();
848         $event = $events[2];
850         // Check that the event contains the expected values.
851         $this->assertInstanceOf('\mod_assign\event\statement_accepted', $event);
852         $this->assertEquals($assign->get_context(), $event->get_context());
853         $this->assertEventLegacyLogData($expected, $event);
854         $this->assertEventContextNotUsed($event);
855     }
857     /**
858      * Test the batch_set_workflow_state_viewed event.
859      */
860     public function test_batch_set_workflow_state_viewed() {
861         $assign = $this->create_instance();
863         // Trigger and capture the event.
864         $sink = $this->redirectEvents();
865         $assign->testable_view_batch_set_workflow_state();
866         $events = $sink->get_events();
867         $event = reset($events);
869         // Check that the event contains the expected values.
870         $this->assertInstanceOf('\mod_assign\event\batch_set_workflow_state_viewed', $event);
871         $this->assertEquals($assign->get_context(), $event->get_context());
872         $expected = array(
873             $assign->get_course()->id,
874             'assign',
875             'view batch set marking workflow state',
876             'view.php?id=' . $assign->get_course_module()->id,
877             get_string('viewbatchsetmarkingworkflowstate', 'assign'),
878             $assign->get_course_module()->id
879         );
880         $this->assertEventLegacyLogData($expected, $event);
881         $this->assertEventContextNotUsed($event);
882     }
884     /**
885      * Test the batch_set_marker_allocation_viewed event.
886      */
887     public function test_batch_set_marker_allocation_viewed() {
888         $assign = $this->create_instance();
890         // Trigger and capture the event.
891         $sink = $this->redirectEvents();
892         $assign->testable_view_batch_markingallocation();
893         $events = $sink->get_events();
894         $event = reset($events);
896         // Check that the event contains the expected values.
897         $this->assertInstanceOf('\mod_assign\event\batch_set_marker_allocation_viewed', $event);
898         $this->assertEquals($assign->get_context(), $event->get_context());
899         $expected = array(
900             $assign->get_course()->id,
901             'assign',
902             'view batch set marker allocation',
903             'view.php?id=' . $assign->get_course_module()->id,
904             get_string('viewbatchmarkingallocation', 'assign'),
905             $assign->get_course_module()->id
906         );
907         $this->assertEventLegacyLogData($expected, $event);
908         $this->assertEventContextNotUsed($event);
909     }