on-demand release 2.7rc1+
[moodle.git] / mod / assign / tests / base_test.php
CommitLineData
9c986ee0
DW
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
17/**
18 * Base class for unit tests for mod_assign.
19 *
20 * @package mod_assign
21 * @category phpunit
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30require_once($CFG->dirroot . '/mod/assign/locallib.php');
31require_once($CFG->dirroot . '/mod/assign/upgradelib.php');
32
33/**
34 * Unit tests for (some of) mod/assign/locallib.php.
35 *
36 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class mod_assign_base_testcase extends advanced_testcase {
40
41 /** @const Default number of students to create */
42 const DEFAULT_STUDENT_COUNT = 3;
43 /** @const Default number of teachers to create */
44 const DEFAULT_TEACHER_COUNT = 2;
45 /** @const Default number of editing teachers to create */
46 const DEFAULT_EDITING_TEACHER_COUNT = 2;
47 /** @const Optional extra number of students to create */
48 const EXTRA_STUDENT_COUNT = 40;
e6cc5347
RT
49 /** @const Optional number of suspended students */
50 const EXTRA_SUSPENDED_COUNT = 10;
9c986ee0
DW
51 /** @const Optional extra number of teachers to create */
52 const EXTRA_TEACHER_COUNT = 5;
53 /** @const Optional extra number of editing teachers to create */
54 const EXTRA_EDITING_TEACHER_COUNT = 5;
55 /** @const Number of groups to create */
56 const GROUP_COUNT = 6;
57
58 /** @var stdClass $course New course created to hold the assignments */
59 protected $course = null;
60
61 /** @var array $teachers List of DEFAULT_TEACHER_COUNT teachers in the course*/
62 protected $teachers = null;
63
64 /** @var array $editingteachers List of DEFAULT_EDITING_TEACHER_COUNT editing teachers in the course */
65 protected $editingteachers = null;
66
67 /** @var array $students List of DEFAULT_STUDENT_COUNT students in the course*/
68 protected $students = null;
69
70 /** @var array $extrateachers List of EXTRA_TEACHER_COUNT teachers in the course*/
71 protected $extrateachers = null;
72
73 /** @var array $extraeditingteachers List of EXTRA_EDITING_TEACHER_COUNT editing teachers in the course*/
74 protected $extraeditingteachers = null;
75
76 /** @var array $extrastudents List of EXTRA_STUDENT_COUNT students in the course*/
77 protected $extrastudents = null;
78
e6cc5347
RT
79 /** @var array $extrasuspendedstudents List of EXTRA_SUSPENDED_COUNT students in the course*/
80 protected $extrasuspendedstudents = null;
81
9c986ee0
DW
82 /** @var array $groups List of 10 groups in the course */
83 protected $groups = null;
84
85 /**
86 * Setup function - we will create a course and add an assign instance to it.
87 */
88 protected function setUp() {
89 global $DB;
90
91 $this->resetAfterTest(true);
92
93 $this->course = $this->getDataGenerator()->create_course();
94 $this->teachers = array();
95 for ($i = 0; $i < self::DEFAULT_TEACHER_COUNT; $i++) {
96 array_push($this->teachers, $this->getDataGenerator()->create_user());
97 }
98
99 $this->editingteachers = array();
100 for ($i = 0; $i < self::DEFAULT_EDITING_TEACHER_COUNT; $i++) {
101 array_push($this->editingteachers, $this->getDataGenerator()->create_user());
102 }
103
104 $this->students = array();
105 for ($i = 0; $i < self::DEFAULT_STUDENT_COUNT; $i++) {
106 array_push($this->students, $this->getDataGenerator()->create_user());
107 }
108
109 $this->groups = array();
110 for ($i = 0; $i < self::GROUP_COUNT; $i++) {
111 array_push($this->groups, $this->getDataGenerator()->create_group(array('courseid'=>$this->course->id)));
112 }
113
114 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
115 foreach ($this->teachers as $i => $teacher) {
116 $this->getDataGenerator()->enrol_user($teacher->id,
117 $this->course->id,
118 $teacherrole->id);
119 groups_add_member($this->groups[$i % self::GROUP_COUNT], $teacher);
120 }
121
122 $editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
123 foreach ($this->editingteachers as $i => $editingteacher) {
124 $this->getDataGenerator()->enrol_user($editingteacher->id,
125 $this->course->id,
126 $editingteacherrole->id);
127 groups_add_member($this->groups[$i % self::GROUP_COUNT], $editingteacher);
128 }
129
130 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
131 foreach ($this->students as $i => $student) {
132 $this->getDataGenerator()->enrol_user($student->id,
133 $this->course->id,
134 $studentrole->id);
135 groups_add_member($this->groups[$i % self::GROUP_COUNT], $student);
136 }
137 }
138
139 /*
140 * For tests that make sense to use alot of data, create extra students/teachers.
141 */
df211804 142 protected function create_extra_users() {
9c986ee0
DW
143 global $DB;
144 $this->extrateachers = array();
145 for ($i = 0; $i < self::EXTRA_TEACHER_COUNT; $i++) {
146 array_push($this->extrateachers, $this->getDataGenerator()->create_user());
147 }
148
149 $this->extraeditingteachers = array();
150 for ($i = 0; $i < self::EXTRA_EDITING_TEACHER_COUNT; $i++) {
151 array_push($this->extraeditingteachers, $this->getDataGenerator()->create_user());
152 }
153
154 $this->extrastudents = array();
155 for ($i = 0; $i < self::EXTRA_STUDENT_COUNT; $i++) {
156 array_push($this->extrastudents, $this->getDataGenerator()->create_user());
157 }
158
e6cc5347
RT
159 $this->extrasuspendedstudents = array();
160 for ($i = 0; $i < self::EXTRA_SUSPENDED_COUNT; $i++) {
161 array_push($this->extrasuspendedstudents, $this->getDataGenerator()->create_user());
162 }
163
9c986ee0
DW
164 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
165 foreach ($this->extrateachers as $i => $teacher) {
166 $this->getDataGenerator()->enrol_user($teacher->id,
167 $this->course->id,
168 $teacherrole->id);
169 groups_add_member($this->groups[$i % self::GROUP_COUNT], $teacher);
170 }
171
172 $editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
173 foreach ($this->extraeditingteachers as $i => $editingteacher) {
174 $this->getDataGenerator()->enrol_user($editingteacher->id,
175 $this->course->id,
176 $editingteacherrole->id);
177 groups_add_member($this->groups[$i % self::GROUP_COUNT], $editingteacher);
178 }
179
180 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
181 foreach ($this->extrastudents as $i => $student) {
182 $this->getDataGenerator()->enrol_user($student->id,
183 $this->course->id,
184 $studentrole->id);
185 if ($i < (self::EXTRA_STUDENT_COUNT / 2)) {
186 groups_add_member($this->groups[$i % self::GROUP_COUNT], $student);
187 }
188 }
189
e6cc5347
RT
190 foreach ($this->extrasuspendedstudents as $i => $suspendedstudent) {
191 $this->getDataGenerator()->enrol_user($suspendedstudent->id,
192 $this->course->id,
193 $studentrole->id, 'manual', 0, 0, ENROL_USER_SUSPENDED);
194 if ($i < (self::EXTRA_SUSPENDED_COUNT / 2)) {
195 groups_add_member($this->groups[$i % self::GROUP_COUNT], $suspendedstudent);
196 }
197 }
9c986ee0
DW
198 }
199
200 /**
201 * Convenience function to create a testable instance of an assignment.
202 *
203 * @param array $params Array of parameters to pass to the generator
204 * @return testable_assign Testable wrapper around the assign class.
205 */
206 protected function create_instance($params=array()) {
207 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
208 $params['course'] = $this->course->id;
209 $instance = $generator->create_instance($params);
210 $cm = get_coursemodule_from_instance('assign', $instance->id);
211 $context = context_module::instance($cm->id);
212 return new testable_assign($context, $cm, $this->course);
213 }
214
215 public function test_create_instance() {
216 $this->assertNotEmpty($this->create_instance());
217 }
218
219}
220
221/**
222 * Test subclass that makes all the protected methods we want to test public.
223 */
224class testable_assign extends assign {
225
9c986ee0
DW
226 public function testable_show_intro() {
227 return parent::show_intro();
228 }
229
230 public function testable_delete_grades() {
231 return parent::delete_grades();
232 }
233
df211804
DW
234 public function testable_apply_grade_to_user($formdata, $userid, $attemptnumber) {
235 return parent::apply_grade_to_user($formdata, $userid, $attemptnumber);
9c986ee0
DW
236 }
237
89fbc202
FM
238 public function testable_format_submission_for_log(stdClass $submission) {
239 return parent::format_submission_for_log($submission);
240 }
241
9c986ee0
DW
242 public function testable_get_grading_userid_list() {
243 return parent::get_grading_userid_list();
244 }
245
246 public function testable_is_graded($userid) {
247 return parent::is_graded($userid);
248 }
249
250 public function testable_update_submission(stdClass $submission, $userid, $updatetime, $teamsubmission) {
251 return parent::update_submission($submission, $userid, $updatetime, $teamsubmission);
252 }
253
df211804
DW
254 public function testable_process_add_attempt($userid = 0) {
255 return parent::process_add_attempt($userid);
256 }
257
bd2e9829
FM
258 public function testable_process_save_quick_grades($postdata) {
259 // Ugly hack to get something into the method.
260 global $_POST;
261 $_POST = $postdata;
262 return parent::process_save_quick_grades();
263 }
264
e59a85ba
FM
265 public function testable_process_set_batch_marking_allocation($selectedusers, $markerid) {
266 // Ugly hack to get something into the method.
267 global $_POST;
268 $_POST['selectedusers'] = $selectedusers;
269 $_POST['allocatedmarker'] = $markerid;
270 return parent::process_set_batch_marking_allocation();
271 }
272
8bb213eb
FM
273 public function testable_process_set_batch_marking_workflow_state($selectedusers, $state) {
274 // Ugly hack to get something into the method.
275 global $_POST;
276 $_POST['selectedusers'] = $selectedusers;
277 $_POST['markingworkflowstate'] = $state;
278 return parent::process_set_batch_marking_workflow_state();
279 }
280
9c986ee0
DW
281 public function testable_submissions_open($userid = 0) {
282 return parent::submissions_open($userid);
283 }
284
285 public function testable_save_user_extension($userid, $extensionduedate) {
286 return parent::save_user_extension($userid, $extensionduedate);
287 }
288
289 public function testable_get_graders($userid) {
290 // Changed method from protected to public.
291 return parent::get_graders($userid);
292 }
eef4d594
MN
293
294 public function testable_view_batch_set_workflow_state() {
295 global $CFG;
296
297 require_once($CFG->dirroot . '/mod/assign/batchsetmarkingworkflowstateform.php');
298
299 // Mock submit data.
300 $data = array();
301 $data['selectedusers'] = '1';
302 mod_assign_batch_set_marking_workflow_state_form::mock_submit($data);
303
304 // Set required variables in the form - not valid just allows us to continue.
305 $formparams = array();
306 $formparams['users'] = array(1);
307 $formparams['usershtml'] = 1;
308 $formparams['cm'] = $this->get_course_module()->id;
309 $formparams['context'] = $this->get_context();
310 $formparams['markingworkflowstates'] = 1;
311 $mform = new mod_assign_batch_set_marking_workflow_state_form('', $formparams);
312
313 return parent::view_batch_set_workflow_state($mform);
314 }
81f92c22
MN
315
316 public function testable_view_batch_markingallocation() {
317 global $CFG;
318
319 require_once($CFG->dirroot . '/mod/assign/batchsetallocatedmarkerform.php');
320
321 // Mock submit data.
322 $data = array();
323 $data['selectedusers'] = '1';
324 mod_assign_batch_set_allocatedmarker_form::mock_submit($data);
325
326 // Set required variables in the form - not valid just allows us to continue.
327 $formparams = array();
328 $formparams['users'] = array(1);
329 $formparams['usershtml'] = 1;
330 $formparams['cm'] = $this->get_course_module()->id;
331 $formparams['context'] = $this->get_context();
332 $formparams['markers'] = 1;
333 $mform = new mod_assign_batch_set_allocatedmarker_form('', $formparams);
334
335 return parent::view_batch_markingallocation($mform);
336 }
9c986ee0 337}