UpdateCalendarResourcesRoomsBackgroundJobTest.php 14 KB

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