Skip to content

Commit

Permalink
MDL-37774 Add unit test to illustrate the problem with the trailing s…
Browse files Browse the repository at this point in the history
…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
mudrd8mz committed Feb 8, 2013
1 parent 0e6d8bc commit 8616f93
Showing 1 changed file with 66 additions and 7 deletions.
73 changes: 66 additions & 7 deletions backup/converter/moodle1/tests/lib_test.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 8616f93

Please sign in to comment.