weekly release 2.6dev
[moodle.git] / mod / assign / tests / externallib_test.php
CommitLineData
1f8c8f61
PC
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17defined('MOODLE_INTERNAL') || die();
18
19global $CFG;
20
21require_once($CFG->dirroot . '/webservice/tests/helpers.php');
22
23/**
24 * External mod assign functions unit tests
25 *
26 * @package mod_assign
27 * @category external
28 * @copyright 2012 Paul Charsley
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 */
31class mod_assign_external_testcase extends externallib_advanced_testcase {
32
33 /**
34 * Tests set up
35 */
36 protected function setUp() {
37 global $CFG;
38 require_once($CFG->dirroot . '/mod/assign/externallib.php');
39 }
40
41 /**
42 * Test get_grades
43 */
44 public function test_get_grades () {
45 global $DB, $USER;
46
47 $this->resetAfterTest(true);
48 // Create a course and assignment.
49 $coursedata['idnumber'] = 'idnumbercourse';
50 $coursedata['fullname'] = 'Lightwork Course';
51 $coursedata['summary'] = 'Lightwork Course description';
52 $coursedata['summaryformat'] = FORMAT_MOODLE;
53 $course = self::getDataGenerator()->create_course($coursedata);
54
55 $assigndata['course'] = $course->id;
56 $assigndata['name'] = 'lightwork assignment';
57
58 $assign = self::getDataGenerator()->create_module('assign', $assigndata);
59
60 // Create a manual enrolment record.
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);
65
66 // Create a teacher and give them capabilities.
67 $context = context_course::instance($course->id);
68 $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
69 $context = context_module::instance($assign->id);
70 $this->assignUserCapability('mod/assign:grade', $context->id, $roleid);
71
72 // Create the teacher's enrolment record.
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);
77
a13fbf5f 78 // Create a student and give them 2 grades (for 2 attempts).
1f8c8f61 79 $student = self::getDataGenerator()->create_user();
a13fbf5f
DW
80 $grade = new stdClass();
81 $grade->assignment = $assign->id;
82 $grade->userid = $student->id;
83 $grade->timecreated = time();
84 $grade->timemodified = $grade->timecreated;
85 $grade->grader = $USER->id;
86 $grade->grade = 50;
a13fbf5f
DW
87 $grade->attemptnumber = 0;
88 $DB->insert_record('assign_grades', $grade);
89
1f8c8f61
PC
90 $grade = new stdClass();
91 $grade->assignment = $assign->id;
92 $grade->userid = $student->id;
93 $grade->timecreated = time();
94 $grade->timemodified = $grade->timecreated;
95 $grade->grader = $USER->id;
96 $grade->grade = 75;
a13fbf5f 97 $grade->attemptnumber = 1;
1f8c8f61
PC
98 $DB->insert_record('assign_grades', $grade);
99
100 $assignmentids[] = $assign->id;
101 $result = mod_assign_external::get_grades($assignmentids);
102
fb695f6e
JM
103 // We need to execute the return values cleaning process to simulate the web service server.
104 $result = external_api::clean_returnvalue(mod_assign_external::get_grades_returns(), $result);
105
1f8c8f61
PC
106 // Check that the correct grade information for the student is returned.
107 $this->assertEquals(1, count($result['assignments']));
108 $assignment = $result['assignments'][0];
109 $this->assertEquals($assign->id, $assignment['assignmentid']);
a13fbf5f 110 // Should only get the last grade for this student.
1f8c8f61
PC
111 $this->assertEquals(1, count($assignment['grades']));
112 $grade = $assignment['grades'][0];
113 $this->assertEquals($student->id, $grade['userid']);
13e82f1c 114 // Should be the last grade (not the first).
1f8c8f61
PC
115 $this->assertEquals(75, $grade['grade']);
116 }
117
1378838e
PC
118 /**
119 * Test get_assignments
120 */
121 public function test_get_assignments () {
122 global $DB, $USER;
123
124 $this->resetAfterTest(true);
125
126 $category = self::getDataGenerator()->create_category(array(
127 'name' => 'Test category'
128 ));
129
130 // Create a course.
131 $course1 = self::getDataGenerator()->create_course(array(
132 'idnumber' => 'idnumbercourse1',
133 'fullname' => 'Lightwork Course 1',
134 'summary' => 'Lightwork Course 1 description',
135 'summaryformat' => FORMAT_MOODLE,
136 'category' => $category->id
137 ));
138
139 // Create a second course, just for testing.
140 $course2 = self::getDataGenerator()->create_course(array(
141 'idnumber' => 'idnumbercourse2',
142 'fullname' => 'Lightwork Course 2',
143 'summary' => 'Lightwork Course 2 description',
144 'summaryformat' => FORMAT_MOODLE,
145 'category' => $category->id
146 ));
147
148 // Create the assignment module.
149 $assign1 = self::getDataGenerator()->create_module('assign', array(
150 'course' => $course1->id,
151 'name' => 'lightwork assignment'
152 ));
153
154 // Create manual enrolment record.
155 $enrolid = $DB->insert_record('enrol', (object)array(
156 'enrol' => 'manual',
157 'status' => 0,
158 'courseid' => $course1->id
159 ));
160
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);
166
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 ));
173
174 $result = mod_assign_external::get_assignments();
fb695f6e
JM
175
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);
178
1378838e
PC
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']);
188
189 $result = mod_assign_external::get_assignments(array($course1->id));
fb695f6e
JM
190
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);
193
1378838e
PC
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']);
202
203 $result = mod_assign_external::get_assignments(array($course2->id));
fb695f6e
JM
204
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);
207
1378838e
PC
208 $this->assertEquals(0, count($result['courses']));
209 $this->assertEquals(1, count($result['warnings']));
210 }
c144959c
PC
211
212 /**
213 * Test get_submissions
214 */
215 public function test_get_submissions () {
216 global $DB, $USER;
217
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);
225
226 $assigndata['course'] = $course1->id;
227 $assigndata['name'] = 'lightwork assignment';
228
229 $assign1 = self::getDataGenerator()->create_module('assign', $assigndata);
230
231 // Create a student with an online text submission.
a13fbf5f 232 // First attempt.
c144959c
PC
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;
a13fbf5f
DW
239 $submission->status = 'draft';
240 $submission->attemptnumber = 0;
241 $sid = $DB->insert_record('assign_submission', $submission);
242
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;
c144959c 249 $submission->status = 'submitted';
a13fbf5f 250 $submission->attemptnumber = 1;
c144959c
PC
251 $sid = $DB->insert_record('assign_submission', $submission);
252 $submission->id = $sid;
253
254 $onlinetextsubmission = new stdClass();
9f780282 255 $onlinetextsubmission->onlinetext = "<p>online test text</p>";
c144959c
PC
256 $onlinetextsubmission->onlineformat = 1;
257 $onlinetextsubmission->submission = $submission->id;
258 $onlinetextsubmission->assignment = $assign1->id;
259 $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission);
260
261 // Create manual enrolment record.
262 $manual_enrol_data['enrol'] = 'manual';
263 $manual_enrol_data['status'] = 0;
264 $manual_enrol_data['courseid'] = $course1->id;
265 $enrolid = $DB->insert_record('enrol', $manual_enrol_data);
266
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);
272
273 // Create the teacher's enrolment record.
274 $user_enrolment_data['status'] = 0;
275 $user_enrolment_data['enrolid'] = $enrolid;
276 $user_enrolment_data['userid'] = $USER->id;
277 $DB->insert_record('user_enrolments', $user_enrolment_data);
278
279 $assignmentids[] = $assign1->id;
280 $result = mod_assign_external::get_submissions($assignmentids);
281
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 }
1f8c8f61 300}