MDL-37028 Fixed bug, orphaned modules are moved to the 0-section
[moodle.git] / mod / assign / tests / externallib_test.php
CommitLineData
1f8c8f61
PC
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
17defined('MOODLE_INTERNAL') || die();
18
19global $CFG;
20
21require_once($CFG->dirroot . '/webservice/tests/helpers.php');
22
23/**
24 * External mod assign functions unit tests
25 *
26 * @package mod_assign
27 * @category external
28 * @copyright 2012 Paul Charsley
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 */
31class mod_assign_external_testcase extends externallib_advanced_testcase {
32
33 /**
34 * Tests set up
35 */
36 protected function setUp() {
37 global $CFG;
38 require_once($CFG->dirroot . '/mod/assign/externallib.php');
39 }
40
41 /**
42 * Test get_grades
43 */
44 public function test_get_grades () {
45 global $DB, $USER;
46
47 $this->resetAfterTest(true);
48 // Create a course and assignment.
49 $coursedata['idnumber'] = 'idnumbercourse';
50 $coursedata['fullname'] = 'Lightwork Course';
51 $coursedata['summary'] = 'Lightwork Course description';
52 $coursedata['summaryformat'] = FORMAT_MOODLE;
53 $course = self::getDataGenerator()->create_course($coursedata);
54
55 $assigndata['course'] = $course->id;
56 $assigndata['name'] = 'lightwork assignment';
57
58 $assign = self::getDataGenerator()->create_module('assign', $assigndata);
59
60 // Create a manual enrolment record.
07df8c38
PC
61 $manualenroldata['enrol'] = 'manual';
62 $manualenroldata['status'] = 0;
63 $manualenroldata['courseid'] = $course->id;
64 $enrolid = $DB->insert_record('enrol', $manualenroldata);
1f8c8f61
PC
65
66 // Create a teacher and give them capabilities.
67 $context = context_course::instance($course->id);
68 $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
69 $context = context_module::instance($assign->id);
70 $this->assignUserCapability('mod/assign:grade', $context->id, $roleid);
71
72 // Create the teacher's enrolment record.
07df8c38
PC
73 $userenrolmentdata['status'] = 0;
74 $userenrolmentdata['enrolid'] = $enrolid;
75 $userenrolmentdata['userid'] = $USER->id;
76 $DB->insert_record('user_enrolments', $userenrolmentdata);
1f8c8f61 77
a13fbf5f 78 // Create a student and give them 2 grades (for 2 attempts).
1f8c8f61 79 $student = self::getDataGenerator()->create_user();
a13fbf5f
DW
80 $grade = new stdClass();
81 $grade->assignment = $assign->id;
82 $grade->userid = $student->id;
83 $grade->timecreated = time();
84 $grade->timemodified = $grade->timecreated;
85 $grade->grader = $USER->id;
86 $grade->grade = 50;
a13fbf5f
DW
87 $grade->attemptnumber = 0;
88 $DB->insert_record('assign_grades', $grade);
89
1f8c8f61
PC
90 $grade = new stdClass();
91 $grade->assignment = $assign->id;
92 $grade->userid = $student->id;
93 $grade->timecreated = time();
94 $grade->timemodified = $grade->timecreated;
95 $grade->grader = $USER->id;
96 $grade->grade = 75;
a13fbf5f 97 $grade->attemptnumber = 1;
1f8c8f61
PC
98 $DB->insert_record('assign_grades', $grade);
99
100 $assignmentids[] = $assign->id;
101 $result = mod_assign_external::get_grades($assignmentids);
102
fb695f6e
JM
103 // We need to execute the return values cleaning process to simulate the web service server.
104 $result = external_api::clean_returnvalue(mod_assign_external::get_grades_returns(), $result);
105
1f8c8f61
PC
106 // Check that the correct grade information for the student is returned.
107 $this->assertEquals(1, count($result['assignments']));
108 $assignment = $result['assignments'][0];
109 $this->assertEquals($assign->id, $assignment['assignmentid']);
a13fbf5f 110 // Should only get the last grade for this student.
1f8c8f61
PC
111 $this->assertEquals(1, count($assignment['grades']));
112 $grade = $assignment['grades'][0];
113 $this->assertEquals($student->id, $grade['userid']);
13e82f1c 114 // Should be the last grade (not the first).
1f8c8f61
PC
115 $this->assertEquals(75, $grade['grade']);
116 }
117
1378838e
PC
118 /**
119 * Test get_assignments
120 */
121 public function test_get_assignments () {
122 global $DB, $USER;
123
124 $this->resetAfterTest(true);
125
126 $category = self::getDataGenerator()->create_category(array(
127 'name' => 'Test category'
128 ));
129
130 // Create a course.
131 $course1 = self::getDataGenerator()->create_course(array(
132 'idnumber' => 'idnumbercourse1',
133 'fullname' => 'Lightwork Course 1',
134 'summary' => 'Lightwork Course 1 description',
135 'summaryformat' => FORMAT_MOODLE,
136 'category' => $category->id
137 ));
138
139 // Create a second course, just for testing.
140 $course2 = self::getDataGenerator()->create_course(array(
141 'idnumber' => 'idnumbercourse2',
142 'fullname' => 'Lightwork Course 2',
143 'summary' => 'Lightwork Course 2 description',
144 'summaryformat' => FORMAT_MOODLE,
145 'category' => $category->id
146 ));
147
148 // Create the assignment module.
149 $assign1 = self::getDataGenerator()->create_module('assign', array(
150 'course' => $course1->id,
d1ec5c83
PC
151 'name' => 'lightwork assignment',
152 'markingworkflow' => 1,
153 'markingallocation' => 1
1378838e
PC
154 ));
155
156 // Create manual enrolment record.
157 $enrolid = $DB->insert_record('enrol', (object)array(
158 'enrol' => 'manual',
159 'status' => 0,
160 'courseid' => $course1->id
161 ));
162
163 // Create the user and give them capabilities.
164 $context = context_course::instance($course1->id);
165 $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
166 $context = context_module::instance($assign1->id);
167 $this->assignUserCapability('mod/assign:view', $context->id, $roleid);
168
169 // Create the user enrolment record.
170 $DB->insert_record('user_enrolments', (object)array(
171 'status' => 0,
172 'enrolid' => $enrolid,
173 'userid' => $USER->id
174 ));
175
176 $result = mod_assign_external::get_assignments();
fb695f6e
JM
177
178 // We need to execute the return values cleaning process to simulate the web service server.
179 $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
180
1378838e
PC
181 // Check the course and assignment are returned.
182 $this->assertEquals(1, count($result['courses']));
183 $course = $result['courses'][0];
184 $this->assertEquals('Lightwork Course 1', $course['fullname']);
185 $this->assertEquals(1, count($course['assignments']));
186 $assignment = $course['assignments'][0];
187 $this->assertEquals($assign1->id, $assignment['id']);
188 $this->assertEquals($course1->id, $assignment['course']);
189 $this->assertEquals('lightwork assignment', $assignment['name']);
d1ec5c83
PC
190 $this->assertEquals(1, $assignment['markingworkflow']);
191 $this->assertEquals(1, $assignment['markingallocation']);
1378838e
PC
192
193 $result = mod_assign_external::get_assignments(array($course1->id));
fb695f6e
JM
194
195 // We need to execute the return values cleaning process to simulate the web service server.
196 $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
197
1378838e
PC
198 $this->assertEquals(1, count($result['courses']));
199 $course = $result['courses'][0];
200 $this->assertEquals('Lightwork Course 1', $course['fullname']);
201 $this->assertEquals(1, count($course['assignments']));
202 $assignment = $course['assignments'][0];
203 $this->assertEquals($assign1->id, $assignment['id']);
204 $this->assertEquals($course1->id, $assignment['course']);
205 $this->assertEquals('lightwork assignment', $assignment['name']);
d1ec5c83
PC
206 $this->assertEquals(1, $assignment['markingworkflow']);
207 $this->assertEquals(1, $assignment['markingallocation']);
1378838e
PC
208
209 $result = mod_assign_external::get_assignments(array($course2->id));
fb695f6e
JM
210
211 // We need to execute the return values cleaning process to simulate the web service server.
212 $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
213
1378838e
PC
214 $this->assertEquals(0, count($result['courses']));
215 $this->assertEquals(1, count($result['warnings']));
216 }
c144959c
PC
217
218 /**
219 * Test get_submissions
220 */
221 public function test_get_submissions () {
222 global $DB, $USER;
223
224 $this->resetAfterTest(true);
225 // Create a course and assignment.
226 $coursedata['idnumber'] = 'idnumbercourse1';
227 $coursedata['fullname'] = 'Lightwork Course 1';
228 $coursedata['summary'] = 'Lightwork Course 1 description';
229 $coursedata['summaryformat'] = FORMAT_MOODLE;
230 $course1 = self::getDataGenerator()->create_course($coursedata);
231
232 $assigndata['course'] = $course1->id;
233 $assigndata['name'] = 'lightwork assignment';
234
235 $assign1 = self::getDataGenerator()->create_module('assign', $assigndata);
236
237 // Create a student with an online text submission.
a13fbf5f 238 // First attempt.
c144959c
PC
239 $student = self::getDataGenerator()->create_user();
240 $submission = new stdClass();
241 $submission->assignment = $assign1->id;
242 $submission->userid = $student->id;
243 $submission->timecreated = time();
244 $submission->timemodified = $submission->timecreated;
a13fbf5f
DW
245 $submission->status = 'draft';
246 $submission->attemptnumber = 0;
247 $sid = $DB->insert_record('assign_submission', $submission);
248
249 // Second attempt.
250 $submission = new stdClass();
251 $submission->assignment = $assign1->id;
252 $submission->userid = $student->id;
253 $submission->timecreated = time();
254 $submission->timemodified = $submission->timecreated;
c144959c 255 $submission->status = 'submitted';
a13fbf5f 256 $submission->attemptnumber = 1;
c144959c
PC
257 $sid = $DB->insert_record('assign_submission', $submission);
258 $submission->id = $sid;
259
260 $onlinetextsubmission = new stdClass();
9f780282 261 $onlinetextsubmission->onlinetext = "<p>online test text</p>";
c144959c
PC
262 $onlinetextsubmission->onlineformat = 1;
263 $onlinetextsubmission->submission = $submission->id;
264 $onlinetextsubmission->assignment = $assign1->id;
265 $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission);
266
267 // Create manual enrolment record.
07df8c38
PC
268 $manualenroldata['enrol'] = 'manual';
269 $manualenroldata['status'] = 0;
270 $manualenroldata['courseid'] = $course1->id;
271 $enrolid = $DB->insert_record('enrol', $manualenroldata);
c144959c
PC
272
273 // Create a teacher and give them capabilities.
274 $context = context_course::instance($course1->id);
275 $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
276 $context = context_module::instance($assign1->id);
277 $this->assignUserCapability('mod/assign:grade', $context->id, $roleid);
278
279 // Create the teacher's enrolment record.
07df8c38
PC
280 $userenrolmentdata['status'] = 0;
281 $userenrolmentdata['enrolid'] = $enrolid;
282 $userenrolmentdata['userid'] = $USER->id;
283 $DB->insert_record('user_enrolments', $userenrolmentdata);
c144959c
PC
284
285 $assignmentids[] = $assign1->id;
286 $result = mod_assign_external::get_submissions($assignmentids);
287
288 // Check the online text submission is returned.
289 $this->assertEquals(1, count($result['assignments']));
290 $assignment = $result['assignments'][0];
291 $this->assertEquals($assign1->id, $assignment['assignmentid']);
292 $this->assertEquals(1, count($assignment['submissions']));
293 $submission = $assignment['submissions'][0];
294 $this->assertEquals($sid, $submission['id']);
295 $this->assertGreaterThanOrEqual(3, count($submission['plugins']));
296 $plugins = $submission['plugins'];
297 foreach ($plugins as $plugin) {
298 $foundonlinetext = false;
299 if ($plugin['type'] == 'onlinetext') {
300 $foundonlinetext = true;
301 break;
302 }
303 }
304 $this->assertTrue($foundonlinetext);
305 }
07df8c38
PC
306
307 /**
308 * Test get_user_flags
309 */
310 public function test_get_user_flags () {
311 global $DB, $USER;
312
313 $this->resetAfterTest(true);
314 // Create a course and assignment.
315 $coursedata['idnumber'] = 'idnumbercourse';
316 $coursedata['fullname'] = 'Lightwork Course';
317 $coursedata['summary'] = 'Lightwork Course description';
318 $coursedata['summaryformat'] = FORMAT_MOODLE;
319 $course = self::getDataGenerator()->create_course($coursedata);
320
321 $assigndata['course'] = $course->id;
322 $assigndata['name'] = 'lightwork assignment';
323
324 $assign = self::getDataGenerator()->create_module('assign', $assigndata);
325
326 // Create a manual enrolment record.
327 $manualenroldata['enrol'] = 'manual';
328 $manualenroldata['status'] = 0;
329 $manualenroldata['courseid'] = $course->id;
330 $enrolid = $DB->insert_record('enrol', $manualenroldata);
331
332 // Create a teacher and give them capabilities.
333 $context = context_course::instance($course->id);
334 $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
335 $context = context_module::instance($assign->id);
336 $this->assignUserCapability('mod/assign:grade', $context->id, $roleid);
337
338 // Create the teacher's enrolment record.
339 $userenrolmentdata['status'] = 0;
340 $userenrolmentdata['enrolid'] = $enrolid;
341 $userenrolmentdata['userid'] = $USER->id;
342 $DB->insert_record('user_enrolments', $userenrolmentdata);
343
344 // Create a student and give them a user flag record.
345 $student = self::getDataGenerator()->create_user();
346 $userflag = new stdClass();
347 $userflag->assignment = $assign->id;
348 $userflag->userid = $student->id;
349 $userflag->locked = 0;
350 $userflag->mailed = 0;
351 $userflag->extensionduedate = 0;
352 $userflag->workflowstate = 'inmarking';
353 $userflag->allocatedmarker = $USER->id;
354
355 $DB->insert_record('assign_user_flags', $userflag);
356
357 $assignmentids[] = $assign->id;
358 $result = mod_assign_external::get_user_flags($assignmentids);
359
360 // We need to execute the return values cleaning process to simulate the web service server.
361 $result = external_api::clean_returnvalue(mod_assign_external::get_user_flags_returns(), $result);
362
363 // Check that the correct user flag information for the student is returned.
364 $this->assertEquals(1, count($result['assignments']));
365 $assignment = $result['assignments'][0];
366 $this->assertEquals($assign->id, $assignment['assignmentid']);
367 // Should be one user flag record.
368 $this->assertEquals(1, count($assignment['userflags']));
369 $userflag = $assignment['userflags'][0];
370 $this->assertEquals($student->id, $userflag['userid']);
371 $this->assertEquals(0, $userflag['locked']);
372 $this->assertEquals(0, $userflag['mailed']);
373 $this->assertEquals(0, $userflag['extensionduedate']);
374 $this->assertEquals('inmarking', $userflag['workflowstate']);
375 $this->assertEquals($USER->id, $userflag['allocatedmarker']);
376 }
377
378 /**
379 * Test get_user_mappings
380 */
381 public function test_get_user_mappings () {
382 global $DB, $USER;
383
384 $this->resetAfterTest(true);
385 // Create a course and assignment.
386 $coursedata['idnumber'] = 'idnumbercourse';
387 $coursedata['fullname'] = 'Lightwork Course';
388 $coursedata['summary'] = 'Lightwork Course description';
389 $coursedata['summaryformat'] = FORMAT_MOODLE;
390 $course = self::getDataGenerator()->create_course($coursedata);
391
392 $assigndata['course'] = $course->id;
393 $assigndata['name'] = 'lightwork assignment';
394
395 $assign = self::getDataGenerator()->create_module('assign', $assigndata);
396
397 // Create a manual enrolment record.
398 $manualenroldata['enrol'] = 'manual';
399 $manualenroldata['status'] = 0;
400 $manualenroldata['courseid'] = $course->id;
401 $enrolid = $DB->insert_record('enrol', $manualenroldata);
402
403 // Create a teacher and give them capabilities.
404 $context = context_course::instance($course->id);
405 $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
406 $context = context_module::instance($assign->id);
407 $this->assignUserCapability('mod/assign:revealidentities', $context->id, $roleid);
408
409 // Create the teacher's enrolment record.
410 $userenrolmentdata['status'] = 0;
411 $userenrolmentdata['enrolid'] = $enrolid;
412 $userenrolmentdata['userid'] = $USER->id;
413 $DB->insert_record('user_enrolments', $userenrolmentdata);
414
415 // Create a student and give them a user mapping record.
416 $student = self::getDataGenerator()->create_user();
417 $mapping = new stdClass();
418 $mapping->assignment = $assign->id;
419 $mapping->userid = $student->id;
420
421 $DB->insert_record('assign_user_mapping', $mapping);
422
423 $assignmentids[] = $assign->id;
424 $result = mod_assign_external::get_user_mappings($assignmentids);
425
426 // We need to execute the return values cleaning process to simulate the web service server.
427 $result = external_api::clean_returnvalue(mod_assign_external::get_user_mappings_returns(), $result);
428
429 // Check that the correct user mapping information for the student is returned.
430 $this->assertEquals(1, count($result['assignments']));
431 $assignment = $result['assignments'][0];
432 $this->assertEquals($assign->id, $assignment['assignmentid']);
433 // Should be one user mapping record.
434 $this->assertEquals(1, count($assignment['mappings']));
435 $mapping = $assignment['mappings'][0];
436 $this->assertEquals($student->id, $mapping['userid']);
437 }
438
1f8c8f61 439}