weekly on-sync release 2.5dev
[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
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);
90
91 $assignmentids[] = $assign->id;
92 $result = mod_assign_external::get_grades($assignmentids);
93
94 // Check that the correct grade information for the student is returned.
95 $this->assertEquals(1, count($result['assignments']));
96 $assignment = $result['assignments'][0];
97 $this->assertEquals($assign->id, $assignment['assignmentid']);
98 $this->assertEquals(1, count($assignment['grades']));
99 $grade = $assignment['grades'][0];
100 $this->assertEquals($student->id, $grade['userid']);
101 $this->assertEquals(75, $grade['grade']);
102 }
103
1378838e
PC
104 /**
105 * Test get_assignments
106 */
107 public function test_get_assignments () {
108 global $DB, $USER;
109
110 $this->resetAfterTest(true);
111
112 $category = self::getDataGenerator()->create_category(array(
113 'name' => 'Test category'
114 ));
115
116 // Create a course.
117 $course1 = self::getDataGenerator()->create_course(array(
118 'idnumber' => 'idnumbercourse1',
119 'fullname' => 'Lightwork Course 1',
120 'summary' => 'Lightwork Course 1 description',
121 'summaryformat' => FORMAT_MOODLE,
122 'category' => $category->id
123 ));
124
125 // Create a second course, just for testing.
126 $course2 = self::getDataGenerator()->create_course(array(
127 'idnumber' => 'idnumbercourse2',
128 'fullname' => 'Lightwork Course 2',
129 'summary' => 'Lightwork Course 2 description',
130 'summaryformat' => FORMAT_MOODLE,
131 'category' => $category->id
132 ));
133
134 // Create the assignment module.
135 $assign1 = self::getDataGenerator()->create_module('assign', array(
136 'course' => $course1->id,
137 'name' => 'lightwork assignment'
138 ));
139
140 // Create manual enrolment record.
141 $enrolid = $DB->insert_record('enrol', (object)array(
142 'enrol' => 'manual',
143 'status' => 0,
144 'courseid' => $course1->id
145 ));
146
147 // Create the user and give them capabilities.
148 $context = context_course::instance($course1->id);
149 $roleid = $this->assignUserCapability('moodle/course:view', $context->id);
150 $context = context_module::instance($assign1->id);
151 $this->assignUserCapability('mod/assign:view', $context->id, $roleid);
152
153 // Create the user enrolment record.
154 $DB->insert_record('user_enrolments', (object)array(
155 'status' => 0,
156 'enrolid' => $enrolid,
157 'userid' => $USER->id
158 ));
159
160 $result = mod_assign_external::get_assignments();
161 // Check the course and assignment are returned.
162 $this->assertEquals(1, count($result['courses']));
163 $course = $result['courses'][0];
164 $this->assertEquals('Lightwork Course 1', $course['fullname']);
165 $this->assertEquals(1, count($course['assignments']));
166 $assignment = $course['assignments'][0];
167 $this->assertEquals($assign1->id, $assignment['id']);
168 $this->assertEquals($course1->id, $assignment['course']);
169 $this->assertEquals('lightwork assignment', $assignment['name']);
170
171 $result = mod_assign_external::get_assignments(array($course1->id));
172 $this->assertEquals(1, count($result['courses']));
173 $course = $result['courses'][0];
174 $this->assertEquals('Lightwork Course 1', $course['fullname']);
175 $this->assertEquals(1, count($course['assignments']));
176 $assignment = $course['assignments'][0];
177 $this->assertEquals($assign1->id, $assignment['id']);
178 $this->assertEquals($course1->id, $assignment['course']);
179 $this->assertEquals('lightwork assignment', $assignment['name']);
180
181 $result = mod_assign_external::get_assignments(array($course2->id));
182 $this->assertEquals(0, count($result['courses']));
183 $this->assertEquals(1, count($result['warnings']));
184 }
1f8c8f61 185}