7dfe83907d34260b051878770b309f4c78cf1fce
[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->editingteachers[0]->ignoresesskey = true;
476         $this->setUser($this->editingteachers[0]);
477         $assign = $this->create_instance();
479         // Test apply_grade_to_user.
480         $sink = $this->redirectEvents();
482         $data = new stdClass();
483         $data->grade = '50.0';
484         $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
485         $grade = $assign->get_user_grade($this->students[0]->id, false, 0);
487         $events = $sink->get_events();
488         $this->assertCount(1, $events);
489         $event = reset($events);
490         $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
491         $this->assertEquals($assign->get_context(), $event->get_context());
492         $this->assertEquals($grade->id, $event->objectid);
493         $this->assertEquals($this->students[0]->id, $event->relateduserid);
494         $expected = array(
495             $assign->get_course()->id,
496             'assign',
497             'grade submission',
498             'view.php?id=' . $assign->get_course_module()->id,
499             $assign->format_grade_for_log($grade),
500             $assign->get_course_module()->id
501         );
502         $this->assertEventLegacyLogData($expected, $event);
503         $sink->close();
505         // Test process_save_quick_grades.
506         $sink = $this->redirectEvents();
508         $data = array(
509             'grademodified_' . $this->students[0]->id => time(),
510             'quickgrade_' . $this->students[0]->id => '60.0'
511         );
512         $assign->testable_process_save_quick_grades($data);
513         $grade = $assign->get_user_grade($this->students[0]->id, false);
514         $this->assertEquals('60.0', $grade->grade);
516         $events = $sink->get_events();
517         $this->assertCount(1, $events);
518         $event = reset($events);
519         $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
520         $this->assertEquals($assign->get_context(), $event->get_context());
521         $this->assertEquals($grade->id, $event->objectid);
522         $this->assertEquals($this->students[0]->id, $event->relateduserid);
523         $expected = array(
524             $assign->get_course()->id,
525             'assign',
526             'grade submission',
527             'view.php?id=' . $assign->get_course_module()->id,
528             $assign->format_grade_for_log($grade),
529             $assign->get_course_module()->id
530         );
531         $this->assertEventLegacyLogData($expected, $event);
532         $sink->close();
534         // Test update_grade.
535         $sink = $this->redirectEvents();
536         $data = clone($grade);
537         $data->grade = '50.0';
538         $assign->update_grade($data);
539         $grade = $assign->get_user_grade($this->students[0]->id, false, 0);
540         $this->assertEquals('50.0', $grade->grade);
541         $events = $sink->get_events();
543         $this->assertCount(1, $events);
544         $event = reset($events);
545         $this->assertInstanceOf('\mod_assign\event\submission_graded', $event);
546         $this->assertEquals($assign->get_context(), $event->get_context());
547         $this->assertEquals($grade->id, $event->objectid);
548         $this->assertEquals($this->students[0]->id, $event->relateduserid);
549         $expected = array(
550             $assign->get_course()->id,
551             'assign',
552             'grade submission',
553             'view.php?id=' . $assign->get_course_module()->id,
554             $assign->format_grade_for_log($grade),
555             $assign->get_course_module()->id
556         );
557         $this->assertEventLegacyLogData($expected, $event);
558         $sink->close();
559         // Revert to defaults.
560         $this->editingteachers[0]->ignoresesskey = false;
561     }
563     /**
564      * Test the submission_viewed event.
565      */
566     public function test_submission_viewed() {
567         global $PAGE;
569         $this->setUser($this->editingteachers[0]);
571         $assign = $this->create_instance();
572         $submission = $assign->get_user_submission($this->students[0]->id, true);
574         // We need to set the URL in order to view the submission.
575         $PAGE->set_url('/a_url');
576         // A hack - these variables are used by the view_plugin_content function to
577         // determine what we actually want to view - would usually be set in URL.
578         global $_POST;
579         $_POST['plugin'] = 'comments';
580         $_POST['sid'] = $submission->id;
582         // Trigger and capture the event.
583         $sink = $this->redirectEvents();
584         $assign->view('viewpluginassignsubmission');
585         $events = $sink->get_events();
586         $this->assertCount(1, $events);
587         $event = reset($events);
589         // Check that the event contains the expected values.
590         $this->assertInstanceOf('\mod_assign\event\submission_viewed', $event);
591         $this->assertEquals($assign->get_context(), $event->get_context());
592         $this->assertEquals($submission->id, $event->objectid);
593         $expected = array(
594             $assign->get_course()->id,
595             'assign',
596             'view submission',
597             'view.php?id=' . $assign->get_course_module()->id,
598             get_string('viewsubmissionforuser', 'assign', $this->students[0]->id),
599             $assign->get_course_module()->id
600         );
601         $this->assertEventLegacyLogData($expected, $event);
602         $this->assertEventContextNotUsed($event);
603     }
605     /**
606      * Test the feedback_viewed event.
607      */
608     public function test_feedback_viewed() {
609         global $DB, $PAGE;
611         $this->setUser($this->editingteachers[0]);
613         $assign = $this->create_instance();
614         $submission = $assign->get_user_submission($this->students[0]->id, true);
616         // Insert a grade for this submission.
617         $grade = new stdClass();
618         $grade->assignment = $assign->get_instance()->id;
619         $grade->userid = $this->students[0]->id;
620         $gradeid = $DB->insert_record('assign_grades', $grade);
622         // We need to set the URL in order to view the feedback.
623         $PAGE->set_url('/a_url');
624         // A hack - these variables are used by the view_plugin_content function to
625         // determine what we actually want to view - would usually be set in URL.
626         global $_POST;
627         $_POST['plugin'] = 'comments';
628         $_POST['gid'] = $gradeid;
629         $_POST['sid'] = $submission->id;
631         // Trigger and capture the event.
632         $sink = $this->redirectEvents();
633         $assign->view('viewpluginassignfeedback');
634         $events = $sink->get_events();
635         $this->assertCount(1, $events);
636         $event = reset($events);
638         // Check that the event contains the expected values.
639         $this->assertInstanceOf('\mod_assign\event\feedback_viewed', $event);
640         $this->assertEquals($assign->get_context(), $event->get_context());
641         $this->assertEquals($gradeid, $event->objectid);
642         $expected = array(
643             $assign->get_course()->id,
644             'assign',
645             'view feedback',
646             'view.php?id=' . $assign->get_course_module()->id,
647             get_string('viewfeedbackforuser', 'assign', $this->students[0]->id),
648             $assign->get_course_module()->id
649         );
650         $this->assertEventLegacyLogData($expected, $event);
651         $this->assertEventContextNotUsed($event);
652     }
654     /**
655      * Test the grading_form_viewed event.
656      */
657     public function test_grading_form_viewed() {
658         global $PAGE;
660         $this->setUser($this->editingteachers[0]);
662         $assign = $this->create_instance();
664         // We need to set the URL in order to view the feedback.
665         $PAGE->set_url('/a_url');
666         // A hack - this variable is used by the view_single_grade_page function.
667         global $_POST;
668         $_POST['rownum'] = 1;
669         $_POST['userid'] = $this->students[0]->id;
671         // Trigger and capture the event.
672         $sink = $this->redirectEvents();
673         $assign->view('grade');
674         $events = $sink->get_events();
675         $this->assertCount(1, $events);
676         $event = reset($events);
678         // Check that the event contains the expected values.
679         $this->assertInstanceOf('\mod_assign\event\grading_form_viewed', $event);
680         $this->assertEquals($assign->get_context(), $event->get_context());
681         $expected = array(
682             $assign->get_course()->id,
683             'assign',
684             'view grading form',
685             'view.php?id=' . $assign->get_course_module()->id,
686             get_string('viewgradingformforstudent', 'assign', array('id' => $this->students[0]->id,
687                 'fullname' => fullname($this->students[0]))),
688             $assign->get_course_module()->id
689         );
690         $this->assertEventLegacyLogData($expected, $event);
691         $this->assertEventContextNotUsed($event);
692     }
694     /**
695      * Test the grading_table_viewed event.
696      */
697     public function test_grading_table_viewed() {
698         global $PAGE;
700         $this->setUser($this->editingteachers[0]);
702         $assign = $this->create_instance();
704         // We need to set the URL in order to view the feedback.
705         $PAGE->set_url('/a_url');
706         // A hack - this variable is used by the view_single_grade_page function.
707         global $_POST;
708         $_POST['rownum'] = 1;
709         $_POST['userid'] = $this->students[0]->id;
711         // Trigger and capture the event.
712         $sink = $this->redirectEvents();
713         $assign->view('grading');
714         $events = $sink->get_events();
715         $this->assertCount(1, $events);
716         $event = reset($events);
718         // Check that the event contains the expected values.
719         $this->assertInstanceOf('\mod_assign\event\grading_table_viewed', $event);
720         $this->assertEquals($assign->get_context(), $event->get_context());
721         $expected = array(
722             $assign->get_course()->id,
723             'assign',
724             'view submission grading table',
725             'view.php?id=' . $assign->get_course_module()->id,
726             get_string('viewsubmissiongradingtable', 'assign'),
727             $assign->get_course_module()->id
728         );
729         $this->assertEventLegacyLogData($expected, $event);
730         $this->assertEventContextNotUsed($event);
731     }
733     /**
734      * Test the submission_form_viewed event.
735      */
736     public function test_submission_form_viewed() {
737         global $PAGE;
739         $this->setUser($this->students[0]);
741         $assign = $this->create_instance();
743         // We need to set the URL in order to view the submission form.
744         $PAGE->set_url('/a_url');
746         // Trigger and capture the event.
747         $sink = $this->redirectEvents();
748         $assign->view('editsubmission');
749         $events = $sink->get_events();
750         $this->assertCount(1, $events);
751         $event = reset($events);
753         // Check that the event contains the expected values.
754         $this->assertInstanceOf('\mod_assign\event\submission_form_viewed', $event);
755         $this->assertEquals($assign->get_context(), $event->get_context());
756         $expected = array(
757             $assign->get_course()->id,
758             'assign',
759             'view submit assignment form',
760             'view.php?id=' . $assign->get_course_module()->id,
761             get_string('editsubmission', 'assign'),
762             $assign->get_course_module()->id
763         );
764         $this->assertEventLegacyLogData($expected, $event);
765         $this->assertEventContextNotUsed($event);
766     }
768     /**
769      * Test the submission_form_viewed event.
770      */
771     public function test_submission_confirmation_form_viewed() {
772         global $PAGE;
774         $this->setUser($this->students[0]);
776         $assign = $this->create_instance();
778         // We need to set the URL in order to view the submission form.
779         $PAGE->set_url('/a_url');
781         // Trigger and capture the event.
782         $sink = $this->redirectEvents();
783         $assign->view('submit');
784         $events = $sink->get_events();
785         $this->assertCount(1, $events);
786         $event = reset($events);
788         // Check that the event contains the expected values.
789         $this->assertInstanceOf('\mod_assign\event\submission_confirmation_form_viewed', $event);
790         $this->assertEquals($assign->get_context(), $event->get_context());
791         $expected = array(
792             $assign->get_course()->id,
793             'assign',
794             'view confirm submit assignment form',
795             'view.php?id=' . $assign->get_course_module()->id,
796             get_string('viewownsubmissionform', 'assign'),
797             $assign->get_course_module()->id
798         );
799         $this->assertEventLegacyLogData($expected, $event);
800         $this->assertEventContextNotUsed($event);
801     }
803     /**
804      * Test the reveal_identities_confirmation_page_viewed event.
805      */
806     public function test_reveal_identities_confirmation_page_viewed() {
807         global $PAGE;
809         // Set to the admin user so we have the permission to reveal identities.
810         $this->setAdminUser();
812         $assign = $this->create_instance();
814         // We need to set the URL in order to view the submission form.
815         $PAGE->set_url('/a_url');
817         // Trigger and capture the event.
818         $sink = $this->redirectEvents();
819         $assign->view('revealidentities');
820         $events = $sink->get_events();
821         $this->assertCount(1, $events);
822         $event = reset($events);
824         // Check that the event contains the expected values.
825         $this->assertInstanceOf('\mod_assign\event\reveal_identities_confirmation_page_viewed', $event);
826         $this->assertEquals($assign->get_context(), $event->get_context());
827         $expected = array(
828             $assign->get_course()->id,
829             'assign',
830             'view',
831             'view.php?id=' . $assign->get_course_module()->id,
832             get_string('viewrevealidentitiesconfirm', 'assign'),
833             $assign->get_course_module()->id
834         );
835         $this->assertEventLegacyLogData($expected, $event);
836         $this->assertEventContextNotUsed($event);
837     }
839     /**
840      * Test the statement_accepted event.
841      */
842     public function test_statement_accepted() {
843         // We want to be a student so we can submit assignments.
844         $this->setUser($this->students[0]);
846         // We do not want to send any messages to the student during the PHPUNIT test.
847         set_config('submissionreceipts', false, 'assign');
849         $assign = $this->create_instance();
851         // Create the data we want to pass to the submit_for_grading function.
852         $data = new stdClass();
853         $data->submissionstatement = 'We are the Borg. You will be assimilated. Resistance is futile. - do you agree
854             to these terms?';
856         // Trigger and capture the event.
857         $sink = $this->redirectEvents();
858         $assign->submit_for_grading($data, array());
859         $events = $sink->get_events();
860         $event = reset($events);
862         // Check that the event contains the expected values.
863         $this->assertInstanceOf('\mod_assign\event\statement_accepted', $event);
864         $this->assertEquals($assign->get_context(), $event->get_context());
865         $expected = array(
866             $assign->get_course()->id,
867             'assign',
868             'submission statement accepted',
869             'view.php?id=' . $assign->get_course_module()->id,
870             get_string('submissionstatementacceptedlog',
871                 'mod_assign',
872                 fullname($this->students[0])),
873             $assign->get_course_module()->id
874         );
875         $this->assertEventLegacyLogData($expected, $event);
876         $this->assertEventContextNotUsed($event);
878         // Enable the online text submission plugin.
879         $submissionplugins = $assign->get_submission_plugins();
880         foreach ($submissionplugins as $plugin) {
881             if ($plugin->get_type() === 'onlinetext') {
882                 $plugin->enable();
883                 break;
884             }
885         }
887         // Create the data we want to pass to the save_submission function.
888         $data = new stdClass();
889         $data->onlinetext_editor = array(
890             'text' => 'Online text',
891             'format' => FORMAT_HTML,
892             'itemid' => file_get_unused_draft_itemid()
893         );
894         $data->submissionstatement = 'We are the Borg. You will be assimilated. Resistance is futile. - do you agree
895             to these terms?';
897         // Trigger and capture the event.
898         $sink = $this->redirectEvents();
899         $assign->save_submission($data, $notices);
900         $events = $sink->get_events();
901         $event = $events[2];
903         // Check that the event contains the expected values.
904         $this->assertInstanceOf('\mod_assign\event\statement_accepted', $event);
905         $this->assertEquals($assign->get_context(), $event->get_context());
906         $this->assertEventLegacyLogData($expected, $event);
907         $this->assertEventContextNotUsed($event);
908     }
910     /**
911      * Test the batch_set_workflow_state_viewed event.
912      */
913     public function test_batch_set_workflow_state_viewed() {
914         $assign = $this->create_instance();
916         // Trigger and capture the event.
917         $sink = $this->redirectEvents();
918         $assign->testable_view_batch_set_workflow_state($this->students[0]->id);
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_workflow_state_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 marking workflow state',
929             'view.php?id=' . $assign->get_course_module()->id,
930             get_string('viewbatchsetmarkingworkflowstate', 'assign'),
931             $assign->get_course_module()->id
932         );
933         $this->assertEventLegacyLogData($expected, $event);
934         $this->assertEventContextNotUsed($event);
935     }
937     /**
938      * Test the batch_set_marker_allocation_viewed event.
939      */
940     public function test_batch_set_marker_allocation_viewed() {
941         $assign = $this->create_instance();
943         // Trigger and capture the event.
944         $sink = $this->redirectEvents();
945         $assign->testable_view_batch_markingallocation($this->students[0]->id);
946         $events = $sink->get_events();
947         $event = reset($events);
949         // Check that the event contains the expected values.
950         $this->assertInstanceOf('\mod_assign\event\batch_set_marker_allocation_viewed', $event);
951         $this->assertEquals($assign->get_context(), $event->get_context());
952         $expected = array(
953             $assign->get_course()->id,
954             'assign',
955             'view batch set marker allocation',
956             'view.php?id=' . $assign->get_course_module()->id,
957             get_string('viewbatchmarkingallocation', 'assign'),
958             $assign->get_course_module()->id
959         );
960         $this->assertEventLegacyLogData($expected, $event);
961         $this->assertEventContextNotUsed($event);
962     }