split off former searchSharee unit test

also moves registering default plugins to Server for proper unit testing

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
Arthur Schiwon 2017-09-13 14:29:57 +02:00
parent 4a315ede81
commit a28b1d91f9
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
4 changed files with 235 additions and 184 deletions

View File

@ -399,182 +399,6 @@ class ShareesAPIControllerTest extends TestCase {
$this->assertSame($expected, $this->invokePrivate($this->sharees, 'isRemoteSharingAllowed', [$itemType])); $this->assertSame($expected, $this->invokePrivate($this->sharees, 'isRemoteSharingAllowed', [$itemType]));
} }
public function dataSearchSharees() {
return [
['test', 'folder', [Share::SHARE_TYPE_USER, Share::SHARE_TYPE_GROUP, Share::SHARE_TYPE_REMOTE], 1, 2, false, [], [], ['results' => [], 'exact' => [], 'exactIdMatch' => false],
[
'exact' => ['users' => [], 'groups' => [], 'remotes' => [], 'circles' => [], 'emails' => []],
'users' => [],
'groups' => [],
'remotes' => [],
'emails' => [],
'circles' => [],
'lookup' => [],
], false],
['test', 'folder', [Share::SHARE_TYPE_USER, Share::SHARE_TYPE_GROUP, Share::SHARE_TYPE_REMOTE], 1, 2, false, [], [], ['results' => [], 'exact' => [], 'exactIdMatch' => false],
[
'exact' => ['users' => [], 'groups' => [], 'remotes' => [], 'circles' => [], 'emails' => []],
'users' => [],
'groups' => [],
'remotes' => [],
'emails' => [],
'circles' => [],
'lookup' => [],
], false],
[
'test', 'folder', [Share::SHARE_TYPE_USER, Share::SHARE_TYPE_GROUP, Share::SHARE_TYPE_REMOTE], 1, 2, false, [
['label' => 'test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
], [
['label' => 'testgroup1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'testgroup1']],
], [
'results' => [['label' => 'testz@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'testz@remote']]], 'exact' => [], 'exactIdMatch' => false,
],
[
'exact' => ['users' => [], 'groups' => [], 'remotes' => [], 'circles' => [], 'emails' => []],
'users' => [
['label' => 'test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
],
'groups' => [
['label' => 'testgroup1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'testgroup1']],
],
'remotes' => [
['label' => 'testz@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'testz@remote']],
],
'emails' => [],
'circles' => [],
'lookup' => [],
], true,
],
// No groups requested
[
'test', 'folder', [Share::SHARE_TYPE_USER, Share::SHARE_TYPE_REMOTE], 1, 2, false, [
['label' => 'test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
], null, [
'results' => [['label' => 'testz@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'testz@remote']]], 'exact' => [], 'exactIdMatch' => false
],
[
'exact' => ['users' => [], 'groups' => [], 'remotes' => [], 'circles' => [], 'emails' => []],
'users' => [
['label' => 'test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
],
'groups' => [],
'remotes' => [
['label' => 'testz@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'testz@remote']],
],
'emails' => [],
'circles' => [],
'lookup' => [],
], false,
],
// Share type restricted to user - Only one user
[
'test', 'folder', [Share::SHARE_TYPE_USER], 1, 2, false, [
['label' => 'test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
], null, null,
[
'exact' => ['users' => [], 'groups' => [], 'remotes' => [], 'circles' => [], 'emails' => []],
'users' => [
['label' => 'test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
],
'groups' => [],
'remotes' => [],
'emails' => [],
'circles' => [],
'lookup' => [],
], false,
],
// Share type restricted to user - Multipage result
[
'test', 'folder', [Share::SHARE_TYPE_USER], 1, 2, false, [
['label' => 'test 1', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
['label' => 'test 2', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test2']],
], null, null,
[
'exact' => ['users' => [], 'groups' => [], 'remotes' => [], 'circles' => [], 'emails' => []],
'users' => [
['label' => 'test 1', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
['label' => 'test 2', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test2']],
],
'groups' => [],
'remotes' => [],
'emails' => [],
'circles' => [],
'lookup' => [],
], true,
],
];
}
/**
* @dataProvider dataSearchSharees
*
* @param string $searchTerm
* @param string $itemType
* @param array $shareTypes
* @param int $page
* @param int $perPage
* @param bool $shareWithGroupOnly
* @param array $mockedUserResult
* @param array $mockedGroupsResult
* @param array $mockedRemotesResult
* @param array $expected
* @param bool $nextLink
*/
public function testSearchSharees($searchTerm, $itemType, array $shareTypes, $page, $perPage, $shareWithGroupOnly,
$mockedUserResult, $mockedGroupsResult, $mockedRemotesResult, $expected, $nextLink) {
/** @var \PHPUnit_Framework_MockObject_MockObject|\OCA\Files_Sharing\Controller\ShareesAPIController $sharees */
$sharees = $this->getMockBuilder('\OCA\Files_Sharing\Controller\ShareesAPIController')
->setConstructorArgs([
'files_sharing',
$this->getMockBuilder('OCP\IRequest')->disableOriginalConstructor()->getMock(),
$this->groupManager,
$this->userManager,
$this->contactsManager,
$this->getMockBuilder('OCP\IConfig')->disableOriginalConstructor()->getMock(),
$this->session,
$this->getMockBuilder('OCP\IURLGenerator')->disableOriginalConstructor()->getMock(),
$this->getMockBuilder('OCP\ILogger')->disableOriginalConstructor()->getMock(),
$this->shareManager,
$this->clientService,
$this->cloudIdManager
])
->setMethods(array('getShareesForShareIds', 'getUsers', 'getGroups', 'getRemote'))
->getMock();
$sharees->expects(($mockedUserResult === null) ? $this->never() : $this->once())
->method('getUsers')
->with($searchTerm)
->willReturnCallback(function() use ($sharees, $mockedUserResult) {
$result = $this->invokePrivate($sharees, 'result');
$result['users'] = $mockedUserResult;
$this->invokePrivate($sharees, 'result', [$result]);
});
$sharees->expects(($mockedGroupsResult === null) ? $this->never() : $this->once())
->method('getGroups')
->with($searchTerm)
->willReturnCallback(function() use ($sharees, $mockedGroupsResult) {
$result = $this->invokePrivate($sharees, 'result');
$result['groups'] = $mockedGroupsResult;
$this->invokePrivate($sharees, 'result', [$result]);
});
$sharees->expects(($mockedRemotesResult === null) ? $this->never() : $this->once())
->method('getRemote')
->with($searchTerm)
->willReturn($mockedRemotesResult);
$ocs = $this->invokePrivate($sharees, 'searchSharees', [$searchTerm, $itemType, $shareTypes, $page, $perPage, $shareWithGroupOnly]);
$this->assertInstanceOf('\OCP\AppFramework\Http\DataResponse', $ocs);
$this->assertEquals($expected, $ocs->getData());
// Check if next link is set
if ($nextLink) {
$headers = $ocs->getHeaders();
$this->assertArrayHasKey('Link', $headers);
$this->assertStringStartsWith('<', $headers['Link']);
$this->assertStringEndsWith('>; rel="next"', $headers['Link']);
}
}
/** /**
* @expectedException \OCP\AppFramework\OCS\OCSBadRequestException * @expectedException \OCP\AppFramework\OCS\OCSBadRequestException
* @expectedExceptionMessage Missing itemType * @expectedExceptionMessage Missing itemType

View File

@ -34,12 +34,7 @@ class Search implements ISearch {
/** @var IContainer */ /** @var IContainer */
private $c; private $c;
protected $pluginList = [ protected $pluginList = [];
Share::SHARE_TYPE_USER => [UserPlugin::class],
Share::SHARE_TYPE_GROUP => [GroupPlugin::class],
Share::SHARE_TYPE_EMAIL => [MailPlugin::class],
Share::SHARE_TYPE_REMOTE => [RemotePlugin::class],
];
public function __construct(IContainer $c) { public function __construct(IContainer $c) {
$this->c = $c; $this->c = $c;
@ -81,7 +76,7 @@ class Search implements ISearch {
$searchResult->unsetResult($emailType); $searchResult->unsetResult($emailType);
} }
return [$searchResult->asArray(), $hasMoreResults]; return [$searchResult->asArray(), (bool)$hasMoreResults];
} }
public function registerPlugin(array $pluginInfo) { public function registerPlugin(array $pluginInfo) {

View File

@ -52,6 +52,10 @@ use OC\AppFramework\Http\Request;
use OC\AppFramework\Utility\SimpleContainer; use OC\AppFramework\Utility\SimpleContainer;
use OC\AppFramework\Utility\TimeFactory; use OC\AppFramework\Utility\TimeFactory;
use OC\Authentication\LoginCredentials\Store; use OC\Authentication\LoginCredentials\Store;
use OC\Collaboration\Collaborators\GroupPlugin;
use OC\Collaboration\Collaborators\MailPlugin;
use OC\Collaboration\Collaborators\RemotePlugin;
use OC\Collaboration\Collaborators\UserPlugin;
use OC\Command\CronBus; use OC\Command\CronBus;
use OC\Contacts\ContactsMenu\ActionFactory; use OC\Contacts\ContactsMenu\ActionFactory;
use OC\Diagnostics\EventLogger; use OC\Diagnostics\EventLogger;
@ -115,6 +119,7 @@ use OCP\Contacts\ContactsMenu\IActionFactory;
use OCP\Lock\ILockingProvider; use OCP\Lock\ILockingProvider;
use OCP\RichObjectStrings\IValidator; use OCP\RichObjectStrings\IValidator;
use OCP\Security\IContentSecurityPolicyManager; use OCP\Security\IContentSecurityPolicyManager;
use OCP\Share;
use OCP\Share\IShareHelper; use OCP\Share\IShareHelper;
use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@ -994,7 +999,15 @@ class Server extends ServerContainer implements IServerContainer {
$this->registerAlias('ShareManager', \OCP\Share\IManager::class); $this->registerAlias('ShareManager', \OCP\Share\IManager::class);
$this->registerService(\OCP\Collaboration\Collaborators\ISearch::class, function(Server $c) { $this->registerService(\OCP\Collaboration\Collaborators\ISearch::class, function(Server $c) {
return new Collaboration\Collaborators\Search($c); $instance = new Collaboration\Collaborators\Search($c);
// register default plugins
$instance->registerPlugin(['shareType' => 'SHARE_TYPE_USER', 'class' => UserPlugin::class]);
$instance->registerPlugin(['shareType' => 'SHARE_TYPE_GROUP', 'class' => GroupPlugin::class]);
$instance->registerPlugin(['shareType' => 'SHARE_TYPE_EMAIL', 'class' => MailPlugin::class]);
$instance->registerPlugin(['shareType' => 'SHARE_TYPE_REMOTE', 'class' => RemotePlugin::class]);
return $instance;
}); });
$this->registerAlias('CollaboratorSearch', \OCP\Collaboration\Collaborators\ISearch::class); $this->registerAlias('CollaboratorSearch', \OCP\Collaboration\Collaborators\ISearch::class);

View File

@ -0,0 +1,219 @@
<?php
/**
* @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace Test\Collaboration\Collaborators;
use OC\Collaboration\Collaborators\Search;
use OC\Collaboration\Collaborators\SearchResult;
use OCP\Collaboration\Collaborators\ISearch;
use OCP\Collaboration\Collaborators\ISearchPlugin;
use OCP\Collaboration\Collaborators\SearchResultType;
use OCP\IContainer;
use OCP\Share;
use Test\TestCase;
class SearchTest extends TestCase {
/** @var IContainer|\PHPUnit_Framework_MockObject_MockObject */
protected $container;
/** @var ISearch */
protected $search;
public function setUp() {
parent::setUp();
$this->container = $this->createMock(IContainer::class);
$this->search = new Search($this->container);
}
/**
* @dataProvider dataSearchSharees
*
* @param string $searchTerm
* @param array $shareTypes
* @param int $page
* @param int $perPage
* @param array $mockedUserResult
* @param array $mockedGroupsResult
* @param array $mockedRemotesResult
* @param array $expected
* @param bool $expectedMoreResults
*/
public function testSearch(
$searchTerm,
array $shareTypes,
$page,
$perPage,
array $mockedUserResult,
array $mockedGroupsResult,
array $mockedRemotesResult,
array $expected,
$expectedMoreResults
) {
$searchResult = new SearchResult();
$userPlugin = $this->createMock(ISearchPlugin::class);
$userPlugin->expects($this->any())
->method('search')
->willReturnCallback(function() use ($searchResult, $mockedUserResult, $expectedMoreResults) {
$type = new SearchResultType('users');
$searchResult->addResultSet($type, $mockedUserResult);
return $expectedMoreResults;
});
$groupPlugin = $this->createMock(ISearchPlugin::class);
$groupPlugin->expects($this->any())
->method('search')
->willReturnCallback(function() use ($searchResult, $mockedGroupsResult, $expectedMoreResults) {
$type = new SearchResultType('groups');
$searchResult->addResultSet($type, $mockedGroupsResult);
return $expectedMoreResults;
});
$remotePlugin = $this->createMock(ISearchPlugin::class);
$remotePlugin->expects($this->any())
->method('search')
->willReturnCallback(function() use ($searchResult, $mockedRemotesResult, $expectedMoreResults) {
if($mockedRemotesResult !== null) {
$type = new SearchResultType('remotes');
$searchResult->addResultSet($type, $mockedRemotesResult['results'], $mockedRemotesResult['exact']);
if($mockedRemotesResult['exactIdMatch'] === true) {
$searchResult->markExactIdMatch($type);
}
}
return $expectedMoreResults;
});
$this->container->expects($this->any())
->method('resolve')
->willReturnCallback(function($class) use ($searchResult, $userPlugin, $groupPlugin, $remotePlugin) {
if($class === SearchResult::class) {
return $searchResult;
} elseif ($class === $userPlugin) {
return $userPlugin;
} elseif ($class === $groupPlugin) {
return $groupPlugin;
} elseif ($class === $remotePlugin) {
return $remotePlugin;
}
return null;
});
$this->search->registerPlugin(['shareType' => 'SHARE_TYPE_USER', 'class' => $userPlugin]);
$this->search->registerPlugin(['shareType' => 'SHARE_TYPE_GROUP', 'class' => $groupPlugin]);
$this->search->registerPlugin(['shareType' => 'SHARE_TYPE_REMOTE', 'class' => $remotePlugin]);
list($results, $moreResults) = $this->search->search($searchTerm, $shareTypes, false, $perPage, $perPage * ($page - 1));
$this->assertEquals($expected, $results);
$this->assertSame($expectedMoreResults, $moreResults);
}
public function dataSearchSharees() {
return [
[
'test', [Share::SHARE_TYPE_USER, Share::SHARE_TYPE_GROUP, Share::SHARE_TYPE_REMOTE], 1, 2, [], [], ['results' => [], 'exact' => [], 'exactIdMatch' => false],
[
'exact' => ['users' => [], 'groups' => [], 'remotes' => []],
'users' => [],
'groups' => [],
'remotes' => [],
], false
],
[
'test', [Share::SHARE_TYPE_USER, Share::SHARE_TYPE_GROUP, Share::SHARE_TYPE_REMOTE], 1, 2, [], [], ['results' => [], 'exact' => [], 'exactIdMatch' => false],
[
'exact' => ['users' => [], 'groups' => [], 'remotes' => []],
'users' => [],
'groups' => [],
'remotes' => [],
], false
],
[
'test', [Share::SHARE_TYPE_USER, Share::SHARE_TYPE_GROUP, Share::SHARE_TYPE_REMOTE], 1, 2, [
['label' => 'test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
], [
['label' => 'testgroup1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'testgroup1']],
], [
'results' => [['label' => 'testz@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'testz@remote']]], 'exact' => [], 'exactIdMatch' => false,
],
[
'exact' => ['users' => [], 'groups' => [], 'remotes' => []],
'users' => [
['label' => 'test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
],
'groups' => [
['label' => 'testgroup1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'testgroup1']],
],
'remotes' => [
['label' => 'testz@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'testz@remote']],
],
], true,
],
// No groups requested
[
'test', [Share::SHARE_TYPE_USER, Share::SHARE_TYPE_REMOTE], 1, 2, [
['label' => 'test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
], [], [
'results' => [['label' => 'testz@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'testz@remote']]], 'exact' => [], 'exactIdMatch' => false
],
[
'exact' => ['users' => [], 'remotes' => []],
'users' => [
['label' => 'test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
],
'remotes' => [
['label' => 'testz@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'testz@remote']],
],
], false,
],
// Share type restricted to user - Only one user
[
'test', [Share::SHARE_TYPE_USER], 1, 2, [
['label' => 'test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
], [], [],
[
'exact' => ['users' => []],
'users' => [
['label' => 'test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
],
], false,
],
// Share type restricted to user - Multipage result
[
'test', [Share::SHARE_TYPE_USER], 1, 2, [
['label' => 'test 1', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
['label' => 'test 2', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test2']],
], [], [],
[
'exact' => ['users' => []],
'users' => [
['label' => 'test 1', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
['label' => 'test 2', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test2']],
],
], true,
],
];
}
}