BackendTest.php 12 KB

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