[ Index ] |
PHP Cross Reference of moodle-2.8 |
[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 * Unit tests for the moodle1 converter 19 * 20 * @package core_backup 21 * @subpackage backup-convert 22 * @category phpunit 23 * @copyright 2011 Mark Nielsen <[email protected]> 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 25 */ 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 global $CFG; 30 require_once($CFG->dirroot . '/backup/converter/moodle1/lib.php'); 31 32 33 class core_backup_moodle1_converter_testcase extends advanced_testcase { 34 35 /** @var string the name of the directory containing the unpacked Moodle 1.9 backup */ 36 protected $tempdir; 37 38 /** @var string saved hash of an icon file used during testing */ 39 protected $iconhash; 40 41 protected function setUp() { 42 global $CFG; 43 44 $this->tempdir = convert_helper::generate_id('unittest'); 45 check_dir_exists("$CFG->tempdir/backup/$this->tempdir/course_files/sub1"); 46 check_dir_exists("$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/7"); 47 copy( 48 "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/moodle.xml", 49 "$CFG->tempdir/backup/$this->tempdir/moodle.xml" 50 ); 51 copy( 52 "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif", 53 "$CFG->tempdir/backup/$this->tempdir/course_files/file1.gif" 54 ); 55 copy( 56 "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif", 57 "$CFG->tempdir/backup/$this->tempdir/course_files/sub1/file2.gif" 58 ); 59 copy( 60 "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif", 61 "$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/file1.gif" 62 ); 63 copy( 64 "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif", 65 "$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/icon.gif" 66 ); 67 $this->iconhash = sha1_file($CFG->tempdir.'/backup/'.$this->tempdir.'/moddata/unittest/4/icon.gif'); 68 copy( 69 "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif", 70 "$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/7/icon.gif" 71 ); 72 } 73 74 protected function tearDown() { 75 global $CFG; 76 if (empty($CFG->keeptempdirectoriesonbackup)) { 77 fulldelete("$CFG->tempdir/backup/$this->tempdir"); 78 } 79 } 80 81 public function test_detect_format() { 82 $detected = moodle1_converter::detect_format($this->tempdir); 83 $this->assertEquals(backup::FORMAT_MOODLE1, $detected); 84 } 85 86 public function test_convert_factory() { 87 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 88 $this->assertInstanceOf('moodle1_converter', $converter); 89 } 90 91 public function test_stash_storage_not_created() { 92 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 93 $this->setExpectedException('moodle1_convert_storage_exception'); 94 $converter->set_stash('tempinfo', 12); 95 } 96 97 public function test_stash_requiring_empty_stash() { 98 $this->resetAfterTest(true); 99 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 100 $converter->create_stash_storage(); 101 $converter->set_stash('tempinfo', 12); 102 $this->setExpectedException('moodle1_convert_empty_storage_exception'); 103 try { 104 $converter->get_stash('anothertempinfo'); 105 106 } catch (moodle1_convert_empty_storage_exception $e) { 107 // we must drop the storage here so we are able to re-create it in the next test 108 $converter->drop_stash_storage(); 109 throw new moodle1_convert_empty_storage_exception('rethrowing'); 110 } 111 } 112 113 public function test_stash_storage() { 114 $this->resetAfterTest(true); 115 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 116 $converter->create_stash_storage(); 117 118 // no implicit stashes 119 $stashes = $converter->get_stash_names(); 120 $this->assertEquals(gettype($stashes), 'array'); 121 $this->assertTrue(empty($stashes)); 122 123 // test stashes without itemid 124 $converter->set_stash('tempinfo1', 12); 125 $converter->set_stash('tempinfo2', array('a' => 2, 'b' => 3)); 126 $stashes = $converter->get_stash_names(); 127 $this->assertEquals('array', gettype($stashes)); 128 $this->assertEquals(2, count($stashes)); 129 $this->assertTrue(in_array('tempinfo1', $stashes)); 130 $this->assertTrue(in_array('tempinfo2', $stashes)); 131 $this->assertEquals(12, $converter->get_stash('tempinfo1')); 132 $this->assertEquals(array('a' => 2, 'b' => 3), $converter->get_stash('tempinfo2')); 133 134 // overwriting a stashed value is allowed 135 $converter->set_stash('tempinfo1', '13'); 136 $this->assertNotSame(13, $converter->get_stash('tempinfo1')); 137 $this->assertSame('13', $converter->get_stash('tempinfo1')); 138 139 // repeated reading is allowed 140 $this->assertEquals('13', $converter->get_stash('tempinfo1')); 141 142 // storing empty array 143 $converter->set_stash('empty_array_stash', array()); 144 $restored = $converter->get_stash('empty_array_stash'); 145 //$this->assertEquals(gettype($restored), 'array'); // todo return null now, this needs MDL-27713 to be fixed, then uncomment 146 $this->assertTrue(empty($restored)); 147 148 // test stashes with itemid 149 $converter->set_stash('tempinfo', 'Hello', 1); 150 $converter->set_stash('tempinfo', 'World', 2); 151 $this->assertSame('Hello', $converter->get_stash('tempinfo', 1)); 152 $this->assertSame('World', $converter->get_stash('tempinfo', 2)); 153 154 // test get_stash_itemids() 155 $ids = $converter->get_stash_itemids('course_fileref'); 156 $this->assertEquals(gettype($ids), 'array'); 157 $this->assertTrue(empty($ids)); 158 159 $converter->set_stash('course_fileref', null, 34); 160 $converter->set_stash('course_fileref', null, 52); 161 $ids = $converter->get_stash_itemids('course_fileref'); 162 $this->assertEquals(2, count($ids)); 163 $this->assertTrue(in_array(34, $ids)); 164 $this->assertTrue(in_array(52, $ids)); 165 166 $converter->drop_stash_storage(); 167 } 168 169 public function test_get_stash_or_default() { 170 $this->resetAfterTest(true); 171 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 172 $converter->create_stash_storage(); 173 174 $this->assertTrue(is_null($converter->get_stash_or_default('stashname'))); 175 $this->assertTrue(is_null($converter->get_stash_or_default('stashname', 7))); 176 $this->assertTrue('default' === $converter->get_stash_or_default('stashname', 0, 'default')); 177 $this->assertTrue(array('foo', 'bar') === $converter->get_stash_or_default('stashname', 42, array('foo', 'bar'))); 178 179 //$converter->set_stash('stashname', 0); 180 //$this->assertFalse(is_null($converter->get_stash_or_default('stashname'))); // todo returns true now, this needs MDL-27713 to be fixed 181 182 //$converter->set_stash('stashname', ''); 183 //$this->assertFalse(is_null($converter->get_stash_or_default('stashname'))); // todo returns true now, this needs MDL-27713 to be fixed 184 185 //$converter->set_stash('stashname', array()); 186 //$this->assertFalse(is_null($converter->get_stash_or_default('stashname'))); // todo returns true now, this needs MDL-27713 to be fixed 187 188 $converter->set_stash('stashname', 42); 189 $this->assertTrue(42 === $converter->get_stash_or_default('stashname')); 190 $this->assertTrue(is_null($converter->get_stash_or_default('stashname', 1))); 191 $this->assertTrue(42 === $converter->get_stash_or_default('stashname', 0, 61)); 192 193 $converter->set_stash('stashname', array(42 => (object)array('id' => 42)), 18); 194 $stashed = $converter->get_stash_or_default('stashname', 18, 1984); 195 $this->assertEquals(gettype($stashed), 'array'); 196 $this->assertTrue(is_object($stashed[42])); 197 $this->assertTrue($stashed[42]->id === 42); 198 199 $converter->drop_stash_storage(); 200 } 201 202 public function test_get_contextid() { 203 $this->resetAfterTest(true); 204 205 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 206 207 // stash storage must be created in advance 208 $converter->create_stash_storage(); 209 210 // ids are generated on the first call 211 $id1 = $converter->get_contextid(CONTEXT_BLOCK, 10); 212 $id2 = $converter->get_contextid(CONTEXT_BLOCK, 11); 213 $id3 = $converter->get_contextid(CONTEXT_MODULE, 10); 214 215 $this->assertNotEquals($id1, $id2); 216 $this->assertNotEquals($id1, $id3); 217 $this->assertNotEquals($id2, $id3); 218 219 // and then re-used if called with the same params 220 $this->assertEquals($id1, $converter->get_contextid(CONTEXT_BLOCK, 10)); 221 $this->assertEquals($id2, $converter->get_contextid(CONTEXT_BLOCK, 11)); 222 $this->assertEquals($id3, $converter->get_contextid(CONTEXT_MODULE, 10)); 223 224 // for system and course level, the instance is irrelevant 225 // as we need only one system and one course 226 $id1 = $converter->get_contextid(CONTEXT_COURSE); 227 $id2 = $converter->get_contextid(CONTEXT_COURSE, 10); 228 $id3 = $converter->get_contextid(CONTEXT_COURSE, 14); 229 230 $this->assertEquals($id1, $id2); 231 $this->assertEquals($id1, $id3); 232 233 $id1 = $converter->get_contextid(CONTEXT_SYSTEM); 234 $id2 = $converter->get_contextid(CONTEXT_SYSTEM, 11); 235 $id3 = $converter->get_contextid(CONTEXT_SYSTEM, 15); 236 237 $this->assertEquals($id1, $id2); 238 $this->assertEquals($id1, $id3); 239 240 $converter->drop_stash_storage(); 241 } 242 243 public function test_get_nextid() { 244 $this->resetAfterTest(true); 245 246 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 247 248 $id1 = $converter->get_nextid(); 249 $id2 = $converter->get_nextid(); 250 $id3 = $converter->get_nextid(); 251 252 $this->assertTrue(0 < $id1); 253 $this->assertTrue($id1 < $id2); 254 $this->assertTrue($id2 < $id3); 255 } 256 257 public function test_migrate_file() { 258 $this->resetAfterTest(true); 259 260 // set-up the file manager 261 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 262 $converter->create_stash_storage(); 263 $contextid = $converter->get_contextid(CONTEXT_MODULE, 32); 264 $fileman = $converter->get_file_manager($contextid, 'mod_unittest', 'testarea'); 265 // this fileman has not converted anything yet 266 $fileids = $fileman->get_fileids(); 267 $this->assertEquals(gettype($fileids), 'array'); 268 $this->assertEquals(0, count($fileids)); 269 // try to migrate an invalid file 270 $fileman->itemid = 1; 271 $thrown = false; 272 try { 273 $fileman->migrate_file('/../../../../../../../../../../../../../../etc/passwd'); 274 } catch (moodle1_convert_exception $e) { 275 $thrown = true; 276 } 277 $this->assertTrue($thrown); 278 // migrate a single file 279 $fileman->itemid = 4; 280 $fileman->migrate_file('moddata/unittest/4/icon.gif'); 281 $subdir = substr($this->iconhash, 0, 2); 282 $this->assertTrue(is_file($converter->get_workdir_path().'/files/'.$subdir.'/'.$this->iconhash)); 283 // get the file id 284 $fileids = $fileman->get_fileids(); 285 $this->assertEquals(gettype($fileids), 'array'); 286 $this->assertEquals(1, count($fileids)); 287 // migrate another single file into another file area 288 $fileman->filearea = 'anotherarea'; 289 $fileman->itemid = 7; 290 $fileman->migrate_file('moddata/unittest/4/7/icon.gif', '/', 'renamed.gif'); 291 // get the file records 292 $filerecordids = $converter->get_stash_itemids('files'); 293 foreach ($filerecordids as $filerecordid) { 294 $filerecord = $converter->get_stash('files', $filerecordid); 295 $this->assertEquals($this->iconhash, $filerecord['contenthash']); 296 $this->assertEquals($contextid, $filerecord['contextid']); 297 $this->assertEquals('mod_unittest', $filerecord['component']); 298 if ($filerecord['filearea'] === 'testarea') { 299 $this->assertEquals(4, $filerecord['itemid']); 300 $this->assertEquals('icon.gif', $filerecord['filename']); 301 } 302 } 303 // explicitly clear the list of migrated files 304 $this->assertTrue(count($fileman->get_fileids()) > 0); 305 $fileman->reset_fileids(); 306 $this->assertTrue(count($fileman->get_fileids()) == 0); 307 $converter->drop_stash_storage(); 308 } 309 310 public function test_migrate_directory() { 311 $this->resetAfterTest(true); 312 313 // Set-up the file manager. 314 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 315 $converter->create_stash_storage(); 316 $contextid = $converter->get_contextid(CONTEXT_MODULE, 32); 317 $fileman = $converter->get_file_manager($contextid, 'mod_unittest', 'testarea'); 318 // This fileman has not converted anything yet. 319 $fileids = $fileman->get_fileids(); 320 $this->assertEquals(gettype($fileids), 'array'); 321 $this->assertEquals(0, count($fileids)); 322 // Try to migrate a non-existing directory. 323 $returned = $fileman->migrate_directory('not/existing/directory'); 324 $this->assertEquals(gettype($returned), 'array'); 325 $this->assertEquals(0, count($returned)); 326 $fileids = $fileman->get_fileids(); 327 $this->assertEquals(gettype($fileids), 'array'); 328 $this->assertEquals(0, count($fileids)); 329 // Try to migrate whole course_files. 330 $returned = $fileman->migrate_directory('course_files'); 331 $this->assertEquals(gettype($returned), 'array'); 332 $this->assertEquals(4, count($returned)); // Two files, two directories. 333 $fileids = $fileman->get_fileids(); 334 $this->assertEquals(gettype($fileids), 'array'); 335 $this->assertEquals(4, count($fileids)); 336 $subdir = substr($this->iconhash, 0, 2); 337 $this->assertTrue(is_file($converter->get_workdir_path().'/files/'.$subdir.'/'.$this->iconhash)); 338 339 // Check the file records. 340 $files = array(); 341 $filerecordids = $converter->get_stash_itemids('files'); 342 foreach ($filerecordids as $filerecordid) { 343 $filerecord = $converter->get_stash('files', $filerecordid); 344 $files[$filerecord['filepath'].$filerecord['filename']] = $filerecord; 345 } 346 $this->assertEquals('array', gettype($files['/.'])); 347 $this->assertEquals('array', gettype($files['/file1.gif'])); 348 $this->assertEquals('array', gettype($files['/sub1/.'])); 349 $this->assertEquals('array', gettype($files['/sub1/file2.gif'])); 350 $this->assertEquals(sha1(''), $files['/.']['contenthash']); 351 $this->assertEquals(sha1(''), $files['/sub1/.']['contenthash']); 352 $this->assertEquals($this->iconhash, $files['/file1.gif']['contenthash']); 353 $this->assertEquals($this->iconhash, $files['/sub1/file2.gif']['contenthash']); 354 355 $converter->drop_stash_storage(); 356 } 357 358 public function test_migrate_directory_with_trailing_slash() { 359 $this->resetAfterTest(true); 360 361 // Set-up the file manager. 362 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 363 $converter->create_stash_storage(); 364 $contextid = $converter->get_contextid(CONTEXT_MODULE, 32); 365 $fileman = $converter->get_file_manager($contextid, 'mod_unittest', 'testarea'); 366 // Try to migrate a subdirectory passed with the trailing slash. 367 $returned = $fileman->migrate_directory('course_files/sub1/'); 368 // Debugging message must be thrown in this case. 369 $this->assertDebuggingCalled(null, DEBUG_DEVELOPER); 370 $this->assertEquals(gettype($returned), 'array'); 371 $this->assertEquals(2, count($returned)); // One file, one directory. 372 373 $converter->drop_stash_storage(); 374 } 375 376 public function test_convert_path() { 377 $path = new convert_path('foo_bar', '/ROOT/THINGS/FOO/BAR'); 378 $this->assertEquals('foo_bar', $path->get_name()); 379 $this->assertEquals('/ROOT/THINGS/FOO/BAR', $path->get_path()); 380 $this->assertEquals('process_foo_bar', $path->get_processing_method()); 381 $this->assertEquals('on_foo_bar_start', $path->get_start_method()); 382 $this->assertEquals('on_foo_bar_end', $path->get_end_method()); 383 } 384 385 public function test_convert_path_implicit_recipes() { 386 $path = new convert_path('foo_bar', '/ROOT/THINGS/FOO/BAR'); 387 $data = array( 388 'ID' => 76, 389 'ELOY' => 'stronk7', 390 'MARTIN' => 'moodler', 391 'EMPTY' => null, 392 ); 393 // apply default recipes (converting keys to lowercase) 394 $data = $path->apply_recipes($data); 395 $this->assertEquals(4, count($data)); 396 $this->assertEquals(76, $data['id']); 397 $this->assertEquals('stronk7', $data['eloy']); 398 $this->assertEquals('moodler', $data['martin']); 399 $this->assertSame(null, $data['empty']); 400 } 401 402 public function test_convert_path_explicit_recipes() { 403 $path = new convert_path( 404 'foo_bar', '/ROOT/THINGS/FOO/BAR', 405 array( 406 'newfields' => array( 407 'david' => 'mudrd8mz', 408 'petr' => 'skodak', 409 ), 410 'renamefields' => array( 411 'empty' => 'nothing', 412 ), 413 'dropfields' => array( 414 'id' 415 ), 416 ) 417 ); 418 $data = array( 419 'ID' => 76, 420 'ELOY' => 'stronk7', 421 'MARTIN' => 'moodler', 422 'EMPTY' => null, 423 ); 424 $data = $path->apply_recipes($data); 425 426 $this->assertEquals(5, count($data)); 427 $this->assertFalse(array_key_exists('id', $data)); 428 $this->assertEquals('stronk7', $data['eloy']); 429 $this->assertEquals('moodler', $data['martin']); 430 $this->assertEquals('mudrd8mz', $data['david']); 431 $this->assertEquals('skodak', $data['petr']); 432 $this->assertSame(null, $data['nothing']); 433 } 434 435 public function test_grouped_data_on_nongrouped_convert_path() { 436 // prepare some grouped data 437 $data = array( 438 'ID' => 77, 439 'NAME' => 'Pale lagers', 440 'BEERS' => array( 441 array( 442 'BEER' => array( 443 'ID' => 67, 444 'NAME' => 'Pilsner Urquell', 445 ) 446 ), 447 array( 448 'BEER' => array( 449 'ID' => 34, 450 'NAME' => 'Heineken', 451 ) 452 ), 453 ) 454 ); 455 456 // declare a non-grouped path 457 $path = new convert_path('beer_style', '/ROOT/BEER_STYLES/BEER_STYLE'); 458 459 // an attempt to apply recipes throws exception because we do not expect grouped data 460 $this->setExpectedException('convert_path_exception'); 461 $data = $path->apply_recipes($data); 462 } 463 464 public function test_grouped_convert_path_with_recipes() { 465 // prepare some grouped data 466 $data = array( 467 'ID' => 77, 468 'NAME' => 'Pale lagers', 469 'BEERS' => array( 470 array( 471 'BEER' => array( 472 'ID' => 67, 473 'NAME' => 'Pilsner Urquell', 474 ) 475 ), 476 array( 477 'BEER' => array( 478 'ID' => 34, 479 'NAME' => 'Heineken', 480 ) 481 ), 482 ) 483 ); 484 485 // implict recipes work for grouped data if the path is declared as grouped 486 $path = new convert_path('beer_style', '/ROOT/BEER_STYLES/BEER_STYLE', array(), true); 487 $data = $path->apply_recipes($data); 488 $this->assertEquals('Heineken', $data['beers'][1]['beer']['name']); 489 490 // an attempt to provide explicit recipes on grouped elements throws exception 491 $this->setExpectedException('convert_path_exception'); 492 $path = new convert_path( 493 'beer_style', '/ROOT/BEER_STYLES/BEER_STYLE', 494 array( 495 'renamefields' => array( 496 'name' => 'beername', // note this is confusing recipe because the 'name' is used for both 497 // beer-style name ('Pale lagers') and beer name ('Pilsner Urquell') 498 ) 499 ), true); 500 } 501 502 public function test_referenced_course_files() { 503 504 $text = 'This is a text containing links to file.php 505 as it is parsed from the backup file. <br /><br /><img border="0" width="110" vspace="0" hspace="0" height="92" title="News" alt="News" src="$@FILEPHP@$$@SLASH@$pics$@SLASH@$news.gif" /><a href="$@FILEPHP@$$@SLASH@$pics$@SLASH@$news.gif$@FORCEDOWNLOAD@$">download image</a><br /> 506 <div><a href=\'$@FILEPHP@$/../../../../../../../../../../../../../../../etc/passwd\'>download passwords</a></div> 507 <div><a href=\'$@FILEPHP@$$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$etc$@SLASH@$shadow\'>download shadows</a></div> 508 <br /><a href=\'$@FILEPHP@$$@SLASH@$MANUAL.DOC$@FORCEDOWNLOAD@$\'>download manual</a><br />'; 509 510 $files = moodle1_converter::find_referenced_files($text); 511 $this->assertEquals(gettype($files), 'array'); 512 $this->assertEquals(2, count($files)); 513 $this->assertTrue(in_array('/pics/news.gif', $files)); 514 $this->assertTrue(in_array('/MANUAL.DOC', $files)); 515 516 $text = moodle1_converter::rewrite_filephp_usage($text, array('/pics/news.gif', '/another/file/notused.txt')); 517 $this->assertEquals($text, 'This is a text containing links to file.php 518 as it is parsed from the backup file. <br /><br /><img border="0" width="110" vspace="0" hspace="0" height="92" title="News" alt="News" src="@@PLUGINFILE@@/pics/news.gif" /><a href="@@PLUGINFILE@@/pics/news.gif?forcedownload=1">download image</a><br /> 519 <div><a href=\'$@FILEPHP@$/../../../../../../../../../../../../../../../etc/passwd\'>download passwords</a></div> 520 <div><a href=\'$@FILEPHP@$$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$..$@SLASH@$etc$@SLASH@$shadow\'>download shadows</a></div> 521 <br /><a href=\'$@FILEPHP@$$@SLASH@$MANUAL.DOC$@FORCEDOWNLOAD@$\'>download manual</a><br />'); 522 } 523 524 public function test_referenced_files_urlencoded() { 525 526 $text = 'This is a text containing links to file.php 527 as it is parsed from the backup file. <br /><br /><img border="0" width="110" vspace="0" hspace="0" height="92" title="News" alt="News" src="$@FILEPHP@$$@SLASH@$pics$@SLASH@$news.gif" /><a href="$@FILEPHP@$$@SLASH@$pics$@SLASH@$news.gif$@FORCEDOWNLOAD@$">no space</a><br /> 528 <br /><a href=\'$@FILEPHP@$$@SLASH@$pics$@SLASH@$news%20with%20spaces.gif$@FORCEDOWNLOAD@$\'>with urlencoded spaces</a><br /> 529 <a href="$@FILEPHP@$$@SLASH@$illegal%20pics%2Bmovies$@SLASH@$romeo%2Bjuliet.avi">Download the full AVI for free! (space and plus encoded)</a> 530 <a href="$@FILEPHP@$$@SLASH@$illegal pics+movies$@SLASH@$romeo+juliet.avi">Download the full AVI for free! (none encoded)</a> 531 <a href="$@FILEPHP@$$@SLASH@$illegal%20pics+movies$@SLASH@$romeo+juliet.avi">Download the full AVI for free! (only space encoded)</a> 532 <a href="$@FILEPHP@$$@SLASH@$illegal pics%2Bmovies$@SLASH@$romeo%2Bjuliet.avi">Download the full AVI for free! (only plus)</a>'; 533 534 $files = moodle1_converter::find_referenced_files($text); 535 $this->assertEquals(gettype($files), 'array'); 536 $this->assertEquals(3, count($files)); 537 $this->assertTrue(in_array('/pics/news.gif', $files)); 538 $this->assertTrue(in_array('/pics/news with spaces.gif', $files)); 539 $this->assertTrue(in_array('/illegal pics+movies/romeo+juliet.avi', $files)); 540 541 $text = moodle1_converter::rewrite_filephp_usage($text, $files); 542 $this->assertEquals('This is a text containing links to file.php 543 as it is parsed from the backup file. <br /><br /><img border="0" width="110" vspace="0" hspace="0" height="92" title="News" alt="News" src="@@PLUGINFILE@@/pics/news.gif" /><a href="@@PLUGINFILE@@/pics/news.gif?forcedownload=1">no space</a><br /> 544 <br /><a href=\'@@PLUGINFILE@@/pics/news%20with%20spaces.gif?forcedownload=1\'>with urlencoded spaces</a><br /> 545 <a href="@@PLUGINFILE@@/illegal%20pics%2Bmovies/romeo%2Bjuliet.avi">Download the full AVI for free! (space and plus encoded)</a> 546 <a href="@@PLUGINFILE@@/illegal%20pics%2Bmovies/romeo%2Bjuliet.avi">Download the full AVI for free! (none encoded)</a> 547 <a href="$@FILEPHP@$$@SLASH@$illegal%20pics+movies$@SLASH@$romeo+juliet.avi">Download the full AVI for free! (only space encoded)</a> 548 <a href="$@FILEPHP@$$@SLASH@$illegal pics%2Bmovies$@SLASH@$romeo%2Bjuliet.avi">Download the full AVI for free! (only plus)</a>', $text); 549 } 550 551 public function test_question_bank_conversion() { 552 global $CFG; 553 554 $this->resetAfterTest(true); 555 556 copy( 557 "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/questions.xml", 558 "$CFG->tempdir/backup/$this->tempdir/moodle.xml" 559 ); 560 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 561 $converter->convert(); 562 } 563 564 public function test_convert_run_convert() { 565 $this->resetAfterTest(true); 566 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 567 $converter->convert(); 568 } 569 570 public function test_inforef_manager() { 571 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 572 $inforef = $converter->get_inforef_manager('unittest'); 573 $inforef->add_ref('file', 45); 574 $inforef->add_refs('file', array(46, 47)); 575 // todo test the write_refs() via some dummy xml_writer 576 $this->setExpectedException('coding_exception'); 577 $inforef->add_ref('unknown_referenced_item_name', 76); 578 } 579 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |