2015-12-03 13:26:16 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-01-12 17:02:16 +03:00
|
|
|
*
|
2015-12-03 13:26:16 +03:00
|
|
|
*/
|
|
|
|
|
2016-05-25 17:04:15 +03:00
|
|
|
namespace OCA\DAV\Tests\unit\SystemTag;
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
use OC\SystemTag\SystemTag;
|
2017-10-25 01:03:28 +03:00
|
|
|
use OCA\DAV\SystemTag\SystemTagNode;
|
|
|
|
use OCA\DAV\SystemTag\SystemTagsByIdCollection;
|
|
|
|
use OCA\DAV\SystemTag\SystemTagsObjectMappingCollection;
|
2016-02-19 21:45:03 +03:00
|
|
|
use OCP\IGroupManager;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\IUser;
|
2016-02-19 21:45:03 +03:00
|
|
|
use OCP\IUserSession;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\SystemTag\ISystemTag;
|
2017-10-25 01:03:28 +03:00
|
|
|
use OCP\SystemTag\ISystemTagManager;
|
2015-12-03 13:26:16 +03:00
|
|
|
use OCP\SystemTag\TagAlreadyExistsException;
|
2017-10-24 16:26:53 +03:00
|
|
|
use Sabre\DAV\Tree;
|
|
|
|
use Sabre\HTTP\RequestInterface;
|
|
|
|
use Sabre\HTTP\ResponseInterface;
|
2015-12-03 13:26:16 +03:00
|
|
|
|
2016-05-25 17:04:15 +03:00
|
|
|
class SystemTagPluginTest extends \Test\TestCase {
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
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;
|
2016-05-13 12:07:21 +03:00
|
|
|
const CANASSIGN_PROPERTYNAME = \OCA\DAV\SystemTag\SystemTagPlugin::CANASSIGN_PROPERTYNAME;
|
|
|
|
const GROUPS_PROPERTYNAME = \OCA\DAV\SystemTag\SystemTagPlugin::GROUPS_PROPERTYNAME;
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Sabre\DAV\Server
|
|
|
|
*/
|
|
|
|
private $server;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Sabre\DAV\Tree
|
|
|
|
*/
|
|
|
|
private $tree;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OCP\SystemTag\ISystemTagManager
|
|
|
|
*/
|
|
|
|
private $tagManager;
|
|
|
|
|
2016-02-19 21:45:03 +03:00
|
|
|
/**
|
|
|
|
* @var IGroupManager
|
|
|
|
*/
|
|
|
|
private $groupManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var IUserSession
|
|
|
|
*/
|
|
|
|
private $userSession;
|
|
|
|
|
2016-05-13 11:22:14 +03:00
|
|
|
/**
|
|
|
|
* @var IUser
|
|
|
|
*/
|
|
|
|
private $user;
|
|
|
|
|
2015-12-03 13:26:16 +03:00
|
|
|
/**
|
2016-01-11 17:15:21 +03:00
|
|
|
* @var \OCA\DAV\SystemTag\SystemTagPlugin
|
2015-12-03 13:26:16 +03:00
|
|
|
*/
|
|
|
|
private $plugin;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2015-12-03 13:26:16 +03:00
|
|
|
parent::setUp();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->tree = $this->getMockBuilder(Tree::class)
|
2015-12-03 13:26:16 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->server = new \Sabre\DAV\Server($this->tree);
|
|
|
|
|
2017-10-25 01:03:28 +03:00
|
|
|
$this->tagManager = $this->getMockBuilder(ISystemTagManager::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->groupManager = $this->getMockBuilder(IGroupManager::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->user = $this->getMockBuilder(IUser::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->userSession = $this->getMockBuilder(IUserSession::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->getMock();
|
2016-05-13 11:22:14 +03:00
|
|
|
$this->userSession
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getUser')
|
|
|
|
->willReturn($this->user);
|
|
|
|
$this->userSession
|
|
|
|
->expects($this->any())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->willReturn(true);
|
2015-12-03 13:26:16 +03:00
|
|
|
|
2016-02-19 21:45:03 +03:00
|
|
|
$this->plugin = new \OCA\DAV\SystemTag\SystemTagPlugin(
|
|
|
|
$this->tagManager,
|
|
|
|
$this->groupManager,
|
|
|
|
$this->userSession
|
|
|
|
);
|
2015-12-03 13:26:16 +03:00
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
}
|
|
|
|
|
2016-05-13 12:07:21 +03:00
|
|
|
public function getPropertiesDataProvider() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
new SystemTag(1, 'Test', true, true),
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
self::ID_PROPERTYNAME,
|
|
|
|
self::DISPLAYNAME_PROPERTYNAME,
|
|
|
|
self::USERVISIBLE_PROPERTYNAME,
|
|
|
|
self::USERASSIGNABLE_PROPERTYNAME,
|
|
|
|
self::CANASSIGN_PROPERTYNAME,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
self::ID_PROPERTYNAME => '1',
|
|
|
|
self::DISPLAYNAME_PROPERTYNAME => 'Test',
|
|
|
|
self::USERVISIBLE_PROPERTYNAME => 'true',
|
|
|
|
self::USERASSIGNABLE_PROPERTYNAME => 'true',
|
|
|
|
self::CANASSIGN_PROPERTYNAME => 'true',
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
new SystemTag(1, 'Test', true, false),
|
|
|
|
[],
|
|
|
|
[
|
|
|
|
self::ID_PROPERTYNAME,
|
|
|
|
self::DISPLAYNAME_PROPERTYNAME,
|
|
|
|
self::USERVISIBLE_PROPERTYNAME,
|
|
|
|
self::USERASSIGNABLE_PROPERTYNAME,
|
|
|
|
self::CANASSIGN_PROPERTYNAME,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
self::ID_PROPERTYNAME => '1',
|
|
|
|
self::DISPLAYNAME_PROPERTYNAME => 'Test',
|
|
|
|
self::USERVISIBLE_PROPERTYNAME => 'true',
|
|
|
|
self::USERASSIGNABLE_PROPERTYNAME => 'false',
|
|
|
|
self::CANASSIGN_PROPERTYNAME => 'false',
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
new SystemTag(1, 'Test', true, false),
|
|
|
|
['group1', 'group2'],
|
|
|
|
[
|
|
|
|
self::ID_PROPERTYNAME,
|
|
|
|
self::GROUPS_PROPERTYNAME,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
self::ID_PROPERTYNAME => '1',
|
|
|
|
self::GROUPS_PROPERTYNAME => 'group1|group2',
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
new SystemTag(1, 'Test', true, true),
|
|
|
|
['group1', 'group2'],
|
|
|
|
[
|
|
|
|
self::ID_PROPERTYNAME,
|
|
|
|
self::GROUPS_PROPERTYNAME,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
self::ID_PROPERTYNAME => '1',
|
|
|
|
// groups only returned when userAssignable is false
|
|
|
|
self::GROUPS_PROPERTYNAME => '',
|
|
|
|
]
|
|
|
|
],
|
2015-12-03 13:26:16 +03:00
|
|
|
];
|
2016-05-13 12:07:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getPropertiesDataProvider
|
|
|
|
*/
|
|
|
|
public function testGetProperties(ISystemTag $systemTag, $groups, $requestedProperties, $expectedProperties) {
|
|
|
|
$this->user->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('admin');
|
|
|
|
$this->groupManager
|
|
|
|
->expects($this->any())
|
|
|
|
->method('isAdmin')
|
|
|
|
->with('admin')
|
|
|
|
->willReturn(true);
|
2015-12-03 13:26:16 +03:00
|
|
|
|
2017-10-25 01:03:28 +03:00
|
|
|
$node = $this->getMockBuilder(SystemTagNode::class)
|
2015-12-03 13:26:16 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$node->expects($this->any())
|
|
|
|
->method('getSystemTag')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($systemTag);
|
2015-12-03 13:26:16 +03:00
|
|
|
|
2016-05-13 12:07:21 +03:00
|
|
|
$this->tagManager->expects($this->any())
|
|
|
|
->method('canUserAssignTag')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($systemTag->isUserAssignable());
|
2016-05-13 12:07:21 +03:00
|
|
|
|
|
|
|
$this->tagManager->expects($this->any())
|
|
|
|
->method('getTagGroups')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($groups);
|
2016-05-13 12:07:21 +03:00
|
|
|
|
2015-12-03 13:26:16 +03:00
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/systemtag/1')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($node);
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
$propFind = new \Sabre\DAV\PropFind(
|
|
|
|
'/systemtag/1',
|
|
|
|
$requestedProperties,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->plugin->handleGetProperties(
|
|
|
|
$propFind,
|
|
|
|
$node
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $propFind->getResultForMultiStatus();
|
|
|
|
|
|
|
|
$this->assertEmpty($result[404]);
|
2016-05-13 12:07:21 +03:00
|
|
|
$this->assertEquals($expectedProperties, $result[200]);
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2016-05-13 12:07:21 +03:00
|
|
|
public function testGetPropertiesForbidden() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Sabre\DAV\Exception\Forbidden::class);
|
|
|
|
|
2016-05-13 12:07:21 +03:00
|
|
|
$systemTag = new SystemTag(1, 'Test', true, false);
|
|
|
|
$requestedProperties = [
|
|
|
|
self::ID_PROPERTYNAME,
|
|
|
|
self::GROUPS_PROPERTYNAME,
|
|
|
|
];
|
|
|
|
$this->user->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('admin');
|
|
|
|
$this->groupManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('isAdmin')
|
|
|
|
->with('admin')
|
|
|
|
->willReturn(false);
|
|
|
|
|
2017-10-25 01:03:28 +03:00
|
|
|
$node = $this->getMockBuilder(SystemTagNode::class)
|
2016-05-13 12:07:21 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$node->expects($this->any())
|
|
|
|
->method('getSystemTag')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($systemTag);
|
2016-05-13 12:07:21 +03:00
|
|
|
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/systemtag/1')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($node);
|
2016-05-13 12:07:21 +03:00
|
|
|
|
|
|
|
$propFind = new \Sabre\DAV\PropFind(
|
|
|
|
'/systemtag/1',
|
|
|
|
$requestedProperties,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->plugin->handleGetProperties(
|
|
|
|
$propFind,
|
|
|
|
$node
|
|
|
|
);
|
2015-12-03 13:26:16 +03:00
|
|
|
}
|
|
|
|
|
2016-05-13 12:07:21 +03:00
|
|
|
public function testUpdatePropertiesAdmin() {
|
2015-12-03 13:26:16 +03:00
|
|
|
$systemTag = new SystemTag(1, 'Test', true, false);
|
2016-05-13 12:07:21 +03:00
|
|
|
$this->user->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('admin');
|
|
|
|
$this->groupManager
|
|
|
|
->expects($this->any())
|
|
|
|
->method('isAdmin')
|
|
|
|
->with('admin')
|
|
|
|
->willReturn(true);
|
|
|
|
|
2017-10-25 01:03:28 +03:00
|
|
|
$node = $this->getMockBuilder(SystemTagNode::class)
|
2015-12-03 13:26:16 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$node->expects($this->any())
|
|
|
|
->method('getSystemTag')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($systemTag);
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/systemtag/1')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($node);
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
$node->expects($this->once())
|
|
|
|
->method('update')
|
|
|
|
->with('Test changed', false, true);
|
|
|
|
|
2016-05-13 12:07:21 +03:00
|
|
|
$this->tagManager->expects($this->once())
|
|
|
|
->method('setTagGroups')
|
|
|
|
->with($systemTag, ['group1', 'group2']);
|
|
|
|
|
2015-12-03 13:26:16 +03:00
|
|
|
// properties to set
|
2020-03-26 11:30:18 +03:00
|
|
|
$propPatch = new \Sabre\DAV\PropPatch([
|
2015-12-03 13:26:16 +03:00
|
|
|
self::DISPLAYNAME_PROPERTYNAME => 'Test changed',
|
2016-01-27 12:52:00 +03:00
|
|
|
self::USERVISIBLE_PROPERTYNAME => 'false',
|
|
|
|
self::USERASSIGNABLE_PROPERTYNAME => 'true',
|
2016-05-13 12:07:21 +03:00
|
|
|
self::GROUPS_PROPERTYNAME => 'group1|group2',
|
2020-03-26 11:30:18 +03:00
|
|
|
]);
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
$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]);
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2016-05-13 12:07:21 +03:00
|
|
|
public function testUpdatePropertiesForbidden() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Sabre\DAV\Exception\Forbidden::class);
|
|
|
|
|
2016-05-13 12:07:21 +03:00
|
|
|
$systemTag = new SystemTag(1, 'Test', true, false);
|
|
|
|
$this->user->expects($this->any())
|
2016-02-19 21:45:03 +03:00
|
|
|
->method('getUID')
|
|
|
|
->willReturn('admin');
|
|
|
|
$this->groupManager
|
2016-05-13 12:07:21 +03:00
|
|
|
->expects($this->any())
|
2016-02-19 21:45:03 +03:00
|
|
|
->method('isAdmin')
|
|
|
|
->with('admin')
|
|
|
|
->willReturn(false);
|
|
|
|
|
2017-10-25 01:03:28 +03:00
|
|
|
$node = $this->getMockBuilder(SystemTagNode::class)
|
2016-02-19 21:45:03 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-05-13 12:07:21 +03:00
|
|
|
$node->expects($this->any())
|
|
|
|
->method('getSystemTag')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($systemTag);
|
2016-02-19 21:45:03 +03:00
|
|
|
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
2016-05-13 12:07:21 +03:00
|
|
|
->with('/systemtag/1')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($node);
|
2016-02-19 21:45:03 +03:00
|
|
|
|
2016-05-13 12:07:21 +03:00
|
|
|
$node->expects($this->never())
|
|
|
|
->method('update');
|
2016-02-19 21:45:03 +03:00
|
|
|
|
2016-05-13 12:07:21 +03:00
|
|
|
$this->tagManager->expects($this->never())
|
|
|
|
->method('setTagGroups');
|
2016-02-19 21:45:03 +03:00
|
|
|
|
2016-05-13 12:07:21 +03:00
|
|
|
// properties to set
|
2020-03-26 11:30:18 +03:00
|
|
|
$propPatch = new \Sabre\DAV\PropPatch([
|
2016-05-13 12:07:21 +03:00
|
|
|
self::GROUPS_PROPERTYNAME => 'group1|group2',
|
2020-03-26 11:30:18 +03:00
|
|
|
]);
|
2016-02-19 21:45:03 +03:00
|
|
|
|
2016-05-13 12:07:21 +03:00
|
|
|
$this->plugin->handleUpdateProperties(
|
|
|
|
'/systemtag/1',
|
|
|
|
$propPatch
|
|
|
|
);
|
2016-02-19 21:45:03 +03:00
|
|
|
|
2016-05-13 12:07:21 +03:00
|
|
|
$propPatch->commit();
|
2016-02-19 21:45:03 +03:00
|
|
|
}
|
|
|
|
|
2016-05-13 12:07:21 +03:00
|
|
|
public function createTagInsufficientPermissionsProvider() {
|
|
|
|
return [
|
|
|
|
[true, false, ''],
|
|
|
|
[false, true, ''],
|
|
|
|
[true, true, 'group1|group2'],
|
|
|
|
];
|
|
|
|
}
|
2016-02-19 21:45:03 +03:00
|
|
|
/**
|
2016-05-13 12:07:21 +03:00
|
|
|
* @dataProvider createTagInsufficientPermissionsProvider
|
2016-02-19 21:45:03 +03:00
|
|
|
*/
|
2016-05-13 12:07:21 +03:00
|
|
|
public function testCreateNotAssignableTagAsRegularUser($userVisible, $userAssignable, $groups) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Sabre\DAV\Exception\BadRequest::class);
|
|
|
|
$this->expectExceptionMessage('Not sufficient permissions');
|
|
|
|
|
2016-05-13 11:22:14 +03:00
|
|
|
$this->user->expects($this->once())
|
2016-02-19 21:45:03 +03:00
|
|
|
->method('getUID')
|
|
|
|
->willReturn('admin');
|
|
|
|
$this->groupManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('isAdmin')
|
|
|
|
->with('admin')
|
|
|
|
->willReturn(false);
|
|
|
|
|
2016-05-13 12:07:21 +03:00
|
|
|
$requestData = [
|
2016-02-19 21:45:03 +03:00
|
|
|
'name' => 'Test',
|
2016-05-13 12:07:21 +03:00
|
|
|
'userVisible' => $userVisible,
|
|
|
|
'userAssignable' => $userAssignable,
|
|
|
|
];
|
|
|
|
if (!empty($groups)) {
|
|
|
|
$requestData['groups'] = $groups;
|
|
|
|
}
|
|
|
|
$requestData = json_encode($requestData);
|
2016-02-19 21:45:03 +03:00
|
|
|
|
2017-10-25 01:03:28 +03:00
|
|
|
$node = $this->getMockBuilder(SystemTagsByIdCollection::class)
|
2016-02-19 21:45:03 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$this->tagManager->expects($this->never())
|
|
|
|
->method('createTag');
|
2016-05-13 12:07:21 +03:00
|
|
|
$this->tagManager->expects($this->never())
|
|
|
|
->method('setTagGroups');
|
2016-02-19 21:45:03 +03:00
|
|
|
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/systemtags')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($node);
|
2016-02-19 21:45:03 +03:00
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$request = $this->getMockBuilder(RequestInterface::class)
|
2016-02-19 21:45:03 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$response = $this->getMockBuilder(ResponseInterface::class)
|
2016-02-19 21:45:03 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getPath')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('/systemtags');
|
2016-02-19 21:45:03 +03:00
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getBodyAsString')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($requestData);
|
2016-02-19 21:45:03 +03:00
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getHeader')
|
|
|
|
->with('Content-Type')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('application/json');
|
2016-02-19 21:45:03 +03:00
|
|
|
|
|
|
|
$this->plugin->httpPost($request, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateTagInByIdCollectionAsRegularUser() {
|
|
|
|
$systemTag = new SystemTag(1, 'Test', true, false);
|
|
|
|
|
|
|
|
$requestData = json_encode([
|
|
|
|
'name' => 'Test',
|
|
|
|
'userVisible' => true,
|
|
|
|
'userAssignable' => true,
|
|
|
|
]);
|
|
|
|
|
2017-10-25 01:03:28 +03:00
|
|
|
$node = $this->getMockBuilder(SystemTagsByIdCollection::class)
|
2016-02-19 21:45:03 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$this->tagManager->expects($this->once())
|
|
|
|
->method('createTag')
|
|
|
|
->with('Test', true, true)
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($systemTag);
|
2016-02-19 21:45:03 +03:00
|
|
|
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/systemtags')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($node);
|
2016-02-19 21:45:03 +03:00
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$request = $this->getMockBuilder(RequestInterface::class)
|
2016-02-19 21:45:03 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$response = $this->getMockBuilder(ResponseInterface::class)
|
2016-02-19 21:45:03 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getPath')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('/systemtags');
|
2016-02-19 21:45:03 +03:00
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getBodyAsString')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($requestData);
|
2016-02-19 21:45:03 +03:00
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getHeader')
|
|
|
|
->with('Content-Type')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('application/json');
|
2016-02-19 21:45:03 +03:00
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getUrl')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('http://example.com/dav/systemtags');
|
2016-02-19 21:45:03 +03:00
|
|
|
|
|
|
|
$response->expects($this->once())
|
|
|
|
->method('setHeader')
|
|
|
|
->with('Content-Location', 'http://example.com/dav/systemtags/1');
|
|
|
|
|
|
|
|
$this->plugin->httpPost($request, $response);
|
|
|
|
}
|
|
|
|
|
2016-05-13 12:07:21 +03:00
|
|
|
public function createTagProvider() {
|
|
|
|
return [
|
|
|
|
[true, false, ''],
|
|
|
|
[false, false, ''],
|
|
|
|
[true, false, 'group1|group2'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider createTagProvider
|
|
|
|
*/
|
|
|
|
public function testCreateTagInByIdCollection($userVisible, $userAssignable, $groups) {
|
2016-05-13 11:22:14 +03:00
|
|
|
$this->user->expects($this->once())
|
2016-02-19 21:45:03 +03:00
|
|
|
->method('getUID')
|
|
|
|
->willReturn('admin');
|
|
|
|
$this->groupManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('isAdmin')
|
|
|
|
->with('admin')
|
|
|
|
->willReturn(true);
|
|
|
|
|
2015-12-03 13:26:16 +03:00
|
|
|
$systemTag = new SystemTag(1, 'Test', true, false);
|
|
|
|
|
2016-05-13 12:07:21 +03:00
|
|
|
$requestData = [
|
2015-12-03 13:26:16 +03:00
|
|
|
'name' => 'Test',
|
2016-05-13 12:07:21 +03:00
|
|
|
'userVisible' => $userVisible,
|
|
|
|
'userAssignable' => $userAssignable,
|
|
|
|
];
|
|
|
|
if (!empty($groups)) {
|
|
|
|
$requestData['groups'] = $groups;
|
|
|
|
}
|
|
|
|
$requestData = json_encode($requestData);
|
2015-12-03 13:26:16 +03:00
|
|
|
|
2017-10-25 01:03:28 +03:00
|
|
|
$node = $this->getMockBuilder(SystemTagsByIdCollection::class)
|
2015-12-03 13:26:16 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$this->tagManager->expects($this->once())
|
|
|
|
->method('createTag')
|
2016-05-13 12:07:21 +03:00
|
|
|
->with('Test', $userVisible, $userAssignable)
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($systemTag);
|
2016-05-13 12:07:21 +03:00
|
|
|
|
|
|
|
if (!empty($groups)) {
|
|
|
|
$this->tagManager->expects($this->once())
|
|
|
|
->method('setTagGroups')
|
|
|
|
->with($systemTag, explode('|', $groups))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($systemTag);
|
2016-05-13 12:07:21 +03:00
|
|
|
} else {
|
|
|
|
$this->tagManager->expects($this->never())
|
|
|
|
->method('setTagGroups');
|
|
|
|
}
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/systemtags')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($node);
|
2015-12-03 13:26:16 +03:00
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$request = $this->getMockBuilder(RequestInterface::class)
|
2015-12-03 13:26:16 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$response = $this->getMockBuilder(ResponseInterface::class)
|
2015-12-03 13:26:16 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getPath')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('/systemtags');
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getBodyAsString')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($requestData);
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getHeader')
|
|
|
|
->with('Content-Type')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('application/json');
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getUrl')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('http://example.com/dav/systemtags');
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
$response->expects($this->once())
|
|
|
|
->method('setHeader')
|
2015-11-30 17:40:17 +03:00
|
|
|
->with('Content-Location', 'http://example.com/dav/systemtags/1');
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
$this->plugin->httpPost($request, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function nodeClassProvider() {
|
|
|
|
return [
|
|
|
|
['\OCA\DAV\SystemTag\SystemTagsByIdCollection'],
|
|
|
|
['\OCA\DAV\SystemTag\SystemTagsObjectMappingCollection'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateTagInMappingCollection() {
|
2016-05-13 11:22:14 +03:00
|
|
|
$this->user->expects($this->once())
|
2016-02-19 21:45:03 +03:00
|
|
|
->method('getUID')
|
|
|
|
->willReturn('admin');
|
|
|
|
$this->groupManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('isAdmin')
|
|
|
|
->with('admin')
|
|
|
|
->willReturn(true);
|
|
|
|
|
2015-12-03 13:26:16 +03:00
|
|
|
$systemTag = new SystemTag(1, 'Test', true, false);
|
|
|
|
|
|
|
|
$requestData = json_encode([
|
|
|
|
'name' => 'Test',
|
|
|
|
'userVisible' => true,
|
|
|
|
'userAssignable' => false,
|
|
|
|
]);
|
|
|
|
|
2017-10-25 01:03:28 +03:00
|
|
|
$node = $this->getMockBuilder(SystemTagsObjectMappingCollection::class)
|
2015-12-03 13:26:16 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->tagManager->expects($this->once())
|
|
|
|
->method('createTag')
|
|
|
|
->with('Test', true, false)
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($systemTag);
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/systemtags-relations/files/12')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($node);
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
$node->expects($this->once())
|
|
|
|
->method('createFile')
|
|
|
|
->with(1);
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$request = $this->getMockBuilder(RequestInterface::class)
|
2015-12-03 13:26:16 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$response = $this->getMockBuilder(ResponseInterface::class)
|
2015-12-03 13:26:16 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getPath')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('/systemtags-relations/files/12');
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getBodyAsString')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($requestData);
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getHeader')
|
|
|
|
->with('Content-Type')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('application/json');
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getBaseUrl')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('http://example.com/dav/');
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
$response->expects($this->once())
|
|
|
|
->method('setHeader')
|
2015-11-30 17:40:17 +03:00
|
|
|
->with('Content-Location', 'http://example.com/dav/systemtags/1');
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
$this->plugin->httpPost($request, $response);
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2016-02-01 20:18:17 +03:00
|
|
|
public function testCreateTagToUnknownNode() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Sabre\DAV\Exception\NotFound::class);
|
|
|
|
|
2017-10-25 01:03:28 +03:00
|
|
|
$node = $this->getMockBuilder(SystemTagsObjectMappingCollection::class)
|
2016-02-01 20:18:17 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->will($this->throwException(new \Sabre\DAV\Exception\NotFound()));
|
|
|
|
|
|
|
|
$this->tagManager->expects($this->never())
|
|
|
|
->method('createTag');
|
|
|
|
|
|
|
|
$node->expects($this->never())
|
|
|
|
->method('createFile');
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$request = $this->getMockBuilder(RequestInterface::class)
|
2016-02-01 20:18:17 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$response = $this->getMockBuilder(ResponseInterface::class)
|
2016-02-01 20:18:17 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getPath')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('/systemtags-relations/files/12');
|
2016-02-01 20:18:17 +03:00
|
|
|
|
|
|
|
$this->plugin->httpPost($request, $response);
|
|
|
|
}
|
|
|
|
|
2015-12-03 13:26:16 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider nodeClassProvider
|
|
|
|
*/
|
|
|
|
public function testCreateTagConflict($nodeClass) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Sabre\DAV\Exception\Conflict::class);
|
|
|
|
|
2016-05-13 11:22:14 +03:00
|
|
|
$this->user->expects($this->once())
|
2016-02-19 21:45:03 +03:00
|
|
|
->method('getUID')
|
|
|
|
->willReturn('admin');
|
|
|
|
$this->groupManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('isAdmin')
|
|
|
|
->with('admin')
|
|
|
|
->willReturn(true);
|
|
|
|
|
2015-12-03 13:26:16 +03:00
|
|
|
$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')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($node);
|
2015-12-03 13:26:16 +03:00
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$request = $this->getMockBuilder(RequestInterface::class)
|
2015-12-03 13:26:16 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$response = $this->getMockBuilder(ResponseInterface::class)
|
2015-12-03 13:26:16 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getPath')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('/systemtags');
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getBodyAsString')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($requestData);
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getHeader')
|
|
|
|
->with('Content-Type')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('application/json');
|
2015-12-03 13:26:16 +03:00
|
|
|
|
|
|
|
$this->plugin->httpPost($request, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|