From dd9e191d373217b2a07e4ac5b2cc294c0a6227a1 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Tue, 12 Sep 2017 17:42:57 +0200 Subject: split off getUsers unit tests Signed-off-by: Arthur Schiwon --- .../tests/Controller/ShareesAPIControllerTest.php | 318 --------------------- 1 file changed, 318 deletions(-) (limited to 'apps/files_sharing/tests') diff --git a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php index e027d0751cb..aa5ad4eec65 100644 --- a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php +++ b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php @@ -162,324 +162,6 @@ class ShareesAPIControllerTest extends TestCase { return $group; } - public function dataGetUsers() { - return [ - ['test', false, true, [], [], [], [], true, false], - ['test', false, false, [], [], [], [], true, false], - ['test', true, true, [], [], [], [], true, false], - ['test', true, false, [], [], [], [], true, false], - [ - 'test', false, true, [], [], - [ - ['label' => 'Test', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test']], - ], [], true, $this->getUserMock('test', 'Test') - ], - [ - 'test', false, false, [], [], - [ - ['label' => 'Test', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test']], - ], [], true, $this->getUserMock('test', 'Test') - ], - [ - 'test', true, true, [], [], - [], [], true, $this->getUserMock('test', 'Test') - ], - [ - 'test', true, false, [], [], - [], [], true, $this->getUserMock('test', 'Test') - ], - [ - 'test', true, true, ['test-group'], [['test-group', 'test', 2, 0, []]], - [ - ['label' => 'Test', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test']], - ], [], true, $this->getUserMock('test', 'Test') - ], - [ - 'test', true, false, ['test-group'], [['test-group', 'test', 2, 0, []]], - [ - ['label' => 'Test', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test']], - ], [], true, $this->getUserMock('test', 'Test') - ], - [ - 'test', - false, - true, - [], - [ - $this->getUserMock('test1', 'Test One'), - ], - [], - [ - ['label' => 'Test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']], - ], - true, - false, - ], - [ - 'test', - false, - false, - [], - [ - $this->getUserMock('test1', 'Test One'), - ], - [], - [], - true, - false, - ], - [ - 'test', - false, - true, - [], - [ - $this->getUserMock('test1', 'Test One'), - $this->getUserMock('test2', 'Test Two'), - ], - [], - [ - ['label' => 'Test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']], - ['label' => 'Test Two', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test2']], - ], - false, - false, - ], - [ - 'test', - false, - false, - [], - [ - $this->getUserMock('test1', 'Test One'), - $this->getUserMock('test2', 'Test Two'), - ], - [], - [], - true, - false, - ], - [ - 'test', - false, - true, - [], - [ - $this->getUserMock('test0', 'Test'), - $this->getUserMock('test1', 'Test One'), - $this->getUserMock('test2', 'Test Two'), - ], - [ - ['label' => 'Test', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test0']], - ], - [ - ['label' => 'Test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']], - ['label' => 'Test Two', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test2']], - ], - false, - false, - ], - [ - 'test', - false, - false, - [], - [ - $this->getUserMock('test0', 'Test'), - $this->getUserMock('test1', 'Test One'), - $this->getUserMock('test2', 'Test Two'), - ], - [ - ['label' => 'Test', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test0']], - ], - [], - true, - false, - ], - [ - 'test', - true, - true, - ['abc', 'xyz'], - [ - ['abc', 'test', 2, 0, ['test1' => 'Test One']], - ['xyz', 'test', 2, 0, []], - ], - [], - [ - ['label' => 'Test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']], - ], - true, - false, - ], - [ - 'test', - true, - false, - ['abc', 'xyz'], - [ - ['abc', 'test', 2, 0, ['test1' => 'Test One']], - ['xyz', 'test', 2, 0, []], - ], - [], - [], - true, - false, - ], - [ - 'test', - true, - true, - ['abc', 'xyz'], - [ - ['abc', 'test', 2, 0, [ - 'test1' => 'Test One', - 'test2' => 'Test Two', - ]], - ['xyz', 'test', 2, 0, [ - 'test1' => 'Test One', - 'test2' => 'Test Two', - ]], - ], - [], - [ - ['label' => 'Test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']], - ['label' => 'Test Two', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test2']], - ], - false, - false, - ], - [ - 'test', - true, - false, - ['abc', 'xyz'], - [ - ['abc', 'test', 2, 0, [ - 'test1' => 'Test One', - 'test2' => 'Test Two', - ]], - ['xyz', 'test', 2, 0, [ - 'test1' => 'Test One', - 'test2' => 'Test Two', - ]], - ], - [], - [], - true, - false, - ], - [ - 'test', - true, - true, - ['abc', 'xyz'], - [ - ['abc', 'test', 2, 0, [ - 'test' => 'Test One', - ]], - ['xyz', 'test', 2, 0, [ - 'test2' => 'Test Two', - ]], - ], - [ - ['label' => 'Test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test']], - ], - [ - ['label' => 'Test Two', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test2']], - ], - false, - false, - ], - [ - 'test', - true, - false, - ['abc', 'xyz'], - [ - ['abc', 'test', 2, 0, [ - 'test' => 'Test One', - ]], - ['xyz', 'test', 2, 0, [ - 'test2' => 'Test Two', - ]], - ], - [ - ['label' => 'Test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test']], - ], - [], - true, - false, - ], - ]; - } - - /** - * @dataProvider dataGetUsers - * - * @param string $searchTerm - * @param bool $shareWithGroupOnly - * @param bool $shareeEnumeration - * @param array $groupResponse - * @param array $userResponse - * @param array $exactExpected - * @param array $expected - * @param bool $reachedEnd - * @param mixed $singleUser - */ - public function testGetUsers($searchTerm, $shareWithGroupOnly, $shareeEnumeration, $groupResponse, $userResponse, $exactExpected, $expected, $reachedEnd, $singleUser) { - $this->invokePrivate($this->sharees, 'limit', [2]); - $this->invokePrivate($this->sharees, 'offset', [0]); - $this->invokePrivate($this->sharees, 'shareWithGroupOnly', [$shareWithGroupOnly]); - $this->invokePrivate($this->sharees, 'shareeEnumeration', [$shareeEnumeration]); - - $user = $this->getUserMock('admin', 'Administrator'); - $this->session->expects($this->any()) - ->method('getUser') - ->willReturn($user); - - if (!$shareWithGroupOnly) { - $this->userManager->expects($this->once()) - ->method('searchDisplayName') - ->with($searchTerm, $this->invokePrivate($this->sharees, 'limit'), $this->invokePrivate($this->sharees, 'offset')) - ->willReturn($userResponse); - } else { - if ($singleUser !== false) { - $this->groupManager->expects($this->exactly(2)) - ->method('getUserGroupIds') - ->withConsecutive( - $user, - $singleUser - ) - ->willReturn($groupResponse); - } else { - $this->groupManager->expects($this->once()) - ->method('getUserGroupIds') - ->with($user) - ->willReturn($groupResponse); - } - - $this->groupManager->expects($this->exactly(sizeof($groupResponse))) - ->method('displayNamesInGroup') - ->with($this->anything(), $searchTerm, $this->invokePrivate($this->sharees, 'limit'), $this->invokePrivate($this->sharees, 'offset')) - ->willReturnMap($userResponse); - } - - if ($singleUser !== false) { - $this->userManager->expects($this->once()) - ->method('get') - ->with($searchTerm) - ->willReturn($singleUser); - } - - $this->invokePrivate($this->sharees, 'getUsers', [$searchTerm]); - $result = $this->invokePrivate($this->sharees, 'result'); - - $this->assertEquals($exactExpected, $result['exact']['users']); - $this->assertEquals($expected, $result['users']); - $this->assertCount((int) $reachedEnd, $this->invokePrivate($this->sharees, 'reachedEndFor')); - } - public function dataGetGroups() { return [ ['test', false, true, [], [], [], [], true, false], -- cgit v1.2.3 From b35760de2718cbf8c9361d2192051e50ff447d43 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Tue, 12 Sep 2017 21:58:09 +0200 Subject: split off getGroups unit tests Signed-off-by: Arthur Schiwon --- .../tests/Controller/ShareesAPIControllerTest.php | 392 ---------------- .../Collaborators/GroupPluginTest.php | 491 +++++++++++++++++++++ 2 files changed, 491 insertions(+), 392 deletions(-) create mode 100644 tests/lib/Collaboration/Collaborators/GroupPluginTest.php (limited to 'apps/files_sharing/tests') diff --git a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php index aa5ad4eec65..f9d762d65aa 100644 --- a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php +++ b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php @@ -116,398 +116,6 @@ class ShareesAPIControllerTest extends TestCase { ); } - /** - * @param string $uid - * @param string $displayName - * @return \OCP\IUser|\PHPUnit_Framework_MockObject_MockObject - */ - protected function getUserMock($uid, $displayName) { - $user = $this->getMockBuilder('OCP\IUser') - ->disableOriginalConstructor() - ->getMock(); - - $user->expects($this->any()) - ->method('getUID') - ->willReturn($uid); - - $user->expects($this->any()) - ->method('getDisplayName') - ->willReturn($displayName); - - return $user; - } - - /** - * @param string $gid - * @return \OCP\IGroup|\PHPUnit_Framework_MockObject_MockObject - */ - protected function getGroupMock($gid, $displayName = null) { - $group = $this->getMockBuilder('OCP\IGroup') - ->disableOriginalConstructor() - ->getMock(); - - $group->expects($this->any()) - ->method('getGID') - ->willReturn($gid); - - if (is_null($displayName)) { - // note: this is how the Group class behaves - $displayName = $gid; - } - - $group->expects($this->any()) - ->method('getDisplayName') - ->willReturn($displayName); - - return $group; - } - - public function dataGetGroups() { - return [ - ['test', false, true, [], [], [], [], true, false], - ['test', false, false, [], [], [], [], true, false], - // group without display name - [ - 'test', false, true, - [$this->getGroupMock('test1')], - [], - [], - [['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]], - true, - false, - ], - // group with display name, search by id - [ - 'test', false, true, - [$this->getGroupMock('test1', 'Test One')], - [], - [], - [['label' => 'Test One', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]], - true, - false, - ], - // group with display name, search by display name - [ - 'one', false, true, - [$this->getGroupMock('test1', 'Test One')], - [], - [], - [['label' => 'Test One', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]], - true, - false, - ], - // group with display name, search by display name, exact expected - [ - 'Test One', false, true, - [$this->getGroupMock('test1', 'Test One')], - [], - [['label' => 'Test One', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]], - [], - true, - false, - ], - [ - 'test', false, false, - [$this->getGroupMock('test1')], - [], - [], - [], - true, - false, - ], - [ - 'test', false, true, - [ - $this->getGroupMock('test'), - $this->getGroupMock('test1'), - ], - [], - [['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']]], - [['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]], - false, - false, - ], - [ - 'test', false, false, - [ - $this->getGroupMock('test'), - $this->getGroupMock('test1'), - ], - [], - [['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']]], - [], - true, - false, - ], - [ - 'test', false, true, - [ - $this->getGroupMock('test0'), - $this->getGroupMock('test1'), - ], - [], - [], - [ - ['label' => 'test0', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test0']], - ['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']], - ], - false, - null, - ], - [ - 'test', false, false, - [ - $this->getGroupMock('test0'), - $this->getGroupMock('test1'), - ], - [], - [], - [], - true, - null, - ], - [ - 'test', false, true, - [ - $this->getGroupMock('test0'), - $this->getGroupMock('test1'), - ], - [], - [ - ['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']], - ], - [ - ['label' => 'test0', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test0']], - ['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']], - ], - false, - $this->getGroupMock('test'), - ], - [ - 'test', false, false, - [ - $this->getGroupMock('test0'), - $this->getGroupMock('test1'), - ], - [], - [ - ['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']], - ], - [], - true, - $this->getGroupMock('test'), - ], - ['test', true, true, [], [], [], [], true, false], - ['test', true, false, [], [], [], [], true, false], - [ - 'test', true, true, - [ - $this->getGroupMock('test1'), - $this->getGroupMock('test2'), - ], - [$this->getGroupMock('test1')], - [], - [['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]], - false, - false, - ], - [ - 'test', true, false, - [ - $this->getGroupMock('test1'), - $this->getGroupMock('test2'), - ], - [$this->getGroupMock('test1')], - [], - [], - true, - false, - ], - [ - 'test', true, true, - [ - $this->getGroupMock('test'), - $this->getGroupMock('test1'), - ], - [$this->getGroupMock('test')], - [['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']]], - [], - false, - false, - ], - [ - 'test', true, false, - [ - $this->getGroupMock('test'), - $this->getGroupMock('test1'), - ], - [$this->getGroupMock('test')], - [['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']]], - [], - true, - false, - ], - [ - 'test', true, true, - [ - $this->getGroupMock('test'), - $this->getGroupMock('test1'), - ], - [$this->getGroupMock('test1')], - [], - [['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]], - false, - false, - ], - [ - 'test', true, false, - [ - $this->getGroupMock('test'), - $this->getGroupMock('test1'), - ], - [$this->getGroupMock('test1')], - [], - [], - true, - false, - ], - [ - 'test', true, true, - [ - $this->getGroupMock('test'), - $this->getGroupMock('test1'), - ], - [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')], - [['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']]], - [['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]], - false, - false, - ], - [ - 'test', true, false, - [ - $this->getGroupMock('test'), - $this->getGroupMock('test1'), - ], - [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')], - [['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']]], - [], - true, - false, - ], - [ - 'test', true, true, - [ - $this->getGroupMock('test0'), - $this->getGroupMock('test1'), - ], - [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')], - [], - [ - ['label' => 'test0', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test0']], - ['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']], - ], - false, - null, - ], - [ - 'test', true, false, - [ - $this->getGroupMock('test0'), - $this->getGroupMock('test1'), - ], - [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')], - [], - [], - true, - null, - ], - [ - 'test', true, true, - [ - $this->getGroupMock('test0'), - $this->getGroupMock('test1'), - ], - [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')], - [ - ['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']], - ], - [ - ['label' => 'test0', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test0']], - ['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']], - ], - false, - $this->getGroupMock('test'), - ], - [ - 'test', true, false, - [ - $this->getGroupMock('test0'), - $this->getGroupMock('test1'), - ], - [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')], - [ - ['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']], - ], - [], - true, - $this->getGroupMock('test'), - ], - ]; - } - - /** - * @dataProvider dataGetGroups - * - * @param string $searchTerm - * @param bool $shareWithGroupOnly - * @param bool $shareeEnumeration - * @param array $groupResponse - * @param array $userGroupsResponse - * @param array $exactExpected - * @param array $expected - * @param bool $reachedEnd - * @param mixed $singleGroup - */ - public function testGetGroups($searchTerm, $shareWithGroupOnly, $shareeEnumeration, $groupResponse, $userGroupsResponse, $exactExpected, $expected, $reachedEnd, $singleGroup) { - $this->invokePrivate($this->sharees, 'limit', [2]); - $this->invokePrivate($this->sharees, 'offset', [0]); - $this->invokePrivate($this->sharees, 'shareWithGroupOnly', [$shareWithGroupOnly]); - $this->invokePrivate($this->sharees, 'shareeEnumeration', [$shareeEnumeration]); - - $this->groupManager->expects($this->once()) - ->method('search') - ->with($searchTerm, $this->invokePrivate($this->sharees, 'limit'), $this->invokePrivate($this->sharees, 'offset')) - ->willReturn($groupResponse); - - if ($singleGroup !== false) { - $this->groupManager->expects($this->once()) - ->method('get') - ->with($searchTerm) - ->willReturn($singleGroup); - } - - if ($shareWithGroupOnly) { - $user = $this->getUserMock('admin', 'Administrator'); - $this->session->expects($this->any()) - ->method('getUser') - ->willReturn($user); - - $numGetUserGroupsCalls = empty($groupResponse) ? 0 : 1; - $this->groupManager->expects($this->exactly($numGetUserGroupsCalls)) - ->method('getUserGroups') - ->with($user) - ->willReturn($userGroupsResponse); - } - - $this->invokePrivate($this->sharees, 'getGroups', [$searchTerm]); - $result = $this->invokePrivate($this->sharees, 'result'); - - $this->assertEquals($exactExpected, $result['exact']['groups']); - $this->assertEquals($expected, $result['groups']); - $this->assertCount((int) $reachedEnd, $this->invokePrivate($this->sharees, 'reachedEndFor')); - } - /** * @dataProvider dataGetRemote * diff --git a/tests/lib/Collaboration/Collaborators/GroupPluginTest.php b/tests/lib/Collaboration/Collaborators/GroupPluginTest.php new file mode 100644 index 00000000000..9849bdb874a --- /dev/null +++ b/tests/lib/Collaboration/Collaborators/GroupPluginTest.php @@ -0,0 +1,491 @@ + + * + * @author Arthur Schiwon + * + * @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 . + * + */ + +namespace Test\Collaboration\Collaborators; + + +use OC\Collaboration\Collaborators\GroupPlugin; +use OC\Collaboration\Collaborators\SearchResult; +use OCP\Collaboration\Collaborators\ISearchResult; +use OCP\IConfig; +use OCP\IGroup; +use OCP\IGroupManager; +use OCP\IUser; +use OCP\IUserSession; +use OCP\Share; +use Test\TestCase; + +class GroupPluginTest extends TestCase { + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ + protected $config; + + /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */ + protected $groupManager; + + /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */ + protected $session; + + /** @var ISearchResult */ + protected $searchResult; + + /** @var GroupPlugin */ + protected $plugin; + + /** @var int */ + protected $limit = 2; + + /** @var int */ + protected $offset = 0; + + /** @var IUser|\PHPUnit_Framework_MockObject_MockObject */ + protected $user; + + public function setUp() { + parent::setUp(); + + $this->config = $this->createMock(IConfig::class); + + $this->groupManager = $this->createMock(IGroupManager::class); + + $this->session = $this->createMock(IUserSession::class); + + $this->searchResult = new SearchResult(); + + $this->user = $this->getUserMock('admin', 'Administrator'); + } + + public function instantiatePlugin() { + // cannot be done within setUp, because dependent mocks needs to be set + // up with configuration etc. first + $this->plugin = new GroupPlugin( + $this->config, + $this->groupManager, + $this->session + ); + } + + public function getUserMock($uid, $displayName) { + $user = $this->createMock(IUser::class); + + $user->expects($this->any()) + ->method('getUID') + ->willReturn($uid); + + $user->expects($this->any()) + ->method('getDisplayName') + ->willReturn($displayName); + + return $user; + } + + /** + * @param string $gid + * @param null $displayName + * @return IGroup|\PHPUnit_Framework_MockObject_MockObject + */ + protected function getGroupMock($gid, $displayName = null) { + $group = $this->createMock(IGroup::class); + + $group->expects($this->any()) + ->method('getGID') + ->willReturn($gid); + + if (is_null($displayName)) { + // note: this is how the Group class behaves + $displayName = $gid; + } + + $group->expects($this->any()) + ->method('getDisplayName') + ->willReturn($displayName); + + return $group; + } + + public function dataGetGroups() { + return [ + ['test', false, true, [], [], [], [], true, false], + ['test', false, false, [], [], [], [], true, false], + // group without display name + [ + 'test', false, true, + [$this->getGroupMock('test1')], + [], + [], + [['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]], + true, + false, + ], + // group with display name, search by id + [ + 'test', false, true, + [$this->getGroupMock('test1', 'Test One')], + [], + [], + [['label' => 'Test One', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]], + true, + false, + ], + // group with display name, search by display name + [ + 'one', false, true, + [$this->getGroupMock('test1', 'Test One')], + [], + [], + [['label' => 'Test One', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]], + true, + false, + ], + // group with display name, search by display name, exact expected + [ + 'Test One', false, true, + [$this->getGroupMock('test1', 'Test One')], + [], + [['label' => 'Test One', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]], + [], + true, + false, + ], + [ + 'test', false, false, + [$this->getGroupMock('test1')], + [], + [], + [], + true, + false, + ], + [ + 'test', false, true, + [ + $this->getGroupMock('test'), + $this->getGroupMock('test1'), + ], + [], + [['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']]], + [['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]], + false, + false, + ], + [ + 'test', false, false, + [ + $this->getGroupMock('test'), + $this->getGroupMock('test1'), + ], + [], + [['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']]], + [], + true, + false, + ], + [ + 'test', false, true, + [ + $this->getGroupMock('test0'), + $this->getGroupMock('test1'), + ], + [], + [], + [ + ['label' => 'test0', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test0']], + ['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']], + ], + false, + null, + ], + [ + 'test', false, false, + [ + $this->getGroupMock('test0'), + $this->getGroupMock('test1'), + ], + [], + [], + [], + true, + null, + ], + [ + 'test', false, true, + [ + $this->getGroupMock('test0'), + $this->getGroupMock('test1'), + ], + [], + [ + ['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']], + ], + [ + ['label' => 'test0', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test0']], + ['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']], + ], + false, + $this->getGroupMock('test'), + ], + [ + 'test', false, false, + [ + $this->getGroupMock('test0'), + $this->getGroupMock('test1'), + ], + [], + [ + ['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']], + ], + [], + true, + $this->getGroupMock('test'), + ], + ['test', true, true, [], [], [], [], true, false], + ['test', true, false, [], [], [], [], true, false], + [ + 'test', true, true, + [ + $this->getGroupMock('test1'), + $this->getGroupMock('test2'), + ], + [$this->getGroupMock('test1')], + [], + [['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]], + false, + false, + ], + [ + 'test', true, false, + [ + $this->getGroupMock('test1'), + $this->getGroupMock('test2'), + ], + [$this->getGroupMock('test1')], + [], + [], + true, + false, + ], + [ + 'test', true, true, + [ + $this->getGroupMock('test'), + $this->getGroupMock('test1'), + ], + [$this->getGroupMock('test')], + [['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']]], + [], + false, + false, + ], + [ + 'test', true, false, + [ + $this->getGroupMock('test'), + $this->getGroupMock('test1'), + ], + [$this->getGroupMock('test')], + [['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']]], + [], + true, + false, + ], + [ + 'test', true, true, + [ + $this->getGroupMock('test'), + $this->getGroupMock('test1'), + ], + [$this->getGroupMock('test1')], + [], + [['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]], + false, + false, + ], + [ + 'test', true, false, + [ + $this->getGroupMock('test'), + $this->getGroupMock('test1'), + ], + [$this->getGroupMock('test1')], + [], + [], + true, + false, + ], + [ + 'test', true, true, + [ + $this->getGroupMock('test'), + $this->getGroupMock('test1'), + ], + [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')], + [['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']]], + [['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]], + false, + false, + ], + [ + 'test', true, false, + [ + $this->getGroupMock('test'), + $this->getGroupMock('test1'), + ], + [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')], + [['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']]], + [], + true, + false, + ], + [ + 'test', true, true, + [ + $this->getGroupMock('test0'), + $this->getGroupMock('test1'), + ], + [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')], + [], + [ + ['label' => 'test0', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test0']], + ['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']], + ], + false, + null, + ], + [ + 'test', true, false, + [ + $this->getGroupMock('test0'), + $this->getGroupMock('test1'), + ], + [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')], + [], + [], + true, + null, + ], + [ + 'test', true, true, + [ + $this->getGroupMock('test0'), + $this->getGroupMock('test1'), + ], + [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')], + [ + ['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']], + ], + [ + ['label' => 'test0', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test0']], + ['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']], + ], + false, + $this->getGroupMock('test'), + ], + [ + 'test', true, false, + [ + $this->getGroupMock('test0'), + $this->getGroupMock('test1'), + ], + [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')], + [ + ['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']], + ], + [], + true, + $this->getGroupMock('test'), + ], + ]; + } + + /** + * @dataProvider dataGetGroups + * + * @param string $searchTerm + * @param bool $shareWithGroupOnly + * @param bool $shareeEnumeration + * @param array $groupResponse + * @param array $userGroupsResponse + * @param array $exactExpected + * @param array $expected + * @param bool $reachedEnd + * @param bool|IGroup $singleGroup + */ + public function testSearch( + $searchTerm, + $shareWithGroupOnly, + $shareeEnumeration, + array $groupResponse, + array $userGroupsResponse, + array $exactExpected, + array $expected, + $reachedEnd, + $singleGroup + ) { + $this->config->expects($this->any()) + ->method('getAppValue') + ->willReturnCallback( + function($appName, $key, $default) + use ($shareWithGroupOnly, $shareeEnumeration) + { + if ($appName === 'core' && $key === 'shareapi_only_share_with_group_members') { + return $shareWithGroupOnly ? 'yes' : 'no'; + } else if ($appName === 'core' && $key === 'shareapi_allow_share_dialog_user_enumeration') { + return $shareeEnumeration ? 'yes' : 'no'; + } + return $default; + } + ); + + $this->instantiatePlugin(); + + $this->groupManager->expects($this->once()) + ->method('search') + ->with($searchTerm, $this->limit, $this->offset) + ->willReturn($groupResponse); + + if ($singleGroup !== false) { + $this->groupManager->expects($this->once()) + ->method('get') + ->with($searchTerm) + ->willReturn($singleGroup); + } + + if ($shareWithGroupOnly) { + $this->session->expects($this->any()) + ->method('getUser') + ->willReturn($this->user); + + $numGetUserGroupsCalls = empty($groupResponse) ? 0 : 1; + $this->groupManager->expects($this->exactly($numGetUserGroupsCalls)) + ->method('getUserGroups') + ->with($this->user) + ->willReturn($userGroupsResponse); + } + + $moreResults = $this->plugin->search($searchTerm, $this->limit, $this->offset, $this->searchResult); + $result = $this->searchResult->asArray(); + + $this->assertEquals($exactExpected, $result['exact']['groups']); + $this->assertEquals($expected, $result['groups']); + $this->assertSame($reachedEnd, $moreResults); + } +} -- cgit v1.2.3 From c92f7ee7674541f1e899e0f5077d79907d21df4c Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Tue, 12 Sep 2017 22:38:36 +0200 Subject: split off getRemote unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit adds two small fixes → they actually work \o/ Signed-off-by: Arthur Schiwon --- .../tests/Controller/ShareesAPIControllerTest.php | 220 --------------- .../Collaboration/Collaborators/RemotePlugin.php | 4 +- .../Collaborators/RemotePluginTest.php | 314 +++++++++++++++++++++ 3 files changed, 316 insertions(+), 222 deletions(-) create mode 100644 tests/lib/Collaboration/Collaborators/RemotePluginTest.php (limited to 'apps/files_sharing/tests') diff --git a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php index f9d762d65aa..7b321678908 100644 --- a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php +++ b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php @@ -116,226 +116,6 @@ class ShareesAPIControllerTest extends TestCase { ); } - /** - * @dataProvider dataGetRemote - * - * @param string $searchTerm - * @param array $contacts - * @param bool $shareeEnumeration - * @param array $expected - * @param bool $reachedEnd - */ - public function testGetRemote($searchTerm, $contacts, $shareeEnumeration, $expected, $reachedEnd) { - $this->invokePrivate($this->sharees, 'shareeEnumeration', [$shareeEnumeration]); - $this->contactsManager->expects($this->any()) - ->method('search') - ->with($searchTerm, ['CLOUD', 'FN']) - ->willReturn($contacts); - - $result = $this->invokePrivate($this->sharees, 'getRemote', [$searchTerm]); - - $this->assertEquals($expected, $result); - $this->assertCount((int) $reachedEnd, $this->invokePrivate($this->sharees, 'reachedEndFor')); - } - - public function dataGetRemote() { - return [ - ['test', [], true, ['results' => [], 'exact' => [], 'exactIdMatch' => false], true], - ['test', [], false, ['results' => [], 'exact' => [], 'exactIdMatch' => false], true], - [ - 'test@remote', - [], - true, - ['results' => [], 'exact' => [['label' => 'test@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'test@remote']]], 'exactIdMatch' => false], - true, - ], - [ - 'test@remote', - [], - false, - ['results' => [], 'exact' => [['label' => 'test@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'test@remote']]], 'exactIdMatch' => false], - true, - ], - [ - 'test', - [ - [ - 'FN' => 'User3 @ Localhost', - ], - [ - 'FN' => 'User2 @ Localhost', - 'CLOUD' => [ - ], - ], - [ - 'FN' => 'User @ Localhost', - 'CLOUD' => [ - 'username@localhost', - ], - ], - ], - true, - ['results' => [['label' => 'User @ Localhost (username@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'username@localhost', 'server' => 'localhost']]], 'exact' => [], 'exactIdMatch' => false], - true, - ], - [ - 'test', - [ - [ - 'FN' => 'User3 @ Localhost', - ], - [ - 'FN' => 'User2 @ Localhost', - 'CLOUD' => [ - ], - ], - [ - 'FN' => 'User @ Localhost', - 'CLOUD' => [ - 'username@localhost', - ], - ], - ], - false, - ['results' => [], 'exact' => [], 'exactIdMatch' => false], - true, - ], - [ - 'test@remote', - [ - [ - 'FN' => 'User3 @ Localhost', - ], - [ - 'FN' => 'User2 @ Localhost', - 'CLOUD' => [ - ], - ], - [ - 'FN' => 'User @ Localhost', - 'CLOUD' => [ - 'username@localhost', - ], - ], - ], - true, - ['results' => [['label' => 'User @ Localhost (username@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'username@localhost', 'server' => 'localhost']]], 'exact' => [['label' => 'test@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'test@remote']]], 'exactIdMatch' => false], - true, - ], - [ - 'test@remote', - [ - [ - 'FN' => 'User3 @ Localhost', - ], - [ - 'FN' => 'User2 @ Localhost', - 'CLOUD' => [ - ], - ], - [ - 'FN' => 'User @ Localhost', - 'CLOUD' => [ - 'username@localhost', - ], - ], - ], - false, - ['results' => [], 'exact' => [['label' => 'test@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'test@remote']]], 'exactIdMatch' => false], - true, - ], - [ - 'username@localhost', - [ - [ - 'FN' => 'User3 @ Localhost', - ], - [ - 'FN' => 'User2 @ Localhost', - 'CLOUD' => [ - ], - ], - [ - 'FN' => 'User @ Localhost', - 'CLOUD' => [ - 'username@localhost', - ], - ], - ], - true, - ['results' => [], 'exact' => [['label' => 'User @ Localhost (username@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'username@localhost', 'server' => 'localhost']]], 'exactIdMatch' => true], - true, - ], - [ - 'username@localhost', - [ - [ - 'FN' => 'User3 @ Localhost', - ], - [ - 'FN' => 'User2 @ Localhost', - 'CLOUD' => [ - ], - ], - [ - 'FN' => 'User @ Localhost', - 'CLOUD' => [ - 'username@localhost', - ], - ], - ], - false, - ['results' => [], 'exact' => [['label' => 'User @ Localhost (username@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'username@localhost', 'server' => 'localhost']]], 'exactIdMatch' => true], - true, - ], - // contact with space - [ - 'user name@localhost', - [ - [ - 'FN' => 'User3 @ Localhost', - ], - [ - 'FN' => 'User2 @ Localhost', - 'CLOUD' => [ - ], - ], - [ - 'FN' => 'User Name @ Localhost', - 'CLOUD' => [ - 'user name@localhost', - ], - ], - ], - false, - ['results' => [], 'exact' => [['label' => 'User Name @ Localhost (user name@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'user name@localhost', 'server' => 'localhost']]], 'exactIdMatch' => true], - true, - ], - // remote with space, no contact - [ - 'user space@remote', - [ - [ - 'FN' => 'User3 @ Localhost', - ], - [ - 'FN' => 'User2 @ Localhost', - 'CLOUD' => [ - ], - ], - [ - 'FN' => 'User @ Localhost', - 'CLOUD' => [ - 'username@localhost', - ], - ], - ], - false, - ['results' => [], 'exact' => [['label' => 'user space@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'user space@remote']]], 'exactIdMatch' => false], - true, - ], - ]; - } /** * @dataProvider dataGetEmail diff --git a/lib/private/Collaboration/Collaborators/RemotePlugin.php b/lib/private/Collaboration/Collaborators/RemotePlugin.php index c97a951be7e..b17a64e4ff1 100644 --- a/lib/private/Collaboration/Collaborators/RemotePlugin.php +++ b/lib/private/Collaboration/Collaborators/RemotePlugin.php @@ -76,7 +76,7 @@ class RemotePlugin implements ISearchPlugin { if (strtolower($contact['FN']) === $lowerSearch || strtolower($cloudId) === $lowerSearch) { if (strtolower($cloudId) === $lowerSearch) { - $searchResult->hasExactIdMatch($resultType); + $searchResult->markExactIdMatch($resultType); } $result['exact'][] = [ 'label' => $contact['FN'] . " ($cloudId)", @@ -116,7 +116,7 @@ class RemotePlugin implements ISearchPlugin { $searchResult->addResultSet($resultType, $result['wide'], $result['exact']); - return false; + return true; } /** diff --git a/tests/lib/Collaboration/Collaborators/RemotePluginTest.php b/tests/lib/Collaboration/Collaborators/RemotePluginTest.php new file mode 100644 index 00000000000..4856230bf69 --- /dev/null +++ b/tests/lib/Collaboration/Collaborators/RemotePluginTest.php @@ -0,0 +1,314 @@ + + * + * @author Arthur Schiwon + * + * @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 . + * + */ + +namespace Test\Collaboration\Collaborators; + + +use OC\Collaboration\Collaborators\RemotePlugin; +use OC\Collaboration\Collaborators\SearchResult; +use OC\Federation\CloudIdManager; +use OCP\Collaboration\Collaborators\SearchResultType; +use OCP\Contacts\IManager; +use OCP\Federation\ICloudIdManager; +use OCP\IConfig; +use OCP\Share; +use Test\TestCase; + +class RemotePluginTest extends TestCase { + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ + protected $config; + + /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ + protected $contactsManager; + + /** @var ICloudIdManager|\PHPUnit_Framework_MockObject_MockObject */ + protected $cloudIdManager; + + /** @var RemotePlugin */ + protected $plugin; + + /** @var SearchResult */ + protected $searchResult; + + public function setUp() { + parent::setUp(); + + $this->config = $this->createMock(IConfig::class); + $this->contactsManager = $this->createMock(IManager::class); + //$this->cloudIdManager = $this->createMock(ICloudIdManager::class); + $this->cloudIdManager = new CloudIdManager(); + $this->searchResult = new SearchResult(); + } + + public function instantiatePlugin() { + $this->plugin = new RemotePlugin($this->contactsManager, $this->cloudIdManager, $this->config); + } + + /** + * @dataProvider dataGetRemote + * + * @param string $searchTerm + * @param array $contacts + * @param bool $shareeEnumeration + * @param array $expected + * @param bool $exactIdMatch + * @param bool $reachedEnd + */ + public function testSearch($searchTerm, array $contacts, $shareeEnumeration, array $expected, $exactIdMatch, $reachedEnd) { + $this->config->expects($this->any()) + ->method('getAppValue') + ->willReturnCallback( + function($appName, $key, $default) + use ($shareeEnumeration) + { + if ($appName === 'core' && $key === 'shareapi_allow_share_dialog_user_enumeration') { + return $shareeEnumeration ? 'yes' : 'no'; + } + return $default; + } + ); + + $this->instantiatePlugin(); + + $this->contactsManager->expects($this->any()) + ->method('search') + ->with($searchTerm, ['CLOUD', 'FN']) + ->willReturn($contacts); + + $moreResults = $this->plugin->search($searchTerm, 0, 0, $this->searchResult); + $result = $this->searchResult->asArray(); + + $this->assertSame($exactIdMatch, $this->searchResult->hasExactIdMatch(new SearchResultType('remotes'))); + $this->assertEquals($expected, $result); + $this->assertSame($reachedEnd, $moreResults); + } + + public function dataGetRemote() { + return [ + ['test', [], true, ['remotes' => [], 'exact' => ['remotes' => []]], false, true], + ['test', [], false, ['remotes' => [], 'exact' => ['remotes' => []]], false, true], + [ + 'test@remote', + [], + true, + ['remotes' => [], 'exact' => ['remotes' => [['label' => 'test@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'test@remote']]]]], + false, + true, + ], + [ + 'test@remote', + [], + false, + ['remotes' => [], 'exact' => ['remotes' => [['label' => 'test@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'test@remote']]]]], + false, + true, + ], + [ + 'test', + [ + [ + 'FN' => 'User3 @ Localhost', + ], + [ + 'FN' => 'User2 @ Localhost', + 'CLOUD' => [ + ], + ], + [ + 'FN' => 'User @ Localhost', + 'CLOUD' => [ + 'username@localhost', + ], + ], + ], + true, + ['remotes' => [['label' => 'User @ Localhost (username@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'username@localhost', 'server' => 'localhost']]], 'exact' => ['remotes' => []]], + false, + true, + ], + [ + 'test', + [ + [ + 'FN' => 'User3 @ Localhost', + ], + [ + 'FN' => 'User2 @ Localhost', + 'CLOUD' => [ + ], + ], + [ + 'FN' => 'User @ Localhost', + 'CLOUD' => [ + 'username@localhost', + ], + ], + ], + false, + ['remotes' => [], 'exact' => ['remotes' => []]], + false, + true, + ], + [ + 'test@remote', + [ + [ + 'FN' => 'User3 @ Localhost', + ], + [ + 'FN' => 'User2 @ Localhost', + 'CLOUD' => [ + ], + ], + [ + 'FN' => 'User @ Localhost', + 'CLOUD' => [ + 'username@localhost', + ], + ], + ], + true, + ['remotes' => [['label' => 'User @ Localhost (username@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'username@localhost', 'server' => 'localhost']]], 'exact' => ['remotes' => [['label' => 'test@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'test@remote']]]]], + false, + true, + ], + [ + 'test@remote', + [ + [ + 'FN' => 'User3 @ Localhost', + ], + [ + 'FN' => 'User2 @ Localhost', + 'CLOUD' => [ + ], + ], + [ + 'FN' => 'User @ Localhost', + 'CLOUD' => [ + 'username@localhost', + ], + ], + ], + false, + ['remotes' => [], 'exact' => ['remotes' => [['label' => 'test@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'test@remote']]]]], + false, + true, + ], + [ + 'username@localhost', + [ + [ + 'FN' => 'User3 @ Localhost', + ], + [ + 'FN' => 'User2 @ Localhost', + 'CLOUD' => [ + ], + ], + [ + 'FN' => 'User @ Localhost', + 'CLOUD' => [ + 'username@localhost', + ], + ], + ], + true, + ['remotes' => [], 'exact' => ['remotes' => [['label' => 'User @ Localhost (username@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'username@localhost', 'server' => 'localhost']]]]], + true, + true, + ], + [ + 'username@localhost', + [ + [ + 'FN' => 'User3 @ Localhost', + ], + [ + 'FN' => 'User2 @ Localhost', + 'CLOUD' => [ + ], + ], + [ + 'FN' => 'User @ Localhost', + 'CLOUD' => [ + 'username@localhost', + ], + ], + ], + false, + ['remotes' => [], 'exact' => ['remotes' => [['label' => 'User @ Localhost (username@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'username@localhost', 'server' => 'localhost']]]]], + true, + true, + ], + // contact with space + [ + 'user name@localhost', + [ + [ + 'FN' => 'User3 @ Localhost', + ], + [ + 'FN' => 'User2 @ Localhost', + 'CLOUD' => [ + ], + ], + [ + 'FN' => 'User Name @ Localhost', + 'CLOUD' => [ + 'user name@localhost', + ], + ], + ], + false, + ['remotes' => [], 'exact' => ['remotes' => [['label' => 'User Name @ Localhost (user name@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'user name@localhost', 'server' => 'localhost']]]]], + true, + true, + ], + // remote with space, no contact + [ + 'user space@remote', + [ + [ + 'FN' => 'User3 @ Localhost', + ], + [ + 'FN' => 'User2 @ Localhost', + 'CLOUD' => [ + ], + ], + [ + 'FN' => 'User @ Localhost', + 'CLOUD' => [ + 'username@localhost', + ], + ], + ], + false, + ['remotes' => [], 'exact' => ['remotes' => [['label' => 'user space@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'user space@remote']]]]], + false, + true, + ], + ]; + } +} -- cgit v1.2.3 From 3d1d03a51175e0e52b506764bc7d96ddeae4cb65 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Tue, 12 Sep 2017 23:40:46 +0200 Subject: split off getEmail tests Signed-off-by: Arthur Schiwon --- .../tests/Controller/ShareesAPIControllerTest.php | 244 --------------- .../Collaboration/Collaborators/MailPlugin.php | 2 +- .../Collaboration/Collaborators/MailPluginTest.php | 337 +++++++++++++++++++++ 3 files changed, 338 insertions(+), 245 deletions(-) create mode 100644 tests/lib/Collaboration/Collaborators/MailPluginTest.php (limited to 'apps/files_sharing/tests') diff --git a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php index 7b321678908..928a151c420 100644 --- a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php +++ b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php @@ -116,250 +116,6 @@ class ShareesAPIControllerTest extends TestCase { ); } - - /** - * @dataProvider dataGetEmail - * - * @param string $searchTerm - * @param array $contacts - * @param bool $shareeEnumeration - * @param array $expected - * @param bool $reachedEnd - */ - public function testGetEmail($searchTerm, $contacts, $shareeEnumeration, $expected, $reachedEnd) { - $this->invokePrivate($this->sharees, 'shareeEnumeration', [$shareeEnumeration]); - $this->contactsManager->expects($this->any()) - ->method('search') - ->with($searchTerm, ['EMAIL', 'FN']) - ->willReturn($contacts); - - $result = $this->invokePrivate($this->sharees, 'getEmail', [$searchTerm]); - - $this->assertEquals($expected, $result); - $this->assertCount((int) $reachedEnd, $this->invokePrivate($this->sharees, 'reachedEndFor')); - } - - public function dataGetEmail() { - return [ - ['test', [], true, ['results' => [], 'exact' => [], 'exactIdMatch' => false], true], - ['test', [], false, ['results' => [], 'exact' => [], 'exactIdMatch' => false], true], - [ - 'test@remote.com', - [], - true, - ['results' => [], 'exact' => [['label' => 'test@remote.com', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'test@remote.com']]], 'exactIdMatch' => false], - true, - ], - [ // no valid email address - 'test@remote', - [], - true, - ['results' => [], 'exact' => [], 'exactIdMatch' => false], - true, - ], - [ - 'test@remote.com', - [], - false, - ['results' => [], 'exact' => [['label' => 'test@remote.com', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'test@remote.com']]], 'exactIdMatch' => false], - true, - ], - [ - 'test', - [ - [ - 'FN' => 'User3 @ Localhost', - ], - [ - 'FN' => 'User2 @ Localhost', - 'EMAIL' => [ - ], - ], - [ - 'FN' => 'User @ Localhost', - 'EMAIL' => [ - 'username@localhost', - ], - ], - ], - true, - ['results' => [['label' => 'User @ Localhost (username@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'username@localhost']]], 'exact' => [], 'exactIdMatch' => false], - true, - ], - [ - 'test', - [ - [ - 'FN' => 'User3 @ Localhost', - ], - [ - 'FN' => 'User2 @ Localhost', - 'EMAIL' => [ - ], - ], - [ - 'FN' => 'User @ Localhost', - 'EMAIL' => [ - 'username@localhost', - ], - ], - ], - false, - ['results' => [], 'exact' => [], 'exactIdMatch' => false], - true, - ], - [ - 'test@remote.com', - [ - [ - 'FN' => 'User3 @ Localhost', - ], - [ - 'FN' => 'User2 @ Localhost', - 'EMAIL' => [ - ], - ], - [ - 'FN' => 'User @ Localhost', - 'EMAIL' => [ - 'username@localhost', - ], - ], - ], - true, - ['results' => [['label' => 'User @ Localhost (username@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'username@localhost']]], 'exact' => [['label' => 'test@remote.com', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'test@remote.com']]], 'exactIdMatch' => false], - true, - ], - [ - 'test@remote.com', - [ - [ - 'FN' => 'User3 @ Localhost', - ], - [ - 'FN' => 'User2 @ Localhost', - 'EMAIL' => [ - ], - ], - [ - 'FN' => 'User @ Localhost', - 'EMAIL' => [ - 'username@localhost', - ], - ], - ], - false, - ['results' => [], 'exact' => [['label' => 'test@remote.com', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'test@remote.com']]], 'exactIdMatch' => false], - true, - ], - [ - 'username@localhost', - [ - [ - 'FN' => 'User3 @ Localhost', - ], - [ - 'FN' => 'User2 @ Localhost', - 'EMAIL' => [ - ], - ], - [ - 'FN' => 'User @ Localhost', - 'EMAIL' => [ - 'username@localhost', - ], - ], - ], - true, - ['results' => [], 'exact' => [['label' => 'User @ Localhost (username@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'username@localhost']]], 'exactIdMatch' => true], - true, - ], - [ - 'username@localhost', - [ - [ - 'FN' => 'User3 @ Localhost', - ], - [ - 'FN' => 'User2 @ Localhost', - 'EMAIL' => [ - ], - ], - [ - 'FN' => 'User @ Localhost', - 'EMAIL' => [ - 'username@localhost', - ], - ], - ], - false, - ['results' => [], 'exact' => [['label' => 'User @ Localhost (username@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'username@localhost']]], 'exactIdMatch' => true], - true, - ], - // contact with space - [ - 'user name@localhost', - [ - [ - 'FN' => 'User3 @ Localhost', - ], - [ - 'FN' => 'User2 @ Localhost', - 'EMAIL' => [ - ], - ], - [ - 'FN' => 'User Name @ Localhost', - 'EMAIL' => [ - 'user name@localhost', - ], - ], - ], - false, - ['results' => [], 'exact' => [['label' => 'User Name @ Localhost (user name@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'user name@localhost']]], 'exactIdMatch' => true], - true, - ], - // remote with space, no contact - [ - 'user space@remote.com', - [ - [ - 'FN' => 'User3 @ Localhost', - ], - [ - 'FN' => 'User2 @ Localhost', - 'EMAIL' => [ - ], - ], - [ - 'FN' => 'User @ Localhost', - 'EMAIL' => [ - 'username@localhost', - ], - ], - ], - false, - ['results' => [], 'exact' => [], 'exactIdMatch' => false], - true, - ], - // Local user found by email - [ - 'test@example.com', - [ - [ - 'FN' => 'User', - 'EMAIL' => ['test@example.com'], - 'CLOUD' => ['test@localhost'], - 'isLocalSystemBook' => true, - ] - ], - false, - ['results' => [], 'exact' => [], 'exactIdMatch' => true], - false, - ] - ]; - } - public function dataSearch() { $noRemote = [Share::SHARE_TYPE_USER, Share::SHARE_TYPE_GROUP, Share::SHARE_TYPE_EMAIL]; $allTypes = [Share::SHARE_TYPE_USER, Share::SHARE_TYPE_GROUP, Share::SHARE_TYPE_REMOTE, Share::SHARE_TYPE_EMAIL]; diff --git a/lib/private/Collaboration/Collaborators/MailPlugin.php b/lib/private/Collaboration/Collaborators/MailPlugin.php index e6bda176566..d28bd3692a4 100644 --- a/lib/private/Collaboration/Collaborators/MailPlugin.php +++ b/lib/private/Collaboration/Collaborators/MailPlugin.php @@ -159,6 +159,6 @@ class MailPlugin implements ISearchPlugin { $searchResult->addResultSet($emailType, $result['wide'], $result['exact']); - return false; + return true; } } diff --git a/tests/lib/Collaboration/Collaborators/MailPluginTest.php b/tests/lib/Collaboration/Collaborators/MailPluginTest.php new file mode 100644 index 00000000000..af6e0680526 --- /dev/null +++ b/tests/lib/Collaboration/Collaborators/MailPluginTest.php @@ -0,0 +1,337 @@ + + * + * @author Arthur Schiwon + * + * @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 . + * + */ + +namespace Test\Collaboration\Collaborators; + + +use OC\Collaboration\Collaborators\MailPlugin; +use OC\Collaboration\Collaborators\SearchResult; +use OC\Federation\CloudIdManager; +use OCP\Collaboration\Collaborators\SearchResultType; +use OCP\Contacts\IManager; +use OCP\Federation\ICloudIdManager; +use OCP\IConfig; +use OCP\Share; +use Test\TestCase; + +class MailPluginTest extends TestCase { + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ + protected $config; + + /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ + protected $contactsManager; + + /** @var ICloudIdManager|\PHPUnit_Framework_MockObject_MockObject */ + protected $cloudIdManager; + + /** @var MailPlugin */ + protected $plugin; + + /** @var SearchResult */ + protected $searchResult; + + public function setUp() { + parent::setUp(); + + $this->config = $this->createMock(IConfig::class); + $this->contactsManager = $this->createMock(IManager::class); + //$this->cloudIdManager = $this->createMock(ICloudIdManager::class); + $this->cloudIdManager = new CloudIdManager(); + $this->searchResult = new SearchResult(); + } + + public function instantiatePlugin() { + $this->plugin = new MailPlugin($this->contactsManager, $this->cloudIdManager, $this->config); + } + + /** + * @dataProvider dataGetEmail + * + * @param string $searchTerm + * @param array $contacts + * @param bool $shareeEnumeration + * @param array $expected + * @param bool $reachedEnd + */ + public function testSearch($searchTerm, $contacts, $shareeEnumeration, $expected, $exactIdMatch, $reachedEnd) { + $this->config->expects($this->any()) + ->method('getAppValue') + ->willReturnCallback( + function($appName, $key, $default) + use ($shareeEnumeration) + { + if ($appName === 'core' && $key === 'shareapi_allow_share_dialog_user_enumeration') { + return $shareeEnumeration ? 'yes' : 'no'; + } + return $default; + } + ); + + $this->instantiatePlugin(); + + $this->contactsManager->expects($this->any()) + ->method('search') + ->with($searchTerm, ['EMAIL', 'FN']) + ->willReturn($contacts); + + $moreResults = $this->plugin->search($searchTerm, 0, 0, $this->searchResult); + $result = $this->searchResult->asArray(); + + $this->assertSame($exactIdMatch, $this->searchResult->hasExactIdMatch(new SearchResultType('emails'))); + $this->assertEquals($expected, $result); + $this->assertSame($reachedEnd, $moreResults); + } + + public function dataGetEmail() { + return [ + ['test', [], true, ['emails' => [], 'exact' => ['emails' => []]], false, true], + ['test', [], false, ['emails' => [], 'exact' => ['emails' => []]], false, true], + [ + 'test@remote.com', + [], + true, + ['emails' => [], 'exact' => ['emails' => [['label' => 'test@remote.com', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'test@remote.com']]]]], + false, + true, + ], + [ // no valid email address + 'test@remote', + [], + true, + ['emails' => [], 'exact' => ['emails' => []]], + false, + true, + ], + [ + 'test@remote.com', + [], + false, + ['emails' => [], 'exact' => ['emails' => [['label' => 'test@remote.com', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'test@remote.com']]]]], + false, + true, + ], + [ + 'test', + [ + [ + 'FN' => 'User3 @ Localhost', + ], + [ + 'FN' => 'User2 @ Localhost', + 'EMAIL' => [ + ], + ], + [ + 'FN' => 'User @ Localhost', + 'EMAIL' => [ + 'username@localhost', + ], + ], + ], + true, + ['emails' => [['label' => 'User @ Localhost (username@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'username@localhost']]], 'exact' => ['emails' => []]], + false, + true, + ], + [ + 'test', + [ + [ + 'FN' => 'User3 @ Localhost', + ], + [ + 'FN' => 'User2 @ Localhost', + 'EMAIL' => [ + ], + ], + [ + 'FN' => 'User @ Localhost', + 'EMAIL' => [ + 'username@localhost', + ], + ], + ], + false, + ['emails' => [], 'exact' => ['emails' => []]], + false, + true, + ], + [ + 'test@remote.com', + [ + [ + 'FN' => 'User3 @ Localhost', + ], + [ + 'FN' => 'User2 @ Localhost', + 'EMAIL' => [ + ], + ], + [ + 'FN' => 'User @ Localhost', + 'EMAIL' => [ + 'username@localhost', + ], + ], + ], + true, + ['emails' => [['label' => 'User @ Localhost (username@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'username@localhost']]], 'exact' => ['emails' => [['label' => 'test@remote.com', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'test@remote.com']]]]], + false, + true, + ], + [ + 'test@remote.com', + [ + [ + 'FN' => 'User3 @ Localhost', + ], + [ + 'FN' => 'User2 @ Localhost', + 'EMAIL' => [ + ], + ], + [ + 'FN' => 'User @ Localhost', + 'EMAIL' => [ + 'username@localhost', + ], + ], + ], + false, + ['emails' => [], 'exact' => ['emails' => [['label' => 'test@remote.com', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'test@remote.com']]]]], + false, + true, + ], + [ + 'username@localhost', + [ + [ + 'FN' => 'User3 @ Localhost', + ], + [ + 'FN' => 'User2 @ Localhost', + 'EMAIL' => [ + ], + ], + [ + 'FN' => 'User @ Localhost', + 'EMAIL' => [ + 'username@localhost', + ], + ], + ], + true, + ['emails' => [], 'exact' => ['emails' => [['label' => 'User @ Localhost (username@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'username@localhost']]]]], + true, + true, + ], + [ + 'username@localhost', + [ + [ + 'FN' => 'User3 @ Localhost', + ], + [ + 'FN' => 'User2 @ Localhost', + 'EMAIL' => [ + ], + ], + [ + 'FN' => 'User @ Localhost', + 'EMAIL' => [ + 'username@localhost', + ], + ], + ], + false, + ['emails' => [], 'exact' => ['emails' => [['label' => 'User @ Localhost (username@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'username@localhost']]]]], + true, + true, + ], + // contact with space + [ + 'user name@localhost', + [ + [ + 'FN' => 'User3 @ Localhost', + ], + [ + 'FN' => 'User2 @ Localhost', + 'EMAIL' => [ + ], + ], + [ + 'FN' => 'User Name @ Localhost', + 'EMAIL' => [ + 'user name@localhost', + ], + ], + ], + false, + ['emails' => [], 'exact' => ['emails' => [['label' => 'User Name @ Localhost (user name@localhost)', 'value' => ['shareType' => Share::SHARE_TYPE_EMAIL, 'shareWith' => 'user name@localhost']]]]], + true, + true, + ], + // remote with space, no contact + [ + 'user space@remote.com', + [ + [ + 'FN' => 'User3 @ Localhost', + ], + [ + 'FN' => 'User2 @ Localhost', + 'EMAIL' => [ + ], + ], + [ + 'FN' => 'User @ Localhost', + 'EMAIL' => [ + 'username@localhost', + ], + ], + ], + false, + ['emails' => [], 'exact' => ['emails' => []]], + false, + true, + ], + // Local user found by email + [ + 'test@example.com', + [ + [ + 'FN' => 'User', + 'EMAIL' => ['test@example.com'], + 'CLOUD' => ['test@localhost'], + 'isLocalSystemBook' => true, + ] + ], + false, + ['users' => [], 'exact' => ['users' => [['label' => 'User (test@example.com)','value' => ['shareType' => 0, 'shareWith' => 'test'],]]]], + true, + false, + ] + ]; + } +} -- cgit v1.2.3 From 38ff7eaeced5e56786376f7c522fb07c8420c7b3 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 13 Sep 2017 12:32:47 +0200 Subject: adjust ShareeAPIController search test Signed-off-by: Arthur Schiwon --- .../lib/Controller/ShareesAPIController.php | 5 ++- .../tests/Controller/ShareesAPIControllerTest.php | 49 ++++++++-------------- 2 files changed, 22 insertions(+), 32 deletions(-) (limited to 'apps/files_sharing/tests') diff --git a/apps/files_sharing/lib/Controller/ShareesAPIController.php b/apps/files_sharing/lib/Controller/ShareesAPIController.php index 45c1b5e6dcf..17d258b3a8f 100644 --- a/apps/files_sharing/lib/Controller/ShareesAPIController.php +++ b/apps/files_sharing/lib/Controller/ShareesAPIController.php @@ -199,7 +199,9 @@ class ShareesAPIController extends OCSController { list($result, $hasMoreResults) = $this->collaboratorSearch->search($search, $shareTypes, $lookup, $this->limit, $this->offset); // extra treatment for 'exact' subarray, with a single merge expected keys might be lost - $result['exact'] = array_merge($this->result['exact'], $result['exact']); + if(isset($result['exact'])) { + $result['exact'] = array_merge($this->result['exact'], $result['exact']); + } $this->result = array_merge($this->result, $result); $response = new DataResponse($this->result); @@ -223,6 +225,7 @@ class ShareesAPIController extends OCSController { */ protected function isRemoteSharingAllowed($itemType) { try { + // FIXME: static foo makes unit testing unnecessarily difficult $backend = \OC\Share\Share::getBackend($itemType); return $backend->isShareTypeAllowed(Share::SHARE_TYPE_REMOTE); } catch (\Exception $e) { diff --git a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php index 928a151c420..2586876f88d 100644 --- a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php +++ b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php @@ -250,14 +250,13 @@ class ShareesAPIControllerTest extends TestCase { */ public function testSearch($getData, $apiSetting, $enumSetting, $remoteSharingEnabled, $emailSharingEnabled, $shareTypes, $shareWithGroupOnly, $shareeEnumeration, $allowGroupSharing) { $search = isset($getData['search']) ? $getData['search'] : ''; - $itemType = isset($getData['itemType']) ? $getData['itemType'] : null; + $itemType = isset($getData['itemType']) ? $getData['itemType'] : 'irrelevant'; $page = isset($getData['page']) ? $getData['page'] : 1; $perPage = isset($getData['perPage']) ? $getData['perPage'] : 200; $shareType = isset($getData['shareType']) ? $getData['shareType'] : null; - $config = $this->getMockBuilder('OCP\IConfig') - ->disableOriginalConstructor() - ->getMock(); + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject $config */ + $config = $this->createMock(IConfig::class); $config->expects($this->exactly(2)) ->method('getAppValue') ->with('core', $this->anything(), $this->anything()) @@ -270,41 +269,29 @@ class ShareesAPIControllerTest extends TestCase { ->method('allowGroupSharing') ->willReturn($allowGroupSharing); + /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject $request */ + $request = $this->createMock(IRequest::class); + /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject $urlGenerator */ + $urlGenerator = $this->createMock(IURLGenerator::class); + /** @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, + $request, $config, - $this->session, - $this->getMockBuilder('OCP\IURLGenerator')->disableOriginalConstructor()->getMock(), - $this->getMockBuilder('OCP\ILogger')->disableOriginalConstructor()->getMock(), + $urlGenerator, $this->shareManager, - $this->clientService, - $this->cloudIdManager + $this->collaboratorSearch ]) - ->setMethods(array('searchSharees', 'isRemoteSharingAllowed', 'shareProviderExists')) + ->setMethods(array('isRemoteSharingAllowed', 'shareProviderExists')) ->getMock(); - $sharees->expects($this->once()) - ->method('searchSharees') - ->willReturnCallback(function - ($isearch, $iitemType, $ishareTypes, $ipage, $iperPage) - use ($search, $itemType, $shareTypes, $page, $perPage) { - - // We are doing strict comparisons here, so we can differ 0/'' and null on shareType/itemType - $this->assertSame($search, $isearch); - $this->assertSame($itemType, $iitemType); - $this->assertSame(count($shareTypes), count($ishareTypes)); - foreach($shareTypes as $expected) { - $this->assertTrue(in_array($expected, $ishareTypes)); - } - $this->assertSame($page, $ipage); - $this->assertSame($perPage, $iperPage); - return new Http\DataResponse(); - }); + + $this->collaboratorSearch->expects($this->once()) + ->method('search') + ->with($search, $shareTypes, $this->anything(), $perPage, $this->invokePrivate($sharees, 'offset')) + ->willReturn([[], false]); + $sharees->expects($this->any()) ->method('isRemoteSharingAllowed') ->with($itemType) -- cgit v1.2.3 From 4a315ede819b700b6f55fa1c6dc70c60ea728ea4 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 13 Sep 2017 13:21:02 +0200 Subject: adjust ShareeAPIController invalid-input search unit test Signed-off-by: Arthur Schiwon --- .../tests/Controller/ShareesAPIControllerTest.php | 31 +++++++++++----------- 1 file changed, 15 insertions(+), 16 deletions(-) (limited to 'apps/files_sharing/tests') diff --git a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php index 2586876f88d..ccacd382935 100644 --- a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php +++ b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php @@ -284,7 +284,7 @@ class ShareesAPIControllerTest extends TestCase { $this->shareManager, $this->collaboratorSearch ]) - ->setMethods(array('isRemoteSharingAllowed', 'shareProviderExists')) + ->setMethods(['isRemoteSharingAllowed', 'shareProviderExists']) ->getMock(); $this->collaboratorSearch->expects($this->once()) @@ -344,35 +344,34 @@ class ShareesAPIControllerTest extends TestCase { $page = isset($getData['page']) ? $getData['page'] : 1; $perPage = isset($getData['perPage']) ? $getData['perPage'] : 200; - $config = $this->getMockBuilder('OCP\IConfig') - ->disableOriginalConstructor() - ->getMock(); + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject $config */ + $config = $this->createMock(IConfig::class); $config->expects($this->never()) ->method('getAppValue'); + /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject $request */ + $request = $this->createMock(IRequest::class); + /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject $urlGenerator */ + $urlGenerator = $this->createMock(IURLGenerator::class); + /** @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, + $request, $config, - $this->session, - $this->getMockBuilder('OCP\IURLGenerator')->disableOriginalConstructor()->getMock(), - $this->getMockBuilder('OCP\ILogger')->disableOriginalConstructor()->getMock(), + $urlGenerator, $this->shareManager, - $this->clientService, - $this->cloudIdManager + $this->collaboratorSearch ]) - ->setMethods(array('searchSharees', 'isRemoteSharingAllowed')) + ->setMethods(['isRemoteSharingAllowed']) ->getMock(); - $sharees->expects($this->never()) - ->method('searchSharees'); $sharees->expects($this->never()) ->method('isRemoteSharingAllowed'); + $this->collaboratorSearch->expects($this->never()) + ->method('search'); + try { $sharees->search('', null, $page, $perPage, null); $this->fail(); -- cgit v1.2.3 From a28b1d91f95f421c6efbb7752d9404c6719c0688 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 13 Sep 2017 14:29:57 +0200 Subject: split off former searchSharee unit test also moves registering default plugins to Server for proper unit testing Signed-off-by: Arthur Schiwon --- .../tests/Controller/ShareesAPIControllerTest.php | 176 ----------------- lib/private/Collaboration/Collaborators/Search.php | 9 +- lib/private/Server.php | 15 +- .../lib/Collaboration/Collaborators/SearchTest.php | 219 +++++++++++++++++++++ 4 files changed, 235 insertions(+), 184 deletions(-) create mode 100644 tests/lib/Collaboration/Collaborators/SearchTest.php (limited to 'apps/files_sharing/tests') diff --git a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php index ccacd382935..529a8754b83 100644 --- a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php +++ b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php @@ -399,182 +399,6 @@ class ShareesAPIControllerTest extends TestCase { $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 * @expectedExceptionMessage Missing itemType diff --git a/lib/private/Collaboration/Collaborators/Search.php b/lib/private/Collaboration/Collaborators/Search.php index 5a17a3fb653..e9b15dd1201 100644 --- a/lib/private/Collaboration/Collaborators/Search.php +++ b/lib/private/Collaboration/Collaborators/Search.php @@ -34,12 +34,7 @@ class Search implements ISearch { /** @var IContainer */ private $c; - 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], - ]; + protected $pluginList = []; public function __construct(IContainer $c) { $this->c = $c; @@ -81,7 +76,7 @@ class Search implements ISearch { $searchResult->unsetResult($emailType); } - return [$searchResult->asArray(), $hasMoreResults]; + return [$searchResult->asArray(), (bool)$hasMoreResults]; } public function registerPlugin(array $pluginInfo) { diff --git a/lib/private/Server.php b/lib/private/Server.php index 88fc79e472e..29aee06d896 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -52,6 +52,10 @@ use OC\AppFramework\Http\Request; use OC\AppFramework\Utility\SimpleContainer; use OC\AppFramework\Utility\TimeFactory; 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\Contacts\ContactsMenu\ActionFactory; use OC\Diagnostics\EventLogger; @@ -115,6 +119,7 @@ use OCP\Contacts\ContactsMenu\IActionFactory; use OCP\Lock\ILockingProvider; use OCP\RichObjectStrings\IValidator; use OCP\Security\IContentSecurityPolicyManager; +use OCP\Share; use OCP\Share\IShareHelper; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -994,7 +999,15 @@ class Server extends ServerContainer implements IServerContainer { $this->registerAlias('ShareManager', \OCP\Share\IManager::class); $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); diff --git a/tests/lib/Collaboration/Collaborators/SearchTest.php b/tests/lib/Collaboration/Collaborators/SearchTest.php new file mode 100644 index 00000000000..80e6c7f0beb --- /dev/null +++ b/tests/lib/Collaboration/Collaborators/SearchTest.php @@ -0,0 +1,219 @@ + + * + * @author Arthur Schiwon + * + * @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 . + * + */ + +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, + ], + ]; + } +} -- cgit v1.2.3 From 3888b3a285734f8c3319689f5681dff3aa21f5e9 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 13 Sep 2017 14:32:19 +0200 Subject: adapt unit test for ensuring exception Signed-off-by: Arthur Schiwon --- apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'apps/files_sharing/tests') diff --git a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php index 529a8754b83..85b4f5ae35d 100644 --- a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php +++ b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php @@ -403,8 +403,8 @@ class ShareesAPIControllerTest extends TestCase { * @expectedException \OCP\AppFramework\OCS\OCSBadRequestException * @expectedExceptionMessage Missing itemType */ - public function testSearchShareesNoItemType() { - $this->invokePrivate($this->sharees, 'searchSharees', ['', null, [], [], 0, 0, false]); + public function testSearchNoItemType() { + $this->sharees->search('', null, 0, 0, [], false); } public function dataGetPaginationLink() { -- cgit v1.2.3 From a7a651f4a44be00b7be777baf4adfa76a8490c9f Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 13 Sep 2017 14:39:04 +0200 Subject: move splitUserRemote unit tests Signed-off-by: Arthur Schiwon --- .../tests/Controller/ShareesAPIControllerTest.php | 72 --------------------- .../Collaborators/RemotePluginTest.php | 75 ++++++++++++++++++++++ 2 files changed, 75 insertions(+), 72 deletions(-) (limited to 'apps/files_sharing/tests') diff --git a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php index 85b4f5ae35d..80cc66afaac 100644 --- a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php +++ b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php @@ -451,78 +451,6 @@ class ShareesAPIControllerTest extends TestCase { $this->assertEquals($expected, $this->invokePrivate($this->sharees, 'isV2')); } - /** - * @dataProvider dataTestSplitUserRemote - * - * @param string $remote - * @param string $expectedUser - * @param string $expectedUrl - */ - public function testSplitUserRemote($remote, $expectedUser, $expectedUrl) { - list($remoteUser, $remoteUrl) = $this->sharees->splitUserRemote($remote); - $this->assertSame($expectedUser, $remoteUser); - $this->assertSame($expectedUrl, $remoteUrl); - } - - public function dataTestSplitUserRemote() { - $userPrefix = ['user@name', 'username']; - $protocols = ['', 'http://', 'https://']; - $remotes = [ - 'localhost', - 'local.host', - 'dev.local.host', - 'dev.local.host/path', - 'dev.local.host/at@inpath', - '127.0.0.1', - '::1', - '::192.0.2.128', - '::192.0.2.128/at@inpath', - ]; - - $testCases = []; - foreach ($userPrefix as $user) { - foreach ($remotes as $remote) { - foreach ($protocols as $protocol) { - $baseUrl = $user . '@' . $protocol . $remote; - - $testCases[] = [$baseUrl, $user, $protocol . $remote]; - $testCases[] = [$baseUrl . '/', $user, $protocol . $remote]; - $testCases[] = [$baseUrl . '/index.php', $user, $protocol . $remote]; - $testCases[] = [$baseUrl . '/index.php/s/token', $user, $protocol . $remote]; - } - } - } - return $testCases; - } - - public function dataTestSplitUserRemoteError() { - return array( - // Invalid path - array('user@'), - - // Invalid user - array('@server'), - array('us/er@server'), - array('us:er@server'), - - // Invalid splitting - array('user'), - array(''), - array('us/erserver'), - array('us:erserver'), - ); - } - - /** - * @dataProvider dataTestSplitUserRemoteError - * - * @param string $id - * @expectedException \Exception - */ - public function testSplitUserRemoteError($id) { - $this->sharees->splitUserRemote($id); - } - /** * @dataProvider dataTestFixRemoteUrl * diff --git a/tests/lib/Collaboration/Collaborators/RemotePluginTest.php b/tests/lib/Collaboration/Collaborators/RemotePluginTest.php index 4856230bf69..78ef45d0146 100644 --- a/tests/lib/Collaboration/Collaborators/RemotePluginTest.php +++ b/tests/lib/Collaboration/Collaborators/RemotePluginTest.php @@ -103,6 +103,32 @@ class RemotePluginTest extends TestCase { $this->assertSame($reachedEnd, $moreResults); } + /** + * @dataProvider dataTestSplitUserRemote + * + * @param string $remote + * @param string $expectedUser + * @param string $expectedUrl + */ + public function testSplitUserRemote($remote, $expectedUser, $expectedUrl) { + $this->instantiatePlugin(); + + list($remoteUser, $remoteUrl) = $this->plugin->splitUserRemote($remote); + $this->assertSame($expectedUser, $remoteUser); + $this->assertSame($expectedUrl, $remoteUrl); + } + + /** + * @dataProvider dataTestSplitUserRemoteError + * + * @param string $id + * @expectedException \Exception + */ + public function testSplitUserRemoteError($id) { + $this->instantiatePlugin(); + $this->plugin->splitUserRemote($id); + } + public function dataGetRemote() { return [ ['test', [], true, ['remotes' => [], 'exact' => ['remotes' => []]], false, true], @@ -311,4 +337,53 @@ class RemotePluginTest extends TestCase { ], ]; } + + public function dataTestSplitUserRemote() { + $userPrefix = ['user@name', 'username']; + $protocols = ['', 'http://', 'https://']; + $remotes = [ + 'localhost', + 'local.host', + 'dev.local.host', + 'dev.local.host/path', + 'dev.local.host/at@inpath', + '127.0.0.1', + '::1', + '::192.0.2.128', + '::192.0.2.128/at@inpath', + ]; + + $testCases = []; + foreach ($userPrefix as $user) { + foreach ($remotes as $remote) { + foreach ($protocols as $protocol) { + $baseUrl = $user . '@' . $protocol . $remote; + + $testCases[] = [$baseUrl, $user, $protocol . $remote]; + $testCases[] = [$baseUrl . '/', $user, $protocol . $remote]; + $testCases[] = [$baseUrl . '/index.php', $user, $protocol . $remote]; + $testCases[] = [$baseUrl . '/index.php/s/token', $user, $protocol . $remote]; + } + } + } + return $testCases; + } + + public function dataTestSplitUserRemoteError() { + return array( + // Invalid path + array('user@'), + + // Invalid user + array('@server'), + array('us/er@server'), + array('us:er@server'), + + // Invalid splitting + array('user'), + array(''), + array('us/erserver'), + array('us:erserver'), + ); + } } -- cgit v1.2.3 From f2c8e1ed2f2e5c8c4801f9fd8122cc92f6850fa8 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 13 Sep 2017 14:40:04 +0200 Subject: remove unused method Signed-off-by: Arthur Schiwon --- .../lib/Controller/ShareesAPIController.php | 22 ---------------------- .../tests/Controller/ShareesAPIControllerTest.php | 21 --------------------- 2 files changed, 43 deletions(-) (limited to 'apps/files_sharing/tests') diff --git a/apps/files_sharing/lib/Controller/ShareesAPIController.php b/apps/files_sharing/lib/Controller/ShareesAPIController.php index 17d258b3a8f..3afa0f5d799 100644 --- a/apps/files_sharing/lib/Controller/ShareesAPIController.php +++ b/apps/files_sharing/lib/Controller/ShareesAPIController.php @@ -102,28 +102,6 @@ class ShareesAPIController extends OCSController { $this->collaboratorSearch = $collaboratorSearch; } - /** - * Strips away a potential file names and trailing slashes: - * - http://localhost - * - http://localhost/ - * - http://localhost/index.php - * - http://localhost/index.php/s/{shareToken} - * - * all return: http://localhost - * - * @param string $remote - * @return string - */ - protected function fixRemoteURL($remote) { - $remote = str_replace('\\', '/', $remote); - if ($fileNamePosition = strpos($remote, '/index.php')) { - $remote = substr($remote, 0, $fileNamePosition); - } - $remote = rtrim($remote, '/'); - - return $remote; - } - /** * @NoAdminRequired * diff --git a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php index 80cc66afaac..d300b0fcfca 100644 --- a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php +++ b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php @@ -450,25 +450,4 @@ class ShareesAPIControllerTest extends TestCase { $this->assertEquals($expected, $this->invokePrivate($this->sharees, 'isV2')); } - - /** - * @dataProvider dataTestFixRemoteUrl - * - * @param string $url - * @param string $expected - */ - public function testFixRemoteUrl($url, $expected) { - $this->assertSame($expected, - $this->invokePrivate($this->sharees, 'fixRemoteURL', [$url]) - ); - } - - public function dataTestFixRemoteUrl() { - return [ - ['http://localhost', 'http://localhost'], - ['http://localhost/', 'http://localhost'], - ['http://localhost/index.php', 'http://localhost'], - ['http://localhost/index.php/s/AShareToken', 'http://localhost'], - ]; - } } -- cgit v1.2.3 From c43685e6fc142da72a0a77587739986f29635447 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 13 Sep 2017 16:21:23 +0200 Subject: shareeAPIController unit test clean up and last fixes Signed-off-by: Arthur Schiwon --- .../lib/Controller/ShareesAPIController.php | 1 + .../tests/Controller/ShareesAPIControllerTest.php | 78 ++++++---------------- 2 files changed, 22 insertions(+), 57 deletions(-) (limited to 'apps/files_sharing/tests') diff --git a/apps/files_sharing/lib/Controller/ShareesAPIController.php b/apps/files_sharing/lib/Controller/ShareesAPIController.php index 3afa0f5d799..dcecf2c8e08 100644 --- a/apps/files_sharing/lib/Controller/ShareesAPIController.php +++ b/apps/files_sharing/lib/Controller/ShareesAPIController.php @@ -157,6 +157,7 @@ class ShareesAPIController extends OCSController { $shareTypes[] = Share::SHARE_TYPE_EMAIL; } + // FIXME: DI if (\OC::$server->getAppManager()->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) { $shareTypes[] = Share::SHARE_TYPE_CIRCLE; } diff --git a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php index d300b0fcfca..2b919f4849d 100644 --- a/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php +++ b/apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php @@ -25,14 +25,16 @@ namespace OCA\Files_Sharing\Tests\Controller; -use OC\Federation\CloudIdManager; use OCA\Files_Sharing\Controller\ShareesAPIController; use OCA\Files_Sharing\Tests\TestCase; use OCP\AppFramework\Http; use OCP\AppFramework\OCS\OCSBadRequestException; -use OCP\Federation\ICloudIdManager; -use OCP\Http\Client\IClientService; +use OCP\Collaboration\Collaborators\ISearch; +use OCP\IConfig; +use OCP\IRequest; +use OCP\IURLGenerator; use OCP\Share; +use OCP\Share\IManager; /** * Class ShareesTest @@ -45,74 +47,36 @@ class ShareesAPIControllerTest extends TestCase { /** @var ShareesAPIController */ protected $sharees; - /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject */ - protected $userManager; - - /** @var \OCP\IGroupManager|\PHPUnit_Framework_MockObject_MockObject */ - protected $groupManager; - - /** @var \OCP\Contacts\IManager|\PHPUnit_Framework_MockObject_MockObject */ - protected $contactsManager; - - /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject */ - protected $session; - - /** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */ + /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */ protected $request; - /** @var \OCP\Share\IManager|\PHPUnit_Framework_MockObject_MockObject */ + /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ protected $shareManager; - /** @var IClientService|\PHPUnit_Framework_MockObject_MockObject */ - private $clientService; - - /** @var ICloudIdManager */ - private $cloudIdManager; + /** @var ISearch|\PHPUnit_Framework_MockObject_MockObject */ + protected $collaboratorSearch; protected function setUp() { parent::setUp(); - $this->userManager = $this->getMockBuilder('OCP\IUserManager') - ->disableOriginalConstructor() - ->getMock(); - - $this->groupManager = $this->getMockBuilder('OCP\IGroupManager') - ->disableOriginalConstructor() - ->getMock(); + $this->request = $this->createMock(IRequest::class); + $this->shareManager = $this->createMock(IManager::class); - $this->contactsManager = $this->getMockBuilder('OCP\Contacts\IManager') - ->disableOriginalConstructor() - ->getMock(); - - $this->session = $this->getMockBuilder('OCP\IUserSession') - ->disableOriginalConstructor() - ->getMock(); - - $this->request = $this->getMockBuilder('OCP\IRequest') - ->disableOriginalConstructor() - ->getMock(); - - $this->shareManager = $this->getMockBuilder('OCP\Share\IManager') - ->disableOriginalConstructor() - ->getMock(); + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject $configMock */ + $configMock = $this->createMock(IConfig::class); - $this->clientService = $this->createMock(IClientService::class); + /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject $urlGeneratorMock */ + $urlGeneratorMock = $this->createMock(IURLGenerator::class); - $this->cloudIdManager = new CloudIdManager(); + $this->collaboratorSearch = $this->createMock(ISearch::class); $this->sharees = new ShareesAPIController( 'files_sharing', $this->request, - $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(), + $configMock, + $urlGeneratorMock, $this->shareManager, - $this->clientService, - $this->cloudIdManager + $this->collaboratorSearch ); } @@ -289,7 +253,7 @@ class ShareesAPIControllerTest extends TestCase { $this->collaboratorSearch->expects($this->once()) ->method('search') - ->with($search, $shareTypes, $this->anything(), $perPage, $this->invokePrivate($sharees, 'offset')) + ->with($search, $shareTypes, $this->anything(), $perPage, $perPage * ($page -1)) ->willReturn([[], false]); $sharees->expects($this->any()) @@ -404,7 +368,7 @@ class ShareesAPIControllerTest extends TestCase { * @expectedExceptionMessage Missing itemType */ public function testSearchNoItemType() { - $this->sharees->search('', null, 0, 0, [], false); + $this->sharees->search('', null, 1, 10, [], false); } public function dataGetPaginationLink() { -- cgit v1.2.3