Skip to content
Browse files

MDL-37774 Add unit test to illustrate the problem with the trailing s…

…lash

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.
  • Loading branch information...
1 parent 0e6d8bc commit 8616f9380c1d850907ac94df615b85ba3571474c @mudrd8mz mudrd8mz committed Feb 8, 2013
Showing with 66 additions and 7 deletions.
  1. +66 −7 backup/converter/moodle1/tests/lib_test.php
View
73 backup/converter/moodle1/tests/lib_test.php
@@ -266,13 +266,6 @@ public function test_migrate_file() {
$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,72 @@ public function test_migrate_file() {
$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/');
+ // Debugging message must be thrown in this case.
+ $this->assertDebuggingCalled(null, DEBUG_DEVELOPER);
+ $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());

0 comments on commit 8616f93

Please sign in to comment.
Something went wrong with that request. Please try again.