MDL-36804 Assignment - Make sure webservice only returns the last submission/grade.
[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         $manual_enrol_data['enrol'] = 'manual';
62         $manual_enrol_data['status'] = 0;
63         $manual_enrol_data['courseid'] = $course->id;
64         $enrolid = $DB->insert_record('enrol', $manual_enrol_data);
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         $user_enrolment_data['status'] = 0;
74         $user_enrolment_data['enrolid'] = $enrolid;
75         $user_enrolment_data['userid'] = $USER->id;
76         $DB->insert_record('user_enrolments', $user_enrolment_data);
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->locked = false;
88         $grade->mailed = true;
89         $grade->attemptnumber = 0;
90         $DB->insert_record('assign_grades', $grade);
92         $grade = new stdClass();
93         $grade->assignment = $assign->id;
94         $grade->userid = $student->id;
95         $grade->timecreated = time();
96         $grade->timemodified = $grade->timecreated;
97         $grade->grader = $USER->id;
98         $grade->grade = 75;
99         $grade->locked = false;
100         $grade->mailed = true;
101         $grade->attemptnumber = 1;
102         $DB->insert_record('assign_grades', $grade);
104         $assignmentids[] = $assign->id;
105         $result = mod_assign_external::get_grades($assignmentids);
107         // We need to execute the return values cleaning process to simulate the web service server.
108         $result = external_api::clean_returnvalue(mod_assign_external::get_grades_returns(), $result);
110         // Check that the correct grade information for the student is returned.
111         $this->assertEquals(1, count($result['assignments']));
112         $assignment = $result['assignments'][0];
113         $this->assertEquals($assign->id, $assignment['assignmentid']);
114         // Should only get the last grade for this student.
115         $this->assertEquals(1, count($assignment['grades']));
116         $grade = $assignment['grades'][0];
117         $this->assertEquals($student->id, $grade['userid']);
118         // Should be the last grade (not the first)
119         $this->assertEquals(75, $grade['grade']);
120     }
122     /**
123      * Test get_assignments
124      */
125     public function test_get_assignments () {
126         global $DB, $USER;
128         $this->resetAfterTest(true);
130         $category = self::getDataGenerator()->create_category(array(
131             'name' => 'Test category'
132         ));
134         // Create a course.
135         $course1 = self::getDataGenerator()->create_course(array(
136             'idnumber' => 'idnumbercourse1',
137             'fullname' => 'Lightwork Course 1',
138             'summary' => 'Lightwork Course 1 description',
139             'summaryformat' => FORMAT_MOODLE,
140             'category' => $category->id
141         ));
143         // Create a second course, just for testing.
144         $course2 = self::getDataGenerator()->create_course(array(
145             'idnumber' => 'idnumbercourse2',
146             'fullname' => 'Lightwork Course 2',
147             'summary' => 'Lightwork Course 2 description',
148             'summaryformat' => FORMAT_MOODLE,
149             'category' => $category->id
150         ));
152         // Create the assignment module.
153         $assign1 = self::getDataGenerator()->create_module('assign', array(
154             'course' => $course1->id,
155             'name' => 'lightwork assignment'
156         ));
158         // Create manual enrolment record.
159         $enrolid = $DB->insert_record('enrol', (object)array(
160             'enrol' => 'manual',
161             'status' => 0,
162             'courseid' => $course1->id
163         ));
165         // Create the user and give them capabilities.
166         $context = context_course::instance($course1->id);
167         $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
168         $context = context_module::instance($assign1->id);
169         $this->assignUserCapability('mod/assign:view', $context->id, $roleid);
171         // Create the user enrolment record.
172         $DB->insert_record('user_enrolments', (object)array(
173             'status' => 0,
174             'enrolid' => $enrolid,
175             'userid' => $USER->id
176         ));
178         $result = mod_assign_external::get_assignments();
180         // We need to execute the return values cleaning process to simulate the web service server.
181         $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
183         // Check the course and assignment are returned.
184         $this->assertEquals(1, count($result['courses']));
185         $course = $result['courses'][0];
186         $this->assertEquals('Lightwork Course 1', $course['fullname']);
187         $this->assertEquals(1, count($course['assignments']));
188         $assignment = $course['assignments'][0];
189         $this->assertEquals($assign1->id, $assignment['id']);
190         $this->assertEquals($course1->id, $assignment['course']);
191         $this->assertEquals('lightwork assignment', $assignment['name']);
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']);
207         $result = mod_assign_external::get_assignments(array($course2->id));
209         // We need to execute the return values cleaning process to simulate the web service server.
210         $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
212         $this->assertEquals(0, count($result['courses']));
213         $this->assertEquals(1, count($result['warnings']));
214     }
216     /**
217      * Test get_submissions
218      */
219     public function test_get_submissions () {
220         global $DB, $USER;
222         $this->resetAfterTest(true);
223         // Create a course and assignment.
224         $coursedata['idnumber'] = 'idnumbercourse1';
225         $coursedata['fullname'] = 'Lightwork Course 1';
226         $coursedata['summary'] = 'Lightwork Course 1 description';
227         $coursedata['summaryformat'] = FORMAT_MOODLE;
228         $course1 = self::getDataGenerator()->create_course($coursedata);
230         $assigndata['course'] = $course1->id;
231         $assigndata['name'] = 'lightwork assignment';
233         $assign1 = self::getDataGenerator()->create_module('assign', $assigndata);
235         // Create a student with an online text submission.
236         // First attempt.
237         $student = self::getDataGenerator()->create_user();
238         $submission = new stdClass();
239         $submission->assignment = $assign1->id;
240         $submission->userid = $student->id;
241         $submission->timecreated = time();
242         $submission->timemodified = $submission->timecreated;
243         $submission->status = 'draft';
244         $submission->attemptnumber = 0;
245         $sid = $DB->insert_record('assign_submission', $submission);
247         // Second attempt.
248         $submission = new stdClass();
249         $submission->assignment = $assign1->id;
250         $submission->userid = $student->id;
251         $submission->timecreated = time();
252         $submission->timemodified = $submission->timecreated;
253         $submission->status = 'submitted';
254         $submission->attemptnumber = 1;
255         $sid = $DB->insert_record('assign_submission', $submission);
256         $submission->id = $sid;
258         $onlinetextsubmission = new stdClass();
259         $onlinetextsubmission->onlinetext = "<p>online test text</p>";
260         $onlinetextsubmission->onlineformat = 1;
261         $onlinetextsubmission->submission = $submission->id;
262         $onlinetextsubmission->assignment = $assign1->id;
263         $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission);
265         // Create manual enrolment record.
266         $manual_enrol_data['enrol'] = 'manual';
267         $manual_enrol_data['status'] = 0;
268         $manual_enrol_data['courseid'] = $course1->id;
269         $enrolid = $DB->insert_record('enrol', $manual_enrol_data);
271         // Create a teacher and give them capabilities.
272         $context = context_course::instance($course1->id);
273         $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
274         $context = context_module::instance($assign1->id);
275         $this->assignUserCapability('mod/assign:grade', $context->id, $roleid);
277         // Create the teacher's enrolment record.
278         $user_enrolment_data['status'] = 0;
279         $user_enrolment_data['enrolid'] = $enrolid;
280         $user_enrolment_data['userid'] = $USER->id;
281         $DB->insert_record('user_enrolments', $user_enrolment_data);
283         $assignmentids[] = $assign1->id;
284         $result = mod_assign_external::get_submissions($assignmentids);
286         // Check the online text submission is returned.
287         $this->assertEquals(1, count($result['assignments']));
288         $assignment = $result['assignments'][0];
289         $this->assertEquals($assign1->id, $assignment['assignmentid']);
290         $this->assertEquals(1, count($assignment['submissions']));
291         $submission = $assignment['submissions'][0];
292         $this->assertEquals($sid, $submission['id']);
293         $this->assertGreaterThanOrEqual(3, count($submission['plugins']));
294         $plugins = $submission['plugins'];
295         foreach ($plugins as $plugin) {
296             $foundonlinetext = false;
297             if ($plugin['type'] == 'onlinetext') {
298                 $foundonlinetext = true;
299                 break;
300             }
301         }
302         $this->assertTrue($foundonlinetext);
303     }