MDL-37082: Fixed failed unit tests from previous patch
[moodle.git] / mod / assign / tests / upgradelib_test.php
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/>.
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  */
27 defined('MOODLE_INTERNAL') || die();
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');
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 {
42     /** @var stdClass $course New course created to hold the assignments */
43     protected $course = null;
45     /** @var array $teachers List of 5 default teachers in the course*/
46     protected $teachers = null;
48     /** @var array $editingteachers List of 5 default editing teachers in the course*/
49     protected $editingteachers = null;
51     /** @var array $students List of 100 default students in the course*/
52     protected $students = null;
54     /** @var array $groups List of 10 groups in the course */
55     protected $groups = null;
57     /**
58      * Setup function - we will create a course and add users to it.
59      */
60     protected function setUp() {
61         global $DB, $CFG;
63         $this->resetAfterTest(true);
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         }
71         $this->editingteachers = array();
72         for ($i = 0; $i < 5; $i++) {
73             array_push($this->editingteachers, $this->getDataGenerator()->create_user());
74         }
76         $this->students = array();
77         for ($i = 0; $i < 100; $i++) {
78             array_push($this->students, $this->getDataGenerator()->create_user());
79         }
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         }
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         }
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         }
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     }
113     public function test_upgrade_upload_assignment() {
114         global $DB;
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);
122         $assignment = new assignment_base($record->cmid);
124         $this->setAdminUser();
125         $log = '';
126         $upgrader = new assign_upgrade_manager();
128         $this->assertTrue($upgrader->upgrade_assignment($assignment->assignment->id, $log));
129         $record = $DB->get_record('assign', array('course'=>$this->course->id));
131         $cm = get_coursemodule_from_instance('assign', $record->id);
132         $context = context_module::instance($cm->id);
134         $assign = new assign($context, $cm, $this->course);
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());
149         course_delete_module($cm->id);
150     }
152     public function test_upgrade_uploadsingle_assignment() {
153         global $DB;
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);
161         $assignment = new assignment_base($record->cmid);
163         $this->setAdminUser();
164         $log = '';
165         $upgrader = new assign_upgrade_manager();
167         $this->assertTrue($upgrader->upgrade_assignment($assignment->assignment->id, $log));
168         $record = $DB->get_record('assign', array('course'=>$this->course->id));
170         $cm = get_coursemodule_from_instance('assign', $record->id);
171         $context = context_module::instance($cm->id);
173         $assign = new assign($context, $cm, $this->course);
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());
188         course_delete_module($cm->id);
189     }
191     public function test_upgrade_onlinetext_assignment() {
192         global $DB;
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);
200         $assignment = new assignment_base($record->cmid);
202         $this->setAdminUser();
203         $log = '';
204         $upgrader = new assign_upgrade_manager();
206         $this->assertTrue($upgrader->upgrade_assignment($assignment->assignment->id, $log));
207         $record = $DB->get_record('assign', array('course'=>$this->course->id));
209         $cm = get_coursemodule_from_instance('assign', $record->id);
210         $context = context_module::instance($cm->id);
212         $assign = new assign($context, $cm, $this->course);
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());
227         course_delete_module($cm->id);
228     }
230     public function test_upgrade_offline_assignment() {
231         global $DB;
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);
239         $assignment = new assignment_base($record->cmid);
241         $this->setAdminUser();
242         $log = '';
243         $upgrader = new assign_upgrade_manager();
245         $this->assertTrue($upgrader->upgrade_assignment($assignment->assignment->id, $log));
246         $record = $DB->get_record('assign', array('course'=>$this->course->id));
248         $cm = get_coursemodule_from_instance('assign', $record->id);
249         $context = context_module::instance($cm->id);
251         $assign = new assign($context, $cm, $this->course);
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());
266         course_delete_module($cm->id);
267     }