diff options
author | Georg Ehrke <developer@georgehrke.com> | 2018-06-19 13:22:30 +0300 |
---|---|---|
committer | Georg Ehrke <developer@georgehrke.com> | 2018-06-25 05:59:04 +0300 |
commit | cba783f5df8b0129d6b95056ef299e504fed4094 (patch) | |
tree | 0d9744ee201a041822277607181e8d81e3d04d63 | |
parent | da533d747088633535e5d5281da433753d3cfc81 (diff) |
Calendar resource booking: unit tests / adding missing annotations
Signed-off-by: Georg Ehrke <developer@georgehrke.com>
9 files changed, 505 insertions, 4 deletions
diff --git a/apps/dav/lib/BackgroundJob/UpdateCalendarResourcesRoomsBackgroundJob.php b/apps/dav/lib/BackgroundJob/UpdateCalendarResourcesRoomsBackgroundJob.php index 0c78f713605..a01540a6292 100644 --- a/apps/dav/lib/BackgroundJob/UpdateCalendarResourcesRoomsBackgroundJob.php +++ b/apps/dav/lib/BackgroundJob/UpdateCalendarResourcesRoomsBackgroundJob.php @@ -243,7 +243,7 @@ class UpdateCalendarResourcesRoomsBackgroundJob extends TimedJob { $sorted = [ 'new' => [], 'deleted' => [], - 'existing' => [], + 'edited' => [], ]; $backendIds = array_merge(array_keys($cached), array_keys($remote)); @@ -251,11 +251,11 @@ class UpdateCalendarResourcesRoomsBackgroundJob extends TimedJob { if (!isset($cached[$backendId])) { $sorted['new'][$backendId] = $remote[$backendId]; } elseif (!isset($remote[$backendId])) { - $sorted['deleted'][$backendId] = $remote[$backendId]; + $sorted['deleted'][$backendId] = $cached[$backendId]; } else { $sorted['new'][$backendId] = array_diff($remote[$backendId], $cached[$backendId]); $sorted['deleted'][$backendId] = array_diff($cached[$backendId], $remote[$backendId]); - $sorted['existing'][$backendId] = array_intersect($remote[$backendId], $cached[$backendId]); + $sorted['edited'][$backendId] = array_intersect($remote[$backendId], $cached[$backendId]); } } @@ -272,7 +272,7 @@ class UpdateCalendarResourcesRoomsBackgroundJob extends TimedJob { $query = $this->db->getQueryBuilder(); $query->insert($table) ->values([ - 'backend_id' => $query->createNamedParameter($remote->getBackend()), + 'backend_id' => $query->createNamedParameter($remote->getBackend()->getBackendIdentifier()), 'resource_id' => $query->createNamedParameter($remote->getId()), 'email' => $query->createNamedParameter($remote->getEMail()), 'displayname' => $query->createNamedParameter($remote->getDisplayName()), @@ -320,6 +320,8 @@ class UpdateCalendarResourcesRoomsBackgroundJob extends TimedJob { $this->serializeGroupRestrictions( $remote->getGroupRestrictions() ))) + ->where($query->expr()->eq('backend_id', $query->createNamedParameter($remote->getBackend()->getBackendIdentifier()))) + ->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($remote->getId()))) ->execute(); } diff --git a/apps/dav/tests/unit/BackgroundJob/UpdateCalendarResourcesRoomsBackgroundJobTest.php b/apps/dav/tests/unit/BackgroundJob/UpdateCalendarResourcesRoomsBackgroundJobTest.php new file mode 100644 index 00000000000..56f768ceda0 --- /dev/null +++ b/apps/dav/tests/unit/BackgroundJob/UpdateCalendarResourcesRoomsBackgroundJobTest.php @@ -0,0 +1,285 @@ +<?php +/** + * @copyright Copyright (c) 2018, Georg Ehrke + * + * @author Georg Ehrke <oc.list@georgehrke.com> + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\DAV\Tests\unit\BackgroundJob; + +use OCA\DAV\BackgroundJob\UpdateCalendarResourcesRoomsBackgroundJob; + +use OCA\DAV\CalDAV\CalDavBackend; +use OCP\Calendar\BackendTemporarilyUnavailableException; +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 Test\TestCase; + +class UpdateCalendarResourcesRoomsBackgroundJobTest extends TestCase { + + /** @var UpdateCalendarResourcesRoomsBackgroundJob */ + private $backgroundJob; + + /** @var IResourceManager | \PHPUnit_Framework_MockObject_MockObject */ + private $resourceManager; + + /** @var IRoomManager | \PHPUnit_Framework_MockObject_MockObject */ + private $roomManager; + + /** @var CalDavBackend | \PHPUnit_Framework_MockObject_MockObject */ + private $calDavBackend; + + protected function setUp() { + parent::setUp(); + + $this->resourceManager = $this->createMock(IResourceManager::class); + $this->roomManager = $this->createMock(IRoomManager::class); + $this->calDavBackend = $this->createMock(CalDavBackend::class); + + $this->backgroundJob = new UpdateCalendarResourcesRoomsBackgroundJob( + $this->resourceManager, $this->roomManager, self::$realDatabase, + $this->calDavBackend); + } + + protected function tearDown() { + $query = self::$realDatabase->getQueryBuilder(); + $query->delete('calendar_resources_cache')->execute(); + $query->delete('calendar_rooms_cache')->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}] + * + * Data in Backend: + * backend1 gone + * backend2 throws BackendTemporarilyUnavailableException + * [backend3, res6, Pointer123, {foo, biz}] + * [backend3, res7, Resource4, {biz}] + * [backend4, res8, Beamer, {}] + * [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, {}] + */ + + public function testRun() { + $this->createTestResourcesInCache(); + + $backend2 = $this->createMock(IBackend::class); + $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); + $res9 = $this->createMock(IResource::class); + + $backend2->method('getBackendIdentifier') + ->will($this->returnValue('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') + ->will($this->returnValue('backend3')); + $backend3->method('listAllResources') + ->will($this->returnValue(['res6', 'res7'])); + $backend3->method('getResource') + ->will($this->returnValueMap([ + ['res6', $res6], + ['res7', $res7], + ])); + $backend4->method('getBackendIdentifier') + ->will($this->returnValue('backend4')); + $backend4->method('listAllResources') + ->will($this->returnValue(['res8', 'res9'])); + $backend4->method('getResource') + ->will($this->returnValueMap([ + ['res8', $res8], + ['res9', $res9], + ])); + + $res6->method('getId')->will($this->returnValue('res6')); + $res6->method('getDisplayName')->will($this->returnValue('Pointer123')); + $res6->method('getGroupRestrictions')->will($this->returnValue(['foo', 'biz'])); + $res6->method('getEMail')->will($this->returnValue('res6@foo.bar')); + $res6->method('getBackend')->will($this->returnValue($backend3)); + + $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)); + + $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)); + + $res9->method('getId')->will($this->returnValue('res9')); + $res9->method('getDisplayName')->will($this->returnValue('Beamer2')); + $res9->method('getGroupRestrictions')->will($this->returnValue([])); + $res9->method('getEMail')->will($this->returnValue('res9@foo.bar')); + $res9->method('getBackend')->will($this->returnValue($backend4)); + + $this->resourceManager + ->method('getBackends') + ->will($this->returnValue([ + $backend2, $backend3, $backend4 + ])); + $this->resourceManager + ->method('getBackend') + ->will($this->returnValueMap([ + ['backend2', $backend2], + ['backend3', $backend3], + ['backend4', $backend4], + ])); + + $this->backgroundJob->run([]); + + $query = self::$realDatabase->getQueryBuilder(); + $query->select('*')->from('calendar_resources_cache'); + + $rows = []; + $stmt = $query->execute(); + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { + unset($row['id']); + $rows[] = $row; + } + + $this->assertEquals([ + [ + '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); + } + + protected function createTestResourcesInCache() { + $query = self::$realDatabase->getQueryBuilder(); + $query->insert('calendar_resources_cache') + ->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_cache') + ->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_cache') + ->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(); + $query->insert('calendar_resources_cache') + ->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(); + $query->insert('calendar_resources_cache') + ->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_cache') + ->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(); + } +} diff --git a/lib/public/Calendar/BackendTemporarilyUnavailableException.php b/lib/public/Calendar/BackendTemporarilyUnavailableException.php index 61c1934c342..632a802359c 100644 --- a/lib/public/Calendar/BackendTemporarilyUnavailableException.php +++ b/lib/public/Calendar/BackendTemporarilyUnavailableException.php @@ -23,4 +23,10 @@ namespace OCP\Calendar; +/** + * Class BackendTemporarilyUnavailableException + * + * @package OCP\Calendar + * @since 14.0.0 + */ class BackendTemporarilyUnavailableException extends \Exception {} diff --git a/lib/public/Calendar/Resource/IBackend.php b/lib/public/Calendar/Resource/IBackend.php index 564c9e008b5..24635219fe0 100644 --- a/lib/public/Calendar/Resource/IBackend.php +++ b/lib/public/Calendar/Resource/IBackend.php @@ -37,6 +37,7 @@ interface IBackend { * * @throws BackendTemporarilyUnavailableException * @return IResource[] + * @since 14.0.0 */ public function getAllResources():array; @@ -45,6 +46,7 @@ interface IBackend { * * @throws BackendTemporarilyUnavailableException * @return string[] + * @since 14.0.0 */ public function listAllResources():array; @@ -54,6 +56,7 @@ interface IBackend { * @param string $id * @throws BackendTemporarilyUnavailableException * @return IResource|null + * @since 14.0.0 */ public function getResource($id); diff --git a/lib/public/Calendar/Resource/IManager.php b/lib/public/Calendar/Resource/IManager.php index d2ec7350dad..8542e13eba7 100644 --- a/lib/public/Calendar/Resource/IManager.php +++ b/lib/public/Calendar/Resource/IManager.php @@ -23,6 +23,12 @@ namespace OCP\Calendar\Resource; +/** + * Interface IManager + * + * @package OCP\Calendar\Resource + * @since 14.0.0 + */ interface IManager { /** @@ -52,6 +58,7 @@ interface IManager { /** * @param string $backendId * @return IBackend + * @since 14.0.0 */ public function getBackend($backendId):IBackend; diff --git a/lib/public/Calendar/Room/IBackend.php b/lib/public/Calendar/Room/IBackend.php index 0d4c3ef678f..f675012ab39 100644 --- a/lib/public/Calendar/Room/IBackend.php +++ b/lib/public/Calendar/Room/IBackend.php @@ -37,6 +37,7 @@ interface IBackend { * * @throws BackendTemporarilyUnavailableException * @return IRoom[] + * @since 14.0.0 */ public function getAllRooms():array; @@ -45,6 +46,7 @@ interface IBackend { * * @throws BackendTemporarilyUnavailableException * @return string[] + * @since 14.0.0 */ public function listAllRooms():array; @@ -54,6 +56,7 @@ interface IBackend { * @param string $id * @throws BackendTemporarilyUnavailableException * @return IRoom|null + * @since 14.0.0 */ public function getRoom($id); diff --git a/lib/public/Calendar/Room/IManager.php b/lib/public/Calendar/Room/IManager.php index 6f55f0f76c0..39e85c43e45 100644 --- a/lib/public/Calendar/Room/IManager.php +++ b/lib/public/Calendar/Room/IManager.php @@ -23,6 +23,12 @@ namespace OCP\Calendar\Room; +/** + * Interface IManager + * + * @package OCP\Calendar\Room + * @since 14.0.0 + */ interface IManager { /** @@ -52,6 +58,7 @@ interface IManager { /** * @param string $backendId * @return IBackend + * @since 14.0.0 */ public function getBackend($backendId):IBackend; diff --git a/tests/lib/Calendar/Resource/ManagerTest.php b/tests/lib/Calendar/Resource/ManagerTest.php new file mode 100644 index 00000000000..ccc6bbf182b --- /dev/null +++ b/tests/lib/Calendar/Resource/ManagerTest.php @@ -0,0 +1,94 @@ +<?php +/** + * @copyright 2018, Georg Ehrke <oc.list@georgehrke.com> + * + * @author Georg Ehrke <oc.list@georgehrke.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace Test\Calendar\Resource; + +use \OC\Calendar\Resource\Manager; +use \OCP\Calendar\Resource\IBackend; +use \Test\TestCase; + +class ManagerTest extends TestCase { + + /** @var Manager */ + private $manager; + + protected function setUp() { + parent::setUp(); + + $this->manager = new Manager(); + } + + public function testRegisterUnregisterBackend() { + $backend1 = $this->createMock(IBackend::class); + $backend1->method('getBackendIdentifier')->will($this->returnValue('backend_1')); + + $backend2 = $this->createMock(IBackend::class); + $backend2->method('getBackendIdentifier')->will($this->returnValue('backend_2')); + + $this->manager->registerBackend($backend1); + $this->manager->registerBackend($backend2); + + $this->assertEquals([ + $backend1, $backend2 + ], $this->manager->getBackends()); + + $this->manager->unregisterBackend($backend1); + + $this->assertEquals([ + $backend2 + ], $this->manager->getBackends()); + } + + public function testGetBackend() { + $backend1 = $this->createMock(IBackend::class); + $backend1->method('getBackendIdentifier')->will($this->returnValue('backend_1')); + + $backend2 = $this->createMock(IBackend::class); + $backend2->method('getBackendIdentifier')->will($this->returnValue('backend_2')); + + $this->manager->registerBackend($backend1); + $this->manager->registerBackend($backend2); + + $this->assertEquals($backend1, $this->manager->getBackend('backend_1')); + $this->assertEquals($backend2, $this->manager->getBackend('backend_2')); + } + + public function testClear() { + $backend1 = $this->createMock(IBackend::class); + $backend1->method('getBackendIdentifier')->will($this->returnValue('backend_1')); + + $backend2 = $this->createMock(IBackend::class); + $backend2->method('getBackendIdentifier')->will($this->returnValue('backend_2')); + + $this->manager->registerBackend($backend1); + $this->manager->registerBackend($backend2); + + $this->assertEquals([ + $backend1, $backend2 + ], $this->manager->getBackends()); + + $this->manager->clear(); + + $this->assertEquals([], $this->manager->getBackends()); + } +} diff --git a/tests/lib/Calendar/Room/ManagerTest.php b/tests/lib/Calendar/Room/ManagerTest.php new file mode 100644 index 00000000000..34d69d127e2 --- /dev/null +++ b/tests/lib/Calendar/Room/ManagerTest.php @@ -0,0 +1,94 @@ +<?php +/** + * @copyright 2018, Georg Ehrke <oc.list@georgehrke.com> + * + * @author Georg Ehrke <oc.list@georgehrke.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace Test\Calendar\Room; + +use \OC\Calendar\Room\Manager; +use \OCP\Calendar\Room\IBackend; +use \Test\TestCase; + +class ManagerTest extends TestCase { + + /** @var Manager */ + private $manager; + + protected function setUp() { + parent::setUp(); + + $this->manager = new Manager(); + } + + public function testRegisterUnregisterBackend() { + $backend1 = $this->createMock(IBackend::class); + $backend1->method('getBackendIdentifier')->will($this->returnValue('backend_1')); + + $backend2 = $this->createMock(IBackend::class); + $backend2->method('getBackendIdentifier')->will($this->returnValue('backend_2')); + + $this->manager->registerBackend($backend1); + $this->manager->registerBackend($backend2); + + $this->assertEquals([ + $backend1, $backend2 + ], $this->manager->getBackends()); + + $this->manager->unregisterBackend($backend1); + + $this->assertEquals([ + $backend2 + ], $this->manager->getBackends()); + } + + public function testGetBackend() { + $backend1 = $this->createMock(IBackend::class); + $backend1->method('getBackendIdentifier')->will($this->returnValue('backend_1')); + + $backend2 = $this->createMock(IBackend::class); + $backend2->method('getBackendIdentifier')->will($this->returnValue('backend_2')); + + $this->manager->registerBackend($backend1); + $this->manager->registerBackend($backend2); + + $this->assertEquals($backend1, $this->manager->getBackend('backend_1')); + $this->assertEquals($backend2, $this->manager->getBackend('backend_2')); + } + + public function testClear() { + $backend1 = $this->createMock(IBackend::class); + $backend1->method('getBackendIdentifier')->will($this->returnValue('backend_1')); + + $backend2 = $this->createMock(IBackend::class); + $backend2->method('getBackendIdentifier')->will($this->returnValue('backend_2')); + + $this->manager->registerBackend($backend1); + $this->manager->registerBackend($backend2); + + $this->assertEquals([ + $backend1, $backend2 + ], $this->manager->getBackends()); + + $this->manager->clear(); + + $this->assertEquals([], $this->manager->getBackends()); + } +} |