MDL-37337 Assignment: Restructure assignment settings screen to make it "wieldy"
[moodle.git] / mod / assign / tests / lib_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  * Unit tests for (some of) mod/assign/lib.php.
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/lib.php');
31 require_once($CFG->dirroot . '/mod/assign/locallib.php');
33 /**
34  * Unit tests for (some of) mod/assign/lib.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_lib_testcase extends advanced_testcase {
41     /** @var stdClass $course New course created to hold the assignments */
42     protected $course = null;
44     /** @var array $teachers List of 5 default teachers in the course*/
45     protected $teachers = null;
47     /** @var array $editingteachers List of 5 default editing teachers in the course*/
48     protected $editingteachers = null;
50     /** @var array $students List of 100 default students in the course*/
51     protected $students = null;
53     /** @var array $groups List of 10 groups in the course */
54     protected $groups = null;
56     /**
57      * Setup function - we will create a course and users.
58      */
59     protected function setUp() {
60         global $DB, $CFG;
62         $this->resetAfterTest(true);
64         $this->course = $this->getDataGenerator()->create_course();
65         $this->teachers = array();
66         for ($i = 0; $i < 5; $i++) {
67             array_push($this->teachers, $this->getDataGenerator()->create_user());
68         }
70         $this->editingteachers = array();
71         for ($i = 0; $i < 5; $i++) {
72             array_push($this->editingteachers, $this->getDataGenerator()->create_user());
73         }
75         $this->students = array();
76         for ($i = 0; $i < 100; $i++) {
77             array_push($this->students, $this->getDataGenerator()->create_user());
78         }
80         $this->groups = array();
81         for ($i = 0; $i < 10; $i++) {
82             array_push($this->groups, $this->getDataGenerator()->create_group(array('courseid'=>$this->course->id)));
83         }
85         $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
86         foreach ($this->teachers as $i => $teacher) {
87             $this->getDataGenerator()->enrol_user($teacher->id,
88                                                   $this->course->id,
89                                                   $teacherrole->id);
90             groups_add_member($this->groups[$i % 10], $teacher);
91         }
93         $editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
94         foreach ($this->editingteachers as $i => $editingteacher) {
95             $this->getDataGenerator()->enrol_user($editingteacher->id,
96                                                   $this->course->id,
97                                                   $editingteacherrole->id);
98             groups_add_member($this->groups[$i % 10], $editingteacher);
99         }
101         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
102         foreach ($this->students as $i => $student) {
103             $this->getDataGenerator()->enrol_user($student->id,
104                                                   $this->course->id,
105                                                   $studentrole->id);
106             if ($i < 80) {
107                 groups_add_member($this->groups[$i % 10], $student);
108             }
109         }
110     }
112     /**
113      * Create an assignment in the current course.
114      *
115      * @param array $params - A list of params used to configure the assignment
116      * @return assign class
117      */
118     private function create_instance($params=array()) {
119         $this->setUser($this->editingteachers[0]);
120         $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
121         $params['course'] = $this->course->id;
122         $instance = $generator->create_instance($params);
123         $cm = get_coursemodule_from_instance('assign', $instance->id);
124         $context = context_module::instance($cm->id);
125         return new assign($context, $cm, $this->course);
126     }
128     public function test_assign_print_overview() {
129         $this->create_instance();
130         $this->create_instance(array('duedate'=>time()));
132         $this->setUser($this->students[0]);
133         $overview = array();
134         assign_print_overview(array($this->course->id => $this->course), $overview);
135         $this->assertEquals(count($overview), 1);
137         $this->setUser($this->teachers[0]);
138         $overview = array();
139         assign_print_overview(array($this->course->id => $this->course), $overview);
140         $this->assertEquals(count($overview), 1);
142         $this->setUser($this->editingteachers[0]);
143         $overview = array();
144         assign_print_overview(array($this->course->id => $this->course), $overview);
145         $this->assertEquals(1, count($overview));
146     }
148     public function test_print_recent_activity() {
149         $assign = $this->create_instance();
151         $submission = $assign->get_user_submission($this->students[0]->id, true);
153         $this->expectOutputRegex('/submitted:/');
154         assign_print_recent_activity($this->course, true, time() - 3600);
155     }
157     public function test_assign_get_recent_mod_activity() {
158         $assign = $this->create_instance();
160         $submission = $assign->get_user_submission($this->students[0]->id, true);
162         $activities = array();
163         $index = 0;
165         $activity = new stdClass();
166         $activity->type    = 'activity';
167         $activity->cmid    = $assign->get_course_module()->id;
168         $activities[$index++] = $activity;
170         assign_get_recent_mod_activity( $activities,
171                                         $index,
172                                         time() - 3600,
173                                         $this->course->id,
174                                         $assign->get_course_module()->id);
176         $this->assertEquals("assign", $activities[1]->type);
177     }
179     public function test_assign_user_complete() {
180         $assign = $this->create_instance(array('submissiondrafts' => 1));
182         $submission = $assign->get_user_submission($this->students[0]->id, true);
184         $this->expectOutputRegex('/Draft/');
185         assign_user_complete($this->course, $this->students[0], $assign->get_course_module(), $assign->get_instance());
187     }
189     public function test_assign_user_outline() {
190         $assign = $this->create_instance();
192         $this->setUser($this->teachers[0]);
193         $data = $assign->get_user_grade($this->students[0]->id, true);
194         $data->grade = '50.5';
195         $assign->update_grade($data);
197         $result = assign_user_outline($this->course, $this->students[0], $assign->get_course_module(), $assign->get_instance());
199         $this->assertRegExp('/50.5/', $result->info);
200     }
202     public function test_assign_get_completion_state() {
203         global $DB;
204         $assign = $this->create_instance(array('submissiondrafts'=>0, 'completionsubmit'=>1));
206         $this->setUser($this->students[0]);
207         $result = assign_get_completion_state($this->course, $assign->get_course_module(), $this->students[0]->id, false);
208         $this->assertFalse($result);
209         $submission = $assign->get_user_submission($this->students[0]->id, true);
210         $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
211         $DB->update_record('assign_submission', $submission);
213         $result = assign_get_completion_state($this->course, $assign->get_course_module(), $this->students[0]->id, false);
215         $this->assertTrue($result);
216     }