aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_sharing/tests/Controller
diff options
context:
space:
mode:
Diffstat (limited to 'apps/files_sharing/tests/Controller')
-rw-r--r--apps/files_sharing/tests/Controller/ShareAPIControllerTest.php143
-rw-r--r--apps/files_sharing/tests/Controller/ShareControllerTest.php24
-rw-r--r--apps/files_sharing/tests/Controller/ShareInfoControllerTest.php21
3 files changed, 126 insertions, 62 deletions
diff --git a/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php b/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php
index 02c133ee5d1..f245f0a25c7 100644
--- a/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php
+++ b/apps/files_sharing/tests/Controller/ShareAPIControllerTest.php
@@ -1542,84 +1542,129 @@ class ShareAPIControllerTest extends TestCase {
$this->assertEquals($expected, $result->getData());
}
- public function testCanAccessShare(): void {
- $share = $this->getMockBuilder(IShare::class)->getMock();
+ public function testCanAccessShareAsOwner(): void {
+ $share = $this->createMock(IShare::class);
$share->method('getShareOwner')->willReturn($this->currentUser);
$this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
+ }
- $share = $this->getMockBuilder(IShare::class)->getMock();
+ public function testCanAccessShareAsSharer(): void {
+ $share = $this->createMock(IShare::class);
$share->method('getSharedBy')->willReturn($this->currentUser);
$this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
+ }
- $share = $this->getMockBuilder(IShare::class)->getMock();
+ public function testCanAccessShareAsSharee(): void {
+ $share = $this->createMock(IShare::class);
$share->method('getShareType')->willReturn(IShare::TYPE_USER);
$share->method('getSharedWith')->willReturn($this->currentUser);
$this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
+ }
- $file = $this->getMockBuilder(File::class)->getMock();
+ public function testCannotAccessLinkShare(): void {
+ $share = $this->createMock(IShare::class);
+ $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
+ $share->method('getNodeId')->willReturn(42);
- $userFolder = $this->getMockBuilder(Folder::class)->getMock();
+ $userFolder = $this->createMock(Folder::class);
$this->rootFolder->method('getUserFolder')
->with($this->currentUser)
->willReturn($userFolder);
+ $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
+ }
+
+ /**
+ * @dataProvider dataCanAccessShareWithPermissions
+ */
+ public function testCanAccessShareWithPermissions(int $permissions, bool $expected): void {
+ $share = $this->createMock(IShare::class);
+ $share->method('getShareType')->willReturn(IShare::TYPE_USER);
+ $share->method('getSharedWith')->willReturn($this->createMock(IUser::class));
+ $share->method('getNodeId')->willReturn(42);
+
+ $file = $this->createMock(File::class);
+
+ $userFolder = $this->getMockBuilder(Folder::class)->getMock();
$userFolder->method('getFirstNodeById')
->with($share->getNodeId())
->willReturn($file);
$userFolder->method('getById')
->with($share->getNodeId())
->willReturn([$file]);
+ $this->rootFolder->method('getUserFolder')
+ ->with($this->currentUser)
+ ->willReturn($userFolder);
$file->method('getPermissions')
- ->will($this->onConsecutiveCalls(Constants::PERMISSION_SHARE, Constants::PERMISSION_READ));
+ ->willReturn($permissions);
- // getPermissions -> share
- $share = $this->getMockBuilder(IShare::class)->getMock();
- $share->method('getShareType')->willReturn(IShare::TYPE_USER);
- $share->method('getSharedWith')->willReturn($this->getMockBuilder(IUser::class)->getMock());
- $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
+ if ($expected) {
+ $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
+ } else {
+ $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
+ }
+ }
- // getPermissions -> read
- $share = $this->getMockBuilder(IShare::class)->getMock();
- $share->method('getShareType')->willReturn(IShare::TYPE_USER);
- $share->method('getSharedWith')->willReturn($this->getMockBuilder(IUser::class)->getMock());
- $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
+ public static function dataCanAccessShareWithPermissions(): array {
+ return [
+ [Constants::PERMISSION_SHARE, true],
+ [Constants::PERMISSION_READ, false],
+ [Constants::PERMISSION_READ | Constants::PERMISSION_SHARE, true],
+ ];
+ }
- $share = $this->getMockBuilder(IShare::class)->getMock();
+ /**
+ * @dataProvider dataCanAccessShareAsGroupMember
+ */
+ public function testCanAccessShareAsGroupMember(string $group, bool $expected): void {
+ $share = $this->createMock(IShare::class);
$share->method('getShareType')->willReturn(IShare::TYPE_GROUP);
- $share->method('getSharedWith')->willReturn('group');
+ $share->method('getSharedWith')->willReturn($group);
+ $share->method('getNodeId')->willReturn(42);
+
+ $file = $this->createMock(File::class);
+
+ $userFolder = $this->createMock(Folder::class);
+ $userFolder->method('getFirstNodeById')
+ ->with($share->getNodeId())
+ ->willReturn($file);
+ $userFolder->method('getById')
+ ->with($share->getNodeId())
+ ->willReturn([$file]);
+ $this->rootFolder->method('getUserFolder')
+ ->with($this->currentUser)
+ ->willReturn($userFolder);
$user = $this->createMock(IUser::class);
$this->userManager->method('get')
->with($this->currentUser)
->willReturn($user);
- $group = $this->getMockBuilder(IGroup::class)->getMock();
+ $group = $this->createMock(IGroup::class);
$group->method('inGroup')->with($user)->willReturn(true);
- $group2 = $this->getMockBuilder(IGroup::class)->getMock();
+ $group2 = $this->createMock(IGroup::class);
$group2->method('inGroup')->with($user)->willReturn(false);
$this->groupManager->method('get')->willReturnMap([
['group', $group],
['group2', $group2],
- ['groupnull', null],
+ ['group-null', null],
]);
- $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
- $share = $this->createMock(IShare::class);
- $share->method('getShareType')->willReturn(IShare::TYPE_GROUP);
- $share->method('getSharedWith')->willReturn('group2');
- $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
-
- // null group
- $share = $this->createMock(IShare::class);
- $share->method('getShareType')->willReturn(IShare::TYPE_GROUP);
- $share->method('getSharedWith')->willReturn('groupnull');
- $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
+ if ($expected) {
+ $this->assertTrue($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
+ } else {
+ $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
+ }
+ }
- $share = $this->createMock(IShare::class);
- $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
- $this->assertFalse($this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
+ public static function dataCanAccessShareAsGroupMember(): array {
+ return [
+ ['group', true],
+ ['group2', false],
+ ['group-null', false],
+ ];
}
public function dataCanAccessRoomShare() {
@@ -1675,8 +1720,11 @@ class ShareAPIControllerTest extends TestCase {
->with('spreed')
->willReturn(true);
- $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
- ->setMethods(['canAccessShare'])
+ // This is not possible anymore with PHPUnit 10+
+ // as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
+ // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
+ $helper = $this->getMockBuilder(\stdClass::class)
+ ->addMethods(['canAccessShare'])
->getMock();
$helper->method('canAccessShare')
->with($share, $this->currentUser)
@@ -2492,8 +2540,11 @@ class ShareAPIControllerTest extends TestCase {
->with('spreed')
->willReturn(true);
- $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
- ->setMethods(['createShare'])
+ // This is not possible anymore with PHPUnit 10+
+ // as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
+ // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
+ $helper = $this->getMockBuilder(\stdClass::class)
+ ->addMethods(['createShare'])
->getMock();
$helper->method('createShare')
->with(
@@ -2598,7 +2649,10 @@ class ShareAPIControllerTest extends TestCase {
->with('spreed')
->willReturn(true);
- $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
+ // This is not possible anymore with PHPUnit 10+
+ // as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
+ // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
+ $helper = $this->getMockBuilder(\stdClass::class)
->addMethods(['createShare'])
->getMock();
$helper->method('createShare')
@@ -5093,8 +5147,11 @@ class ShareAPIControllerTest extends TestCase {
->with('spreed')
->willReturn(true);
- $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
- ->setMethods(['formatShare', 'canAccessShare'])
+ // This is not possible anymore with PHPUnit 10+
+ // as `setMethods` was removed and now real reflection is used, thus the class needs to exist.
+ // $helper = $this->getMockBuilder('\OCA\Talk\Share\Helper\ShareAPIController')
+ $helper = $this->getMockBuilder(\stdClass::class)
+ ->addMethods(['formatShare', 'canAccessShare'])
->getMock();
$helper->method('formatShare')
->with($share)
diff --git a/apps/files_sharing/tests/Controller/ShareControllerTest.php b/apps/files_sharing/tests/Controller/ShareControllerTest.php
index 58cbb4e0b82..a6bef1bed56 100644
--- a/apps/files_sharing/tests/Controller/ShareControllerTest.php
+++ b/apps/files_sharing/tests/Controller/ShareControllerTest.php
@@ -261,8 +261,12 @@ class ShareControllerTest extends \Test\TestCase {
['files_sharing.sharecontroller.showShare', ['token' => 'token'], 'shareUrl'],
// this share is not an image to the default preview is used
['files_sharing.PublicPreview.getPreview', ['x' => 256, 'y' => 256, 'file' => $share->getTarget(), 'token' => 'token'], 'previewUrl'],
- // for the direct link
- ['files_sharing.sharecontroller.downloadShare', ['token' => 'token', 'filename' => $filename ], 'downloadUrl'],
+ ]);
+
+ $this->urlGenerator->expects($this->once())
+ ->method('getAbsoluteURL')
+ ->willReturnMap([
+ ['/public.php/dav/files/token/?accept=zip', 'downloadUrl'],
]);
$this->previewManager->method('isMimeSupported')->with('text/plain')->willReturn(true);
@@ -395,6 +399,8 @@ class ShareControllerTest extends \Test\TestCase {
->setPassword('password')
->setShareOwner('ownerUID')
->setSharedBy('initiatorUID')
+ ->setNote('The note')
+ ->setLabel('A label')
->setNode($file)
->setTarget("/$filename")
->setToken('token');
@@ -474,6 +480,8 @@ class ShareControllerTest extends \Test\TestCase {
'disclaimer' => 'My disclaimer text',
'owner' => 'ownerUID',
'ownerDisplayName' => 'ownerDisplay',
+ 'note' => 'The note',
+ 'label' => 'A label',
];
$response = $this->shareController->showShare();
@@ -483,9 +491,9 @@ class ShareControllerTest extends \Test\TestCase {
$csp = new ContentSecurityPolicy();
$csp->addAllowedFrameDomain('\'self\'');
$expectedResponse = new PublicTemplateResponse('files', 'index');
- $expectedResponse->setParams(['pageTitle' => $filename]);
+ $expectedResponse->setParams(['pageTitle' => 'A label']);
$expectedResponse->setContentSecurityPolicy($csp);
- $expectedResponse->setHeaderTitle($filename);
+ $expectedResponse->setHeaderTitle('A label');
$expectedResponse->setHeaderDetails('shared by ownerDisplay');
$expectedResponse->setHeaderActions([
new LinkMenuAction($this->l10n->t('Direct link'), 'icon-public', 'shareUrl'),
@@ -552,8 +560,12 @@ class ShareControllerTest extends \Test\TestCase {
['files_sharing.sharecontroller.showShare', ['token' => 'token'], 'shareUrl'],
// this share is not an image to the default preview is used
['files_sharing.PublicPreview.getPreview', ['x' => 256, 'y' => 256, 'file' => $share->getTarget(), 'token' => 'token'], 'previewUrl'],
- // for the direct link
- ['files_sharing.sharecontroller.downloadShare', ['token' => 'token', 'filename' => $filename ], 'downloadUrl'],
+ ]);
+
+ $this->urlGenerator->expects($this->once())
+ ->method('getAbsoluteURL')
+ ->willReturnMap([
+ ['/public.php/dav/files/token/?accept=zip', 'downloadUrl'],
]);
$this->previewManager->method('isMimeSupported')->with('text/plain')->willReturn(true);
diff --git a/apps/files_sharing/tests/Controller/ShareInfoControllerTest.php b/apps/files_sharing/tests/Controller/ShareInfoControllerTest.php
index f2df74fd01b..571647829f2 100644
--- a/apps/files_sharing/tests/Controller/ShareInfoControllerTest.php
+++ b/apps/files_sharing/tests/Controller/ShareInfoControllerTest.php
@@ -15,15 +15,13 @@ use OCP\IRequest;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager as ShareManager;
use OCP\Share\IShare;
+use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class ShareInfoControllerTest extends TestCase {
- /** @var ShareInfoController */
- private $controller;
-
- /** @var ShareManager|\PHPUnit\Framework\MockObject\MockObject */
- private $shareManager;
+ protected ShareInfoController $controller;
+ protected ShareManager&MockObject $shareManager;
protected function setUp(): void {
@@ -31,14 +29,11 @@ class ShareInfoControllerTest extends TestCase {
$this->shareManager = $this->createMock(ShareManager::class);
- $this->controller = $this->getMockBuilder(ShareInfoController::class)
- ->setConstructorArgs([
- 'files_sharing',
- $this->createMock(IRequest::class),
- $this->shareManager
- ])
- ->setMethods(['addROWrapper'])
- ->getMock();
+ $this->controller = new ShareInfoController(
+ 'files_sharing',
+ $this->createMock(IRequest::class),
+ $this->shareManager
+ );
}
public function testNoShare(): void {