Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions apps/dav/tests/unit/Connector/Sabre/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@
use Test\Traits\MountProviderTrait;
use Test\Traits\UserTrait;

/**
* Internal helper to mock legacy hook receiver.
*/
interface EventHandlerMock {
public function writeCallback(): void;
public function postWriteCallback(): void;
}

/**
* Class File
*
Expand Down Expand Up @@ -822,9 +830,7 @@ public function testPutLocking(): void {

$wasLockedPre = false;
$wasLockedPost = false;
$eventHandler = $this->getMockBuilder(\stdclass::class)
->addMethods(['writeCallback', 'postWriteCallback'])
->getMock();
$eventHandler = $this->createMock(EventHandlerMock::class);

// both pre and post hooks might need access to the file,
// so only shared lock is acceptable
Expand Down
2 changes: 1 addition & 1 deletion apps/files_sharing/lib/Controller/ShareAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,7 @@ private function getRoomShareHelper() {
throw new QueryException();
}

return $this->serverContainer->get('\OCA\Talk\Share\Helper\ShareAPIController');
return $this->serverContainer->get(\OCA\Talk\Share\Helper\ShareAPIController::class);
}

/**
Expand Down
83 changes: 38 additions & 45 deletions apps/files_sharing/tests/Controller/ShareAPIControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace OCA\Files_Sharing\Tests\Controller;

use OC\Files\Storage\Wrapper\Wrapper;
use OC\Session\Internal;
use OCA\Federation\TrustedServers;
use OCA\Files_Sharing\Controller\ShareAPIController;
use OCA\Files_Sharing\External\Storage;
Expand All @@ -26,6 +27,7 @@
use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Mount\IShareOwnerlessMount;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\Storage\IStorage;
use OCP\IAppConfig;
Expand Down Expand Up @@ -59,6 +61,16 @@
use Test\TestCase;
use Test\Traits\EmailValidatorTrait;

/**
* Internal mock to allow mocking the Talk controller used for room shares,
* needed when Talk is not installed during tests (PHPUnit does not allow mocking of non-existing classes).
*/
interface InternalTalkShareAPIController {
public function formatShare(IShare $share): array;
public function canAccessShare(IShare $share, string $user): bool;
public function createShare(IShare $share, string $shareWith, int $permissions, string $expireDate): void;
}

/**
* Class ShareAPIControllerTest
*
Expand Down Expand Up @@ -1831,25 +1843,15 @@ public function testCanAccessRoomShare(
->with('spreed')
->willReturn(true);

// 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')
$this->mockTalkController()
->method('canAccessShare')
->with($share, $this->currentUser)
->willReturn($canAccessShareByHelper);

$this->serverContainer->method('get')
->with('\OCA\Talk\Share\Helper\ShareAPIController')
->willReturn($helper);
}

$this->assertEquals($expected, $this->invokePrivate($this->ocs, 'canAccessShare', [$share]));
}


public function testCreateShareNoPath(): void {
$this->expectException(OCSNotFoundException::class);
$this->expectExceptionMessage('Please specify a file or folder path');
Expand Down Expand Up @@ -2663,13 +2665,8 @@ public function testCreateShareRoom(): void {
->with('spreed')
->willReturn(true);

// 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')
$this->mockTalkController()
->method('createShare')
->with(
$share,
'recipientRoom',
Expand All @@ -2684,10 +2681,6 @@ function ($share): void {
}
);

$this->serverContainer->method('get')
->with('\OCA\Talk\Share\Helper\ShareAPIController')
->willReturn($helper);

$this->shareManager->method('createShare')
->with($this->callback(function (IShare $share) use ($path) {
return $share->getNode() === $path
Expand Down Expand Up @@ -2772,13 +2765,8 @@ public function testCreateShareRoomHelperThrowException(): void {
->with('spreed')
->willReturn(true);

// 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')
$this->mockTalkController()
->method('createShare')
->with(
$share,
'recipientRoom',
Expand All @@ -2790,10 +2778,6 @@ function ($share): void {
}
);

$this->serverContainer->method('get')
->with('\OCA\Talk\Share\Helper\ShareAPIController')
->willReturn($helper);

$this->shareManager->expects($this->never())->method('createShare');

$ocs->createShare('valid-path', Constants::PERMISSION_ALL, IShare::TYPE_ROOM, 'recipientRoom');
Expand Down Expand Up @@ -4941,6 +4925,7 @@ function ($user) {
$expects['attributes'] = \json_encode($shareParams['attributes']);
}
if (isset($shareParams['node'])) {
/** @var Node&MockObject */
$node = $this->createMock($shareParams['node']['class']);

