2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Unit tests for (some of) mod/assign/lib.php.
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
30 require_once($CFG->dirroot . '/mod/assign/lib.php');
31 require_once($CFG->dirroot . '/mod/assign/locallib.php');
34 * Unit tests for (some of) mod/assign/lib.php.
36 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
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;
57 * Setup function - we will create a course and users.
59 protected function setUp() {
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());
70 $this->editingteachers = array();
71 for ($i = 0; $i < 5; $i++) {
72 array_push($this->editingteachers, $this->getDataGenerator()->create_user());
75 $this->students = array();
76 for ($i = 0; $i < 100; $i++) {
77 array_push($this->students, $this->getDataGenerator()->create_user());
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)));
85 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
86 foreach ($this->teachers as $i => $teacher) {
87 $this->getDataGenerator()->enrol_user($teacher->id,
90 groups_add_member($this->groups[$i % 10], $teacher);
93 $editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
94 foreach ($this->editingteachers as $i => $editingteacher) {
95 $this->getDataGenerator()->enrol_user($editingteacher->id,
97 $editingteacherrole->id);
98 groups_add_member($this->groups[$i % 10], $editingteacher);
101 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
102 foreach ($this->students as $i => $student) {
103 $this->getDataGenerator()->enrol_user($student->id,
107 groups_add_member($this->groups[$i % 10], $student);
113 * Create an assignment in the current course.
115 * @param array $params - A list of params used to configure the assignment
116 * @return assign class
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);
128 public function test_assign_print_overview() {
129 $this->create_instance();
130 $this->create_instance(array('duedate'=>time()));
132 $this->setUser($this->students[0]);
134 assign_print_overview(array($this->course->id => $this->course), $overview);
135 $this->assertEquals(count($overview), 1);
137 $this->setUser($this->teachers[0]);
139 assign_print_overview(array($this->course->id => $this->course), $overview);
140 $this->assertEquals(count($overview), 1);
142 $this->setUser($this->editingteachers[0]);
144 assign_print_overview(array($this->course->id => $this->course), $overview);
145 $this->assertEquals(1, count($overview));
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);
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();
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,
174 $assign->get_course_module()->id);
176 $this->assertEquals("assign", $activities[1]->type);
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);
185 $this->expectOutputRegex('/Draft/');
186 assign_user_complete($this->course, $this->students[0], $assign->get_course_module(), $assign->get_instance());
190 public function test_assign_user_outline() {
191 $assign = $this->create_instance();
193 $this->setUser($this->teachers[0]);
194 $data = $assign->get_user_grade($this->students[0]->id, true);
195 $data->grade = '50.0';
196 $assign->update_grade($data);
198 $result = assign_user_outline($this->course, $this->students[0], $assign->get_course_module(), $assign->get_instance());
200 $this->assertRegExp('/50.0/', $result->info);
203 public function test_assign_get_completion_state() {
205 $assign = $this->create_instance(array('submissiondrafts'=>0, 'completionsubmit'=>1));
207 $this->setUser($this->students[0]);
208 $result = assign_get_completion_state($this->course, $assign->get_course_module(), $this->students[0]->id, false);
209 $this->assertFalse($result);
210 $submission = $assign->get_user_submission($this->students[0]->id, true);
211 $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
212 $DB->update_record('assign_submission', $submission);
214 $result = assign_get_completion_state($this->course, $assign->get_course_module(), $this->students[0]->id, false);
216 $this->assertTrue($result);