UpdateCalendarResourcesRoomsBackgroundJobTest.php 14 KB

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