8454229f80efcb3e5ea0ac2bf5fca91719948e35
[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         $assign->delete_instance();
150         delete_course_module($cm->id);
151         delete_mod_from_section($cm->id, $cm->section);
152     }
154     public function test_upgrade_uploadsingle_assignment() {
155         global $DB;
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);
163         $assignment = new assignment_base($record->cmid);
165         $this->setAdminUser();
166         $log = '';
167         $upgrader = new assign_upgrade_manager();
169         $this->assertTrue($upgrader->upgrade_assignment($assignment->assignment->id, $log));
170         $record = $DB->get_record('assign', array('course'=>$this->course->id));
172         $cm = get_coursemodule_from_instance('assign', $record->id);
173         $context = context_module::instance($cm->id);
175         $assign = new assign($context, $cm, $this->course);
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());
190         $assign->delete_instance();
191         delete_course_module($cm->id);
192         delete_mod_from_section($cm->id, $cm->section);
193     }
195     public function test_upgrade_onlinetext_assignment() {
196         global $DB;
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);
204         $assignment = new assignment_base($record->cmid);
206         $this->setAdminUser();
207         $log = '';
208         $upgrader = new assign_upgrade_manager();
210         $this->assertTrue($upgrader->upgrade_assignment($assignment->assignment->id, $log));
211         $record = $DB->get_record('assign', array('course'=>$this->course->id));
213         $cm = get_coursemodule_from_instance('assign', $record->id);
214         $context = context_module::instance($cm->id);
216         $assign = new assign($context, $cm, $this->course);
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());
231         $assign->delete_instance();
232         delete_course_module($cm->id);
233         delete_mod_from_section($cm->id, $cm->section);
234     }
236     public function test_upgrade_offline_assignment() {
237         global $DB;
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);
245         $assignment = new assignment_base($record->cmid);
247         $this->setAdminUser();
248         $log = '';
249         $upgrader = new assign_upgrade_manager();
251         $this->assertTrue($upgrader->upgrade_assignment($assignment->assignment->id, $log));
252         $record = $DB->get_record('assign', array('course'=>$this->course->id));
254         $cm = get_coursemodule_from_instance('assign', $record->id);
255         $context = context_module::instance($cm->id);
257         $assign = new assign($context, $cm, $this->course);
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());
272         $assign->delete_instance();
273         delete_course_module($cm->id);
274         delete_mod_from_section($cm->id, $cm->section);
275     }