Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

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