UpdateCalendarResourcesRoomsBackgroundJobTest.php 14 KB

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