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 |
namespace mod_assign;
|
|
|
18 |
|
|
|
19 |
use mod_assign_testable_assign;
|
|
|
20 |
|
|
|
21 |
defined('MOODLE_INTERNAL') || die();
|
|
|
22 |
|
|
|
23 |
global $CFG;
|
|
|
24 |
require_once($CFG->dirroot . '/mod/assign/locallib.php');
|
|
|
25 |
require_once($CFG->dirroot . '/mod/assign/tests/fixtures/testable_assign.php');
|
|
|
26 |
require_once($CFG->dirroot . '/group/lib.php');
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Class mod_assign_portfolio_caller_testcase
|
|
|
30 |
*
|
|
|
31 |
* Tests behaviour of the assign_portfolio_caller class.
|
|
|
32 |
*
|
|
|
33 |
* @package mod_assign
|
|
|
34 |
* @category test
|
|
|
35 |
* @copyright Brendan Cox <brendan.cox@totaralearning.com>
|
|
|
36 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
37 |
*/
|
|
|
38 |
class portfolio_caller_test extends \advanced_testcase {
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Test an assignment file is loaded for a user who submitted it.
|
|
|
42 |
*/
|
|
|
43 |
public function test_user_submission_file_is_loaded() {
|
|
|
44 |
$this->resetAfterTest(true);
|
|
|
45 |
|
|
|
46 |
$user = $this->getDataGenerator()->create_user();
|
|
|
47 |
$course = $this->getDataGenerator()->create_course();
|
|
|
48 |
|
|
|
49 |
/* @var mod_assign_generator $assigngenerator */
|
|
|
50 |
$assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
|
|
|
51 |
|
|
|
52 |
$activityrecord = $assigngenerator->create_instance(array('course' => $course->id));
|
|
|
53 |
$cm = get_coursemodule_from_instance('assign', $activityrecord->id);
|
|
|
54 |
$context = \context_module::instance($cm->id);
|
|
|
55 |
$assign = new mod_assign_testable_assign($context, $cm, $course);
|
|
|
56 |
|
|
|
57 |
$submission = $assign->get_user_submission($user->id, true);
|
|
|
58 |
|
|
|
59 |
$fs = get_file_storage();
|
|
|
60 |
$dummy = (object) array(
|
|
|
61 |
'contextid' => $context->id,
|
|
|
62 |
'component' => 'assignsubmission_file',
|
|
|
63 |
'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
|
|
|
64 |
'itemid' => $submission->id,
|
|
|
65 |
'filepath' => '/',
|
|
|
66 |
'filename' => 'myassignmnent.pdf'
|
|
|
67 |
);
|
|
|
68 |
$file = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
|
|
|
69 |
|
|
|
70 |
$caller = new \assign_portfolio_caller(array('cmid' => $cm->id, 'fileid' => $file->get_id()));
|
|
|
71 |
$caller->set('user', $user);
|
|
|
72 |
$caller->load_data();
|
|
|
73 |
$this->assertEquals($file->get_contenthash(), $caller->get_sha1_file());
|
|
|
74 |
|
|
|
75 |
// This processes the file either by fileid or by other fields in the file table.
|
|
|
76 |
// We should get the same outcome with either approach.
|
|
|
77 |
$caller = new \assign_portfolio_caller(
|
|
|
78 |
array(
|
|
|
79 |
'cmid' => $cm->id,
|
|
|
80 |
'sid' => $submission->id,
|
|
|
81 |
'area' => ASSIGNSUBMISSION_FILE_FILEAREA,
|
|
|
82 |
'component' => 'assignsubmission_file',
|
|
|
83 |
)
|
|
|
84 |
);
|
|
|
85 |
$caller->set('user', $user);
|
|
|
86 |
$caller->load_data();
|
|
|
87 |
$this->assertEquals($file->get_contenthash(), $caller->get_sha1_file());
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Test an assignment file is not loaded for a user that did not submit it.
|
|
|
92 |
*/
|
|
|
93 |
public function test_different_user_submission_file_is_not_loaded() {
|
|
|
94 |
$this->resetAfterTest(true);
|
|
|
95 |
|
|
|
96 |
$user = $this->getDataGenerator()->create_user();
|
|
|
97 |
$course = $this->getDataGenerator()->create_course();
|
|
|
98 |
|
|
|
99 |
/* @var mod_assign_generator $assigngenerator */
|
|
|
100 |
$assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
|
|
|
101 |
|
|
|
102 |
$activityrecord = $assigngenerator->create_instance(array('course' => $course->id));
|
|
|
103 |
$cm = get_coursemodule_from_instance('assign', $activityrecord->id);
|
|
|
104 |
$context = \context_module::instance($cm->id);
|
|
|
105 |
$assign = new mod_assign_testable_assign($context, $cm, $course);
|
|
|
106 |
|
|
|
107 |
$submission = $assign->get_user_submission($user->id, true);
|
|
|
108 |
|
|
|
109 |
$fs = get_file_storage();
|
|
|
110 |
$dummy = (object) array(
|
|
|
111 |
'contextid' => $context->id,
|
|
|
112 |
'component' => 'assignsubmission_file',
|
|
|
113 |
'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
|
|
|
114 |
'itemid' => $submission->id,
|
|
|
115 |
'filepath' => '/',
|
|
|
116 |
'filename' => 'myassignmnent.pdf'
|
|
|
117 |
);
|
|
|
118 |
$file = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
|
|
|
119 |
|
|
|
120 |
// Now add second user.
|
|
|
121 |
$wronguser = $this->getDataGenerator()->create_user();
|
|
|
122 |
|
|
|
123 |
$caller = new \assign_portfolio_caller(array('cmid' => $cm->id, 'fileid' => $file->get_id()));
|
|
|
124 |
$caller->set('user', $wronguser);
|
|
|
125 |
|
|
|
126 |
$this->expectException(\portfolio_caller_exception::class);
|
|
|
127 |
$this->expectExceptionMessage('Sorry, the requested file could not be found');
|
|
|
128 |
|
|
|
129 |
$caller->load_data();
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Test an assignment file is loaded for a user who is part of a group that submitted it.
|
|
|
134 |
*/
|
|
|
135 |
public function test_group_submission_file_is_loaded() {
|
|
|
136 |
$this->resetAfterTest(true);
|
|
|
137 |
|
|
|
138 |
$user = $this->getDataGenerator()->create_user();
|
|
|
139 |
$course = $this->getDataGenerator()->create_course();
|
|
|
140 |
|
|
|
141 |
$groupdata = new \stdClass();
|
|
|
142 |
$groupdata->courseid = $course->id;
|
|
|
143 |
$groupdata->name = 'group1';
|
|
|
144 |
$groupid = groups_create_group($groupdata);
|
|
|
145 |
$this->getDataGenerator()->enrol_user($user->id, $course->id);
|
|
|
146 |
groups_add_member($groupid, $user);
|
|
|
147 |
|
|
|
148 |
/* @var mod_assign_generator $assigngenerator */
|
|
|
149 |
$assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
|
|
|
150 |
|
|
|
151 |
$activityrecord = $assigngenerator->create_instance(array('course' => $course->id));
|
|
|
152 |
$cm = get_coursemodule_from_instance('assign', $activityrecord->id);
|
|
|
153 |
$context = \context_module::instance($cm->id);
|
|
|
154 |
$assign = new mod_assign_testable_assign($context, $cm, $course);
|
|
|
155 |
|
|
|
156 |
$submission = $assign->get_group_submission($user->id, $groupid, true);
|
|
|
157 |
|
|
|
158 |
$fs = get_file_storage();
|
|
|
159 |
$dummy = (object) array(
|
|
|
160 |
'contextid' => $context->id,
|
|
|
161 |
'component' => 'assignsubmission_file',
|
|
|
162 |
'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
|
|
|
163 |
'itemid' => $submission->id,
|
|
|
164 |
'filepath' => '/',
|
|
|
165 |
'filename' => 'myassignmnent.pdf'
|
|
|
166 |
);
|
|
|
167 |
$file = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
|
|
|
168 |
|
|
|
169 |
$caller = new \assign_portfolio_caller(array('cmid' => $cm->id, 'fileid' => $file->get_id()));
|
|
|
170 |
$caller->set('user', $user);
|
|
|
171 |
$caller->load_data();
|
|
|
172 |
$this->assertEquals($file->get_contenthash(), $caller->get_sha1_file());
|
|
|
173 |
|
|
|
174 |
// This processes the file either by fileid or by other fields in the file table.
|
|
|
175 |
// We should get the same outcome with either approach.
|
|
|
176 |
$caller = new \assign_portfolio_caller(
|
|
|
177 |
array(
|
|
|
178 |
'cmid' => $cm->id,
|
|
|
179 |
'sid' => $submission->id,
|
|
|
180 |
'area' => ASSIGNSUBMISSION_FILE_FILEAREA,
|
|
|
181 |
'component' => 'assignsubmission_file',
|
|
|
182 |
)
|
|
|
183 |
);
|
|
|
184 |
$caller->set('user', $user);
|
|
|
185 |
$caller->load_data();
|
|
|
186 |
$this->assertEquals($file->get_contenthash(), $caller->get_sha1_file());
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
/**
|
|
|
190 |
* Test an assignment file is not loaded for a user who is not part of a group that submitted it.
|
|
|
191 |
*/
|
|
|
192 |
public function test_different_group_submission_file_is_not_loaded() {
|
|
|
193 |
$this->resetAfterTest(true);
|
|
|
194 |
|
|
|
195 |
$user = $this->getDataGenerator()->create_user();
|
|
|
196 |
$course = $this->getDataGenerator()->create_course();
|
|
|
197 |
|
|
|
198 |
$groupdata = new \stdClass();
|
|
|
199 |
$groupdata->courseid = $course->id;
|
|
|
200 |
$groupdata->name = 'group1';
|
|
|
201 |
$groupid = groups_create_group($groupdata);
|
|
|
202 |
$this->getDataGenerator()->enrol_user($user->id, $course->id);
|
|
|
203 |
groups_add_member($groupid, $user);
|
|
|
204 |
|
|
|
205 |
/* @var mod_assign_generator $assigngenerator */
|
|
|
206 |
$assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
|
|
|
207 |
|
|
|
208 |
$activityrecord = $assigngenerator->create_instance(array('course' => $course->id));
|
|
|
209 |
$cm = get_coursemodule_from_instance('assign', $activityrecord->id);
|
|
|
210 |
$context = \context_module::instance($cm->id);
|
|
|
211 |
$assign = new mod_assign_testable_assign($context, $cm, $course);
|
|
|
212 |
|
|
|
213 |
$submission = $assign->get_group_submission($user->id, $groupid,true);
|
|
|
214 |
|
|
|
215 |
$fs = get_file_storage();
|
|
|
216 |
$dummy = (object) array(
|
|
|
217 |
'contextid' => $context->id,
|
|
|
218 |
'component' => 'assignsubmission_file',
|
|
|
219 |
'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
|
|
|
220 |
'itemid' => $submission->id,
|
|
|
221 |
'filepath' => '/',
|
|
|
222 |
'filename' => 'myassignmnent.pdf'
|
|
|
223 |
);
|
|
|
224 |
$file = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
|
|
|
225 |
|
|
|
226 |
// Now add second user.
|
|
|
227 |
$wronguser = $this->getDataGenerator()->create_user();
|
|
|
228 |
|
|
|
229 |
// Create a new group for the wrong user.
|
|
|
230 |
$groupdata = new \stdClass();
|
|
|
231 |
$groupdata->courseid = $course->id;
|
|
|
232 |
$groupdata->name = 'group2';
|
|
|
233 |
$groupid = groups_create_group($groupdata);
|
|
|
234 |
$this->getDataGenerator()->enrol_user($wronguser->id, $course->id);
|
|
|
235 |
groups_add_member($groupid, $wronguser);
|
|
|
236 |
|
|
|
237 |
// In the negative test for the user, we loaded the caller via fileid. Switching to the other approach this time.
|
|
|
238 |
$caller = new \assign_portfolio_caller(
|
|
|
239 |
array(
|
|
|
240 |
'cmid' => $cm->id,
|
|
|
241 |
'sid' => $submission->id,
|
|
|
242 |
'area' => ASSIGNSUBMISSION_FILE_FILEAREA,
|
|
|
243 |
'component' => 'assignsubmission_file',
|
|
|
244 |
)
|
|
|
245 |
);
|
|
|
246 |
$caller->set('user', $wronguser);
|
|
|
247 |
|
|
|
248 |
$this->expectException(\portfolio_caller_exception::class);
|
|
|
249 |
$this->expectExceptionMessage('Sorry, the requested file could not be found');
|
|
|
250 |
|
|
|
251 |
$caller->load_data();
|
|
|
252 |
}
|
|
|
253 |
}
|