2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * File containing tests for the processor.
20 * @package tool_uploadcourse
21 * @copyright 2013 Frédéric Massart
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->libdir . '/csvlib.class.php');
31 * Processor test case.
33 * @package tool_uploadcourse
34 * @copyright 2013 Frédéric Massart
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class tool_uploadcourse_processor_testcase extends advanced_testcase {
39 public function test_basic() {
41 $this->resetAfterTest(true);
44 "shortname,fullname,summary",
45 "c1,Course 1,Course 1 summary",
46 "c2,Course 2,Course 2 summary",
48 $content = implode("\n", $content);
49 $iid = csv_import_reader::get_new_iid('uploadcourse');
50 $cir = new csv_import_reader($iid, 'uploadcourse');
51 $cir->load_csv_content($content, 'utf-8', 'comma');
54 $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_ALL);
55 $defaults = array('category' => '1');
57 $p = new tool_uploadcourse_processor($cir, $options, $defaults);
58 $this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
59 $this->assertFalse($DB->record_exists('course', array('shortname' => 'c2')));
61 $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
62 $this->assertTrue($DB->record_exists('course', array('shortname' => 'c2')));
65 public function test_restore_template_course() {
67 $this->resetAfterTest(true);
68 $this->setAdminUser();
70 $c1 = $this->getDataGenerator()->create_course();
71 $c1f1 = $this->getDataGenerator()->create_module('forum', array('course' => $c1->id));
74 "shortname,fullname,summary",
75 "c2,Course 2,Course 2 summary",
77 $content = implode("\n", $content);
78 $iid = csv_import_reader::get_new_iid('uploadcourse');
79 $cir = new csv_import_reader($iid, 'uploadcourse');
80 $cir->load_csv_content($content, 'utf-8', 'comma');
83 $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW, 'templatecourse' => $c1->shortname);
84 $defaults = array('category' => '1');
86 $p = new tool_uploadcourse_processor($cir, $options, $defaults);
87 $this->assertFalse($DB->record_exists('course', array('shortname' => 'c2')));
89 $c2 = $DB->get_record('course', array('shortname' => 'c2'));
90 $modinfo = get_fast_modinfo($c2);
92 foreach ($modinfo->get_cms() as $cmid => $cm) {
93 if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
98 $this->assertTrue($found);
101 public function test_restore_restore_file() {
103 $this->resetAfterTest(true);
104 $this->setAdminUser();
107 "shortname,fullname,summary",
108 "c1,Course 1,Course 1 summary",
110 $content = implode("\n", $content);
111 $iid = csv_import_reader::get_new_iid('uploadcourse');
112 $cir = new csv_import_reader($iid, 'uploadcourse');
113 $cir->load_csv_content($content, 'utf-8', 'comma');
117 'mode' => tool_uploadcourse_processor::MODE_CREATE_NEW,
118 'restorefile' => __DIR__ . '/fixtures/backup.mbz',
119 'templatecourse' => 'DoesNotExist' // Restorefile takes priority.
121 $defaults = array('category' => '1');
123 $p = new tool_uploadcourse_processor($cir, $options, $defaults);
124 $this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
126 $c1 = $DB->get_record('course', array('shortname' => 'c1'));
127 $modinfo = get_fast_modinfo($c1);
129 foreach ($modinfo->get_cms() as $cmid => $cm) {
130 if ($cm->modname == 'glossary' && $cm->name == 'Imported Glossary') {
135 $this->assertTrue($found);
138 public function test_shortname_template() {
140 $this->resetAfterTest(true);
143 "shortname,fullname,summary,idnumber",
144 ",Course 1,C1 Summary,ID123",
146 $content = implode("\n", $content);
147 $iid = csv_import_reader::get_new_iid('uploadcourse');
148 $cir = new csv_import_reader($iid, 'uploadcourse');
149 $cir->load_csv_content($content, 'utf-8', 'comma');
152 $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW, 'shortnametemplate' => '%i: %f');
153 $defaults = array('category' => '1');
155 $p = new tool_uploadcourse_processor($cir, $options, $defaults);
156 $this->assertFalse($DB->record_exists('course', array('idnumber' => 'ID123')));
158 $this->assertTrue($DB->record_exists('course', array('idnumber' => 'ID123')));
159 $c = $DB->get_record('course', array('idnumber' => 'ID123'));
160 $this->assertEquals('ID123: Course 1', $c->shortname);
163 public function test_empty_csv() {
164 $this->resetAfterTest(true);
167 $content = implode("\n", $content);
168 $iid = csv_import_reader::get_new_iid('uploadcourse');
169 $cir = new csv_import_reader($iid, 'uploadcourse');
170 $cir->load_csv_content($content, 'utf-8', 'comma');
173 $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW);
174 $this->setExpectedException('moodle_exception');
175 $p = new tool_uploadcourse_processor($cir, $options, array());
178 public function test_not_enough_columns() {
179 $this->resetAfterTest(true);
185 $content = implode("\n", $content);
186 $iid = csv_import_reader::get_new_iid('uploadcourse');
187 $cir = new csv_import_reader($iid, 'uploadcourse');
188 $cir->load_csv_content($content, 'utf-8', 'comma');
191 $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW);
192 $this->setExpectedException('moodle_exception');
193 $p = new tool_uploadcourse_processor($cir, $options, array());
196 public function test_preview() {
198 $this->resetAfterTest(true);
201 "shortname,fullname,summary",
202 "c1,Course 1,Course 1 summary",
203 "c2,Course 2,Course 2 summary",
205 $content = implode("\n", $content);
206 $iid = csv_import_reader::get_new_iid('uploadcourse');
207 $cir = new csv_import_reader($iid, 'uploadcourse');
208 $cir->load_csv_content($content, 'utf-8', 'comma');
211 $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_ALL);
212 $defaults = array('category' => '1');
214 $p = new tool_uploadcourse_processor($cir, $options, $defaults);
215 // Nothing special to expect here, just make sure no exceptions are thrown.