2015-12-03 13:26:16 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-01-12 17:02:16 +03:00
|
|
|
* @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-12-03 13:26:16 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\DAV\Tests\Unit\SystemTag;
|
|
|
|
|
|
|
|
|
|
|
|
use OC\SystemTag\SystemTag;
|
|
|
|
use OCP\SystemTag\TagNotFoundException;
|
|
|
|
|
|
|
|
class SystemTagsObjectMappingCollection extends \Test\TestCase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OCP\SystemTag\ISystemTagManager
|
|
|
|
*/
|
|
|
|
private $tagManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OCP\SystemTag\ISystemTagMapper
|
|
|
|
*/
|
|
|
|
private $tagMapper;
|
|
|
|
|
2016-05-11 16:39:24 +03:00
|
|
|
/**
|
|
|
|
* @var \OCP\IUser
|
|
|
|
*/
|
|
|
|
private $user;
|
|
|
|
|
2015-12-03 13:26:16 +03:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->tagManager = $this->getMock('\OCP\SystemTag\ISystemTagManager');
|
|
|
|
$this->tagMapper = $this->getMock('\OCP\SystemTag\ISystemTagObjectMapper');
|
2016-05-11 16:39:24 +03:00
|
|
|
|
|
|
|
$this->user = $this->getMock('\OCP\IUser');
|
2016-01-20 13:47:23 +03:00
|
|
|
}
|
2015-12-03 13:26:16 +03:00
|
|
|
|
2016-05-11 16:39:24 +03:00
|
|
|
public function getNode() {
|
2016-01-20 13:47:23 +03:00
|
|
|
return new \OCA\DAV\SystemTag\SystemTagsObjectMappingCollection (
|
2015-12-03 13:26:16 +03:00
|
|
|
111,
|
|
|
|
'files',
|
2016-05-11 16:39:24 +03:00
|
|
|
$this->user,
|
2015-12-03 13:26:16 +03:00
|
|
|
$this->tagManager,
|
|
|
|
$this->tagMapper
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-05-11 16:39:24 +03:00
|
|
|
public function testAssignTag() {
|
2016-01-20 13:47:23 +03:00
|
|
|
$tag = new SystemTag('1', 'Test', true, true);
|
2016-05-11 16:39:24 +03:00
|
|
|
$this->tagManager->expects($this->once())
|
|
|
|
->method('canUserSeeTag')
|
|
|
|
->with($tag)
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$this->tagManager->expects($this->once())
|
|
|
|
->method('canUserAssignTag')
|
|
|
|
->with($tag)
|
|
|
|
->will($this->returnValue(true));
|
2016-01-20 13:47:23 +03:00
|
|
|
|
|
|
|
$this->tagManager->expects($this->once())
|
|
|
|
->method('getTagsByIds')
|
2016-05-11 16:39:24 +03:00
|
|
|
->with(['555'])
|
2016-01-20 13:47:23 +03:00
|
|
|
->will($this->returnValue([$tag]));
|
2015-12-03 13:26:16 +03:00
|
|
|
$this->tagMapper->expects($this->once())
|
|
|
|
->method('assignTags')
|
|
|
|
->with(111, 'files', '555');
|
|
|
|
|
2016-05-11 16:39:24 +03:00
|
|
|
$this->getNode()->createFile('555');
|
2016-01-20 13:47:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function permissionsProvider() {
|
|
|
|
return [
|
|
|
|
// invisible, tag does not exist for user
|
|
|
|
[false, true, '\Sabre\DAV\Exception\PreconditionFailed'],
|
|
|
|
// visible but static, cannot assign tag
|
|
|
|
[true, false, '\Sabre\DAV\Exception\Forbidden'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider permissionsProvider
|
|
|
|
*/
|
2016-05-11 16:39:24 +03:00
|
|
|
public function testAssignTagNoPermission($userVisible, $userAssignable, $expectedException) {
|
2016-01-20 13:47:23 +03:00
|
|
|
$tag = new SystemTag('1', 'Test', $userVisible, $userAssignable);
|
2016-05-11 16:39:24 +03:00
|
|
|
$this->tagManager->expects($this->once())
|
|
|
|
->method('canUserSeeTag')
|
|
|
|
->with($tag)
|
|
|
|
->will($this->returnValue($userVisible));
|
|
|
|
$this->tagManager->expects($this->any())
|
|
|
|
->method('canUserAssignTag')
|
|
|
|
->with($tag)
|
|
|
|
->will($this->returnValue($userAssignable));
|
2016-01-20 13:47:23 +03:00
|
|
|
|
|
|
|
$this->tagManager->expects($this->once())
|
|
|
|
->method('getTagsByIds')
|
2016-05-11 16:39:24 +03:00
|
|
|
->with(['555'])
|
2016-01-20 13:47:23 +03:00
|
|
|
->will($this->returnValue([$tag]));
|
|
|
|
$this->tagMapper->expects($this->never())
|
|
|
|
->method('assignTags');
|
|
|
|
|
|
|
|
$thrown = null;
|
|
|
|
try {
|
2016-05-11 16:39:24 +03:00
|
|
|
$this->getNode()->createFile('555');
|
2016-01-20 13:47:23 +03:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
$thrown = $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertInstanceOf($expectedException, $thrown);
|
2015-12-03 13:26:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Sabre\DAV\Exception\PreconditionFailed
|
|
|
|
*/
|
|
|
|
public function testAssignTagNotFound() {
|
2016-05-11 16:39:24 +03:00
|
|
|
$this->tagManager->expects($this->once())
|
|
|
|
->method('getTagsByIds')
|
|
|
|
->with(['555'])
|
2015-12-03 13:26:16 +03:00
|
|
|
->will($this->throwException(new TagNotFoundException()));
|
|
|
|
|
2016-01-20 13:47:23 +03:00
|
|
|
$this->getNode()->createFile('555');
|
2015-12-03 13:26:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Sabre\DAV\Exception\Forbidden
|
|
|
|
*/
|
|
|
|
public function testForbiddenCreateDirectory() {
|
2016-01-20 13:47:23 +03:00
|
|
|
$this->getNode()->createDirectory('789');
|
2015-12-03 13:26:16 +03:00
|
|
|
}
|
|
|
|
|
2016-05-11 16:39:24 +03:00
|
|
|
public function testGetChild() {
|
|
|
|
$tag = new SystemTag(555, 'TheTag', true, false);
|
|
|
|
$this->tagManager->expects($this->once())
|
|
|
|
->method('canUserSeeTag')
|
|
|
|
->with($tag)
|
|
|
|
->will($this->returnValue(true));
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
$this->tagMapper->expects($this->once())
|
|
|
|
->method('haveTag')
|
2015-12-04 19:30:22 +03:00
|
|
|
->with([111], 'files', '555', true)
|
2015-12-03 13:26:16 +03:00
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$this->tagManager->expects($this->once())
|
2015-12-04 19:30:22 +03:00
|
|
|
->method('getTagsByIds')
|
|
|
|
->with(['555'])
|
|
|
|
->will($this->returnValue(['555' => $tag]));
|
2015-12-03 13:26:16 +03:00
|
|
|
|
2016-05-11 16:39:24 +03:00
|
|
|
$childNode = $this->getNode()->getChild('555');
|
2015-12-03 13:26:16 +03:00
|
|
|
|
2016-05-11 16:39:24 +03:00
|
|
|
$this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagMappingNode', $childNode);
|
2015-12-03 13:26:16 +03:00
|
|
|
$this->assertEquals('555', $childNode->getName());
|
|
|
|
}
|
|
|
|
|
2016-01-20 13:47:23 +03:00
|
|
|
/**
|
|
|
|
* @expectedException \Sabre\DAV\Exception\NotFound
|
|
|
|
*/
|
2016-05-11 16:39:24 +03:00
|
|
|
public function testGetChildNonVisible() {
|
2016-01-20 13:47:23 +03:00
|
|
|
$tag = new SystemTag(555, 'TheTag', false, false);
|
2016-05-11 16:39:24 +03:00
|
|
|
$this->tagManager->expects($this->once())
|
|
|
|
->method('canUserSeeTag')
|
|
|
|
->with($tag)
|
|
|
|
->will($this->returnValue(false));
|
2016-01-20 13:47:23 +03:00
|
|
|
|
|
|
|
$this->tagMapper->expects($this->once())
|
|
|
|
->method('haveTag')
|
|
|
|
->with([111], 'files', '555', true)
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$this->tagManager->expects($this->once())
|
|
|
|
->method('getTagsByIds')
|
|
|
|
->with(['555'])
|
|
|
|
->will($this->returnValue(['555' => $tag]));
|
|
|
|
|
2016-05-11 16:39:24 +03:00
|
|
|
$this->getNode()->getChild('555');
|
2016-01-20 13:47:23 +03:00
|
|
|
}
|
|
|
|
|
2015-12-03 13:26:16 +03:00
|
|
|
/**
|
|
|
|
* @expectedException Sabre\DAV\Exception\NotFound
|
|
|
|
*/
|
|
|
|
public function testGetChildRelationNotFound() {
|
|
|
|
$this->tagMapper->expects($this->once())
|
|
|
|
->method('haveTag')
|
2015-12-04 19:30:22 +03:00
|
|
|
->with([111], 'files', '777')
|
2015-12-03 13:26:16 +03:00
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
2016-01-20 13:47:23 +03:00
|
|
|
$this->getNode()->getChild('777');
|
2015-12-03 13:26:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Sabre\DAV\Exception\BadRequest
|
|
|
|
*/
|
|
|
|
public function testGetChildInvalidId() {
|
|
|
|
$this->tagMapper->expects($this->once())
|
|
|
|
->method('haveTag')
|
2015-12-04 19:30:22 +03:00
|
|
|
->with([111], 'files', 'badid')
|
2015-12-03 13:26:16 +03:00
|
|
|
->will($this->throwException(new \InvalidArgumentException()));
|
|
|
|
|
2016-01-20 13:47:23 +03:00
|
|
|
$this->getNode()->getChild('badid');
|
2015-12-03 13:26:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Sabre\DAV\Exception\NotFound
|
|
|
|
*/
|
|
|
|
public function testGetChildTagDoesNotExist() {
|
|
|
|
$this->tagMapper->expects($this->once())
|
|
|
|
->method('haveTag')
|
2015-12-04 19:30:22 +03:00
|
|
|
->with([111], 'files', '777')
|
2015-12-03 13:26:16 +03:00
|
|
|
->will($this->throwException(new TagNotFoundException()));
|
|
|
|
|
2016-01-20 13:47:23 +03:00
|
|
|
$this->getNode()->getChild('777');
|
|
|
|
}
|
|
|
|
|
2016-05-11 16:39:24 +03:00
|
|
|
public function testGetChildren() {
|
2016-01-20 13:47:23 +03:00
|
|
|
$tag1 = new SystemTag(555, 'TagOne', true, false);
|
|
|
|
$tag2 = new SystemTag(556, 'TagTwo', true, true);
|
|
|
|
$tag3 = new SystemTag(557, 'InvisibleTag', false, true);
|
|
|
|
|
|
|
|
$this->tagMapper->expects($this->once())
|
|
|
|
->method('getTagIdsForObjects')
|
|
|
|
->with([111], 'files')
|
|
|
|
->will($this->returnValue(['111' => ['555', '556', '557']]));
|
|
|
|
|
|
|
|
$this->tagManager->expects($this->once())
|
|
|
|
->method('getTagsByIds')
|
|
|
|
->with(['555', '556', '557'])
|
|
|
|
->will($this->returnValue(['555' => $tag1, '556' => $tag2, '557' => $tag3]));
|
|
|
|
|
2016-05-11 16:39:24 +03:00
|
|
|
$this->tagManager->expects($this->exactly(3))
|
|
|
|
->method('canUserSeeTag')
|
|
|
|
->will($this->returnCallback(function($tag) {
|
|
|
|
return $tag->isUserVisible();
|
|
|
|
}));
|
2016-01-20 13:47:23 +03:00
|
|
|
|
2016-05-11 16:39:24 +03:00
|
|
|
$children = $this->getNode()->getChildren();
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
$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());
|
|
|
|
}
|
|
|
|
|
2016-05-11 16:39:24 +03:00
|
|
|
public function testChildExistsWithVisibleTag() {
|
2016-01-20 13:47:23 +03:00
|
|
|
$tag = new SystemTag(555, 'TagOne', true, false);
|
|
|
|
|
|
|
|
$this->tagMapper->expects($this->once())
|
|
|
|
->method('haveTag')
|
|
|
|
->with([111], 'files', '555')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
2016-05-11 16:39:24 +03:00
|
|
|
$this->tagManager->expects($this->once())
|
|
|
|
->method('canUserSeeTag')
|
|
|
|
->with($tag)
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
2016-01-20 13:47:23 +03:00
|
|
|
$this->tagManager->expects($this->once())
|
|
|
|
->method('getTagsByIds')
|
2016-05-11 16:39:24 +03:00
|
|
|
->with(['555'])
|
2016-01-20 13:47:23 +03:00
|
|
|
->will($this->returnValue([$tag]));
|
|
|
|
|
2016-05-11 16:39:24 +03:00
|
|
|
$this->assertTrue($this->getNode()->childExists('555'));
|
2016-01-20 13:47:23 +03:00
|
|
|
}
|
|
|
|
|
2016-05-11 16:39:24 +03:00
|
|
|
public function testChildExistsWithInvisibleTag() {
|
2016-01-20 13:47:23 +03:00
|
|
|
$tag = new SystemTag(555, 'TagOne', false, false);
|
|
|
|
|
|
|
|
$this->tagMapper->expects($this->once())
|
|
|
|
->method('haveTag')
|
|
|
|
->with([111], 'files', '555')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$this->tagManager->expects($this->once())
|
|
|
|
->method('getTagsByIds')
|
2016-05-11 16:39:24 +03:00
|
|
|
->with(['555'])
|
2016-01-20 13:47:23 +03:00
|
|
|
->will($this->returnValue([$tag]));
|
|
|
|
|
2016-05-11 16:39:24 +03:00
|
|
|
$this->assertFalse($this->getNode()->childExists('555'));
|
2015-12-03 13:26:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testChildExistsNotFound() {
|
|
|
|
$this->tagMapper->expects($this->once())
|
|
|
|
->method('haveTag')
|
2015-12-04 19:30:22 +03:00
|
|
|
->with([111], 'files', '555')
|
2015-12-03 13:26:16 +03:00
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
2016-01-20 13:47:23 +03:00
|
|
|
$this->assertFalse($this->getNode()->childExists('555'));
|
2015-12-03 13:26:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testChildExistsTagNotFound() {
|
|
|
|
$this->tagMapper->expects($this->once())
|
|
|
|
->method('haveTag')
|
2015-12-04 19:30:22 +03:00
|
|
|
->with([111], 'files', '555')
|
2015-12-03 13:26:16 +03:00
|
|
|
->will($this->throwException(new TagNotFoundException()));
|
|
|
|
|
2016-01-20 13:47:23 +03:00
|
|
|
$this->assertFalse($this->getNode()->childExists('555'));
|
2015-12-03 13:26:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Sabre\DAV\Exception\BadRequest
|
|
|
|
*/
|
|
|
|
public function testChildExistsInvalidId() {
|
|
|
|
$this->tagMapper->expects($this->once())
|
|
|
|
->method('haveTag')
|
2015-12-04 19:30:22 +03:00
|
|
|
->with([111], 'files', '555')
|
2015-12-03 13:26:16 +03:00
|
|
|
->will($this->throwException(new \InvalidArgumentException()));
|
|
|
|
|
2016-01-20 13:47:23 +03:00
|
|
|
$this->getNode()->childExists('555');
|
2015-12-03 13:26:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Sabre\DAV\Exception\Forbidden
|
|
|
|
*/
|
|
|
|
public function testDelete() {
|
2016-01-20 13:47:23 +03:00
|
|
|
$this->getNode()->delete();
|
2015-12-03 13:26:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Sabre\DAV\Exception\Forbidden
|
|
|
|
*/
|
|
|
|
public function testSetName() {
|
2016-01-20 13:47:23 +03:00
|
|
|
$this->getNode()->setName('somethingelse');
|
2015-12-03 13:26:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetName() {
|
2016-01-20 13:47:23 +03:00
|
|
|
$this->assertEquals('111', $this->getNode()->getName());
|
2015-12-03 13:26:16 +03:00
|
|
|
}
|
|
|
|
}
|