From 8e13ff2c8621efc2640ff1fcd2c2b58391b09f6c Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 24 May 2016 15:23:50 +0200 Subject: [PATCH] Fix TODO and bring in abstraction (similar to comments) --- apps/dav/lib/RootCollection.php | 3 +-- .../SystemTagsObjectTypeCollection.php | 23 ++++++++----------- .../SystemTagsRelationsCollection.php | 10 ++++---- .../SystemTagsObjectTypeCollectionTest.php | 13 +++++------ 4 files changed, 21 insertions(+), 28 deletions(-) diff --git a/apps/dav/lib/RootCollection.php b/apps/dav/lib/RootCollection.php index fd05ec2626..356aad4e03 100644 --- a/apps/dav/lib/RootCollection.php +++ b/apps/dav/lib/RootCollection.php @@ -71,8 +71,7 @@ class RootCollection extends SimpleCollection { \OC::$server->getSystemTagManager(), \OC::$server->getSystemTagObjectMapper(), \OC::$server->getUserSession(), - \OC::$server->getGroupManager(), - \OC::$server->getRootFolder() + \OC::$server->getGroupManager() ); $commentsCollection = new Comments\RootCollection( \OC::$server->getCommentsManager(), diff --git a/apps/dav/lib/SystemTag/SystemTagsObjectTypeCollection.php b/apps/dav/lib/SystemTag/SystemTagsObjectTypeCollection.php index ae4b9d51a1..ba5b3e1185 100644 --- a/apps/dav/lib/SystemTag/SystemTagsObjectTypeCollection.php +++ b/apps/dav/lib/SystemTag/SystemTagsObjectTypeCollection.php @@ -31,7 +31,6 @@ use OCP\SystemTag\ISystemTagManager; use OCP\SystemTag\ISystemTagObjectMapper; use OCP\IUserSession; use OCP\IGroupManager; -use OCP\Files\IRootFolder; /** * Collection containing object ids by object type @@ -64,9 +63,9 @@ class SystemTagsObjectTypeCollection implements ICollection { private $userSession; /** - * @var IRootFolder + * @var \Closure **/ - protected $fileRoot; + protected $childExistsFunction; /** * Constructor @@ -76,7 +75,7 @@ class SystemTagsObjectTypeCollection implements ICollection { * @param ISystemTagObjectMapper $tagMapper * @param IUserSession $userSession * @param IGroupManager $groupManager - * @param IRootFolder $fileRoot + * @param \Closure $childExistsFunction */ public function __construct( $objectType, @@ -84,14 +83,14 @@ class SystemTagsObjectTypeCollection implements ICollection { ISystemTagObjectMapper $tagMapper, IUserSession $userSession, IGroupManager $groupManager, - IRootFolder $fileRoot + \Closure $childExistsFunction ) { $this->tagManager = $tagManager; $this->tagMapper = $tagMapper; $this->objectType = $objectType; $this->userSession = $userSession; $this->groupManager = $groupManager; - $this->fileRoot = $fileRoot; + $this->childExistsFunction = $childExistsFunction; } /** @@ -133,17 +132,13 @@ class SystemTagsObjectTypeCollection implements ICollection { } /** + * Checks if a child-node with the specified name exists + * * @param string $name + * @return bool */ function childExists($name) { - // TODO: make this more abstract - if ($this->objectType === 'files') { - // make sure the object is reachable for the current user - $userId = $this->userSession->getUser()->getUID(); - $nodes = $this->fileRoot->getUserFolder($userId)->getById(intval($name)); - return !empty($nodes); - } - return true; + return call_user_func($this->childExistsFunction, $name); } function delete() { diff --git a/apps/dav/lib/SystemTag/SystemTagsRelationsCollection.php b/apps/dav/lib/SystemTag/SystemTagsRelationsCollection.php index 19db39d3f3..b09f95ccd6 100644 --- a/apps/dav/lib/SystemTag/SystemTagsRelationsCollection.php +++ b/apps/dav/lib/SystemTag/SystemTagsRelationsCollection.php @@ -29,7 +29,6 @@ use Sabre\DAV\Exception\Forbidden; use Sabre\DAV\SimpleCollection; use OCP\IUserSession; use OCP\IGroupManager; -use OCP\Files\IRootFolder; class SystemTagsRelationsCollection extends SimpleCollection { @@ -40,14 +39,12 @@ class SystemTagsRelationsCollection extends SimpleCollection { * @param ISystemTagObjectMapper $tagMapper * @param IUserSession $userSession * @param IGroupManager $groupManager - * @param IRootFolder $fileRoot */ public function __construct( ISystemTagManager $tagManager, ISystemTagObjectMapper $tagMapper, IUserSession $userSession, - IGroupManager $groupManager, - IRootFolder $fileRoot + IGroupManager $groupManager ) { $children = [ new SystemTagsObjectTypeCollection( @@ -56,7 +53,10 @@ class SystemTagsRelationsCollection extends SimpleCollection { $tagMapper, $userSession, $groupManager, - $fileRoot + function($name) { + $nodes = \OC::$server->getUserFolder()->getById(intval($name)); + return !empty($nodes); + } ), ]; diff --git a/apps/dav/tests/unit/SystemTag/SystemTagsObjectTypeCollectionTest.php b/apps/dav/tests/unit/SystemTag/SystemTagsObjectTypeCollectionTest.php index 628abe6689..5b86480225 100644 --- a/apps/dav/tests/unit/SystemTag/SystemTagsObjectTypeCollectionTest.php +++ b/apps/dav/tests/unit/SystemTag/SystemTagsObjectTypeCollectionTest.php @@ -71,13 +71,12 @@ class SystemTagsObjectTypeCollectionTest extends \Test\TestCase { $this->userFolder = $this->getMockBuilder('\OCP\Files\Folder') ->getMock(); + $userFolder = $this->userFolder; - $fileRoot = $this->getMockBuilder('\OCP\Files\IRootFolder') - ->getMock(); - $fileRoot->expects($this->any()) - ->method('getUserfolder') - ->with('testuser') - ->will($this->returnValue($this->userFolder)); + $closure = function($name) use ($userFolder) { + $nodes = $userFolder->getById(intval($name)); + return !empty($nodes); + }; $this->node = new \OCA\DAV\SystemTag\SystemTagsObjectTypeCollection( 'files', @@ -85,7 +84,7 @@ class SystemTagsObjectTypeCollectionTest extends \Test\TestCase { $this->tagMapper, $userSession, $groupManager, - $fileRoot + $closure ); }