aeb0cb3eb7f8992b2cac9470d6dd838b1fd0655b
[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         $instance = $generator->create_instance($params);
588         $cm = get_coursemodule_from_instance('assign', $instance->id);
589         $context = context_module::instance($cm->id);
591         $assign = new assign($context, $cm, $course);
593         $student1 = self::getDataGenerator()->create_user();
594         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
595         $this->getDataGenerator()->enrol_user($student1->id,
596                                               $course->id,
597                                               $studentrole->id);
599         // Create a student1 with an online text submission.
600         // Simulate a submission.
601         $this->setUser($student1);
602         $submission = $assign->get_user_submission($student1->id, true);
603         $data = new stdClass();
604         $data->onlinetext_editor = array('itemid'=>file_get_unused_draft_itemid(),
605                                          'text'=>'Submission text',
606                                          'format'=>FORMAT_MOODLE);
607         $plugin = $assign->get_submission_plugin_by_type('onlinetext');
608         $plugin->save($submission, $data);
610         $result = mod_assign_external::submit_for_grading($instance->id);
612         // Check for 0 warnings.
613         $this->assertEquals(0, count($result));
615         $submission = $assign->get_user_submission($student1->id, false);
617         $this->assertEquals(ASSIGN_SUBMISSION_STATUS_SUBMITTED, $submission->status);
618     }
620     /**
621      * Test save_user_extensions
622      */
623     public function test_save_user_extensions() {
624         global $DB, $USER;
626         $this->resetAfterTest(true);
627         // Create a course and assignment and users.
628         $course = self::getDataGenerator()->create_course();
630         $teacher = self::getDataGenerator()->create_user();
631         $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
632         $this->getDataGenerator()->enrol_user($teacher->id,
633                                               $course->id,
634                                               $teacherrole->id);
635         $this->setUser($teacher);
637         $now = time();
638         $yesterday = $now - 24*60*60;
639         $tomorrow = $now + 24*60*60;
640         set_config('submissionreceipts', 0, 'assign');
641         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
642         $params['course'] = $course->id;
643         $params['submissiondrafts'] = 1;
644         $params['sendnotifications'] = 0;
645         $params['duedate'] = $yesterday;
646         $params['cutoffdate'] = $now - 10;
647         $instance = $generator->create_instance($params);
648         $cm = get_coursemodule_from_instance('assign', $instance->id);
649         $context = context_module::instance($cm->id);
651         $assign = new assign($context, $cm, $course);
653         $student1 = self::getDataGenerator()->create_user();
654         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
655         $this->getDataGenerator()->enrol_user($student1->id,
656                                               $course->id,
657                                               $studentrole->id);
659         $this->setUser($student1);
660         $result = mod_assign_external::submit_for_grading($instance->id);
662         // Check for 0 warnings.
663         $this->assertEquals(1, count($result));
665         $this->setUser($teacher);
666         $result = mod_assign_external::save_user_extensions($instance->id, array($student1->id), array($now, $tomorrow));
667         $this->assertEquals(1, count($result));
669         $this->setUser($teacher);
670         $result = mod_assign_external::save_user_extensions($instance->id, array($student1->id), array($yesterday - 10));
671         $this->assertEquals(1, count($result));
673         $this->setUser($teacher);
674         $result = mod_assign_external::save_user_extensions($instance->id, array($student1->id), array($tomorrow));
675         $this->assertEquals(0, count($result));
677         $this->setUser($student1);
678         $result = mod_assign_external::submit_for_grading($instance->id);
679         $this->assertEquals(0, count($result));
681         $this->setUser($student1);
682         $result = mod_assign_external::save_user_extensions($instance->id, array($student1->id), array($now, $tomorrow));
684     }
686     /**
687      * Test reveal_identities
688      */
689     public function test_reveal_identities() {
690         global $DB, $USER;
692         $this->resetAfterTest(true);
693         // Create a course and assignment and users.
694         $course = self::getDataGenerator()->create_course();
696         $teacher = self::getDataGenerator()->create_user();
697         $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
698         $this->getDataGenerator()->enrol_user($teacher->id,
699                                               $course->id,
700                                               $teacherrole->id);
701         $this->setUser($teacher);
703         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
704         $params['course'] = $course->id;
705         $params['submissiondrafts'] = 1;
706         $params['sendnotifications'] = 0;
707         $params['blindmarking'] = 1;
708         $instance = $generator->create_instance($params);
709         $cm = get_coursemodule_from_instance('assign', $instance->id);
710         $context = context_module::instance($cm->id);
712         $assign = new assign($context, $cm, $course);
714         $student1 = self::getDataGenerator()->create_user();
715         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
716         $this->getDataGenerator()->enrol_user($student1->id,
717                                               $course->id,
718                                               $studentrole->id);
720         $this->setUser($student1);
721         $this->setExpectedException('required_capability_exception');
722         $result = mod_assign_external::reveal_identities($instance->id);
723         $this->assertEquals(1, count($result));
724         $this->assertEquals(true, $assign->is_blind_marking());
726         $this->setUser($teacher);
727         $result = mod_assign_external::reveal_identities($instance->id);
728         $this->assertEquals(0, count($result));
729         $this->assertEquals(false, $assign->is_blind_marking());
731         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
732         $params['course'] = $course->id;
733         $params['submissiondrafts'] = 1;
734         $params['sendnotifications'] = 0;
735         $params['blindmarking'] = 0;
736         $instance = $generator->create_instance($params);
737         $cm = get_coursemodule_from_instance('assign', $instance->id);
738         $context = context_module::instance($cm->id);
740         $assign = new assign($context, $cm, $course);
741         $result = mod_assign_external::reveal_identities($instance->id);
742         $this->assertEquals(1, count($result));
743         $this->assertEquals(false, $assign->is_blind_marking());
745     }
747     /**
748      * Test revert_submissions_to_draft
749      */
750     public function test_revert_submissions_to_draft() {
751         global $DB, $USER;
753         $this->resetAfterTest(true);
754         set_config('submissionreceipts', 0, 'assign');
755         // Create a course and assignment and users.
756         $course = self::getDataGenerator()->create_course();
758         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
759         $params['course'] = $course->id;
760         $params['sendnotifications'] = 0;
761         $params['submissiondrafts'] = 1;
762         $instance = $generator->create_instance($params);
763         $cm = get_coursemodule_from_instance('assign', $instance->id);
764         $context = context_module::instance($cm->id);
766         $assign = new assign($context, $cm, $course);
768         $student1 = self::getDataGenerator()->create_user();
769         $student2 = self::getDataGenerator()->create_user();
770         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
771         $this->getDataGenerator()->enrol_user($student1->id,
772                                               $course->id,
773                                               $studentrole->id);
774         $this->getDataGenerator()->enrol_user($student2->id,
775                                               $course->id,
776                                               $studentrole->id);
777         $teacher = self::getDataGenerator()->create_user();
778         $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
779         $this->getDataGenerator()->enrol_user($teacher->id,
780                                               $course->id,
781                                               $teacherrole->id);
783         // Create a student1 with an online text submission.
784         // Simulate a submission.
785         $this->setUser($student1);
786         $result = mod_assign_external::submit_for_grading($instance->id);
787         $this->assertEquals(0, count($result));
789         // Ready to test.
790         $this->setUser($teacher);
791         $students = array($student1->id, $student2->id);
792         $result = mod_assign_external::revert_submissions_to_draft($instance->id, array($student1->id));
794         // Check for 0 warnings.
795         $this->assertEquals(0, count($result));
797     }
799     /**
800      * Test save_submission
801      */
802     public function test_save_submission() {
803         global $DB, $USER;
805         $this->resetAfterTest(true);
806         // Create a course and assignment and users.
807         $course = self::getDataGenerator()->create_course();
809         $teacher = self::getDataGenerator()->create_user();
810         $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
811         $this->getDataGenerator()->enrol_user($teacher->id,
812                                               $course->id,
813                                               $teacherrole->id);
814         $this->setUser($teacher);
816         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
817         $params['course'] = $course->id;
818         $params['assignsubmission_onlinetext_enabled'] = 1;
819         $params['assignsubmission_file_enabled'] = 1;
820         $params['assignsubmission_file_maxfiles'] = 5;
821         $params['assignsubmission_file_maxsizebytes'] = 1024*1024;
822         $instance = $generator->create_instance($params);
823         $cm = get_coursemodule_from_instance('assign', $instance->id);
824         $context = context_module::instance($cm->id);
826         $assign = new assign($context, $cm, $course);
828         $student1 = self::getDataGenerator()->create_user();
829         $student2 = self::getDataGenerator()->create_user();
830         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
831         $this->getDataGenerator()->enrol_user($student1->id,
832                                               $course->id,
833                                               $studentrole->id);
834         $this->getDataGenerator()->enrol_user($student2->id,
835                                               $course->id,
836                                               $studentrole->id);
837         // Create a student1 with an online text submission.
838         // Simulate a submission.
839         $this->setUser($student1);
841         // Create a file in a draft area.
842         $draftidfile = file_get_unused_draft_itemid();
844         $usercontext = context_user::instance($student1->id);
845         $filerecord = array(
846             'contextid' => $usercontext->id,
847             'component' => 'user',
848             'filearea'  => 'draft',
849             'itemid'    => $draftidfile,
850             'filepath'  => '/',
851             'filename'  => 'testtext.txt',
852         );
854         $fs = get_file_storage();
855         $fs->create_file_from_string($filerecord, 'text contents');
857         // Create another file in a different draft area.
858         $draftidonlinetext = file_get_unused_draft_itemid();
860         $filerecord = array(
861             'contextid' => $usercontext->id,
862             'component' => 'user',
863             'filearea'  => 'draft',
864             'itemid'    => $draftidonlinetext,
865             'filepath'  => '/',
866             'filename'  => 'shouldbeanimage.txt',
867         );
869         $fs->create_file_from_string($filerecord, 'image contents (not really)');
871         // Now try a submission.
872         $submissionpluginparams = array();
873         $submissionpluginparams['files_filemanager'] = $draftidfile;
874         $onlinetexteditorparams = array('text'=>'Yeeha!',
875                                         'format'=>1,
876                                         'itemid'=>$draftidonlinetext);
877         $submissionpluginparams['onlinetext_editor'] = $onlinetexteditorparams;
878         $result = mod_assign_external::save_submission($instance->id, $submissionpluginparams);
880         $this->assertEquals(0, count($result));
882     }
884     /**
885      * Test save_grade
886      */
887     public function test_save_grade() {
888         global $DB, $USER;
890         $this->resetAfterTest(true);
891         // Create a course and assignment and users.
892         $course = self::getDataGenerator()->create_course();
894         $teacher = self::getDataGenerator()->create_user();
895         $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
896         $this->getDataGenerator()->enrol_user($teacher->id,
897                                               $course->id,
898                                               $teacherrole->id);
899         $this->setUser($teacher);
901         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
902         $params['course'] = $course->id;
903         $params['assignfeedback_file_enabled'] = 1;
904         $params['assignfeedback_comments_enabled'] = 1;
905         $instance = $generator->create_instance($params);
906         $cm = get_coursemodule_from_instance('assign', $instance->id);
907         $context = context_module::instance($cm->id);
909         $assign = new assign($context, $cm, $course);
911         $student1 = self::getDataGenerator()->create_user();
912         $student2 = self::getDataGenerator()->create_user();
913         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
914         $this->getDataGenerator()->enrol_user($student1->id,
915                                               $course->id,
916                                               $studentrole->id);
917         $this->getDataGenerator()->enrol_user($student2->id,
918                                               $course->id,
919                                               $studentrole->id);
920         // Simulate a grade.
921         $this->setUser($teacher);
923         // Create a file in a draft area.
924         $draftidfile = file_get_unused_draft_itemid();
926         $usercontext = context_user::instance($teacher->id);
927         $filerecord = array(
928             'contextid' => $usercontext->id,
929             'component' => 'user',
930             'filearea'  => 'draft',
931             'itemid'    => $draftidfile,
932             'filepath'  => '/',
933             'filename'  => 'testtext.txt',
934         );
936         $fs = get_file_storage();
937         $fs->create_file_from_string($filerecord, 'text contents');
939         // Now try a grade.
940         $feedbackpluginparams = array();
941         $feedbackpluginparams['files_filemanager'] = $draftidfile;
942         $feedbackeditorparams = array('text'=>'Yeeha!',
943                                         'format'=>1);
944         $feedbackpluginparams['assignfeedbackcomments_editor'] = $feedbackeditorparams;
945         $result = mod_assign_external::save_grade($instance->id,
946                                                   $student1->id,
947                                                   50.0,
948                                                   -1,
949                                                   true,
950                                                   'released',
951                                                   false,
952                                                   $feedbackpluginparams);
954         // No warnings.
955         $this->assertEquals(0, count($result));
957         $result = mod_assign_external::get_grades(array($instance->id));
959         $this->assertEquals($result['assignments'][0]['grades'][0]['grade'], '50.0');
960     }
962     /**
963      * Test copy_previous_attempt
964      */
965     public function test_copy_previous_attempt() {
966         global $DB, $USER;
968         $this->resetAfterTest(true);
969         // Create a course and assignment and users.
970         $course = self::getDataGenerator()->create_course();
972         $teacher = self::getDataGenerator()->create_user();
973         $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
974         $this->getDataGenerator()->enrol_user($teacher->id,
975                                               $course->id,
976                                               $teacherrole->id);
977         $this->setUser($teacher);
979         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
980         $params['course'] = $course->id;
981         $params['assignsubmission_onlinetext_enabled'] = 1;
982         $params['assignsubmission_file_enabled'] = 0;
983         $params['assignfeedback_file_enabled'] = 0;
984         $params['attemptreopenmethod'] = 'manual';
985         $params['maxattempts'] = 5;
986         $instance = $generator->create_instance($params);
987         $cm = get_coursemodule_from_instance('assign', $instance->id);
988         $context = context_module::instance($cm->id);
990         $assign = new assign($context, $cm, $course);
992         $student1 = self::getDataGenerator()->create_user();
993         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
994         $this->getDataGenerator()->enrol_user($student1->id,
995                                               $course->id,
996                                               $studentrole->id);
997         // Now try a submission.
998         $this->setUser($student1);
999         $draftidonlinetext = file_get_unused_draft_itemid();
1000         $submissionpluginparams = array();
1001         $onlinetexteditorparams = array('text'=>'Yeeha!',
1002                                         'format'=>1,
1003                                         'itemid'=>$draftidonlinetext);
1004         $submissionpluginparams['onlinetext_editor'] = $onlinetexteditorparams;
1005         $submissionpluginparams['files_filemanager'] = file_get_unused_draft_itemid();
1006         $result = mod_assign_external::save_submission($instance->id, $submissionpluginparams);
1008         $this->setUser($teacher);
1009         // Add a grade and reopen the attempt.
1010         // Now try a grade.
1011         $feedbackpluginparams = array();
1012         $feedbackpluginparams['files_filemanager'] = file_get_unused_draft_itemid();
1013         $feedbackeditorparams = array('text'=>'Yeeha!',
1014                                         'format'=>1);
1015         $feedbackpluginparams['assignfeedbackcomments_editor'] = $feedbackeditorparams;
1016         $result = mod_assign_external::save_grade($instance->id,
1017                                                   $student1->id,
1018                                                   50.0,
1019                                                   -1,
1020                                                   true,
1021                                                   'released',
1022                                                   false,
1023                                                   $feedbackpluginparams);
1025         $this->setUser($student1);
1026         // Now copy the previous attempt.
1027         $result = mod_assign_external::copy_previous_attempt($instance->id);
1028         // No warnings.
1029         $this->assertEquals(0, count($result));
1031         $this->setUser($teacher);
1032         $result = mod_assign_external::get_submissions(array($instance->id));
1034         // Check we are now on the second attempt.
1035         $this->assertEquals($result['assignments'][0]['submissions'][0]['attemptnumber'], 1);
1036         // Check the plugins data is not empty.
1037         $this->assertNotEmpty($result['assignments'][0]['submissions'][0]['plugins']);
1039     }