|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 require_once($CFG->dirroot . '/backup/converter/moodle1/lib.php'); 00030 00031 class moodle1_converter_test extends UnitTestCase { 00032 00033 public static $includecoverage = array(); 00034 00036 protected $tempdir; 00037 00038 public function setUp() { 00039 global $CFG; 00040 00041 $this->tempdir = convert_helper::generate_id('simpletest'); 00042 check_dir_exists("$CFG->tempdir/backup/$this->tempdir/course_files/sub1"); 00043 check_dir_exists("$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/7"); 00044 copy( 00045 "$CFG->dirroot/backup/converter/moodle1/simpletest/files/moodle.xml", 00046 "$CFG->tempdir/backup/$this->tempdir/moodle.xml" 00047 ); 00048 copy( 00049 "$CFG->dirroot/backup/converter/moodle1/simpletest/files/icon.gif", 00050 "$CFG->tempdir/backup/$this->tempdir/course_files/file1.gif" 00051 ); 00052 copy( 00053 "$CFG->dirroot/backup/converter/moodle1/simpletest/files/icon.gif", 00054 "$CFG->tempdir/backup/$this->tempdir/course_files/sub1/file2.gif" 00055 ); 00056 copy( 00057 "$CFG->dirroot/backup/converter/moodle1/simpletest/files/icon.gif", 00058 "$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/file1.gif" 00059 ); 00060 copy( 00061 "$CFG->dirroot/backup/converter/moodle1/simpletest/files/icon.gif", 00062 "$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/icon.gif" 00063 ); 00064 copy( 00065 "$CFG->dirroot/backup/converter/moodle1/simpletest/files/icon.gif", 00066 "$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/7/icon.gif" 00067 ); 00068 } 00069 00070 public function tearDown() { 00071 global $CFG; 00072 if (empty($CFG->keeptempdirectoriesonbackup)) { 00073 fulldelete("$CFG->tempdir/backup/$this->tempdir"); 00074 } 00075 } 00076 00077 public function test_detect_format() { 00078 $detected = moodle1_converter::detect_format($this->tempdir); 00079 $this->assertEqual(backup::FORMAT_MOODLE1, $detected); 00080 } 00081 00082 public function test_convert_factory() { 00083 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 00084 $this->assertIsA($converter, 'moodle1_converter'); 00085 } 00086 00087 public function test_stash_storage_not_created() { 00088 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 00089 $this->expectException('moodle1_convert_storage_exception'); 00090 $converter->set_stash('tempinfo', 12); 00091 } 00092 00093 public function test_stash_requiring_empty_stash() { 00094 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 00095 $converter->create_stash_storage(); 00096 $converter->set_stash('tempinfo', 12); 00097 $this->expectException('moodle1_convert_empty_storage_exception'); 00098 try { 00099 $converter->get_stash('anothertempinfo'); 00100 00101 } catch (moodle1_convert_empty_storage_exception $e) { 00102 // we must drop the storage here so we are able to re-create it in the next test 00103 $converter->drop_stash_storage(); 00104 throw new moodle1_convert_empty_storage_exception('rethrowing'); 00105 } 00106 } 00107 00108 public function test_stash_storage() { 00109 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 00110 $converter->create_stash_storage(); 00111 00112 // no implicit stashes 00113 $stashes = $converter->get_stash_names(); 00114 $this->assertIsA($stashes, 'array'); 00115 $this->assertTrue(empty($stashes)); 00116 00117 // test stashes without itemid 00118 $converter->set_stash('tempinfo1', 12); 00119 $converter->set_stash('tempinfo2', array('a' => 2, 'b' => 3)); 00120 $stashes = $converter->get_stash_names(); 00121 $this->assertIsA($stashes, 'array'); 00122 $this->assertEqual(2, count($stashes)); 00123 $this->assertTrue(in_array('tempinfo1', $stashes)); 00124 $this->assertTrue(in_array('tempinfo2', $stashes)); 00125 $this->assertIdentical(12, $converter->get_stash('tempinfo1')); 00126 $this->assertIdentical(array('a' => 2, 'b' => 3), $converter->get_stash('tempinfo2')); 00127 00128 // overwriting a stashed value is allowed 00129 $converter->set_stash('tempinfo1', '13'); 00130 $this->assertNotIdentical(13, $converter->get_stash('tempinfo1')); 00131 $this->assertIdentical('13', $converter->get_stash('tempinfo1')); 00132 00133 // repeated reading is allowed 00134 $this->assertIdentical('13', $converter->get_stash('tempinfo1')); 00135 00136 // storing empty array 00137 $converter->set_stash('empty_array_stash', array()); 00138 $restored = $converter->get_stash('empty_array_stash'); 00139 //$this->assertIsA($restored, 'array'); // todo return null now, this needs MDL-27713 to be fixed, then uncomment 00140 $this->assertTrue(empty($restored)); 00141 00142 // test stashes with itemid 00143 $converter->set_stash('tempinfo', 'Hello', 1); 00144 $converter->set_stash('tempinfo', 'World', 2); 00145 $this->assertIdentical('Hello', $converter->get_stash('tempinfo', 1)); 00146 $this->assertIdentical('World', $converter->get_stash('tempinfo', 2)); 00147 00148 // test get_stash_itemids() 00149 $ids = $converter->get_stash_itemids('course_fileref'); 00150 $this->assertIsA($ids, 'array'); 00151 $this->assertTrue(empty($ids)); 00152 00153 $converter->set_stash('course_fileref', null, 34); 00154 $converter->set_stash('course_fileref', null, 52); 00155 $ids = $converter->get_stash_itemids('course_fileref'); 00156 $this->assertEqual(2, count($ids)); 00157 $this->assertTrue(in_array(34, $ids)); 00158 $this->assertTrue(in_array(52, $ids)); 00159 00160 $converter->drop_stash_storage(); 00161 } 00162 00163 public function test_get_stash_or_default() { 00164 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 00165 $converter->create_stash_storage(); 00166 00167 $this->assertTrue(is_null($converter->get_stash_or_default('stashname'))); 00168 $this->assertTrue(is_null($converter->get_stash_or_default('stashname', 7))); 00169 $this->assertTrue('default' === $converter->get_stash_or_default('stashname', 0, 'default')); 00170 $this->assertTrue(array('foo', 'bar') === $converter->get_stash_or_default('stashname', 42, array('foo', 'bar'))); 00171 00172 //$converter->set_stash('stashname', 0); 00173 //$this->assertFalse(is_null($converter->get_stash_or_default('stashname'))); // todo returns true now, this needs MDL-27713 to be fixed 00174 00175 //$converter->set_stash('stashname', ''); 00176 //$this->assertFalse(is_null($converter->get_stash_or_default('stashname'))); // todo returns true now, this needs MDL-27713 to be fixed 00177 00178 //$converter->set_stash('stashname', array()); 00179 //$this->assertFalse(is_null($converter->get_stash_or_default('stashname'))); // todo returns true now, this needs MDL-27713 to be fixed 00180 00181 $converter->set_stash('stashname', 42); 00182 $this->assertTrue(42 === $converter->get_stash_or_default('stashname')); 00183 $this->assertTrue(is_null($converter->get_stash_or_default('stashname', 1))); 00184 $this->assertTrue(42 === $converter->get_stash_or_default('stashname', 0, 61)); 00185 00186 $converter->set_stash('stashname', array(42 => (object)array('id' => 42)), 18); 00187 $stashed = $converter->get_stash_or_default('stashname', 18, 1984); 00188 $this->assertIsA($stashed, 'array'); 00189 $this->assertTrue(is_object($stashed[42])); 00190 $this->assertTrue($stashed[42]->id === 42); 00191 00192 $converter->drop_stash_storage(); 00193 } 00194 00195 public function test_get_contextid() { 00196 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 00197 00198 // stash storage must be created in advance 00199 $converter->create_stash_storage(); 00200 00201 // ids are generated on the first call 00202 $id1 = $converter->get_contextid(CONTEXT_BLOCK, 10); 00203 $id2 = $converter->get_contextid(CONTEXT_BLOCK, 11); 00204 $id3 = $converter->get_contextid(CONTEXT_MODULE, 10); 00205 00206 $this->assertNotEqual($id1, $id2); 00207 $this->assertNotEqual($id1, $id3); 00208 $this->assertNotEqual($id2, $id3); 00209 00210 // and then re-used if called with the same params 00211 $this->assertEqual($id1, $converter->get_contextid(CONTEXT_BLOCK, 10)); 00212 $this->assertEqual($id2, $converter->get_contextid(CONTEXT_BLOCK, 11)); 00213 $this->assertEqual($id3, $converter->get_contextid(CONTEXT_MODULE, 10)); 00214 00215 // for system and course level, the instance is irrelevant 00216 // as we need only one system and one course 00217 $id1 = $converter->get_contextid(CONTEXT_COURSE); 00218 $id2 = $converter->get_contextid(CONTEXT_COURSE, 10); 00219 $id3 = $converter->get_contextid(CONTEXT_COURSE, 14); 00220 00221 $this->assertEqual($id1, $id2); 00222 $this->assertEqual($id1, $id3); 00223 00224 $id1 = $converter->get_contextid(CONTEXT_SYSTEM); 00225 $id2 = $converter->get_contextid(CONTEXT_SYSTEM, 11); 00226 $id3 = $converter->get_contextid(CONTEXT_SYSTEM, 15); 00227 00228 $this->assertEqual($id1, $id2); 00229 $this->assertEqual($id1, $id3); 00230 00231 $converter->drop_stash_storage(); 00232 } 00233 00234 public function test_get_nextid() { 00235 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 00236 00237 $id1 = $converter->get_nextid(); 00238 $id2 = $converter->get_nextid(); 00239 $id3 = $converter->get_nextid(); 00240 00241 $this->assertTrue(0 < $id1); 00242 $this->assertTrue($id1 < $id2); 00243 $this->assertTrue($id2 < $id3); 00244 } 00245 00246 public function test_migrate_file() { 00247 // set-up the file manager 00248 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 00249 $converter->create_stash_storage(); 00250 $contextid = $converter->get_contextid(CONTEXT_MODULE, 32); 00251 $fileman = $converter->get_file_manager($contextid, 'mod_unittest', 'testarea'); 00252 // this fileman has not converted anything yet 00253 $fileids = $fileman->get_fileids(); 00254 $this->assertIsA($fileids, 'array'); 00255 $this->assertEqual(0, count($fileids)); 00256 // try to migrate a non-existing directory 00257 $returned = $fileman->migrate_directory('not/existing/directory'); 00258 $this->assertIsA($returned, 'array'); 00259 $this->assertEqual(0, count($returned)); 00260 $fileids = $fileman->get_fileids(); 00261 $this->assertIsA($fileids, 'array'); 00262 $this->assertEqual(0, count($fileids)); 00263 // migrate a single file 00264 $fileman->itemid = 4; 00265 $fileman->migrate_file('moddata/unittest/4/icon.gif'); 00266 $this->assertTrue(is_file($converter->get_workdir_path().'/files/4e/4ea114b0558f53e3af8dd9afc0e0810a95c2a724')); 00267 // get the file id 00268 $fileids = $fileman->get_fileids(); 00269 $this->assertIsA($fileids, 'array'); 00270 $this->assertEqual(1, count($fileids)); 00271 // migrate another single file into another file area 00272 $fileman->filearea = 'anotherarea'; 00273 $fileman->itemid = 7; 00274 $fileman->migrate_file('moddata/unittest/4/7/icon.gif', '/', 'renamed.gif'); 00275 // get the file records 00276 $filerecordids = $converter->get_stash_itemids('files'); 00277 foreach ($filerecordids as $filerecordid) { 00278 $filerecord = $converter->get_stash('files', $filerecordid); 00279 $this->assertEqual('4ea114b0558f53e3af8dd9afc0e0810a95c2a724', $filerecord['contenthash']); 00280 $this->assertEqual($contextid, $filerecord['contextid']); 00281 $this->assertEqual('mod_unittest', $filerecord['component']); 00282 if ($filerecord['filearea'] === 'testarea') { 00283 $this->assertEqual(4, $filerecord['itemid']); 00284 $this->assertEqual('icon.gif', $filerecord['filename']); 00285 } 00286 } 00287 // explicitly clear the list of migrated files 00288 $this->assertTrue(count($fileman->get_fileids()) > 0); 00289 $fileman->reset_fileids(); 00290 $this->assertTrue(count($fileman->get_fileids()) == 0); 00291 $converter->drop_stash_storage(); 00292 } 00293 00294 public function test_convert_path() { 00295 $path = new convert_path('foo_bar', '/ROOT/THINGS/FOO/BAR'); 00296 $this->assertEqual('foo_bar', $path->get_name()); 00297 $this->assertEqual('/ROOT/THINGS/FOO/BAR', $path->get_path()); 00298 $this->assertEqual('process_foo_bar', $path->get_processing_method()); 00299 $this->assertEqual('on_foo_bar_start', $path->get_start_method()); 00300 $this->assertEqual('on_foo_bar_end', $path->get_end_method()); 00301 } 00302 00303 public function test_convert_path_implicit_recipes() { 00304 $path = new convert_path('foo_bar', '/ROOT/THINGS/FOO/BAR'); 00305 $data = array( 00306 'ID' => 76, 00307 'ELOY' => 'stronk7', 00308 'MARTIN' => 'moodler', 00309 'EMPTY' => null, 00310 ); 00311 // apply default recipes (converting keys to lowercase) 00312 $data = $path->apply_recipes($data); 00313 $this->assertEqual(4, count($data)); 00314 $this->assertEqual(76, $data['id']); 00315 $this->assertEqual('stronk7', $data['eloy']); 00316 $this->assertEqual('moodler', $data['martin']); 00317 $this->assertIdentical(null, $data['empty']); 00318 } 00319 00320 public function test_convert_path_explicit_recipes() { 00321 $path = new convert_path( 00322 'foo_bar', '/ROOT/THINGS/FOO/BAR', 00323 array( 00324 'newfields' => array( 00325 'david' => 'mudrd8mz', 00326 'petr' => 'skodak', 00327 ), 00328 'renamefields' => array( 00329 'empty' => 'nothing', 00330 ), 00331 'dropfields' => array( 00332 'id' 00333 ), 00334 ) 00335 ); 00336 $data = array( 00337 'ID' => 76, 00338 'ELOY' => 'stronk7', 00339 'MARTIN' => 'moodler', 00340 'EMPTY' => null, 00341 ); 00342 $data = $path->apply_recipes($data); 00343 00344 $this->assertEqual(5, count($data)); 00345 $this->assertFalse(array_key_exists('id', $data)); 00346 $this->assertEqual('stronk7', $data['eloy']); 00347 $this->assertEqual('moodler', $data['martin']); 00348 $this->assertEqual('mudrd8mz', $data['david']); 00349 $this->assertEqual('skodak', $data['petr']); 00350 $this->assertIdentical(null, $data['nothing']); 00351 } 00352 00353 public function test_grouped_data_on_nongrouped_convert_path() { 00354 // prepare some grouped data 00355 $data = array( 00356 'ID' => 77, 00357 'NAME' => 'Pale lagers', 00358 'BEERS' => array( 00359 array( 00360 'BEER' => array( 00361 'ID' => 67, 00362 'NAME' => 'Pilsner Urquell', 00363 ) 00364 ), 00365 array( 00366 'BEER' => array( 00367 'ID' => 34, 00368 'NAME' => 'Heineken', 00369 ) 00370 ), 00371 ) 00372 ); 00373 00374 // declare a non-grouped path 00375 $path = new convert_path('beer_style', '/ROOT/BEER_STYLES/BEER_STYLE'); 00376 00377 // an attempt to apply recipes throws exception because we do not expect grouped data 00378 $this->expectException('convert_path_exception'); 00379 $data = $path->apply_recipes($data); 00380 } 00381 00382 public function test_grouped_convert_path_with_recipes() { 00383 // prepare some grouped data 00384 $data = array( 00385 'ID' => 77, 00386 'NAME' => 'Pale lagers', 00387 'BEERS' => array( 00388 array( 00389 'BEER' => array( 00390 'ID' => 67, 00391 'NAME' => 'Pilsner Urquell', 00392 ) 00393 ), 00394 array( 00395 'BEER' => array( 00396 'ID' => 34, 00397 'NAME' => 'Heineken', 00398 ) 00399 ), 00400 ) 00401 ); 00402 00403 // implict recipes work for grouped data if the path is declared as grouped 00404 $path = new convert_path('beer_style', '/ROOT/BEER_STYLES/BEER_STYLE', array(), true); 00405 $data = $path->apply_recipes($data); 00406 $this->assertEqual('Heineken', $data['beers'][1]['beer']['name']); 00407 00408 // an attempt to provide explicit recipes on grouped elements throws exception 00409 $this->expectException('convert_path_exception'); 00410 $path = new convert_path( 00411 'beer_style', '/ROOT/BEER_STYLES/BEER_STYLE', 00412 array( 00413 'renamefields' => array( 00414 'name' => 'beername', // note this is confusing recipe because the 'name' is used for both 00415 // beer-style name ('Pale lagers') and beer name ('Pilsner Urquell') 00416 ) 00417 ), true); 00418 } 00419 00420 public function test_referenced_course_files() { 00421 00422 $text = 'This is a text containing links to file.php 00423 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 /> 00424 <br /><a href=\'$@FILEPHP@$$@SLASH@$MANUAL.DOC$@FORCEDOWNLOAD@$\'>download manual</a><br />'; 00425 00426 $files = moodle1_converter::find_referenced_files($text); 00427 $this->assertIsA($files, 'array'); 00428 $this->assertEqual(2, count($files)); 00429 $this->assertTrue(in_array('/pics/news.gif', $files)); 00430 $this->assertTrue(in_array('/MANUAL.DOC', $files)); 00431 00432 $text = moodle1_converter::rewrite_filephp_usage($text, array('/pics/news.gif', '/another/file/notused.txt'), $files); 00433 $this->assertEqual($text, 'This is a text containing links to file.php 00434 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 /> 00435 <br /><a href=\'$@FILEPHP@$$@SLASH@$MANUAL.DOC$@FORCEDOWNLOAD@$\'>download manual</a><br />'); 00436 } 00437 00438 public function test_question_bank_conversion() { 00439 global $CFG; 00440 00441 copy( 00442 "$CFG->dirroot/backup/converter/moodle1/simpletest/files/questions.xml", 00443 "$CFG->tempdir/backup/$this->tempdir/moodle.xml" 00444 ); 00445 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 00446 $converter->convert(); 00447 } 00448 00449 public function test_convert_run_convert() { 00450 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 00451 $converter->convert(); 00452 } 00453 00454 public function test_inforef_manager() { 00455 $converter = convert_factory::get_converter('moodle1', $this->tempdir); 00456 $inforef = $converter->get_inforef_manager('unittest'); 00457 $inforef->add_ref('file', 45); 00458 $inforef->add_refs('file', array(46, 47)); 00459 // todo test the write_refs() via some dummy xml_writer 00460 $this->expectException('coding_exception'); 00461 $inforef->add_ref('unknown_referenced_item_name', 76); 00462 } 00463 }