diff options
author | Bart Visscher <bartv@thisnet.nl> | 2013-03-08 14:22:04 +0100 |
---|---|---|
committer | Bart Visscher <bartv@thisnet.nl> | 2013-07-05 13:03:01 +0200 |
commit | 424ec94680e69a3082a3d3f7c1ceefd7eeae15d2 (patch) | |
tree | 829468c3a7e567f44c2550c252ed52f47618e6fd | |
parent | 9c855f7b3d97b312095ba53a08fe45b71ca06c98 (diff) | |
download | nextcloud-server-424ec94680e69a3082a3d3f7c1ceefd7eeae15d2.tar.gz nextcloud-server-424ec94680e69a3082a3d3f7c1ceefd7eeae15d2.zip |
Make buildNotExistingFileName testable and write unittests
-rw-r--r-- | lib/helper.php | 14 | ||||
-rw-r--r-- | tests/lib/helper.php | 42 |
2 files changed, 55 insertions, 1 deletions
diff --git a/lib/helper.php b/lib/helper.php index 1860a55fc8f..017221cef77 100644 --- a/lib/helper.php +++ b/lib/helper.php @@ -636,6 +636,18 @@ class OC_Helper { * @return string */ public static function buildNotExistingFileName($path, $filename) { + $view = \OC\Files\Filesystem::getView(); + return self::buildNotExistingFileNameForView($path, $filename, $view); + } + + /** + * Adds a suffix to the name in case the file exists + * + * @param $path + * @param $filename + * @return string + */ + public static function buildNotExistingFileNameForView($path, $filename, \OC\Files\View $view) { if($path==='/') { $path=''; } @@ -649,7 +661,7 @@ class OC_Helper { $newpath = $path . '/' . $filename; $counter = 2; - while (\OC\Files\Filesystem::file_exists($newpath)) { + while ($view->file_exists($newpath)) { $newname = $name . ' (' . $counter . ')' . $ext; $newpath = $path . '/' . $newname; $counter++; diff --git a/tests/lib/helper.php b/tests/lib/helper.php index 6acb0dfaa6b..410117a9e67 100644 --- a/tests/lib/helper.php +++ b/tests/lib/helper.php @@ -146,4 +146,46 @@ class Test_Helper extends PHPUnit_Framework_TestCase { $result = OC_Helper::recursiveArraySearch($haystack, "NotFound"); $this->assertFalse($result); } + + function testBuildNotExistingFileNameForView() { + $viewMock = $this->getMock('\OC\Files\View', array(), array(), '', false); + $this->assertEquals('/filename', OC_Helper::buildNotExistingFileNameForView('/', 'filename', $viewMock)); + $this->assertEquals('dir/filename.ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock)); + + $viewMock->expects($this->at(0)) + ->method('file_exists') + ->will($this->returnValue(true)); + $this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock)); + + $viewMock->expects($this->at(0)) + ->method('file_exists') + ->will($this->returnValue(true)); + $viewMock->expects($this->at(1)) + ->method('file_exists') + ->will($this->returnValue(true)); + $this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock)); + + $viewMock->expects($this->at(0)) + ->method('file_exists') + ->will($this->returnValue(true)); + $this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (1).ext', $viewMock)); + + $viewMock->expects($this->at(0)) + ->method('file_exists') + ->will($this->returnValue(true)); + $viewMock->expects($this->at(1)) + ->method('file_exists') + ->will($this->returnValue(true)); + $this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock)); + + $viewMock->expects($this->at(0)) + ->method('file_exists') + ->will($this->returnValue(true)); + $this->assertEquals('dir/filename(2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1).ext', $viewMock)); + + $viewMock->expects($this->at(0)) + ->method('file_exists') + ->will($this->returnValue(true)); + $this->assertEquals('dir/filename(1) (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock)); + } } |