MDL-37148 assign: Fix missing parameter for submit_for_grading webservice.
[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 */
05a6445a 44 public function test_get_grades() {
1f8c8f61
PC
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 */
05a6445a 121 public function test_get_assignments() {
1378838e
PC
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 */
05a6445a 221 public function test_get_submissions() {
c144959c
PC
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 */
05a6445a 310 public function test_get_user_flags() {
07df8c38
PC
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 */
05a6445a 381 public function test_get_user_mappings() {
07df8c38
PC
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
05a6445a
DW
439 /**
440 * Test lock_submissions
441 */
442 public function test_lock_submissions() {
443 global $DB, $USER;
444
445 $this->resetAfterTest(true);
446 // Create a course and assignment and users.
447 $course = self::getDataGenerator()->create_course();
448
449 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
450 $params['course'] = $course->id;
451 $params['assignsubmission_onlinetext_enabled'] = 1;
452 $instance = $generator->create_instance($params);
453 $cm = get_coursemodule_from_instance('assign', $instance->id);
454 $context = context_module::instance($cm->id);
455
456 $assign = new assign($context, $cm, $course);
457
458 $student1 = self::getDataGenerator()->create_user();
459 $student2 = self::getDataGenerator()->create_user();
460 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
461 $this->getDataGenerator()->enrol_user($student1->id,
462 $course->id,
463 $studentrole->id);
464 $this->getDataGenerator()->enrol_user($student2->id,
465 $course->id,
466 $studentrole->id);
467 $teacher = self::getDataGenerator()->create_user();
468 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
469 $this->getDataGenerator()->enrol_user($teacher->id,
470 $course->id,
471 $teacherrole->id);
472
473 // Create a student1 with an online text submission.
474 // Simulate a submission.
475 $this->setUser($student1);
476 $submission = $assign->get_user_submission($student1->id, true);
477 $data = new stdClass();
478 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
479 'text'=>'Submission text',
480 'format'=>FORMAT_MOODLE);
481 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
482 $plugin->save($submission, $data);
483
539af602 484 // Ready to test.
05a6445a
DW
485 $this->setUser($teacher);
486 $students = array($student1->id, $student2->id);
487 $result = mod_assign_external::lock_submissions($instance->id, $students);
488
489 // Check for 0 warnings.
490 $this->assertEquals(0, count($result));
491
492 $this->setUser($student2);
493 $submission = $assign->get_user_submission($student2->id, true);
494 $data = new stdClass();
495 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
496 'text'=>'Submission text',
497 'format'=>FORMAT_MOODLE);
498 $notices = array();
499 $this->setExpectedException('moodle_exception');
500 $assign->save_submission($data, $notices);
501 }
502
503 /**
504 * Test unlock_submissions
505 */
506 public function test_unlock_submissions() {
507 global $DB, $USER;
508
509 $this->resetAfterTest(true);
510 // Create a course and assignment and users.
511 $course = self::getDataGenerator()->create_course();
512
513 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
514 $params['course'] = $course->id;
515 $params['assignsubmission_onlinetext_enabled'] = 1;
516 $instance = $generator->create_instance($params);
517 $cm = get_coursemodule_from_instance('assign', $instance->id);
518 $context = context_module::instance($cm->id);
519
520 $assign = new assign($context, $cm, $course);
521
522 $student1 = self::getDataGenerator()->create_user();
523 $student2 = self::getDataGenerator()->create_user();
524 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
525 $this->getDataGenerator()->enrol_user($student1->id,
526 $course->id,
527 $studentrole->id);
528 $this->getDataGenerator()->enrol_user($student2->id,
529 $course->id,
530 $studentrole->id);
531 $teacher = self::getDataGenerator()->create_user();
532 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
533 $this->getDataGenerator()->enrol_user($teacher->id,
534 $course->id,
535 $teacherrole->id);
536
537 // Create a student1 with an online text submission.
538 // Simulate a submission.
539 $this->setUser($student1);
540 $submission = $assign->get_user_submission($student1->id, true);
541 $data = new stdClass();
542 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
543 'text'=>'Submission text',
544 'format'=>FORMAT_MOODLE);
545 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
546 $plugin->save($submission, $data);
547
539af602 548 // Ready to test.
05a6445a
DW
549 $this->setUser($teacher);
550 $students = array($student1->id, $student2->id);
551 $result = mod_assign_external::lock_submissions($instance->id, $students);
552
553 // Check for 0 warnings.
554 $this->assertEquals(0, count($result));
555
556 $result = mod_assign_external::unlock_submissions($instance->id, $students);
557
558 // Check for 0 warnings.
559 $this->assertEquals(0, count($result));
560
561 $this->setUser($student2);
562 $submission = $assign->get_user_submission($student2->id, true);
563 $data = new stdClass();
564 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
565 'text'=>'Submission text',
566 'format'=>FORMAT_MOODLE);
567 $notices = array();
568 $assign->save_submission($data, $notices);
569 }
570
571 /**
572 * Test submit_for_grading
573 */
574 public function test_submit_for_grading() {
575 global $DB, $USER;
576
577 $this->resetAfterTest(true);
578 // Create a course and assignment and users.
579 $course = self::getDataGenerator()->create_course();
580
581 set_config('submissionreceipts', 0, 'assign');
582 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
583 $params['course'] = $course->id;
584 $params['assignsubmission_onlinetext_enabled'] = 1;
585 $params['submissiondrafts'] = 1;
586 $params['sendnotifications'] = 0;
91327d96 587 $params['requiresubmissionstatement'] = 1;
05a6445a
DW
588 $instance = $generator->create_instance($params);
589 $cm = get_coursemodule_from_instance('assign', $instance->id);
590 $context = context_module::instance($cm->id);
591
592 $assign = new assign($context, $cm, $course);
593
594 $student1 = self::getDataGenerator()->create_user();
595 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
596 $this->getDataGenerator()->enrol_user($student1->id,
597 $course->id,
598 $studentrole->id);
599
600 // Create a student1 with an online text submission.
601 // Simulate a submission.
602 $this->setUser($student1);
603 $submission = $assign->get_user_submission($student1->id, true);
604 $data = new stdClass();
605 $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
606 'text'=>'Submission text',
607 'format'=>FORMAT_MOODLE);
608 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
609 $plugin->save($submission, $data);
610
91327d96
DW
611 $result = mod_assign_external::submit_for_grading($instance->id, false);
612
613 // Should be 1 fail because the submission statement was not aceptted.
614 $this->assertEquals(1, count($result));
615
616 $result = mod_assign_external::submit_for_grading($instance->id, true);
05a6445a
DW
617
618 // Check for 0 warnings.
619 $this->assertEquals(0, count($result));
620
621 $submission = $assign->get_user_submission($student1->id, false);
622
623 $this->assertEquals(ASSIGN_SUBMISSION_STATUS_SUBMITTED, $submission->status);
624 }
625
626 /**
627 * Test save_user_extensions
628 */
629 public function test_save_user_extensions() {
630 global $DB, $USER;
631
632 $this->resetAfterTest(true);
633 // Create a course and assignment and users.
634 $course = self::getDataGenerator()->create_course();
635
636 $teacher = self::getDataGenerator()->create_user();
637 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
638 $this->getDataGenerator()->enrol_user($teacher->id,
639 $course->id,
640 $teacherrole->id);
641 $this->setUser($teacher);
642
643 $now = time();
644 $yesterday = $now - 24*60*60;
645 $tomorrow = $now + 24*60*60;
646 set_config('submissionreceipts', 0, 'assign');
647 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
648 $params['course'] = $course->id;
649 $params['submissiondrafts'] = 1;
650 $params['sendnotifications'] = 0;
651 $params['duedate'] = $yesterday;
652 $params['cutoffdate'] = $now - 10;
653 $instance = $generator->create_instance($params);
654 $cm = get_coursemodule_from_instance('assign', $instance->id);
655 $context = context_module::instance($cm->id);
656
657 $assign = new assign($context, $cm, $course);
658
659 $student1 = self::getDataGenerator()->create_user();
660 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
661 $this->getDataGenerator()->enrol_user($student1->id,
662 $course->id,
663 $studentrole->id);
664
665 $this->setUser($student1);
91327d96 666 $result = mod_assign_external::submit_for_grading($instance->id, true);
05a6445a
DW
667
668 // Check for 0 warnings.
669 $this->assertEquals(1, count($result));
670
671 $this->setUser($teacher);
672 $result = mod_assign_external::save_user_extensions($instance->id, array($student1->id), array($now, $tomorrow));
673 $this->assertEquals(1, count($result));
674
675 $this->setUser($teacher);
676 $result = mod_assign_external::save_user_extensions($instance->id, array($student1->id), array($yesterday - 10));
677 $this->assertEquals(1, count($result));
678
679 $this->setUser($teacher);
680 $result = mod_assign_external::save_user_extensions($instance->id, array($student1->id), array($tomorrow));
681 $this->assertEquals(0, count($result));
682
683 $this->setUser($student1);
91327d96 684 $result = mod_assign_external::submit_for_grading($instance->id, true);
05a6445a
DW
685 $this->assertEquals(0, count($result));
686
687 $this->setUser($student1);
688 $result = mod_assign_external::save_user_extensions($instance->id, array($student1->id), array($now, $tomorrow));
689
690 }
691
692 /**
693 * Test reveal_identities
694 */
695 public function test_reveal_identities() {
696 global $DB, $USER;
697
698 $this->resetAfterTest(true);
699 // Create a course and assignment and users.
700 $course = self::getDataGenerator()->create_course();
701
702 $teacher = self::getDataGenerator()->create_user();
703 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
704 $this->getDataGenerator()->enrol_user($teacher->id,
705 $course->id,
706 $teacherrole->id);
707 $this->setUser($teacher);
708
709 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
710 $params['course'] = $course->id;
711 $params['submissiondrafts'] = 1;
712 $params['sendnotifications'] = 0;
713 $params['blindmarking'] = 1;
714 $instance = $generator->create_instance($params);
715 $cm = get_coursemodule_from_instance('assign', $instance->id);
716 $context = context_module::instance($cm->id);
717
718 $assign = new assign($context, $cm, $course);
719
720 $student1 = self::getDataGenerator()->create_user();
721 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
722 $this->getDataGenerator()->enrol_user($student1->id,
723 $course->id,
724 $studentrole->id);
725
726 $this->setUser($student1);
727 $this->setExpectedException('required_capability_exception');
728 $result = mod_assign_external::reveal_identities($instance->id);
729 $this->assertEquals(1, count($result));
730 $this->assertEquals(true, $assign->is_blind_marking());
731
732 $this->setUser($teacher);
733 $result = mod_assign_external::reveal_identities($instance->id);
734 $this->assertEquals(0, count($result));
735 $this->assertEquals(false, $assign->is_blind_marking());
736
737 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
738 $params['course'] = $course->id;
739 $params['submissiondrafts'] = 1;
740 $params['sendnotifications'] = 0;
741 $params['blindmarking'] = 0;
742 $instance = $generator->create_instance($params);
743 $cm = get_coursemodule_from_instance('assign', $instance->id);
744 $context = context_module::instance($cm->id);
745
746 $assign = new assign($context, $cm, $course);
747 $result = mod_assign_external::reveal_identities($instance->id);
748 $this->assertEquals(1, count($result));
749 $this->assertEquals(false, $assign->is_blind_marking());
750
751 }
752
753 /**
754 * Test revert_submissions_to_draft
755 */
756 public function test_revert_submissions_to_draft() {
757 global $DB, $USER;
758
759 $this->resetAfterTest(true);
760 set_config('submissionreceipts', 0, 'assign');
761 // Create a course and assignment and users.
762 $course = self::getDataGenerator()->create_course();
763
764 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
765 $params['course'] = $course->id;
766 $params['sendnotifications'] = 0;
767 $params['submissiondrafts'] = 1;
768 $instance = $generator->create_instance($params);
769 $cm = get_coursemodule_from_instance('assign', $instance->id);
770 $context = context_module::instance($cm->id);
771
772 $assign = new assign($context, $cm, $course);
773
774 $student1 = self::getDataGenerator()->create_user();
775 $student2 = self::getDataGenerator()->create_user();
776 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
777 $this->getDataGenerator()->enrol_user($student1->id,
778 $course->id,
779 $studentrole->id);
780 $this->getDataGenerator()->enrol_user($student2->id,
781 $course->id,
782 $studentrole->id);
783 $teacher = self::getDataGenerator()->create_user();
784 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
785 $this->getDataGenerator()->enrol_user($teacher->id,
786 $course->id,
787 $teacherrole->id);
788
789 // Create a student1 with an online text submission.
790 // Simulate a submission.
791 $this->setUser($student1);
91327d96 792 $result = mod_assign_external::submit_for_grading($instance->id, true);
05a6445a
DW
793 $this->assertEquals(0, count($result));
794
539af602 795 // Ready to test.
05a6445a
DW
796 $this->setUser($teacher);
797 $students = array($student1->id, $student2->id);
798 $result = mod_assign_external::revert_submissions_to_draft($instance->id, array($student1->id));
799
800 // Check for 0 warnings.
801 $this->assertEquals(0, count($result));
802
803 }
804
805 /**
806 * Test save_submission
807 */
808 public function test_save_submission() {
809 global $DB, $USER;
810
811 $this->resetAfterTest(true);
812 // Create a course and assignment and users.
813 $course = self::getDataGenerator()->create_course();
814
815 $teacher = self::getDataGenerator()->create_user();
816 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
817 $this->getDataGenerator()->enrol_user($teacher->id,
818 $course->id,
819 $teacherrole->id);
820 $this->setUser($teacher);
821
822 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
823 $params['course'] = $course->id;
824 $params['assignsubmission_onlinetext_enabled'] = 1;
825 $params['assignsubmission_file_enabled'] = 1;
826 $params['assignsubmission_file_maxfiles'] = 5;
827 $params['assignsubmission_file_maxsizebytes'] = 1024*1024;
828 $instance = $generator->create_instance($params);
829 $cm = get_coursemodule_from_instance('assign', $instance->id);
830 $context = context_module::instance($cm->id);
831
832 $assign = new assign($context, $cm, $course);
833
834 $student1 = self::getDataGenerator()->create_user();
835 $student2 = self::getDataGenerator()->create_user();
836 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
837 $this->getDataGenerator()->enrol_user($student1->id,
838 $course->id,
839 $studentrole->id);
840 $this->getDataGenerator()->enrol_user($student2->id,
841 $course->id,
842 $studentrole->id);
843 // Create a student1 with an online text submission.
844 // Simulate a submission.
845 $this->setUser($student1);
846
847 // Create a file in a draft area.
848 $draftidfile = file_get_unused_draft_itemid();
849
850 $usercontext = context_user::instance($student1->id);
851 $filerecord = array(
852 'contextid' => $usercontext->id,
853 'component' => 'user',
854 'filearea' => 'draft',
855 'itemid' => $draftidfile,
856 'filepath' => '/',
857 'filename' => 'testtext.txt',
858 );
859
860 $fs = get_file_storage();
861 $fs->create_file_from_string($filerecord, 'text contents');
862
863 // Create another file in a different draft area.
864 $draftidonlinetext = file_get_unused_draft_itemid();
865
866 $filerecord = array(
867 'contextid' => $usercontext->id,
868 'component' => 'user',
869 'filearea' => 'draft',
870 'itemid' => $draftidonlinetext,
871 'filepath' => '/',
872 'filename' => 'shouldbeanimage.txt',
873 );
874
875 $fs->create_file_from_string($filerecord, 'image contents (not really)');
876
539af602 877 // Now try a submission.
05a6445a
DW
878 $submissionpluginparams = array();
879 $submissionpluginparams['files_filemanager'] = $draftidfile;
880 $onlinetexteditorparams = array('text'=>'Yeeha!',
881 'format'=>1,
882 'itemid'=>$draftidonlinetext);
883 $submissionpluginparams['onlinetext_editor'] = $onlinetexteditorparams;
884 $result = mod_assign_external::save_submission($instance->id, $submissionpluginparams);
885
886 $this->assertEquals(0, count($result));
887
888 }
889
890 /**
891 * Test save_grade
892 */
893 public function test_save_grade() {
894 global $DB, $USER;
895
896 $this->resetAfterTest(true);
897 // Create a course and assignment and users.
898 $course = self::getDataGenerator()->create_course();
899
900 $teacher = self::getDataGenerator()->create_user();
901 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
902 $this->getDataGenerator()->enrol_user($teacher->id,
903 $course->id,
904 $teacherrole->id);
905 $this->setUser($teacher);
906
907 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
908 $params['course'] = $course->id;
909 $params['assignfeedback_file_enabled'] = 1;
910 $params['assignfeedback_comments_enabled'] = 1;
911 $instance = $generator->create_instance($params);
912 $cm = get_coursemodule_from_instance('assign', $instance->id);
913 $context = context_module::instance($cm->id);
914
915 $assign = new assign($context, $cm, $course);
916
917 $student1 = self::getDataGenerator()->create_user();
918 $student2 = self::getDataGenerator()->create_user();
919 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
920 $this->getDataGenerator()->enrol_user($student1->id,
921 $course->id,
922 $studentrole->id);
923 $this->getDataGenerator()->enrol_user($student2->id,
924 $course->id,
925 $studentrole->id);
926 // Simulate a grade.
927 $this->setUser($teacher);
928
929 // Create a file in a draft area.
930 $draftidfile = file_get_unused_draft_itemid();
931
932 $usercontext = context_user::instance($teacher->id);
933 $filerecord = array(
934 'contextid' => $usercontext->id,
935 'component' => 'user',
936 'filearea' => 'draft',
937 'itemid' => $draftidfile,
938 'filepath' => '/',
939 'filename' => 'testtext.txt',
940 );
941
942 $fs = get_file_storage();
943 $fs->create_file_from_string($filerecord, 'text contents');
944
539af602 945 // Now try a grade.
05a6445a
DW
946 $feedbackpluginparams = array();
947 $feedbackpluginparams['files_filemanager'] = $draftidfile;
948 $feedbackeditorparams = array('text'=>'Yeeha!',
949 'format'=>1);
950 $feedbackpluginparams['assignfeedbackcomments_editor'] = $feedbackeditorparams;
539af602
DW
951 $result = mod_assign_external::save_grade($instance->id,
952 $student1->id,
953 50.0,
954 -1,
955 true,
956 'released',
957 false,
958 $feedbackpluginparams);
05a6445a
DW
959
960 // No warnings.
961 $this->assertEquals(0, count($result));
962
963 $result = mod_assign_external::get_grades(array($instance->id));
964
965 $this->assertEquals($result['assignments'][0]['grades'][0]['grade'], '50.0');
05a6445a
DW
966 }
967
968 /**
969 * Test copy_previous_attempt
970 */
971 public function test_copy_previous_attempt() {
972 global $DB, $USER;
973
974 $this->resetAfterTest(true);
975 // Create a course and assignment and users.
976 $course = self::getDataGenerator()->create_course();
977
978 $teacher = self::getDataGenerator()->create_user();
979 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
980 $this->getDataGenerator()->enrol_user($teacher->id,
981 $course->id,
982 $teacherrole->id);
983 $this->setUser($teacher);
984
985 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
986 $params['course'] = $course->id;
987 $params['assignsubmission_onlinetext_enabled'] = 1;
988 $params['assignsubmission_file_enabled'] = 0;
989 $params['assignfeedback_file_enabled'] = 0;
990 $params['attemptreopenmethod'] = 'manual';
991 $params['maxattempts'] = 5;
992 $instance = $generator->create_instance($params);
993 $cm = get_coursemodule_from_instance('assign', $instance->id);
994 $context = context_module::instance($cm->id);
995
996 $assign = new assign($context, $cm, $course);
997
998 $student1 = self::getDataGenerator()->create_user();
999 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
1000 $this->getDataGenerator()->enrol_user($student1->id,
1001 $course->id,
1002 $studentrole->id);
539af602 1003 // Now try a submission.
05a6445a
DW
1004 $this->setUser($student1);
1005 $draftidonlinetext = file_get_unused_draft_itemid();
1006 $submissionpluginparams = array();
1007 $onlinetexteditorparams = array('text'=>'Yeeha!',
1008 'format'=>1,
1009 'itemid'=>$draftidonlinetext);
1010 $submissionpluginparams['onlinetext_editor'] = $onlinetexteditorparams;
1011 $submissionpluginparams['files_filemanager'] = file_get_unused_draft_itemid();
1012 $result = mod_assign_external::save_submission($instance->id, $submissionpluginparams);
1013
1014 $this->setUser($teacher);
1015 // Add a grade and reopen the attempt.
539af602 1016 // Now try a grade.
05a6445a
DW
1017 $feedbackpluginparams = array();
1018 $feedbackpluginparams['files_filemanager'] = file_get_unused_draft_itemid();
1019 $feedbackeditorparams = array('text'=>'Yeeha!',
1020 'format'=>1);
1021 $feedbackpluginparams['assignfeedbackcomments_editor'] = $feedbackeditorparams;
539af602
DW
1022 $result = mod_assign_external::save_grade($instance->id,
1023 $student1->id,
1024 50.0,
1025 -1,
1026 true,
1027 'released',
1028 false,
1029 $feedbackpluginparams);
05a6445a
DW
1030
1031 $this->setUser($student1);
1032 // Now copy the previous attempt.
1033 $result = mod_assign_external::copy_previous_attempt($instance->id);
1034 // No warnings.
1035 $this->assertEquals(0, count($result));
1036
1037 $this->setUser($teacher);
1038 $result = mod_assign_external::get_submissions(array($instance->id));
1039
1040 // Check we are now on the second attempt.
1041 $this->assertEquals($result['assignments'][0]['submissions'][0]['attemptnumber'], 1);
1042 // Check the plugins data is not empty.
1043 $this->assertNotEmpty($result['assignments'][0]['submissions'][0]['plugins']);
1044
1045 }
1f8c8f61 1046}