Merge branch 'MDL-37039-23' of git://github.com/damyon/moodle into MOODLE_23_STABLE
[moodle.git] / backup / converter / moodle1 / tests / lib_test.php
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/>.
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 <mark@moodlerooms.com>
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
30 require_once($CFG->dirroot . '/backup/converter/moodle1/lib.php');
33 class moodle1_converter_testcase extends advanced_testcase {
35     /** @var string the name of the directory containing the unpacked Moodle 1.9 backup */
36     protected $tempdir;
38     /** @var string saved hash of an icon file used during testing */
39     protected $iconhash;
41     protected function setUp() {
42         global $CFG;
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     }
74     protected function tearDown() {
75         global $CFG;
76         if (empty($CFG->keeptempdirectoriesonbackup)) {
77             fulldelete("$CFG->tempdir/backup/$this->tempdir");
78         }
79     }
81     public function test_detect_format() {
82         $detected = moodle1_converter::detect_format($this->tempdir);
83         $this->assertEquals(backup::FORMAT_MOODLE1, $detected);
84     }
86     public function test_convert_factory() {
87         $converter = convert_factory::get_converter('moodle1', $this->tempdir);
88         $this->assertInstanceOf('moodle1_converter', $converter);
89     }
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     }
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');
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     }
113     public function test_stash_storage() {
114         $this->resetAfterTest(true);
115         $converter = convert_factory::get_converter('moodle1', $this->tempdir);
116         $converter->create_stash_storage();
118         // no implicit stashes
119         $stashes = $converter->get_stash_names();
120         $this->assertEquals(gettype($stashes), 'array');
121         $this->assertTrue(empty($stashes));
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'));
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'));
139         // repeated reading is allowed
140         $this->assertEquals('13', $converter->get_stash('tempinfo1'));
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));
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));
154         // test get_stash_itemids()
155         $ids = $converter->get_stash_itemids('course_fileref');
156         $this->assertEquals(gettype($ids), 'array');
157         $this->assertTrue(empty($ids));
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));
166         $converter->drop_stash_storage();
167     }
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();
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')));
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
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
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
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));
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);
199         $converter->drop_stash_storage();
200     }
202     public function test_get_contextid() {
203         $this->resetAfterTest(true);
205         $converter = convert_factory::get_converter('moodle1', $this->tempdir);
207         // stash storage must be created in advance
208         $converter->create_stash_storage();
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);
215         $this->assertNotEquals($id1, $id2);
216         $this->assertNotEquals($id1, $id3);
217         $this->assertNotEquals($id2, $id3);
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));
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);
230         $this->assertEquals($id1, $id2);
231         $this->assertEquals($id1, $id3);
233         $id1 = $converter->get_contextid(CONTEXT_SYSTEM);
234         $id2 = $converter->get_contextid(CONTEXT_SYSTEM, 11);
235         $id3 = $converter->get_contextid(CONTEXT_SYSTEM, 15);
237         $this->assertEquals($id1, $id2);
238         $this->assertEquals($id1, $id3);
240         $converter->drop_stash_storage();
241     }
243     public function test_get_nextid() {
244         $this->resetAfterTest(true);
246         $converter = convert_factory::get_converter('moodle1', $this->tempdir);
248         $id1 = $converter->get_nextid();
249         $id2 = $converter->get_nextid();
250         $id3 = $converter->get_nextid();
252         $this->assertTrue(0 < $id1);
253         $this->assertTrue($id1 < $id2);
254         $this->assertTrue($id2 < $id3);
255     }
257     public function test_migrate_file() {
258         $this->resetAfterTest(true);
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     }
310     public function test_migrate_directory() {
311         $this->resetAfterTest(true);
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));
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']);
355         $converter->drop_stash_storage();
356     }
358     public function test_migrate_directory_with_trailing_slash() {
359         $this->resetAfterTest(true);
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         $this->assertEquals(gettype($returned), 'array');
369         $this->assertEquals(2, count($returned)); // One file, one directory.
371         $converter->drop_stash_storage();
372     }
374     public function test_convert_path() {
375         $path = new convert_path('foo_bar', '/ROOT/THINGS/FOO/BAR');
376         $this->assertEquals('foo_bar', $path->get_name());
377         $this->assertEquals('/ROOT/THINGS/FOO/BAR', $path->get_path());
378         $this->assertEquals('process_foo_bar', $path->get_processing_method());
379         $this->assertEquals('on_foo_bar_start', $path->get_start_method());
380         $this->assertEquals('on_foo_bar_end', $path->get_end_method());
381     }
383     public function test_convert_path_implicit_recipes() {
384         $path = new convert_path('foo_bar', '/ROOT/THINGS/FOO/BAR');
385         $data = array(
386             'ID' => 76,
387             'ELOY' => 'stronk7',
388             'MARTIN' => 'moodler',
389             'EMPTY' => null,
390         );
391         // apply default recipes (converting keys to lowercase)
392         $data = $path->apply_recipes($data);
393         $this->assertEquals(4, count($data));
394         $this->assertEquals(76, $data['id']);
395         $this->assertEquals('stronk7', $data['eloy']);
396         $this->assertEquals('moodler', $data['martin']);
397         $this->assertSame(null, $data['empty']);
398     }
400     public function test_convert_path_explicit_recipes() {
401         $path = new convert_path(
402             'foo_bar', '/ROOT/THINGS/FOO/BAR',
403             array(
404                 'newfields' => array(
405                     'david' => 'mudrd8mz',
406                     'petr'  => 'skodak',
407                 ),
408                 'renamefields' => array(
409                     'empty' => 'nothing',
410                 ),
411                 'dropfields' => array(
412                     'id'
413                 ),
414             )
415         );
416         $data = array(
417             'ID' => 76,
418             'ELOY' => 'stronk7',
419             'MARTIN' => 'moodler',
420             'EMPTY' => null,
421         );
422         $data = $path->apply_recipes($data);
424         $this->assertEquals(5, count($data));
425         $this->assertFalse(array_key_exists('id', $data));
426         $this->assertEquals('stronk7', $data['eloy']);
427         $this->assertEquals('moodler', $data['martin']);
428         $this->assertEquals('mudrd8mz', $data['david']);
429         $this->assertEquals('skodak', $data['petr']);
430         $this->assertSame(null, $data['nothing']);
431     }
433     public function test_grouped_data_on_nongrouped_convert_path() {
434         // prepare some grouped data
435         $data = array(
436             'ID' => 77,
437             'NAME' => 'Pale lagers',
438             'BEERS' => array(
439                 array(
440                     'BEER' => array(
441                         'ID' => 67,
442                         'NAME' => 'Pilsner Urquell',
443                     )
444                 ),
445                 array(
446                     'BEER' => array(
447                         'ID' => 34,
448                         'NAME' => 'Heineken',
449                     )
450                 ),
451             )
452         );
454         // declare a non-grouped path
455         $path = new convert_path('beer_style', '/ROOT/BEER_STYLES/BEER_STYLE');
457         // an attempt to apply recipes throws exception because we do not expect grouped data
458         $this->setExpectedException('convert_path_exception');
459         $data = $path->apply_recipes($data);
460     }
462     public function test_grouped_convert_path_with_recipes() {
463         // prepare some grouped data
464         $data = array(
465             'ID' => 77,
466             'NAME' => 'Pale lagers',
467             'BEERS' => array(
468                 array(
469                     'BEER' => array(
470                         'ID' => 67,
471                         'NAME' => 'Pilsner Urquell',
472                     )
473                 ),
474                 array(
475                     'BEER' => array(
476                         'ID' => 34,
477                         'NAME' => 'Heineken',
478                     )
479                 ),
480             )
481         );
483         // implict recipes work for grouped data if the path is declared as grouped
484         $path = new convert_path('beer_style', '/ROOT/BEER_STYLES/BEER_STYLE', array(), true);
485         $data = $path->apply_recipes($data);
486         $this->assertEquals('Heineken', $data['beers'][1]['beer']['name']);
488         // an attempt to provide explicit recipes on grouped elements throws exception
489         $this->setExpectedException('convert_path_exception');
490         $path = new convert_path(
491             'beer_style', '/ROOT/BEER_STYLES/BEER_STYLE',
492             array(
493                 'renamefields' => array(
494                     'name' => 'beername',   // note this is confusing recipe because the 'name' is used for both
495                     // beer-style name ('Pale lagers') and beer name ('Pilsner Urquell')
496                 )
497             ), true);
498     }
500     public function test_referenced_course_files() {
502         $text = 'This is a text containing links to file.php
503 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 />
504     <div><a href=\'$@FILEPHP@$/../../../../../../../../../../../../../../../etc/passwd\'>download passwords</a></div>
505     <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>
506     <br /><a href=\'$@FILEPHP@$$@SLASH@$MANUAL.DOC$@FORCEDOWNLOAD@$\'>download manual</a><br />';
508         $files = moodle1_converter::find_referenced_files($text);
509         $this->assertEquals(gettype($files), 'array');
510         $this->assertEquals(2, count($files));
511         $this->assertTrue(in_array('/pics/news.gif', $files));
512         $this->assertTrue(in_array('/MANUAL.DOC', $files));
514         $text = moodle1_converter::rewrite_filephp_usage($text, array('/pics/news.gif', '/another/file/notused.txt'));
515         $this->assertEquals($text, 'This is a text containing links to file.php
516 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 />
517     <div><a href=\'$@FILEPHP@$/../../../../../../../../../../../../../../../etc/passwd\'>download passwords</a></div>
518     <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>
519     <br /><a href=\'$@FILEPHP@$$@SLASH@$MANUAL.DOC$@FORCEDOWNLOAD@$\'>download manual</a><br />');
520     }
522     public function test_referenced_files_urlencoded() {
524         $text = 'This is a text containing links to file.php
525 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 />
526     <br /><a href=\'$@FILEPHP@$$@SLASH@$pics$@SLASH@$news%20with%20spaces.gif$@FORCEDOWNLOAD@$\'>with urlencoded spaces</a><br />
527 <a href="$@FILEPHP@$$@SLASH@$illegal%20pics%2Bmovies$@SLASH@$romeo%2Bjuliet.avi">Download the full AVI for free! (space and plus encoded)</a>
528 <a href="$@FILEPHP@$$@SLASH@$illegal pics+movies$@SLASH@$romeo+juliet.avi">Download the full AVI for free! (none encoded)</a>
529 <a href="$@FILEPHP@$$@SLASH@$illegal%20pics+movies$@SLASH@$romeo+juliet.avi">Download the full AVI for free! (only space encoded)</a>
530 <a href="$@FILEPHP@$$@SLASH@$illegal pics%2Bmovies$@SLASH@$romeo%2Bjuliet.avi">Download the full AVI for free! (only plus)</a>';
532         $files = moodle1_converter::find_referenced_files($text);
533         $this->assertEquals(gettype($files), 'array');
534         $this->assertEquals(3, count($files));
535         $this->assertTrue(in_array('/pics/news.gif', $files));
536         $this->assertTrue(in_array('/pics/news with spaces.gif', $files));
537         $this->assertTrue(in_array('/illegal pics+movies/romeo+juliet.avi', $files));
539         $text = moodle1_converter::rewrite_filephp_usage($text, $files);
540         $this->assertEquals('This is a text containing links to file.php
541 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 />
542     <br /><a href=\'@@PLUGINFILE@@/pics/news%20with%20spaces.gif?forcedownload=1\'>with urlencoded spaces</a><br />
543 <a href="@@PLUGINFILE@@/illegal%20pics%2Bmovies/romeo%2Bjuliet.avi">Download the full AVI for free! (space and plus encoded)</a>
544 <a href="@@PLUGINFILE@@/illegal%20pics%2Bmovies/romeo%2Bjuliet.avi">Download the full AVI for free! (none encoded)</a>
545 <a href="$@FILEPHP@$$@SLASH@$illegal%20pics+movies$@SLASH@$romeo+juliet.avi">Download the full AVI for free! (only space encoded)</a>
546 <a href="$@FILEPHP@$$@SLASH@$illegal pics%2Bmovies$@SLASH@$romeo%2Bjuliet.avi">Download the full AVI for free! (only plus)</a>', $text);
547     }
549     public function test_question_bank_conversion() {
550         global $CFG;
552         $this->resetAfterTest(true);
554         copy(
555             "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/questions.xml",
556             "$CFG->tempdir/backup/$this->tempdir/moodle.xml"
557         );
558         $converter = convert_factory::get_converter('moodle1', $this->tempdir);
559         $converter->convert();
560     }
562     public function test_convert_run_convert() {
563         $this->resetAfterTest(true);
564         $converter = convert_factory::get_converter('moodle1', $this->tempdir);
565         $converter->convert();
566     }
568     public function test_inforef_manager() {
569         $converter = convert_factory::get_converter('moodle1', $this->tempdir);
570         $inforef = $converter->get_inforef_manager('unittest');
571         $inforef->add_ref('file', 45);
572         $inforef->add_refs('file', array(46, 47));
573         // todo test the write_refs() via some dummy xml_writer
574         $this->setExpectedException('coding_exception');
575         $inforef->add_ref('unknown_referenced_item_name', 76);
576     }