From 68c1945a8e3676945a0ca3a610c82b4ffbdf01c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mudr=C3=A1k?= Date: Fri, 8 Feb 2013 11:41:38 +0100 Subject: [PATCH] MDL-37774 Add unit test to illustrate the problem with the trailing slash The patch introduces two new tests for moodle1_file_manager::migrate_directory(). The second test with the trailing slash reveals the trouble here. The expected behaviour is that the method would deal with the trailing slash. Debugging message for the developer should be displayed though. --- backup/converter/moodle1/tests/lib_test.php | 71 +++++++++++++++++++-- 1 file changed, 64 insertions(+), 7 deletions(-) diff --git a/backup/converter/moodle1/tests/lib_test.php b/backup/converter/moodle1/tests/lib_test.php index 154b6c88d7c..ede6582be18 100644 --- a/backup/converter/moodle1/tests/lib_test.php +++ b/backup/converter/moodle1/tests/lib_test.php @@ -266,13 +266,6 @@ class moodle1_converter_testcase extends advanced_testcase { $fileids = $fileman->get_fileids(); $this->assertEquals(gettype($fileids), 'array'); $this->assertEquals(0, count($fileids)); - // try to migrate a non-existing directory - $returned = $fileman->migrate_directory('not/existing/directory'); - $this->assertEquals(gettype($returned), 'array'); - $this->assertEquals(0, count($returned)); - $fileids = $fileman->get_fileids(); - $this->assertEquals(gettype($fileids), 'array'); - $this->assertEquals(0, count($fileids)); // try to migrate an invalid file $fileman->itemid = 1; $thrown = false; @@ -314,6 +307,70 @@ class moodle1_converter_testcase extends advanced_testcase { $converter->drop_stash_storage(); } + public function test_migrate_directory() { + $this->resetAfterTest(true); + + // Set-up the file manager. + $converter = convert_factory::get_converter('moodle1', $this->tempdir); + $converter->create_stash_storage(); + $contextid = $converter->get_contextid(CONTEXT_MODULE, 32); + $fileman = $converter->get_file_manager($contextid, 'mod_unittest', 'testarea'); + // This fileman has not converted anything yet. + $fileids = $fileman->get_fileids(); + $this->assertEquals(gettype($fileids), 'array'); + $this->assertEquals(0, count($fileids)); + // Try to migrate a non-existing directory. + $returned = $fileman->migrate_directory('not/existing/directory'); + $this->assertEquals(gettype($returned), 'array'); + $this->assertEquals(0, count($returned)); + $fileids = $fileman->get_fileids(); + $this->assertEquals(gettype($fileids), 'array'); + $this->assertEquals(0, count($fileids)); + // Try to migrate whole course_files. + $returned = $fileman->migrate_directory('course_files'); + $this->assertEquals(gettype($returned), 'array'); + $this->assertEquals(4, count($returned)); // Two files, two directories. + $fileids = $fileman->get_fileids(); + $this->assertEquals(gettype($fileids), 'array'); + $this->assertEquals(4, count($fileids)); + $subdir = substr($this->iconhash, 0, 2); + $this->assertTrue(is_file($converter->get_workdir_path().'/files/'.$subdir.'/'.$this->iconhash)); + + // Check the file records. + $files = array(); + $filerecordids = $converter->get_stash_itemids('files'); + foreach ($filerecordids as $filerecordid) { + $filerecord = $converter->get_stash('files', $filerecordid); + $files[$filerecord['filepath'].$filerecord['filename']] = $filerecord; + } + $this->assertEquals('array', gettype($files['/.'])); + $this->assertEquals('array', gettype($files['/file1.gif'])); + $this->assertEquals('array', gettype($files['/sub1/.'])); + $this->assertEquals('array', gettype($files['/sub1/file2.gif'])); + $this->assertEquals(sha1(''), $files['/.']['contenthash']); + $this->assertEquals(sha1(''), $files['/sub1/.']['contenthash']); + $this->assertEquals($this->iconhash, $files['/file1.gif']['contenthash']); + $this->assertEquals($this->iconhash, $files['/sub1/file2.gif']['contenthash']); + + $converter->drop_stash_storage(); + } + + public function test_migrate_directory_with_trailing_slash() { + $this->resetAfterTest(true); + + // Set-up the file manager. + $converter = convert_factory::get_converter('moodle1', $this->tempdir); + $converter->create_stash_storage(); + $contextid = $converter->get_contextid(CONTEXT_MODULE, 32); + $fileman = $converter->get_file_manager($contextid, 'mod_unittest', 'testarea'); + // Try to migrate a subdirectory passed with the trailing slash. + $returned = $fileman->migrate_directory('course_files/sub1/'); + $this->assertEquals(gettype($returned), 'array'); + $this->assertEquals(2, count($returned)); // One file, one directory. + + $converter->drop_stash_storage(); + } + public function test_convert_path() { $path = new convert_path('foo_bar', '/ROOT/THINGS/FOO/BAR'); $this->assertEquals('foo_bar', $path->get_name());