MDL-37539 assign: Fix unit test after changes to upgrade for advanced file assignments.
[moodle.git] / mod / assign / tests / upgradelib_test.php
CommitLineData
47f48152
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/upgradelib.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/locallib.php');
31require_once($CFG->dirroot . '/mod/assign/upgradelib.php');
32require_once($CFG->dirroot . '/mod/assignment/lib.php');
33
34/**
35 * Unit tests for (some of) mod/assign/upgradelib.php.
36 *
37 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class mod_assign_upgradelib_testcase extends advanced_testcase {
41
42 /** @var stdClass $course New course created to hold the assignments */
43 protected $course = null;
44
45 /** @var array $teachers List of 5 default teachers in the course*/
46 protected $teachers = null;
47
48 /** @var array $editingteachers List of 5 default editing teachers in the course*/
49 protected $editingteachers = null;
50
51 /** @var array $students List of 100 default students in the course*/
52 protected $students = null;
53
54 /** @var array $groups List of 10 groups in the course */
55 protected $groups = null;
56
57 /**
58 * Setup function - we will create a course and add users to it.
59 */
60 protected function setUp() {
61 global $DB, $CFG;
62
63 $this->resetAfterTest(true);
64
65 $this->course = $this->getDataGenerator()->create_course();
66 $this->teachers = array();
67 for ($i = 0; $i < 5; $i++) {
68 array_push($this->teachers, $this->getDataGenerator()->create_user());
69 }
70
71 $this->editingteachers = array();
72 for ($i = 0; $i < 5; $i++) {
73 array_push($this->editingteachers, $this->getDataGenerator()->create_user());
74 }
75
76 $this->students = array();
77 for ($i = 0; $i < 100; $i++) {
78 array_push($this->students, $this->getDataGenerator()->create_user());
79 }
80
81 $this->groups = array();
82 for ($i = 0; $i < 10; $i++) {
83 array_push($this->groups, $this->getDataGenerator()->create_group(array('courseid'=>$this->course->id)));
84 }
85
86 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
87 foreach ($this->teachers as $i => $teacher) {
88 $this->getDataGenerator()->enrol_user($teacher->id,
89 $this->course->id,
90 $teacherrole->id);
91 groups_add_member($this->groups[$i % 10], $teacher);
92 }
93
94 $editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
95 foreach ($this->editingteachers as $i => $editingteacher) {
96 $this->getDataGenerator()->enrol_user($editingteacher->id,
97 $this->course->id,
98 $editingteacherrole->id);
99 groups_add_member($this->groups[$i % 10], $editingteacher);
100 }
101
102 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
103 foreach ($this->students as $i => $student) {
104 $this->getDataGenerator()->enrol_user($student->id,
105 $this->course->id,
106 $studentrole->id);
107 if ($i < 80) {
108 groups_add_member($this->groups[$i % 10], $student);
109 }
110 }
111 }
112
113 public function test_upgrade_upload_assignment() {
114 global $DB;
115
116 $this->setUser($this->editingteachers[0]);
117 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assignment');
118 $params = array('course'=>$this->course->id,
119 'assignmenttype'=>'upload');
120 $record = $generator->create_instance($params);
121
122 $assignment = new assignment_base($record->cmid);
123
124 $this->setAdminUser();
125 $log = '';
126 $upgrader = new assign_upgrade_manager();
127
128 $this->assertTrue($upgrader->upgrade_assignment($assignment->assignment->id, $log));
129 $record = $DB->get_record('assign', array('course'=>$this->course->id));
130
131 $cm = get_coursemodule_from_instance('assign', $record->id);
132 $context = context_module::instance($cm->id);
133
134 $assign = new assign($context, $cm, $this->course);
135
136 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
137 $this->assertEmpty($plugin->is_enabled());
138 $plugin = $assign->get_submission_plugin_by_type('comments');
f518843d 139 $this->assertEmpty($plugin->is_enabled());
47f48152
DW
140 $plugin = $assign->get_submission_plugin_by_type('file');
141 $this->assertNotEmpty($plugin->is_enabled());
142 $plugin = $assign->get_feedback_plugin_by_type('comments');
143 $this->assertNotEmpty($plugin->is_enabled());
144 $plugin = $assign->get_feedback_plugin_by_type('file');
145 $this->assertNotEmpty($plugin->is_enabled());
146 $plugin = $assign->get_feedback_plugin_by_type('offline');
147 $this->assertEmpty($plugin->is_enabled());
148
149 $assign->delete_instance();
150 delete_course_module($cm->id);
151 delete_mod_from_section($cm->id, $cm->section);
152 }
153
154 public function test_upgrade_uploadsingle_assignment() {
155 global $DB;
156
157 $this->setUser($this->editingteachers[0]);
158 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assignment');
159 $params = array('course'=>$this->course->id,
160 'assignmenttype'=>'uploadsingle');
161 $record = $generator->create_instance($params);
162
163 $assignment = new assignment_base($record->cmid);
164
165 $this->setAdminUser();
166 $log = '';
167 $upgrader = new assign_upgrade_manager();
168
169 $this->assertTrue($upgrader->upgrade_assignment($assignment->assignment->id, $log));
170 $record = $DB->get_record('assign', array('course'=>$this->course->id));
171
172 $cm = get_coursemodule_from_instance('assign', $record->id);
173 $context = context_module::instance($cm->id);
174
175 $assign = new assign($context, $cm, $this->course);
176
177 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
178 $this->assertEmpty($plugin->is_enabled());
179 $plugin = $assign->get_submission_plugin_by_type('comments');
180 $this->assertEmpty($plugin->is_enabled());
181 $plugin = $assign->get_submission_plugin_by_type('file');
182 $this->assertNotEmpty($plugin->is_enabled());
183 $plugin = $assign->get_feedback_plugin_by_type('comments');
184 $this->assertNotEmpty($plugin->is_enabled());
185 $plugin = $assign->get_feedback_plugin_by_type('file');
186 $this->assertNotEmpty($plugin->is_enabled());
187 $plugin = $assign->get_feedback_plugin_by_type('offline');
188 $this->assertEmpty($plugin->is_enabled());
189
190 $assign->delete_instance();
191 delete_course_module($cm->id);
192 delete_mod_from_section($cm->id, $cm->section);
193 }
194
195 public function test_upgrade_onlinetext_assignment() {
196 global $DB;
197
198 $this->setUser($this->editingteachers[0]);
199 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assignment');
200 $params = array('course'=>$this->course->id,
201 'assignmenttype'=>'online');
202 $record = $generator->create_instance($params);
203
204 $assignment = new assignment_base($record->cmid);
205
206 $this->setAdminUser();
207 $log = '';
208 $upgrader = new assign_upgrade_manager();
209
210 $this->assertTrue($upgrader->upgrade_assignment($assignment->assignment->id, $log));
211 $record = $DB->get_record('assign', array('course'=>$this->course->id));
212
213 $cm = get_coursemodule_from_instance('assign', $record->id);
214 $context = context_module::instance($cm->id);
215
216 $assign = new assign($context, $cm, $this->course);
217
218 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
219 $this->assertNotEmpty($plugin->is_enabled());
220 $plugin = $assign->get_submission_plugin_by_type('comments');
221 $this->assertEmpty($plugin->is_enabled());
222 $plugin = $assign->get_submission_plugin_by_type('file');
223 $this->assertEmpty($plugin->is_enabled());
224 $plugin = $assign->get_feedback_plugin_by_type('comments');
225 $this->assertNotEmpty($plugin->is_enabled());
226 $plugin = $assign->get_feedback_plugin_by_type('file');
227 $this->assertEmpty($plugin->is_enabled());
228 $plugin = $assign->get_feedback_plugin_by_type('offline');
229 $this->assertEmpty($plugin->is_enabled());
230
231 $assign->delete_instance();
232 delete_course_module($cm->id);
233 delete_mod_from_section($cm->id, $cm->section);
234 }
235
236 public function test_upgrade_offline_assignment() {
237 global $DB;
238
239 $this->setUser($this->editingteachers[0]);
240 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assignment');
241 $params = array('course'=>$this->course->id,
242 'assignmenttype'=>'offline');
243 $record = $generator->create_instance($params);
244
245 $assignment = new assignment_base($record->cmid);
246
247 $this->setAdminUser();
248 $log = '';
249 $upgrader = new assign_upgrade_manager();
250
251 $this->assertTrue($upgrader->upgrade_assignment($assignment->assignment->id, $log));
252 $record = $DB->get_record('assign', array('course'=>$this->course->id));
253
254 $cm = get_coursemodule_from_instance('assign', $record->id);
255 $context = context_module::instance($cm->id);
256
257 $assign = new assign($context, $cm, $this->course);
258
259 $plugin = $assign->get_submission_plugin_by_type('onlinetext');
260 $this->assertEmpty($plugin->is_enabled());
261 $plugin = $assign->get_submission_plugin_by_type('comments');
262 $this->assertEmpty($plugin->is_enabled());
263 $plugin = $assign->get_submission_plugin_by_type('file');
264 $this->assertEmpty($plugin->is_enabled());
265 $plugin = $assign->get_feedback_plugin_by_type('comments');
266 $this->assertNotEmpty($plugin->is_enabled());
267 $plugin = $assign->get_feedback_plugin_by_type('file');
268 $this->assertEmpty($plugin->is_enabled());
269 $plugin = $assign->get_feedback_plugin_by_type('offline');
270 $this->assertEmpty($plugin->is_enabled());
271
272 $assign->delete_instance();
273 delete_course_module($cm->id);
274 delete_mod_from_section($cm->id, $cm->section);
275 }
276}