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_helper;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Helper test case.
|
|
|
23 |
*
|
|
|
24 |
* @package tool_uploadcourse
|
|
|
25 |
* @copyright 2013 Frédéric Massart
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
27 |
*/
|
|
|
28 |
class helper_test extends \advanced_testcase {
|
|
|
29 |
|
11 |
efrain |
30 |
public function test_generate_shortname(): void {
|
1 |
efrain |
31 |
$data = (object) array('fullname' => 'Ah bh Ch 01 02 03', 'idnumber' => 'ID123');
|
|
|
32 |
|
|
|
33 |
$this->assertSame($data->fullname, tool_uploadcourse_helper::generate_shortname($data, '%f'));
|
|
|
34 |
$this->assertSame($data->idnumber, tool_uploadcourse_helper::generate_shortname($data, '%i'));
|
|
|
35 |
$this->assertSame('Ah Bh Ch', tool_uploadcourse_helper::generate_shortname($data, '%~8f'));
|
|
|
36 |
$this->assertSame('AH BH CH', tool_uploadcourse_helper::generate_shortname($data, '%+8f'));
|
|
|
37 |
$this->assertSame('id123', tool_uploadcourse_helper::generate_shortname($data, '%-i'));
|
|
|
38 |
$this->assertSame('[Ah bh Ch] = ID123', tool_uploadcourse_helper::generate_shortname($data, '[%8f] = %i'));
|
|
|
39 |
$this->assertSame('0', tool_uploadcourse_helper::generate_shortname($data, '0'));
|
|
|
40 |
$this->assertSame('%unknown', tool_uploadcourse_helper::generate_shortname($data, '%unknown'));
|
|
|
41 |
|
|
|
42 |
$this->assertNull(tool_uploadcourse_helper::generate_shortname($data, ''));
|
|
|
43 |
$this->assertNull(tool_uploadcourse_helper::generate_shortname(array(), '%f'));
|
|
|
44 |
}
|
|
|
45 |
|
11 |
efrain |
46 |
public function test_get_course_formats(): void {
|
1 |
efrain |
47 |
$result = tool_uploadcourse_helper::get_course_formats();
|
|
|
48 |
$this->assertSame(array_keys(\core_component::get_plugin_list('format')), $result);
|
|
|
49 |
// Should be similar as first result, as cached.
|
|
|
50 |
$this->assertSame($result, tool_uploadcourse_helper::get_course_formats());
|
|
|
51 |
}
|
|
|
52 |
|
11 |
efrain |
53 |
public function test_get_enrolment_data(): void {
|
1 |
efrain |
54 |
$this->resetAfterTest(true);
|
|
|
55 |
$data = array(
|
|
|
56 |
'enrolment_1' => 'unknown',
|
|
|
57 |
'enrolment_1_foo' => '1',
|
|
|
58 |
'enrolment_1_bar' => '2',
|
|
|
59 |
'enrolment_2' => 'self',
|
|
|
60 |
'enrolment_2_delete' => '1',
|
|
|
61 |
'enrolment_2_foo' => 'a',
|
|
|
62 |
'enrolment_2_bar' => '1',
|
|
|
63 |
'enrolment_3' => 'manual',
|
|
|
64 |
'enrolment_3_disable' => '2',
|
|
|
65 |
'enrolment_3_foo' => 'b',
|
|
|
66 |
'enrolment_3_bar' => '2',
|
|
|
67 |
'enrolment_4' => 'database',
|
|
|
68 |
'enrolment_4_foo' => 'x',
|
|
|
69 |
'enrolment_4_bar' => '3',
|
|
|
70 |
'enrolment_5_test3' => 'test3',
|
|
|
71 |
'enrolment_5_test2' => 'test2',
|
|
|
72 |
'enrolment_5_test1' => 'test1',
|
|
|
73 |
'enrolment_5' => 'flatfile',
|
|
|
74 |
);
|
|
|
75 |
$expected = array(
|
|
|
76 |
'self' => array(
|
|
|
77 |
'delete' => '1',
|
|
|
78 |
'foo' => 'a',
|
|
|
79 |
'bar' => '1',
|
|
|
80 |
),
|
|
|
81 |
'manual' => array(
|
|
|
82 |
'disable' => '2',
|
|
|
83 |
'foo' => 'b',
|
|
|
84 |
'bar' => '2',
|
|
|
85 |
),
|
|
|
86 |
'database' => array(
|
|
|
87 |
'foo' => 'x',
|
|
|
88 |
'bar' => '3',
|
|
|
89 |
),
|
|
|
90 |
'flatfile' => array(
|
|
|
91 |
'test3' => 'test3',
|
|
|
92 |
'test2' => 'test2',
|
|
|
93 |
'test1' => 'test1',
|
|
|
94 |
)
|
|
|
95 |
);
|
|
|
96 |
$this->assertSame(tool_uploadcourse_helper::get_enrolment_data($data), $expected);
|
|
|
97 |
}
|
|
|
98 |
|
11 |
efrain |
99 |
public function test_get_enrolment_plugins(): void {
|
1 |
efrain |
100 |
$this->resetAfterTest(true);
|
|
|
101 |
$actual = tool_uploadcourse_helper::get_enrolment_plugins();
|
|
|
102 |
$this->assertSame(array_keys(enrol_get_plugins(false)), array_keys($actual));
|
|
|
103 |
// This should be identical as cached.
|
|
|
104 |
$secondactual = tool_uploadcourse_helper::get_enrolment_plugins();
|
|
|
105 |
$this->assertEquals($actual, $secondactual);
|
|
|
106 |
}
|
|
|
107 |
|
11 |
efrain |
108 |
public function test_get_restore_content_dir(): void {
|
1 |
efrain |
109 |
global $CFG;
|
|
|
110 |
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
|
|
|
111 |
$this->resetAfterTest(true);
|
|
|
112 |
$this->setAdminUser();
|
|
|
113 |
|
|
|
114 |
$c1 = $this->getDataGenerator()->create_course();
|
|
|
115 |
$c2 = $this->getDataGenerator()->create_course((object) array('shortname' => 'Yay'));
|
|
|
116 |
|
|
|
117 |
// Creating backup file.
|
|
|
118 |
$bc = new \backup_controller(\backup::TYPE_1COURSE, $c1->id, \backup::FORMAT_MOODLE,
|
|
|
119 |
\backup::INTERACTIVE_NO, \backup::MODE_GENERAL, 2);
|
|
|
120 |
$bc->execute_plan();
|
|
|
121 |
$result = $bc->get_results();
|
|
|
122 |
$this->assertTrue(isset($result['backup_destination']));
|
|
|
123 |
$c1backupfile = $result['backup_destination']->copy_content_to_temp();
|
|
|
124 |
$bc->destroy();
|
|
|
125 |
|
|
|
126 |
// Creating backup file.
|
|
|
127 |
$bc = new \backup_controller(\backup::TYPE_1COURSE, $c2->id, \backup::FORMAT_MOODLE,
|
|
|
128 |
\backup::INTERACTIVE_NO, \backup::MODE_GENERAL, 2);
|
|
|
129 |
$bc->execute_plan();
|
|
|
130 |
$result = $bc->get_results();
|
|
|
131 |
$this->assertTrue(isset($result['backup_destination']));
|
|
|
132 |
$c2backupfile = $result['backup_destination']->copy_content_to_temp();
|
|
|
133 |
$bc->destroy();
|
|
|
134 |
|
|
|
135 |
$oldcfg = isset($CFG->keeptempdirectoriesonbackup) ? $CFG->keeptempdirectoriesonbackup : false;
|
|
|
136 |
$CFG->keeptempdirectoriesonbackup = true;
|
|
|
137 |
|
|
|
138 |
// Checking restore dir.
|
|
|
139 |
$dir = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
|
|
|
140 |
$bcinfo = \backup_general_helper::get_backup_information($dir);
|
|
|
141 |
$this->assertEquals($bcinfo->original_course_id, $c1->id);
|
|
|
142 |
$this->assertEquals($bcinfo->original_course_fullname, $c1->fullname);
|
|
|
143 |
|
|
|
144 |
// Do it again, it should be the same directory.
|
|
|
145 |
$dir2 = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
|
|
|
146 |
$this->assertEquals($dir, $dir2);
|
|
|
147 |
|
|
|
148 |
// Get the second course.
|
|
|
149 |
$dir = tool_uploadcourse_helper::get_restore_content_dir($c2backupfile, null);
|
|
|
150 |
$bcinfo = \backup_general_helper::get_backup_information($dir);
|
|
|
151 |
$this->assertEquals($bcinfo->original_course_id, $c2->id);
|
|
|
152 |
$this->assertEquals($bcinfo->original_course_fullname, $c2->fullname);
|
|
|
153 |
|
|
|
154 |
// Checking with a shortname.
|
|
|
155 |
$dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
|
|
|
156 |
$bcinfo = \backup_general_helper::get_backup_information($dir);
|
|
|
157 |
$this->assertEquals($bcinfo->original_course_id, $c1->id);
|
|
|
158 |
$this->assertEquals($bcinfo->original_course_fullname, $c1->fullname);
|
|
|
159 |
|
|
|
160 |
// Do it again, it should be the same directory.
|
|
|
161 |
$dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
|
|
|
162 |
$this->assertEquals($dir, $dir2);
|
|
|
163 |
|
|
|
164 |
// Get the second course.
|
|
|
165 |
$dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c2->shortname);
|
|
|
166 |
$bcinfo = \backup_general_helper::get_backup_information($dir);
|
|
|
167 |
$this->assertEquals($bcinfo->original_course_id, $c2->id);
|
|
|
168 |
$this->assertEquals($bcinfo->original_course_fullname, $c2->fullname);
|
|
|
169 |
|
|
|
170 |
// Get a course that does not exist.
|
|
|
171 |
$errors = array();
|
|
|
172 |
$dir = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors);
|
|
|
173 |
$this->assertFalse($dir);
|
|
|
174 |
$this->assertArrayHasKey('coursetorestorefromdoesnotexist', $errors);
|
|
|
175 |
|
|
|
176 |
// Trying again without caching. $CFG->keeptempdirectoriesonbackup is required for caching.
|
|
|
177 |
$CFG->keeptempdirectoriesonbackup = false;
|
|
|
178 |
|
|
|
179 |
// Checking restore dir.
|
|
|
180 |
$dir = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
|
|
|
181 |
$dir2 = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
|
|
|
182 |
$this->assertNotEquals($dir, $dir2);
|
|
|
183 |
|
|
|
184 |
// Checking with a shortname.
|
|
|
185 |
$dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
|
|
|
186 |
$dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
|
|
|
187 |
$this->assertNotEquals($dir, $dir2);
|
|
|
188 |
|
|
|
189 |
// Get a course that does not exist.
|
|
|
190 |
$errors = array();
|
|
|
191 |
$dir = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors);
|
|
|
192 |
$this->assertFalse($dir);
|
|
|
193 |
$this->assertArrayHasKey('coursetorestorefromdoesnotexist', $errors);
|
|
|
194 |
$dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors);
|
|
|
195 |
$this->assertEquals($dir, $dir2);
|
|
|
196 |
|
|
|
197 |
$CFG->keeptempdirectoriesonbackup = $oldcfg;
|
|
|
198 |
}
|
|
|
199 |
|
11 |
efrain |
200 |
public function test_get_role_ids(): void {
|
1 |
efrain |
201 |
$this->getDataGenerator();
|
|
|
202 |
// Mimic function result.
|
|
|
203 |
$expected = array();
|
|
|
204 |
$roles = get_all_roles();
|
|
|
205 |
foreach ($roles as $role) {
|
|
|
206 |
$expected[$role->shortname] = $role->id;
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
$actual = tool_uploadcourse_helper::get_role_ids();
|
|
|
210 |
$this->assertSame($actual, $expected);
|
|
|
211 |
|
|
|
212 |
// Check cache.
|
|
|
213 |
$this->assertSame($actual, tool_uploadcourse_helper::get_role_ids());
|
|
|
214 |
}
|
|
|
215 |
|
11 |
efrain |
216 |
public function test_get_role_names(): void {
|
1 |
efrain |
217 |
$this->resetAfterTest(true);
|
|
|
218 |
|
|
|
219 |
create_role('Villain', 'villain', 'The bad guys');
|
|
|
220 |
$data = array(
|
|
|
221 |
'role_student' => 'Padawan',
|
|
|
222 |
'role_teacher' => 'Guardian',
|
|
|
223 |
'role_editingteacher' => 'Knight',
|
|
|
224 |
'role_manager' => 'Master',
|
|
|
225 |
'role_villain' => 'Jabba the Hutt',
|
|
|
226 |
'role_android' => 'R2D2',
|
|
|
227 |
);
|
|
|
228 |
|
|
|
229 |
// Get the role IDs, but need to force the cache reset as a new role is defined.
|
|
|
230 |
$roleids = tool_uploadcourse_helper::get_role_ids(true);
|
|
|
231 |
|
|
|
232 |
$expected = array(
|
|
|
233 |
'role_' . $roleids['student'] => 'Padawan',
|
|
|
234 |
'role_' . $roleids['teacher'] => 'Guardian',
|
|
|
235 |
'role_' . $roleids['editingteacher'] => 'Knight',
|
|
|
236 |
'role_' . $roleids['manager'] => 'Master',
|
|
|
237 |
'role_' . $roleids['villain'] => 'Jabba the Hutt',
|
|
|
238 |
);
|
|
|
239 |
|
|
|
240 |
$errors = array();
|
|
|
241 |
$actual = tool_uploadcourse_helper::get_role_names($data, $errors);
|
|
|
242 |
$this->assertSame($actual, $expected);
|
|
|
243 |
$this->assertArrayHasKey('invalidroles', $errors);
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
/**
|
|
|
247 |
* Test custom field data processing
|
|
|
248 |
*/
|
11 |
efrain |
249 |
public function test_get_custom_course_field_data(): void {
|
1 |
efrain |
250 |
global $DB;
|
|
|
251 |
|
|
|
252 |
$this->resetAfterTest();
|
|
|
253 |
|
|
|
254 |
// Create all the fields!
|
|
|
255 |
$category = $this->get_customfield_generator()->create_category();
|
|
|
256 |
|
|
|
257 |
$checkboxfield = $this->create_custom_field($category, 'checkbox', 'mycheckbox');
|
|
|
258 |
$datefield = $this->create_custom_field($category, 'date', 'mydate');
|
|
|
259 |
$selectfield = $this->create_custom_field($category, 'select', 'myselect', ['options' => "Red\nGreen\nBlue"]);
|
|
|
260 |
$textfield = $this->create_custom_field($category, 'text', 'mytext', ['locked' => 1]);
|
|
|
261 |
$textareafield = $this->create_custom_field($category, 'textarea', 'mytextarea');
|
|
|
262 |
|
|
|
263 |
$fields = tool_uploadcourse_helper::get_custom_course_fields();
|
|
|
264 |
$this->assertCount(5, $fields);
|
|
|
265 |
|
|
|
266 |
$this->assertArrayHasKey($checkboxfield->get('shortname'), $fields);
|
|
|
267 |
$this->assertInstanceOf(\customfield_checkbox\field_controller::class, $fields[$checkboxfield->get('shortname')]);
|
|
|
268 |
|
|
|
269 |
$this->assertArrayHasKey($datefield->get('shortname'), $fields);
|
|
|
270 |
$this->assertInstanceOf(\customfield_date\field_controller::class, $fields[$datefield->get('shortname')]);
|
|
|
271 |
|
|
|
272 |
$this->assertArrayHasKey($selectfield->get('shortname'), $fields);
|
|
|
273 |
$this->assertInstanceOf(\customfield_select\field_controller::class, $fields[$selectfield->get('shortname')]);
|
|
|
274 |
|
|
|
275 |
$this->assertArrayHasKey($textfield->get('shortname'), $fields);
|
|
|
276 |
$this->assertInstanceOf(\customfield_text\field_controller::class, $fields[$textfield->get('shortname')]);
|
|
|
277 |
|
|
|
278 |
$this->assertArrayHasKey($textareafield->get('shortname'), $fields);
|
|
|
279 |
$this->assertInstanceOf(\customfield_textarea\field_controller::class, $fields[$textareafield->get('shortname')]);
|
|
|
280 |
|
|
|
281 |
$data = [
|
|
|
282 |
'customfield_mycheckbox' => '1',
|
|
|
283 |
'customfield_mydate' => '2019-10-01',
|
|
|
284 |
'customfield_myselect' => 'Green',
|
|
|
285 |
'customfield_mytext' => 'Hello',
|
|
|
286 |
'customfield_myunknownfield' => 'Goodbye',
|
|
|
287 |
];
|
|
|
288 |
|
|
|
289 |
$expected = [
|
|
|
290 |
'customfield_mycheckbox' => '1',
|
|
|
291 |
'customfield_mydate' => strtotime('2019-10-01'),
|
|
|
292 |
'customfield_myselect' => 2,
|
|
|
293 |
'customfield_mytext' => 'Hello',
|
|
|
294 |
];
|
|
|
295 |
|
|
|
296 |
$course = $this->getDataGenerator()->create_course();
|
|
|
297 |
$user = $this->getDataGenerator()->create_and_enrol($course, 'manager');
|
|
|
298 |
$this->setUser($user);
|
|
|
299 |
|
|
|
300 |
$context = \context_course::instance($course->id);
|
|
|
301 |
|
|
|
302 |
$this->assertEquals($expected, tool_uploadcourse_helper::get_custom_course_field_data($data, [], $context));
|
|
|
303 |
|
|
|
304 |
// Now add our custom textarea field (separately because the value of it's 'itemid' element is unknown).
|
|
|
305 |
$data['customfield_mytextarea'] = 'Something';
|
|
|
306 |
$fields = tool_uploadcourse_helper::get_custom_course_field_data($data, [], $context);
|
|
|
307 |
$this->assertArrayHasKey('customfield_mytextarea_editor', $fields);
|
|
|
308 |
$this->assertArrayHasKey('text', $fields['customfield_mytextarea_editor']);
|
|
|
309 |
$this->assertEquals('Something', $fields['customfield_mytextarea_editor']['text']);
|
|
|
310 |
|
|
|
311 |
// Now prohibit the capability to change locked fields for the manager role.
|
|
|
312 |
$managerrole = $DB->get_record('role', ['shortname' => 'manager']);
|
|
|
313 |
role_change_permission($managerrole->id, $context, 'moodle/course:changelockedcustomfields', CAP_PROHIBIT);
|
|
|
314 |
|
|
|
315 |
// The locked 'mytext' custom field should not be returned.
|
|
|
316 |
$fields = tool_uploadcourse_helper::get_custom_course_field_data($data, [], $context);
|
|
|
317 |
$this->assertCount(4, $fields);
|
|
|
318 |
$this->assertArrayNotHasKey('customfield_mytext', $fields);
|
|
|
319 |
}
|
|
|
320 |
|
11 |
efrain |
321 |
public function test_increment_idnumber(): void {
|
1 |
efrain |
322 |
$this->resetAfterTest(true);
|
|
|
323 |
|
|
|
324 |
$c1 = $this->getDataGenerator()->create_course(array('idnumber' => 'C1'));
|
|
|
325 |
$c2 = $this->getDataGenerator()->create_course(array('idnumber' => 'C2'));
|
|
|
326 |
$c3 = $this->getDataGenerator()->create_course(array('idnumber' => 'Yo'));
|
|
|
327 |
|
|
|
328 |
$this->assertEquals('C3', tool_uploadcourse_helper::increment_idnumber('C1'));
|
|
|
329 |
$this->assertEquals('Yo_2', tool_uploadcourse_helper::increment_idnumber('Yo'));
|
|
|
330 |
$this->assertEquals('DoesNotExist', tool_uploadcourse_helper::increment_idnumber('DoesNotExist'));
|
|
|
331 |
}
|
|
|
332 |
|
11 |
efrain |
333 |
public function test_increment_shortname(): void {
|
1 |
efrain |
334 |
$this->resetAfterTest(true);
|
|
|
335 |
|
|
|
336 |
$c1 = $this->getDataGenerator()->create_course(array('shortname' => 'C1'));
|
|
|
337 |
$c2 = $this->getDataGenerator()->create_course(array('shortname' => 'C2'));
|
|
|
338 |
$c3 = $this->getDataGenerator()->create_course(array('shortname' => 'Yo'));
|
|
|
339 |
|
|
|
340 |
// FYI: increment_shortname assumes that the course exists, and so increment the shortname immediately.
|
|
|
341 |
$this->assertEquals('C3', tool_uploadcourse_helper::increment_shortname('C1'));
|
|
|
342 |
$this->assertEquals('Yo_2', tool_uploadcourse_helper::increment_shortname('Yo'));
|
|
|
343 |
$this->assertEquals('DoesNotExist_2', tool_uploadcourse_helper::increment_shortname('DoesNotExist'));
|
|
|
344 |
}
|
|
|
345 |
|
11 |
efrain |
346 |
public function test_resolve_category(): void {
|
1 |
efrain |
347 |
$this->resetAfterTest(true);
|
|
|
348 |
|
|
|
349 |
$c1 = $this->getDataGenerator()->create_category(array('name' => 'First level'));
|
|
|
350 |
$c2 = $this->getDataGenerator()->create_category(array('name' => 'Second level', 'parent' => $c1->id));
|
|
|
351 |
$c3 = $this->getDataGenerator()->create_category(array('idnumber' => 'C3'));
|
|
|
352 |
|
|
|
353 |
$data = array(
|
|
|
354 |
'category' => $c1->id,
|
|
|
355 |
'category_path' => $c1->name . ' / ' . $c2->name,
|
|
|
356 |
'category_idnumber' => $c3->idnumber,
|
|
|
357 |
);
|
|
|
358 |
|
|
|
359 |
$this->assertEquals($c1->id, tool_uploadcourse_helper::resolve_category($data));
|
|
|
360 |
unset($data['category']);
|
|
|
361 |
$this->assertEquals($c3->id, tool_uploadcourse_helper::resolve_category($data));
|
|
|
362 |
unset($data['category_idnumber']);
|
|
|
363 |
$this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category($data));
|
|
|
364 |
|
|
|
365 |
// Adding unexisting data.
|
|
|
366 |
$errors = array();
|
|
|
367 |
$data['category_idnumber'] = 1234;
|
|
|
368 |
$this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category($data, $errors));
|
|
|
369 |
$this->assertArrayHasKey('couldnotresolvecatgorybyidnumber', $errors);
|
|
|
370 |
$errors = array();
|
|
|
371 |
$data['category'] = 1234;
|
|
|
372 |
$this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category($data, $errors));
|
|
|
373 |
$this->assertArrayHasKey('couldnotresolvecatgorybyid', $errors);
|
|
|
374 |
$errors = array();
|
|
|
375 |
$data['category_path'] = 'Not exist';
|
|
|
376 |
$this->assertEmpty(tool_uploadcourse_helper::resolve_category($data, $errors));
|
|
|
377 |
$this->assertArrayHasKey('couldnotresolvecatgorybypath', $errors);
|
|
|
378 |
}
|
|
|
379 |
|
11 |
efrain |
380 |
public function test_resolve_category_by_idnumber(): void {
|
1 |
efrain |
381 |
$this->resetAfterTest(true);
|
|
|
382 |
|
|
|
383 |
$c1 = $this->getDataGenerator()->create_category(array('idnumber' => 'C1'));
|
|
|
384 |
$c2 = $this->getDataGenerator()->create_category(array('idnumber' => 'C2'));
|
|
|
385 |
$c3 = $this->getDataGenerator()->create_category(['idnumber' => 'C3', 'visible' => false]);
|
|
|
386 |
|
|
|
387 |
// Doubled for cache check.
|
|
|
388 |
$this->assertEquals($c1->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C1'));
|
|
|
389 |
$this->assertEquals($c1->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C1'));
|
|
|
390 |
$this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C2'));
|
|
|
391 |
$this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C2'));
|
|
|
392 |
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_idnumber('C3'));
|
|
|
393 |
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_idnumber('C3'));
|
|
|
394 |
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_idnumber('DoesNotExist'));
|
|
|
395 |
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_idnumber('DoesNotExist'));
|
|
|
396 |
}
|
|
|
397 |
|
11 |
efrain |
398 |
public function test_resolve_category_by_path(): void {
|
1 |
efrain |
399 |
$this->resetAfterTest(true);
|
|
|
400 |
|
|
|
401 |
$cat1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1'));
|
|
|
402 |
$cat1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.1', 'parent' => $cat1->id));
|
|
|
403 |
$cat1_1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.1.1', 'parent' => $cat1_1->id));
|
|
|
404 |
$cat1_1_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.1.2', 'parent' => $cat1_1->id));
|
|
|
405 |
$cat1_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.2', 'parent' => $cat1->id));
|
|
|
406 |
|
|
|
407 |
$cat2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2'));
|
|
|
408 |
$cat2_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.1', 'parent' => $cat2->id, 'visible' => false));
|
|
|
409 |
$cat2_1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.1.1', 'parent' => $cat2_1->id));
|
|
|
410 |
$cat2_1_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.1.2', 'parent' => $cat2_1->id));
|
|
|
411 |
$cat2_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.2', 'parent' => $cat2->id));
|
|
|
412 |
|
|
|
413 |
$cat3 = $this->getDataGenerator()->create_category(array('name' => 'Cat 3'));
|
|
|
414 |
$cat3_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1 Doubled', 'parent' => $cat3->id));
|
|
|
415 |
$cat3_1b = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1 Doubled', 'parent' => $cat3->id));
|
|
|
416 |
$cat3_1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1.1', 'parent' => $cat3_1->id));
|
|
|
417 |
$cat3_fakedouble = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1.1', 'parent' => $cat3->id));
|
|
|
418 |
|
|
|
419 |
// Existing categories. Doubled for cache testing.
|
|
|
420 |
$path = array('Cat 1');
|
|
|
421 |
$this->assertEquals($cat1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
|
|
|
422 |
$this->assertEquals($cat1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
|
|
|
423 |
|
|
|
424 |
$path = array('Cat 1', 'Cat 1.1', 'Cat 1.1.2');
|
|
|
425 |
$this->assertEquals($cat1_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
|
|
|
426 |
$this->assertEquals($cat1_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
|
|
|
427 |
|
|
|
428 |
$path = array('Cat 1', 'Cat 1.2');
|
|
|
429 |
$this->assertEquals($cat1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
|
|
|
430 |
$this->assertEquals($cat1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
|
|
|
431 |
|
|
|
432 |
$path = array('Cat 2');
|
|
|
433 |
$this->assertEquals($cat2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
|
|
|
434 |
$this->assertEquals($cat2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
|
|
|
435 |
|
|
|
436 |
// Hidden category.
|
|
|
437 |
$path = array('Cat 2', 'Cat 2.1');
|
|
|
438 |
$this->assertEquals($cat2_1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
|
|
|
439 |
$this->assertEquals($cat2_1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
|
|
|
440 |
|
|
|
441 |
// Hidden parent.
|
|
|
442 |
$path = array('Cat 2', 'Cat 2.1', 'Cat 2.1.2');
|
|
|
443 |
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
|
|
|
444 |
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
|
|
|
445 |
|
|
|
446 |
// Does not exist.
|
|
|
447 |
$path = array('No cat 3', 'Cat 1.2');
|
|
|
448 |
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
|
|
|
449 |
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
|
|
|
450 |
|
|
|
451 |
$path = array('Cat 2', 'Cat 2.x');
|
|
|
452 |
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
|
|
|
453 |
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
|
|
|
454 |
|
|
|
455 |
// Name conflict.
|
|
|
456 |
$path = array('Cat 3', 'Cat 3.1 Doubled');
|
|
|
457 |
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
|
|
|
458 |
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
|
|
|
459 |
|
|
|
460 |
$path = array('Cat 3', 'Cat 3.1 Doubled', 'Cat 3.1.1');
|
|
|
461 |
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
|
|
|
462 |
$this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
|
|
|
463 |
|
|
|
464 |
$path = array('Cat 3', 'Cat 3.1.1');
|
|
|
465 |
$this->assertEquals($cat3_fakedouble->id, tool_uploadcourse_helper::resolve_category_by_path($path));
|
|
|
466 |
$this->assertEquals($cat3_fakedouble->id, tool_uploadcourse_helper::resolve_category_by_path($path));
|
|
|
467 |
}
|
|
|
468 |
|
|
|
469 |
/**
|
|
|
470 |
* Get custom field plugin generator
|
|
|
471 |
*
|
|
|
472 |
* @return core_customfield_generator
|
|
|
473 |
*/
|
|
|
474 |
protected function get_customfield_generator(): \core_customfield_generator {
|
|
|
475 |
return $this->getDataGenerator()->get_plugin_generator('core_customfield');
|
|
|
476 |
}
|
|
|
477 |
|
|
|
478 |
/**
|
|
|
479 |
* Helper method to create custom course field
|
|
|
480 |
*
|
|
|
481 |
* @param \core_customfield\category_controller $category
|
|
|
482 |
* @param string $type
|
|
|
483 |
* @param string $shortname
|
|
|
484 |
* @param array $configdata
|
|
|
485 |
* @return \core_customfield\field_controller
|
|
|
486 |
*/
|
|
|
487 |
protected function create_custom_field(\core_customfield\category_controller $category, string $type, string $shortname,
|
|
|
488 |
array $configdata = []): \core_customfield\field_controller {
|
|
|
489 |
|
|
|
490 |
return $this->get_customfield_generator()->create_field([
|
|
|
491 |
'categoryid' => $category->get('id'),
|
|
|
492 |
'type' => $type,
|
|
|
493 |
'shortname' => $shortname,
|
|
|
494 |
'configdata' => $configdata,
|
|
|
495 |
]);
|
|
|
496 |
}
|
|
|
497 |
}
|