MDL-41101 mod_assign: replaced 'grade submission' add_to_log call with an event
[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(2, $events);
295         $event = $events[1];
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();
509         // Test update_grade.
510         $sink = $this->redirectEvents();
511         $data = clone($grade);
512         $data->grade = '50.0';
513         $assign->update_grade($data);
514         $grade = $assign->get_user_grade($this->students[0]->id, false, 0);
515         $this->assertEquals('50.0', $grade->grade);
516         $events = $sink->get_events();
518         $this->assertCount(1, $events);
519         $event = reset($events);
520         $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
521         $this->assertEquals($assign->get_context(), $event->get_context());
522         $this->assertEquals($grade->id, $event->objectid);
523         $this->assertEquals($this->students[0]->id, $event->relateduserid);
524         $expected = array(
525             $assign->get_course()->id,
526             'assign',
527             'grade submission',
528             'view.php?id=' . $assign->get_course_module()->id,
529             $assign->format_grade_for_log($grade),
530             $assign->get_course_module()->id
531         );
532         $this->assertEventLegacyLogData($expected, $event);
533         $sink->close();
534     }
536     /**
537      * Test the submission_viewed event.
538      */
539     public function test_submission_viewed() {
540         global $PAGE;
542         $this->setUser($this->editingteachers[0]);
544         $assign = $this->create_instance();
545         $submission = $assign->get_user_submission($this->students[0]->id, true);
547         // We need to set the URL in order to view the submission.
548         $PAGE->set_url('/a_url');
549         // A hack - these variables are used by the view_plugin_content function to
550         // determine what we actually want to view - would usually be set in URL.
551         global $_POST;
552         $_POST['plugin'] = 'comments';
553         $_POST['sid'] = $submission->id;
555         // Trigger and capture the event.
556         $sink = $this->redirectEvents();
557         $assign->view('viewpluginassignsubmission');
558         $events = $sink->get_events();
559         $this->assertCount(1, $events);
560         $event = reset($events);
562         // Check that the event contains the expected values.
563         $this->assertInstanceOf('\mod_assign\event\submission_viewed', $event);
564         $this->assertEquals($assign->get_context(), $event->get_context());
565         $this->assertEquals($submission->id, $event->objectid);
566         $expected = array(
567             $assign->get_course()->id,
568             'assign',
569             'view submission',
570             'view.php?id=' . $assign->get_course_module()->id,
571             get_string('viewsubmissionforuser', 'assign', $this->students[0]->id),
572             $assign->get_course_module()->id
573         );
574         $this->assertEventLegacyLogData($expected, $event);
575         $this->assertEventContextNotUsed($event);
576     }
578     /**
579      * Test the feedback_viewed event.
580      */
581     public function test_feedback_viewed() {
582         global $DB, $PAGE;
584         $this->setUser($this->editingteachers[0]);
586         $assign = $this->create_instance();
587         $submission = $assign->get_user_submission($this->students[0]->id, true);
589         // Insert a grade for this submission.
590         $grade = new stdClass();
591         $grade->assignment = 1;
592         $grade->userid = $this->students[0]->id;
593         $gradeid = $DB->insert_record('assign_grades', $grade);
595         // We need to set the URL in order to view the feedback.
596         $PAGE->set_url('/a_url');
597         // A hack - these variables are used by the view_plugin_content function to
598         // determine what we actually want to view - would usually be set in URL.
599         global $_POST;
600         $_POST['plugin'] = 'comments';
601         $_POST['gid'] = $gradeid;
602         $_POST['sid'] = $submission->id;
604         // Trigger and capture the event.
605         $sink = $this->redirectEvents();
606         $assign->view('viewpluginassignfeedback');
607         $events = $sink->get_events();
608         $this->assertCount(1, $events);
609         $event = reset($events);
611         // Check that the event contains the expected values.
612         $this->assertInstanceOf('\mod_assign\event\feedback_viewed', $event);
613         $this->assertEquals($assign->get_context(), $event->get_context());
614         $this->assertEquals($gradeid, $event->objectid);
615         $expected = array(
616             $assign->get_course()->id,
617             'assign',
618             'view feedback',
619             'view.php?id=' . $assign->get_course_module()->id,
620             get_string('viewfeedbackforuser', 'assign', $this->students[0]->id),
621             $assign->get_course_module()->id
622         );
623         $this->assertEventLegacyLogData($expected, $event);
624         $this->assertEventContextNotUsed($event);
625     }
627     /**
628      * Test the grading_form_viewed event.
629      */
630     public function test_grading_form_viewed() {
631         global $PAGE;
633         $this->setUser($this->editingteachers[0]);
635         $assign = $this->create_instance();
637         // We need to set the URL in order to view the feedback.
638         $PAGE->set_url('/a_url');
639         // A hack - this variable is used by the view_single_grade_page function.
640         global $_POST;
641         $_POST['rownum'] = 1;
642         $_POST['userid'] = $this->students[0]->id;
644         // Trigger and capture the event.
645         $sink = $this->redirectEvents();
646         $assign->view('grade');
647         $events = $sink->get_events();
648         $this->assertCount(1, $events);
649         $event = reset($events);
651         // Check that the event contains the expected values.
652         $this->assertInstanceOf('\mod_assign\event\grading_form_viewed', $event);
653         $this->assertEquals($assign->get_context(), $event->get_context());
654         $expected = array(
655             $assign->get_course()->id,
656             'assign',
657             'view grading form',
658             'view.php?id=' . $assign->get_course_module()->id,
659             get_string('viewgradingformforstudent', 'assign', array('id' => $this->students[0]->id,
660                 'fullname' => fullname($this->students[0]))),
661             $assign->get_course_module()->id
662         );
663         $this->assertEventLegacyLogData($expected, $event);
664         $this->assertEventContextNotUsed($event);
665     }
667     /**
668      * Test the grading_table_viewed event.
669      */
670     public function test_grading_table_viewed() {
671         global $PAGE;
673         $this->setUser($this->editingteachers[0]);
675         $assign = $this->create_instance();
677         // We need to set the URL in order to view the feedback.
678         $PAGE->set_url('/a_url');
679         // A hack - this variable is used by the view_single_grade_page function.
680         global $_POST;
681         $_POST['rownum'] = 1;
682         $_POST['userid'] = $this->students[0]->id;
684         // Trigger and capture the event.
685         $sink = $this->redirectEvents();
686         $assign->view('grading');
687         $events = $sink->get_events();
688         $this->assertCount(1, $events);
689         $event = reset($events);
691         // Check that the event contains the expected values.
692         $this->assertInstanceOf('\mod_assign\event\grading_table_viewed', $event);
693         $this->assertEquals($assign->get_context(), $event->get_context());
694         $expected = array(
695             $assign->get_course()->id,
696             'assign',
697             'view submission grading table',
698             'view.php?id=' . $assign->get_course_module()->id,
699             get_string('viewsubmissiongradingtable', 'assign'),
700             $assign->get_course_module()->id
701         );
702         $this->assertEventLegacyLogData($expected, $event);
703         $this->assertEventContextNotUsed($event);
704     }
706     /**
707      * Test the submission_form_viewed event.
708      */
709     public function test_submission_form_viewed() {
710         global $PAGE;
712         $this->setUser($this->students[0]);
714         $assign = $this->create_instance();
716         // We need to set the URL in order to view the submission form.
717         $PAGE->set_url('/a_url');
719         // Trigger and capture the event.
720         $sink = $this->redirectEvents();
721         $assign->view('editsubmission');
722         $events = $sink->get_events();
723         $this->assertCount(1, $events);
724         $event = reset($events);
726         // Check that the event contains the expected values.
727         $this->assertInstanceOf('\mod_assign\event\submission_form_viewed', $event);
728         $this->assertEquals($assign->get_context(), $event->get_context());
729         $expected = array(
730             $assign->get_course()->id,
731             'assign',
732             'view submit assignment form',
733             'view.php?id=' . $assign->get_course_module()->id,
734             get_string('editsubmission', 'assign'),
735             $assign->get_course_module()->id
736         );
737         $this->assertEventLegacyLogData($expected, $event);
738         $this->assertEventContextNotUsed($event);
739     }
741     /**
742      * Test the submission_form_viewed event.
743      */
744     public function test_submission_confirmation_form_viewed() {
745         global $PAGE;
747         $this->setUser($this->students[0]);
749         $assign = $this->create_instance();
751         // We need to set the URL in order to view the submission form.
752         $PAGE->set_url('/a_url');
754         // Trigger and capture the event.
755         $sink = $this->redirectEvents();
756         $assign->view('submit');
757         $events = $sink->get_events();
758         $this->assertCount(1, $events);
759         $event = reset($events);
761         // Check that the event contains the expected values.
762         $this->assertInstanceOf('\mod_assign\event\submission_confirmation_form_viewed', $event);
763         $this->assertEquals($assign->get_context(), $event->get_context());
764         $expected = array(
765             $assign->get_course()->id,
766             'assign',
767             'view confirm submit assignment form',
768             'view.php?id=' . $assign->get_course_module()->id,
769             get_string('viewownsubmissionform', 'assign'),
770             $assign->get_course_module()->id
771         );
772         $this->assertEventLegacyLogData($expected, $event);
773         $this->assertEventContextNotUsed($event);
774     }
776     /**
777      * Test the reveal_identities_confirmation_page_viewed event.
778      */
779     public function test_reveal_identities_confirmation_page_viewed() {
780         global $PAGE;
782         // Set to the admin user so we have the permission to reveal identities.
783         $this->setAdminUser();
785         $assign = $this->create_instance();
787         // We need to set the URL in order to view the submission form.
788         $PAGE->set_url('/a_url');
790         // Trigger and capture the event.
791         $sink = $this->redirectEvents();
792         $assign->view('revealidentities');
793         $events = $sink->get_events();
794         $this->assertCount(1, $events);
795         $event = reset($events);
797         // Check that the event contains the expected values.
798         $this->assertInstanceOf('\mod_assign\event\reveal_identities_confirmation_page_viewed', $event);
799         $this->assertEquals($assign->get_context(), $event->get_context());
800         $expected = array(
801             $assign->get_course()->id,
802             'assign',
803             'view',
804             'view.php?id=' . $assign->get_course_module()->id,
805             get_string('viewrevealidentitiesconfirm', 'assign'),
806             $assign->get_course_module()->id
807         );
808         $this->assertEventLegacyLogData($expected, $event);
809         $this->assertEventContextNotUsed($event);
810     }
812     /**
813      * Test the statement_accepted event.
814      */
815     public function test_statement_accepted() {
816         // We want to be a student so we can submit assignments.
817         $this->setUser($this->students[0]);
819         // We do not want to send any messages to the student during the PHPUNIT test.
820         set_config('submissionreceipts', false, 'assign');
822         $assign = $this->create_instance();
824         // Create the data we want to pass to the submit_for_grading function.
825         $data = new stdClass();
826         $data->submissionstatement = 'We are the Borg. You will be assimilated. Resistance is futile. - do you agree
827             to these terms?';
829         // Trigger and capture the event.
830         $sink = $this->redirectEvents();
831         $assign->submit_for_grading($data, array());
832         $events = $sink->get_events();
833         $event = reset($events);
835         // Check that the event contains the expected values.
836         $this->assertInstanceOf('\mod_assign\event\statement_accepted', $event);
837         $this->assertEquals($assign->get_context(), $event->get_context());
838         $expected = array(
839             $assign->get_course()->id,
840             'assign',
841             'submission statement accepted',
842             'view.php?id=' . $assign->get_course_module()->id,
843             get_string('submissionstatementacceptedlog',
844                 'mod_assign',
845                 fullname($this->students[0])),
846             $assign->get_course_module()->id
847         );
848         $this->assertEventLegacyLogData($expected, $event);
849         $this->assertEventContextNotUsed($event);
851         // Enable the online text submission plugin.
852         $submissionplugins = $assign->get_submission_plugins();
853         foreach ($submissionplugins as $plugin) {
854             if ($plugin->get_type() === 'onlinetext') {
855                 $plugin->enable();
856                 break;
857             }
858         }
860         // Create the data we want to pass to the save_submission function.
861         $data = new stdClass();
862         $data->onlinetext_editor = array(
863             'text' => 'Online text',
864             'format' => FORMAT_HTML,
865             'itemid' => file_get_unused_draft_itemid()
866         );
867         $data->submissionstatement = 'We are the Borg. You will be assimilated. Resistance is futile. - do you agree
868             to these terms?';
870         // Trigger and capture the event.
871         $sink = $this->redirectEvents();
872         $assign->save_submission($data, $notices);
873         $events = $sink->get_events();
874         $event = $events[2];
876         // Check that the event contains the expected values.
877         $this->assertInstanceOf('\mod_assign\event\statement_accepted', $event);
878         $this->assertEquals($assign->get_context(), $event->get_context());
879         $this->assertEventLegacyLogData($expected, $event);
880         $this->assertEventContextNotUsed($event);
881     }
883     /**
884      * Test the batch_set_workflow_state_viewed event.
885      */
886     public function test_batch_set_workflow_state_viewed() {
887         $assign = $this->create_instance();
889         // Trigger and capture the event.
890         $sink = $this->redirectEvents();
891         $assign->testable_view_batch_set_workflow_state();
892         $events = $sink->get_events();
893         $event = reset($events);
895         // Check that the event contains the expected values.
896         $this->assertInstanceOf('\mod_assign\event\batch_set_workflow_state_viewed', $event);
897         $this->assertEquals($assign->get_context(), $event->get_context());
898         $expected = array(
899             $assign->get_course()->id,
900             'assign',
901             'view batch set marking workflow state',
902             'view.php?id=' . $assign->get_course_module()->id,
903             get_string('viewbatchsetmarkingworkflowstate', 'assign'),
904             $assign->get_course_module()->id
905         );
906         $this->assertEventLegacyLogData($expected, $event);
907         $this->assertEventContextNotUsed($event);
908     }
910     /**
911      * Test the batch_set_marker_allocation_viewed event.
912      */
913     public function test_batch_set_marker_allocation_viewed() {
914         $assign = $this->create_instance();
916         // Trigger and capture the event.
917         $sink = $this->redirectEvents();
918         $assign->testable_view_batch_markingallocation();
919         $events = $sink->get_events();
920         $event = reset($events);
922         // Check that the event contains the expected values.
923         $this->assertInstanceOf('\mod_assign\event\batch_set_marker_allocation_viewed', $event);
924         $this->assertEquals($assign->get_context(), $event->get_context());
925         $expected = array(
926             $assign->get_course()->id,
927             'assign',
928             'view batch set marker allocation',
929             'view.php?id=' . $assign->get_course_module()->id,
930             get_string('viewbatchmarkingallocation', 'assign'),
931             $assign->get_course_module()->id
932         );
933         $this->assertEventLegacyLogData($expected, $event);
934         $this->assertEventContextNotUsed($event);
935     }