2014-12-15 19:49:24 +03:00
|
|
|
<?php
|
2016-01-12 17:02:16 +03:00
|
|
|
/**
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
2015-10-30 18:05:25 +03:00
|
|
|
namespace OCA\DAV\Tests\Unit\Connector\Sabre;
|
2014-12-15 19:49:24 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
|
|
|
|
2015-08-30 20:13:01 +03:00
|
|
|
const TAGS_PROPERTYNAME = \OCA\DAV\Connector\Sabre\TagsPlugin::TAGS_PROPERTYNAME;
|
|
|
|
const FAVORITE_PROPERTYNAME = \OCA\DAV\Connector\Sabre\TagsPlugin::FAVORITE_PROPERTYNAME;
|
|
|
|
const TAG_FAVORITE = \OCA\DAV\Connector\Sabre\TagsPlugin::TAG_FAVORITE;
|
2014-12-15 19:49:24 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Sabre\DAV\Server
|
|
|
|
*/
|
|
|
|
private $server;
|
|
|
|
|
|
|
|
/**
|
2015-10-30 18:05:25 +03:00
|
|
|
* @var \Sabre\DAV\Tree
|
2014-12-15 19:49:24 +03:00
|
|
|
*/
|
|
|
|
private $tree;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OCP\ITagManager
|
|
|
|
*/
|
|
|
|
private $tagManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OCP\ITags
|
|
|
|
*/
|
|
|
|
private $tagger;
|
|
|
|
|
|
|
|
/**
|
2015-08-30 20:13:01 +03:00
|
|
|
* @var \OCA\DAV\Connector\Sabre\TagsPlugin
|
2014-12-15 19:49:24 +03:00
|
|
|
*/
|
|
|
|
private $plugin;
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
$this->server = new \Sabre\DAV\Server();
|
2015-02-12 14:29:01 +03:00
|
|
|
$this->tree = $this->getMockBuilder('\Sabre\DAV\Tree')
|
2014-12-15 19:49:24 +03:00
|
|
|
->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));
|
2015-08-30 20:13:01 +03:00
|
|
|
$this->plugin = new \OCA\DAV\Connector\Sabre\TagsPlugin($this->tree, $this->tagManager);
|
2014-12-15 19:49:24 +03:00
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider tagsGetPropertiesDataProvider
|
|
|
|
*/
|
|
|
|
public function testGetProperties($tags, $requestedProperties, $expectedProperties) {
|
2015-08-30 20:13:01 +03:00
|
|
|
$node = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\Node')
|
2014-12-15 19:49:24 +03:00
|
|
|
->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)));
|
|
|
|
|
2015-02-12 14:29:01 +03:00
|
|
|
$propFind = new \Sabre\DAV\PropFind(
|
|
|
|
'/dummyPath',
|
2014-12-15 19:49:24 +03:00
|
|
|
$requestedProperties,
|
2015-02-12 14:29:01 +03:00
|
|
|
0
|
2014-12-15 19:49:24 +03:00
|
|
|
);
|
|
|
|
|
2015-02-12 14:29:01 +03:00
|
|
|
$this->plugin->handleGetProperties(
|
|
|
|
$propFind,
|
|
|
|
$node
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $propFind->getResultForMultiStatus();
|
|
|
|
|
|
|
|
$this->assertEmpty($result[404]);
|
|
|
|
unset($result[404]);
|
|
|
|
$this->assertEquals($expectedProperties, $result);
|
2014-12-15 19:49:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider tagsGetPropertiesDataProvider
|
|
|
|
*/
|
|
|
|
public function testPreloadThenGetProperties($tags, $requestedProperties, $expectedProperties) {
|
2015-08-30 20:13:01 +03:00
|
|
|
$node1 = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\File')
|
2014-12-15 19:49:24 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$node1->expects($this->any())
|
|
|
|
->method('getId')
|
|
|
|
->will($this->returnValue(111));
|
2015-08-30 20:13:01 +03:00
|
|
|
$node2 = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\File')
|
2014-12-15 19:49:24 +03:00
|
|
|
->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;
|
|
|
|
}
|
|
|
|
|
2015-08-30 20:13:01 +03:00
|
|
|
$node = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\Directory')
|
2014-12-15 19:49:24 +03:00
|
|
|
->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->tagger->expects($this->exactly($expectedCallCount))
|
|
|
|
->method('getTagsForObjects')
|
2015-02-12 14:29:01 +03:00
|
|
|
->with($this->equalTo(array(123, 111, 222)))
|
2014-12-15 19:49:24 +03:00
|
|
|
->will($this->returnValue(
|
|
|
|
array(
|
|
|
|
111 => $tags,
|
|
|
|
123 => $tags
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
2015-02-12 14:29:01 +03:00
|
|
|
// simulate sabre recursive PROPFIND traversal
|
|
|
|
$propFindRoot = new \Sabre\DAV\PropFind(
|
2014-12-15 19:49:24 +03:00
|
|
|
'/subdir',
|
|
|
|
$requestedProperties,
|
|
|
|
1
|
|
|
|
);
|
2015-02-12 14:29:01 +03:00
|
|
|
$propFind1 = new \Sabre\DAV\PropFind(
|
2014-12-15 19:49:24 +03:00
|
|
|
'/subdir/test.txt',
|
|
|
|
$requestedProperties,
|
2015-02-12 14:29:01 +03:00
|
|
|
0
|
|
|
|
);
|
|
|
|
$propFind2 = new \Sabre\DAV\PropFind(
|
|
|
|
'/subdir/test2.txt',
|
|
|
|
$requestedProperties,
|
|
|
|
0
|
2014-12-15 19:49:24 +03:00
|
|
|
);
|
|
|
|
|
2015-02-12 14:29:01 +03:00
|
|
|
$this->plugin->handleGetProperties(
|
|
|
|
$propFindRoot,
|
|
|
|
$node
|
|
|
|
);
|
|
|
|
$this->plugin->handleGetProperties(
|
|
|
|
$propFind1,
|
|
|
|
$node1
|
|
|
|
);
|
|
|
|
$this->plugin->handleGetProperties(
|
|
|
|
$propFind2,
|
|
|
|
$node2
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $propFind1->getResultForMultiStatus();
|
|
|
|
|
|
|
|
$this->assertEmpty($result[404]);
|
|
|
|
unset($result[404]);
|
|
|
|
$this->assertEquals($expectedProperties, $result);
|
2014-12-15 19:49:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
2015-08-30 20:13:01 +03:00
|
|
|
self::TAGS_PROPERTYNAME => new \OCA\DAV\Connector\Sabre\TagList(array('tag1', 'tag2')),
|
2014-12-15 19:49:24 +03:00
|
|
|
self::FAVORITE_PROPERTYNAME => true,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
// request tags alone
|
|
|
|
array(
|
|
|
|
array('tag1', 'tag2', self::TAG_FAVORITE),
|
|
|
|
array(self::TAGS_PROPERTYNAME),
|
|
|
|
array(
|
|
|
|
200 => array(
|
2015-08-30 20:13:01 +03:00
|
|
|
self::TAGS_PROPERTYNAME => new \OCA\DAV\Connector\Sabre\TagList(array('tag1', 'tag2')),
|
2014-12-15 19:49:24 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
// 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(),
|
2015-02-12 14:29:01 +03:00
|
|
|
array(
|
|
|
|
200 => array()
|
|
|
|
),
|
2014-12-15 19:49:24 +03:00
|
|
|
),
|
|
|
|
// request both with none set, receive both
|
|
|
|
array(
|
|
|
|
array(),
|
|
|
|
array(self::TAGS_PROPERTYNAME, self::FAVORITE_PROPERTYNAME),
|
|
|
|
array(
|
|
|
|
200 => array(
|
2015-08-30 20:13:01 +03:00
|
|
|
self::TAGS_PROPERTYNAME => new \OCA\DAV\Connector\Sabre\TagList(array()),
|
2014-12-15 19:49:24 +03:00
|
|
|
self::FAVORITE_PROPERTYNAME => false,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdateTags() {
|
|
|
|
// this test will replace the existing tags "tagremove" with "tag1" and "tag2"
|
|
|
|
// and keep "tagkeep"
|
2015-08-30 20:13:01 +03:00
|
|
|
$node = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\Node')
|
2014-12-15 19:49:24 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$node->expects($this->any())
|
|
|
|
->method('getId')
|
|
|
|
->will($this->returnValue(123));
|
|
|
|
|
2015-02-12 14:29:01 +03:00
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/dummypath')
|
|
|
|
->will($this->returnValue($node));
|
|
|
|
|
2014-12-15 19:49:24 +03:00
|
|
|
$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
|
2015-02-12 14:29:01 +03:00
|
|
|
$propPatch = new \Sabre\DAV\PropPatch(array(
|
2015-08-30 20:13:01 +03:00
|
|
|
self::TAGS_PROPERTYNAME => new \OCA\DAV\Connector\Sabre\TagList(array('tag1', 'tag2', 'tagkeep'))
|
2015-02-12 14:29:01 +03:00
|
|
|
));
|
2014-12-15 19:49:24 +03:00
|
|
|
|
2015-02-12 14:29:01 +03:00
|
|
|
$this->plugin->handleUpdateProperties(
|
|
|
|
'/dummypath',
|
|
|
|
$propPatch
|
2014-12-15 19:49:24 +03:00
|
|
|
);
|
|
|
|
|
2015-02-12 14:29:01 +03:00
|
|
|
$propPatch->commit();
|
|
|
|
|
2014-12-15 19:49:24 +03:00
|
|
|
// all requested properties removed, as they were processed already
|
2015-02-12 14:29:01 +03:00
|
|
|
$this->assertEmpty($propPatch->getRemainingMutations());
|
|
|
|
|
|
|
|
$result = $propPatch->getResult();
|
|
|
|
$this->assertEquals(200, $result[self::TAGS_PROPERTYNAME]);
|
|
|
|
$this->assertFalse(isset($result[self::FAVORITE_PROPERTYNAME]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdateTagsFromScratch() {
|
2015-08-30 20:13:01 +03:00
|
|
|
$node = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\Node')
|
2015-02-12 14:29:01 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$node->expects($this->any())
|
|
|
|
->method('getId')
|
|
|
|
->will($this->returnValue(123));
|
|
|
|
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/dummypath')
|
|
|
|
->will($this->returnValue($node));
|
|
|
|
|
|
|
|
$this->tagger->expects($this->at(0))
|
|
|
|
->method('getTagsForObjects')
|
|
|
|
->with($this->equalTo(array(123)))
|
|
|
|
->will($this->returnValue(array()));
|
|
|
|
|
|
|
|
// 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');
|
2014-12-15 19:49:24 +03:00
|
|
|
|
2015-02-12 14:29:01 +03:00
|
|
|
// properties to set
|
|
|
|
$propPatch = new \Sabre\DAV\PropPatch(array(
|
2015-08-30 20:13:01 +03:00
|
|
|
self::TAGS_PROPERTYNAME => new \OCA\DAV\Connector\Sabre\TagList(array('tag1', 'tag2', 'tagkeep'))
|
2015-02-12 14:29:01 +03:00
|
|
|
));
|
|
|
|
|
|
|
|
$this->plugin->handleUpdateProperties(
|
|
|
|
'/dummypath',
|
|
|
|
$propPatch
|
2014-12-15 19:49:24 +03:00
|
|
|
);
|
2015-02-12 14:29:01 +03:00
|
|
|
|
|
|
|
$propPatch->commit();
|
|
|
|
|
|
|
|
// all requested properties removed, as they were processed already
|
|
|
|
$this->assertEmpty($propPatch->getRemainingMutations());
|
|
|
|
|
|
|
|
$result = $propPatch->getResult();
|
|
|
|
$this->assertEquals(200, $result[self::TAGS_PROPERTYNAME]);
|
|
|
|
$this->assertFalse(false, isset($result[self::FAVORITE_PROPERTYNAME]));
|
2014-12-15 19:49:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdateFav() {
|
|
|
|
// this test will replace the existing tags "tagremove" with "tag1" and "tag2"
|
|
|
|
// and keep "tagkeep"
|
2015-08-30 20:13:01 +03:00
|
|
|
$node = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\Node')
|
2014-12-15 19:49:24 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$node->expects($this->any())
|
|
|
|
->method('getId')
|
|
|
|
->will($this->returnValue(123));
|
|
|
|
|
2015-02-12 14:29:01 +03:00
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/dummypath')
|
|
|
|
->will($this->returnValue($node));
|
|
|
|
|
2014-12-15 19:49:24 +03:00
|
|
|
// set favorite tag
|
|
|
|
$this->tagger->expects($this->once())
|
|
|
|
->method('tagAs')
|
|
|
|
->with(123, self::TAG_FAVORITE);
|
|
|
|
|
|
|
|
// properties to set
|
2015-02-12 14:29:01 +03:00
|
|
|
$propPatch = new \Sabre\DAV\PropPatch(array(
|
2014-12-15 19:49:24 +03:00
|
|
|
self::FAVORITE_PROPERTYNAME => true
|
2015-02-12 14:29:01 +03:00
|
|
|
));
|
|
|
|
|
|
|
|
$this->plugin->handleUpdateProperties(
|
|
|
|
'/dummypath',
|
|
|
|
$propPatch
|
2014-12-15 19:49:24 +03:00
|
|
|
);
|
|
|
|
|
2015-02-12 14:29:01 +03:00
|
|
|
$propPatch->commit();
|
|
|
|
|
2014-12-15 19:49:24 +03:00
|
|
|
// all requested properties removed, as they were processed already
|
2015-02-12 14:29:01 +03:00
|
|
|
$this->assertEmpty($propPatch->getRemainingMutations());
|
2014-12-15 19:49:24 +03:00
|
|
|
|
2015-02-12 14:29:01 +03:00
|
|
|
$result = $propPatch->getResult();
|
|
|
|
$this->assertFalse(false, isset($result[self::TAGS_PROPERTYNAME]));
|
|
|
|
$this->assertEquals(200, isset($result[self::FAVORITE_PROPERTYNAME]));
|
2014-12-15 19:49:24 +03:00
|
|
|
|
|
|
|
// unfavorite now
|
|
|
|
// set favorite tag
|
|
|
|
$this->tagger->expects($this->once())
|
|
|
|
->method('unTag')
|
|
|
|
->with(123, self::TAG_FAVORITE);
|
|
|
|
|
2015-02-12 14:29:01 +03:00
|
|
|
// properties to set
|
|
|
|
$propPatch = new \Sabre\DAV\PropPatch(array(
|
2014-12-15 19:49:24 +03:00
|
|
|
self::FAVORITE_PROPERTYNAME => false
|
2015-02-12 14:29:01 +03:00
|
|
|
));
|
|
|
|
|
|
|
|
$this->plugin->handleUpdateProperties(
|
|
|
|
'/dummypath',
|
|
|
|
$propPatch
|
2014-12-15 19:49:24 +03:00
|
|
|
);
|
|
|
|
|
2015-02-12 14:29:01 +03:00
|
|
|
$propPatch->commit();
|
|
|
|
|
|
|
|
// all requested properties removed, as they were processed already
|
|
|
|
$this->assertEmpty($propPatch->getRemainingMutations());
|
|
|
|
|
|
|
|
$result = $propPatch->getResult();
|
|
|
|
$this->assertFalse(false, isset($result[self::TAGS_PROPERTYNAME]));
|
|
|
|
$this->assertEquals(200, isset($result[self::FAVORITE_PROPERTYNAME]));
|
2014-12-15 19:49:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|