MDL-37148 assign: Fix missing parameter for submit_for_grading webservice.
[moodle.git] / mod / assign / tests / externallib_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 defined('MOODLE_INTERNAL') || die();
19 global $CFG;
21 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
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  */
31 class mod_assign_external_testcase extends externallib_advanced_testcase {
33     /**
34      * Tests set up
35      */
36     protected function setUp() {
37         global $CFG;
38         require_once($CFG->dirroot . '/mod/assign/externallib.php');
39     }
41     /**
42      * Test get_grades
43      */
44     public function test_get_grades() {
45         global $DB, $USER;
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);
55         $assigndata['course'] = $course->id;
56         $assigndata['name'] = 'lightwork assignment';
58         $assign = self::getDataGenerator()->create_module('assign', $assigndata);
60         // Create a manual enrolment record.
61         $manualenroldata['enrol'] = 'manual';
62         $manualenroldata['status'] = 0;
63         $manualenroldata['courseid'] = $course->id;
64         $enrolid = $DB->insert_record('enrol', $manualenroldata);
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);
72         // Create the teacher's enrolment record.
73         $userenrolmentdata['status'] = 0;
74         $userenrolmentdata['enrolid'] = $enrolid;
75         $userenrolmentdata['userid'] = $USER->id;
76         $DB->insert_record('user_enrolments', $userenrolmentdata);
78         // Create a student and give them 2 grades (for 2 attempts).
79         $student = self::getDataGenerator()->create_user();
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;
87         $grade->attemptnumber = 0;
88         $DB->insert_record('assign_grades', $grade);
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;
97         $grade->attemptnumber = 1;
98         $DB->insert_record('assign_grades', $grade);
100         $assignmentids[] = $assign->id;
101         $result = mod_assign_external::get_grades($assignmentids);
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);
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']);
110         // Should only get the last grade for this student.
111         $this->assertEquals(1, count($assignment['grades']));
112         $grade = $assignment['grades'][0];
113         $this->assertEquals($student->id, $grade['userid']);
114         // Should be the last grade (not the first).
115         $this->assertEquals(75, $grade['grade']);
116     }
118     /**
119      * Test get_assignments
120      */
121     public function test_get_assignments() {
122         global $DB, $USER;
124         $this->resetAfterTest(true);
126         $category = self::getDataGenerator()->create_category(array(
127             'name' => 'Test category'
128         ));
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         ));
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         ));
148         // Create the assignment module.
149         $assign1 = self::getDataGenerator()->create_module('assign', array(
150             'course' => $course1->id,
151             'name' => 'lightwork assignment',
152             'markingworkflow' => 1,
153             'markingallocation' => 1
154         ));
156         // Create manual enrolment record.
157         $enrolid = $DB->insert_record('enrol', (object)array(
158             'enrol' => 'manual',
159             'status' => 0,
160             'courseid' => $course1->id
161         ));
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);
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         ));
176         $result = mod_assign_external::get_assignments();
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);
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']);
190         $this->assertEquals(1, $assignment['markingworkflow']);
191         $this->assertEquals(1, $assignment['markingallocation']);
193         $result = mod_assign_external::get_assignments(array($course1->id));
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);
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']);
206         $this->assertEquals(1, $assignment['markingworkflow']);
207         $this->assertEquals(1, $assignment['markingallocation']);
209         $result = mod_assign_external::get_assignments(array($course2->id));
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);
214         $this->assertEquals(0, count($result['courses']));
215         $this->assertEquals(1, count($result['warnings']));
216     }
218     /**
219      * Test get_submissions
220      */
221     public function test_get_submissions() {
222         global $DB, $USER;
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);
232         $assigndata['course'] = $course1->id;
233         $assigndata['name'] = 'lightwork assignment';
235         $assign1 = self::getDataGenerator()->create_module('assign', $assigndata);
237         // Create a student with an online text submission.
238         // First attempt.
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;
245         $submission->status = 'draft';
246         $submission->attemptnumber = 0;
247         $sid = $DB->insert_record('assign_submission', $submission);
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;
255         $submission->status = 'submitted';
256         $submission->attemptnumber = 1;
257         $sid = $DB->insert_record('assign_submission', $submission);
258         $submission->id = $sid;
260         $onlinetextsubmission = new stdClass();
261         $onlinetextsubmission->onlinetext = "<p>online test text</p>";
262         $onlinetextsubmission->onlineformat = 1;
263         $onlinetextsubmission->submission = $submission->id;
264         $onlinetextsubmission->assignment = $assign1->id;
265         $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission);
267         // Create manual enrolment record.
268         $manualenroldata['enrol'] = 'manual';
269         $manualenroldata['status'] = 0;
270         $manualenroldata['courseid'] = $course1->id;
271         $enrolid = $DB->insert_record('enrol', $manualenroldata);
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);
279         // Create the teacher's enrolment record.
280         $userenrolmentdata['status'] = 0;
281         $userenrolmentdata['enrolid'] = $enrolid;
282         $userenrolmentdata['userid'] = $USER->id;
283         $DB->insert_record('user_enrolments', $userenrolmentdata);
285         $assignmentids[] = $assign1->id;
286         $result = mod_assign_external::get_submissions($assignmentids);
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     }
307     /**
308      * Test get_user_flags
309      */
310     public function test_get_user_flags() {
311         global $DB, $USER;
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);
321         $assigndata['course'] = $course->id;
322         $assigndata['name'] = 'lightwork assignment';
324         $assign = self::getDataGenerator()->create_module('assign', $assigndata);
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);
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);
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);
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;
355         $DB->insert_record('assign_user_flags', $userflag);
357         $assignmentids[] = $assign->id;
358         $result = mod_assign_external::get_user_flags($assignmentids);
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);
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     }
378     /**
379      * Test get_user_mappings
380      */
381     public function test_get_user_mappings() {
382         global $DB, $USER;
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);
392         $assigndata['course'] = $course->id;
393         $assigndata['name'] = 'lightwork assignment';
395         $assign = self::getDataGenerator()->create_module('assign', $assigndata);
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);
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);
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);
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;
421         $DB->insert_record('assign_user_mapping', $mapping);
423         $assignmentids[] = $assign->id;
424         $result = mod_assign_external::get_user_mappings($assignmentids);
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);
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     }
439     /**
440      * Test lock_submissions
441      */
442     public function test_lock_submissions() {
443         global $DB, $USER;
445         $this->resetAfterTest(true);
446         // Create a course and assignment and users.
447         $course = self::getDataGenerator()->create_course();
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);
456         $assign = new assign($context, $cm, $course);
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);
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);
484         // Ready to test.
485         $this->setUser($teacher);
486         $students = array($student1->id, $student2->id);
487         $result = mod_assign_external::lock_submissions($instance->id, $students);
489         // Check for 0 warnings.
490         $this->assertEquals(0, count($result));
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     }
503     /**
504      * Test unlock_submissions
505      */
506     public function test_unlock_submissions() {
507         global $DB, $USER;
509         $this->resetAfterTest(true);
510         // Create a course and assignment and users.
511         $course = self::getDataGenerator()->create_course();
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);
520         $assign = new assign($context, $cm, $course);
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);
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);
548         // Ready to test.
549         $this->setUser($teacher);
550         $students = array($student1->id, $student2->id);
551         $result = mod_assign_external::lock_submissions($instance->id, $students);
553         // Check for 0 warnings.
554         $this->assertEquals(0, count($result));
556         $result = mod_assign_external::unlock_submissions($instance->id, $students);
558         // Check for 0 warnings.
559         $this->assertEquals(0, count($result));
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     }
571     /**
572      * Test submit_for_grading
573      */
574     public function test_submit_for_grading() {
575         global $DB, $USER;
577         $this->resetAfterTest(true);
578         // Create a course and assignment and users.
579         $course = self::getDataGenerator()->create_course();
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;
587         $params['requiresubmissionstatement'] = 1;
588         $instance = $generator->create_instance($params);
589         $cm = get_coursemodule_from_instance('assign', $instance->id);
590         $context = context_module::instance($cm->id);
592         $assign = new assign($context, $cm, $course);
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);
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);
611         $result = mod_assign_external::submit_for_grading($instance->id, false);
613         // Should be 1 fail because the submission statement was not aceptted.
614         $this->assertEquals(1, count($result));
616         $result = mod_assign_external::submit_for_grading($instance->id, true);
618         // Check for 0 warnings.
619         $this->assertEquals(0, count($result));
621         $submission = $assign->get_user_submission($student1->id, false);
623         $this->assertEquals(ASSIGN_SUBMISSION_STATUS_SUBMITTED, $submission->status);
624     }
626     /**
627      * Test save_user_extensions
628      */
629     public function test_save_user_extensions() {
630         global $DB, $USER;
632         $this->resetAfterTest(true);
633         // Create a course and assignment and users.
634         $course = self::getDataGenerator()->create_course();
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);
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);
657         $assign = new assign($context, $cm, $course);
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);
665         $this->setUser($student1);
666         $result = mod_assign_external::submit_for_grading($instance->id, true);
668         // Check for 0 warnings.
669         $this->assertEquals(1, count($result));
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));
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));
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));
683         $this->setUser($student1);
684         $result = mod_assign_external::submit_for_grading($instance->id, true);
685         $this->assertEquals(0, count($result));
687         $this->setUser($student1);
688         $result = mod_assign_external::save_user_extensions($instance->id, array($student1->id), array($now, $tomorrow));
690     }
692     /**
693      * Test reveal_identities
694      */
695     public function test_reveal_identities() {
696         global $DB, $USER;
698         $this->resetAfterTest(true);
699         // Create a course and assignment and users.
700         $course = self::getDataGenerator()->create_course();
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);
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);
718         $assign = new assign($context, $cm, $course);
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);
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());
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());
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);
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());
751     }
753     /**
754      * Test revert_submissions_to_draft
755      */
756     public function test_revert_submissions_to_draft() {
757         global $DB, $USER;
759         $this->resetAfterTest(true);
760         set_config('submissionreceipts', 0, 'assign');
761         // Create a course and assignment and users.
762         $course = self::getDataGenerator()->create_course();
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);
772         $assign = new assign($context, $cm, $course);
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);
789         // Create a student1 with an online text submission.
790         // Simulate a submission.
791         $this->setUser($student1);
792         $result = mod_assign_external::submit_for_grading($instance->id, true);
793         $this->assertEquals(0, count($result));
795         // Ready to test.
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));
800         // Check for 0 warnings.
801         $this->assertEquals(0, count($result));
803     }
805     /**
806      * Test save_submission
807      */
808     public function test_save_submission() {
809         global $DB, $USER;
811         $this->resetAfterTest(true);
812         // Create a course and assignment and users.
813         $course = self::getDataGenerator()->create_course();
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);
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);
832         $assign = new assign($context, $cm, $course);
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);
847         // Create a file in a draft area.
848         $draftidfile = file_get_unused_draft_itemid();
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         );
860         $fs = get_file_storage();
861         $fs->create_file_from_string($filerecord, 'text contents');
863         // Create another file in a different draft area.
864         $draftidonlinetext = file_get_unused_draft_itemid();
866         $filerecord = array(
867             'contextid' => $usercontext->id,
868             'component' => 'user',
869             'filearea'  => 'draft',
870             'itemid'    => $draftidonlinetext,
871             'filepath'  => '/',
872             'filename'  => 'shouldbeanimage.txt',
873         );
875         $fs->create_file_from_string($filerecord, 'image contents (not really)');
877         // Now try a submission.
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);
886         $this->assertEquals(0, count($result));
888     }
890     /**
891      * Test save_grade
892      */
893     public function test_save_grade() {
894         global $DB, $USER;
896         $this->resetAfterTest(true);
897         // Create a course and assignment and users.
898         $course = self::getDataGenerator()->create_course();
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);
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);
915         $assign = new assign($context, $cm, $course);
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);
929         // Create a file in a draft area.
930         $draftidfile = file_get_unused_draft_itemid();
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         );
942         $fs = get_file_storage();
943         $fs->create_file_from_string($filerecord, 'text contents');
945         // Now try a grade.
946         $feedbackpluginparams = array();
947         $feedbackpluginparams['files_filemanager'] = $draftidfile;
948         $feedbackeditorparams = array('text'=>'Yeeha!',
949                                         'format'=>1);
950         $feedbackpluginparams['assignfeedbackcomments_editor'] = $feedbackeditorparams;
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);
960         // No warnings.
961         $this->assertEquals(0, count($result));
963         $result = mod_assign_external::get_grades(array($instance->id));
965         $this->assertEquals($result['assignments'][0]['grades'][0]['grade'], '50.0');
966     }
968     /**
969      * Test copy_previous_attempt
970      */
971     public function test_copy_previous_attempt() {
972         global $DB, $USER;
974         $this->resetAfterTest(true);
975         // Create a course and assignment and users.
976         $course = self::getDataGenerator()->create_course();
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);
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);
996         $assign = new assign($context, $cm, $course);
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);
1003         // Now try a submission.
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);
1014         $this->setUser($teacher);
1015         // Add a grade and reopen the attempt.
1016         // Now try a grade.
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;
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);
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));
1037         $this->setUser($teacher);
1038         $result = mod_assign_external::get_submissions(array($instance->id));
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']);
1045     }