$node->method('getMimeType')->willReturn($shareParams['node']['mimeType']);
Expand Down Expand Up @@ -5214,22 +5199,13 @@ public function testFormatRoomShare(array $expects, bool $helperAvailable, array
->with('spreed')
->willReturn(true);

// 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')
$helper = $this->mockTalkController();
$helper ->method('formatShare')
->with($share)
->willReturn($formatShareByHelper);
$helper->method('canAccessShare')
->with($share)
->willReturn(true);

$this->serverContainer->method('get')
->with('\OCA\Talk\Share\Helper\ShareAPIController')
->willReturn($helper);
}

$result = $this->invokePrivate($this->ocs, 'formatShare', [$share]);
Expand Down Expand Up @@ -5647,4 +5623,21 @@ public function testWrapperStorageUnwrapped(): void {

$this->invokePrivate($ocs, 'checkInheritedAttributes', [$share]);
}

/**
* Helper to allow testing Talk integration even if Talk
* is not available during tests.
*/
private function mockTalkController(): MockObject {
if (class_exists(\OCA\Talk\Share\Helper\ShareAPIController::class)) {
$helper = $this->createMock(\OCA\Talk\Share\Helper\ShareAPIController::class);
} else {
$helper = $this->createMock(InternalTalkShareAPIController::class);
}

$this->serverContainer->method('get')
->with(\OCA\Talk\Share\Helper\ShareAPIController::class)
->willReturn($helper);
return $helper;
}
}
6 changes: 0 additions & 6 deletions build/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1471,12 +1471,6 @@
<code><![CDATA[Circles]]></code>
<code><![CDATA[Circles]]></code>
</UndefinedClass>
<UndefinedDocblockClass>
<code><![CDATA[$this->getRoomShareHelper()]]></code>
<code><![CDATA[$this->getRoomShareHelper()]]></code>
<code><![CDATA[$this->getRoomShareHelper()]]></code>
<code><![CDATA[\OCA\Talk\Share\Helper\ShareAPIController]]></code>
</UndefinedDocblockClass>
</file>
<file src="apps/files_sharing/lib/Controller/ShareController.php">
<DeprecatedClass>
Expand Down
44 changes: 44 additions & 0 deletions build/stubs/oca_talk_share_helper_shareapicontroller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* SPDX-FileCopyrightText: Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Talk\Share\Helper;

/**
* Helper of OCA\Files_Sharing\Controller\ShareAPIController for room shares.
*
* The methods of this class are called from the ShareAPIController to perform
* actions or checks specific to room shares.
*/
class ShareAPIController
{
public function __construct(protected string $userId, protected \OCA\Talk\Manager $manager, protected \OCA\Talk\Service\ParticipantService $participantService, protected \OCP\AppFramework\Utility\ITimeFactory $timeFactory, protected \OCP\IL10N $l, protected \OCP\IURLGenerator $urlGenerator)
{
}
/**
* Formats the specific fields of a room share for OCS output.
*
* The returned fields override those set by the main ShareAPIController.
*/
public function formatShare(\OCP\Share\IShare $share): array
{
}
/**
* Prepares the given share to be passed to OC\Share20\Manager::createShare.
*
* @throws OCSNotFoundException
*/
public function createShare(\OCP\Share\IShare $share, string $shareWith, int $permissions, string $expireDate): void
{
}
/**
* Returns whether the given user can access the given room share or not.
*
* A user can access a room share only if they are a participant of the room.
*/
public function canAccessShare(\OCP\Share\IShare $share, string $user): bool
{
}
}
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<file name="3rdparty/sabre/uri/lib/functions.php" />
<file name="build/stubs/app_api.php" />
<file name="build/stubs/php-polyfill.php" />
<file name="build/stubs/oca_talk_share_helper_shareapicontroller.php" />
</stubs>
<issueHandlers>
<LessSpecificReturnStatement errorLevel="error"/>
Expand Down
Loading