diff options
Diffstat (limited to 'apps/dav/tests')
-rw-r--r-- | apps/dav/tests/unit/DAV/Listener/UserEventsListenerTest.php | 8 | ||||
-rw-r--r-- | apps/dav/tests/unit/Service/ExampleContactServiceTest.php (renamed from apps/dav/tests/unit/Service/DefaultContactServiceTest.php) | 85 | ||||
-rw-r--r-- | apps/dav/tests/unit/Upload/UploadAutoMkcolPluginTest.php | 135 |
3 files changed, 196 insertions, 32 deletions
diff --git a/apps/dav/tests/unit/DAV/Listener/UserEventsListenerTest.php b/apps/dav/tests/unit/DAV/Listener/UserEventsListenerTest.php index f03343af008..40d2fb62431 100644 --- a/apps/dav/tests/unit/DAV/Listener/UserEventsListenerTest.php +++ b/apps/dav/tests/unit/DAV/Listener/UserEventsListenerTest.php @@ -14,7 +14,7 @@ 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\Defaults; use OCP\IUser; @@ -29,7 +29,7 @@ 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; @@ -43,7 +43,7 @@ class UserEventsListenerTest extends TestCase { $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); @@ -53,7 +53,7 @@ class UserEventsListenerTest extends TestCase { $this->calDavBackend, $this->cardDavBackend, $this->defaults, - $this->defaultContactService, + $this->exampleContactService, $this->exampleEventService, $this->logger, ); diff --git a/apps/dav/tests/unit/Service/DefaultContactServiceTest.php b/apps/dav/tests/unit/Service/ExampleContactServiceTest.php index 3bd8c9cb6f6..4c8d900ae86 100644 --- a/apps/dav/tests/unit/Service/DefaultContactServiceTest.php +++ b/apps/dav/tests/unit/Service/ExampleContactServiceTest.php @@ -10,56 +10,60 @@ declare(strict_types=1); namespace OCA\DAV\Tests\unit\Service; use OCA\DAV\CardDAV\CardDavBackend; -use OCA\DAV\Service\DefaultContactService; +use OCA\DAV\Service\ExampleContactService; use OCP\App\IAppManager; +use OCP\AppFramework\Services\IAppConfig; use OCP\Files\AppData\IAppDataFactory; use OCP\Files\IAppData; use OCP\Files\NotFoundException; use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFolder; -use OCP\IAppConfig; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Symfony\Component\Uid\Uuid; use Test\TestCase; -class DefaultContactServiceTest extends TestCase { - protected DefaultContactService $service; +class ExampleContactServiceTest extends TestCase { + protected ExampleContactService $service; protected CardDavBackend&MockObject $cardDav; protected IAppManager&MockObject $appManager; protected IAppDataFactory&MockObject $appDataFactory; protected LoggerInterface&MockObject $logger; - protected IAppConfig&MockObject $config; + protected IAppConfig&MockObject $appConfig; + protected IAppData&MockObject $appData; protected function setUp(): void { parent::setUp(); $this->cardDav = $this->createMock(CardDavBackend::class); - $this->appManager = $this->createMock(IAppManager::class); $this->appDataFactory = $this->createMock(IAppDataFactory::class); $this->logger = $this->createMock(LoggerInterface::class); - $this->config = $this->createMock(IAppConfig::class); + $this->appConfig = $this->createMock(IAppConfig::class); - $this->service = new DefaultContactService( - $this->cardDav, - $this->appManager, + $this->appData = $this->createMock(IAppData::class); + $this->appDataFactory->method('get') + ->with('dav') + ->willReturn($this->appData); + + $this->service = new ExampleContactService( $this->appDataFactory, - $this->config, + $this->appConfig, $this->logger, + $this->cardDav, ); } public function testCreateDefaultContactWithInvalidCard(): void { // Invalid vCard missing required FN property $vcardContent = "BEGIN:VCARD\nVERSION:3.0\nEND:VCARD"; - $this->config->method('getValueString')->willReturn('yes'); - $appData = $this->createMock(IAppData::class); + $this->appConfig->method('getAppValueBool') + ->with('enableDefaultContact', true) + ->willReturn(true); $folder = $this->createMock(ISimpleFolder::class); $file = $this->createMock(ISimpleFile::class); $file->method('getContent')->willReturn($vcardContent); $folder->method('getFile')->willReturn($file); - $appData->method('getFolder')->willReturn($folder); - $this->appDataFactory->method('get')->willReturn($appData); + $this->appData->method('getFolder')->willReturn($folder); $this->logger->expects($this->once()) ->method('error') @@ -76,14 +80,14 @@ class DefaultContactServiceTest extends TestCase { $originalRev = '20200101T000000Z'; $vcardContent = "BEGIN:VCARD\nVERSION:3.0\nFN:Test User\nUID:$originalUid\nREV:$originalRev\nEND:VCARD"; - $this->config->method('getValueString')->willReturn('yes'); - $appData = $this->createMock(IAppData::class); + $this->appConfig->method('getAppValueBool') + ->with('enableDefaultContact', true) + ->willReturn(true); $folder = $this->createMock(ISimpleFolder::class); $file = $this->createMock(ISimpleFile::class); $file->method('getContent')->willReturn($vcardContent); $folder->method('getFile')->willReturn($file); - $appData->method('getFolder')->willReturn($folder); - $this->appDataFactory->method('get')->willReturn($appData); + $this->appData->method('getFolder')->willReturn($folder); $capturedCardData = null; $this->cardDav->expects($this->once()) @@ -107,10 +111,10 @@ class DefaultContactServiceTest extends TestCase { } public function testDefaultContactFileDoesNotExist(): void { - $appData = $this->createMock(IAppData::class); - $this->config->method('getValueString')->willReturn('yes'); - $appData->method('getFolder')->willThrowException(new NotFoundException()); - $this->appDataFactory->method('get')->willReturn($appData); + $this->appConfig->method('getAppValueBool') + ->with('enableDefaultContact', true) + ->willReturn(true); + $this->appData->method('getFolder')->willThrowException(new NotFoundException()); $this->cardDav->expects($this->never()) ->method('createCard'); @@ -121,14 +125,14 @@ class DefaultContactServiceTest extends TestCase { public function testUidAndRevAreAddedIfMissing(): void { $vcardContent = "BEGIN:VCARD\nVERSION:3.0\nFN:Test User\nEND:VCARD"; - $this->config->method('getValueString')->willReturn('yes'); - $appData = $this->createMock(IAppData::class); + $this->appConfig->method('getAppValueBool') + ->with('enableDefaultContact', true) + ->willReturn(true); $folder = $this->createMock(ISimpleFolder::class); $file = $this->createMock(ISimpleFile::class); $file->method('getContent')->willReturn($vcardContent); $folder->method('getFile')->willReturn($file); - $appData->method('getFolder')->willReturn($folder); - $this->appDataFactory->method('get')->willReturn($appData); + $this->appData->method('getFolder')->willReturn($folder); $capturedCardData = 'new-card-data'; @@ -154,7 +158,9 @@ class DefaultContactServiceTest extends TestCase { } public function testDefaultContactIsNotCreatedIfEnabled(): void { - $this->config->method('getValueString')->willReturn('no'); + $this->appConfig->method('getAppValueBool') + ->with('enableDefaultContact', true) + ->willReturn(false); $this->logger->expects($this->never()) ->method('error'); $this->cardDav->expects($this->never()) @@ -162,4 +168,27 @@ class DefaultContactServiceTest extends TestCase { $this->service->createDefaultContact(123); } + + public static function provideDefaultContactEnableData(): array { + return [[true], [false]]; + } + + /** @dataProvider provideDefaultContactEnableData */ + public function testIsDefaultContactEnabled(bool $enabled): void { + $this->appConfig->expects(self::once()) + ->method('getAppValueBool') + ->with('enableDefaultContact', true) + ->willReturn($enabled); + + $this->assertEquals($enabled, $this->service->isDefaultContactEnabled()); + } + + /** @dataProvider provideDefaultContactEnableData */ + public function testSetDefaultContactEnabled(bool $enabled): void { + $this->appConfig->expects(self::once()) + ->method('setAppValueBool') + ->with('enableDefaultContact', $enabled); + + $this->service->setDefaultContactEnabled($enabled); + } } diff --git a/apps/dav/tests/unit/Upload/UploadAutoMkcolPluginTest.php b/apps/dav/tests/unit/Upload/UploadAutoMkcolPluginTest.php new file mode 100644 index 00000000000..9467b5df249 --- /dev/null +++ b/apps/dav/tests/unit/Upload/UploadAutoMkcolPluginTest.php @@ -0,0 +1,135 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OCA\DAV\Tests\unit\Upload; + +use Generator; +use OCA\DAV\Upload\UploadAutoMkcolPlugin; +use PHPUnit\Framework\MockObject\MockObject; +use Sabre\DAV\ICollection; +use Sabre\DAV\INode; +use Sabre\DAV\Server; +use Sabre\DAV\Tree; +use Sabre\HTTP\RequestInterface; +use Sabre\HTTP\ResponseInterface; +use Test\TestCase; + +class UploadAutoMkcolPluginTest extends TestCase { + + private Tree&MockObject $tree; + private RequestInterface&MockObject $request; + private ResponseInterface&MockObject $response; + + public static function dataMissingHeaderShouldReturnTrue(): Generator { + yield 'missing X-NC-WebDAV-Auto-Mkcol header' => [null]; + yield 'empty X-NC-WebDAV-Auto-Mkcol header' => ['']; + yield 'invalid X-NC-WebDAV-Auto-Mkcol header' => ['enable']; + } + + public function testBeforeMethodWithRootNodeNotAnICollectionShouldReturnTrue(): void { + $this->request->method('getHeader')->willReturn('1'); + $this->request->expects(self::once()) + ->method('getPath') + ->willReturn('/non-relevant/path.txt'); + $this->tree->expects(self::once()) + ->method('nodeExists') + ->with('/non-relevant') + ->willReturn(false); + + $mockNode = $this->getMockBuilder(INode::class); + $this->tree->expects(self::once()) + ->method('getNodeForPath') + ->willReturn($mockNode); + + $return = $this->plugin->beforeMethod($this->request, $this->response); + $this->assertTrue($return); + } + + /** + * @dataProvider dataMissingHeaderShouldReturnTrue + */ + public function testBeforeMethodWithMissingHeaderShouldReturnTrue(?string $header): void { + $this->request->expects(self::once()) + ->method('getHeader') + ->with('X-NC-WebDAV-Auto-Mkcol') + ->willReturn($header); + + $this->request->expects(self::never()) + ->method('getPath'); + + $return = $this->plugin->beforeMethod($this->request, $this->response); + self::assertTrue($return); + } + + public function testBeforeMethodWithExistingPathShouldReturnTrue(): void { + $this->request->method('getHeader')->willReturn('1'); + $this->request->expects(self::once()) + ->method('getPath') + ->willReturn('/files/user/deep/image.jpg'); + $this->tree->expects(self::once()) + ->method('nodeExists') + ->with('/files/user/deep') + ->willReturn(true); + + $this->tree->expects(self::never()) + ->method('getNodeForPath'); + + $return = $this->plugin->beforeMethod($this->request, $this->response); + self::assertTrue($return); + } + + public function testBeforeMethodShouldSucceed(): void { + $this->request->method('getHeader')->willReturn('1'); + $this->request->expects(self::once()) + ->method('getPath') + ->willReturn('/files/user/my/deep/path/image.jpg'); + $this->tree->expects(self::once()) + ->method('nodeExists') + ->with('/files/user/my/deep/path') + ->willReturn(false); + + $mockNode = $this->createMock(ICollection::class); + $this->tree->expects(self::once()) + ->method('getNodeForPath') + ->with('/files') + ->willReturn($mockNode); + $mockNode->expects(self::exactly(4)) + ->method('childExists') + ->willReturnMap([ + ['user', true], + ['my', true], + ['deep', false], + ['path', false], + ]); + $mockNode->expects(self::exactly(2)) + ->method('createDirectory'); + $mockNode->expects(self::exactly(4)) + ->method('getChild') + ->willReturn($mockNode); + + $return = $this->plugin->beforeMethod($this->request, $this->response); + self::assertTrue($return); + } + + protected function setUp(): void { + parent::setUp(); + + $server = $this->createMock(Server::class); + $this->tree = $this->createMock(Tree::class); + + $server->tree = $this->tree; + $this->plugin = new UploadAutoMkcolPlugin(); + + $this->request = $this->createMock(RequestInterface::class); + $this->response = $this->createMock(ResponseInterface::class); + $server->httpRequest = $this->request; + $server->httpResponse = $this->response; + + $this->plugin->initialize($server); + } +} |