MDL-37774 Add unit test to illustrate the problem with the trailing slash
authorDavid Mudrák <david@moodle.com>
Fri, 8 Feb 2013 10:41:38 +0000 (11:41 +0100)
committerDavid Mudrák <david@moodle.com>
Sun, 10 Feb 2013 00:54:41 +0000 (01:54 +0100)
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

index 154b6c8..ede6582 100644 (file)
@@ -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());