MDL-49852 mod_assign: Unit tests for activity completion.
[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
0cf8082a 93 $this->course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
9c986ee0
DW
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 265 public function testable_process_set_batch_marking_allocation($selectedusers, $markerid) {
c462c35b
JF
266 global $CFG;
267 require_once($CFG->dirroot . '/mod/assign/batchsetallocatedmarkerform.php');
268
269 // Simulate the form submission.
270 $data = array();
271 $data['id'] = $this->get_course_module()->id;
272 $data['selectedusers'] = $selectedusers;
273 $data['allocatedmarker'] = $markerid;
274 $data['action'] = 'setbatchmarkingallocation';
275 mod_assign_batch_set_allocatedmarker_form::mock_submit($data);
276
e59a85ba
FM
277 return parent::process_set_batch_marking_allocation();
278 }
279
8bb213eb 280 public function testable_process_set_batch_marking_workflow_state($selectedusers, $state) {
c462c35b
JF
281 global $CFG;
282 require_once($CFG->dirroot . '/mod/assign/batchsetmarkingworkflowstateform.php');
283
284 // Simulate the form submission.
285 $data = array();
286 $data['id'] = $this->get_course_module()->id;
287 $data['selectedusers'] = $selectedusers;
288 $data['markingworkflowstate'] = $state;
289 $data['action'] = 'setbatchmarkingworkflowstate';
290 mod_assign_batch_set_marking_workflow_state_form::mock_submit($data);
291
8bb213eb
FM
292 return parent::process_set_batch_marking_workflow_state();
293 }
294
9c986ee0
DW
295 public function testable_submissions_open($userid = 0) {
296 return parent::submissions_open($userid);
297 }
298
299 public function testable_save_user_extension($userid, $extensionduedate) {
300 return parent::save_user_extension($userid, $extensionduedate);
301 }
302
303 public function testable_get_graders($userid) {
304 // Changed method from protected to public.
305 return parent::get_graders($userid);
306 }
eef4d594 307
83360c8d
S
308 public function testable_get_notifiable_users($userid) {
309 return parent::get_notifiable_users($userid);
310 }
311
c462c35b 312 public function testable_view_batch_set_workflow_state($selectedusers) {
bb690849
DW
313 global $PAGE;
314 $PAGE->set_url('/mod/assign/view.php');
c462c35b 315 $mform = $this->testable_grading_batch_operations_form('setmarkingworkflowstate', $selectedusers);
eef4d594
MN
316 return parent::view_batch_set_workflow_state($mform);
317 }
81f92c22 318
c462c35b 319 public function testable_view_batch_markingallocation($selectedusers) {
bb690849
DW
320 global $PAGE;
321 $PAGE->set_url('/mod/assign/view.php');
c462c35b
JF
322 $mform = $this->testable_grading_batch_operations_form('setmarkingallocation', $selectedusers);
323 return parent::view_batch_markingallocation($mform);
324 }
325
326 public function testable_grading_batch_operations_form($operation, $selectedusers) {
81f92c22
MN
327 global $CFG;
328
c462c35b 329 require_once($CFG->dirroot . '/mod/assign/gradingbatchoperationsform.php');
81f92c22 330
c462c35b 331 // Mock submit the grading operations form.
81f92c22 332 $data = array();
c462c35b
JF
333 $data['id'] = $this->get_course_module()->id;
334 $data['selectedusers'] = $selectedusers;
335 $data['returnaction'] = 'grading';
336 $data['operation'] = $operation;
337 mod_assign_grading_batch_operations_form::mock_submit($data);
81f92c22 338
c462c35b 339 // Set required variables in the form.
81f92c22 340 $formparams = array();
c462c35b
JF
341 $formparams['submissiondrafts'] = 1;
342 $formparams['duedate'] = 1;
343 $formparams['attemptreopenmethod'] = ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL;
344 $formparams['feedbackplugins'] = array();
345 $formparams['markingworkflow'] = 1;
346 $formparams['markingallocation'] = 1;
81f92c22
MN
347 $formparams['cm'] = $this->get_course_module()->id;
348 $formparams['context'] = $this->get_context();
c462c35b 349 $mform = new mod_assign_grading_batch_operations_form(null, $formparams);
81f92c22 350
c462c35b 351 return $mform;
81f92c22 352 }
0cf8082a
AG
353
354 public function testable_update_activity_completion_records($teamsubmission,
355 $requireallteammemberssubmit,
356 $submission,
357 $userid,
358 $complete,
359 $completion) {
360 return parent::update_activity_completion_records($teamsubmission,
361 $requireallteammemberssubmit,
362 $submission,
363 $userid,
364 $complete,
365 $completion);
366 }
9c986ee0 367}