1
0

ResourcesRoomsUpdaterTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Calendar;
  8. use OC\Calendar\ResourcesRoomsUpdater;
  9. use OCA\DAV\CalDAV\CalDavBackend;
  10. use OCP\Calendar\BackendTemporarilyUnavailableException;
  11. use OCP\Calendar\IMetadataProvider;
  12. use OCP\Calendar\Resource\IBackend;
  13. use OCP\Calendar\Resource\IManager as IResourceManager;
  14. use OCP\Calendar\Resource\IResource;
  15. use OCP\Calendar\Room\IManager as IRoomManager;
  16. use PHPUnit\Framework\MockObject\MockObject;
  17. use Psr\Container\ContainerInterface;
  18. use Test\TestCase;
  19. interface tmpI extends IResource, IMetadataProvider {
  20. }
  21. class ResourcesRoomsUpdaterTest extends TestCase {
  22. private ResourcesRoomsUpdater $updater;
  23. /** @var IResourceManager|MockObject */
  24. private $resourceManager;
  25. /** @var IRoomManager|MockObject */
  26. private $roomManager;
  27. /** @var ContainerInterface|MockObject */
  28. private $container;
  29. /** @var CalDavBackend|MockObject */
  30. private $calDavBackend;
  31. protected function setUp(): void {
  32. parent::setUp();
  33. $this->resourceManager = $this->createMock(IResourceManager::class);
  34. $this->roomManager = $this->createMock(IRoomManager::class);
  35. $this->container = $this->createMock(ContainerInterface::class);
  36. $this->calDavBackend = $this->createMock(CalDavBackend::class);
  37. $this->container->method('get')
  38. ->willReturnMap([
  39. [IResourceManager::class, $this->resourceManager],
  40. [IRoomManager::class, $this->roomManager],
  41. ]);
  42. $this->updater = new ResourcesRoomsUpdater(
  43. $this->container,
  44. self::$realDatabase,
  45. $this->calDavBackend
  46. );
  47. }
  48. protected function tearDown(): void {
  49. $query = self::$realDatabase->getQueryBuilder();
  50. $query->delete('calendar_resources')->execute();
  51. $query->delete('calendar_resources_md')->execute();
  52. $query->delete('calendar_rooms')->execute();
  53. $query->delete('calendar_rooms_md')->execute();
  54. }
  55. /**
  56. * Data in Cache:
  57. * resources:
  58. * [backend1, res1, Beamer1, {}] - []
  59. * [backend1, res2, TV1, {}] - []
  60. * [backend2, res3, Beamer2, {}] - ['meta1' => 'value1', 'meta2' => 'value2']
  61. * [backend2, res4, TV2, {}] - ['meta1' => 'value1', 'meta3' => 'value3-old']
  62. * [backend3, res5, Beamer3, {}] - []
  63. * [backend3, res6, Pointer, {foo, bar}] - ['meta99' => 'value99']
  64. *
  65. * Data in Backend:
  66. * backend1 gone
  67. * backend2 throws BackendTemporarilyUnavailableException
  68. * [backend3, res6, Pointer123, {foo, biz}] - ['meta99' => 'value99-new', 'meta123' => 'meta456']
  69. * [backend3, res7, Resource4, {biz}] - ['meta1' => 'value1']
  70. * [backend4, res8, Beamer, {}] - ['meta2' => 'value2']
  71. * [backend4, res9, Beamer2, {}] - []
  72. *
  73. * Expected after run:
  74. * [backend1, res1, Beamer1, {}] - []
  75. * [backend1, res2, TV1, {}] - []
  76. * [backend2, res3, Beamer2, {}] - ['meta1' => 'value1', 'meta2' => 'value2']
  77. * [backend2, res4, TV2, {}] - ['meta1' => 'value1', 'meta3' => 'value3-old']
  78. * [backend3, res6, Pointer123, {foo, biz}] - ['meta99' => 'value99-new', 'meta123' => 'meta456']
  79. * [backend3, res7, Resource4, {biz}] - ['meta1' => 'value1']
  80. * [backend4, res8, Beamer, {}] - ['meta2' => 'value2']
  81. * [backend4, res9, Beamer2, {}] - []
  82. */
  83. public function testUpdateBoth(): void {
  84. $this->createTestResourcesInCache();
  85. $backend2 = $this->createMock(IBackend::class);
  86. $backend3 = $this->createMock(IBackend::class);
  87. $backend4 = $this->createMock(IBackend::class);
  88. $res6 = $this->createMock(tmpI::class);
  89. $res7 = $this->createMock(tmpI::class);
  90. $res8 = $this->createMock(tmpI::class);
  91. $res9 = $this->createMock(IResource::class);
  92. $backend2->method('getBackendIdentifier')
  93. ->willReturn('backend2');
  94. $backend2->method('listAllResources')
  95. ->will($this->throwException(new BackendTemporarilyUnavailableException()));
  96. $backend2->method('getResource')
  97. ->will($this->throwException(new BackendTemporarilyUnavailableException()));
  98. $backend2->method('getAllResources')
  99. ->will($this->throwException(new BackendTemporarilyUnavailableException()));
  100. $backend3->method('getBackendIdentifier')
  101. ->willReturn('backend3');
  102. $backend3->method('listAllResources')
  103. ->willReturn(['res6', 'res7']);
  104. $backend3->method('getResource')
  105. ->willReturnMap([
  106. ['res6', $res6],
  107. ['res7', $res7],
  108. ]);
  109. $backend4->method('getBackendIdentifier')
  110. ->willReturn('backend4');
  111. $backend4->method('listAllResources')
  112. ->willReturn(['res8', 'res9']);
  113. $backend4->method('getResource')
  114. ->willReturnMap([
  115. ['res8', $res8],
  116. ['res9', $res9],
  117. ]);
  118. $res6->method('getId')->willReturn('res6');
  119. $res6->method('getDisplayName')->willReturn('Pointer123');
  120. $res6->method('getGroupRestrictions')->willReturn(['foo', 'biz']);
  121. $res6->method('getEMail')->willReturn('res6@foo.bar');
  122. $res6->method('getBackend')->willReturn($backend3);
  123. $res6->method('getAllAvailableMetadataKeys')->willReturn(['meta99', 'meta123']);
  124. $res6->method('getMetadataForKey')->willReturnCallback(function ($key) {
  125. switch ($key) {
  126. case 'meta99':
  127. return 'value99-new';
  128. case 'meta123':
  129. return 'meta456';
  130. default:
  131. return null;
  132. }
  133. });
  134. $res7->method('getId')->willReturn('res7');
  135. $res7->method('getDisplayName')->willReturn('Resource4');
  136. $res7->method('getGroupRestrictions')->willReturn(['biz']);
  137. $res7->method('getEMail')->willReturn('res7@foo.bar');
  138. $res7->method('getBackend')->willReturn($backend3);
  139. $res7->method('getAllAvailableMetadataKeys')->willReturn(['meta1']);
  140. $res7->method('getMetadataForKey')->willReturnCallback(function ($key) {
  141. switch ($key) {
  142. case 'meta1':
  143. return 'value1';
  144. default:
  145. return null;
  146. }
  147. });
  148. $res8->method('getId')->willReturn('res8');
  149. $res8->method('getDisplayName')->willReturn('Beamer');
  150. $res8->method('getGroupRestrictions')->willReturn([]);
  151. $res8->method('getEMail')->willReturn('res8@foo.bar');
  152. $res8->method('getBackend')->willReturn($backend4);
  153. $res8->method('getAllAvailableMetadataKeys')->willReturn(['meta2']);
  154. $res8->method('getMetadataForKey')->willReturnCallback(function ($key) {
  155. switch ($key) {
  156. case 'meta2':
  157. return 'value2';
  158. default:
  159. return null;
  160. }
  161. });
  162. $res9->method('getId')->willReturn('res9');
  163. $res9->method('getDisplayName')->willReturn('Beamer2');
  164. $res9->method('getGroupRestrictions')->willReturn([]);
  165. $res9->method('getEMail')->willReturn('res9@foo.bar');
  166. $res9->method('getBackend')->willReturn($backend4);
  167. $this->resourceManager
  168. ->method('getBackends')
  169. ->willReturn([
  170. $backend2, $backend3, $backend4
  171. ]);
  172. $this->resourceManager
  173. ->method('getBackend')
  174. ->willReturnMap([
  175. ['backend2', $backend2],
  176. ['backend3', $backend3],
  177. ['backend4', $backend4],
  178. ]);
  179. $this->updater->updateResources();
  180. $this->updater->updateRooms();
  181. $query = self::$realDatabase->getQueryBuilder();
  182. $query->select('*')->from('calendar_resources');
  183. $rows = [];
  184. $ids = [];
  185. $stmt = $query->execute();
  186. while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
  187. $ids[$row['backend_id'] . '::' . $row['resource_id']] = $row['id'];
  188. unset($row['id']);
  189. $rows[] = $row;
  190. }
  191. $this->assertEquals([
  192. [
  193. 'backend_id' => 'backend1',
  194. 'resource_id' => 'res1',
  195. 'displayname' => 'Beamer1',
  196. 'email' => 'res1@foo.bar',
  197. 'group_restrictions' => '[]',
  198. ],
  199. [
  200. 'backend_id' => 'backend1',
  201. 'resource_id' => 'res2',
  202. 'displayname' => 'TV1',
  203. 'email' => 'res2@foo.bar',
  204. 'group_restrictions' => '[]',
  205. ],
  206. [
  207. 'backend_id' => 'backend2',
  208. 'resource_id' => 'res3',
  209. 'displayname' => 'Beamer2',
  210. 'email' => 'res3@foo.bar',
  211. 'group_restrictions' => '[]',
  212. ],
  213. [
  214. 'backend_id' => 'backend2',
  215. 'resource_id' => 'res4',
  216. 'displayname' => 'TV2',
  217. 'email' => 'res4@foo.bar',
  218. 'group_restrictions' => '[]',
  219. ],
  220. [
  221. 'backend_id' => 'backend3',
  222. 'resource_id' => 'res6',
  223. 'displayname' => 'Pointer123',
  224. 'email' => 'res6@foo.bar',
  225. 'group_restrictions' => '["foo","biz"]',
  226. ],
  227. [
  228. 'backend_id' => 'backend3',
  229. 'resource_id' => 'res7',
  230. 'displayname' => 'Resource4',
  231. 'email' => 'res7@foo.bar',
  232. 'group_restrictions' => '["biz"]',
  233. ],
  234. [
  235. 'backend_id' => 'backend4',
  236. 'resource_id' => 'res8',
  237. 'displayname' => 'Beamer',
  238. 'email' => 'res8@foo.bar',
  239. 'group_restrictions' => '[]',
  240. ],
  241. [
  242. 'backend_id' => 'backend4',
  243. 'resource_id' => 'res9',
  244. 'displayname' => 'Beamer2',
  245. 'email' => 'res9@foo.bar',
  246. 'group_restrictions' => '[]',
  247. ],
  248. ], $rows);
  249. $query2 = self::$realDatabase->getQueryBuilder();
  250. $query2->select('*')->from('calendar_resources_md');
  251. $rows2 = [];
  252. $stmt = $query2->execute();
  253. while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
  254. unset($row['id']);
  255. $rows2[] = $row;
  256. }
  257. $this->assertEquals([
  258. [
  259. 'resource_id' => $ids['backend2::res3'],
  260. 'key' => 'meta1',
  261. 'value' => 'value1',
  262. ],
  263. [
  264. 'resource_id' => $ids['backend2::res3'],
  265. 'key' => 'meta2',
  266. 'value' => 'value2',
  267. ],
  268. [
  269. 'resource_id' => $ids['backend2::res4'],
  270. 'key' => 'meta1',
  271. 'value' => 'value1',
  272. ],
  273. [
  274. 'resource_id' => $ids['backend2::res4'],
  275. 'key' => 'meta3',
  276. 'value' => 'value3-old',
  277. ],
  278. [
  279. 'resource_id' => $ids['backend3::res6'],
  280. 'key' => 'meta99',
  281. 'value' => 'value99-new',
  282. ],
  283. [
  284. 'resource_id' => $ids['backend3::res7'],
  285. 'key' => 'meta1',
  286. 'value' => 'value1',
  287. ],
  288. [
  289. 'resource_id' => $ids['backend3::res6'],
  290. 'key' => 'meta123',
  291. 'value' => 'meta456',
  292. ],
  293. [
  294. 'resource_id' => $ids['backend4::res8'],
  295. 'key' => 'meta2',
  296. 'value' => 'value2',
  297. ]
  298. ], $rows2);
  299. }
  300. protected function createTestResourcesInCache() {
  301. $query = self::$realDatabase->getQueryBuilder();
  302. $query->insert('calendar_resources')
  303. ->values([
  304. 'backend_id' => $query->createNamedParameter('backend1'),
  305. 'resource_id' => $query->createNamedParameter('res1'),
  306. 'email' => $query->createNamedParameter('res1@foo.bar'),
  307. 'displayname' => $query->createNamedParameter('Beamer1'),
  308. 'group_restrictions' => $query->createNamedParameter('[]'),
  309. ])
  310. ->execute();
  311. $query->insert('calendar_resources')
  312. ->values([
  313. 'backend_id' => $query->createNamedParameter('backend1'),
  314. 'resource_id' => $query->createNamedParameter('res2'),
  315. 'email' => $query->createNamedParameter('res2@foo.bar'),
  316. 'displayname' => $query->createNamedParameter('TV1'),
  317. 'group_restrictions' => $query->createNamedParameter('[]'),
  318. ])
  319. ->execute();
  320. $query->insert('calendar_resources')
  321. ->values([
  322. 'backend_id' => $query->createNamedParameter('backend2'),
  323. 'resource_id' => $query->createNamedParameter('res3'),
  324. 'email' => $query->createNamedParameter('res3@foo.bar'),
  325. 'displayname' => $query->createNamedParameter('Beamer2'),
  326. 'group_restrictions' => $query->createNamedParameter('[]'),
  327. ])
  328. ->execute();
  329. $id3 = $query->getLastInsertId();
  330. $query->insert('calendar_resources')
  331. ->values([
  332. 'backend_id' => $query->createNamedParameter('backend2'),
  333. 'resource_id' => $query->createNamedParameter('res4'),
  334. 'email' => $query->createNamedParameter('res4@foo.bar'),
  335. 'displayname' => $query->createNamedParameter('TV2'),
  336. 'group_restrictions' => $query->createNamedParameter('[]'),
  337. ])
  338. ->execute();
  339. $id4 = $query->getLastInsertId();
  340. $query->insert('calendar_resources')
  341. ->values([
  342. 'backend_id' => $query->createNamedParameter('backend3'),
  343. 'resource_id' => $query->createNamedParameter('res5'),
  344. 'email' => $query->createNamedParameter('res5@foo.bar'),
  345. 'displayname' => $query->createNamedParameter('Beamer3'),
  346. 'group_restrictions' => $query->createNamedParameter('[]'),
  347. ])
  348. ->execute();
  349. $query->insert('calendar_resources')
  350. ->values([
  351. 'backend_id' => $query->createNamedParameter('backend3'),
  352. 'resource_id' => $query->createNamedParameter('res6'),
  353. 'email' => $query->createNamedParameter('res6@foo.bar'),
  354. 'displayname' => $query->createNamedParameter('Pointer'),
  355. 'group_restrictions' => $query->createNamedParameter('["foo", "bar"]'),
  356. ])
  357. ->execute();
  358. $id6 = $query->getLastInsertId();
  359. $query->insert('calendar_resources_md')
  360. ->values([
  361. 'resource_id' => $query->createNamedParameter($id3),
  362. 'key' => $query->createNamedParameter('meta1'),
  363. 'value' => $query->createNamedParameter('value1')
  364. ])
  365. ->execute();
  366. $query->insert('calendar_resources_md')
  367. ->values([
  368. 'resource_id' => $query->createNamedParameter($id3),
  369. 'key' => $query->createNamedParameter('meta2'),
  370. 'value' => $query->createNamedParameter('value2')
  371. ])
  372. ->execute();
  373. $query->insert('calendar_resources_md')
  374. ->values([
  375. 'resource_id' => $query->createNamedParameter($id4),
  376. 'key' => $query->createNamedParameter('meta1'),
  377. 'value' => $query->createNamedParameter('value1')
  378. ])
  379. ->execute();
  380. $query->insert('calendar_resources_md')
  381. ->values([
  382. 'resource_id' => $query->createNamedParameter($id4),
  383. 'key' => $query->createNamedParameter('meta3'),
  384. 'value' => $query->createNamedParameter('value3-old')
  385. ])
  386. ->execute();
  387. $query->insert('calendar_resources_md')
  388. ->values([
  389. 'resource_id' => $query->createNamedParameter($id6),
  390. 'key' => $query->createNamedParameter('meta99'),
  391. 'value' => $query->createNamedParameter('value99')
  392. ])
  393. ->execute();
  394. }
  395. }