]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add `getNonExistingName()` to the node api
authorRobin Appelman <icewind@owncloud.com>
Tue, 24 Mar 2015 14:00:36 +0000 (15:00 +0100)
committerRobin Appelman <icewind@owncloud.com>
Tue, 24 Mar 2015 14:00:36 +0000 (15:00 +0100)
lib/private/files/node/folder.php
lib/public/files/folder.php
tests/lib/files/node/folder.php

index 5fd73cc5d3614a9623ddab6d522fbdd0991887b2..18a93913f0648b2e188a799bcb87d21ba44d2dc0 100644 (file)
@@ -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), '/');
+       }
 }
index 9797fbc46ed05feba511e45cf3d956c133e7c31a..916f190ac348108a75bc7509d8fed154cc82db77 100644 (file)
@@ -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);
 }
index 54b26ebdfe181755751113f621e3670589cb4e6d..4a88e2acd36ce6124f7a0d01bccfcde02e8843ac 100644 (file)
@@ -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));
+       }
 }