diff options
Diffstat (limited to 'apps/dav/tests/unit/DAV')
-rw-r--r-- | apps/dav/tests/unit/DAV/AnonymousOptionsTest.php | 6 | ||||
-rw-r--r-- | apps/dav/tests/unit/DAV/BrowserErrorPagePluginTest.php | 22 | ||||
-rw-r--r-- | apps/dav/tests/unit/DAV/CustomPropertiesBackendTest.php | 58 | ||||
-rw-r--r-- | apps/dav/tests/unit/DAV/GroupPrincipalTest.php | 48 | ||||
-rw-r--r-- | apps/dav/tests/unit/DAV/Listener/UserEventsListenerTest.php | 74 | ||||
-rw-r--r-- | apps/dav/tests/unit/DAV/Sharing/BackendTest.php | 16 | ||||
-rw-r--r-- | apps/dav/tests/unit/DAV/Sharing/PluginTest.php | 22 | ||||
-rw-r--r-- | apps/dav/tests/unit/DAV/SystemPrincipalBackendTest.php | 54 | ||||
-rw-r--r-- | apps/dav/tests/unit/DAV/ViewOnlyPluginTest.php | 56 |
9 files changed, 166 insertions, 190 deletions
diff --git a/apps/dav/tests/unit/DAV/AnonymousOptionsTest.php b/apps/dav/tests/unit/DAV/AnonymousOptionsTest.php index ea5450391e8..c99ebf327c8 100644 --- a/apps/dav/tests/unit/DAV/AnonymousOptionsTest.php +++ b/apps/dav/tests/unit/DAV/AnonymousOptionsTest.php @@ -1,9 +1,11 @@ <?php + +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCA\DAV\tests\unit\DAV; +namespace OCA\DAV\Tests\unit\DAV; use OCA\DAV\Connector\Sabre\AnonymousOptionsPlugin; use Sabre\DAV\Auth\Backend\BasicCallBack; @@ -14,7 +16,7 @@ use Sabre\HTTP\Sapi; use Test\TestCase; class AnonymousOptionsTest extends TestCase { - private function sendRequest($method, $path, $userAgent = '') { + private function sendRequest(string $method, string $path, string $userAgent = '') { $server = new Server(); $server->addPlugin(new AnonymousOptionsPlugin()); $server->addPlugin(new Plugin(new BasicCallBack(function () { diff --git a/apps/dav/tests/unit/DAV/BrowserErrorPagePluginTest.php b/apps/dav/tests/unit/DAV/BrowserErrorPagePluginTest.php index 088330cecff..0e82ef0a3ae 100644 --- a/apps/dav/tests/unit/DAV/BrowserErrorPagePluginTest.php +++ b/apps/dav/tests/unit/DAV/BrowserErrorPagePluginTest.php @@ -1,5 +1,6 @@ <?php +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -8,25 +9,22 @@ namespace OCA\DAV\Tests\unit\DAV; use OCA\DAV\Files\BrowserErrorPagePlugin; +use PHPUnit\Framework\MockObject\MockObject; use Sabre\DAV\Exception\NotFound; use Sabre\HTTP\Response; class BrowserErrorPagePluginTest extends \Test\TestCase { - /** - * @dataProvider providesExceptions - * @param $expectedCode - * @param $exception - */ - public function test($expectedCode, $exception): void { - /** @var BrowserErrorPagePlugin | \PHPUnit\Framework\MockObject\MockObject $plugin */ - $plugin = $this->getMockBuilder(BrowserErrorPagePlugin::class)->setMethods(['sendResponse', 'generateBody'])->getMock(); + #[\PHPUnit\Framework\Attributes\DataProvider('providesExceptions')] + public function test(int $expectedCode, \Throwable $exception): void { + /** @var BrowserErrorPagePlugin&MockObject $plugin */ + $plugin = $this->getMockBuilder(BrowserErrorPagePlugin::class)->onlyMethods(['sendResponse', 'generateBody'])->getMock(); $plugin->expects($this->once())->method('generateBody')->willReturn(':boom:'); $plugin->expects($this->once())->method('sendResponse'); - /** @var \Sabre\DAV\Server | \PHPUnit\Framework\MockObject\MockObject $server */ - $server = $this->getMockBuilder('Sabre\DAV\Server')->disableOriginalConstructor()->getMock(); + /** @var \Sabre\DAV\Server&MockObject $server */ + $server = $this->createMock('Sabre\DAV\Server'); $server->expects($this->once())->method('on'); - $httpResponse = $this->getMockBuilder(Response::class)->disableOriginalConstructor()->getMock(); + $httpResponse = $this->createMock(Response::class); $httpResponse->expects($this->once())->method('addHeaders'); $httpResponse->expects($this->once())->method('setStatus')->with($expectedCode); $httpResponse->expects($this->once())->method('setBody')->with(':boom:'); @@ -35,7 +33,7 @@ class BrowserErrorPagePluginTest extends \Test\TestCase { $plugin->logException($exception); } - public function providesExceptions() { + public static function providesExceptions(): array { return [ [ 404, new NotFound()], [ 500, new \RuntimeException()], diff --git a/apps/dav/tests/unit/DAV/CustomPropertiesBackendTest.php b/apps/dav/tests/unit/DAV/CustomPropertiesBackendTest.php index 5590e8c1ff0..2a85c0cbecd 100644 --- a/apps/dav/tests/unit/DAV/CustomPropertiesBackendTest.php +++ b/apps/dav/tests/unit/DAV/CustomPropertiesBackendTest.php @@ -1,10 +1,11 @@ <?php +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCA\DAV\Tests\DAV; +namespace OCA\DAV\Tests\unit\DAV; use OCA\DAV\CalDAV\Calendar; use OCA\DAV\CalDAV\DefaultCalendarValidator; @@ -12,6 +13,7 @@ use OCA\DAV\DAV\CustomPropertiesBackend; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; use OCP\IUser; +use PHPUnit\Framework\MockObject\MockObject; use Sabre\DAV\Exception\NotFound; use Sabre\DAV\PropFind; use Sabre\DAV\PropPatch; @@ -28,23 +30,12 @@ use Test\TestCase; class CustomPropertiesBackendTest extends TestCase { private const BASE_URI = '/remote.php/dav/'; - /** @var Server | \PHPUnit\Framework\MockObject\MockObject */ - private $server; - - /** @var Tree | \PHPUnit\Framework\MockObject\MockObject */ - private $tree; - - /** @var IDBConnection */ - private $dbConnection; - - /** @var IUser | \PHPUnit\Framework\MockObject\MockObject */ - private $user; - - /** @var CustomPropertiesBackend | \PHPUnit\Framework\MockObject\MockObject */ - private $backend; - - /** @property DefaultCalendarValidator | \PHPUnit\Framework\MockObject\MockObject */ - private $defaultCalendarValidator; + private Server&MockObject $server; + private Tree&MockObject $tree; + private IDBConnection $dbConnection; + private IUser&MockObject $user; + private DefaultCalendarValidator&MockObject $defaultCalendarValidator; + private CustomPropertiesBackend $backend; protected function setUp(): void { parent::setUp(); @@ -85,13 +76,13 @@ class CustomPropertiesBackendTest extends TestCase { } } - protected function insertProps(string $user, string $path, array $props) { + protected function insertProps(string $user, string $path, array $props): void { foreach ($props as $name => $value) { $this->insertProp($user, $path, $name, $value); } } - protected function insertProp(string $user, string $path, string $name, mixed $value) { + protected function insertProp(string $user, string $path, string $name, mixed $value): void { $type = CustomPropertiesBackend::PROPERTY_TYPE_STRING; if ($value instanceof Href) { $value = $value->getHref(); @@ -110,7 +101,7 @@ class CustomPropertiesBackendTest extends TestCase { $query->execute(); } - protected function getProps(string $user, string $path) { + protected function getProps(string $user, string $path): array { $query = $this->dbConnection->getQueryBuilder(); $query->select('propertyname', 'propertyvalue', 'valuetype') ->from('properties') @@ -245,7 +236,7 @@ class CustomPropertiesBackendTest extends TestCase { $this->assertEquals($props, $setProps); } - public function propFindPrincipalScheduleDefaultCalendarProviderUrlProvider(): array { + public static function propFindPrincipalScheduleDefaultCalendarProviderUrlProvider(): array { // [ user, nodes, existingProps, requestedProps, returnedProps ] return [ [ // Exists @@ -280,9 +271,7 @@ class CustomPropertiesBackendTest extends TestCase { } - /** - * @dataProvider propFindPrincipalScheduleDefaultCalendarProviderUrlProvider - */ + #[\PHPUnit\Framework\Attributes\DataProvider('propFindPrincipalScheduleDefaultCalendarProviderUrlProvider')] public function testPropFindPrincipalScheduleDefaultCalendarUrl( string $user, array $nodes, @@ -344,9 +333,7 @@ class CustomPropertiesBackendTest extends TestCase { $this->assertEquals($returnedProps, $setProps); } - /** - * @dataProvider propPatchProvider - */ + #[\PHPUnit\Framework\Attributes\DataProvider('propPatchProvider')] public function testPropPatch(string $path, array $existing, array $props, array $result): void { $this->server->method('calculateUri') ->willReturnCallback(function ($uri) { @@ -373,7 +360,7 @@ class CustomPropertiesBackendTest extends TestCase { $this->assertEquals($result, $storedProps); } - public function propPatchProvider() { + public static function propPatchProvider(): array { $longPath = str_repeat('long_path', 100); return [ ['foo_bar_path_1337', [], ['{DAV:}displayname' => 'anything'], ['{DAV:}displayname' => 'anything']], @@ -427,25 +414,21 @@ class CustomPropertiesBackendTest extends TestCase { $this->assertEquals([], $storedProps); } - /** - * @dataProvider deleteProvider - */ + #[\PHPUnit\Framework\Attributes\DataProvider('deleteProvider')] public function testDelete(string $path): void { $this->insertProps('dummy_user_42', $path, ['foo' => 'bar']); $this->backend->delete($path); $this->assertEquals([], $this->getProps('dummy_user_42', $path)); } - public function deleteProvider() { + public static function deleteProvider(): array { return [ ['foo_bar_path_1337'], [str_repeat('long_path', 100)] ]; } - /** - * @dataProvider moveProvider - */ + #[\PHPUnit\Framework\Attributes\DataProvider('moveProvider')] public function testMove(string $source, string $target): void { $this->insertProps('dummy_user_42', $source, ['foo' => 'bar']); $this->backend->move($source, $target); @@ -453,7 +436,7 @@ class CustomPropertiesBackendTest extends TestCase { $this->assertEquals(['foo' => 'bar'], $this->getProps('dummy_user_42', $target)); } - public function moveProvider() { + public static function moveProvider(): array { return [ ['foo_bar_path_1337', 'foo_bar_path_7333'], [str_repeat('long_path1', 100), str_repeat('long_path2', 100)] @@ -475,5 +458,4 @@ class CustomPropertiesBackendTest extends TestCase { $this->assertInstanceOf(\Sabre\CalDAV\Xml\Property\ScheduleCalendarTransp::class, $decodeValue); $this->assertEquals('opaque', $decodeValue->getValue()); } - } diff --git a/apps/dav/tests/unit/DAV/GroupPrincipalTest.php b/apps/dav/tests/unit/DAV/GroupPrincipalTest.php index bfc4c5b2493..2756152a6e2 100644 --- a/apps/dav/tests/unit/DAV/GroupPrincipalTest.php +++ b/apps/dav/tests/unit/DAV/GroupPrincipalTest.php @@ -1,4 +1,6 @@ <?php + +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -18,20 +20,11 @@ use PHPUnit\Framework\MockObject\MockObject; use Sabre\DAV\PropPatch; class GroupPrincipalTest extends \Test\TestCase { - /** @var IConfig|MockObject */ - private $config; - - /** @var IGroupManager | MockObject */ - private $groupManager; - - /** @var IUserSession | MockObject */ - private $userSession; - - /** @var IManager | MockObject */ - private $shareManager; - - /** @var GroupPrincipalBackend */ - private $connector; + private IConfig&MockObject $config; + private IGroupManager&MockObject $groupManager; + private IUserSession&MockObject $userSession; + private IManager&MockObject $shareManager; + private GroupPrincipalBackend $connector; protected function setUp(): void { $this->groupManager = $this->createMock(IGroupManager::class); @@ -199,14 +192,7 @@ class GroupPrincipalTest extends \Test\TestCase { ['{DAV:}displayname' => 'Foo'])); } - /** - * @dataProvider searchPrincipalsDataProvider - * @param bool $sharingEnabled - * @param bool $groupSharingEnabled - * @param bool $groupsOnly - * @param string $test - * @param array $result - */ + #[\PHPUnit\Framework\Attributes\DataProvider('searchPrincipalsDataProvider')] public function testSearchPrincipals(bool $sharingEnabled, bool $groupSharingEnabled, bool $groupsOnly, string $test, array $result): void { $this->shareManager->expects($this->once()) ->method('shareAPIEnabled') @@ -264,7 +250,7 @@ class GroupPrincipalTest extends \Test\TestCase { ['{DAV:}displayname' => 'Foo'], $test)); } - public function searchPrincipalsDataProvider() { + public static function searchPrincipalsDataProvider(): array { return [ [true, true, false, 'allof', ['principals/groups/group1', 'principals/groups/group2', 'principals/groups/group3', 'principals/groups/group4', 'principals/groups/group5']], [true, true, false, 'anyof', ['principals/groups/group1', 'principals/groups/group2', 'principals/groups/group3', 'principals/groups/group4', 'principals/groups/group5']], @@ -277,14 +263,7 @@ class GroupPrincipalTest extends \Test\TestCase { ]; } - /** - * @dataProvider findByUriDataProvider - * @param bool $sharingEnabled - * @param bool $groupSharingEnabled - * @param bool $groupsOnly - * @param string $findUri - * @param string|null $result - */ + #[\PHPUnit\Framework\Attributes\DataProvider('findByUriDataProvider')] public function testFindByUri(bool $sharingEnabled, bool $groupSharingEnabled, bool $groupsOnly, string $findUri, ?string $result): void { $this->shareManager->expects($this->once()) ->method('shareAPIEnabled') @@ -320,7 +299,7 @@ class GroupPrincipalTest extends \Test\TestCase { $this->assertEquals($result, $this->connector->findByUri($findUri, 'principals/groups')); } - public function findByUriDataProvider() { + public static function findByUriDataProvider(): array { return [ [false, false, false, 'principal:principals/groups/group1', null], [false, false, false, 'principal:principals/groups/group3', null], @@ -337,10 +316,7 @@ class GroupPrincipalTest extends \Test\TestCase { ]; } - /** - * @return Group|MockObject - */ - private function mockGroup($gid) { + private function mockGroup(string $gid): Group&MockObject { $fooGroup = $this->createMock(Group::class); $fooGroup ->expects($this->exactly(1)) diff --git a/apps/dav/tests/unit/DAV/Listener/UserEventsListenerTest.php b/apps/dav/tests/unit/DAV/Listener/UserEventsListenerTest.php index a0876d8e483..8e410eb0a78 100644 --- a/apps/dav/tests/unit/DAV/Listener/UserEventsListenerTest.php +++ b/apps/dav/tests/unit/DAV/Listener/UserEventsListenerTest.php @@ -10,15 +10,19 @@ declare(strict_types=1); namespace OCA\DAV\Tests\unit\DAV\Listener; +use OCA\DAV\BackgroundJob\UserStatusAutomation; use OCA\DAV\CalDAV\CalDavBackend; use OCA\DAV\CardDAV\CardDavBackend; use OCA\DAV\CardDAV\SyncService; use OCA\DAV\Listener\UserEventsListener; -use OCA\DAV\Service\DefaultContactService; +use OCA\DAV\Service\ExampleContactService; +use OCA\DAV\Service\ExampleEventService; +use OCP\BackgroundJob\IJobList; use OCP\Defaults; use OCP\IUser; use OCP\IUserManager; use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; use Test\TestCase; class UserEventsListenerTest extends TestCase { @@ -27,33 +31,40 @@ class UserEventsListenerTest extends TestCase { private CalDavBackend&MockObject $calDavBackend; private CardDavBackend&MockObject $cardDavBackend; private Defaults&MockObject $defaults; - - private DefaultContactService&MockObject $defaultContactService; + private ExampleContactService&MockObject $exampleContactService; + private ExampleEventService&MockObject $exampleEventService; + private LoggerInterface&MockObject $logger; private UserEventsListener $userEventsListener; protected function setUp(): void { parent::setUp(); + $this->userManager = $this->createMock(IUserManager::class); $this->syncService = $this->createMock(SyncService::class); $this->calDavBackend = $this->createMock(CalDavBackend::class); $this->cardDavBackend = $this->createMock(CardDavBackend::class); $this->defaults = $this->createMock(Defaults::class); - $this->defaultContactService = $this->createMock(DefaultContactService::class); + $this->exampleContactService = $this->createMock(ExampleContactService::class); + $this->exampleEventService = $this->createMock(ExampleEventService::class); + $this->logger = $this->createMock(LoggerInterface::class); + $this->jobList = $this->createMock(IJobList::class); + $this->userEventsListener = new UserEventsListener( $this->userManager, $this->syncService, $this->calDavBackend, $this->cardDavBackend, $this->defaults, - $this->defaultContactService, + $this->exampleContactService, + $this->exampleEventService, + $this->logger, + $this->jobList, ); } public function test(): void { - $user = $this->getMockBuilder(IUser::class) - ->disableOriginalConstructor() - ->getMock(); + $user = $this->createMock(IUser::class); $user->expects($this->once())->method('getUID')->willReturn('newUser'); $this->defaults->expects($this->once())->method('getColorPrimary')->willReturn('#745bca'); @@ -65,7 +76,13 @@ class UserEventsListenerTest extends TestCase { '{DAV:}displayname' => 'Personal', '{http://apple.com/ns/ical/}calendar-color' => '#745bca', 'components' => 'VEVENT' - ]); + ]) + ->willReturn(1000); + $this->calDavBackend->expects(self::never()) + ->method('getCalendarsForUser'); + $this->exampleEventService->expects(self::once()) + ->method('createExampleEvent') + ->with(1000); $this->cardDavBackend->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(0); $this->cardDavBackend->expects($this->once())->method('createAddressBook')->with( @@ -76,13 +93,15 @@ class UserEventsListenerTest extends TestCase { } public function testWithExisting(): void { - $user = $this->getMockBuilder(IUser::class) - ->disableOriginalConstructor() - ->getMock(); + $user = $this->createMock(IUser::class); $user->expects($this->once())->method('getUID')->willReturn('newUser'); $this->calDavBackend->expects($this->once())->method('getCalendarsForUserCount')->willReturn(1); $this->calDavBackend->expects($this->never())->method('createCalendar'); + $this->calDavBackend->expects(self::never()) + ->method('createCalendar'); + $this->exampleEventService->expects(self::never()) + ->method('createExampleEvent'); $this->cardDavBackend->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(1); $this->cardDavBackend->expects($this->never())->method('createAddressBook'); @@ -91,9 +110,7 @@ class UserEventsListenerTest extends TestCase { } public function testWithBirthdayCalendar(): void { - $user = $this->getMockBuilder(IUser::class) - ->disableOriginalConstructor() - ->getMock(); + $user = $this->createMock(IUser::class); $user->expects($this->once())->method('getUID')->willReturn('newUser'); $this->defaults->expects($this->once())->method('getColorPrimary')->willReturn('#745bca'); @@ -116,9 +133,7 @@ class UserEventsListenerTest extends TestCase { } public function testDeleteCalendar(): void { - $user = $this->getMockBuilder(IUser::class) - ->disableOriginalConstructor() - ->getMock(); + $user = $this->createMock(IUser::class); $user->expects($this->once())->method('getUID')->willReturn('newUser'); $this->syncService->expects($this->once()) @@ -142,4 +157,27 @@ class UserEventsListenerTest extends TestCase { $this->userEventsListener->preDeleteUser($user); $this->userEventsListener->postDeleteUser('newUser'); } + + public function testDeleteUserAutomationEvent(): void { + $user = $this->createMock(IUser::class); + $user->expects($this->once())->method('getUID')->willReturn('newUser'); + + $this->syncService->expects($this->once()) + ->method('deleteUser'); + + $this->calDavBackend->expects($this->once())->method('getUsersOwnCalendars')->willReturn([ + ['id' => []] + ]); + $this->calDavBackend->expects($this->once())->method('getSubscriptionsForUser')->willReturn([ + ['id' => []] + ]); + $this->cardDavBackend->expects($this->once())->method('getUsersOwnAddressBooks')->willReturn([ + ['id' => []] + ]); + + $this->jobList->expects(self::once())->method('remove')->with(UserStatusAutomation::class, ['userId' => 'newUser']); + + $this->userEventsListener->preDeleteUser($user); + $this->userEventsListener->postDeleteUser('newUser'); + } } diff --git a/apps/dav/tests/unit/DAV/Sharing/BackendTest.php b/apps/dav/tests/unit/DAV/Sharing/BackendTest.php index dd2681d149f..556a623a73f 100644 --- a/apps/dav/tests/unit/DAV/Sharing/BackendTest.php +++ b/apps/dav/tests/unit/DAV/Sharing/BackendTest.php @@ -24,14 +24,14 @@ use Test\TestCase; class BackendTest extends TestCase { - private IDBConnection|MockObject $db; - private IUserManager|MockObject $userManager; - private IGroupManager|MockObject $groupManager; - private MockObject|Principal $principalBackend; - private MockObject|ICache $shareCache; - private LoggerInterface|MockObject $logger; - private MockObject|ICacheFactory $cacheFactory; - private Service|MockObject $calendarService; + private IDBConnection&MockObject $db; + private IUserManager&MockObject $userManager; + private IGroupManager&MockObject $groupManager; + private Principal&MockObject $principalBackend; + private ICache&MockObject $shareCache; + private LoggerInterface&MockObject $logger; + private ICacheFactory&MockObject $cacheFactory; + private Service&MockObject $calendarService; private CalendarSharingBackend $backend; protected function setUp(): void { diff --git a/apps/dav/tests/unit/DAV/Sharing/PluginTest.php b/apps/dav/tests/unit/DAV/Sharing/PluginTest.php index 9c6950f19e8..7a88f7cc5dd 100644 --- a/apps/dav/tests/unit/DAV/Sharing/PluginTest.php +++ b/apps/dav/tests/unit/DAV/Sharing/PluginTest.php @@ -1,5 +1,6 @@ <?php +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -12,6 +13,7 @@ use OCA\DAV\DAV\Sharing\IShareable; use OCA\DAV\DAV\Sharing\Plugin; use OCP\IConfig; use OCP\IRequest; +use PHPUnit\Framework\MockObject\MockObject; use Sabre\DAV\Server; use Sabre\DAV\SimpleCollection; use Sabre\HTTP\Request; @@ -19,32 +21,24 @@ use Sabre\HTTP\Response; use Test\TestCase; class PluginTest extends TestCase { - - /** @var Plugin */ - private $plugin; - /** @var Server */ - private $server; - /** @var IShareable | \PHPUnit\Framework\MockObject\MockObject */ - private $book; + private Plugin $plugin; + private Server $server; + private IShareable&MockObject $book; protected function setUp(): void { parent::setUp(); - /** @var Auth | \PHPUnit\Framework\MockObject\MockObject $authBackend */ - $authBackend = $this->getMockBuilder(Auth::class)->disableOriginalConstructor()->getMock(); + $authBackend = $this->createMock(Auth::class); $authBackend->method('isDavAuthenticated')->willReturn(true); - /** @var IRequest $request */ - $request = $this->getMockBuilder(IRequest::class)->disableOriginalConstructor()->getMock(); + $request = $this->createMock(IRequest::class); $config = $this->createMock(IConfig::class); $this->plugin = new Plugin($authBackend, $request, $config); $root = new SimpleCollection('root'); $this->server = new \Sabre\DAV\Server($root); /** @var SimpleCollection $node */ - $this->book = $this->getMockBuilder(IShareable::class)-> - disableOriginalConstructor()-> - getMock(); + $this->book = $this->createMock(IShareable::class); $this->book->method('getName')->willReturn('addressbook1.vcf'); $root->addChild($this->book); $this->plugin->initialize($this->server); diff --git a/apps/dav/tests/unit/DAV/SystemPrincipalBackendTest.php b/apps/dav/tests/unit/DAV/SystemPrincipalBackendTest.php index 32916804080..3df861accf2 100644 --- a/apps/dav/tests/unit/DAV/SystemPrincipalBackendTest.php +++ b/apps/dav/tests/unit/DAV/SystemPrincipalBackendTest.php @@ -1,5 +1,6 @@ <?php +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -8,22 +9,19 @@ namespace OCA\DAV\Tests\unit\DAV; use OCA\DAV\DAV\SystemPrincipalBackend; +use Sabre\DAV\Exception; use Test\TestCase; class SystemPrincipalBackendTest extends TestCase { - /** - * @dataProvider providesPrefix - * @param $expected - * @param $prefix - */ - public function testGetPrincipalsByPrefix($expected, $prefix): void { + #[\PHPUnit\Framework\Attributes\DataProvider('providesPrefix')] + public function testGetPrincipalsByPrefix(array $expected, string $prefix): void { $backend = new SystemPrincipalBackend(); $result = $backend->getPrincipalsByPrefix($prefix); $this->assertEquals($expected, $result); } - public function providesPrefix() { + public static function providesPrefix(): array { return [ [[], ''], [[[ @@ -38,18 +36,14 @@ class SystemPrincipalBackendTest extends TestCase { ]; } - /** - * @dataProvider providesPath - * @param $expected - * @param $path - */ - public function testGetPrincipalByPath($expected, $path): void { + #[\PHPUnit\Framework\Attributes\DataProvider('providesPath')] + public function testGetPrincipalByPath(?array $expected, string $path): void { $backend = new SystemPrincipalBackend(); $result = $backend->getPrincipalByPath($path); $this->assertEquals($expected, $result); } - public function providesPath() { + public static function providesPath(): array { return [ [null, ''], [null, 'principals'], @@ -61,59 +55,43 @@ class SystemPrincipalBackendTest extends TestCase { ]; } - /** - * @dataProvider providesPrincipalForGetGroupMemberSet - * - * @param string $principal - * @throws \Sabre\DAV\Exception - */ - public function testGetGroupMemberSetExceptional($principal): void { - $this->expectException(\Sabre\DAV\Exception::class); + #[\PHPUnit\Framework\Attributes\DataProvider('providesPrincipalForGetGroupMemberSet')] + public function testGetGroupMemberSetExceptional(?string $principal): void { + $this->expectException(Exception::class); $this->expectExceptionMessage('Principal not found'); $backend = new SystemPrincipalBackend(); $backend->getGroupMemberSet($principal); } - public function providesPrincipalForGetGroupMemberSet() { + public static function providesPrincipalForGetGroupMemberSet(): array { return [ [null], ['principals/system'], ]; } - /** - * @throws \Sabre\DAV\Exception - */ public function testGetGroupMemberSet(): void { $backend = new SystemPrincipalBackend(); $result = $backend->getGroupMemberSet('principals/system/system'); $this->assertEquals(['principals/system/system'], $result); } - /** - * @dataProvider providesPrincipalForGetGroupMembership - * - * @param string $principal - * @throws \Sabre\DAV\Exception - */ - public function testGetGroupMembershipExceptional($principal): void { - $this->expectException(\Sabre\DAV\Exception::class); + #[\PHPUnit\Framework\Attributes\DataProvider('providesPrincipalForGetGroupMembership')] + public function testGetGroupMembershipExceptional(string $principal): void { + $this->expectException(Exception::class); $this->expectExceptionMessage('Principal not found'); $backend = new SystemPrincipalBackend(); $backend->getGroupMembership($principal); } - public function providesPrincipalForGetGroupMembership() { + public static function providesPrincipalForGetGroupMembership(): array { return [ ['principals/system/a'], ]; } - /** - * @throws \Sabre\DAV\Exception - */ public function testGetGroupMembership(): void { $backend = new SystemPrincipalBackend(); $result = $backend->getGroupMembership('principals/system/system'); diff --git a/apps/dav/tests/unit/DAV/ViewOnlyPluginTest.php b/apps/dav/tests/unit/DAV/ViewOnlyPluginTest.php index 7a4828dd2de..eefbc53fd22 100644 --- a/apps/dav/tests/unit/DAV/ViewOnlyPluginTest.php +++ b/apps/dav/tests/unit/DAV/ViewOnlyPluginTest.php @@ -1,5 +1,6 @@ <?php +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2019 ownCloud GmbH @@ -20,30 +21,29 @@ use OCP\Files\Storage\IStorage; use OCP\IUser; use OCP\Share\IAttributes; use OCP\Share\IShare; +use PHPUnit\Framework\MockObject\MockObject; use Sabre\DAV\Server; use Sabre\DAV\Tree; use Sabre\HTTP\RequestInterface; use Test\TestCase; class ViewOnlyPluginTest extends TestCase { - + private Tree&MockObject $tree; + private RequestInterface&MockObject $request; + private Folder&MockObject $userFolder; private ViewOnlyPlugin $plugin; - /** @var Tree | \PHPUnit\Framework\MockObject\MockObject */ - private $tree; - /** @var RequestInterface | \PHPUnit\Framework\MockObject\MockObject */ - private $request; - /** @var Folder | \PHPUnit\Framework\MockObject\MockObject */ - private $userFolder; public function setUp(): void { + parent::setUp(); + $this->userFolder = $this->createMock(Folder::class); - $this->plugin = new ViewOnlyPlugin( - $this->userFolder, - ); $this->request = $this->createMock(RequestInterface::class); $this->tree = $this->createMock(Tree::class); - $server = $this->createMock(Server::class); + + $this->plugin = new ViewOnlyPlugin( + $this->userFolder, + ); $server->tree = $this->tree; $this->plugin->initialize($server); @@ -71,27 +71,31 @@ class ViewOnlyPluginTest extends TestCase { $this->assertTrue($this->plugin->checkViewOnly($this->request)); } - public function providesDataForCanGet(): array { + public static function providesDataForCanGet(): array { return [ // has attribute permissions-download enabled - can get file - [false, true, true], + [false, true, true, true], // has no attribute permissions-download - can get file - [false, null, true], - // has attribute permissions-download disabled- cannot get the file - [false, false, false], + [false, null, true, true], // has attribute permissions-download enabled - can get file version - [true, true, true], + [true, true, true, true], // has no attribute permissions-download - can get file version - [true, null, true], - // has attribute permissions-download disabled- cannot get the file version - [true, false, false], + [true, null, true, true], + // has attribute permissions-download disabled - cannot get the file + [false, false, false, false], + // has attribute permissions-download disabled - cannot get the file version + [true, false, false, false], + + // Has global allowViewWithoutDownload option enabled + // has attribute permissions-download disabled - can get file + [false, false, false, true], + // has attribute permissions-download disabled - can get file version + [true, false, false, true], ]; } - /** - * @dataProvider providesDataForCanGet - */ - public function testCanGet(bool $isVersion, ?bool $attrEnabled, bool $expectCanDownloadFile): void { + #[\PHPUnit\Framework\Attributes\DataProvider('providesDataForCanGet')] + public function testCanGet(bool $isVersion, ?bool $attrEnabled, bool $expectCanDownloadFile, bool $allowViewWithoutDownload): void { $nodeInfo = $this->createMock(File::class); if ($isVersion) { $davPath = 'versions/alice/versions/117/123456'; @@ -151,6 +155,10 @@ class ViewOnlyPluginTest extends TestCase { ->with('permissions', 'download') ->willReturn($attrEnabled); + $share->expects($this->once()) + ->method('canSeeContent') + ->willReturn($allowViewWithoutDownload); + if (!$expectCanDownloadFile) { $this->expectException(Forbidden::class); } |