From 31d6138f4681c322c0a802e541f3b81c28840a74 Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Thu, 1 Aug 2019 14:03:34 +0200 Subject: [PATCH] Test sync of resource and room metadata Signed-off-by: Georg Ehrke --- ...alendarResourcesRoomsBackgroundJobTest.php | 190 ++++++++++++++++-- 1 file changed, 171 insertions(+), 19 deletions(-) diff --git a/apps/dav/tests/unit/BackgroundJob/UpdateCalendarResourcesRoomsBackgroundJobTest.php b/apps/dav/tests/unit/BackgroundJob/UpdateCalendarResourcesRoomsBackgroundJobTest.php index e012d5e3f18..e451570b73f 100644 --- a/apps/dav/tests/unit/BackgroundJob/UpdateCalendarResourcesRoomsBackgroundJobTest.php +++ b/apps/dav/tests/unit/BackgroundJob/UpdateCalendarResourcesRoomsBackgroundJobTest.php @@ -26,6 +26,7 @@ use OCA\DAV\BackgroundJob\UpdateCalendarResourcesRoomsBackgroundJob; 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; @@ -61,34 +62,38 @@ class UpdateCalendarResourcesRoomsBackgroundJobTest extends TestCase { protected function tearDown() { $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, {}] - * [backend2, res4, TV2, {}] - * [backend3, res5, Beamer3, {}] - * [backend3, res6, Pointer, {foo, bar}] + * [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}] - * [backend3, res7, Resource4, {biz}] - * [backend4, res8, Beamer, {}] - * [backend4, res9, Beamer2, {}] + * [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: - * [backend2, res3, Beamer2, {}] - * [backend2, res4, TV2, {}] - * [backend3, res6, Pointer123, {foo, biz}] - * [backend3, res7, Resource4, {biz}] - * [backend4, res8, Beamer, {}] - * [backend4, res9, Beamer2, {}] + * [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 testRun() { @@ -98,9 +103,9 @@ class UpdateCalendarResourcesRoomsBackgroundJobTest extends TestCase { $backend3 = $this->createMock(IBackend::class); $backend4 = $this->createMock(IBackend::class); - $res6 = $this->createMock(IResource::class); - $res7 = $this->createMock(IResource::class); - $res8 = $this->createMock(IResource::class); + $res6 = $this->createMock([IResource::class, IMetadataProvider::class]); + $res7 = $this->createMock([IResource::class, IMetadataProvider::class]); + $res8 = $this->createMock([IResource::class, IMetadataProvider::class]); $res9 = $this->createMock(IResource::class); $backend2->method('getBackendIdentifier') @@ -136,17 +141,51 @@ class UpdateCalendarResourcesRoomsBackgroundJobTest extends TestCase { $res6->method('getEMail')->will($this->returnValue('res6@foo.bar')); $res6->method('getBackend')->will($this->returnValue($backend3)); + $res6->method('getAllAvailableMetadataKeys')->will($this->returnValue(['meta99', 'meta123'])); + $res6->method('getMetadataForKey')->will($this->returnCallback(function($key) { + switch($key) { + case 'meta99': + return 'value99-new'; + + case 'meta123': + return 'meta456'; + + default: + return null; + } + })); + $res7->method('getId')->will($this->returnValue('res7')); $res7->method('getDisplayName')->will($this->returnValue('Resource4')); $res7->method('getGroupRestrictions')->will($this->returnValue(['biz'])); $res7->method('getEMail')->will($this->returnValue('res7@foo.bar')); $res7->method('getBackend')->will($this->returnValue($backend3)); + $res7->method('getAllAvailableMetadataKeys')->will($this->returnValue(['meta1'])); + $res7->method('getMetadataForKey')->will($this->returnCallback(function($key) { + switch($key) { + case 'meta1': + return 'value1'; + + default: + return null; + } + })); $res8->method('getId')->will($this->returnValue('res8')); $res8->method('getDisplayName')->will($this->returnValue('Beamer')); $res8->method('getGroupRestrictions')->will($this->returnValue([])); $res8->method('getEMail')->will($this->returnValue('res8@foo.bar')); $res8->method('getBackend')->will($this->returnValue($backend4)); + $res8->method('getAllAvailableMetadataKeys')->will($this->returnValue(['meta2'])); + $res8->method('getMetadataForKey')->will($this->returnCallback(function($key) { + switch($key) { + case 'meta2': + return 'value2'; + + default: + return null; + } + })); $res9->method('getId')->will($this->returnValue('res9')); $res9->method('getDisplayName')->will($this->returnValue('Beamer2')); @@ -173,13 +212,29 @@ class UpdateCalendarResourcesRoomsBackgroundJobTest extends TestCase { $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', @@ -223,6 +278,59 @@ class UpdateCalendarResourcesRoomsBackgroundJobTest extends TestCase { '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() { @@ -236,6 +344,7 @@ class UpdateCalendarResourcesRoomsBackgroundJobTest extends TestCase { 'group_restrictions' => $query->createNamedParameter('[]'), ]) ->execute(); + $query->insert('calendar_resources') ->values([ 'backend_id' => $query->createNamedParameter('backend1'), @@ -245,6 +354,7 @@ class UpdateCalendarResourcesRoomsBackgroundJobTest extends TestCase { 'group_restrictions' => $query->createNamedParameter('[]'), ]) ->execute(); + $query->insert('calendar_resources') ->values([ 'backend_id' => $query->createNamedParameter('backend2'), @@ -254,6 +364,8 @@ class UpdateCalendarResourcesRoomsBackgroundJobTest extends TestCase { 'group_restrictions' => $query->createNamedParameter('[]'), ]) ->execute(); + $id3 = $query->getLastInsertId(); + $query->insert('calendar_resources') ->values([ 'backend_id' => $query->createNamedParameter('backend2'), @@ -263,6 +375,8 @@ class UpdateCalendarResourcesRoomsBackgroundJobTest extends TestCase { 'group_restrictions' => $query->createNamedParameter('[]'), ]) ->execute(); + $id4 = $query->getLastInsertId(); + $query->insert('calendar_resources') ->values([ 'backend_id' => $query->createNamedParameter('backend3'), @@ -272,6 +386,7 @@ class UpdateCalendarResourcesRoomsBackgroundJobTest extends TestCase { 'group_restrictions' => $query->createNamedParameter('[]'), ]) ->execute(); + $query->insert('calendar_resources') ->values([ 'backend_id' => $query->createNamedParameter('backend3'), @@ -281,5 +396,42 @@ class UpdateCalendarResourcesRoomsBackgroundJobTest extends TestCase { '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(); } } -- 2.39.5