*/
class Manager {
- /** @var array */
+ /** @var IProviderFactory */
+ private $factory;
+
+ /** @var IShareProvider[] */
private $providers;
/** @var array */
* @param IMountManager $mountManager
* @param IGroupManager $groupManager
* @param IL10N $l
+ * @param IProviderFactory $factory
*/
public function __construct(
ILogger $logger,
IHasher $hasher,
IMountManager $mountManager,
IGroupManager $groupManager,
- IL10N $l
+ IL10N $l,
+ IProviderFactory $factory
) {
$this->providers = [];
$this->type2provider = [];
$this->mountManager = $mountManager;
$this->groupManager = $groupManager;
$this->l = $l;
+ $this->factory = $factory;
}
/**
- * Register a share provider
- *
- * @param string $id The id of the share provider
- * @param int[] $shareTypes Array containing the share types this provider handles
- * @param callable $callback Callback that must return an IShareProvider instance
+ * @param IShareProvider[] $providers
* @throws ProviderException
*/
- public function registerProvider($id, $shareTypes, callable $callback) {
- // Providers must have an unique id
- if (isset($this->providers[$id])) {
- throw new ProviderException('A share provider with the id \''. $id . '\' is already registered');
- }
+ private function addProviders(array $providers) {
+ foreach ($providers as $provider) {
+ $name = get_class($provider);
- if ($shareTypes === []) {
- throw new ProviderException('shareTypes can\'t be an empty array');
- }
+ if (!($provider instanceof IShareProvider)) {
+ throw new ProviderException($name . ' is not an instance of IShareProvider');
+ }
- foreach($shareTypes as $shareType) {
- // We only have 1 provder per share type
- if (isset($this->type2provider[$shareType])) {
- throw new ProviderException('The share provider ' . $this->type2provider[$shareType] . ' is already registered for share type ' . $shareType);
+ if (isset($this->providers[$name])) {
+ throw new ProviderException($name . ' is already registered');
}
- /*
- * We only allow providers that provider
- * user- / group- / link- / federated-shares
- */
- if ($shareType !== \OCP\Share::SHARE_TYPE_USER &&
- $shareType !== \OCP\Share::SHARE_TYPE_GROUP &&
- $shareType !== \OCP\Share::SHARE_TYPE_LINK &&
- $shareType !== \OCP\Share::SHARE_TYPE_REMOTE) {
- throw new ProviderException('Cannot register provider for share type ' . $shareType);
- //Throw exception
+ $this->providers[$name] = $provider;
+ $types = $provider->shareTypes();
+
+ if ($types === []) {
+ throw new ProviderException('Provider ' . $name . ' must supply share types it can handle');
}
- }
- // Add the provider
- $this->providers[$id] = [
- 'id' => $id,
- 'callback' => $callback,
- 'provider' => null,
- 'shareTypes' => $shareTypes,
- ];
+ foreach ($types as $type) {
+ if (isset($this->type2provider[$type])) {
+ throw new ProviderException($name . ' tried to register for share type ' . $type . ' but this type is already handled by ' . $this->type2provider[$type]);
+ }
- // Update the type mapping
- foreach ($shareTypes as $shareType) {
- $this->type2provider[$shareType] = $id;
+ $this->type2provider[$type] = $name;
+ }
}
}
+ private function runFactory() {
+ if (!empty($this->providers)) {
+ return;
+ }
+
+ $this->addProviders($this->factory->getProviders());
+ $this->factoryDone = true;
+ }
+
/**
* @param string $id
* @return IShareProvider
* @throws ProviderException
*/
private function getProvider($id) {
+ $this->runFactory();
+
if (!isset($this->providers[$id])) {
throw new ProviderException('No provider with id ' . $id . ' found');
}
- if ($this->providers[$id]['provider'] === null) {
- // First time using this provider
- $provider = call_user_func($this->providers[$id]['callback']);
-
- // Make sure a proper provider is returned
- if (!($provider instanceof IShareProvider)) {
- throw new ProviderException('Callback does not return an IShareProvider instance for provider with id ' . $id);
- }
-
- $this->providers[$id]['provider'] = $provider;
- }
-
- return $this->providers[$id]['provider'];
+ return $this->providers[$id];
}
/**
* @throws ProviderException
*/
private function getProviderForType($shareType) {
+ $this->runFactory();
+
if (!isset($this->type2provider[$shareType])) {
throw new ProviderException('No share provider registered for share type ' . $shareType);
}
return $this->getProvider($this->type2provider[$shareType]);
}
+ /**
+ * @return IShareProvider[]
+ */
+ private function getProviders() {
+ $this->runFactory();
+
+ return $this->providers;
+ }
+
/**
* Verify if a password meets all requirements
*
protected function deleteChildren(IShare $share) {
$deletedShares = [];
- $providerIds = array_keys($this->providers);
+ $providers = $this->getProviders();
- foreach($providerIds as $providerId) {
- $provider = $this->getProvider($providerId);
+ foreach($providers as $provider) {
foreach ($provider->getChildren($share) as $child) {
$deletedChildren = $this->deleteChildren($child);
$deletedShares = array_merge($deletedShares, $deletedChildren);
//FIXME ids need to become proper providerid:shareid eventually
- $providerIds = array_keys($this->providers);
+ $providers = $this->getProviders();
$share = null;
- foreach ($providerIds as $providerId) {
- $provider = $this->getProvider($providerId);
-
+ foreach ($providers as $provider) {
try {
$share = $provider->getShareById($id);
$found = true;
*/
namespace Test\Share20;
+use OC\Share20\IProviderFactory;
use OC\Share20\Manager;
use OC\Share20\Exception;
/** @var IL10N */
protected $l;
+ /** @var IProviderFactory */
+ protected $factory;
+
public function setUp() {
$this->logger = $this->getMock('\OCP\ILogger');
$this->config = $this->getMock('\OCP\IConfig');
- $this->defaultProvider = $this->getMock('\OC\Share20\IShareProvider');
+ $this->defaultProvider = $this->getMockBuilder('\OC\Share20\IShareProvider')
+ ->setMockClassName('DefaultShareProvider')
+ ->getMock();
$this->secureRandom = $this->getMock('\OCP\Security\ISecureRandom');
$this->hasher = $this->getMock('\OCP\Security\IHasher');
$this->mountManager = $this->getMock('\OCP\Files\Mount\IMountManager');
return vsprintf($text, $parameters);
}));
+ $this->factory = $this->getMock('\OC\Share20\IProviderFactory');
+
$this->manager = new Manager(
$this->logger,
$this->config,
$this->hasher,
$this->mountManager,
$this->groupManager,
- $this->l
+ $this->l,
+ $this->factory
);
- $this->manager->registerProvider('tmp',
- [
- \OCP\SHARE::SHARE_TYPE_USER,
+
+ $this->defaultProvider
+ ->method('shareTypes')
+ ->willReturn([
+ \OCP\Share::SHARE_TYPE_USER,
\OCP\Share::SHARE_TYPE_GROUP,
- \OCP\Share::SHARE_TYPE_LINK
- ],
- function() {
- return $this->defaultProvider;
- }
- );
+ \OCP\Share::SHARE_TYPE_LINK,
+ ]);
+ }
+
+ /**
+ * @return \PHPUnit_Framework_MockObject_MockBuilder
+ */
+ private function createManagerMock() {
+ return $this->getMockBuilder('\OC\Share20\Manager')
+ ->setConstructorArgs([
+ $this->logger,
+ $this->config,
+ $this->secureRandom,
+ $this->hasher,
+ $this->mountManager,
+ $this->groupManager,
+ $this->l,
+ $this->factory
+ ]);
+ }
+
+ /**
+ * Initialise the factory. If $providers is null add the defaultProvider
+ * @param array $providers
+ */
+ private function factoryProviders(array $providers = null) {
+ if ($providers === null) {
+ $this->factory->method('getProviders')->willReturn([$this->defaultProvider]);
+ } else {
+ $this->factory->method('getProviders')->willReturn($providers);
+ }
+ }
+
+ /**
+ * @param int[] $shareTypes Share types for this provider
+ * @param string|null $name The name of this provider
+ * @return \PHPUnit_Framework_MockObject_MockObject|IShareProvider
+ */
+ private function createShareProvider(array $shareTypes, $name = null) {
+ $provider = $this->getMockBuilder('\OC\Share20\IShareProvider');
+
+ if ($name !== null) {
+ $provider->setMockClassName($name);
+ }
+
+ $provider = $provider->getMock();
+
+ $provider->method('shareTypes')->willReturn($shareTypes);
+
+ return $provider;
}
/**
* @expectedException \OC\Share20\Exception\ShareNotFound
*/
public function testDeleteNoShareId() {
+ $this->factoryProviders();
+
$share = $this->getMock('\OC\Share20\IShare');
$share
* @dataProvider dataTestDelete
*/
public function testDelete($shareType, $sharedWith, $sharedWith_string) {
- $manager = $this->getMockBuilder('\OC\Share20\Manager')
- ->setConstructorArgs([
- $this->logger,
- $this->config,
- $this->secureRandom,
- $this->hasher,
- $this->mountManager,
- $this->groupManager,
- $this->l
- ])
+ $manager = $this->createManagerMock()
->setMethods(['getShareById', 'deleteChildren'])
->getMock();
- $manager->registerProvider('tmp',
- [$shareType],
- function() {
- return $this->defaultProvider;
- }
- );
+ $provider = $this->createShareProvider([
+ \OCP\Share::SHARE_TYPE_USER,
+ \OCP\Share::SHARE_TYPE_GROUP,
+ \OCP\Share::SHARE_TYPE_LINK,
+ \OCP\Share::SHARE_TYPE_REMOTE,
+ ]);
+ $this->factoryProviders([$provider]);
$sharedBy = $this->getMock('\OCP\IUser');
$sharedBy->method('getUID')->willReturn('sharedBy');
$manager->expects($this->once())->method('getShareById')->with(42)->willReturn($share);
$manager->expects($this->once())->method('deleteChildren')->with($share);
- $this->defaultProvider
+ $provider
->expects($this->once())
->method('delete')
->with($share);
}
public function testDeleteNested() {
- $manager = $this->getMockBuilder('\OC\Share20\Manager')
- ->setConstructorArgs([
- $this->logger,
- $this->config,
- $this->secureRandom,
- $this->hasher,
- $this->mountManager,
- $this->groupManager,
- $this->l,
- ])
+ $manager = $this->createManagerMock()
->setMethods(['getShareById'])
->getMock();
- $manager->registerProvider('tmp',
- [
- \OCP\Share::SHARE_TYPE_USER
- ],
- function() {
- return $this->defaultProvider;
- }
- );
+ $provider = $this->createShareProvider([\OCP\Share::SHARE_TYPE_USER]);
+ $this->factoryProviders([$provider]);
$sharedBy1 = $this->getMock('\OCP\IUser');
$sharedBy1->method('getUID')->willReturn('sharedBy1');
$manager->expects($this->once())->method('getShareById')->with(42)->willReturn($share1);
- $this->defaultProvider
+ $provider
->method('getChildren')
->will($this->returnValueMap([
[$share1, [$share2]],
}
public function testDeleteChildren() {
- $manager = $this->getMockBuilder('\OC\Share20\Manager')
- ->setConstructorArgs([
- $this->logger,
- $this->config,
- $this->secureRandom,
- $this->hasher,
- $this->mountManager,
- $this->groupManager,
- $this->l,
- ])
+ $manager = $this->createManagerMock()
->setMethods(['deleteShare'])
->getMock();
- $manager->registerProvider('tmp',
- [\OCP\Share::SHARE_TYPE_USER],
- function() {
- return $this->defaultProvider;
- }
- );
+ $provider = $this->createShareProvider([\OCP\Share::SHARE_TYPE_USER]);
+ $this->factoryProviders([$provider]);
$share = $this->getMock('\OC\Share20\IShare');
$child3,
];
- $this->defaultProvider
+ $provider
->expects($this->exactly(4))
->method('getChildren')
->will($this->returnCallback(function($_share) use ($share, $shares) {
return [];
}));
- $this->defaultProvider
+ $provider
->expects($this->exactly(3))
->method('delete')
->withConsecutive($child1, $child2, $child3);
}
public function testGetShareById() {
+ $this->factoryProviders();
+
$share = $this->getMock('\OC\Share20\IShare');
$this->defaultProvider
->with($path)
->willReturn([]);
+ $this->factoryProviders();
+
$this->invokePrivate($this->manager, 'userCreateChecks', [$share]);
}
->with($path)
->willReturn([$share]);
+ $this->factoryProviders();
+
$this->invokePrivate($this->manager, 'userCreateChecks', [$share]);
}
->with($path)
->willReturn([$share2]);
+ $this->factoryProviders();
+
$this->invokePrivate($this->manager, 'userCreateChecks', [$share]);
}
->with($path)
->willReturn([$share2]);
+ $this->factoryProviders();
+
$this->invokePrivate($this->manager, 'userCreateChecks', [$share]);
}
-
/**
* @expectedException Exception
* @expectedExceptionMessage Only sharing within your own groups is allowed
['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
]));
+ $this->factoryProviders();
$this->invokePrivate($this->manager, 'groupCreateChecks', [$share]);
}
->with($path)
->willReturn([$share2]);
+ $this->factoryProviders();
$this->invokePrivate($this->manager, 'groupCreateChecks', [$share]);
}
->with($path)
->willReturn([$share2]);
+ $this->factoryProviders();
+
$this->invokePrivate($this->manager, 'groupCreateChecks', [$share]);
}
$this->invokePrivate($this->manager, 'linkCreateChecks', [$share]);
}
-
public function testLinkCreateChecksPublicUpload() {
$share = new \OC\Share20\Share();
['core', 'shareapi_enabled', 'yes', $sharingEnabled],
]));
- $manager = $this->getMockBuilder('\OC\Share20\Manager')
- ->setConstructorArgs([
- $this->logger,
- $this->config,
- $this->secureRandom,
- $this->hasher,
- $this->mountManager,
- $this->groupManager,
- $this->l,
- ])
+ $manager = $this->createManagerMock()
->setMethods(['isSharingDisabledForUser'])
->getMock();
* @expectedExceptionMessage The Share API is disabled
*/
public function testCreateShareCantShare() {
- $manager = $this->getMockBuilder('\OC\Share20\Manager')
- ->setConstructorArgs([
- $this->logger,
- $this->config,
- $this->secureRandom,
- $this->hasher,
- $this->mountManager,
- $this->groupManager,
- $this->l,
- ])
+ $manager = $this->createManagerMock()
->setMethods(['canShare'])
->getMock();
}
public function testCreateShareUser() {
- $manager = $this->getMockBuilder('\OC\Share20\Manager')
- ->setConstructorArgs([
- $this->logger,
- $this->config,
- $this->secureRandom,
- $this->hasher,
- $this->mountManager,
- $this->groupManager,
- $this->l,
- ])
+ $manager = $this->createManagerMock()
->setMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks'])
->getMock();
- $manager->registerProvider('tmp',
- [\OCP\Share::SHARE_TYPE_USER],
- function() {
- return $this->defaultProvider;
- }
- );
+ $provider = $this->createShareProvider([\OCP\Share::SHARE_TYPE_USER]);
+ $this->factoryProviders([$provider]);
$sharedWith = $this->getMock('\OCP\IUser');
$sharedBy = $this->getMock('\OCP\IUser');
->method('pathCreateChecks')
->with($path);
- $this->defaultProvider
+ $provider
->expects($this->once())
->method('create')
->with($share)
}
public function testCreateShareGroup() {
- $manager = $this->getMockBuilder('\OC\Share20\Manager')
- ->setConstructorArgs([
- $this->logger,
- $this->config,
- $this->secureRandom,
- $this->hasher,
- $this->mountManager,
- $this->groupManager,
- $this->l,
- ])
+ $manager = $this->createManagerMock()
->setMethods(['canShare', 'generalCreateChecks', 'groupCreateChecks', 'pathCreateChecks'])
->getMock();
- $manager->registerProvider('tmp',
- [\OCP\Share::SHARE_TYPE_GROUP],
- function() {
- return $this->defaultProvider;
- }
- );
+ $provider = $this->createShareProvider([\OCP\Share::SHARE_TYPE_GROUP]);
+ $this->factoryProviders([$provider]);
$sharedWith = $this->getMock('\OCP\IGroup');
$sharedBy = $this->getMock('\OCP\IUser');
->method('pathCreateChecks')
->with($path);
- $this->defaultProvider
+ $provider
->expects($this->once())
->method('create')
->with($share)
}
public function testCreateShareLink() {
- $manager = $this->getMockBuilder('\OC\Share20\Manager')
- ->setConstructorArgs([
- $this->logger,
- $this->config,
-
- $this->secureRandom,
- $this->hasher,
- $this->mountManager,
- $this->groupManager,
- $this->l,
- ])
+ $manager = $this->createManagerMock()
->setMethods([
'canShare',
'generalCreateChecks',
])
->getMock();
- $manager->registerProvider('tmp',
- [\OCP\Share::SHARE_TYPE_LINK],
- function() {
- return $this->defaultProvider;
- }
- );
+ $provider = $this->createShareProvider([\OCP\Share::SHARE_TYPE_LINK]);
+ $this->factoryProviders([$provider]);
$sharedBy = $this->getMock('\OCP\IUser');
$sharedBy->method('getUID')->willReturn('sharedBy');
$this->secureRandom->method('generate')
->willReturn('token');
- $this->defaultProvider
+ $provider
->expects($this->once())
->method('create')
->with($share)
* @expectedExceptionMessage I won't let you share
*/
public function testCreateShareHookError() {
- $manager = $this->getMockBuilder('\OC\Share20\Manager')
- ->setConstructorArgs([
- $this->logger,
- $this->config,
- $this->secureRandom,
- $this->hasher,
- $this->mountManager,
- $this->groupManager,
- $this->l,
- ])
+ $manager = $this->createManagerMock()
->setMethods([
'canShare',
'generalCreateChecks',
])
->getMock();
- $manager->registerProvider('tmp',
- [\OCP\Share::SHARE_TYPE_USER],
- function() {
- return $this->defaultProvider;
- }
- );
+ $provider = $this->createShareProvider([\OCP\Share::SHARE_TYPE_USER]);
+ $this->factoryProviders([$provider]);
$sharedWith = $this->getMock('\OCP\IUser');
$sharedBy = $this->getMock('\OCP\IUser');
$path->method('getOwner')->willReturn($shareOwner);
$path->method('getName')->willReturn('target');
- $date = new \DateTime();
-
$share = $this->createShare(
null,
\OCP\Share::SHARE_TYPE_USER,
* @param int[] $shareTypes
*/
public function testRegisterProvider($shareTypes) {
- $manager = new Manager(
- $this->logger,
- $this->config,
- $this->secureRandom,
- $this->hasher,
- $this->mountManager,
- $this->groupManager,
- $this->l
- );
+ $provider = $this->createShareProvider($shareTypes);
+ $this->factoryProviders([$provider]);
- $provider = $this->getMock('OC\Share20\IShareProvider');
- $manager->registerProvider('foo', $shareTypes, function() use ($provider) {
- return $provider;
- });
+ $name = get_class($provider);
- $this->assertEquals($provider, $this->invokePrivate($manager, 'getProvider', ['foo']));
+ $this->assertEquals($provider, $this->invokePrivate($this->manager, 'getProvider', [$name]));
foreach ($shareTypes as $shareType) {
- $this->assertEquals($provider, $this->invokePrivate($manager, 'getProviderForType', [$shareType]));
+ $this->assertEquals($provider, $this->invokePrivate($this->manager, 'getProviderForType', [$shareType]));
}
}
/**
* @expectedException \OC\Share20\Exception\ProviderException
- * @expectedExceptionMessage A share provider with the id 'foo' is already registered
+ * @expectedExceptionMessage ShareProvider is already registered
*/
public function testRegisterProviderDuplicateId() {
- $manager = new Manager(
- $this->logger,
- $this->config,
- $this->secureRandom,
- $this->hasher,
- $this->mountManager,
- $this->groupManager,
- $this->l
- );
-
- $provider = $this->getMock('OC\Share20\IShareProvider');
- $manager->registerProvider('foo', [\OCP\Share::SHARE_TYPE_USER], function() use ($provider) {
- return $provider;
- });
- $manager->registerProvider('foo', [\OCP\Share::SHARE_TYPE_USER], function() use ($provider) {
- return $provider;
- });
+ $provider = $this->createShareProvider([\OCP\Share::SHARE_TYPE_USER], 'ShareProvider');
+ $this->factoryProviders([$provider, $provider]);
+ $this->invokePrivate($this->manager, 'runFactory', []);
}
/**
* @expectedException \OC\Share20\Exception\ProviderException
- * @expectedExceptionMessage shareTypes can't be an empty array
+ * @expectedExceptionMessage Provider ShareProvider must supply share types it can handle
*/
public function testRegisterProviderEmptyShareTypes() {
- $manager = new Manager(
- $this->logger,
- $this->config,
- $this->secureRandom,
- $this->hasher,
- $this->mountManager,
- $this->groupManager,
- $this->l
- );
-
- $provider = $this->getMock('OC\Share20\IShareProvider');
- $manager->registerProvider('foo', [], function() use ($provider) {
- return $provider;
- });
+ $provider = $this->createShareProvider([], 'ShareProvider');
+ $this->factoryProviders([$provider]);
+ $this->invokePrivate($this->manager, 'runFactory', []);
}
/**
* @expectedException \OC\Share20\Exception\ProviderException
- * @expectedExceptionMessage The share provider foo is already registered for share type 0
+ * @expectedExceptionMessage shareProvider2 tried to register for share type 0 but this type is already handled by shareProvider1
*/
public function testRegisterProviderSameType() {
- $manager = new Manager(
- $this->logger,
- $this->config,
- $this->secureRandom,
- $this->hasher,
- $this->mountManager,
- $this->groupManager,
- $this->l
- );
-
- $provider = $this->getMock('OC\Share20\IShareProvider');
- $manager->registerProvider('foo', [\OCP\Share::SHARE_TYPE_USER], function() use ($provider) {
- return $provider;
- });
- $manager->registerProvider('bar', [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP], function() use ($provider) {
- return $provider;
- });
+ $provider1 = $this->createShareProvider([\OCP\Share::SHARE_TYPE_USER], 'shareProvider1');
+ $provider2 = $this->createShareProvider([\OCP\Share::SHARE_TYPE_USER], 'shareProvider2');
+ $this->factoryProviders([$provider1, $provider2]);
+ $this->invokePrivate($this->manager, 'runFactory', []);
}
/**
* @expectedExceptionMessage No provider with id foo found
*/
public function testGetProviderNoProviderWithId() {
- $manager = new Manager(
- $this->logger,
- $this->config,
- $this->secureRandom,
- $this->hasher,
- $this->mountManager,
- $this->groupManager,
- $this->l
- );
-
- $this->invokePrivate($manager, 'getProvider', ['foo']);
- }
-
- /**
- * @expectedException \OC\Share20\Exception\ProviderException
- * @expectedExceptionMessage Callback does not return an IShareProvider instance for provider with id foo
- */
- public function testGetProviderNoIShareProvider() {
- $manager = new Manager(
- $this->logger,
- $this->config,
- $this->secureRandom,
- $this->hasher,
- $this->mountManager,
- $this->groupManager,
- $this->l
- );
-
- $provider = $this->getMock('OC\Share20\IShare');
- $manager->registerProvider('foo', [\OCP\Share::SHARE_TYPE_USER], function() use ($provider) {
- return $provider;
- });
- $this->invokePrivate($manager, 'getProvider', ['foo']);
+ $this->factoryProviders([]);
+ $this->invokePrivate($this->manager, 'getProvider', ['foo']);
}
/**
* @expectedExceptionMessage No share provider registered for share type 1
*/
public function testGetProviderForTypeUnkownType() {
- $manager = new Manager(
- $this->logger,
- $this->config,
- $this->secureRandom,
- $this->hasher,
- $this->mountManager,
- $this->groupManager,
- $this->l
- );
-
- $provider = $this->getMock('OC\Share20\IShareProvider');
- $manager->registerProvider('foo', [\OCP\Share::SHARE_TYPE_USER], function() use ($provider) {
- return $provider;
- });
- $this->invokePrivate($manager, 'getProviderForType', [\OCP\SHARE::SHARE_TYPE_GROUP]);
+ $provider = $this->createShareProvider([\OCP\Share::SHARE_TYPE_USER]);
+ $this->factoryProviders([$provider]);
+ $this->invokePrivate($this->manager, 'getProviderForType', [\OCP\SHARE::SHARE_TYPE_GROUP]);
}
}