45823ff24f0fda831525165c37e58e97947d4518
[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         ));
154         // Create manual enrolment record.
155         $enrolid = $DB->insert_record('enrol', (object)array(
156             'enrol' => 'manual',
157             'status' => 0,
158             'courseid' => $course1->id
159         ));
161         // Create the user and give them capabilities.
162         $context = context_course::instance($course1->id);
163         $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
164         $context = context_module::instance($assign1->id);
165         $this->assignUserCapability('mod/assign:view', $context->id, $roleid);
167         // Create the user enrolment record.
168         $DB->insert_record('user_enrolments', (object)array(
169             'status' => 0,
170             'enrolid' => $enrolid,
171             'userid' => $USER->id
172         ));
174         $result = mod_assign_external::get_assignments();
176         // We need to execute the return values cleaning process to simulate the web service server.
177         $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
179         // Check the course and assignment are returned.
180         $this->assertEquals(1, count($result['courses']));
181         $course = $result['courses'][0];
182         $this->assertEquals('Lightwork Course 1', $course['fullname']);
183         $this->assertEquals(1, count($course['assignments']));
184         $assignment = $course['assignments'][0];
185         $this->assertEquals($assign1->id, $assignment['id']);
186         $this->assertEquals($course1->id, $assignment['course']);
187         $this->assertEquals('lightwork assignment', $assignment['name']);
189         $result = mod_assign_external::get_assignments(array($course1->id));
191         // We need to execute the return values cleaning process to simulate the web service server.
192         $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
194         $this->assertEquals(1, count($result['courses']));
195         $course = $result['courses'][0];
196         $this->assertEquals('Lightwork Course 1', $course['fullname']);
197         $this->assertEquals(1, count($course['assignments']));
198         $assignment = $course['assignments'][0];
199         $this->assertEquals($assign1->id, $assignment['id']);
200         $this->assertEquals($course1->id, $assignment['course']);
201         $this->assertEquals('lightwork assignment', $assignment['name']);
203         $result = mod_assign_external::get_assignments(array($course2->id));
205         // We need to execute the return values cleaning process to simulate the web service server.
206         $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
208         $this->assertEquals(0, count($result['courses']));
209         $this->assertEquals(1, count($result['warnings']));
210     }
212     /**
213      * Test get_submissions
214      */
215     public function test_get_submissions () {
216         global $DB, $USER;
218         $this->resetAfterTest(true);
219         // Create a course and assignment.
220         $coursedata['idnumber'] = 'idnumbercourse1';
221         $coursedata['fullname'] = 'Lightwork Course 1';
222         $coursedata['summary'] = 'Lightwork Course 1 description';
223         $coursedata['summaryformat'] = FORMAT_MOODLE;
224         $course1 = self::getDataGenerator()->create_course($coursedata);
226         $assigndata['course'] = $course1->id;
227         $assigndata['name'] = 'lightwork assignment';
229         $assign1 = self::getDataGenerator()->create_module('assign', $assigndata);
231         // Create a student with an online text submission.
232         // First attempt.
233         $student = self::getDataGenerator()->create_user();
234         $submission = new stdClass();
235         $submission->assignment = $assign1->id;
236         $submission->userid = $student->id;
237         $submission->timecreated = time();
238         $submission->timemodified = $submission->timecreated;
239         $submission->status = 'draft';
240         $submission->attemptnumber = 0;
241         $sid = $DB->insert_record('assign_submission', $submission);
243         // Second attempt.
244         $submission = new stdClass();
245         $submission->assignment = $assign1->id;
246         $submission->userid = $student->id;
247         $submission->timecreated = time();
248         $submission->timemodified = $submission->timecreated;
249         $submission->status = 'submitted';
250         $submission->attemptnumber = 1;
251         $sid = $DB->insert_record('assign_submission', $submission);
252         $submission->id = $sid;
254         $onlinetextsubmission = new stdClass();
255         $onlinetextsubmission->onlinetext = "<p>online test text</p>";
256         $onlinetextsubmission->onlineformat = 1;
257         $onlinetextsubmission->submission = $submission->id;
258         $onlinetextsubmission->assignment = $assign1->id;
259         $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission);
261         // Create manual enrolment record.
262         $manualenroldata['enrol'] = 'manual';
263         $manualenroldata['status'] = 0;
264         $manualenroldata['courseid'] = $course1->id;
265         $enrolid = $DB->insert_record('enrol', $manualenroldata);
267         // Create a teacher and give them capabilities.
268         $context = context_course::instance($course1->id);
269         $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
270         $context = context_module::instance($assign1->id);
271         $this->assignUserCapability('mod/assign:grade', $context->id, $roleid);
273         // Create the teacher's enrolment record.
274         $userenrolmentdata['status'] = 0;
275         $userenrolmentdata['enrolid'] = $enrolid;
276         $userenrolmentdata['userid'] = $USER->id;
277         $DB->insert_record('user_enrolments', $userenrolmentdata);
279         $assignmentids[] = $assign1->id;
280         $result = mod_assign_external::get_submissions($assignmentids);
282         // Check the online text submission is returned.
283         $this->assertEquals(1, count($result['assignments']));
284         $assignment = $result['assignments'][0];
285         $this->assertEquals($assign1->id, $assignment['assignmentid']);
286         $this->assertEquals(1, count($assignment['submissions']));
287         $submission = $assignment['submissions'][0];
288         $this->assertEquals($sid, $submission['id']);
289         $this->assertGreaterThanOrEqual(3, count($submission['plugins']));
290         $plugins = $submission['plugins'];
291         foreach ($plugins as $plugin) {
292             $foundonlinetext = false;
293             if ($plugin['type'] == 'onlinetext') {
294                 $foundonlinetext = true;
295                 break;
296             }
297         }
298         $this->assertTrue($foundonlinetext);
299     }
301     /**
302      * Test get_user_flags
303      */
304     public function test_get_user_flags () {
305         global $DB, $USER;
307         $this->resetAfterTest(true);
308         // Create a course and assignment.
309         $coursedata['idnumber'] = 'idnumbercourse';
310         $coursedata['fullname'] = 'Lightwork Course';
311         $coursedata['summary'] = 'Lightwork Course description';
312         $coursedata['summaryformat'] = FORMAT_MOODLE;
313         $course = self::getDataGenerator()->create_course($coursedata);
315         $assigndata['course'] = $course->id;
316         $assigndata['name'] = 'lightwork assignment';
318         $assign = self::getDataGenerator()->create_module('assign', $assigndata);
320         // Create a manual enrolment record.
321         $manualenroldata['enrol'] = 'manual';
322         $manualenroldata['status'] = 0;
323         $manualenroldata['courseid'] = $course->id;
324         $enrolid = $DB->insert_record('enrol', $manualenroldata);
326         // Create a teacher and give them capabilities.
327         $context = context_course::instance($course->id);
328         $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
329         $context = context_module::instance($assign->id);
330         $this->assignUserCapability('mod/assign:grade', $context->id, $roleid);
332         // Create the teacher's enrolment record.
333         $userenrolmentdata['status'] = 0;
334         $userenrolmentdata['enrolid'] = $enrolid;
335         $userenrolmentdata['userid'] = $USER->id;
336         $DB->insert_record('user_enrolments', $userenrolmentdata);
338         // Create a student and give them a user flag record.
339         $student = self::getDataGenerator()->create_user();
340         $userflag = new stdClass();
341         $userflag->assignment = $assign->id;
342         $userflag->userid = $student->id;
343         $userflag->locked = 0;
344         $userflag->mailed = 0;
345         $userflag->extensionduedate = 0;
346         $userflag->workflowstate = 'inmarking';
347         $userflag->allocatedmarker = $USER->id;
349         $DB->insert_record('assign_user_flags', $userflag);
351         $assignmentids[] = $assign->id;
352         $result = mod_assign_external::get_user_flags($assignmentids);
354         // We need to execute the return values cleaning process to simulate the web service server.
355         $result = external_api::clean_returnvalue(mod_assign_external::get_user_flags_returns(), $result);
357         // Check that the correct user flag information for the student is returned.
358         $this->assertEquals(1, count($result['assignments']));
359         $assignment = $result['assignments'][0];
360         $this->assertEquals($assign->id, $assignment['assignmentid']);
361         // Should be one user flag record.
362         $this->assertEquals(1, count($assignment['userflags']));
363         $userflag = $assignment['userflags'][0];
364         $this->assertEquals($student->id, $userflag['userid']);
365         $this->assertEquals(0, $userflag['locked']);
366         $this->assertEquals(0, $userflag['mailed']);
367         $this->assertEquals(0, $userflag['extensionduedate']);
368         $this->assertEquals('inmarking', $userflag['workflowstate']);
369         $this->assertEquals($USER->id, $userflag['allocatedmarker']);
370     }
372     /**
373      * Test get_user_mappings
374      */
375     public function test_get_user_mappings () {
376         global $DB, $USER;
378         $this->resetAfterTest(true);
379         // Create a course and assignment.
380         $coursedata['idnumber'] = 'idnumbercourse';
381         $coursedata['fullname'] = 'Lightwork Course';
382         $coursedata['summary'] = 'Lightwork Course description';
383         $coursedata['summaryformat'] = FORMAT_MOODLE;
384         $course = self::getDataGenerator()->create_course($coursedata);
386         $assigndata['course'] = $course->id;
387         $assigndata['name'] = 'lightwork assignment';
389         $assign = self::getDataGenerator()->create_module('assign', $assigndata);
391         // Create a manual enrolment record.
392         $manualenroldata['enrol'] = 'manual';
393         $manualenroldata['status'] = 0;
394         $manualenroldata['courseid'] = $course->id;
395         $enrolid = $DB->insert_record('enrol', $manualenroldata);
397         // Create a teacher and give them capabilities.
398         $context = context_course::instance($course->id);
399         $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
400         $context = context_module::instance($assign->id);
401         $this->assignUserCapability('mod/assign:revealidentities', $context->id, $roleid);
403         // Create the teacher's enrolment record.
404         $userenrolmentdata['status'] = 0;
405         $userenrolmentdata['enrolid'] = $enrolid;
406         $userenrolmentdata['userid'] = $USER->id;
407         $DB->insert_record('user_enrolments', $userenrolmentdata);
409         // Create a student and give them a user mapping record.
410         $student = self::getDataGenerator()->create_user();
411         $mapping = new stdClass();
412         $mapping->assignment = $assign->id;
413         $mapping->userid = $student->id;
415         $DB->insert_record('assign_user_mapping', $mapping);
417         $assignmentids[] = $assign->id;
418         $result = mod_assign_external::get_user_mappings($assignmentids);
420         // We need to execute the return values cleaning process to simulate the web service server.
421         $result = external_api::clean_returnvalue(mod_assign_external::get_user_mappings_returns(), $result);
423         // Check that the correct user mapping information for the student is returned.
424         $this->assertEquals(1, count($result['assignments']));
425         $assignment = $result['assignments'][0];
426         $this->assertEquals($assign->id, $assignment['assignmentid']);
427         // Should be one user mapping record.
428         $this->assertEquals(1, count($assignment['mappings']));
429         $mapping = $assignment['mappings'][0];
430         $this->assertEquals($student->id, $mapping['userid']);
431     }