]> source.dussan.org Git - nextcloud-server.git/commitdiff
Fix tests for ShareHelper
authorJoas Schilling <coding@schilljs.com>
Mon, 10 Apr 2017 14:10:34 +0000 (16:10 +0200)
committerRoeland Jago Douma <roeland@famdouma.nl>
Thu, 13 Apr 2017 10:58:52 +0000 (12:58 +0200)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/private/Share20/ShareHelper.php
tests/lib/Share20/ShareHelperTests.php

index ba3f64dbc1a90907c787c41af45bddd5acc8c3e5..90992005017848426352934d800f559c5739f30b 100644 (file)
@@ -56,6 +56,11 @@ class ShareHelper implements IShareHelper {
                return $result;
        }
 
+       /**
+        * @param Node $node
+        * @param array[] $users
+        * @return array
+        */
        protected function getPathsForUsers(Node $node, array $users) {
                $byId = $results = [];
                foreach ($users as $uid => $info) {
@@ -79,6 +84,7 @@ class ShareHelper implements IShareHelper {
                $item = $node;
                $appendix = '/' . $node->getName();
                while (!empty($byId)) {
+                       /** @var Node $item */
                        $item = $item->getParent();
 
                        if (!empty($byId[$item->getId()])) {
@@ -94,6 +100,11 @@ class ShareHelper implements IShareHelper {
                return $results;
        }
 
+       /**
+        * @param Node $node
+        * @param array[] $remotes
+        * @return array
+        */
        protected function getPathsForRemotes(Node $node, array $remotes) {
                $byId = $results = [];
                foreach ($remotes as $cloudId => $info) {
@@ -121,6 +132,10 @@ class ShareHelper implements IShareHelper {
                return $results;
        }
 
+       /**
+        * @param Node $node
+        * @return string
+        */
        protected function getMountedPath(Node $node) {
                $path = $node->getPath();
                $sections = explode('/', $path, 4);
index b6c737cf444ba7d4aafaf8f562e17134e80a078b..a6ca581dbb6f56cb9e27e2b82de4eca61cd89d63 100644 (file)
 namespace Test\Share20;
 
 use OC\Share20\ShareHelper;
-use OCP\Files\Folder;
-use OCP\Files\IRootFolder;
 use OCP\Files\Node;
-use OCP\Files\NotFoundException;
+use OCP\Share\IManager;
 use Test\TestCase;
 
 class ShareHelperTests extends TestCase {
 
-       /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
-       private $rootFolder;
+       /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
+       private $manager;
 
        /** @var ShareHelper */
        private $helper;
@@ -40,55 +38,83 @@ class ShareHelperTests extends TestCase {
        public function setUp() {
                parent::setUp();
 
-               $this->rootFolder = $this->createMock(IRootFolder::class);
+               $this->manager = $this->createMock(IManager::class);
 
-               $this->helper = new ShareHelper($this->rootFolder);
+               $this->helper = new ShareHelper($this->manager);
        }
 
-       /**
-        * uid1 - Exists with valid node
-        * uid2 - Does not exist
-        * uid3 - Exists but no valid node
-        * uid4 - Exists with valid node
-        */
-       public function testGetPathsForAccessList() {
-               /** @var Folder[]|\PHPUnit_Framework_MockObject_MockObject[] $userFolder */
-               $userFolder = [
-                       'uid1' => $this->createMock(Folder::class),
-                       'uid3' => $this->createMock(Folder::class),
-                       'uid4' => $this->createMock(Folder::class),
+       public function dataGetPathsForAccessList() {
+               return [
+                       [[], [], false, [], [], false, [
+                               'users' => [],
+                               'remotes' => [],
+                       ]],
+                       [['user1', 'user2'], ['user1' => 'foo', 'user2' => 'bar'], true, [], [], false, [
+                               'users' => ['user1' => 'foo', 'user2' => 'bar'],
+                               'remotes' => [],
+                       ]],
+                       [[], [], false, ['remote1', 'remote2'], ['remote1' => 'qwe', 'remote2' => 'rtz'], true, [
+                               'users' => [],
+                               'remotes' => ['remote1' => 'qwe', 'remote2' => 'rtz'],
+                       ]],
+                       [['user1', 'user2'], ['user1' => 'foo', 'user2' => 'bar'], true, ['remote1', 'remote2'], ['remote1' => 'qwe', 'remote2' => 'rtz'], true, [
+                               'users' => ['user1' => 'foo', 'user2' => 'bar'],
+                               'remotes' => ['remote1' => 'qwe', 'remote2' => 'rtz'],
+                       ]],
                ];
+       }
 
-               $this->rootFolder->method('getUserFolder')
-                       ->willReturnCallback(function($uid) use ($userFolder) {
-                               if (isset($userFolder[$uid])) {
-                                       return $userFolder[$uid];
-                               }
-                               throw new NotFoundException();
-                       });
+       /**
+        * @dataProvider dataGetPathsForAccessList
+        */
+       public function testGetPathsForAccessList(array $userList, array $userMap, $resolveUsers, array $remoteList, array $remoteMap, $resolveRemotes, array $expected) {
+               $this->manager->expects($this->once())
+                       ->method('getAccessList')
+                       ->willReturn([
+                               'users' => $userList,
+                               'remote' => $remoteList,
+                       ]);
 
                /** @var Node|\PHPUnit_Framework_MockObject_MockObject $node */
                $node = $this->createMock(Node::class);
-               $node->method('getId')
-                       ->willReturn(42);
-
-               $userFolder['uid1']->method('getById')
-                       ->with(42)
-                       ->willReturn([$node]);
-               $userFolder['uid3']->method('getById')
-                       ->with(42)
-                       ->willReturn([]);
-               $userFolder['uid4']->method('getById')
-                       ->with(42)
-                       ->willReturn([$node]);
-
-               $expects = [
-                       'uid1' => [$node],
-                       'uid4' => [$node],
+               /** @var ShareHelper|\PHPUnit_Framework_MockObject_MockObject $helper */
+               $helper = $this->getMockBuilder(ShareHelper::class)
+                       ->setConstructorArgs([$this->manager])
+                       ->setMethods(['getPathsForUsers', 'getPathsForRemotes'])
+                       ->getMock();
+
+               $helper->expects($resolveUsers ? $this->once() : $this->never())
+                       ->method('getPathsForUsers')
+                       ->with($node, $userList)
+                       ->willReturn($userMap);
+
+               $helper->expects($resolveRemotes ? $this->once() : $this->never())
+                       ->method('getPathsForRemotes')
+                       ->with($node, $remoteList)
+                       ->willReturn($remoteMap);
+
+               $this->assertSame($expected, $helper->getPathsForAccessList($node));
+       }
+
+       public function dataGetMountedPath() {
+               return [
+                       ['/admin/files/foobar', '/foobar'],
+                       ['/admin/files/foo/bar', '/foo/bar'],
                ];
+       }
 
-               $result = $this->helper->getPathsForAccessList($node, ['uid1', 'uid2', 'uid3', 'uid4']);
+       /**
+        * @dataProvider dataGetMountedPath
+        * @param string $path
+        * @param string $expected
+        */
+       public function testGetMountedPath($path, $expected) {
+               /** @var Node|\PHPUnit_Framework_MockObject_MockObject $node */
+               $node = $this->createMock(Node::class);
+               $node->expects($this->once())
+                       ->method('getPath')
+                       ->willReturn($path);
 
-               $this->assertSame($expects, $result);
+               $this->assertSame($expected, self::invokePrivate($this->helper, 'getMountedPath', [$node]));
        }
 }