Commit | Line | Data |
---|---|---|
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 | ||
27 | defined('MOODLE_INTERNAL') || die(); | |
28 | ||
29 | global $CFG; | |
30 | require_once($CFG->dirroot . '/mod/assign/locallib.php'); | |
31 | require_once($CFG->dirroot . '/mod/assign/upgradelib.php'); | |
32 | require_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 | */ | |
40 | class 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'); | |
139 | $this->assertNotEmpty($plugin->is_enabled()); | |
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 | ||
18aaeef0 | 149 | course_delete_module($cm->id); |
47f48152 DW |
150 | } |
151 | ||
152 | public function test_upgrade_uploadsingle_assignment() { | |
153 | global $DB; | |
154 | ||
155 | $this->setUser($this->editingteachers[0]); | |
156 | $generator = $this->getDataGenerator()->get_plugin_generator('mod_assignment'); | |
157 | $params = array('course'=>$this->course->id, | |
158 | 'assignmenttype'=>'uploadsingle'); | |
159 | $record = $generator->create_instance($params); | |
160 | ||
161 | $assignment = new assignment_base($record->cmid); | |
162 | ||
163 | $this->setAdminUser(); | |
164 | $log = ''; | |
165 | $upgrader = new assign_upgrade_manager(); | |
166 | ||
167 | $this->assertTrue($upgrader->upgrade_assignment($assignment->assignment->id, $log)); | |
168 | $record = $DB->get_record('assign', array('course'=>$this->course->id)); | |
169 | ||
170 | $cm = get_coursemodule_from_instance('assign', $record->id); | |
171 | $context = context_module::instance($cm->id); | |
172 | ||
173 | $assign = new assign($context, $cm, $this->course); | |
174 | ||
175 | $plugin = $assign->get_submission_plugin_by_type('onlinetext'); | |
176 | $this->assertEmpty($plugin->is_enabled()); | |
177 | $plugin = $assign->get_submission_plugin_by_type('comments'); | |
178 | $this->assertEmpty($plugin->is_enabled()); | |
179 | $plugin = $assign->get_submission_plugin_by_type('file'); | |
180 | $this->assertNotEmpty($plugin->is_enabled()); | |
181 | $plugin = $assign->get_feedback_plugin_by_type('comments'); | |
182 | $this->assertNotEmpty($plugin->is_enabled()); | |
183 | $plugin = $assign->get_feedback_plugin_by_type('file'); | |
184 | $this->assertNotEmpty($plugin->is_enabled()); | |
185 | $plugin = $assign->get_feedback_plugin_by_type('offline'); | |
186 | $this->assertEmpty($plugin->is_enabled()); | |
187 | ||
18aaeef0 | 188 | course_delete_module($cm->id); |
47f48152 DW |
189 | } |
190 | ||
191 | public function test_upgrade_onlinetext_assignment() { | |
192 | global $DB; | |
193 | ||
194 | $this->setUser($this->editingteachers[0]); | |
195 | $generator = $this->getDataGenerator()->get_plugin_generator('mod_assignment'); | |
196 | $params = array('course'=>$this->course->id, | |
197 | 'assignmenttype'=>'online'); | |
198 | $record = $generator->create_instance($params); | |
199 | ||
200 | $assignment = new assignment_base($record->cmid); | |
201 | ||
202 | $this->setAdminUser(); | |
203 | $log = ''; | |
204 | $upgrader = new assign_upgrade_manager(); | |
205 | ||
206 | $this->assertTrue($upgrader->upgrade_assignment($assignment->assignment->id, $log)); | |
207 | $record = $DB->get_record('assign', array('course'=>$this->course->id)); | |
208 | ||
209 | $cm = get_coursemodule_from_instance('assign', $record->id); | |
210 | $context = context_module::instance($cm->id); | |
211 | ||
212 | $assign = new assign($context, $cm, $this->course); | |
213 | ||
214 | $plugin = $assign->get_submission_plugin_by_type('onlinetext'); | |
215 | $this->assertNotEmpty($plugin->is_enabled()); | |
216 | $plugin = $assign->get_submission_plugin_by_type('comments'); | |
217 | $this->assertEmpty($plugin->is_enabled()); | |
218 | $plugin = $assign->get_submission_plugin_by_type('file'); | |
219 | $this->assertEmpty($plugin->is_enabled()); | |
220 | $plugin = $assign->get_feedback_plugin_by_type('comments'); | |
221 | $this->assertNotEmpty($plugin->is_enabled()); | |
222 | $plugin = $assign->get_feedback_plugin_by_type('file'); | |
223 | $this->assertEmpty($plugin->is_enabled()); | |
224 | $plugin = $assign->get_feedback_plugin_by_type('offline'); | |
225 | $this->assertEmpty($plugin->is_enabled()); | |
226 | ||
18aaeef0 | 227 | course_delete_module($cm->id); |
47f48152 DW |
228 | } |
229 | ||
230 | public function test_upgrade_offline_assignment() { | |
231 | global $DB; | |
232 | ||
233 | $this->setUser($this->editingteachers[0]); | |
234 | $generator = $this->getDataGenerator()->get_plugin_generator('mod_assignment'); | |
235 | $params = array('course'=>$this->course->id, | |
236 | 'assignmenttype'=>'offline'); | |
237 | $record = $generator->create_instance($params); | |
238 | ||
239 | $assignment = new assignment_base($record->cmid); | |
240 | ||
241 | $this->setAdminUser(); | |
242 | $log = ''; | |
243 | $upgrader = new assign_upgrade_manager(); | |
244 | ||
245 | $this->assertTrue($upgrader->upgrade_assignment($assignment->assignment->id, $log)); | |
246 | $record = $DB->get_record('assign', array('course'=>$this->course->id)); | |
247 | ||
248 | $cm = get_coursemodule_from_instance('assign', $record->id); | |
249 | $context = context_module::instance($cm->id); | |
250 | ||
251 | $assign = new assign($context, $cm, $this->course); | |
252 | ||
253 | $plugin = $assign->get_submission_plugin_by_type('onlinetext'); | |
254 | $this->assertEmpty($plugin->is_enabled()); | |
255 | $plugin = $assign->get_submission_plugin_by_type('comments'); | |
256 | $this->assertEmpty($plugin->is_enabled()); | |
257 | $plugin = $assign->get_submission_plugin_by_type('file'); | |
258 | $this->assertEmpty($plugin->is_enabled()); | |
259 | $plugin = $assign->get_feedback_plugin_by_type('comments'); | |
260 | $this->assertNotEmpty($plugin->is_enabled()); | |
261 | $plugin = $assign->get_feedback_plugin_by_type('file'); | |
262 | $this->assertEmpty($plugin->is_enabled()); | |
263 | $plugin = $assign->get_feedback_plugin_by_type('offline'); | |
264 | $this->assertEmpty($plugin->is_enabled()); | |
265 | ||
18aaeef0 | 266 | course_delete_module($cm->id); |
47f48152 DW |
267 | } |
268 | } |