aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/tests/unit
diff options
context:
space:
mode:
authorAnna Larch <anna@nextcloud.com>2024-09-05 20:35:43 +0200
committerAnna <anna@nextcloud.com>2024-09-30 11:53:02 +0200
commit6468ec5716419f094586f9bc19684679530277c6 (patch)
treeccf4c720a08213f16569245f95f34431fdf65750 /apps/dav/tests/unit
parentda591eaec3050ab02b4a74d1304db78335eeb7fa (diff)
downloadnextcloud-server-6468ec5716419f094586f9bc19684679530277c6.tar.gz
nextcloud-server-6468ec5716419f094586f9bc19684679530277c6.zip
feat(carddav): add command to list address booksfeat/add-addressbook-list-command
Signed-off-by: Anna Larch <anna@nextcloud.com>
Diffstat (limited to 'apps/dav/tests/unit')
-rw-r--r--apps/dav/tests/unit/Command/ListAddressbooksTest.php110
1 files changed, 110 insertions, 0 deletions
diff --git a/apps/dav/tests/unit/Command/ListAddressbooksTest.php b/apps/dav/tests/unit/Command/ListAddressbooksTest.php
new file mode 100644
index 00000000000..b6a8c25477c
--- /dev/null
+++ b/apps/dav/tests/unit/Command/ListAddressbooksTest.php
@@ -0,0 +1,110 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OCA\DAV\Tests\Command;
+
+use OCA\DAV\CardDAV\CardDavBackend;
+use OCA\DAV\Command\ListAddressbooks;
+use OCP\IUserManager;
+use PHPUnit\Framework\MockObject\MockObject;
+use Symfony\Component\Console\Tester\CommandTester;
+use Test\TestCase;
+
+/**
+ * Class ListCalendarsTest
+ *
+ * @package OCA\DAV\Tests\Command
+ */
+class ListAddressbooksTest extends TestCase {
+
+ private \OCP\IUserManager|MockObject $userManager;
+ private CardDavBackend|MockObject $cardDavBackend;
+ private ListAddressbooks $command;
+
+ public const USERNAME = 'username';
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->userManager = $this->createMock(IUserManager::class);
+ $this->cardDavBackend = $this->createMock(CardDavBackend::class);
+
+ $this->command = new ListAddressbooks(
+ $this->userManager,
+ $this->cardDavBackend
+ );
+ }
+
+ public function testWithBadUser(): void {
+ $this->expectException(\InvalidArgumentException::class);
+
+ $this->userManager->expects($this->once())
+ ->method('userExists')
+ ->with(self::USERNAME)
+ ->willReturn(false);
+
+ $commandTester = new CommandTester($this->command);
+ $commandTester->execute([
+ 'uid' => self::USERNAME,
+ ]);
+ $this->assertStringContainsString('User <' . self::USERNAME . '> in unknown', $commandTester->getDisplay());
+ }
+
+ public function testWithCorrectUserWithNoCalendars(): void {
+ $this->userManager->expects($this->once())
+ ->method('userExists')
+ ->with(self::USERNAME)
+ ->willReturn(true);
+
+ $this->cardDavBackend->expects($this->once())
+ ->method('getAddressBooksForUser')
+ ->with('principals/users/' . self::USERNAME)
+ ->willReturn([]);
+
+ $commandTester = new CommandTester($this->command);
+ $commandTester->execute([
+ 'uid' => self::USERNAME,
+ ]);
+ $this->assertStringContainsString('User <' . self::USERNAME . "> has no addressbooks\n", $commandTester->getDisplay());
+ }
+
+ public function dataExecute() {
+ return [
+ [false, '✓'],
+ [true, 'x']
+ ];
+ }
+
+ /**
+ * @dataProvider dataExecute
+ */
+ public function testWithCorrectUser(bool $readOnly, string $output): void {
+ $this->userManager->expects($this->once())
+ ->method('userExists')
+ ->with(self::USERNAME)
+ ->willReturn(true);
+
+ $this->cardDavBackend->expects($this->once())
+ ->method('getAddressBooksForUser')
+ ->with('principals/users/' . self::USERNAME)
+ ->willReturn([
+ [
+ '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => $readOnly,
+ 'uri' => 'test',
+ '{DAV:}displayname' => 'dp',
+ '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => 'owner-principal',
+ '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname' => 'owner-dp',
+ ]
+ ]);
+
+ $commandTester = new CommandTester($this->command);
+ $commandTester->execute([
+ 'uid' => self::USERNAME,
+ ]);
+ $this->assertStringContainsString($output, $commandTester->getDisplay());
+ }
+}