[Pkg-owncloud-commits] [owncloud] 84/118: Add `getNonExistingName()` to the node api

David Prévot taffit at moszumanska.debian.org
Fri Mar 27 22:13:16 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch stable8
in repository owncloud.

commit 4c92aafc19944f613cb0a1f2aa1e3bd2020ad6cd
Author: Robin Appelman <icewind at owncloud.com>
Date:   Tue Mar 24 15:00:36 2015 +0100

    Add `getNonExistingName()` to the node api
---
 lib/private/files/node/folder.php | 12 ++++++++++++
 lib/public/files/folder.php       |  9 +++++++++
 tests/lib/files/node/folder.php   | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+)

diff --git a/lib/private/files/node/folder.php b/lib/private/files/node/folder.php
index 5fd73cc..18a9391 100644
--- a/lib/private/files/node/folder.php
+++ b/lib/private/files/node/folder.php
@@ -389,4 +389,16 @@ class Folder extends Node implements \OCP\Files\Folder {
 			throw new NotPermittedException();
 		}
 	}
+
+	/**
+	 * Add a suffix to the name in case the file exists
+	 *
+	 * @param string $name
+	 * @return string
+	 * @throws NotPermittedException
+	 */
+	public function getNonExistingName($name) {
+		$uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view);
+		return trim($this->getRelativePath($uniqueName), '/');
+	}
 }
diff --git a/lib/public/files/folder.php b/lib/public/files/folder.php
index 9797fbc..916f190 100644
--- a/lib/public/files/folder.php
+++ b/lib/public/files/folder.php
@@ -146,4 +146,13 @@ interface Folder extends Node {
 	 * @return bool
 	 */
 	public function isCreatable();
+
+	/**
+	 * Add a suffix to the name in case the file exists
+	 *
+	 * @param string $name
+	 * @return string
+	 * @throws NotPermittedException
+	 */
+	public function getNonExistingName($name);
 }
diff --git a/tests/lib/files/node/folder.php b/tests/lib/files/node/folder.php
index 54b26eb..4a88e2a 100644
--- a/tests/lib/files/node/folder.php
+++ b/tests/lib/files/node/folder.php
@@ -679,4 +679,40 @@ class Folder extends \Test\TestCase {
 		$this->assertEquals('/bar/foo/qwerty', $result[0]->getPath());
 		$this->assertEquals('/bar/foo/asd/foo/qwerty', $result[1]->getPath());
 	}
+
+	public function uniqueNameProvider() {
+		return [
+			// input, existing, expected
+			['foo', []					, 'foo'],
+			['foo', ['foo']				, 'foo (2)'],
+			['foo', ['foo', 'foo (2)']	, 'foo (3)']
+		];
+	}
+
+	/**
+	 * @dataProvider uniqueNameProvider
+	 */
+	public function testGetUniqueName($name, $existingFiles, $expected) {
+		$manager = $this->getMock('\OC\Files\Mount\Manager');
+		$folderPath = '/bar/foo';
+		/**
+		 * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
+		 */
+		$view = $this->getMock('\OC\Files\View');
+		$root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, $view, $this->user));
+
+		$view->expects($this->any())
+			->method('file_exists')
+			->will($this->returnCallback(function ($path) use ($existingFiles, $folderPath) {
+				foreach ($existingFiles as $existing) {
+					if ($folderPath . '/' . $existing === $path){
+						return true;
+					}
+				}
+				return false;
+			}));
+
+		$node = new \OC\Files\Node\Folder($root, $view, $folderPath);
+		$this->assertEquals($expected, $node->getNonExistingName($name));
+	}
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git



More information about the Pkg-owncloud-commits mailing list