diff options
author | Vincent Petry <pvince81@owncloud.com> | 2014-12-15 17:49:24 +0100 |
---|---|---|
committer | Vincent Petry <pvince81@owncloud.com> | 2014-12-17 16:50:35 +0100 |
commit | 0b3f0716fc8e278a5727885548bfa406004263dc (patch) | |
tree | b0413ceeaa79e69c9a61655baac3bab4278a507d /tests | |
parent | be3d4fd303569a99554dbc6c62ce8992a45c51ad (diff) | |
download | nextcloud-server-0b3f0716fc8e278a5727885548bfa406004263dc.tar.gz nextcloud-server-0b3f0716fc8e278a5727885548bfa406004263dc.zip |
Returns and update tags through WebDAV PROPFIND and PROPPATCH
Added oc:tags and oc:favorites in PROPFIND response.
It is possible to update them with PROPPATCH.
These properties are optional which means they need to be requested
explicitly
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/connector/sabre/directory.php | 54 | ||||
-rw-r--r-- | tests/lib/connector/sabre/tagsplugin.php | 314 |
2 files changed, 368 insertions, 0 deletions
diff --git a/tests/lib/connector/sabre/directory.php b/tests/lib/connector/sabre/directory.php index d8dca35cd71..e9bfea81b77 100644 --- a/tests/lib/connector/sabre/directory.php +++ b/tests/lib/connector/sabre/directory.php @@ -101,4 +101,58 @@ class Test_OC_Connector_Sabre_Directory extends \Test\TestCase { $dir = $this->getRootDir(); $dir->delete(); } + + public function testGetChildren() { + $info1 = $this->getMockBuilder('OC\Files\FileInfo') + ->disableOriginalConstructor() + ->getMock(); + $info2 = $this->getMockBuilder('OC\Files\FileInfo') + ->disableOriginalConstructor() + ->getMock(); + $info1->expects($this->any()) + ->method('getName') + ->will($this->returnValue('first')); + $info1->expects($this->any()) + ->method('getEtag') + ->will($this->returnValue('abc')); + $info2->expects($this->any()) + ->method('getName') + ->will($this->returnValue('second')); + $info2->expects($this->any()) + ->method('getEtag') + ->will($this->returnValue('def')); + + $this->view->expects($this->once()) + ->method('getDirectoryContent') + ->with('') + ->will($this->returnValue(array($info1, $info2))); + + $this->view->expects($this->any()) + ->method('getRelativePath') + ->will($this->returnValue('')); + + $dir = new OC_Connector_Sabre_Directory($this->view, $this->info); + $nodes = $dir->getChildren(); + + $this->assertEquals(2, count($nodes)); + + // calling a second time just returns the cached values, + // does not call getDirectoryContents again + $nodes = $dir->getChildren(); + + $properties = array('testprop', OC_Connector_Sabre_Node::GETETAG_PROPERTYNAME); + $this->assertEquals(2, count($nodes)); + $this->assertEquals( + array( + OC_Connector_Sabre_Node::GETETAG_PROPERTYNAME => '"abc"' + ), + $nodes[0]->getProperties($properties) + ); + $this->assertEquals( + array( + OC_Connector_Sabre_Node::GETETAG_PROPERTYNAME => '"def"' + ), + $nodes[1]->getProperties($properties) + ); + } } diff --git a/tests/lib/connector/sabre/tagsplugin.php b/tests/lib/connector/sabre/tagsplugin.php new file mode 100644 index 00000000000..2afea061ec3 --- /dev/null +++ b/tests/lib/connector/sabre/tagsplugin.php @@ -0,0 +1,314 @@ +<?php + +namespace Tests\Connector\Sabre; + +/** + * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ +class TagsPlugin extends \Test\TestCase { + + const TAGS_PROPERTYNAME = \OC\Connector\Sabre\TagsPlugin::TAGS_PROPERTYNAME; + const FAVORITE_PROPERTYNAME = \OC\Connector\Sabre\TagsPlugin::FAVORITE_PROPERTYNAME; + const TAG_FAVORITE = \OC\Connector\Sabre\TagsPlugin::TAG_FAVORITE; + + /** + * @var \Sabre\DAV\Server + */ + private $server; + + /** + * @var \Sabre\DAV\ObjectTree + */ + private $tree; + + /** + * @var \OCP\ITagManager + */ + private $tagManager; + + /** + * @var \OCP\ITags + */ + private $tagger; + + /** + * @var \OC\Connector\Sabre\TagsPlugin + */ + private $plugin; + + public function setUp() { + parent::setUp(); + $this->server = new \Sabre\DAV\Server(); + $this->tree = $this->getMockBuilder('\Sabre\DAV\ObjectTree') + ->disableOriginalConstructor() + ->getMock(); + $this->tagger = $this->getMock('\OCP\ITags'); + $this->tagManager = $this->getMock('\OCP\ITagManager'); + $this->tagManager->expects($this->any()) + ->method('load') + ->with('files') + ->will($this->returnValue($this->tagger)); + $this->plugin = new \OC\Connector\Sabre\TagsPlugin($this->tree, $this->tagManager); + $this->plugin->initialize($this->server); + } + + /** + * @dataProvider tagsGetPropertiesDataProvider + */ + public function testGetProperties($tags, $requestedProperties, $expectedProperties) { + $node = $this->getMockBuilder('\OC_Connector_Sabre_Node') + ->disableOriginalConstructor() + ->getMock(); + $node->expects($this->any()) + ->method('getId') + ->will($this->returnValue(123)); + + $expectedCallCount = 0; + if (count($requestedProperties) > 0) { + $expectedCallCount = 1; + } + + $this->tagger->expects($this->exactly($expectedCallCount)) + ->method('getTagsForObjects') + ->with($this->equalTo(array(123))) + ->will($this->returnValue(array(123 => $tags))); + + $returnedProperties = array(); + + $this->plugin->beforeGetProperties( + '', + $node, + $requestedProperties, + $returnedProperties + ); + + $this->assertEquals($expectedProperties, $returnedProperties); + } + + /** + * @dataProvider tagsGetPropertiesDataProvider + */ + public function testPreloadThenGetProperties($tags, $requestedProperties, $expectedProperties) { + $node1 = $this->getMockBuilder('\OC_Connector_Sabre_File') + ->disableOriginalConstructor() + ->getMock(); + $node1->expects($this->any()) + ->method('getId') + ->will($this->returnValue(111)); + $node2 = $this->getMockBuilder('\OC_Connector_Sabre_File') + ->disableOriginalConstructor() + ->getMock(); + $node2->expects($this->any()) + ->method('getId') + ->will($this->returnValue(222)); + + $expectedCallCount = 0; + if (count($requestedProperties) > 0) { + // this guarantees that getTagsForObjects + // is only called once and then the tags + // are cached + $expectedCallCount = 1; + } + + $node = $this->getMockBuilder('\OC_Connector_Sabre_Directory') + ->disableOriginalConstructor() + ->getMock(); + $node->expects($this->any()) + ->method('getId') + ->will($this->returnValue(123)); + $node->expects($this->exactly($expectedCallCount)) + ->method('getChildren') + ->will($this->returnValue(array($node1, $node2))); + + $this->tree->expects($this->once()) + ->method('getNodeForPath') + ->with('/subdir') + ->will($this->returnValue($node)); + + $this->tagger->expects($this->exactly($expectedCallCount)) + ->method('getTagsForObjects') + ->with($this->equalTo(array(111, 222))) + ->will($this->returnValue( + array( + 111 => $tags, + 123 => $tags + ) + )); + + $returnedProperties = array(); + + $this->plugin->beforeGetPropertiesForPath( + '/subdir', + $requestedProperties, + 1 + ); + + $this->plugin->beforeGetProperties( + '/subdir/test.txt', + $node1, + $requestedProperties, + $returnedProperties + ); + + $this->assertEquals($expectedProperties, $returnedProperties); + } + + function tagsGetPropertiesDataProvider() { + return array( + // request both, receive both + array( + array('tag1', 'tag2', self::TAG_FAVORITE), + array(self::TAGS_PROPERTYNAME, self::FAVORITE_PROPERTYNAME), + array( + 200 => array( + self::TAGS_PROPERTYNAME => new \OC\Connector\Sabre\TagList(array('tag1', 'tag2')), + self::FAVORITE_PROPERTYNAME => true, + ) + ) + ), + // request tags alone + array( + array('tag1', 'tag2', self::TAG_FAVORITE), + array(self::TAGS_PROPERTYNAME), + array( + 200 => array( + self::TAGS_PROPERTYNAME => new \OC\Connector\Sabre\TagList(array('tag1', 'tag2')), + ) + ) + ), + // request fav alone + array( + array('tag1', 'tag2', self::TAG_FAVORITE), + array(self::FAVORITE_PROPERTYNAME), + array( + 200 => array( + self::FAVORITE_PROPERTYNAME => true, + ) + ) + ), + // request none + array( + array('tag1', 'tag2', self::TAG_FAVORITE), + array(), + array(), + ), + // request both with none set, receive both + array( + array(), + array(self::TAGS_PROPERTYNAME, self::FAVORITE_PROPERTYNAME), + array( + 200 => array( + self::TAGS_PROPERTYNAME => new \OC\Connector\Sabre\TagList(array()), + self::FAVORITE_PROPERTYNAME => false, + ) + ) + ), + ); + } + + public function testUpdateTags() { + // this test will replace the existing tags "tagremove" with "tag1" and "tag2" + // and keep "tagkeep" + $node = $this->getMockBuilder('\OC_Connector_Sabre_Node') + ->disableOriginalConstructor() + ->getMock(); + $node->expects($this->any()) + ->method('getId') + ->will($this->returnValue(123)); + + $this->tagger->expects($this->at(0)) + ->method('getTagsForObjects') + ->with($this->equalTo(array(123))) + ->will($this->returnValue(array(123 => array('tagkeep', 'tagremove', self::TAG_FAVORITE)))); + + // then tag as tag1 and tag2 + $this->tagger->expects($this->at(1)) + ->method('tagAs') + ->with(123, 'tag1'); + $this->tagger->expects($this->at(2)) + ->method('tagAs') + ->with(123, 'tag2'); + + // it will untag tag3 + $this->tagger->expects($this->at(3)) + ->method('unTag') + ->with(123, 'tagremove'); + + // properties to set + $properties = array( + self::TAGS_PROPERTYNAME => new \OC\Connector\Sabre\TagList(array('tag1', 'tag2', 'tagkeep')) + ); + $result = array(); + + $this->plugin->updateProperties( + $properties, + $result, + $node + ); + + // all requested properties removed, as they were processed already + $this->assertEmpty($properties); + + $this->assertEquals( + new \OC\Connector\Sabre\TagList(array('tag1', 'tag2', 'tagkeep')), + $result[200][self::TAGS_PROPERTYNAME] + ); + $this->assertFalse(isset($result[200][self::FAVORITE_PROPERTYNAME])); + } + + public function testUpdateFav() { + // this test will replace the existing tags "tagremove" with "tag1" and "tag2" + // and keep "tagkeep" + $node = $this->getMockBuilder('\OC_Connector_Sabre_Node') + ->disableOriginalConstructor() + ->getMock(); + $node->expects($this->any()) + ->method('getId') + ->will($this->returnValue(123)); + + // set favorite tag + $this->tagger->expects($this->once()) + ->method('tagAs') + ->with(123, self::TAG_FAVORITE); + + // properties to set + $properties = array( + self::FAVORITE_PROPERTYNAME => true + ); + $result = array(); + $this->plugin->updateProperties( + $properties, + $result, + $node + ); + + // all requested properties removed, as they were processed already + $this->assertEmpty($properties); + + $this->assertTrue($result[200][self::FAVORITE_PROPERTYNAME]); + $this->assertFalse(isset($result[200][self::TAGS_PROPERTYNAME])); + + // unfavorite now + // set favorite tag + $this->tagger->expects($this->once()) + ->method('unTag') + ->with(123, self::TAG_FAVORITE); + + $properties = array( + self::FAVORITE_PROPERTYNAME => false + ); + $result = array(); + $this->plugin->updateProperties( + $properties, + $result, + $node + ); + + $this->assertFalse($result[200][self::FAVORITE_PROPERTYNAME]); + $this->assertFalse(isset($result[200][self::TAGS_PROPERTYNAME])); + } + +} |