1 |
efrain |
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/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Contains the event tests for the plugin.
|
|
|
19 |
*
|
|
|
20 |
* @package assignsubmission_file
|
|
|
21 |
* @copyright 2013 Frédéric Massart
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace assignsubmission_file\event;
|
|
|
26 |
|
|
|
27 |
use mod_assign_test_generator;
|
|
|
28 |
|
|
|
29 |
defined('MOODLE_INTERNAL') || die();
|
|
|
30 |
|
|
|
31 |
global $CFG;
|
|
|
32 |
require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
|
|
|
33 |
|
|
|
34 |
class events_test extends \advanced_testcase {
|
|
|
35 |
|
|
|
36 |
// Use the generator helper.
|
|
|
37 |
use mod_assign_test_generator;
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Test that the assessable_uploaded event is fired when a file submission has been made.
|
|
|
41 |
*/
|
11 |
efrain |
42 |
public function test_assessable_uploaded(): void {
|
1 |
efrain |
43 |
$this->resetAfterTest();
|
|
|
44 |
|
|
|
45 |
$course = $this->getDataGenerator()->create_course();
|
|
|
46 |
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
47 |
$assign = $this->create_instance($course);
|
|
|
48 |
$context = $assign->get_context();
|
|
|
49 |
$cm = $assign->get_course_module();
|
|
|
50 |
|
|
|
51 |
$this->setUser($student->id);
|
|
|
52 |
$submission = $assign->get_user_submission($student->id, true);
|
|
|
53 |
|
|
|
54 |
$fs = get_file_storage();
|
|
|
55 |
$dummy = (object) array(
|
|
|
56 |
'contextid' => $context->id,
|
|
|
57 |
'component' => 'assignsubmission_file',
|
|
|
58 |
'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
|
|
|
59 |
'itemid' => $submission->id,
|
|
|
60 |
'filepath' => '/',
|
|
|
61 |
'filename' => 'myassignmnent.pdf'
|
|
|
62 |
);
|
|
|
63 |
$fi = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
|
|
|
64 |
$dummy = (object) array(
|
|
|
65 |
'contextid' => $context->id,
|
|
|
66 |
'component' => 'assignsubmission_file',
|
|
|
67 |
'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
|
|
|
68 |
'itemid' => $submission->id,
|
|
|
69 |
'filepath' => '/',
|
|
|
70 |
'filename' => 'myassignmnent.png'
|
|
|
71 |
);
|
|
|
72 |
$fi2 = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
|
|
|
73 |
$files = $fs->get_area_files($context->id, 'assignsubmission_file', ASSIGNSUBMISSION_FILE_FILEAREA,
|
|
|
74 |
$submission->id, 'id', false);
|
|
|
75 |
|
|
|
76 |
$data = new \stdClass();
|
|
|
77 |
$plugin = $assign->get_submission_plugin_by_type('file');
|
|
|
78 |
$sink = $this->redirectEvents();
|
|
|
79 |
$plugin->save($submission, $data);
|
|
|
80 |
$events = $sink->get_events();
|
|
|
81 |
|
|
|
82 |
$this->assertCount(2, $events);
|
|
|
83 |
$event = reset($events);
|
|
|
84 |
$this->assertInstanceOf('\assignsubmission_file\event\assessable_uploaded', $event);
|
|
|
85 |
$this->assertEquals($context->id, $event->contextid);
|
|
|
86 |
$this->assertEquals($submission->id, $event->objectid);
|
|
|
87 |
$this->assertCount(2, $event->other['pathnamehashes']);
|
|
|
88 |
$this->assertEquals($fi->get_pathnamehash(), $event->other['pathnamehashes'][0]);
|
|
|
89 |
$this->assertEquals($fi2->get_pathnamehash(), $event->other['pathnamehashes'][1]);
|
|
|
90 |
$expected = new \stdClass();
|
|
|
91 |
$expected->modulename = 'assign';
|
|
|
92 |
$expected->cmid = $cm->id;
|
|
|
93 |
$expected->itemid = $submission->id;
|
|
|
94 |
$expected->courseid = $course->id;
|
|
|
95 |
$expected->userid = $student->id;
|
|
|
96 |
$expected->file = $files;
|
|
|
97 |
$expected->files = $files;
|
|
|
98 |
$expected->pathnamehashes = array($fi->get_pathnamehash(), $fi2->get_pathnamehash());
|
|
|
99 |
$this->assertEventContextNotUsed($event);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Test that the submission_created event is fired when a file submission is saved.
|
|
|
104 |
*/
|
11 |
efrain |
105 |
public function test_submission_created(): void {
|
1 |
efrain |
106 |
$this->resetAfterTest();
|
|
|
107 |
|
|
|
108 |
$course = $this->getDataGenerator()->create_course();
|
|
|
109 |
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
110 |
$assign = $this->create_instance($course);
|
|
|
111 |
$context = $assign->get_context();
|
|
|
112 |
|
|
|
113 |
$this->setUser($student->id);
|
|
|
114 |
$submission = $assign->get_user_submission($student->id, true);
|
|
|
115 |
|
|
|
116 |
$fs = get_file_storage();
|
|
|
117 |
$dummy = (object) array(
|
|
|
118 |
'contextid' => $context->id,
|
|
|
119 |
'component' => 'assignsubmission_file',
|
|
|
120 |
'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
|
|
|
121 |
'itemid' => $submission->id,
|
|
|
122 |
'filepath' => '/',
|
|
|
123 |
'filename' => 'myassignmnent.pdf'
|
|
|
124 |
);
|
|
|
125 |
$fi = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
|
|
|
126 |
$dummy = (object) array(
|
|
|
127 |
'contextid' => $context->id,
|
|
|
128 |
'component' => 'assignsubmission_file',
|
|
|
129 |
'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
|
|
|
130 |
'itemid' => $submission->id,
|
|
|
131 |
'filepath' => '/',
|
|
|
132 |
'filename' => 'myassignmnent.png'
|
|
|
133 |
);
|
|
|
134 |
$fi2 = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
|
|
|
135 |
$files = $fs->get_area_files($context->id, 'assignsubmission_file', ASSIGNSUBMISSION_FILE_FILEAREA,
|
|
|
136 |
$submission->id, 'id', false);
|
|
|
137 |
|
|
|
138 |
$data = new \stdClass();
|
|
|
139 |
$plugin = $assign->get_submission_plugin_by_type('file');
|
|
|
140 |
$sink = $this->redirectEvents();
|
|
|
141 |
$plugin->save($submission, $data);
|
|
|
142 |
$events = $sink->get_events();
|
|
|
143 |
|
|
|
144 |
$this->assertCount(2, $events);
|
|
|
145 |
// We want to test the last event fired.
|
|
|
146 |
$event = $events[1];
|
|
|
147 |
$this->assertInstanceOf('\assignsubmission_file\event\submission_created', $event);
|
|
|
148 |
$this->assertEquals($context->id, $event->contextid);
|
|
|
149 |
$this->assertEquals($course->id, $event->courseid);
|
|
|
150 |
$this->assertEquals($submission->id, $event->other['submissionid']);
|
|
|
151 |
$this->assertEquals($submission->attemptnumber, $event->other['submissionattempt']);
|
|
|
152 |
$this->assertEquals($submission->status, $event->other['submissionstatus']);
|
|
|
153 |
$this->assertEquals($submission->userid, $event->relateduserid);
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
/**
|
|
|
157 |
* Test that the submission_updated event is fired when a file submission is saved when an existing submission already exists.
|
|
|
158 |
*/
|
11 |
efrain |
159 |
public function test_submission_updated(): void {
|
1 |
efrain |
160 |
$this->resetAfterTest();
|
|
|
161 |
|
|
|
162 |
$course = $this->getDataGenerator()->create_course();
|
|
|
163 |
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
164 |
$assign = $this->create_instance($course);
|
|
|
165 |
$context = $assign->get_context();
|
|
|
166 |
|
|
|
167 |
$this->setUser($student->id);
|
|
|
168 |
$submission = $assign->get_user_submission($student->id, true);
|
|
|
169 |
|
|
|
170 |
$fs = get_file_storage();
|
|
|
171 |
$dummy = (object) array(
|
|
|
172 |
'contextid' => $context->id,
|
|
|
173 |
'component' => 'assignsubmission_file',
|
|
|
174 |
'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
|
|
|
175 |
'itemid' => $submission->id,
|
|
|
176 |
'filepath' => '/',
|
|
|
177 |
'filename' => 'myassignmnent.pdf'
|
|
|
178 |
);
|
|
|
179 |
$fi = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
|
|
|
180 |
$dummy = (object) array(
|
|
|
181 |
'contextid' => $context->id,
|
|
|
182 |
'component' => 'assignsubmission_file',
|
|
|
183 |
'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
|
|
|
184 |
'itemid' => $submission->id,
|
|
|
185 |
'filepath' => '/',
|
|
|
186 |
'filename' => 'myassignmnent.png'
|
|
|
187 |
);
|
|
|
188 |
$fi2 = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
|
|
|
189 |
$files = $fs->get_area_files($context->id, 'assignsubmission_file', ASSIGNSUBMISSION_FILE_FILEAREA,
|
|
|
190 |
$submission->id, 'id', false);
|
|
|
191 |
|
|
|
192 |
$data = new \stdClass();
|
|
|
193 |
$plugin = $assign->get_submission_plugin_by_type('file');
|
|
|
194 |
$sink = $this->redirectEvents();
|
|
|
195 |
// Create a submission.
|
|
|
196 |
$plugin->save($submission, $data);
|
|
|
197 |
// Update a submission.
|
|
|
198 |
$plugin->save($submission, $data);
|
|
|
199 |
$events = $sink->get_events();
|
|
|
200 |
|
|
|
201 |
$this->assertCount(4, $events);
|
|
|
202 |
// We want to test the last event fired.
|
|
|
203 |
$event = $events[3];
|
|
|
204 |
$this->assertInstanceOf('\assignsubmission_file\event\submission_updated', $event);
|
|
|
205 |
$this->assertEquals($context->id, $event->contextid);
|
|
|
206 |
$this->assertEquals($course->id, $event->courseid);
|
|
|
207 |
$this->assertEquals($submission->id, $event->other['submissionid']);
|
|
|
208 |
$this->assertEquals($submission->attemptnumber, $event->other['submissionattempt']);
|
|
|
209 |
$this->assertEquals($submission->status, $event->other['submissionstatus']);
|
|
|
210 |
$this->assertEquals($submission->userid, $event->relateduserid);
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
}
|