MDL-40218 Assignment: User should not be able to view submissions, whithout proper...
[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;
49 /** @const Optional extra number of teachers to create */
50 const EXTRA_TEACHER_COUNT = 5;
51 /** @const Optional extra number of editing teachers to create */
52 const EXTRA_EDITING_TEACHER_COUNT = 5;
53 /** @const Number of groups to create */
54 const GROUP_COUNT = 6;
55
56 /** @var stdClass $course New course created to hold the assignments */
57 protected $course = null;
58
59 /** @var array $teachers List of DEFAULT_TEACHER_COUNT teachers in the course*/
60 protected $teachers = null;
61
62 /** @var array $editingteachers List of DEFAULT_EDITING_TEACHER_COUNT editing teachers in the course */
63 protected $editingteachers = null;
64
65 /** @var array $students List of DEFAULT_STUDENT_COUNT students in the course*/
66 protected $students = null;
67
68 /** @var array $extrateachers List of EXTRA_TEACHER_COUNT teachers in the course*/
69 protected $extrateachers = null;
70
71 /** @var array $extraeditingteachers List of EXTRA_EDITING_TEACHER_COUNT editing teachers in the course*/
72 protected $extraeditingteachers = null;
73
74 /** @var array $extrastudents List of EXTRA_STUDENT_COUNT students in the course*/
75 protected $extrastudents = null;
76
77 /** @var array $groups List of 10 groups in the course */
78 protected $groups = null;
79
80 /**
81 * Setup function - we will create a course and add an assign instance to it.
82 */
83 protected function setUp() {
84 global $DB;
85
86 $this->resetAfterTest(true);
87
88 $this->course = $this->getDataGenerator()->create_course();
89 $this->teachers = array();
90 for ($i = 0; $i < self::DEFAULT_TEACHER_COUNT; $i++) {
91 array_push($this->teachers, $this->getDataGenerator()->create_user());
92 }
93
94 $this->editingteachers = array();
95 for ($i = 0; $i < self::DEFAULT_EDITING_TEACHER_COUNT; $i++) {
96 array_push($this->editingteachers, $this->getDataGenerator()->create_user());
97 }
98
99 $this->students = array();
100 for ($i = 0; $i < self::DEFAULT_STUDENT_COUNT; $i++) {
101 array_push($this->students, $this->getDataGenerator()->create_user());
102 }
103
104 $this->groups = array();
105 for ($i = 0; $i < self::GROUP_COUNT; $i++) {
106 array_push($this->groups, $this->getDataGenerator()->create_group(array('courseid'=>$this->course->id)));
107 }
108
109 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
110 foreach ($this->teachers as $i => $teacher) {
111 $this->getDataGenerator()->enrol_user($teacher->id,
112 $this->course->id,
113 $teacherrole->id);
114 groups_add_member($this->groups[$i % self::GROUP_COUNT], $teacher);
115 }
116
117 $editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
118 foreach ($this->editingteachers as $i => $editingteacher) {
119 $this->getDataGenerator()->enrol_user($editingteacher->id,
120 $this->course->id,
121 $editingteacherrole->id);
122 groups_add_member($this->groups[$i % self::GROUP_COUNT], $editingteacher);
123 }
124
125 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
126 foreach ($this->students as $i => $student) {
127 $this->getDataGenerator()->enrol_user($student->id,
128 $this->course->id,
129 $studentrole->id);
130 groups_add_member($this->groups[$i % self::GROUP_COUNT], $student);
131 }
132 }
133
134 /*
135 * For tests that make sense to use alot of data, create extra students/teachers.
136 */
df211804 137 protected function create_extra_users() {
9c986ee0
DW
138 global $DB;
139 $this->extrateachers = array();
140 for ($i = 0; $i < self::EXTRA_TEACHER_COUNT; $i++) {
141 array_push($this->extrateachers, $this->getDataGenerator()->create_user());
142 }
143
144 $this->extraeditingteachers = array();
145 for ($i = 0; $i < self::EXTRA_EDITING_TEACHER_COUNT; $i++) {
146 array_push($this->extraeditingteachers, $this->getDataGenerator()->create_user());
147 }
148
149 $this->extrastudents = array();
150 for ($i = 0; $i < self::EXTRA_STUDENT_COUNT; $i++) {
151 array_push($this->extrastudents, $this->getDataGenerator()->create_user());
152 }
153
154 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
155 foreach ($this->extrateachers as $i => $teacher) {
156 $this->getDataGenerator()->enrol_user($teacher->id,
157 $this->course->id,
158 $teacherrole->id);
159 groups_add_member($this->groups[$i % self::GROUP_COUNT], $teacher);
160 }
161
162 $editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
163 foreach ($this->extraeditingteachers as $i => $editingteacher) {
164 $this->getDataGenerator()->enrol_user($editingteacher->id,
165 $this->course->id,
166 $editingteacherrole->id);
167 groups_add_member($this->groups[$i % self::GROUP_COUNT], $editingteacher);
168 }
169
170 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
171 foreach ($this->extrastudents as $i => $student) {
172 $this->getDataGenerator()->enrol_user($student->id,
173 $this->course->id,
174 $studentrole->id);
175 if ($i < (self::EXTRA_STUDENT_COUNT / 2)) {
176 groups_add_member($this->groups[$i % self::GROUP_COUNT], $student);
177 }
178 }
179
180 }
181
182 /**
183 * Convenience function to create a testable instance of an assignment.
184 *
185 * @param array $params Array of parameters to pass to the generator
186 * @return testable_assign Testable wrapper around the assign class.
187 */
188 protected function create_instance($params=array()) {
189 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
190 $params['course'] = $this->course->id;
191 $instance = $generator->create_instance($params);
192 $cm = get_coursemodule_from_instance('assign', $instance->id);
193 $context = context_module::instance($cm->id);
194 return new testable_assign($context, $cm, $this->course);
195 }
196
197 public function test_create_instance() {
198 $this->assertNotEmpty($this->create_instance());
199 }
200
201}
202
203/**
204 * Test subclass that makes all the protected methods we want to test public.
205 */
206class testable_assign extends assign {
207
208 public function testable_process_reveal_identities() {
209 return parent::process_reveal_identities();
210 }
211
212 public function testable_show_intro() {
213 return parent::show_intro();
214 }
215
216 public function testable_delete_grades() {
217 return parent::delete_grades();
218 }
219
df211804
DW
220 public function testable_apply_grade_to_user($formdata, $userid, $attemptnumber) {
221 return parent::apply_grade_to_user($formdata, $userid, $attemptnumber);
9c986ee0
DW
222 }
223
224 public function testable_get_grading_userid_list() {
225 return parent::get_grading_userid_list();
226 }
227
228 public function testable_is_graded($userid) {
229 return parent::is_graded($userid);
230 }
231
232 public function testable_update_submission(stdClass $submission, $userid, $updatetime, $teamsubmission) {
233 return parent::update_submission($submission, $userid, $updatetime, $teamsubmission);
234 }
235
df211804
DW
236 public function testable_process_add_attempt($userid = 0) {
237 return parent::process_add_attempt($userid);
238 }
239
9c986ee0
DW
240 public function testable_submissions_open($userid = 0) {
241 return parent::submissions_open($userid);
242 }
243
244 public function testable_save_user_extension($userid, $extensionduedate) {
245 return parent::save_user_extension($userid, $extensionduedate);
246 }
247
248 public function testable_get_graders($userid) {
249 // Changed method from protected to public.
250 return parent::get_graders($userid);
251 }
252}