1 |
efrain |
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 |
namespace tool_uploadcourse;
|
|
|
18 |
|
|
|
19 |
use tool_uploadcourse_processor;
|
|
|
20 |
|
|
|
21 |
defined('MOODLE_INTERNAL') || die();
|
|
|
22 |
|
|
|
23 |
global $CFG;
|
|
|
24 |
require_once($CFG->libdir . '/csvlib.class.php');
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Processor test case.
|
|
|
28 |
*
|
|
|
29 |
* @package tool_uploadcourse
|
|
|
30 |
* @copyright 2013 Frédéric Massart
|
|
|
31 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
32 |
*/
|
|
|
33 |
class processor_test extends \advanced_testcase {
|
|
|
34 |
|
|
|
35 |
public function test_basic() {
|
|
|
36 |
global $DB;
|
|
|
37 |
$this->resetAfterTest(true);
|
|
|
38 |
$this->setAdminUser();
|
|
|
39 |
|
|
|
40 |
$content = array(
|
|
|
41 |
"shortname,fullname,summary",
|
|
|
42 |
"c1,Course 1,Course 1 summary",
|
|
|
43 |
"c2,Course 2,Course 2 summary",
|
|
|
44 |
);
|
|
|
45 |
$content = implode("\n", $content);
|
|
|
46 |
$iid = \csv_import_reader::get_new_iid('uploadcourse');
|
|
|
47 |
$cir = new \csv_import_reader($iid, 'uploadcourse');
|
|
|
48 |
$cir->load_csv_content($content, 'utf-8', 'comma');
|
|
|
49 |
$cir->init();
|
|
|
50 |
|
|
|
51 |
$options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_ALL);
|
|
|
52 |
$defaults = array('category' => '1');
|
|
|
53 |
|
|
|
54 |
$p = new tool_uploadcourse_processor($cir, $options, $defaults);
|
|
|
55 |
$this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
|
|
|
56 |
$this->assertFalse($DB->record_exists('course', array('shortname' => 'c2')));
|
|
|
57 |
$p->execute();
|
|
|
58 |
$this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
|
|
|
59 |
$this->assertTrue($DB->record_exists('course', array('shortname' => 'c2')));
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
public function test_restore_template_course() {
|
|
|
63 |
global $DB;
|
|
|
64 |
$this->resetAfterTest(true);
|
|
|
65 |
$this->setAdminUser();
|
|
|
66 |
|
|
|
67 |
$c1 = $this->getDataGenerator()->create_course();
|
|
|
68 |
$c1f1 = $this->getDataGenerator()->create_module('forum', array('course' => $c1->id));
|
|
|
69 |
|
|
|
70 |
$content = array(
|
|
|
71 |
"shortname,fullname,summary",
|
|
|
72 |
"c2,Course 2,Course 2 summary",
|
|
|
73 |
);
|
|
|
74 |
$content = implode("\n", $content);
|
|
|
75 |
$iid = \csv_import_reader::get_new_iid('uploadcourse');
|
|
|
76 |
$cir = new \csv_import_reader($iid, 'uploadcourse');
|
|
|
77 |
$cir->load_csv_content($content, 'utf-8', 'comma');
|
|
|
78 |
$cir->init();
|
|
|
79 |
|
|
|
80 |
$options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW, 'templatecourse' => $c1->shortname);
|
|
|
81 |
$defaults = array('category' => '1');
|
|
|
82 |
|
|
|
83 |
$p = new tool_uploadcourse_processor($cir, $options, $defaults);
|
|
|
84 |
$this->assertFalse($DB->record_exists('course', array('shortname' => 'c2')));
|
|
|
85 |
$p->execute();
|
|
|
86 |
$c2 = $DB->get_record('course', array('shortname' => 'c2'));
|
|
|
87 |
$modinfo = get_fast_modinfo($c2);
|
|
|
88 |
$found = false;
|
|
|
89 |
foreach ($modinfo->get_cms() as $cmid => $cm) {
|
|
|
90 |
if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
|
|
|
91 |
$found = true;
|
|
|
92 |
break;
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
$this->assertTrue($found);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
public function test_restore_restore_file() {
|
|
|
99 |
global $DB;
|
|
|
100 |
$this->resetAfterTest(true);
|
|
|
101 |
$this->setAdminUser();
|
|
|
102 |
|
|
|
103 |
$content = array(
|
|
|
104 |
"shortname,fullname,summary",
|
|
|
105 |
"c1,Course 1,Course 1 summary",
|
|
|
106 |
);
|
|
|
107 |
$content = implode("\n", $content);
|
|
|
108 |
$iid = \csv_import_reader::get_new_iid('uploadcourse');
|
|
|
109 |
$cir = new \csv_import_reader($iid, 'uploadcourse');
|
|
|
110 |
$cir->load_csv_content($content, 'utf-8', 'comma');
|
|
|
111 |
$cir->init();
|
|
|
112 |
|
|
|
113 |
$options = array(
|
|
|
114 |
'mode' => tool_uploadcourse_processor::MODE_CREATE_NEW,
|
|
|
115 |
'restorefile' => __DIR__ . '/fixtures/backup.mbz',
|
|
|
116 |
'templatecourse' => 'DoesNotExist' // Restorefile takes priority.
|
|
|
117 |
);
|
|
|
118 |
$defaults = array('category' => '1');
|
|
|
119 |
|
|
|
120 |
$p = new tool_uploadcourse_processor($cir, $options, $defaults);
|
|
|
121 |
$this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
|
|
|
122 |
$p->execute();
|
|
|
123 |
$c1 = $DB->get_record('course', array('shortname' => 'c1'));
|
|
|
124 |
$modinfo = get_fast_modinfo($c1);
|
|
|
125 |
$found = false;
|
|
|
126 |
foreach ($modinfo->get_cms() as $cmid => $cm) {
|
|
|
127 |
if ($cm->modname == 'glossary' && $cm->name == 'Imported Glossary') {
|
|
|
128 |
$found = true;
|
|
|
129 |
break;
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
$this->assertTrue($found);
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
public function test_shortname_template() {
|
|
|
136 |
global $DB;
|
|
|
137 |
$this->resetAfterTest(true);
|
|
|
138 |
$this->setAdminUser();
|
|
|
139 |
|
|
|
140 |
$content = array(
|
|
|
141 |
"shortname,fullname,summary,idnumber",
|
|
|
142 |
",Course 1,C1 Summary,ID123",
|
|
|
143 |
);
|
|
|
144 |
$content = implode("\n", $content);
|
|
|
145 |
$iid = \csv_import_reader::get_new_iid('uploadcourse');
|
|
|
146 |
$cir = new \csv_import_reader($iid, 'uploadcourse');
|
|
|
147 |
$cir->load_csv_content($content, 'utf-8', 'comma');
|
|
|
148 |
$cir->init();
|
|
|
149 |
|
|
|
150 |
$options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW, 'shortnametemplate' => '%i: %f');
|
|
|
151 |
$defaults = array('category' => '1');
|
|
|
152 |
|
|
|
153 |
$p = new tool_uploadcourse_processor($cir, $options, $defaults);
|
|
|
154 |
$this->assertFalse($DB->record_exists('course', array('idnumber' => 'ID123')));
|
|
|
155 |
$p->execute();
|
|
|
156 |
$this->assertTrue($DB->record_exists('course', array('idnumber' => 'ID123')));
|
|
|
157 |
$c = $DB->get_record('course', array('idnumber' => 'ID123'));
|
|
|
158 |
$this->assertEquals('ID123: Course 1', $c->shortname);
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
public function test_empty_csv() {
|
|
|
162 |
$this->resetAfterTest(true);
|
|
|
163 |
|
|
|
164 |
$content = array();
|
|
|
165 |
$content = implode("\n", $content);
|
|
|
166 |
$iid = \csv_import_reader::get_new_iid('uploadcourse');
|
|
|
167 |
$cir = new \csv_import_reader($iid, 'uploadcourse');
|
|
|
168 |
$cir->load_csv_content($content, 'utf-8', 'comma');
|
|
|
169 |
$cir->init();
|
|
|
170 |
|
|
|
171 |
$options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW);
|
|
|
172 |
$this->expectException(\moodle_exception::class);
|
|
|
173 |
$p = new tool_uploadcourse_processor($cir, $options, array());
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
public function test_not_enough_columns() {
|
|
|
177 |
$this->resetAfterTest(true);
|
|
|
178 |
|
|
|
179 |
$content = array(
|
|
|
180 |
"shortname",
|
|
|
181 |
"c1",
|
|
|
182 |
);
|
|
|
183 |
$content = implode("\n", $content);
|
|
|
184 |
$iid = \csv_import_reader::get_new_iid('uploadcourse');
|
|
|
185 |
$cir = new \csv_import_reader($iid, 'uploadcourse');
|
|
|
186 |
$cir->load_csv_content($content, 'utf-8', 'comma');
|
|
|
187 |
$cir->init();
|
|
|
188 |
|
|
|
189 |
$options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW);
|
|
|
190 |
$this->expectException(\moodle_exception::class);
|
|
|
191 |
$p = new tool_uploadcourse_processor($cir, $options, array());
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
public function test_preview() {
|
|
|
195 |
global $DB;
|
|
|
196 |
$this->resetAfterTest(true);
|
|
|
197 |
|
|
|
198 |
$content = array(
|
|
|
199 |
"shortname,fullname,summary",
|
|
|
200 |
"c1,Course 1,Course 1 summary",
|
|
|
201 |
"c2,Course 2,Course 2 summary",
|
|
|
202 |
);
|
|
|
203 |
$content = implode("\n", $content);
|
|
|
204 |
$iid = \csv_import_reader::get_new_iid('uploadcourse');
|
|
|
205 |
$cir = new \csv_import_reader($iid, 'uploadcourse');
|
|
|
206 |
$cir->load_csv_content($content, 'utf-8', 'comma');
|
|
|
207 |
$cir->init();
|
|
|
208 |
|
|
|
209 |
$options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_ALL);
|
|
|
210 |
$defaults = array('category' => '1');
|
|
|
211 |
|
|
|
212 |
$p = new tool_uploadcourse_processor($cir, $options, $defaults);
|
|
|
213 |
// Nothing special to expect here, just make sure no exceptions are thrown.
|
|
|
214 |
$p->preview();
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
}
|