UpdateCalendarResourcesRoomsBackgroundJobTest.php 13 KB

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