MDL-49852 mod_assign: Unit tests for activity completion.
[moodle.git] / mod / assign / tests / base_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 /**
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  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
30 require_once($CFG->dirroot . '/mod/assign/locallib.php');
31 require_once($CFG->dirroot . '/mod/assign/upgradelib.php');
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  */
39 class mod_assign_base_testcase extends advanced_testcase {
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;
49     /** @const Optional number of suspended students */
50     const EXTRA_SUSPENDED_COUNT = 10;
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;
58     /** @var stdClass $course New course created to hold the assignments */
59     protected $course = null;
61     /** @var array $teachers List of DEFAULT_TEACHER_COUNT teachers in the course*/
62     protected $teachers = null;
64     /** @var array $editingteachers List of DEFAULT_EDITING_TEACHER_COUNT editing teachers in the course */
65     protected $editingteachers = null;
67     /** @var array $students List of DEFAULT_STUDENT_COUNT students in the course*/
68     protected $students = null;
70     /** @var array $extrateachers List of EXTRA_TEACHER_COUNT teachers in the course*/
71     protected $extrateachers = null;
73     /** @var array $extraeditingteachers List of EXTRA_EDITING_TEACHER_COUNT editing teachers in the course*/
74     protected $extraeditingteachers = null;
76     /** @var array $extrastudents List of EXTRA_STUDENT_COUNT students in the course*/
77     protected $extrastudents = null;
79     /** @var array $extrasuspendedstudents List of EXTRA_SUSPENDED_COUNT students in the course*/
80     protected $extrasuspendedstudents = null;
82     /** @var array $groups List of 10 groups in the course */
83     protected $groups = null;
85     /**
86      * Setup function - we will create a course and add an assign instance to it.
87      */
88     protected function setUp() {
89         global $DB;
91         $this->resetAfterTest(true);
93         $this->course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
94         $this->teachers = array();
95         for ($i = 0; $i < self::DEFAULT_TEACHER_COUNT; $i++) {
96             array_push($this->teachers, $this->getDataGenerator()->create_user());
97         }
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         }
104         $this->students = array();
105         for ($i = 0; $i < self::DEFAULT_STUDENT_COUNT; $i++) {
106             array_push($this->students, $this->getDataGenerator()->create_user());
107         }
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         }
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         }
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         }
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     }
139     /*
140      * For tests that make sense to use alot of data, create extra students/teachers.
141      */
142     protected function create_extra_users() {
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         }
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         }
154         $this->extrastudents = array();
155         for ($i = 0; $i < self::EXTRA_STUDENT_COUNT; $i++) {
156             array_push($this->extrastudents, $this->getDataGenerator()->create_user());
157         }
159         $this->extrasuspendedstudents = array();
160         for ($i = 0; $i < self::EXTRA_SUSPENDED_COUNT; $i++) {
161             array_push($this->extrasuspendedstudents, $this->getDataGenerator()->create_user());
162         }
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         }
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         }
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         }
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         }
198     }
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     }
215     public function test_create_instance() {
216         $this->assertNotEmpty($this->create_instance());
217     }
221 /**
222  * Test subclass that makes all the protected methods we want to test public.
223  */
224 class testable_assign extends assign {
226     public function testable_show_intro() {
227         return parent::show_intro();
228     }
230     public function testable_delete_grades() {
231         return parent::delete_grades();
232     }
234     public function testable_apply_grade_to_user($formdata, $userid, $attemptnumber) {
235         return parent::apply_grade_to_user($formdata, $userid, $attemptnumber);
236     }
238     public function testable_format_submission_for_log(stdClass $submission) {
239         return parent::format_submission_for_log($submission);
240     }
242     public function testable_get_grading_userid_list() {
243         return parent::get_grading_userid_list();
244     }
246     public function testable_is_graded($userid) {
247         return parent::is_graded($userid);
248     }
250     public function testable_update_submission(stdClass $submission, $userid, $updatetime, $teamsubmission) {
251         return parent::update_submission($submission, $userid, $updatetime, $teamsubmission);
252     }
254     public function testable_process_add_attempt($userid = 0) {
255         return parent::process_add_attempt($userid);
256     }
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     }
265     public function testable_process_set_batch_marking_allocation($selectedusers, $markerid) {
266         global $CFG;
267         require_once($CFG->dirroot . '/mod/assign/batchsetallocatedmarkerform.php');
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);
277         return parent::process_set_batch_marking_allocation();
278     }
280     public function testable_process_set_batch_marking_workflow_state($selectedusers, $state) {
281         global $CFG;
282         require_once($CFG->dirroot . '/mod/assign/batchsetmarkingworkflowstateform.php');
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);
292         return parent::process_set_batch_marking_workflow_state();
293     }
295     public function testable_submissions_open($userid = 0) {
296         return parent::submissions_open($userid);
297     }
299     public function testable_save_user_extension($userid, $extensionduedate) {
300         return parent::save_user_extension($userid, $extensionduedate);
301     }
303     public function testable_get_graders($userid) {
304         // Changed method from protected to public.
305         return parent::get_graders($userid);
306     }
308     public function testable_get_notifiable_users($userid) {
309         return parent::get_notifiable_users($userid);
310     }
312     public function testable_view_batch_set_workflow_state($selectedusers) {
313         global $PAGE;
314         $PAGE->set_url('/mod/assign/view.php');
315         $mform = $this->testable_grading_batch_operations_form('setmarkingworkflowstate', $selectedusers);
316         return parent::view_batch_set_workflow_state($mform);
317     }
319     public function testable_view_batch_markingallocation($selectedusers) {
320         global $PAGE;
321         $PAGE->set_url('/mod/assign/view.php');
322         $mform = $this->testable_grading_batch_operations_form('setmarkingallocation', $selectedusers);
323         return parent::view_batch_markingallocation($mform);
324     }
326     public function testable_grading_batch_operations_form($operation, $selectedusers) {
327         global $CFG;
329         require_once($CFG->dirroot . '/mod/assign/gradingbatchoperationsform.php');
331         // Mock submit the grading operations form.
332         $data = array();
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);
339         // Set required variables in the form.
340         $formparams = array();
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;
347         $formparams['cm'] = $this->get_course_module()->id;
348         $formparams['context'] = $this->get_context();
349         $mform = new mod_assign_grading_batch_operations_form(null, $formparams);
351         return $mform;
352     }
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     }