BackendTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Tests\unit\CalDAV\Reminder;
  8. use OCA\DAV\CalDAV\Reminder\Backend as ReminderBackend;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use Test\TestCase;
  11. class BackendTest extends TestCase {
  12. /**
  13. * Reminder Backend
  14. *
  15. * @var ReminderBackend|\PHPUnit\Framework\MockObject\MockObject
  16. */
  17. private $reminderBackend;
  18. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  19. private $timeFactory;
  20. protected function setUp(): void {
  21. parent::setUp();
  22. $query = self::$realDatabase->getQueryBuilder();
  23. $query->delete('calendar_reminders')->execute();
  24. $query->delete('calendarobjects')->execute();
  25. $query->delete('calendars')->execute();
  26. $this->timeFactory = $this->createMock(ITimeFactory::class);
  27. $this->reminderBackend = new ReminderBackend(self::$realDatabase, $this->timeFactory);
  28. $this->createRemindersTestSet();
  29. }
  30. protected function tearDown(): void {
  31. $query = self::$realDatabase->getQueryBuilder();
  32. $query->delete('calendar_reminders')->execute();
  33. $query->delete('calendarobjects')->execute();
  34. $query->delete('calendars')->execute();
  35. }
  36. public function testCleanRemindersForEvent(): void {
  37. $query = self::$realDatabase->getQueryBuilder();
  38. $rows = $query->select('*')
  39. ->from('calendar_reminders')
  40. ->execute()
  41. ->fetchAll();
  42. $this->assertCount(4, $rows);
  43. $this->reminderBackend->cleanRemindersForEvent(1);
  44. $query = self::$realDatabase->getQueryBuilder();
  45. $rows = $query->select('*')
  46. ->from('calendar_reminders')
  47. ->execute()
  48. ->fetchAll();
  49. $this->assertCount(2, $rows);
  50. }
  51. public function testCleanRemindersForCalendar(): void {
  52. $query = self::$realDatabase->getQueryBuilder();
  53. $rows = $query->select('*')
  54. ->from('calendar_reminders')
  55. ->execute()
  56. ->fetchAll();
  57. $this->assertCount(4, $rows);
  58. $this->reminderBackend->cleanRemindersForCalendar(1);
  59. $query = self::$realDatabase->getQueryBuilder();
  60. $rows = $query->select('*')
  61. ->from('calendar_reminders')
  62. ->execute()
  63. ->fetchAll();
  64. $this->assertCount(1, $rows);
  65. }
  66. public function testRemoveReminder(): void {
  67. $query = self::$realDatabase->getQueryBuilder();
  68. $rows = $query->select('*')
  69. ->from('calendar_reminders')
  70. ->execute()
  71. ->fetchAll();
  72. $this->assertCount(4, $rows);
  73. $this->reminderBackend->removeReminder((int)$rows[3]['id']);
  74. $query = self::$realDatabase->getQueryBuilder();
  75. $rows = $query->select('*')
  76. ->from('calendar_reminders')
  77. ->execute()
  78. ->fetchAll();
  79. $this->assertCount(3, $rows);
  80. }
  81. public function testGetRemindersToProcess(): void {
  82. $this->timeFactory->expects($this->exactly(1))
  83. ->method('getTime')
  84. ->with()
  85. ->willReturn(123457);
  86. $rows = $this->reminderBackend->getRemindersToProcess();
  87. $this->assertCount(2, $rows);
  88. unset($rows[0]['id']);
  89. unset($rows[1]['id']);
  90. $expected1 = [
  91. 'calendar_id' => 1,
  92. 'object_id' => 1,
  93. 'uid' => 'asd',
  94. 'is_recurring' => false,
  95. 'recurrence_id' => 123458,
  96. 'is_recurrence_exception' => false,
  97. 'event_hash' => 'asd123',
  98. 'alarm_hash' => 'asd567',
  99. 'type' => 'EMAIL',
  100. 'is_relative' => true,
  101. 'notification_date' => 123456,
  102. 'is_repeat_based' => false,
  103. 'calendardata' => 'Calendar data 123',
  104. 'displayname' => 'Displayname 123',
  105. 'principaluri' => 'principals/users/user001',
  106. ];
  107. $expected2 = [
  108. 'calendar_id' => 1,
  109. 'object_id' => 1,
  110. 'uid' => 'asd',
  111. 'is_recurring' => false,
  112. 'recurrence_id' => 123458,
  113. 'is_recurrence_exception' => false,
  114. 'event_hash' => 'asd123',
  115. 'alarm_hash' => 'asd567',
  116. 'type' => 'AUDIO',
  117. 'is_relative' => true,
  118. 'notification_date' => 123456,
  119. 'is_repeat_based' => false,
  120. 'calendardata' => 'Calendar data 123',
  121. 'displayname' => 'Displayname 123',
  122. 'principaluri' => 'principals/users/user001',
  123. ];
  124. $this->assertEqualsCanonicalizing([$rows[0],$rows[1]], [$expected1,$expected2]);
  125. }
  126. public function testGetAllScheduledRemindersForEvent(): void {
  127. $rows = $this->reminderBackend->getAllScheduledRemindersForEvent(1);
  128. $this->assertCount(2, $rows);
  129. unset($rows[0]['id']);
  130. unset($rows[1]['id']);
  131. $this->assertEquals($rows[0], [
  132. 'calendar_id' => 1,
  133. 'object_id' => 1,
  134. 'uid' => 'asd',
  135. 'is_recurring' => false,
  136. 'recurrence_id' => 123458,
  137. 'is_recurrence_exception' => false,
  138. 'event_hash' => 'asd123',
  139. 'alarm_hash' => 'asd567',
  140. 'type' => 'EMAIL',
  141. 'is_relative' => true,
  142. 'notification_date' => 123456,
  143. 'is_repeat_based' => false,
  144. ]);
  145. $this->assertEquals($rows[1], [
  146. 'calendar_id' => 1,
  147. 'object_id' => 1,
  148. 'uid' => 'asd',
  149. 'is_recurring' => false,
  150. 'recurrence_id' => 123458,
  151. 'is_recurrence_exception' => false,
  152. 'event_hash' => 'asd123',
  153. 'alarm_hash' => 'asd567',
  154. 'type' => 'AUDIO',
  155. 'is_relative' => true,
  156. 'notification_date' => 123456,
  157. 'is_repeat_based' => false,
  158. ]);
  159. }
  160. public function testInsertReminder(): void {
  161. $query = self::$realDatabase->getQueryBuilder();
  162. $rows = $query->select('*')
  163. ->from('calendar_reminders')
  164. ->execute()
  165. ->fetchAll();
  166. $this->assertCount(4, $rows);
  167. $this->reminderBackend->insertReminder(42, 1337, 'uid99', true, 12345678,
  168. true, 'hash99', 'hash42', 'AUDIO', false, 12345670, false);
  169. $query = self::$realDatabase->getQueryBuilder();
  170. $rows = $query->select('*')
  171. ->from('calendar_reminders')
  172. ->execute()
  173. ->fetchAll();
  174. $this->assertCount(5, $rows);
  175. unset($rows[4]['id']);
  176. $this->assertEquals($rows[4], [
  177. 'calendar_id' => '42',
  178. 'object_id' => '1337',
  179. 'is_recurring' => '1',
  180. 'uid' => 'uid99',
  181. 'recurrence_id' => '12345678',
  182. 'is_recurrence_exception' => '1',
  183. 'event_hash' => 'hash99',
  184. 'alarm_hash' => 'hash42',
  185. 'type' => 'AUDIO',
  186. 'is_relative' => '0',
  187. 'notification_date' => '12345670',
  188. 'is_repeat_based' => '0',
  189. ]);
  190. }
  191. public function testUpdateReminder(): void {
  192. $query = self::$realDatabase->getQueryBuilder();
  193. $rows = $query->select('*')
  194. ->from('calendar_reminders')
  195. ->execute()
  196. ->fetchAll();
  197. $this->assertCount(4, $rows);
  198. $this->assertEquals($rows[3]['notification_date'], 123600);
  199. $reminderId = (int)$rows[3]['id'];
  200. $newNotificationDate = 123700;
  201. $this->reminderBackend->updateReminder($reminderId, $newNotificationDate);
  202. $query = self::$realDatabase->getQueryBuilder();
  203. $row = $query->select('notification_date')
  204. ->from('calendar_reminders')
  205. ->where($query->expr()->eq('id', $query->createNamedParameter($reminderId)))
  206. ->execute()
  207. ->fetch();
  208. $this->assertEquals((int)$row['notification_date'], 123700);
  209. }
  210. private function createRemindersTestSet(): void {
  211. $query = self::$realDatabase->getQueryBuilder();
  212. $query->insert('calendars')
  213. ->values([
  214. 'id' => $query->createNamedParameter(1),
  215. 'principaluri' => $query->createNamedParameter('principals/users/user001'),
  216. 'displayname' => $query->createNamedParameter('Displayname 123'),
  217. ])
  218. ->execute();
  219. $query = self::$realDatabase->getQueryBuilder();
  220. $query->insert('calendars')
  221. ->values([
  222. 'id' => $query->createNamedParameter(99),
  223. 'principaluri' => $query->createNamedParameter('principals/users/user002'),
  224. 'displayname' => $query->createNamedParameter('Displayname 99'),
  225. ])
  226. ->execute();
  227. $query = self::$realDatabase->getQueryBuilder();
  228. $query->insert('calendarobjects')
  229. ->values([
  230. 'id' => $query->createNamedParameter(1),
  231. 'calendardata' => $query->createNamedParameter('Calendar data 123'),
  232. 'calendarid' => $query->createNamedParameter(1),
  233. 'size' => $query->createNamedParameter(42),
  234. ])
  235. ->execute();
  236. $query = self::$realDatabase->getQueryBuilder();
  237. $query->insert('calendarobjects')
  238. ->values([
  239. 'id' => $query->createNamedParameter(2),
  240. 'calendardata' => $query->createNamedParameter('Calendar data 456'),
  241. 'calendarid' => $query->createNamedParameter(1),
  242. 'size' => $query->createNamedParameter(42),
  243. ])
  244. ->execute();
  245. $query = self::$realDatabase->getQueryBuilder();
  246. $query->insert('calendarobjects')
  247. ->values([
  248. 'id' => $query->createNamedParameter(10),
  249. 'calendardata' => $query->createNamedParameter('Calendar data 789'),
  250. 'calendarid' => $query->createNamedParameter(99),
  251. 'size' => $query->createNamedParameter(42),
  252. ])
  253. ->execute();
  254. $query = self::$realDatabase->getQueryBuilder();
  255. $query->insert('calendar_reminders')
  256. ->values([
  257. 'calendar_id' => $query->createNamedParameter(1),
  258. 'object_id' => $query->createNamedParameter(1),
  259. 'uid' => $query->createNamedParameter('asd'),
  260. 'is_recurring' => $query->createNamedParameter(0),
  261. 'recurrence_id' => $query->createNamedParameter(123458),
  262. 'is_recurrence_exception' => $query->createNamedParameter(0),
  263. 'event_hash' => $query->createNamedParameter('asd123'),
  264. 'alarm_hash' => $query->createNamedParameter('asd567'),
  265. 'type' => $query->createNamedParameter('EMAIL'),
  266. 'is_relative' => $query->createNamedParameter(1),
  267. 'notification_date' => $query->createNamedParameter(123456),
  268. 'is_repeat_based' => $query->createNamedParameter(0),
  269. ])
  270. ->execute();
  271. $query = self::$realDatabase->getQueryBuilder();
  272. $query->insert('calendar_reminders')
  273. ->values([
  274. 'calendar_id' => $query->createNamedParameter(1),
  275. 'object_id' => $query->createNamedParameter(1),
  276. 'uid' => $query->createNamedParameter('asd'),
  277. 'is_recurring' => $query->createNamedParameter(0),
  278. 'recurrence_id' => $query->createNamedParameter(123458),
  279. 'is_recurrence_exception' => $query->createNamedParameter(0),
  280. 'event_hash' => $query->createNamedParameter('asd123'),
  281. 'alarm_hash' => $query->createNamedParameter('asd567'),
  282. 'type' => $query->createNamedParameter('AUDIO'),
  283. 'is_relative' => $query->createNamedParameter(1),
  284. 'notification_date' => $query->createNamedParameter(123456),
  285. 'is_repeat_based' => $query->createNamedParameter(0),
  286. ])
  287. ->execute();
  288. $query = self::$realDatabase->getQueryBuilder();
  289. $query->insert('calendar_reminders')
  290. ->values([
  291. 'calendar_id' => $query->createNamedParameter(1),
  292. 'object_id' => $query->createNamedParameter(2),
  293. 'uid' => $query->createNamedParameter('asd'),
  294. 'is_recurring' => $query->createNamedParameter(0),
  295. 'recurrence_id' => $query->createNamedParameter(123900),
  296. 'is_recurrence_exception' => $query->createNamedParameter(0),
  297. 'event_hash' => $query->createNamedParameter('asd123'),
  298. 'alarm_hash' => $query->createNamedParameter('asd567'),
  299. 'type' => $query->createNamedParameter('EMAIL'),
  300. 'is_relative' => $query->createNamedParameter(1),
  301. 'notification_date' => $query->createNamedParameter(123499),
  302. 'is_repeat_based' => $query->createNamedParameter(0),
  303. ])
  304. ->execute();
  305. $query = self::$realDatabase->getQueryBuilder();
  306. $query->insert('calendar_reminders')
  307. ->values([
  308. 'calendar_id' => $query->createNamedParameter(99),
  309. 'object_id' => $query->createNamedParameter(10),
  310. 'uid' => $query->createNamedParameter('asd'),
  311. 'is_recurring' => $query->createNamedParameter(0),
  312. 'recurrence_id' => $query->createNamedParameter(123900),
  313. 'is_recurrence_exception' => $query->createNamedParameter(0),
  314. 'event_hash' => $query->createNamedParameter('asd123'),
  315. 'alarm_hash' => $query->createNamedParameter('asd567'),
  316. 'type' => $query->createNamedParameter('DISPLAY'),
  317. 'is_relative' => $query->createNamedParameter(1),
  318. 'notification_date' => $query->createNamedParameter(123600),
  319. 'is_repeat_based' => $query->createNamedParameter(0),
  320. ])
  321. ->execute();
  322. }
  323. }