123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- <?php
- /**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
- * @author Bjoern Schiessle <bjoern@schiessle.org>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Julius Härtl <jus@bitgrid.net>
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author Robin Appelman <robin@icewind.nl>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Thomas Citharel <nextcloud@tcit.fr>
- * @author Thomas Müller <thomas.mueller@tmit.eu>
- *
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
- */
- namespace OCA\Files_Sharing\Tests\Controller;
- use OCA\Files_Sharing\Controller\ShareesAPIController;
- use OCA\Files_Sharing\Tests\TestCase;
- use OCP\AppFramework\Http;
- use OCP\AppFramework\OCS\OCSBadRequestException;
- use OCP\Collaboration\Collaborators\ISearch;
- use OCP\IConfig;
- use OCP\IRequest;
- use OCP\IURLGenerator;
- use OCP\Share\IManager;
- use OCP\Share\IShare;
- use PHPUnit\Framework\MockObject\MockObject;
- /**
- * Class ShareesTest
- *
- * @group DB
- *
- * @package OCA\Files_Sharing\Tests\API
- */
- class ShareesAPIControllerTest extends TestCase {
- /** @var ShareesAPIController */
- protected $sharees;
- /** @var string */
- protected $uid;
- /** @var IRequest|MockObject */
- protected $request;
- /** @var IManager|MockObject */
- protected $shareManager;
- /** @var ISearch|MockObject */
- protected $collaboratorSearch;
- protected function setUp(): void {
- parent::setUp();
- $this->uid = 'test123';
- $this->request = $this->createMock(IRequest::class);
- $this->shareManager = $this->createMock(IManager::class);
- /** @var IConfig|MockObject $configMock */
- $configMock = $this->createMock(IConfig::class);
- /** @var IURLGenerator|MockObject $urlGeneratorMock */
- $urlGeneratorMock = $this->createMock(IURLGenerator::class);
- $this->collaboratorSearch = $this->createMock(ISearch::class);
- $this->sharees = new ShareesAPIController(
- $this->uid,
- 'files_sharing',
- $this->request,
- $configMock,
- $urlGeneratorMock,
- $this->shareManager,
- $this->collaboratorSearch
- );
- }
- public function dataSearch(): array {
- $noRemote = [IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_EMAIL];
- $allTypes = [IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_REMOTE, IShare::TYPE_REMOTE_GROUP, IShare::TYPE_EMAIL];
- return [
- [[], '', 'yes', true, true, true, $noRemote, false, true, true],
- // Test itemType
- [[
- 'search' => '',
- ], '', 'yes', true, true, true, $noRemote, false, true, true],
- [[
- 'search' => 'foobar',
- ], '', 'yes', true, true, true, $noRemote, false, true, true],
- [[
- 'search' => 0,
- ], '', 'yes', true, true, true, $noRemote, false, true, true],
- // Test itemType
- [[
- 'itemType' => '',
- ], '', 'yes', true, true, true, $noRemote, false, true, true],
- [[
- 'itemType' => 'folder',
- ], '', 'yes', true, true, true, $allTypes, false, true, true],
- [[
- 'itemType' => 0,
- ], '', 'yes', true, true , true, $noRemote, false, true, true],
- // Test shareType
- [[
- 'itemType' => 'call',
- ], '', 'yes', true, true, true, $noRemote, false, true, true],
- [[
- 'itemType' => 'call',
- ], '', 'yes', true, true, true, [0, 4], false, true, false],
- [[
- 'itemType' => 'folder',
- ], '', 'yes', true, true, true, $allTypes, false, true, true],
- [[
- 'itemType' => 'folder',
- 'shareType' => 0,
- ], '', 'yes', true, true, false, [0], false, true, true],
- [[
- 'itemType' => 'folder',
- 'shareType' => '0',
- ], '', 'yes', true, true, false, [0], false, true, true],
- [[
- 'itemType' => 'folder',
- 'shareType' => 1,
- ], '', 'yes', true, true, false, [1], false, true, true],
- [[
- 'itemType' => 'folder',
- 'shareType' => 12,
- ], '', 'yes', true, true, false, [], false, true, true],
- [[
- 'itemType' => 'folder',
- 'shareType' => 'foobar',
- ], '', 'yes', true, true, true, $allTypes, false, true, true],
- [[
- 'itemType' => 'folder',
- 'shareType' => [0, 1, 2],
- ], '', 'yes', false, false, false, [0, 1], false, true, true],
- [[
- 'itemType' => 'folder',
- 'shareType' => [0, 1],
- ], '', 'yes', false, false, false, [0, 1], false, true, true],
- [[
- 'itemType' => 'folder',
- 'shareType' => $allTypes,
- ], '', 'yes', true, true, true, $allTypes, false, true, true],
- [[
- 'itemType' => 'folder',
- 'shareType' => $allTypes,
- ], '', 'yes', false, false, false, [0, 1], false, true, true],
- [[
- 'itemType' => 'folder',
- 'shareType' => $allTypes,
- ], '', 'yes', true, false, false, [0, 6], false, true, false],
- [[
- 'itemType' => 'folder',
- 'shareType' => $allTypes,
- ], '', 'yes', false, false, true, [0, 4], false, true, false],
- [[
- 'itemType' => 'folder',
- 'shareType' => $allTypes,
- ], '', 'yes', true, true, false, [0, 6, 9], false, true, false],
- // Test pagination
- [[
- 'itemType' => 'folder',
- 'page' => 1,
- ], '', 'yes', true, true, true, $allTypes, false, true, true],
- [[
- 'itemType' => 'folder',
- 'page' => 10,
- ], '', 'yes', true, true, true, $allTypes, false, true, true],
- // Test perPage
- [[
- 'itemType' => 'folder',
- 'perPage' => 1,
- ], '', 'yes', true, true, true, $allTypes, false, true, true],
- [[
- 'itemType' => 'folder',
- 'perPage' => 10,
- ], '', 'yes', true, true, true, $allTypes, false, true, true],
- // Test $shareWithGroupOnly setting
- [[
- 'itemType' => 'folder',
- ], 'no', 'yes', true, true, true, $allTypes, false, true, true],
- [[
- 'itemType' => 'folder',
- ], 'yes', 'yes', true, true, true, $allTypes, true, true, true],
- // Test $shareeEnumeration setting
- [[
- 'itemType' => 'folder',
- ], 'no', 'yes', true, true, true, $allTypes, false, true, true],
- [[
- 'itemType' => 'folder',
- ], 'no', 'no', true, true, true, $allTypes, false, false, true],
- ];
- }
- /**
- * @dataProvider dataSearch
- *
- * @param array $getData
- * @param string $apiSetting
- * @param string $enumSetting
- * @param bool $remoteSharingEnabled
- * @param bool $isRemoteGroupSharingEnabled
- * @param bool $emailSharingEnabled
- * @param array $shareTypes
- * @param bool $shareWithGroupOnly
- * @param bool $shareeEnumeration
- * @param bool $allowGroupSharing
- * @throws OCSBadRequestException
- */
- public function testSearch(array $getData, string $apiSetting, string $enumSetting, bool $remoteSharingEnabled, bool $isRemoteGroupSharingEnabled, bool $emailSharingEnabled, array $shareTypes, bool $shareWithGroupOnly, bool $shareeEnumeration, bool $allowGroupSharing) {
- $search = $getData['search'] ?? '';
- $itemType = $getData['itemType'] ?? 'irrelevant';
- $page = $getData['page'] ?? 1;
- $perPage = $getData['perPage'] ?? 200;
- $shareType = $getData['shareType'] ?? null;
- /** @var IConfig|MockObject $config */
- $config = $this->createMock(IConfig::class);
- $config->expects($this->exactly(1))
- ->method('getAppValue')
- ->with($this->anything(), $this->anything(), $this->anything())
- ->willReturnMap([
- ['files_sharing', 'lookupServerEnabled', 'yes', 'yes'],
- ]);
- $this->shareManager->expects($this->once())
- ->method('allowGroupSharing')
- ->willReturn($allowGroupSharing);
- /** @var string */
- $uid = 'test123';
- /** @var IRequest|MockObject $request */
- $request = $this->createMock(IRequest::class);
- /** @var IURLGenerator|MockObject $urlGenerator */
- $urlGenerator = $this->createMock(IURLGenerator::class);
- /** @var MockObject|ShareesAPIController $sharees */
- $sharees = $this->getMockBuilder(ShareesAPIController::class)
- ->setConstructorArgs([
- $uid,
- 'files_sharing',
- $request,
- $config,
- $urlGenerator,
- $this->shareManager,
- $this->collaboratorSearch
- ])
- ->setMethods(['isRemoteSharingAllowed', 'shareProviderExists', 'isRemoteGroupSharingAllowed'])
- ->getMock();
- $expectedShareTypes = $shareTypes;
- sort($expectedShareTypes);
- $this->collaboratorSearch->expects($this->once())
- ->method('search')
- ->with($search, $expectedShareTypes, $this->anything(), $perPage, $perPage * ($page - 1))
- ->willReturn([[], false]);
- $sharees->expects($this->any())
- ->method('isRemoteSharingAllowed')
- ->with($itemType)
- ->willReturn($remoteSharingEnabled);
- $sharees->expects($this->any())
- ->method('isRemoteGroupSharingAllowed')
- ->with($itemType)
- ->willReturn($isRemoteGroupSharingEnabled);
- $this->shareManager->expects($this->any())
- ->method('shareProviderExists')
- ->willReturnCallback(function ($shareType) use ($emailSharingEnabled) {
- if ($shareType === IShare::TYPE_EMAIL) {
- return $emailSharingEnabled;
- } else {
- return false;
- }
- });
- $this->assertInstanceOf(Http\DataResponse::class, $sharees->search($search, $itemType, $page, $perPage, $shareType));
- }
- public function dataSearchInvalid(): array {
- return [
- // Test invalid pagination
- [[
- 'page' => 0,
- ], 'Invalid page'],
- [[
- 'page' => '0',
- ], 'Invalid page'],
- [[
- 'page' => -1,
- ], 'Invalid page'],
- // Test invalid perPage
- [[
- 'perPage' => 0,
- ], 'Invalid perPage argument'],
- [[
- 'perPage' => '0',
- ], 'Invalid perPage argument'],
- [[
- 'perPage' => -1,
- ], 'Invalid perPage argument'],
- ];
- }
- /**
- * @dataProvider dataSearchInvalid
- *
- * @param array $getData
- * @param string $message
- */
- public function testSearchInvalid($getData, $message) {
- $page = $getData['page'] ?? 1;
- $perPage = $getData['perPage'] ?? 200;
- /** @var IConfig|MockObject $config */
- $config = $this->createMock(IConfig::class);
- $config->expects($this->never())
- ->method('getAppValue');
- /** @var string */
- $uid = 'test123';
- /** @var IRequest|MockObject $request */
- $request = $this->createMock(IRequest::class);
- /** @var IURLGenerator|MockObject $urlGenerator */
- $urlGenerator = $this->createMock(IURLGenerator::class);
- /** @var MockObject|ShareesAPIController $sharees */
- $sharees = $this->getMockBuilder('\OCA\Files_Sharing\Controller\ShareesAPIController')
- ->setConstructorArgs([
- $uid,
- 'files_sharing',
- $request,
- $config,
- $urlGenerator,
- $this->shareManager,
- $this->collaboratorSearch
- ])
- ->setMethods(['isRemoteSharingAllowed'])
- ->getMock();
- $sharees->expects($this->never())
- ->method('isRemoteSharingAllowed');
- $this->collaboratorSearch->expects($this->never())
- ->method('search');
- try {
- $sharees->search('', null, $page, $perPage, null);
- $this->fail();
- } catch (OCSBadRequestException $e) {
- $this->assertEquals($message, $e->getMessage());
- }
- }
- public function dataIsRemoteSharingAllowed() {
- return [
- ['file', true],
- ['folder', true],
- ['', false],
- ['contacts', false],
- ];
- }
- /**
- * @dataProvider dataIsRemoteSharingAllowed
- *
- * @param string $itemType
- * @param bool $expected
- */
- public function testIsRemoteSharingAllowed($itemType, $expected) {
- $this->assertSame($expected, $this->invokePrivate($this->sharees, 'isRemoteSharingAllowed', [$itemType]));
- }
- public function testSearchNoItemType() {
- $this->expectException(\OCP\AppFramework\OCS\OCSBadRequestException::class);
- $this->expectExceptionMessage('Missing itemType');
- $this->sharees->search('', null, 1, 10, [], false);
- }
- public function dataGetPaginationLink() {
- return [
- [1, '/ocs/v1.php', ['perPage' => 2], '<?perPage=2&page=2>; rel="next"'],
- [10, '/ocs/v2.php', ['perPage' => 2], '<?perPage=2&page=11>; rel="next"'],
- ];
- }
- /**
- * @dataProvider dataGetPaginationLink
- *
- * @param int $page
- * @param string $scriptName
- * @param array $params
- * @param array $expected
- */
- public function testGetPaginationLink($page, $scriptName, $params, $expected) {
- $this->request->expects($this->once())
- ->method('getScriptName')
- ->willReturn($scriptName);
- $this->assertEquals($expected, $this->invokePrivate($this->sharees, 'getPaginationLink', [$page, $params]));
- }
- public function dataIsV2() {
- return [
- ['/ocs/v1.php', false],
- ['/ocs/v2.php', true],
- ];
- }
- /**
- * @dataProvider dataIsV2
- *
- * @param string $scriptName
- * @param bool $expected
- */
- public function testIsV2($scriptName, $expected) {
- $this->request->expects($this->once())
- ->method('getScriptName')
- ->willReturn($scriptName);
- $this->assertEquals($expected, $this->invokePrivate($this->sharees, 'isV2'));
- }
- }
|