BackendTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. $this->assertEquals($rows[0], [
  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. $this->assertEquals($rows[1], [
  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. }
  125. public function testGetAllScheduledRemindersForEvent(): void {
  126. $rows = $this->reminderBackend->getAllScheduledRemindersForEvent(1);
  127. $this->assertCount(2, $rows);
  128. unset($rows[0]['id']);
  129. unset($rows[1]['id']);
  130. $this->assertEquals($rows[0], [
  131. 'calendar_id' => 1,
  132. 'object_id' => 1,
  133. 'uid' => 'asd',
  134. 'is_recurring' => false,
  135. 'recurrence_id' => 123458,
  136. 'is_recurrence_exception' => false,
  137. 'event_hash' => 'asd123',
  138. 'alarm_hash' => 'asd567',
  139. 'type' => 'EMAIL',
  140. 'is_relative' => true,
  141. 'notification_date' => 123456,
  142. 'is_repeat_based' => false,
  143. ]);
  144. $this->assertEquals($rows[1], [
  145. 'calendar_id' => 1,
  146. 'object_id' => 1,
  147. 'uid' => 'asd',
  148. 'is_recurring' => false,
  149. 'recurrence_id' => 123458,
  150. 'is_recurrence_exception' => false,
  151. 'event_hash' => 'asd123',
  152. 'alarm_hash' => 'asd567',
  153. 'type' => 'AUDIO',
  154. 'is_relative' => true,
  155. 'notification_date' => 123456,
  156. 'is_repeat_based' => false,
  157. ]);
  158. }
  159. public function testInsertReminder(): void {
  160. $query = self::$realDatabase->getQueryBuilder();
  161. $rows = $query->select('*')
  162. ->from('calendar_reminders')
  163. ->execute()
  164. ->fetchAll();
  165. $this->assertCount(4, $rows);
  166. $this->reminderBackend->insertReminder(42, 1337, 'uid99', true, 12345678,
  167. true, 'hash99', 'hash42', 'AUDIO', false, 12345670, false);
  168. $query = self::$realDatabase->getQueryBuilder();
  169. $rows = $query->select('*')
  170. ->from('calendar_reminders')
  171. ->execute()
  172. ->fetchAll();
  173. $this->assertCount(5, $rows);
  174. unset($rows[4]['id']);
  175. $this->assertEquals($rows[4], [
  176. 'calendar_id' => '42',
  177. 'object_id' => '1337',
  178. 'is_recurring' => '1',
  179. 'uid' => 'uid99',
  180. 'recurrence_id' => '12345678',
  181. 'is_recurrence_exception' => '1',
  182. 'event_hash' => 'hash99',
  183. 'alarm_hash' => 'hash42',
  184. 'type' => 'AUDIO',
  185. 'is_relative' => '0',
  186. 'notification_date' => '12345670',
  187. 'is_repeat_based' => '0',
  188. ]);
  189. }
  190. public function testUpdateReminder(): void {
  191. $query = self::$realDatabase->getQueryBuilder();
  192. $rows = $query->select('*')
  193. ->from('calendar_reminders')
  194. ->execute()
  195. ->fetchAll();
  196. $this->assertCount(4, $rows);
  197. $this->assertEquals($rows[3]['notification_date'], 123600);
  198. $reminderId = (int) $rows[3]['id'];
  199. $newNotificationDate = 123700;
  200. $this->reminderBackend->updateReminder($reminderId, $newNotificationDate);
  201. $query = self::$realDatabase->getQueryBuilder();
  202. $row = $query->select('notification_date')
  203. ->from('calendar_reminders')
  204. ->where($query->expr()->eq('id', $query->createNamedParameter($reminderId)))
  205. ->execute()
  206. ->fetch();
  207. $this->assertEquals((int) $row['notification_date'], 123700);
  208. }
  209. private function createRemindersTestSet(): void {
  210. $query = self::$realDatabase->getQueryBuilder();
  211. $query->insert('calendars')
  212. ->values([
  213. 'id' => $query->createNamedParameter(1),
  214. 'principaluri' => $query->createNamedParameter('principals/users/user001'),
  215. 'displayname' => $query->createNamedParameter('Displayname 123'),
  216. ])
  217. ->execute();
  218. $query = self::$realDatabase->getQueryBuilder();
  219. $query->insert('calendars')
  220. ->values([
  221. 'id' => $query->createNamedParameter(99),
  222. 'principaluri' => $query->createNamedParameter('principals/users/user002'),
  223. 'displayname' => $query->createNamedParameter('Displayname 99'),
  224. ])
  225. ->execute();
  226. $query = self::$realDatabase->getQueryBuilder();
  227. $query->insert('calendarobjects')
  228. ->values([
  229. 'id' => $query->createNamedParameter(1),
  230. 'calendardata' => $query->createNamedParameter('Calendar data 123'),
  231. 'calendarid' => $query->createNamedParameter(1),
  232. 'size' => $query->createNamedParameter(42),
  233. ])
  234. ->execute();
  235. $query = self::$realDatabase->getQueryBuilder();
  236. $query->insert('calendarobjects')
  237. ->values([
  238. 'id' => $query->createNamedParameter(2),
  239. 'calendardata' => $query->createNamedParameter('Calendar data 456'),
  240. 'calendarid' => $query->createNamedParameter(1),
  241. 'size' => $query->createNamedParameter(42),
  242. ])
  243. ->execute();
  244. $query = self::$realDatabase->getQueryBuilder();
  245. $query->insert('calendarobjects')
  246. ->values([
  247. 'id' => $query->createNamedParameter(10),
  248. 'calendardata' => $query->createNamedParameter('Calendar data 789'),
  249. 'calendarid' => $query->createNamedParameter(99),
  250. 'size' => $query->createNamedParameter(42),
  251. ])
  252. ->execute();
  253. $query = self::$realDatabase->getQueryBuilder();
  254. $query->insert('calendar_reminders')
  255. ->values([
  256. 'calendar_id' => $query->createNamedParameter(1),
  257. 'object_id' => $query->createNamedParameter(1),
  258. 'uid' => $query->createNamedParameter('asd'),
  259. 'is_recurring' => $query->createNamedParameter(0),
  260. 'recurrence_id' => $query->createNamedParameter(123458),
  261. 'is_recurrence_exception' => $query->createNamedParameter(0),
  262. 'event_hash' => $query->createNamedParameter('asd123'),
  263. 'alarm_hash' => $query->createNamedParameter('asd567'),
  264. 'type' => $query->createNamedParameter('EMAIL'),
  265. 'is_relative' => $query->createNamedParameter(1),
  266. 'notification_date' => $query->createNamedParameter(123456),
  267. 'is_repeat_based' => $query->createNamedParameter(0),
  268. ])
  269. ->execute();
  270. $query = self::$realDatabase->getQueryBuilder();
  271. $query->insert('calendar_reminders')
  272. ->values([
  273. 'calendar_id' => $query->createNamedParameter(1),
  274. 'object_id' => $query->createNamedParameter(1),
  275. 'uid' => $query->createNamedParameter('asd'),
  276. 'is_recurring' => $query->createNamedParameter(0),
  277. 'recurrence_id' => $query->createNamedParameter(123458),
  278. 'is_recurrence_exception' => $query->createNamedParameter(0),
  279. 'event_hash' => $query->createNamedParameter('asd123'),
  280. 'alarm_hash' => $query->createNamedParameter('asd567'),
  281. 'type' => $query->createNamedParameter('AUDIO'),
  282. 'is_relative' => $query->createNamedParameter(1),
  283. 'notification_date' => $query->createNamedParameter(123456),
  284. 'is_repeat_based' => $query->createNamedParameter(0),
  285. ])
  286. ->execute();
  287. $query = self::$realDatabase->getQueryBuilder();
  288. $query->insert('calendar_reminders')
  289. ->values([
  290. 'calendar_id' => $query->createNamedParameter(1),
  291. 'object_id' => $query->createNamedParameter(2),
  292. 'uid' => $query->createNamedParameter('asd'),
  293. 'is_recurring' => $query->createNamedParameter(0),
  294. 'recurrence_id' => $query->createNamedParameter(123900),
  295. 'is_recurrence_exception' => $query->createNamedParameter(0),
  296. 'event_hash' => $query->createNamedParameter('asd123'),
  297. 'alarm_hash' => $query->createNamedParameter('asd567'),
  298. 'type' => $query->createNamedParameter('EMAIL'),
  299. 'is_relative' => $query->createNamedParameter(1),
  300. 'notification_date' => $query->createNamedParameter(123499),
  301. 'is_repeat_based' => $query->createNamedParameter(0),
  302. ])
  303. ->execute();
  304. $query = self::$realDatabase->getQueryBuilder();
  305. $query->insert('calendar_reminders')
  306. ->values([
  307. 'calendar_id' => $query->createNamedParameter(99),
  308. 'object_id' => $query->createNamedParameter(10),
  309. 'uid' => $query->createNamedParameter('asd'),
  310. 'is_recurring' => $query->createNamedParameter(0),
  311. 'recurrence_id' => $query->createNamedParameter(123900),
  312. 'is_recurrence_exception' => $query->createNamedParameter(0),
  313. 'event_hash' => $query->createNamedParameter('asd123'),
  314. 'alarm_hash' => $query->createNamedParameter('asd567'),
  315. 'type' => $query->createNamedParameter('DISPLAY'),
  316. 'is_relative' => $query->createNamedParameter(1),
  317. 'notification_date' => $query->createNamedParameter(123600),
  318. 'is_repeat_based' => $query->createNamedParameter(0),
  319. ])
  320. ->execute();
  321. }
  322. }