aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorRichard Steinmetz <richard@steinmetz.cloud>2024-06-25 16:48:30 +0200
committerGitHub <noreply@github.com>2024-06-25 16:48:30 +0200
commit61213253104cd5719c1ed9fa74935ea6be5b6719 (patch)
treee7977dcd0970709f9154a682109f25c5e17817d5 /tests
parentad2fa3724db6edc88a4997678b95e0d90b8bc7fa (diff)
parent69e0158030e49b3d16276b8488d5dd1c6ffa023b (diff)
downloadnextcloud-server-61213253104cd5719c1ed9fa74935ea6be5b6719.tar.gz
nextcloud-server-61213253104cd5719c1ed9fa74935ea6be5b6719.zip
Merge pull request #45532 from nextcloud/feat/publish-resources-room-update
feat: implement public OCP api to update resources and rooms
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/Calendar/Resource/ManagerTest.php14
-rw-r--r--tests/lib/Calendar/ResourcesRoomsUpdaterTest.php439
-rw-r--r--tests/lib/Calendar/Room/ManagerTest.php14
3 files changed, 467 insertions, 0 deletions
diff --git a/tests/lib/Calendar/Resource/ManagerTest.php b/tests/lib/Calendar/Resource/ManagerTest.php
index d9d66699710..15977765da7 100644
--- a/tests/lib/Calendar/Resource/ManagerTest.php
+++ b/tests/lib/Calendar/Resource/ManagerTest.php
@@ -13,6 +13,7 @@ use OC\AppFramework\Bootstrap\Coordinator;
use OC\AppFramework\Bootstrap\RegistrationContext;
use OC\AppFramework\Bootstrap\ServiceRegistration;
use OC\Calendar\Resource\Manager;
+use OC\Calendar\ResourcesRoomsUpdater;
use OCP\Calendar\Resource\IBackend;
use OCP\IServerContainer;
use PHPUnit\Framework\MockObject\MockObject;
@@ -25,6 +26,9 @@ class ManagerTest extends TestCase {
/** @var IServerContainer|MockObject */
private $server;
+ /** @var ResourcesRoomsUpdater|MockObject */
+ private $resourcesRoomsUpdater;
+
/** @var Manager */
private $manager;
@@ -33,9 +37,12 @@ class ManagerTest extends TestCase {
$this->coordinator = $this->createMock(Coordinator::class);
$this->server = $this->createMock(IServerContainer::class);
+ $this->resourcesRoomsUpdater = $this->createMock(ResourcesRoomsUpdater::class);
+
$this->manager = new Manager(
$this->coordinator,
$this->server,
+ $this->resourcesRoomsUpdater,
);
}
@@ -126,4 +133,11 @@ class ManagerTest extends TestCase {
self::assertEquals([], $this->manager->getBackends());
}
+
+ public function testUpdate(): void {
+ $this->resourcesRoomsUpdater->expects(self::once())
+ ->method('updateResources');
+
+ $this->manager->update();
+ }
}
diff --git a/tests/lib/Calendar/ResourcesRoomsUpdaterTest.php b/tests/lib/Calendar/ResourcesRoomsUpdaterTest.php
new file mode 100644
index 00000000000..7776ba8cd3a
--- /dev/null
+++ b/tests/lib/Calendar/ResourcesRoomsUpdaterTest.php
@@ -0,0 +1,439 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace Test\Calendar;
+
+use OC\Calendar\ResourcesRoomsUpdater;
+use OCA\DAV\CalDAV\CalDavBackend;
+use OCP\Calendar\BackendTemporarilyUnavailableException;
+use OCP\Calendar\IMetadataProvider;
+use OCP\Calendar\Resource\IBackend;
+use OCP\Calendar\Resource\IManager as IResourceManager;
+use OCP\Calendar\Resource\IResource;
+use OCP\Calendar\Room\IManager as IRoomManager;
+use PHPUnit\Framework\MockObject\MockObject;
+use Psr\Container\ContainerInterface;
+use Test\TestCase;
+
+interface tmpI extends IResource, IMetadataProvider {
+}
+
+class ResourcesRoomsUpdaterTest extends TestCase {
+ private ResourcesRoomsUpdater $updater;
+
+ /** @var IResourceManager|MockObject */
+ private $resourceManager;
+
+ /** @var IRoomManager|MockObject */
+ private $roomManager;
+
+ /** @var ContainerInterface|MockObject */
+ private $container;
+
+ /** @var CalDavBackend|MockObject */
+ private $calDavBackend;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->resourceManager = $this->createMock(IResourceManager::class);
+ $this->roomManager = $this->createMock(IRoomManager::class);
+ $this->container = $this->createMock(ContainerInterface::class);
+ $this->calDavBackend = $this->createMock(CalDavBackend::class);
+
+ $this->container->method('get')
+ ->willReturnMap([
+ [IResourceManager::class, $this->resourceManager],
+ [IRoomManager::class, $this->roomManager],
+ ]);
+
+ $this->updater = new ResourcesRoomsUpdater(
+ $this->container,
+ self::$realDatabase,
+ $this->calDavBackend
+ );
+ }
+
+ protected function tearDown(): void {
+ $query = self::$realDatabase->getQueryBuilder();
+ $query->delete('calendar_resources')->execute();
+ $query->delete('calendar_resources_md')->execute();
+ $query->delete('calendar_rooms')->execute();
+ $query->delete('calendar_rooms_md')->execute();
+ }
+
+ /**
+ * Data in Cache:
+ * resources:
+ * [backend1, res1, Beamer1, {}] - []
+ * [backend1, res2, TV1, {}] - []
+ * [backend2, res3, Beamer2, {}] - ['meta1' => 'value1', 'meta2' => 'value2']
+ * [backend2, res4, TV2, {}] - ['meta1' => 'value1', 'meta3' => 'value3-old']
+ * [backend3, res5, Beamer3, {}] - []
+ * [backend3, res6, Pointer, {foo, bar}] - ['meta99' => 'value99']
+ *
+ * Data in Backend:
+ * backend1 gone
+ * backend2 throws BackendTemporarilyUnavailableException
+ * [backend3, res6, Pointer123, {foo, biz}] - ['meta99' => 'value99-new', 'meta123' => 'meta456']
+ * [backend3, res7, Resource4, {biz}] - ['meta1' => 'value1']
+ * [backend4, res8, Beamer, {}] - ['meta2' => 'value2']
+ * [backend4, res9, Beamer2, {}] - []
+ *
+ * Expected after run:
+ * [backend1, res1, Beamer1, {}] - []
+ * [backend1, res2, TV1, {}] - []
+ * [backend2, res3, Beamer2, {}] - ['meta1' => 'value1', 'meta2' => 'value2']
+ * [backend2, res4, TV2, {}] - ['meta1' => 'value1', 'meta3' => 'value3-old']
+ * [backend3, res6, Pointer123, {foo, biz}] - ['meta99' => 'value99-new', 'meta123' => 'meta456']
+ * [backend3, res7, Resource4, {biz}] - ['meta1' => 'value1']
+ * [backend4, res8, Beamer, {}] - ['meta2' => 'value2']
+ * [backend4, res9, Beamer2, {}] - []
+ */
+
+ public function testUpdateBoth(): void {
+ $this->createTestResourcesInCache();
+
+ $backend2 = $this->createMock(IBackend::class);
+ $backend3 = $this->createMock(IBackend::class);
+ $backend4 = $this->createMock(IBackend::class);
+
+ $res6 = $this->createMock(tmpI::class);
+ $res7 = $this->createMock(tmpI::class);
+ $res8 = $this->createMock(tmpI::class);
+ $res9 = $this->createMock(IResource::class);
+
+ $backend2->method('getBackendIdentifier')
+ ->willReturn('backend2');
+ $backend2->method('listAllResources')
+ ->will($this->throwException(new BackendTemporarilyUnavailableException()));
+ $backend2->method('getResource')
+ ->will($this->throwException(new BackendTemporarilyUnavailableException()));
+ $backend2->method('getAllResources')
+ ->will($this->throwException(new BackendTemporarilyUnavailableException()));
+ $backend3->method('getBackendIdentifier')
+ ->willReturn('backend3');
+ $backend3->method('listAllResources')
+ ->willReturn(['res6', 'res7']);
+ $backend3->method('getResource')
+ ->willReturnMap([
+ ['res6', $res6],
+ ['res7', $res7],
+ ]);
+ $backend4->method('getBackendIdentifier')
+ ->willReturn('backend4');
+ $backend4->method('listAllResources')
+ ->willReturn(['res8', 'res9']);
+ $backend4->method('getResource')
+ ->willReturnMap([
+ ['res8', $res8],
+ ['res9', $res9],
+ ]);
+
+ $res6->method('getId')->willReturn('res6');
+ $res6->method('getDisplayName')->willReturn('Pointer123');
+ $res6->method('getGroupRestrictions')->willReturn(['foo', 'biz']);
+ $res6->method('getEMail')->willReturn('res6@foo.bar');
+ $res6->method('getBackend')->willReturn($backend3);
+
+ $res6->method('getAllAvailableMetadataKeys')->willReturn(['meta99', 'meta123']);
+ $res6->method('getMetadataForKey')->willReturnCallback(function ($key) {
+ switch ($key) {
+ case 'meta99':
+ return 'value99-new';
+
+ case 'meta123':
+ return 'meta456';
+
+ default:
+ return null;
+ }
+ });
+
+ $res7->method('getId')->willReturn('res7');
+ $res7->method('getDisplayName')->willReturn('Resource4');
+ $res7->method('getGroupRestrictions')->willReturn(['biz']);
+ $res7->method('getEMail')->willReturn('res7@foo.bar');
+ $res7->method('getBackend')->willReturn($backend3);
+ $res7->method('getAllAvailableMetadataKeys')->willReturn(['meta1']);
+ $res7->method('getMetadataForKey')->willReturnCallback(function ($key) {
+ switch ($key) {
+ case 'meta1':
+ return 'value1';
+
+ default:
+ return null;
+ }
+ });
+
+ $res8->method('getId')->willReturn('res8');
+ $res8->method('getDisplayName')->willReturn('Beamer');
+ $res8->method('getGroupRestrictions')->willReturn([]);
+ $res8->method('getEMail')->willReturn('res8@foo.bar');
+ $res8->method('getBackend')->willReturn($backend4);
+ $res8->method('getAllAvailableMetadataKeys')->willReturn(['meta2']);
+ $res8->method('getMetadataForKey')->willReturnCallback(function ($key) {
+ switch ($key) {
+ case 'meta2':
+ return 'value2';
+
+ default:
+ return null;
+ }
+ });
+
+ $res9->method('getId')->willReturn('res9');
+ $res9->method('getDisplayName')->willReturn('Beamer2');
+ $res9->method('getGroupRestrictions')->willReturn([]);
+ $res9->method('getEMail')->willReturn('res9@foo.bar');
+ $res9->method('getBackend')->willReturn($backend4);
+
+ $this->resourceManager
+ ->method('getBackends')
+ ->willReturn([
+ $backend2, $backend3, $backend4
+ ]);
+ $this->resourceManager
+ ->method('getBackend')
+ ->willReturnMap([
+ ['backend2', $backend2],
+ ['backend3', $backend3],
+ ['backend4', $backend4],
+ ]);
+
+ $this->updater->updateResources();
+ $this->updater->updateRooms();
+
+ $query = self::$realDatabase->getQueryBuilder();
+ $query->select('*')->from('calendar_resources');
+
+ $rows = [];
+ $ids = [];
+ $stmt = $query->execute();
+ while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
+ $ids[$row['backend_id'] . '::' . $row['resource_id']] = $row['id'];
+ unset($row['id']);
+ $rows[] = $row;
+ }
+
+ $this->assertEquals([
+ [
+ 'backend_id' => 'backend1',
+ 'resource_id' => 'res1',
+ 'displayname' => 'Beamer1',
+ 'email' => 'res1@foo.bar',
+ 'group_restrictions' => '[]',
+ ],
+ [
+ 'backend_id' => 'backend1',
+ 'resource_id' => 'res2',
+ 'displayname' => 'TV1',
+ 'email' => 'res2@foo.bar',
+ 'group_restrictions' => '[]',
+ ],
+ [
+ 'backend_id' => 'backend2',
+ 'resource_id' => 'res3',
+ 'displayname' => 'Beamer2',
+ 'email' => 'res3@foo.bar',
+ 'group_restrictions' => '[]',
+ ],
+ [
+ 'backend_id' => 'backend2',
+ 'resource_id' => 'res4',
+ 'displayname' => 'TV2',
+ 'email' => 'res4@foo.bar',
+ 'group_restrictions' => '[]',
+ ],
+ [
+ 'backend_id' => 'backend3',
+ 'resource_id' => 'res6',
+ 'displayname' => 'Pointer123',
+ 'email' => 'res6@foo.bar',
+ 'group_restrictions' => '["foo","biz"]',
+ ],
+ [
+ 'backend_id' => 'backend3',
+ 'resource_id' => 'res7',
+ 'displayname' => 'Resource4',
+ 'email' => 'res7@foo.bar',
+ 'group_restrictions' => '["biz"]',
+ ],
+ [
+ 'backend_id' => 'backend4',
+ 'resource_id' => 'res8',
+ 'displayname' => 'Beamer',
+ 'email' => 'res8@foo.bar',
+ 'group_restrictions' => '[]',
+ ],
+ [
+ 'backend_id' => 'backend4',
+ 'resource_id' => 'res9',
+ 'displayname' => 'Beamer2',
+ 'email' => 'res9@foo.bar',
+ 'group_restrictions' => '[]',
+ ],
+ ], $rows);
+
+ $query2 = self::$realDatabase->getQueryBuilder();
+ $query2->select('*')->from('calendar_resources_md');
+
+ $rows2 = [];
+ $stmt = $query2->execute();
+ while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
+ unset($row['id']);
+ $rows2[] = $row;
+ }
+
+ $this->assertEquals([
+ [
+ 'resource_id' => $ids['backend2::res3'],
+ 'key' => 'meta1',
+ 'value' => 'value1',
+ ],
+ [
+ 'resource_id' => $ids['backend2::res3'],
+ 'key' => 'meta2',
+ 'value' => 'value2',
+ ],
+ [
+ 'resource_id' => $ids['backend2::res4'],
+ 'key' => 'meta1',
+ 'value' => 'value1',
+ ],
+ [
+ 'resource_id' => $ids['backend2::res4'],
+ 'key' => 'meta3',
+ 'value' => 'value3-old',
+ ],
+ [
+ 'resource_id' => $ids['backend3::res6'],
+ 'key' => 'meta99',
+ 'value' => 'value99-new',
+ ],
+ [
+ 'resource_id' => $ids['backend3::res7'],
+ 'key' => 'meta1',
+ 'value' => 'value1',
+ ],
+ [
+ 'resource_id' => $ids['backend3::res6'],
+ 'key' => 'meta123',
+ 'value' => 'meta456',
+ ],
+ [
+ 'resource_id' => $ids['backend4::res8'],
+ 'key' => 'meta2',
+ 'value' => 'value2',
+ ]
+ ], $rows2);
+ }
+
+ protected function createTestResourcesInCache() {
+ $query = self::$realDatabase->getQueryBuilder();
+ $query->insert('calendar_resources')
+ ->values([
+ 'backend_id' => $query->createNamedParameter('backend1'),
+ 'resource_id' => $query->createNamedParameter('res1'),
+ 'email' => $query->createNamedParameter('res1@foo.bar'),
+ 'displayname' => $query->createNamedParameter('Beamer1'),
+ 'group_restrictions' => $query->createNamedParameter('[]'),
+ ])
+ ->execute();
+
+ $query->insert('calendar_resources')
+ ->values([
+ 'backend_id' => $query->createNamedParameter('backend1'),
+ 'resource_id' => $query->createNamedParameter('res2'),
+ 'email' => $query->createNamedParameter('res2@foo.bar'),
+ 'displayname' => $query->createNamedParameter('TV1'),
+ 'group_restrictions' => $query->createNamedParameter('[]'),
+ ])
+ ->execute();
+
+ $query->insert('calendar_resources')
+ ->values([
+ 'backend_id' => $query->createNamedParameter('backend2'),
+ 'resource_id' => $query->createNamedParameter('res3'),
+ 'email' => $query->createNamedParameter('res3@foo.bar'),
+ 'displayname' => $query->createNamedParameter('Beamer2'),
+ 'group_restrictions' => $query->createNamedParameter('[]'),
+ ])
+ ->execute();
+ $id3 = $query->getLastInsertId();
+
+ $query->insert('calendar_resources')
+ ->values([
+ 'backend_id' => $query->createNamedParameter('backend2'),
+ 'resource_id' => $query->createNamedParameter('res4'),
+ 'email' => $query->createNamedParameter('res4@foo.bar'),
+ 'displayname' => $query->createNamedParameter('TV2'),
+ 'group_restrictions' => $query->createNamedParameter('[]'),
+ ])
+ ->execute();
+ $id4 = $query->getLastInsertId();
+
+ $query->insert('calendar_resources')
+ ->values([
+ 'backend_id' => $query->createNamedParameter('backend3'),
+ 'resource_id' => $query->createNamedParameter('res5'),
+ 'email' => $query->createNamedParameter('res5@foo.bar'),
+ 'displayname' => $query->createNamedParameter('Beamer3'),
+ 'group_restrictions' => $query->createNamedParameter('[]'),
+ ])
+ ->execute();
+
+ $query->insert('calendar_resources')
+ ->values([
+ 'backend_id' => $query->createNamedParameter('backend3'),
+ 'resource_id' => $query->createNamedParameter('res6'),
+ 'email' => $query->createNamedParameter('res6@foo.bar'),
+ 'displayname' => $query->createNamedParameter('Pointer'),
+ 'group_restrictions' => $query->createNamedParameter('["foo", "bar"]'),
+ ])
+ ->execute();
+ $id6 = $query->getLastInsertId();
+
+ $query->insert('calendar_resources_md')
+ ->values([
+ 'resource_id' => $query->createNamedParameter($id3),
+ 'key' => $query->createNamedParameter('meta1'),
+ 'value' => $query->createNamedParameter('value1')
+ ])
+ ->execute();
+ $query->insert('calendar_resources_md')
+ ->values([
+ 'resource_id' => $query->createNamedParameter($id3),
+ 'key' => $query->createNamedParameter('meta2'),
+ 'value' => $query->createNamedParameter('value2')
+ ])
+ ->execute();
+ $query->insert('calendar_resources_md')
+ ->values([
+ 'resource_id' => $query->createNamedParameter($id4),
+ 'key' => $query->createNamedParameter('meta1'),
+ 'value' => $query->createNamedParameter('value1')
+ ])
+ ->execute();
+ $query->insert('calendar_resources_md')
+ ->values([
+ 'resource_id' => $query->createNamedParameter($id4),
+ 'key' => $query->createNamedParameter('meta3'),
+ 'value' => $query->createNamedParameter('value3-old')
+ ])
+ ->execute();
+ $query->insert('calendar_resources_md')
+ ->values([
+ 'resource_id' => $query->createNamedParameter($id6),
+ 'key' => $query->createNamedParameter('meta99'),
+ 'value' => $query->createNamedParameter('value99')
+ ])
+ ->execute();
+ }
+}
diff --git a/tests/lib/Calendar/Room/ManagerTest.php b/tests/lib/Calendar/Room/ManagerTest.php
index 587503da312..e0f2de970ed 100644
--- a/tests/lib/Calendar/Room/ManagerTest.php
+++ b/tests/lib/Calendar/Room/ManagerTest.php
@@ -12,6 +12,7 @@ namespace Test\Calendar\Room;
use OC\AppFramework\Bootstrap\Coordinator;
use OC\AppFramework\Bootstrap\RegistrationContext;
use OC\AppFramework\Bootstrap\ServiceRegistration;
+use OC\Calendar\ResourcesRoomsUpdater;
use OC\Calendar\Room\Manager;
use OCP\Calendar\Room\IBackend;
use OCP\IServerContainer;
@@ -25,6 +26,9 @@ class ManagerTest extends TestCase {
/** @var IServerContainer|MockObject */
private $server;
+ /** @var ResourcesRoomsUpdater|MockObject */
+ private $resourcesRoomsUpdater;
+
/** @var Manager */
private $manager;
@@ -33,9 +37,12 @@ class ManagerTest extends TestCase {
$this->coordinator = $this->createMock(Coordinator::class);
$this->server = $this->createMock(IServerContainer::class);
+ $this->resourcesRoomsUpdater = $this->createMock(ResourcesRoomsUpdater::class);
+
$this->manager = new Manager(
$this->coordinator,
$this->server,
+ $this->resourcesRoomsUpdater,
);
}
@@ -128,4 +135,11 @@ class ManagerTest extends TestCase {
self::assertEquals([], $this->manager->getBackends());
}
+
+ public function testUpdate(): void {
+ $this->resourcesRoomsUpdater->expects(self::once())
+ ->method('updateRooms');
+
+ $this->manager->update();
+ }
}