[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 /** 18 * File containing tests for the helper. 19 * 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 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 global $CFG; 28 29 /** 30 * Helper test case. 31 * 32 * @package tool_uploadcourse 33 * @copyright 2013 Frédéric Massart 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class tool_uploadcourse_helper_testcase extends advanced_testcase { 37 38 public function test_generate_shortname() { 39 $data = (object) array('fullname' => 'Ah bh Ch 01 02 03', 'idnumber' => 'ID123'); 40 41 $this->assertSame($data->fullname, tool_uploadcourse_helper::generate_shortname($data, '%f')); 42 $this->assertSame($data->idnumber, tool_uploadcourse_helper::generate_shortname($data, '%i')); 43 $this->assertSame('Ah Bh Ch', tool_uploadcourse_helper::generate_shortname($data, '%~8f')); 44 $this->assertSame('AH BH CH', tool_uploadcourse_helper::generate_shortname($data, '%+8f')); 45 $this->assertSame('id123', tool_uploadcourse_helper::generate_shortname($data, '%-i')); 46 $this->assertSame('[Ah bh Ch] = ID123', tool_uploadcourse_helper::generate_shortname($data, '[%8f] = %i')); 47 $this->assertSame('0', tool_uploadcourse_helper::generate_shortname($data, '0')); 48 $this->assertSame('%unknown', tool_uploadcourse_helper::generate_shortname($data, '%unknown')); 49 50 $this->assertNull(tool_uploadcourse_helper::generate_shortname($data, '')); 51 $this->assertNull(tool_uploadcourse_helper::generate_shortname(array(), '%f')); 52 } 53 54 public function test_get_course_formats() { 55 $result = tool_uploadcourse_helper::get_course_formats(); 56 $this->assertSame(array_keys(core_component::get_plugin_list('format')), $result); 57 // Should be similar as first result, as cached. 58 $this->assertSame($result, tool_uploadcourse_helper::get_course_formats()); 59 } 60 61 public function test_get_enrolment_data() { 62 $this->resetAfterTest(true); 63 $data = array( 64 'enrolment_1' => 'unknown', 65 'enrolment_1_foo' => '1', 66 'enrolment_1_bar' => '2', 67 'enrolment_2' => 'self', 68 'enrolment_2_delete' => '1', 69 'enrolment_2_foo' => 'a', 70 'enrolment_2_bar' => '1', 71 'enrolment_3' => 'manual', 72 'enrolment_3_disable' => '2', 73 'enrolment_3_foo' => 'b', 74 'enrolment_3_bar' => '2', 75 'enrolment_4' => 'database', 76 'enrolment_4_foo' => 'x', 77 'enrolment_4_bar' => '3', 78 'enrolment_5_test3' => 'test3', 79 'enrolment_5_test2' => 'test2', 80 'enrolment_5_test1' => 'test1', 81 'enrolment_5' => 'flatfile', 82 ); 83 $expected = array( 84 'self' => array( 85 'delete' => '1', 86 'foo' => 'a', 87 'bar' => '1', 88 ), 89 'manual' => array( 90 'disable' => '2', 91 'foo' => 'b', 92 'bar' => '2', 93 ), 94 'database' => array( 95 'foo' => 'x', 96 'bar' => '3', 97 ), 98 'flatfile' => array( 99 'test3' => 'test3', 100 'test2' => 'test2', 101 'test1' => 'test1', 102 ) 103 ); 104 $this->assertSame(tool_uploadcourse_helper::get_enrolment_data($data), $expected); 105 } 106 107 public function test_get_enrolment_plugins() { 108 $this->resetAfterTest(true); 109 $actual = tool_uploadcourse_helper::get_enrolment_plugins(); 110 $this->assertSame(array_keys(enrol_get_plugins(false)), array_keys($actual)); 111 // This should be identical as cached. 112 $secondactual = tool_uploadcourse_helper::get_enrolment_plugins(); 113 $this->assertEquals($actual, $secondactual); 114 } 115 116 public function test_get_restore_content_dir() { 117 global $CFG; 118 $this->resetAfterTest(true); 119 $this->setAdminUser(); 120 121 $c1 = $this->getDataGenerator()->create_course(); 122 $c2 = $this->getDataGenerator()->create_course((object) array('shortname' => 'Yay')); 123 124 // Creating backup file. 125 $bc = new backup_controller(backup::TYPE_1COURSE, $c1->id, backup::FORMAT_MOODLE, 126 backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2); 127 $bc->execute_plan(); 128 $result = $bc->get_results(); 129 $this->assertTrue(isset($result['backup_destination'])); 130 $c1backupfile = $result['backup_destination']->copy_content_to_temp(); 131 $bc->destroy(); 132 133 // Creating backup file. 134 $bc = new backup_controller(backup::TYPE_1COURSE, $c2->id, backup::FORMAT_MOODLE, 135 backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2); 136 $bc->execute_plan(); 137 $result = $bc->get_results(); 138 $this->assertTrue(isset($result['backup_destination'])); 139 $c2backupfile = $result['backup_destination']->copy_content_to_temp(); 140 $bc->destroy(); 141 142 $oldcfg = isset($CFG->keeptempdirectoriesonbackup) ? $CFG->keeptempdirectoriesonbackup : false; 143 $CFG->keeptempdirectoriesonbackup = true; 144 145 // Checking restore dir. 146 $dir = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null); 147 $bcinfo = backup_general_helper::get_backup_information($dir); 148 $this->assertEquals($bcinfo->original_course_id, $c1->id); 149 $this->assertEquals($bcinfo->original_course_fullname, $c1->fullname); 150 151 // Do it again, it should be the same directory. 152 $dir2 = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null); 153 $this->assertEquals($dir, $dir2); 154 155 // Get the second course. 156 $dir = tool_uploadcourse_helper::get_restore_content_dir($c2backupfile, null); 157 $bcinfo = backup_general_helper::get_backup_information($dir); 158 $this->assertEquals($bcinfo->original_course_id, $c2->id); 159 $this->assertEquals($bcinfo->original_course_fullname, $c2->fullname); 160 161 // Checking with a shortname. 162 $dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname); 163 $bcinfo = backup_general_helper::get_backup_information($dir); 164 $this->assertEquals($bcinfo->original_course_id, $c1->id); 165 $this->assertEquals($bcinfo->original_course_fullname, $c1->fullname); 166 167 // Do it again, it should be the same directory. 168 $dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname); 169 $this->assertEquals($dir, $dir2); 170 171 // Get the second course. 172 $dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c2->shortname); 173 $bcinfo = backup_general_helper::get_backup_information($dir); 174 $this->assertEquals($bcinfo->original_course_id, $c2->id); 175 $this->assertEquals($bcinfo->original_course_fullname, $c2->fullname); 176 177 // Get a course that does not exist. 178 $errors = array(); 179 $dir = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors); 180 $this->assertFalse($dir); 181 $this->assertArrayHasKey('coursetorestorefromdoesnotexist', $errors); 182 183 // Trying again without caching. $CFG->keeptempdirectoriesonbackup is required for caching. 184 $CFG->keeptempdirectoriesonbackup = false; 185 186 // Checking restore dir. 187 $dir = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null); 188 $dir2 = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null); 189 $this->assertNotEquals($dir, $dir2); 190 191 // Checking with a shortname. 192 $dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname); 193 $dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname); 194 $this->assertNotEquals($dir, $dir2); 195 196 // Get a course that does not exist. 197 $errors = array(); 198 $dir = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors); 199 $this->assertFalse($dir); 200 $this->assertArrayHasKey('coursetorestorefromdoesnotexist', $errors); 201 $dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors); 202 $this->assertEquals($dir, $dir2); 203 204 $CFG->keeptempdirectoriesonbackup = $oldcfg; 205 } 206 207 public function test_get_role_ids() { 208 $this->getDataGenerator(); 209 // Mimic function result. 210 $expected = array(); 211 $roles = get_all_roles(); 212 foreach ($roles as $role) { 213 $expected[$role->shortname] = $role->id; 214 } 215 216 $actual = tool_uploadcourse_helper::get_role_ids(); 217 $this->assertSame($actual, $expected); 218 219 // Check cache. 220 $this->assertSame($actual, tool_uploadcourse_helper::get_role_ids()); 221 } 222 223 public function test_get_role_names() { 224 $this->resetAfterTest(true); 225 226 create_role('Villain', 'villain', 'The bad guys'); 227 $data = array( 228 'role_student' => 'Padawan', 229 'role_teacher' => 'Guardian', 230 'role_editingteacher' => 'Knight', 231 'role_manager' => 'Master', 232 'role_villain' => 'Jabba the Hutt', 233 'role_android' => 'R2D2', 234 ); 235 236 // Get the role IDs, but need to force the cache reset as a new role is defined. 237 $roleids = tool_uploadcourse_helper::get_role_ids(true); 238 239 $expected = array( 240 'role_' . $roleids['student'] => 'Padawan', 241 'role_' . $roleids['teacher'] => 'Guardian', 242 'role_' . $roleids['editingteacher'] => 'Knight', 243 'role_' . $roleids['manager'] => 'Master', 244 'role_' . $roleids['villain'] => 'Jabba the Hutt', 245 ); 246 247 $errors = array(); 248 $actual = tool_uploadcourse_helper::get_role_names($data, $errors); 249 $this->assertSame($actual, $expected); 250 $this->assertArrayHasKey('invalidroles', $errors); 251 } 252 253 public function test_increment_idnumber() { 254 $this->resetAfterTest(true); 255 256 $c1 = $this->getDataGenerator()->create_course(array('idnumber' => 'C1')); 257 $c2 = $this->getDataGenerator()->create_course(array('idnumber' => 'C2')); 258 $c3 = $this->getDataGenerator()->create_course(array('idnumber' => 'Yo')); 259 260 $this->assertEquals('C3', tool_uploadcourse_helper::increment_idnumber('C1')); 261 $this->assertEquals('Yo_2', tool_uploadcourse_helper::increment_idnumber('Yo')); 262 $this->assertEquals('DoesNotExist', tool_uploadcourse_helper::increment_idnumber('DoesNotExist')); 263 } 264 265 public function test_increment_shortname() { 266 $this->resetAfterTest(true); 267 268 $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'C1')); 269 $c2 = $this->getDataGenerator()->create_course(array('shortname' => 'C2')); 270 $c3 = $this->getDataGenerator()->create_course(array('shortname' => 'Yo')); 271 272 // FYI: increment_shortname assumes that the course exists, and so increment the shortname immediately. 273 $this->assertEquals('C3', tool_uploadcourse_helper::increment_shortname('C1')); 274 $this->assertEquals('Yo_2', tool_uploadcourse_helper::increment_shortname('Yo')); 275 $this->assertEquals('DoesNotExist_2', tool_uploadcourse_helper::increment_shortname('DoesNotExist')); 276 } 277 278 public function test_resolve_category() { 279 $this->resetAfterTest(true); 280 281 $c1 = $this->getDataGenerator()->create_category(array('name' => 'First level')); 282 $c2 = $this->getDataGenerator()->create_category(array('name' => 'Second level', 'parent' => $c1->id)); 283 $c3 = $this->getDataGenerator()->create_category(array('idnumber' => 'C3')); 284 285 $data = array( 286 'category' => $c1->id, 287 'category_path' => $c1->name . ' / ' . $c2->name, 288 'category_idnumber' => $c3->idnumber, 289 ); 290 291 $this->assertEquals($c1->id, tool_uploadcourse_helper::resolve_category($data)); 292 unset($data['category']); 293 $this->assertEquals($c3->id, tool_uploadcourse_helper::resolve_category($data)); 294 unset($data['category_idnumber']); 295 $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category($data)); 296 297 // Adding unexisting data. 298 $errors = array(); 299 $data['category_idnumber'] = 1234; 300 $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category($data, $errors)); 301 $this->assertArrayHasKey('couldnotresolvecatgorybyidnumber', $errors); 302 $errors = array(); 303 $data['category'] = 1234; 304 $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category($data, $errors)); 305 $this->assertArrayHasKey('couldnotresolvecatgorybyid', $errors); 306 $errors = array(); 307 $data['category_path'] = 'Not exist'; 308 $this->assertEmpty(tool_uploadcourse_helper::resolve_category($data, $errors)); 309 $this->assertArrayHasKey('couldnotresolvecatgorybypath', $errors); 310 } 311 312 public function test_resolve_category_by_idnumber() { 313 $this->resetAfterTest(true); 314 315 $c1 = $this->getDataGenerator()->create_category(array('idnumber' => 'C1')); 316 $c2 = $this->getDataGenerator()->create_category(array('idnumber' => 'C2')); 317 318 // Doubled for cache check. 319 $this->assertEquals($c1->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C1')); 320 $this->assertEquals($c1->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C1')); 321 $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C2')); 322 $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C2')); 323 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_idnumber('DoesNotExist')); 324 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_idnumber('DoesNotExist')); 325 } 326 327 public function test_resolve_category_by_path() { 328 $this->resetAfterTest(true); 329 330 $cat1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1')); 331 $cat1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.1', 'parent' => $cat1->id)); 332 $cat1_1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.1.1', 'parent' => $cat1_1->id)); 333 $cat1_1_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.1.2', 'parent' => $cat1_1->id)); 334 $cat1_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.2', 'parent' => $cat1->id)); 335 336 $cat2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2')); 337 $cat2_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.1', 'parent' => $cat2->id, 'visible' => false)); 338 $cat2_1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.1.1', 'parent' => $cat2_1->id)); 339 $cat2_1_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.1.2', 'parent' => $cat2_1->id)); 340 $cat2_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.2', 'parent' => $cat2->id)); 341 342 $cat3 = $this->getDataGenerator()->create_category(array('name' => 'Cat 3')); 343 $cat3_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1 Doubled', 'parent' => $cat3->id)); 344 $cat3_1b = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1 Doubled', 'parent' => $cat3->id)); 345 $cat3_1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1.1', 'parent' => $cat3_1->id)); 346 $cat3_fakedouble = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1.1', 'parent' => $cat3->id)); 347 348 // Existing categories. Doubled for cache testing. 349 $path = array('Cat 1'); 350 $this->assertEquals($cat1->id, tool_uploadcourse_helper::resolve_category_by_path($path)); 351 $this->assertEquals($cat1->id, tool_uploadcourse_helper::resolve_category_by_path($path)); 352 353 $path = array('Cat 1', 'Cat 1.1', 'Cat 1.1.2'); 354 $this->assertEquals($cat1_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path)); 355 $this->assertEquals($cat1_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path)); 356 357 $path = array('Cat 1', 'Cat 1.2'); 358 $this->assertEquals($cat1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path)); 359 $this->assertEquals($cat1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path)); 360 361 $path = array('Cat 2'); 362 $this->assertEquals($cat2->id, tool_uploadcourse_helper::resolve_category_by_path($path)); 363 $this->assertEquals($cat2->id, tool_uploadcourse_helper::resolve_category_by_path($path)); 364 365 // Hidden category. 366 $path = array('Cat 2', 'Cat 2.1'); 367 $this->assertEquals($cat2_1->id, tool_uploadcourse_helper::resolve_category_by_path($path)); 368 $this->assertEquals($cat2_1->id, tool_uploadcourse_helper::resolve_category_by_path($path)); 369 370 // Hidden parent. 371 $path = array('Cat 2', 'Cat 2.1', 'Cat 2.1.2'); 372 $this->assertEquals($cat2_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path)); 373 $this->assertEquals($cat2_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path)); 374 375 // Does not exist. 376 $path = array('No cat 3', 'Cat 1.2'); 377 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path)); 378 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path)); 379 380 $path = array('Cat 2', 'Cat 2.x'); 381 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path)); 382 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path)); 383 384 // Name conflict. 385 $path = array('Cat 3', 'Cat 3.1 Doubled'); 386 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path)); 387 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path)); 388 389 $path = array('Cat 3', 'Cat 3.1 Doubled', 'Cat 3.1.1'); 390 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path)); 391 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path)); 392 393 $path = array('Cat 3', 'Cat 3.1.1'); 394 $this->assertEquals($cat3_fakedouble->id, tool_uploadcourse_helper::resolve_category_by_path($path)); 395 $this->assertEquals($cat3_fakedouble->id, tool_uploadcourse_helper::resolve_category_by_path($path)); 396 } 397 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |