]> source.dussan.org Git - nextcloud-server.git/commitdiff
Unit test for system tags plugin
authorVincent Petry <pvince81@owncloud.com>
Thu, 3 Dec 2015 10:26:16 +0000 (11:26 +0100)
committerVincent Petry <pvince81@owncloud.com>
Thu, 3 Dec 2015 14:23:21 +0000 (15:23 +0100)
apps/dav/lib/systemtag/systemtagmappingnode.php
apps/dav/lib/systemtag/systemtagnode.php
apps/dav/lib/systemtag/systemtagplugin.php
apps/dav/lib/systemtag/systemtagsobjectmappingcollection.php
apps/dav/tests/unit/systemtag/systemtagmappingnode.php [new file with mode: 0644]
apps/dav/tests/unit/systemtag/systemtagnode.php [new file with mode: 0644]
apps/dav/tests/unit/systemtag/systemtagplugin.php [new file with mode: 0644]
apps/dav/tests/unit/systemtag/systemtagsbyidcollection.php [new file with mode: 0644]
apps/dav/tests/unit/systemtag/systemtagsobjectmappingcollection.php [new file with mode: 0644]
apps/dav/tests/unit/systemtag/systemtagsobjecttypecollection.php [new file with mode: 0644]

index 03088d734d3225f50b1be95f4369e1cace4b9d74..cbf8542a4fd3c1ab18df5e40529641eea78ca804 100644 (file)
@@ -70,6 +70,24 @@ class SystemTagMappingNode extends SystemTagNode {
                parent::__construct($tag, $tagManager);
        }
 
+       /**
+        * Returns the object id of the relationship
+        *
+        * @return string object id
+        */
+       public function getObjectId() {
+               return $this->objectId;
+       }
+
+       /**
+        * Returns the object type of the relationship
+        *
+        * @return string object type
+        */
+       public function getObjectType() {
+               return $this->objectType;
+       }
+
        /**
         * Delete tag to object association
         */
index b09a77379307d2aa3fffc3e2d4da7276384f24f8..f7228108b3d3e4f0a0b9acbdc5349b741251ed6f 100644 (file)
@@ -21,7 +21,6 @@
 
 namespace OCA\DAV\SystemTag;
 
-use OCP\SystemTag\TagAlreadyExistsException;
 use Sabre\DAV\Exception\NotFound;
 use Sabre\DAV\Exception\MethodNotAllowed;
 use Sabre\DAV\Exception\Conflict;
@@ -29,6 +28,7 @@ use Sabre\DAV\Exception\Conflict;
 use OCP\SystemTag\ISystemTag;
 use OCP\SystemTag\ISystemTagManager;
 use OCP\SystemTag\TagNotFoundException;
+use OCP\SystemTag\TagAlreadyExistsException;
 
 class SystemTagNode implements \Sabre\DAV\INode {
 
@@ -76,23 +76,31 @@ class SystemTagNode implements \Sabre\DAV\INode {
         *
         * @param string $name The new name
         *
-        * @throws \Sabre\DAV\Exception\MethodNotAllowed
+        * @throws MethodNotAllowed not allowed to rename node
         */
        public function setName($name) {
                throw new MethodNotAllowed();
        }
 
        /**
+        * Update tag
+        *
         * @param string $name new tag name
         * @param bool $userVisible user visible
         * @param bool $userAssignable user assignable
-        * @throws Conflict
+        * @throws NotFound whenever the given tag id does not exist
+        * @throws Conflict whenever a tag already exists with the given attributes
         */
        public function update($name, $userVisible, $userAssignable) {
                try {
-                       $this->tagManager->updateTag($name, $userVisible, $userAssignable);
+                       $this->tagManager->updateTag($this->tag->getId(), $name, $userVisible, $userAssignable);
+               } catch (TagNotFoundException $e) {
+                       throw new NotFound('Tag with id ' . $this->tag->getId() . ' does not exist');
                } catch (TagAlreadyExistsException $e) {
-                       throw new Conflict('Tag with the properties "' . $name . '", ' . $userVisible, ', ' . $userAssignable . ' already exists');
+                       throw new Conflict(
+                               'Tag with the properties "' . $name . '", ' .
+                               $userVisible . ', ' . $userAssignable . ' already exists'
+                       );
                }
        }
 
index d5591c284930736a356903926e68b0bf6507c376..692b7e970169bd9c5c68a368acacdd3eed12871f 100644 (file)
@@ -88,18 +88,18 @@ class SystemTagPlugin extends \Sabre\DAV\ServerPlugin {
        /**
         * We intercept this to handle POST requests on calendars.
         *
-        * @param RequestInterface $request
-        * @param ResponseInterface $response
+        * @param RequestInterface $request request object
+        * @param ResponseInterface $response response object
         * @return null|false
         */
-       function httpPost(RequestInterface $request, ResponseInterface $response) {
+       public function httpPost(RequestInterface $request, ResponseInterface $response) {
                $path = $request->getPath();
 
                // Making sure the node exists
                try {
                        $node = $this->server->tree->getNodeForPath($path);
                } catch (NotFound $e) {
-                       return;
+                       return null;
                }
 
                if ($node instanceof SystemTagsByIdCollection || $node instanceof SystemTagsObjectMappingCollection) {
@@ -159,7 +159,7 @@ class SystemTagPlugin extends \Sabre\DAV\ServerPlugin {
                }
 
                if (isset($data['userAssignable'])) {
-                       $userVisible = (bool)$data['userAssignable'];
+                       $userAssignable = (bool)$data['userAssignable'];
                }
                try {
                        return $this->tagManager->createTag($tagName, $userVisible, $userAssignable);
index 6ed959fd886fb642a344bf15950caef57dadb5be..e81994e0bd63b83487f1781530a2c07e2a16c2dc 100644 (file)
@@ -24,6 +24,7 @@ namespace OCA\DAV\SystemTag;
 use Sabre\DAV\Exception\Forbidden;
 use Sabre\DAV\Exception\NotFound;
 use Sabre\DAV\Exception\BadRequest;
+use Sabre\DAV\Exception\PreconditionFailed;
 use Sabre\DAV\ICollection;
 
 use OCP\SystemTag\ISystemTagManager;
@@ -75,7 +76,7 @@ class SystemTagsObjectMappingCollection implements ICollection {
                try {
                        $this->tagMapper->assignTags($this->objectId, $this->objectType, $tagId);
                } catch (TagNotFoundException $e) {
-                       throw new Forbidden('Tag with id ' . $tagId . ' does not exist, cannot assign');
+                       throw new PreconditionFailed('Tag with id ' . $tagId . ' does not exist, cannot assign');
                }
        }
 
@@ -103,9 +104,9 @@ class SystemTagsObjectMappingCollection implements ICollection {
                        return [];
                }
                $tags = $this->tagManager->getTagsById($tagIds);
-               return array_map(function($tag) {
+               return array_values(array_map(function($tag) {
                        return $this->makeNode($tag);
-               }, $tags);
+               }, $tags));
        }
 
        function childExists($tagId) {
@@ -114,7 +115,7 @@ class SystemTagsObjectMappingCollection implements ICollection {
                } catch (\InvalidArgumentException $e) {
                        throw new BadRequest('Invalid tag id', 0, $e);
                } catch (TagNotFoundException $e) {
-                       throw new NotFound('Tag with id ' . $tagId . ' not found', 0, $e);
+                       return false;
                }
        }
 
diff --git a/apps/dav/tests/unit/systemtag/systemtagmappingnode.php b/apps/dav/tests/unit/systemtag/systemtagmappingnode.php
new file mode 100644 (file)
index 0000000..849f7c2
--- /dev/null
@@ -0,0 +1,84 @@
+<?php
+/**
+ * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OCA\DAV\Tests\Unit\SystemTag;
+
+use Sabre\DAV\Exception\NotFound;
+use Sabre\DAV\Exception\MethodNotAllowed;
+use Sabre\DAV\Exception\Conflict;
+
+use OC\SystemTag\SystemTag;
+use OCP\SystemTag\TagNotFoundException;
+use OCP\SystemTag\TagAlreadyExistsException;
+
+class SystemTagMappingNode extends SystemTagNode {
+
+       /**
+        * @var \OCA\DAV\SystemTag\SystemTagMappingNode
+        */
+       private $node;
+
+       /**
+        * @var \OCP\SystemTag\ISystemTagManager
+        */
+       private $tagManager;
+
+       /**
+        * @var \OCP\SystemTag\ISystemTagObjectMapper
+        */
+       private $tagMapper;
+
+       /**
+        * @var \OCP\SystemTag\ISystemTag
+        */
+       private $tag;
+
+       protected function setUp() {
+               parent::setUp();
+
+               $this->tag = new SystemTag(1, 'Test', true, false);
+               $this->tagManager = $this->getMock('\OCP\SystemTag\ISystemTagManager');
+               $this->tagMapper = $this->getMock('\OCP\SystemTag\ISystemTagObjectMapper');
+
+               $this->node = new \OCA\DAV\SystemTag\SystemTagMappingNode(
+                       $this->tag,
+                       123,
+                       'files',
+                       $this->tagManager,
+                       $this->tagMapper
+               );
+       }
+
+       public function testGetters() {
+               parent::testGetters();
+               $this->assertEquals(123, $this->node->getObjectId());
+               $this->assertEquals('files', $this->node->getObjectType());
+       }
+
+       public function testDeleteTag() {
+               $this->tagManager->expects($this->never())
+                       ->method('deleteTags');
+               $this->tagMapper->expects($this->once())
+                       ->method('unassignTags')
+                       ->with(123, 'files', 1);
+
+               $this->node->delete();
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\NotFound
+        */
+       public function testDeleteTagNotFound() {
+               $this->tagMapper->expects($this->once())
+                       ->method('unassignTags')
+                       ->with(123, 'files', 1)
+                       ->will($this->throwException(new TagNotFoundException()));
+
+               $this->node->delete();
+       }
+}
diff --git a/apps/dav/tests/unit/systemtag/systemtagnode.php b/apps/dav/tests/unit/systemtag/systemtagnode.php
new file mode 100644 (file)
index 0000000..a43dda3
--- /dev/null
@@ -0,0 +1,103 @@
+<?php
+/**
+ * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OCA\DAV\Tests\Unit\SystemTag;
+
+use Sabre\DAV\Exception\NotFound;
+use Sabre\DAV\Exception\MethodNotAllowed;
+use Sabre\DAV\Exception\Conflict;
+
+use OC\SystemTag\SystemTag;
+use OCP\SystemTag\TagNotFoundException;
+use OCP\SystemTag\TagAlreadyExistsException;
+
+class SystemTagNode extends \Test\TestCase {
+
+       /**
+        * @var \OCA\DAV\SystemTag\SystemTagNode
+        */
+       private $node;
+
+       /**
+        * @var \OCP\SystemTag\ISystemTagManager
+        */
+       private $tagManager;
+
+       /**
+        * @var \OCP\SystemTag\ISystemTag
+        */
+       private $tag;
+
+       protected function setUp() {
+               parent::setUp();
+
+               $this->tag = new SystemTag(1, 'Test', true, false);
+               $this->tagManager = $this->getMock('\OCP\SystemTag\ISystemTagManager');
+
+               $this->node = new \OCA\DAV\SystemTag\SystemTagNode($this->tag, $this->tagManager);
+       }
+
+       public function testGetters() {
+               $this->assertEquals('1', $this->node->getName());
+               $this->assertEquals($this->tag, $this->node->getSystemTag());
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\MethodNotAllowed
+        */
+       public function testSetName() {
+               $this->node->setName('2');
+       }
+
+       public function testUpdateTag() {
+               $this->tagManager->expects($this->once())
+                       ->method('updateTag')
+                       ->with(1, 'Renamed', false, true);
+               $this->node->update('Renamed', false, true);
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\Conflict
+        */
+       public function testUpdateTagAlreadyExists() {
+               $this->tagManager->expects($this->once())
+                       ->method('updateTag')
+                       ->with(1, 'Renamed', false, true)
+                       ->will($this->throwException(new TagAlreadyExistsException()));
+               $this->node->update('Renamed', false, true);
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\NotFound
+        */
+       public function testUpdateTagNotFound() {
+               $this->tagManager->expects($this->once())
+                       ->method('updateTag')
+                       ->with(1, 'Renamed', false, true)
+                       ->will($this->throwException(new TagNotFoundException()));
+               $this->node->update('Renamed', false, true);
+       }
+
+       public function testDeleteTag() {
+               $this->tagManager->expects($this->once())
+                       ->method('deleteTags')
+                       ->with('1');
+               $this->node->delete();
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\NotFound
+        */
+       public function testDeleteTagNotFound() {
+               $this->tagManager->expects($this->once())
+                       ->method('deleteTags')
+                       ->with('1')
+                       ->will($this->throwException(new TagNotFoundException()));
+               $this->node->delete();
+       }
+}
diff --git a/apps/dav/tests/unit/systemtag/systemtagplugin.php b/apps/dav/tests/unit/systemtag/systemtagplugin.php
new file mode 100644 (file)
index 0000000..48c9aa6
--- /dev/null
@@ -0,0 +1,308 @@
+<?php
+/**
+ * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OCA\DAV\Tests\Unit\SystemTag;
+
+use OC\SystemTag\SystemTag;
+use OCP\SystemTag\TagAlreadyExistsException;
+
+class SystemTagPlugin extends \Test\TestCase {
+
+       const ID_PROPERTYNAME = \OCA\DAV\SystemTag\SystemTagPlugin::ID_PROPERTYNAME;
+       const DISPLAYNAME_PROPERTYNAME = \OCA\DAV\SystemTag\SystemTagPlugin::DISPLAYNAME_PROPERTYNAME;
+       const USERVISIBLE_PROPERTYNAME = \OCA\DAV\SystemTag\SystemTagPlugin::USERVISIBLE_PROPERTYNAME;
+       const USERASSIGNABLE_PROPERTYNAME = \OCA\DAV\SystemTag\SystemTagPlugin::USERASSIGNABLE_PROPERTYNAME;
+
+       /**
+        * @var \Sabre\DAV\Server
+        */
+       private $server;
+
+       /**
+        * @var \Sabre\DAV\Tree
+        */
+       private $tree;
+
+       /**
+        * @var \OCP\SystemTag\ISystemTagManager
+        */
+       private $tagManager;
+
+       /**
+        * @var \OCA\DAV\Connector\Sabre\TagsPlugin
+        */
+       private $plugin;
+
+       public function setUp() {
+               parent::setUp();
+               $this->tree = $this->getMockBuilder('\Sabre\DAV\Tree')
+                       ->disableOriginalConstructor()
+                       ->getMock();
+
+               $this->server = new \Sabre\DAV\Server($this->tree);
+
+               $this->tagManager = $this->getMock('\OCP\SystemTag\ISystemTagManager');
+
+               $this->plugin = new \OCA\DAV\SystemTag\SystemTagPlugin($this->tagManager);
+               $this->plugin->initialize($this->server);
+       }
+
+       public function testGetProperties() {
+               $systemTag = new SystemTag(1, 'Test', true, true);
+               $requestedProperties = [
+                       self::ID_PROPERTYNAME,
+                       self::DISPLAYNAME_PROPERTYNAME,
+                       self::USERVISIBLE_PROPERTYNAME,
+                       self::USERASSIGNABLE_PROPERTYNAME
+               ];
+               $expectedProperties = [
+                       200 => [
+                               self::ID_PROPERTYNAME => '1',
+                               self::DISPLAYNAME_PROPERTYNAME => 'Test',
+                               self::USERVISIBLE_PROPERTYNAME => 1,
+                               self::USERASSIGNABLE_PROPERTYNAME => 1,
+                       ]
+               ];
+
+               $node = $this->getMockBuilder('\OCA\DAV\SystemTag\SystemTagNode')
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               $node->expects($this->any())
+                       ->method('getSystemTag')
+                       ->will($this->returnValue($systemTag));
+
+               $this->tree->expects($this->any())
+                       ->method('getNodeForPath')
+                       ->with('/systemtag/1')
+                       ->will($this->returnValue($node));
+
+               $propFind = new \Sabre\DAV\PropFind(
+                       '/systemtag/1',
+                       $requestedProperties,
+                       0
+               );
+
+               $this->plugin->handleGetProperties(
+                       $propFind,
+                       $node
+               );
+
+               $result = $propFind->getResultForMultiStatus();
+
+               $this->assertEmpty($result[404]);
+               unset($result[404]);
+               $this->assertEquals($expectedProperties, $result);
+       }
+
+       public function testUpdateProperties() {
+               $systemTag = new SystemTag(1, 'Test', true, false);
+               $node = $this->getMockBuilder('\OCA\DAV\SystemTag\SystemTagNode')
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               $node->expects($this->any())
+                       ->method('getSystemTag')
+                       ->will($this->returnValue($systemTag));
+
+               $this->tree->expects($this->any())
+                       ->method('getNodeForPath')
+                       ->with('/systemtag/1')
+                       ->will($this->returnValue($node));
+
+               $node->expects($this->once())
+                       ->method('update')
+                       ->with('Test changed', false, true);
+
+               // properties to set
+               $propPatch = new \Sabre\DAV\PropPatch(array(
+                       self::DISPLAYNAME_PROPERTYNAME => 'Test changed',
+                       self::USERVISIBLE_PROPERTYNAME => 0,
+                       self::USERASSIGNABLE_PROPERTYNAME => 1,
+               ));
+
+               $this->plugin->handleUpdateProperties(
+                       '/systemtag/1',
+                       $propPatch
+               );
+
+               $propPatch->commit();
+
+               // all requested properties removed, as they were processed already
+               $this->assertEmpty($propPatch->getRemainingMutations());
+
+               $result = $propPatch->getResult();
+               $this->assertEquals(200, $result[self::DISPLAYNAME_PROPERTYNAME]);
+               $this->assertEquals(200, $result[self::USERASSIGNABLE_PROPERTYNAME]);
+               $this->assertEquals(200, $result[self::USERVISIBLE_PROPERTYNAME]);
+       }
+
+       public function testCreateTagInByIdCollection() {
+               $systemTag = new SystemTag(1, 'Test', true, false);
+
+               $requestData = json_encode([
+                       'name' => 'Test',
+                       'userVisible' => true,
+                       'userAssignable' => false,
+               ]);
+
+               $node = $this->getMockBuilder('\OCA\DAV\SystemTag\SystemTagsByIdCollection')
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               $this->tagManager->expects($this->once())
+                       ->method('createTag')
+                       ->with('Test', true, false)
+                       ->will($this->returnValue($systemTag));
+
+               $this->tree->expects($this->any())
+                       ->method('getNodeForPath')
+                       ->with('/systemtags')
+                       ->will($this->returnValue($node));
+
+               $request = $this->getMockBuilder('Sabre\HTTP\RequestInterface')
+                               ->disableOriginalConstructor()
+                               ->getMock();
+               $response = $this->getMockBuilder('Sabre\HTTP\ResponseInterface')
+                               ->disableOriginalConstructor()
+                               ->getMock();
+
+               $request->expects($this->once())
+                       ->method('getPath')
+                       ->will($this->returnValue('/systemtags'));
+
+               $request->expects($this->once())
+                       ->method('getBodyAsString')
+                       ->will($this->returnValue($requestData));
+
+               $request->expects($this->once())
+                       ->method('getHeader')
+                       ->with('Content-Type')
+                       ->will($this->returnValue('application/json')); 
+
+               $request->expects($this->once())
+                       ->method('getUrl')
+                       ->will($this->returnValue('http://example.com/dav/systemtags'));
+
+               $response->expects($this->once())
+                       ->method('setHeader')
+                       ->with('Location', 'http://example.com/dav/systemtags/1');
+
+               $this->plugin->httpPost($request, $response);
+       }
+
+       public function nodeClassProvider() {
+               return [
+                       ['\OCA\DAV\SystemTag\SystemTagsByIdCollection'],
+                       ['\OCA\DAV\SystemTag\SystemTagsObjectMappingCollection'],
+               ];
+       }
+
+       public function testCreateTagInMappingCollection() {
+               $systemTag = new SystemTag(1, 'Test', true, false);
+
+               $requestData = json_encode([
+                       'name' => 'Test',
+                       'userVisible' => true,
+                       'userAssignable' => false,
+               ]);
+
+               $node = $this->getMockBuilder('\OCA\DAV\SystemTag\SystemTagsObjectMappingCollection')
+                       ->disableOriginalConstructor()
+                       ->getMock();
+
+               $this->tagManager->expects($this->once())
+                       ->method('createTag')
+                       ->with('Test', true, false)
+                       ->will($this->returnValue($systemTag));
+
+               $this->tree->expects($this->any())
+                       ->method('getNodeForPath')
+                       ->with('/systemtags-relations/files/12')
+                       ->will($this->returnValue($node));
+
+               $node->expects($this->once())
+                       ->method('createFile')
+                       ->with(1);
+
+               $request = $this->getMockBuilder('Sabre\HTTP\RequestInterface')
+                               ->disableOriginalConstructor()
+                               ->getMock();
+               $response = $this->getMockBuilder('Sabre\HTTP\ResponseInterface')
+                               ->disableOriginalConstructor()
+                               ->getMock();
+
+               $request->expects($this->once())
+                       ->method('getPath')
+                       ->will($this->returnValue('/systemtags-relations/files/12'));
+
+               $request->expects($this->once())
+                       ->method('getBodyAsString')
+                       ->will($this->returnValue($requestData));
+
+               $request->expects($this->once())
+                       ->method('getHeader')
+                       ->with('Content-Type')
+                       ->will($this->returnValue('application/json')); 
+
+               $request->expects($this->once())
+                       ->method('getBaseUrl')
+                       ->will($this->returnValue('http://example.com/dav/'));
+
+               $response->expects($this->once())
+                       ->method('setHeader')
+                       ->with('Location', 'http://example.com/dav/systemtags/1');
+
+               $this->plugin->httpPost($request, $response);
+       }
+
+       /**
+        * @dataProvider nodeClassProvider
+        * @expectedException Sabre\DAV\Exception\Conflict
+        */
+       public function testCreateTagConflict($nodeClass) {
+               $requestData = json_encode([
+                       'name' => 'Test',
+                       'userVisible' => true,
+                       'userAssignable' => false,
+               ]);
+
+               $node = $this->getMockBuilder($nodeClass)
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               $this->tagManager->expects($this->once())
+                       ->method('createTag')
+                       ->with('Test', true, false)
+                       ->will($this->throwException(new TagAlreadyExistsException('Tag already exists')));
+
+               $this->tree->expects($this->any())
+                       ->method('getNodeForPath')
+                       ->with('/systemtags')
+                       ->will($this->returnValue($node));
+
+               $request = $this->getMockBuilder('Sabre\HTTP\RequestInterface')
+                               ->disableOriginalConstructor()
+                               ->getMock();
+               $response = $this->getMockBuilder('Sabre\HTTP\ResponseInterface')
+                               ->disableOriginalConstructor()
+                               ->getMock();
+
+               $request->expects($this->once())
+                       ->method('getPath')
+                       ->will($this->returnValue('/systemtags'));
+
+               $request->expects($this->once())
+                       ->method('getBodyAsString')
+                       ->will($this->returnValue($requestData));
+
+               $request->expects($this->once())
+                       ->method('getHeader')
+                       ->with('Content-Type')
+                       ->will($this->returnValue('application/json')); 
+
+               $this->plugin->httpPost($request, $response);
+       }
+
+}
diff --git a/apps/dav/tests/unit/systemtag/systemtagsbyidcollection.php b/apps/dav/tests/unit/systemtag/systemtagsbyidcollection.php
new file mode 100644 (file)
index 0000000..fdaaf2c
--- /dev/null
@@ -0,0 +1,147 @@
+<?php
+/**
+ * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OCA\DAV\Tests\Unit\SystemTag;
+
+
+use OC\SystemTag\SystemTag;
+use OCP\SystemTag\TagNotFoundException;
+use OCP\SystemTag\TagAlreadyExistsException;
+
+class SystemTagsByIdCollection extends \Test\TestCase {
+
+       /**
+        * @var \OCA\DAV\SystemTag\SystemTagsByIdCollection
+        */
+       private $node;
+
+       /**
+        * @var \OCP\SystemTag\ISystemTagManager
+        */
+       private $tagManager;
+
+       protected function setUp() {
+               parent::setUp();
+
+               $this->tagManager = $this->getMock('\OCP\SystemTag\ISystemTagManager');
+
+               $this->node = new \OCA\DAV\SystemTag\SystemTagsByIdCollection($this->tagManager);
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\Forbidden
+        */
+       public function testForbiddenCreateFile() {
+               $this->node->createFile('555');
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\Forbidden
+        */
+       public function testForbiddenCreateDirectory() {
+               $this->node->createDirectory('789');
+       }
+
+       public function testGetChild() {
+               $tag = new SystemTag(123, 'Test', true, false);
+
+               $this->tagManager->expects($this->once())
+                       ->method('getTagsById')
+                       ->with('123')
+                       ->will($this->returnValue([$tag]));
+
+               $childNode = $this->node->getChild('123');
+
+               $this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagNode', $childNode);
+               $this->assertEquals('123', $childNode->getName());
+               $this->assertEquals($tag, $childNode->getSystemTag());
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\BadRequest
+        */
+       public function testGetChildInvalidName() {
+               $this->tagManager->expects($this->once())
+                       ->method('getTagsById')
+                       ->with('invalid')
+                       ->will($this->throwException(new \InvalidArgumentException()));
+
+               $this->node->getChild('invalid');
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\NotFound
+        */
+       public function testGetChildNotFound() {
+               $this->tagManager->expects($this->once())
+                       ->method('getTagsById')
+                       ->with('444')
+                       ->will($this->throwException(new TagNotFoundException()));
+
+               $this->node->getChild('444');
+       }
+
+       public function testGetChildren() {
+               $tag1 = new SystemTag(123, 'One', true, false);
+               $tag2 = new SystemTag(456, 'Two', true, true);
+
+               $this->tagManager->expects($this->once())
+                       ->method('getAllTags')
+                       ->with(true)
+                       ->will($this->returnValue([$tag1, $tag2]));
+
+               $children = $this->node->getChildren();
+
+               $this->assertCount(2, $children);
+
+               $this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagNode', $children[0]);
+               $this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagNode', $children[1]);
+               $this->assertEquals($tag1, $children[0]->getSystemTag());
+               $this->assertEquals($tag2, $children[1]->getSystemTag());
+       }
+
+       public function testGetChildrenEmpty() {
+               $this->tagManager->expects($this->once())
+                       ->method('getAllTags')
+                       ->with(true)
+                       ->will($this->returnValue([]));
+               $this->assertCount(0, $this->node->getChildren());
+       }
+
+       public function testChildExists() {
+               $tag = new SystemTag(123, 'One', true, false);
+
+               $this->tagManager->expects($this->once())
+                       ->method('getTagsById')
+                       ->with('123')
+                       ->will($this->returnValue([$tag]));
+
+               $this->assertTrue($this->node->childExists('123'));
+       }
+
+       public function testChildExistsNotFound() {
+               $this->tagManager->expects($this->once())
+                       ->method('getTagsById')
+                       ->with('123')
+                       ->will($this->throwException(new TagNotFoundException()));
+
+               $this->assertFalse($this->node->childExists('123'));
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\BadRequest
+        */
+       public function testChildExistsBadRequest() {
+               $this->tagManager->expects($this->once())
+                       ->method('getTagsById')
+                       ->with('invalid')
+                       ->will($this->throwException(new \InvalidArgumentException()));
+
+               $this->node->childExists('invalid');
+       }
+}
diff --git a/apps/dav/tests/unit/systemtag/systemtagsobjectmappingcollection.php b/apps/dav/tests/unit/systemtag/systemtagsobjectmappingcollection.php
new file mode 100644 (file)
index 0000000..1a9ffa6
--- /dev/null
@@ -0,0 +1,215 @@
+<?php
+/**
+ * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OCA\DAV\Tests\Unit\SystemTag;
+
+
+use OC\SystemTag\SystemTag;
+use OCP\SystemTag\TagNotFoundException;
+use OCP\SystemTag\TagAlreadyExistsException;
+
+class SystemTagsObjectMappingCollection extends \Test\TestCase {
+
+       /**
+        * @var \OCA\DAV\SystemTag\SystemTagsObjectTypeCollection
+        */
+       private $node;
+
+       /**
+        * @var \OCP\SystemTag\ISystemTagManager
+        */
+       private $tagManager;
+
+       /**
+        * @var \OCP\SystemTag\ISystemTagMapper
+        */
+       private $tagMapper;
+
+       protected function setUp() {
+               parent::setUp();
+
+               $this->tagManager = $this->getMock('\OCP\SystemTag\ISystemTagManager');
+               $this->tagMapper = $this->getMock('\OCP\SystemTag\ISystemTagObjectMapper');
+
+               $this->node = new \OCA\DAV\SystemTag\SystemTagsObjectMappingCollection (
+                       111,
+                       'files',
+                       $this->tagManager,
+                       $this->tagMapper
+               );
+       }
+
+       public function testAssignTag() {
+               $this->tagMapper->expects($this->once())
+                       ->method('assignTags')
+                       ->with(111, 'files', '555');
+
+               $this->node->createFile('555');
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\PreconditionFailed
+        */
+       public function testAssignTagNotFound() {
+               $this->tagMapper->expects($this->once())
+                       ->method('assignTags')
+                       ->with(111, 'files', '555')
+                       ->will($this->throwException(new TagNotFoundException()));
+
+               $this->node->createFile('555');
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\Forbidden
+        */
+       public function testForbiddenCreateDirectory() {
+               $this->node->createDirectory('789');
+       }
+
+       public function testGetChild() {
+               $tag = new SystemTag(555, 'TheTag', true, false);
+
+               $this->tagMapper->expects($this->once())
+                       ->method('haveTag')
+                       ->with(111, 'files', '555', true)
+                       ->will($this->returnValue(true));
+
+               $this->tagManager->expects($this->once())
+                       ->method('getTagsById')
+                       ->with('555')
+                       ->will($this->returnValue([$tag]));
+
+               $childNode = $this->node->getChild('555');
+
+               $this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagNode', $childNode);
+               $this->assertEquals('555', $childNode->getName());
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\NotFound
+        */
+       public function testGetChildRelationNotFound() {
+               $this->tagMapper->expects($this->once())
+                       ->method('haveTag')
+                       ->with(111, 'files', '777')
+                       ->will($this->returnValue(false));
+
+               $this->node->getChild('777');
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\BadRequest
+        */
+       public function testGetChildInvalidId() {
+               $this->tagMapper->expects($this->once())
+                       ->method('haveTag')
+                       ->with(111, 'files', 'badid')
+                       ->will($this->throwException(new \InvalidArgumentException()));
+
+               $this->node->getChild('badid');
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\NotFound
+        */
+       public function testGetChildTagDoesNotExist() {
+               $this->tagMapper->expects($this->once())
+                       ->method('haveTag')
+                       ->with(111, 'files', '777')
+                       ->will($this->throwException(new TagNotFoundException()));
+
+               $this->node->getChild('777');
+       }
+
+       public function testGetChildren() {
+               $tag1 = new SystemTag(555, 'TagOne', true, false);
+               $tag2 = new SystemTag(556, 'TagTwo', true, true);
+
+               $this->tagMapper->expects($this->once())
+                       ->method('getTagIdsForObjects')
+                       ->with(111, 'files')
+                       ->will($this->returnValue(['111' => ['555', '556']]));
+
+               $this->tagManager->expects($this->once())
+                       ->method('getTagsById')
+                       ->with(['555', '556'])
+                       ->will($this->returnValue(['555' => $tag1, '666' => $tag2]));
+
+               $children = $this->node->getChildren();
+
+               $this->assertCount(2, $children);
+
+               $this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagMappingNode', $children[0]);
+               $this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagMappingNode', $children[1]);
+
+               $this->assertEquals(111, $children[0]->getObjectId());
+               $this->assertEquals('files', $children[0]->getObjectType());
+               $this->assertEquals($tag1, $children[0]->getSystemTag());
+
+               $this->assertEquals(111, $children[1]->getObjectId());
+               $this->assertEquals('files', $children[1]->getObjectType());
+               $this->assertEquals($tag2, $children[1]->getSystemTag());
+       }
+
+       public function testChildExists() {
+               $this->tagMapper->expects($this->once())
+                       ->method('haveTag')
+                       ->with(111, 'files', '555')
+                       ->will($this->returnValue(true));
+
+               $this->assertTrue($this->node->childExists('555'));
+       }
+
+       public function testChildExistsNotFound() {
+               $this->tagMapper->expects($this->once())
+                       ->method('haveTag')
+                       ->with(111, 'files', '555')
+                       ->will($this->returnValue(false));
+
+               $this->assertFalse($this->node->childExists('555'));
+       }
+
+       public function testChildExistsTagNotFound() {
+               $this->tagMapper->expects($this->once())
+                       ->method('haveTag')
+                       ->with(111, 'files', '555')
+                       ->will($this->throwException(new TagNotFoundException()));
+
+               $this->assertFalse($this->node->childExists('555'));
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\BadRequest
+        */
+       public function testChildExistsInvalidId() {
+               $this->tagMapper->expects($this->once())
+                       ->method('haveTag')
+                       ->with(111, 'files', '555')
+                       ->will($this->throwException(new \InvalidArgumentException()));
+
+               $this->node->childExists('555');
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\Forbidden
+        */
+       public function testDelete() {
+               $this->node->delete();
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\Forbidden
+        */
+       public function testSetName() {
+               $this->node->setName('somethingelse');
+       }
+
+       public function testGetName() {
+               $this->assertEquals('111', $this->node->getName());
+       }
+}
diff --git a/apps/dav/tests/unit/systemtag/systemtagsobjecttypecollection.php b/apps/dav/tests/unit/systemtag/systemtagsobjecttypecollection.php
new file mode 100644 (file)
index 0000000..39223ff
--- /dev/null
@@ -0,0 +1,90 @@
+<?php
+/**
+ * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OCA\DAV\Tests\Unit\SystemTag;
+
+class SystemTagsObjectTypeCollection extends \Test\TestCase {
+
+       /**
+        * @var \OCA\DAV\SystemTag\SystemTagsObjectTypeCollection
+        */
+       private $node;
+
+       /**
+        * @var \OCP\SystemTag\ISystemTagManager
+        */
+       private $tagManager;
+
+       /**
+        * @var \OCP\SystemTag\ISystemTagMapper
+        */
+       private $tagMapper;
+
+       protected function setUp() {
+               parent::setUp();
+
+               $this->tagManager = $this->getMock('\OCP\SystemTag\ISystemTagManager');
+               $this->tagMapper = $this->getMock('\OCP\SystemTag\ISystemTagObjectMapper');
+
+               $this->node = new \OCA\DAV\SystemTag\SystemTagsObjectTypeCollection(
+                       'files',
+                       $this->tagManager,
+                       $this->tagMapper
+               );
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\Forbidden
+        */
+       public function testForbiddenCreateFile() {
+               $this->node->createFile('555');
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\Forbidden
+        */
+       public function testForbiddenCreateDirectory() {
+               $this->node->createDirectory('789');
+       }
+
+       public function testGetChild() {
+               $childNode = $this->node->getChild('files');
+
+               $this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagsObjectMappingCollection', $childNode);
+               $this->assertEquals('files', $childNode->getName());
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\MethodNotAllowed
+        */
+       public function testGetChildren() {
+               $this->node->getChildren();
+       }
+
+       public function testChildExists() {
+               $this->assertTrue($this->node->childExists('123'));
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\Forbidden
+        */
+       public function testDelete() {
+               $this->node->delete();
+       }
+
+       /**
+        * @expectedException Sabre\DAV\Exception\Forbidden
+        */
+       public function testSetName() {
+               $this->node->setName('somethingelse');
+       }
+
+       public function testGetName() {
+               $this->assertEquals('files', $this->node->getName());
+       }
+}