Permalink
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 c9d6862 commit bf465486700c9f5542d5172e1c414a5156b5564f @mudrd8mz mudrd8mz committed Feb 8, 2013
Showing with 57 additions and 7 deletions.
  1. +57 −7 backup/converter/moodle1/simpletest/testlib.php
@@ -253,13 +253,6 @@ public function test_migrate_file() {
$fileids = $fileman->get_fileids();
$this->assertIsA($fileids, 'array');
$this->assertEqual(0, count($fileids));
- // try to migrate a non-existing directory
- $returned = $fileman->migrate_directory('not/existing/directory');
- $this->assertIsA($returned, 'array');
- $this->assertEqual(0, count($returned));
- $fileids = $fileman->get_fileids();
- $this->assertIsA($fileids, 'array');
- $this->assertEqual(0, count($fileids));
// try to migrate an invalid file
$fileman->itemid = 1;
$thrown = false;
@@ -300,6 +293,63 @@ public function test_migrate_file() {
$converter->drop_stash_storage();
}
+ public function test_migrate_directory() {
+ // 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->assertIsA($fileids, 'array');
+ $this->assertEqual(0, count($fileids));
+ // Try to migrate a non-existing directory.
+ $returned = $fileman->migrate_directory('not/existing/directory');
+ $this->assertIsA($returned, 'array');
+ $this->assertEqual(0, count($returned));
+ $fileids = $fileman->get_fileids();
+ $this->assertIsA($fileids, 'array');
+ $this->assertEqual(0, count($fileids));
+ // Try to migrate whole course_files.
+ $returned = $fileman->migrate_directory('course_files');
+ $this->assertIsA($returned, 'array');
+ $this->assertEqual(4, count($returned)); // Two files, two directories.
+ $fileids = $fileman->get_fileids();
+ $this->assertIsA($fileids, 'array');
+ $this->assertEqual(4, count($fileids));
+ $this->assertTrue(is_file($converter->get_workdir_path().'/files/4e/4ea114b0558f53e3af8dd9afc0e0810a95c2a724'));
+ // 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->assertIsA($files['/.'], 'array');
+ $this->assertIsA($files['/file1.gif'], 'array');
+ $this->assertIsA($files['/sub1/.'], 'array');
+ $this->assertIsA($files['/sub1/file2.gif'], 'array');
+ $this->assertEqual(sha1(''), $files['/.']['contenthash']);
+ $this->assertEqual(sha1(''), $files['/sub1/.']['contenthash']);
+ $this->assertEqual('4ea114b0558f53e3af8dd9afc0e0810a95c2a724', $files['/file1.gif']['contenthash']);
+ $this->assertEqual('4ea114b0558f53e3af8dd9afc0e0810a95c2a724', $files['/sub1/file2.gif']['contenthash']);
+ $converter->drop_stash_storage();
+ }
+
+ public function test_migrate_directory_with_trailing_slash() {
+ // 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.
+ // This may display a debugging message which is expected.
+ $returned = $fileman->migrate_directory('course_files/sub1/');
+ $this->assertIsA($returned, 'array');
+ $this->assertEqual(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->assertEqual('foo_bar', $path->get_name());

0 comments on commit bf46548

Please sign in to comment.