2015-03-21 22:03:56 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, 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/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Test;
|
|
|
|
|
2016-08-15 21:22:48 +03:00
|
|
|
use OC\CapabilitiesManager;
|
|
|
|
use OCP\AppFramework\QueryException;
|
|
|
|
use OCP\Capabilities\ICapability;
|
2017-06-30 12:21:15 +03:00
|
|
|
use OCP\Capabilities\IPublicCapability;
|
2021-04-19 15:06:34 +03:00
|
|
|
use Psr\Log\LoggerInterface;
|
2016-08-15 21:22:48 +03:00
|
|
|
|
2015-03-21 22:03:56 +03:00
|
|
|
class CapabilitiesManagerTest extends TestCase {
|
|
|
|
|
2016-08-15 21:22:48 +03:00
|
|
|
/** @var CapabilitiesManager */
|
|
|
|
private $manager;
|
|
|
|
|
2021-04-19 15:06:34 +03:00
|
|
|
/** @var LoggerInterface */
|
2016-08-15 21:22:48 +03:00
|
|
|
private $logger;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2017-06-30 12:21:15 +03:00
|
|
|
parent::setUp();
|
2021-04-19 15:06:34 +03:00
|
|
|
$this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
|
2016-08-15 21:22:48 +03:00
|
|
|
$this->manager = new CapabilitiesManager($this->logger);
|
|
|
|
}
|
|
|
|
|
2015-03-21 22:03:56 +03:00
|
|
|
/**
|
|
|
|
* Test no capabilities
|
|
|
|
*/
|
|
|
|
public function testNoCapabilities() {
|
2016-08-15 21:22:48 +03:00
|
|
|
$res = $this->manager->getCapabilities();
|
2015-03-21 22:03:56 +03:00
|
|
|
$this->assertEmpty($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test a valid capabilitie
|
|
|
|
*/
|
|
|
|
public function testValidCapability() {
|
2020-04-09 14:53:40 +03:00
|
|
|
$this->manager->registerCapability(function () {
|
2015-03-21 22:03:56 +03:00
|
|
|
return new SimpleCapability();
|
|
|
|
});
|
|
|
|
|
2016-08-15 21:22:48 +03:00
|
|
|
$res = $this->manager->getCapabilities();
|
2015-03-21 22:03:56 +03:00
|
|
|
$this->assertEquals(['foo' => 1], $res);
|
|
|
|
}
|
|
|
|
|
2017-06-30 12:21:15 +03:00
|
|
|
/**
|
|
|
|
* Test a public capabilitie
|
|
|
|
*/
|
|
|
|
public function testPublicCapability() {
|
2020-04-09 14:53:40 +03:00
|
|
|
$this->manager->registerCapability(function () {
|
2017-06-30 12:21:15 +03:00
|
|
|
return new PublicSimpleCapability1();
|
|
|
|
});
|
2020-04-09 14:53:40 +03:00
|
|
|
$this->manager->registerCapability(function () {
|
2017-06-30 12:21:15 +03:00
|
|
|
return new SimpleCapability2();
|
|
|
|
});
|
2020-04-09 14:53:40 +03:00
|
|
|
$this->manager->registerCapability(function () {
|
2017-06-30 12:21:15 +03:00
|
|
|
return new SimpleCapability3();
|
|
|
|
});
|
|
|
|
|
|
|
|
$res = $this->manager->getCapabilities(true);
|
|
|
|
$this->assertEquals(['foo' => 1], $res);
|
|
|
|
}
|
|
|
|
|
2015-03-21 22:03:56 +03:00
|
|
|
/**
|
|
|
|
* Test that we need something that implents ICapability
|
|
|
|
*/
|
|
|
|
public function testNoICapability() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
$this->expectExceptionMessage('The given Capability (Test\\NoCapability) does not implement the ICapability interface');
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
$this->manager->registerCapability(function () {
|
2015-03-21 22:03:56 +03:00
|
|
|
return new NoCapability();
|
|
|
|
});
|
|
|
|
|
2016-08-15 21:22:48 +03:00
|
|
|
$res = $this->manager->getCapabilities();
|
2015-03-21 22:03:56 +03:00
|
|
|
$this->assertEquals([], $res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test a bunch of merged Capabilities
|
|
|
|
*/
|
|
|
|
public function testMergedCapabilities() {
|
2020-04-09 14:53:40 +03:00
|
|
|
$this->manager->registerCapability(function () {
|
2015-07-22 14:04:56 +03:00
|
|
|
return new SimpleCapability();
|
2015-03-21 22:03:56 +03:00
|
|
|
});
|
2020-04-09 14:53:40 +03:00
|
|
|
$this->manager->registerCapability(function () {
|
2015-07-22 14:04:56 +03:00
|
|
|
return new SimpleCapability2();
|
2015-03-21 22:03:56 +03:00
|
|
|
});
|
2020-04-09 14:53:40 +03:00
|
|
|
$this->manager->registerCapability(function () {
|
2015-07-22 14:04:56 +03:00
|
|
|
return new SimpleCapability3();
|
2015-03-21 22:03:56 +03:00
|
|
|
});
|
|
|
|
|
2016-08-15 21:22:48 +03:00
|
|
|
$res = $this->manager->getCapabilities();
|
2015-03-21 22:03:56 +03:00
|
|
|
$expected = [
|
|
|
|
'foo' => 1,
|
|
|
|
'bar' => [
|
|
|
|
'x' => 1,
|
|
|
|
'y' => 2
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test deep identical capabilities
|
|
|
|
*/
|
|
|
|
public function testDeepIdenticalCapabilities() {
|
2020-04-09 14:53:40 +03:00
|
|
|
$this->manager->registerCapability(function () {
|
2015-03-21 22:03:56 +03:00
|
|
|
return new DeepCapability();
|
|
|
|
});
|
2020-04-09 14:53:40 +03:00
|
|
|
$this->manager->registerCapability(function () {
|
2015-03-21 22:03:56 +03:00
|
|
|
return new DeepCapability();
|
|
|
|
});
|
|
|
|
|
2016-08-15 21:22:48 +03:00
|
|
|
$res = $this->manager->getCapabilities();
|
2015-03-21 22:03:56 +03:00
|
|
|
$expected = [
|
|
|
|
'foo' => [
|
|
|
|
'bar' => [
|
|
|
|
'baz' => true
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
2021-04-19 15:06:34 +03:00
|
|
|
|
2015-03-21 22:03:56 +03:00
|
|
|
$this->assertEquals($expected, $res);
|
|
|
|
}
|
2016-08-15 21:22:48 +03:00
|
|
|
|
|
|
|
public function testInvalidCapability() {
|
|
|
|
$this->manager->registerCapability(function () {
|
|
|
|
throw new QueryException();
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->logger->expects($this->once())
|
2018-01-17 17:21:56 +03:00
|
|
|
->method('logException');
|
2016-08-15 21:22:48 +03:00
|
|
|
|
|
|
|
$res = $this->manager->getCapabilities();
|
|
|
|
|
|
|
|
$this->assertEquals([], $res);
|
|
|
|
}
|
2015-03-21 22:03:56 +03:00
|
|
|
}
|
|
|
|
|
2016-08-15 21:22:48 +03:00
|
|
|
class SimpleCapability implements ICapability {
|
2015-03-21 22:03:56 +03:00
|
|
|
public function getCapabilities() {
|
|
|
|
return [
|
|
|
|
'foo' => 1
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 21:22:48 +03:00
|
|
|
class SimpleCapability2 implements ICapability {
|
2015-03-21 22:03:56 +03:00
|
|
|
public function getCapabilities() {
|
|
|
|
return [
|
|
|
|
'bar' => ['x' => 1]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 21:22:48 +03:00
|
|
|
class SimpleCapability3 implements ICapability {
|
2015-03-21 22:03:56 +03:00
|
|
|
public function getCapabilities() {
|
|
|
|
return [
|
|
|
|
'bar' => ['y' => 2]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 12:21:15 +03:00
|
|
|
class PublicSimpleCapability1 implements IPublicCapability {
|
|
|
|
public function getCapabilities() {
|
|
|
|
return [
|
|
|
|
'foo' => 1
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-22 14:04:56 +03:00
|
|
|
class NoCapability {
|
|
|
|
public function getCapabilities() {
|
|
|
|
return [
|
|
|
|
'baz' => 'z'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 21:22:48 +03:00
|
|
|
class DeepCapability implements ICapability {
|
2015-03-21 22:03:56 +03:00
|
|
|
public function getCapabilities() {
|
|
|
|
return [
|
|
|
|
'foo' => [
|
|
|
|
'bar' => [
|
|
|
|
'baz' => true
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|