123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519 |
- <?php
- /**
- * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
- *
- * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- namespace Test\Collaboration\Collaborators;
- use OC\Collaboration\Collaborators\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\IShare;
- 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;
- protected function setUp(): void {
- 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
- * @param bool $hide
- * @return IGroup|\PHPUnit\Framework\MockObject\MockObject
- */
- protected function getGroupMock($gid, $displayName = null, $hide = false) {
- $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);
- $group->method('hideFromCollaboration')
- ->willReturn($hide);
- return $group;
- }
- public function dataGetGroups(): array {
- return [
- ['test', false, true, false, [], [], [], [], true, false],
- ['test', false, false, false, [], [], [], [], true, false],
- // group sharing disabled
- ['test', false, true, true, [], [], [], [], false, false],
- // group without display name
- [
- 'test', false, true, false,
- [$this->getGroupMock('test1')],
- [],
- [],
- [['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
- true,
- false,
- ],
- // group with display name, search by id
- [
- 'test', false, true, false,
- [$this->getGroupMock('test1', 'Test One')],
- [],
- [],
- [['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
- true,
- false,
- ],
- // group with display name, search by display name
- [
- 'one', false, true, false,
- [$this->getGroupMock('test1', 'Test One')],
- [],
- [],
- [['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
- true,
- false,
- ],
- // group with display name, search by display name, exact expected
- [
- 'Test One', false, true, false,
- [$this->getGroupMock('test1', 'Test One')],
- [],
- [['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
- [],
- true,
- false,
- ],
- [
- 'test', false, false, false,
- [$this->getGroupMock('test1')],
- [],
- [],
- [],
- true,
- false,
- ],
- [
- 'test', false, true, false,
- [
- $this->getGroupMock('test'),
- $this->getGroupMock('test1'),
- ],
- [],
- [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
- [['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
- false,
- false,
- ],
- [
- 'test', false, false, false,
- [
- $this->getGroupMock('test'),
- $this->getGroupMock('test1'),
- ],
- [],
- [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
- [],
- true,
- false,
- ],
- [
- 'test', false, true, false,
- [
- $this->getGroupMock('test0'),
- $this->getGroupMock('test1'),
- ],
- [],
- [],
- [
- ['label' => 'test0', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test0']],
- ['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']],
- ],
- false,
- null,
- ],
- [
- 'test', false, false, false,
- [
- $this->getGroupMock('test0'),
- $this->getGroupMock('test1'),
- ],
- [],
- [],
- [],
- true,
- null,
- ],
- [
- 'test', false, true, false,
- [
- $this->getGroupMock('test0'),
- $this->getGroupMock('test1'),
- ],
- [],
- [
- ['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']],
- ],
- [
- ['label' => 'test0', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test0']],
- ['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']],
- ],
- false,
- $this->getGroupMock('test'),
- ],
- [
- 'test', false, false, false,
- [
- $this->getGroupMock('test0'),
- $this->getGroupMock('test1'),
- ],
- [],
- [
- ['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']],
- ],
- [],
- true,
- $this->getGroupMock('test'),
- ],
- ['test', true, true, false, [], [], [], [], true, false],
- ['test', true, false, false, [], [], [], [], true, false],
- [
- 'test', true, true, false,
- [
- $this->getGroupMock('test1'),
- $this->getGroupMock('test2'),
- ],
- [$this->getGroupMock('test1')],
- [],
- [['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
- false,
- false,
- ],
- [
- 'test', true, false, false,
- [
- $this->getGroupMock('test1'),
- $this->getGroupMock('test2'),
- ],
- [$this->getGroupMock('test1')],
- [],
- [],
- true,
- false,
- ],
- [
- 'test', true, true, false,
- [
- $this->getGroupMock('test'),
- $this->getGroupMock('test1'),
- ],
- [$this->getGroupMock('test')],
- [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
- [],
- false,
- false,
- ],
- [
- 'test', true, false, false,
- [
- $this->getGroupMock('test'),
- $this->getGroupMock('test1'),
- ],
- [$this->getGroupMock('test')],
- [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
- [],
- true,
- false,
- ],
- [
- 'test', true, true, false,
- [
- $this->getGroupMock('test'),
- $this->getGroupMock('test1'),
- ],
- [$this->getGroupMock('test1')],
- [],
- [['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
- false,
- false,
- ],
- [
- 'test', true, false, false,
- [
- $this->getGroupMock('test'),
- $this->getGroupMock('test1'),
- ],
- [$this->getGroupMock('test1')],
- [],
- [],
- true,
- false,
- ],
- [
- 'test', true, true, false,
- [
- $this->getGroupMock('test'),
- $this->getGroupMock('test1'),
- ],
- [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
- [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
- [['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']]],
- false,
- false,
- ],
- [
- 'test', true, false, false,
- [
- $this->getGroupMock('test'),
- $this->getGroupMock('test1'),
- ],
- [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
- [['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']]],
- [],
- true,
- false,
- ],
- [
- 'test', true, true, false,
- [
- $this->getGroupMock('test0'),
- $this->getGroupMock('test1'),
- ],
- [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
- [],
- [
- ['label' => 'test0', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test0']],
- ['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']],
- ],
- false,
- null,
- ],
- [
- 'test', true, false, false,
- [
- $this->getGroupMock('test0'),
- $this->getGroupMock('test1'),
- ],
- [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
- [],
- [],
- true,
- null,
- ],
- [
- 'test', true, true, false,
- [
- $this->getGroupMock('test0'),
- $this->getGroupMock('test1'),
- ],
- [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
- [
- ['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']],
- ],
- [
- ['label' => 'test0', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test0']],
- ['label' => 'test1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test1']],
- ],
- false,
- $this->getGroupMock('test'),
- ],
- [
- 'test', true, false, false,
- [
- $this->getGroupMock('test0'),
- $this->getGroupMock('test1'),
- ],
- [$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
- [
- ['label' => 'test', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'test']],
- ],
- [],
- true,
- $this->getGroupMock('test'),
- ],
- [
- 'test', false, false, false,
- [
- $this->getGroupMock('test', null, true),
- $this->getGroupMock('test1'),
- ],
- [],
- [],
- [],
- true,
- false,
- ],
- ];
- }
- /**
- * @dataProvider dataGetGroups
- *
- * @param string $searchTerm
- * @param bool $shareWithGroupOnly
- * @param bool $shareeEnumeration
- * @param bool $groupSharingDisabled
- * @param array $groupResponse
- * @param array $userGroupsResponse
- * @param array $exactExpected
- * @param array $expected
- * @param bool $reachedEnd
- * @param bool|IGroup $singleGroup
- */
- public function testSearch(
- string $searchTerm,
- bool $shareWithGroupOnly,
- bool $shareeEnumeration,
- bool $groupSharingDisabled,
- array $groupResponse,
- array $userGroupsResponse,
- array $exactExpected,
- array $expected,
- bool $reachedEnd,
- $singleGroup
- ) {
- $this->config->expects($this->any())
- ->method('getAppValue')
- ->willReturnCallback(
- function ($appName, $key, $default) use ($shareWithGroupOnly, $shareeEnumeration, $groupSharingDisabled) {
- if ($appName !== 'core') {
- return $default;
- }
- switch ($key) {
- case 'shareapi_only_share_with_group_members':
- return $shareWithGroupOnly ? 'yes' : 'no';
- case 'shareapi_allow_share_dialog_user_enumeration':
- return $shareeEnumeration ? 'yes' : 'no';
- case 'shareapi_allow_group_sharing':
- return $groupSharingDisabled ? 'no' : 'yes';
- default:
- return $default;
- }
- }
- );
- $this->instantiatePlugin();
- if (!$groupSharingDisabled) {
- $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();
- if (!$groupSharingDisabled) {
- $this->assertEquals($exactExpected, $result['exact']['groups']);
- $this->assertEquals($expected, $result['groups']);
- }
- $this->assertSame($reachedEnd, $moreResults);
- }
- }
|