Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit 68c1945a8e3676945a0ca3a610c82b4ffbdf01c3 1 parent c192132
David Mudrák authored February 08, 2013
71  backup/converter/moodle1/tests/lib_test.php
@@ -266,13 +266,6 @@ public function test_migrate_file() {
266 266
         $fileids = $fileman->get_fileids();
267 267
         $this->assertEquals(gettype($fileids), 'array');
268 268
         $this->assertEquals(0, count($fileids));
269  
-        // try to migrate a non-existing directory
270  
-        $returned = $fileman->migrate_directory('not/existing/directory');
271  
-        $this->assertEquals(gettype($returned), 'array');
272  
-        $this->assertEquals(0, count($returned));
273  
-        $fileids = $fileman->get_fileids();
274  
-        $this->assertEquals(gettype($fileids), 'array');
275  
-        $this->assertEquals(0, count($fileids));
276 269
         // try to migrate an invalid file
277 270
         $fileman->itemid = 1;
278 271
         $thrown = false;
@@ -314,6 +307,70 @@ public function test_migrate_file() {
314 307
         $converter->drop_stash_storage();
315 308
     }
316 309
 
  310
+    public function test_migrate_directory() {
  311
+        $this->resetAfterTest(true);
  312
+
  313
+        // Set-up the file manager.
  314
+        $converter = convert_factory::get_converter('moodle1', $this->tempdir);
  315
+        $converter->create_stash_storage();
  316
+        $contextid = $converter->get_contextid(CONTEXT_MODULE, 32);
  317
+        $fileman   = $converter->get_file_manager($contextid, 'mod_unittest', 'testarea');
  318
+        // This fileman has not converted anything yet.
  319
+        $fileids = $fileman->get_fileids();
  320
+        $this->assertEquals(gettype($fileids), 'array');
  321
+        $this->assertEquals(0, count($fileids));
  322
+        // Try to migrate a non-existing directory.
  323
+        $returned = $fileman->migrate_directory('not/existing/directory');
  324
+        $this->assertEquals(gettype($returned), 'array');
  325
+        $this->assertEquals(0, count($returned));
  326
+        $fileids = $fileman->get_fileids();
  327
+        $this->assertEquals(gettype($fileids), 'array');
  328
+        $this->assertEquals(0, count($fileids));
  329
+        // Try to migrate whole course_files.
  330
+        $returned = $fileman->migrate_directory('course_files');
  331
+        $this->assertEquals(gettype($returned), 'array');
  332
+        $this->assertEquals(4, count($returned)); // Two files, two directories.
  333
+        $fileids = $fileman->get_fileids();
  334
+        $this->assertEquals(gettype($fileids), 'array');
  335
+        $this->assertEquals(4, count($fileids));
  336
+        $subdir = substr($this->iconhash, 0, 2);
  337
+        $this->assertTrue(is_file($converter->get_workdir_path().'/files/'.$subdir.'/'.$this->iconhash));
  338
+
  339
+        // Check the file records.
  340
+        $files = array();
  341
+        $filerecordids = $converter->get_stash_itemids('files');
  342
+        foreach ($filerecordids as $filerecordid) {
  343
+            $filerecord = $converter->get_stash('files', $filerecordid);
  344
+            $files[$filerecord['filepath'].$filerecord['filename']] = $filerecord;
  345
+        }
  346
+        $this->assertEquals('array', gettype($files['/.']));
  347
+        $this->assertEquals('array', gettype($files['/file1.gif']));
  348
+        $this->assertEquals('array', gettype($files['/sub1/.']));
  349
+        $this->assertEquals('array', gettype($files['/sub1/file2.gif']));
  350
+        $this->assertEquals(sha1(''), $files['/.']['contenthash']);
  351
+        $this->assertEquals(sha1(''), $files['/sub1/.']['contenthash']);
  352
+        $this->assertEquals($this->iconhash, $files['/file1.gif']['contenthash']);
  353
+        $this->assertEquals($this->iconhash, $files['/sub1/file2.gif']['contenthash']);
  354
+
  355
+        $converter->drop_stash_storage();
  356
+    }
  357
+
  358
+    public function test_migrate_directory_with_trailing_slash() {
  359
+        $this->resetAfterTest(true);
  360
+
  361
+        // Set-up the file manager.
  362
+        $converter = convert_factory::get_converter('moodle1', $this->tempdir);
  363
+        $converter->create_stash_storage();
  364
+        $contextid = $converter->get_contextid(CONTEXT_MODULE, 32);
  365
+        $fileman   = $converter->get_file_manager($contextid, 'mod_unittest', 'testarea');
  366
+        // Try to migrate a subdirectory passed with the trailing slash.
  367
+        $returned = $fileman->migrate_directory('course_files/sub1/');
  368
+        $this->assertEquals(gettype($returned), 'array');
  369
+        $this->assertEquals(2, count($returned)); // One file, one directory.
  370
+
  371
+        $converter->drop_stash_storage();
  372
+    }
  373
+
317 374
     public function test_convert_path() {
318 375
         $path = new convert_path('foo_bar', '/ROOT/THINGS/FOO/BAR');
319 376
         $this->assertEquals('foo_bar', $path->get_name());

0 notes on commit 68c1945

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