mirror of
https://github.com/nextcloud/server.git
synced 2026-04-21 06:08:46 -04:00
Make buildNotExistingFileName testable and write unittests
This commit is contained in:
parent
9c855f7b3d
commit
424ec94680
2 changed files with 55 additions and 1 deletions
|
|
@ -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++;
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue