b56ffeea956746d2db94255ea8a2d07c65d4e710
[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 a grade.
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 = 75;
87         $grade->locked = false;
88         $grade->mailed = true;
89         $DB->insert_record('assign_grades', $grade);
91         $assignmentids[] = $assign->id;
92         $result = mod_assign_external::get_grades($assignmentids);
94         // We need to execute the return values cleaning process to simulate the web service server.
95         $result = external_api::clean_returnvalue(mod_assign_external::get_grades_returns(), $result);
97         // Check that the correct grade information for the student is returned.
98         $this->assertEquals(1, count($result['assignments']));
99         $assignment = $result['assignments'][0];
100         $this->assertEquals($assign->id, $assignment['assignmentid']);
101         $this->assertEquals(1, count($assignment['grades']));
102         $grade = $assignment['grades'][0];
103         $this->assertEquals($student->id, $grade['userid']);
104         $this->assertEquals(75, $grade['grade']);
105     }
107     /**
108      * Test get_assignments
109      */
110     public function test_get_assignments () {
111         global $DB, $USER;
113         $this->resetAfterTest(true);
115         $category = self::getDataGenerator()->create_category(array(
116             'name' => 'Test category'
117         ));
119         // Create a course.
120         $course1 = self::getDataGenerator()->create_course(array(
121             'idnumber' => 'idnumbercourse1',
122             'fullname' => 'Lightwork Course 1',
123             'summary' => 'Lightwork Course 1 description',
124             'summaryformat' => FORMAT_MOODLE,
125             'category' => $category->id
126         ));
128         // Create a second course, just for testing.
129         $course2 = self::getDataGenerator()->create_course(array(
130             'idnumber' => 'idnumbercourse2',
131             'fullname' => 'Lightwork Course 2',
132             'summary' => 'Lightwork Course 2 description',
133             'summaryformat' => FORMAT_MOODLE,
134             'category' => $category->id
135         ));
137         // Create the assignment module.
138         $assign1 = self::getDataGenerator()->create_module('assign', array(
139             'course' => $course1->id,
140             'name' => 'lightwork assignment'
141         ));
143         // Create manual enrolment record.
144         $enrolid = $DB->insert_record('enrol', (object)array(
145             'enrol' => 'manual',
146             'status' => 0,
147             'courseid' => $course1->id
148         ));
150         // Create the user and give them capabilities.
151         $context = context_course::instance($course1->id);
152         $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
153         $context = context_module::instance($assign1->id);
154         $this->assignUserCapability('mod/assign:view', $context->id, $roleid);
156         // Create the user enrolment record.
157         $DB->insert_record('user_enrolments', (object)array(
158             'status' => 0,
159             'enrolid' => $enrolid,
160             'userid' => $USER->id
161         ));
163         $result = mod_assign_external::get_assignments();
165         // We need to execute the return values cleaning process to simulate the web service server.
166         $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
168         // Check the course and assignment are returned.
169         $this->assertEquals(1, count($result['courses']));
170         $course = $result['courses'][0];
171         $this->assertEquals('Lightwork Course 1', $course['fullname']);
172         $this->assertEquals(1, count($course['assignments']));
173         $assignment = $course['assignments'][0];
174         $this->assertEquals($assign1->id, $assignment['id']);
175         $this->assertEquals($course1->id, $assignment['course']);
176         $this->assertEquals('lightwork assignment', $assignment['name']);
178         $result = mod_assign_external::get_assignments(array($course1->id));
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         $this->assertEquals(1, count($result['courses']));
184         $course = $result['courses'][0];
185         $this->assertEquals('Lightwork Course 1', $course['fullname']);
186         $this->assertEquals(1, count($course['assignments']));
187         $assignment = $course['assignments'][0];
188         $this->assertEquals($assign1->id, $assignment['id']);
189         $this->assertEquals($course1->id, $assignment['course']);
190         $this->assertEquals('lightwork assignment', $assignment['name']);
192         $result = mod_assign_external::get_assignments(array($course2->id));
194         // We need to execute the return values cleaning process to simulate the web service server.
195         $result = external_api::clean_returnvalue(mod_assign_external::get_assignments_returns(), $result);
197         $this->assertEquals(0, count($result['courses']));
198         $this->assertEquals(1, count($result['warnings']));
199     }
201     /**
202      * Test get_submissions
203      */
204     public function test_get_submissions () {
205         global $DB, $USER;
207         $this->resetAfterTest(true);
208         // Create a course and assignment.
209         $coursedata['idnumber'] = 'idnumbercourse1';
210         $coursedata['fullname'] = 'Lightwork Course 1';
211         $coursedata['summary'] = 'Lightwork Course 1 description';
212         $coursedata['summaryformat'] = FORMAT_MOODLE;
213         $course1 = self::getDataGenerator()->create_course($coursedata);
215         $assigndata['course'] = $course1->id;
216         $assigndata['name'] = 'lightwork assignment';
218         $assign1 = self::getDataGenerator()->create_module('assign', $assigndata);
220         // Create a student with an online text submission.
221         $student = self::getDataGenerator()->create_user();
222         $submission = new stdClass();
223         $submission->assignment = $assign1->id;
224         $submission->userid = $student->id;
225         $submission->timecreated = time();
226         $submission->timemodified = $submission->timecreated;
227         $submission->status = 'submitted';
228         $sid = $DB->insert_record('assign_submission', $submission);
229         $submission->id = $sid;
231         $onlinetextsubmission = new stdClass();
232         $onlinetextsubmission->onlinetext = "<p>online test text</p>";
233         $onlinetextsubmission->onlineformat = 1;
234         $onlinetextsubmission->submission = $submission->id;
235         $onlinetextsubmission->assignment = $assign1->id;
236         $DB->insert_record('assignsubmission_onlinetext', $onlinetextsubmission);
238         // Create manual enrolment record.
239         $manual_enrol_data['enrol'] = 'manual';
240         $manual_enrol_data['status'] = 0;
241         $manual_enrol_data['courseid'] = $course1->id;
242         $enrolid = $DB->insert_record('enrol', $manual_enrol_data);
244         // Create a teacher and give them capabilities.
245         $context = context_course::instance($course1->id);
246         $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
247         $context = context_module::instance($assign1->id);
248         $this->assignUserCapability('mod/assign:grade', $context->id, $roleid);
250         // Create the teacher's enrolment record.
251         $user_enrolment_data['status'] = 0;
252         $user_enrolment_data['enrolid'] = $enrolid;
253         $user_enrolment_data['userid'] = $USER->id;
254         $DB->insert_record('user_enrolments', $user_enrolment_data);
256         $assignmentids[] = $assign1->id;
257         $result = mod_assign_external::get_submissions($assignmentids);
259         // Check the online text submission is returned.
260         $this->assertEquals(1, count($result['assignments']));
261         $assignment = $result['assignments'][0];
262         $this->assertEquals($assign1->id, $assignment['assignmentid']);
263         $this->assertEquals(1, count($assignment['submissions']));
264         $submission = $assignment['submissions'][0];
265         $this->assertEquals($sid, $submission['id']);
266         $this->assertGreaterThanOrEqual(3, count($submission['plugins']));
267         $plugins = $submission['plugins'];
268         foreach ($plugins as $plugin) {
269             $foundonlinetext = false;
270             if ($plugin['type'] == 'onlinetext') {
271                 $foundonlinetext = true;
272                 break;
273             }
274         }
275         $this->assertTrue($foundonlinetext);
276     }