MDL-37337 Assignment: Restructure assignment settings screen to make it "wieldy"
[moodle.git] / mod / assign / tests / lib_test.php
CommitLineData
cb5b4860
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 * 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 */
25
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30require_once($CFG->dirroot . '/mod/assign/lib.php');
31require_once($CFG->dirroot . '/mod/assign/locallib.php');
32
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 */
39class mod_assign_lib_testcase extends advanced_testcase {
40
41 /** @var stdClass $course New course created to hold the assignments */
42 protected $course = null;
43
44 /** @var array $teachers List of 5 default teachers in the course*/
45 protected $teachers = null;
46
47 /** @var array $editingteachers List of 5 default editing teachers in the course*/
48 protected $editingteachers = null;
49
50 /** @var array $students List of 100 default students in the course*/
51 protected $students = null;
52
53 /** @var array $groups List of 10 groups in the course */
54 protected $groups = null;
55
56 /**
57 * Setup function - we will create a course and users.
58 */
59 protected function setUp() {
60 global $DB, $CFG;
61
62 $this->resetAfterTest(true);
63
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 }
69
70 $this->editingteachers = array();
71 for ($i = 0; $i < 5; $i++) {
72 array_push($this->editingteachers, $this->getDataGenerator()->create_user());
73 }
74
75 $this->students = array();
76 for ($i = 0; $i < 100; $i++) {
77 array_push($this->students, $this->getDataGenerator()->create_user());
78 }
79
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 }
84
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 }
92
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 }
100
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 }
111
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 }
127
128 public function test_assign_print_overview() {
129 $this->create_instance();
130 $this->create_instance(array('duedate'=>time()));
131
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);
136
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);
141
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 }
147
148 public function test_print_recent_activity() {
149 $assign = $this->create_instance();
150
151 $submission = $assign->get_user_submission($this->students[0]->id, true);
152
153 $this->expectOutputRegex('/submitted:/');
154 assign_print_recent_activity($this->course, true, time() - 3600);
155 }
156
157 public function test_assign_get_recent_mod_activity() {
158 $assign = $this->create_instance();
159
160 $submission = $assign->get_user_submission($this->students[0]->id, true);
161
162 $activities = array();
163 $index = 0;
164
165 $activity = new stdClass();
166 $activity->type = 'activity';
167 $activity->cmid = $assign->get_course_module()->id;
168 $activities[$index++] = $activity;
169
170 assign_get_recent_mod_activity( $activities,
171 $index,
172 time() - 3600,
173 $this->course->id,
174 $assign->get_course_module()->id);
175
176 $this->assertEquals("assign", $activities[1]->type);
177 }
178
179 public function test_assign_user_complete() {
180 $assign = $this->create_instance(array('submissiondrafts' => 1));
181
182 $submission = $assign->get_user_submission($this->students[0]->id, true);
183
cb5b4860
DW
184 $this->expectOutputRegex('/Draft/');
185 assign_user_complete($this->course, $this->students[0], $assign->get_course_module(), $assign->get_instance());
186
187 }
188
189 public function test_assign_user_outline() {
190 $assign = $this->create_instance();
191
192 $this->setUser($this->teachers[0]);
193 $data = $assign->get_user_grade($this->students[0]->id, true);
d021941d 194 $data->grade = '50.5';
cb5b4860
DW
195 $assign->update_grade($data);
196
197 $result = assign_user_outline($this->course, $this->students[0], $assign->get_course_module(), $assign->get_instance());
198
d021941d 199 $this->assertRegExp('/50.5/', $result->info);
cb5b4860
DW
200 }
201
202 public function test_assign_get_completion_state() {
203 global $DB;
204 $assign = $this->create_instance(array('submissiondrafts'=>0, 'completionsubmit'=>1));
205
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);
212
213 $result = assign_get_completion_state($this->course, $assign->get_course_module(), $this->students[0]->id, false);
214
215 $this->assertTrue($result);
216 }
217
218}
219