123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- <?php
- declare(strict_types=1);
- /**
- * @copyright Copyright (c) 2019, Thomas Citharel
- * @copyright Copyright (c) 2019, Georg Ehrke
- *
- * @author Georg Ehrke <oc.list@georgehrke.com>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Thomas Citharel <nextcloud@tcit.fr>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- namespace OCA\DAV\Tests\unit\CalDAV\Reminder;
- use OCA\DAV\CalDAV\Reminder\Backend as ReminderBackend;
- use OCP\AppFramework\Utility\ITimeFactory;
- use Test\TestCase;
- class BackendTest extends TestCase {
- /**
- * Reminder Backend
- *
- * @var ReminderBackend|\PHPUnit\Framework\MockObject\MockObject
- */
- private $reminderBackend;
- /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
- private $timeFactory;
- protected function setUp(): void {
- parent::setUp();
- $query = self::$realDatabase->getQueryBuilder();
- $query->delete('calendar_reminders')->execute();
- $query->delete('calendarobjects')->execute();
- $query->delete('calendars')->execute();
- $this->timeFactory = $this->createMock(ITimeFactory::class);
- $this->reminderBackend = new ReminderBackend(self::$realDatabase, $this->timeFactory);
- $this->createRemindersTestSet();
- }
- protected function tearDown(): void {
- $query = self::$realDatabase->getQueryBuilder();
- $query->delete('calendar_reminders')->execute();
- $query->delete('calendarobjects')->execute();
- $query->delete('calendars')->execute();
- }
- public function testCleanRemindersForEvent(): void {
- $query = self::$realDatabase->getQueryBuilder();
- $rows = $query->select('*')
- ->from('calendar_reminders')
- ->execute()
- ->fetchAll();
- $this->assertCount(4, $rows);
- $this->reminderBackend->cleanRemindersForEvent(1);
- $query = self::$realDatabase->getQueryBuilder();
- $rows = $query->select('*')
- ->from('calendar_reminders')
- ->execute()
- ->fetchAll();
- $this->assertCount(2, $rows);
- }
- public function testCleanRemindersForCalendar(): void {
- $query = self::$realDatabase->getQueryBuilder();
- $rows = $query->select('*')
- ->from('calendar_reminders')
- ->execute()
- ->fetchAll();
- $this->assertCount(4, $rows);
- $this->reminderBackend->cleanRemindersForCalendar(1);
- $query = self::$realDatabase->getQueryBuilder();
- $rows = $query->select('*')
- ->from('calendar_reminders')
- ->execute()
- ->fetchAll();
- $this->assertCount(1, $rows);
- }
- public function testRemoveReminder(): void {
- $query = self::$realDatabase->getQueryBuilder();
- $rows = $query->select('*')
- ->from('calendar_reminders')
- ->execute()
- ->fetchAll();
- $this->assertCount(4, $rows);
- $this->reminderBackend->removeReminder((int) $rows[3]['id']);
- $query = self::$realDatabase->getQueryBuilder();
- $rows = $query->select('*')
- ->from('calendar_reminders')
- ->execute()
- ->fetchAll();
- $this->assertCount(3, $rows);
- }
- public function testGetRemindersToProcess(): void {
- $this->timeFactory->expects($this->exactly(1))
- ->method('getTime')
- ->with()
- ->willReturn(123457);
- $rows = $this->reminderBackend->getRemindersToProcess();
- $this->assertCount(2, $rows);
- unset($rows[0]['id']);
- unset($rows[1]['id']);
- $this->assertEquals($rows[0], [
- 'calendar_id' => 1,
- 'object_id' => 1,
- 'uid' => 'asd',
- 'is_recurring' => false,
- 'recurrence_id' => 123458,
- 'is_recurrence_exception' => false,
- 'event_hash' => 'asd123',
- 'alarm_hash' => 'asd567',
- 'type' => 'EMAIL',
- 'is_relative' => true,
- 'notification_date' => 123456,
- 'is_repeat_based' => false,
- 'calendardata' => 'Calendar data 123',
- 'displayname' => 'Displayname 123',
- 'principaluri' => 'principals/users/user001',
- ]);
- $this->assertEquals($rows[1], [
- 'calendar_id' => 1,
- 'object_id' => 1,
- 'uid' => 'asd',
- 'is_recurring' => false,
- 'recurrence_id' => 123458,
- 'is_recurrence_exception' => false,
- 'event_hash' => 'asd123',
- 'alarm_hash' => 'asd567',
- 'type' => 'AUDIO',
- 'is_relative' => true,
- 'notification_date' => 123456,
- 'is_repeat_based' => false,
- 'calendardata' => 'Calendar data 123',
- 'displayname' => 'Displayname 123',
- 'principaluri' => 'principals/users/user001',
- ]);
- }
- public function testGetAllScheduledRemindersForEvent(): void {
- $rows = $this->reminderBackend->getAllScheduledRemindersForEvent(1);
- $this->assertCount(2, $rows);
- unset($rows[0]['id']);
- unset($rows[1]['id']);
- $this->assertEquals($rows[0], [
- 'calendar_id' => 1,
- 'object_id' => 1,
- 'uid' => 'asd',
- 'is_recurring' => false,
- 'recurrence_id' => 123458,
- 'is_recurrence_exception' => false,
- 'event_hash' => 'asd123',
- 'alarm_hash' => 'asd567',
- 'type' => 'EMAIL',
- 'is_relative' => true,
- 'notification_date' => 123456,
- 'is_repeat_based' => false,
- ]);
- $this->assertEquals($rows[1], [
- 'calendar_id' => 1,
- 'object_id' => 1,
- 'uid' => 'asd',
- 'is_recurring' => false,
- 'recurrence_id' => 123458,
- 'is_recurrence_exception' => false,
- 'event_hash' => 'asd123',
- 'alarm_hash' => 'asd567',
- 'type' => 'AUDIO',
- 'is_relative' => true,
- 'notification_date' => 123456,
- 'is_repeat_based' => false,
- ]);
- }
- public function testInsertReminder(): void {
- $query = self::$realDatabase->getQueryBuilder();
- $rows = $query->select('*')
- ->from('calendar_reminders')
- ->execute()
- ->fetchAll();
- $this->assertCount(4, $rows);
- $this->reminderBackend->insertReminder(42, 1337, 'uid99', true, 12345678,
- true, 'hash99', 'hash42', 'AUDIO', false, 12345670, false);
- $query = self::$realDatabase->getQueryBuilder();
- $rows = $query->select('*')
- ->from('calendar_reminders')
- ->execute()
- ->fetchAll();
- $this->assertCount(5, $rows);
- unset($rows[4]['id']);
- $this->assertEquals($rows[4], [
- 'calendar_id' => '42',
- 'object_id' => '1337',
- 'is_recurring' => '1',
- 'uid' => 'uid99',
- 'recurrence_id' => '12345678',
- 'is_recurrence_exception' => '1',
- 'event_hash' => 'hash99',
- 'alarm_hash' => 'hash42',
- 'type' => 'AUDIO',
- 'is_relative' => '0',
- 'notification_date' => '12345670',
- 'is_repeat_based' => '0',
- ]);
- }
- public function testUpdateReminder() {
- $query = self::$realDatabase->getQueryBuilder();
- $rows = $query->select('*')
- ->from('calendar_reminders')
- ->execute()
- ->fetchAll();
- $this->assertCount(4, $rows);
- $this->assertEquals($rows[3]['notification_date'], 123600);
- $reminderId = (int) $rows[3]['id'];
- $newNotificationDate = 123700;
- $this->reminderBackend->updateReminder($reminderId, $newNotificationDate);
- $query = self::$realDatabase->getQueryBuilder();
- $row = $query->select('notification_date')
- ->from('calendar_reminders')
- ->where($query->expr()->eq('id', $query->createNamedParameter($reminderId)))
- ->execute()
- ->fetch();
- $this->assertEquals((int) $row['notification_date'], 123700);
- }
- private function createRemindersTestSet(): void {
- $query = self::$realDatabase->getQueryBuilder();
- $query->insert('calendars')
- ->values([
- 'id' => $query->createNamedParameter(1),
- 'principaluri' => $query->createNamedParameter('principals/users/user001'),
- 'displayname' => $query->createNamedParameter('Displayname 123'),
- ])
- ->execute();
- $query = self::$realDatabase->getQueryBuilder();
- $query->insert('calendars')
- ->values([
- 'id' => $query->createNamedParameter(99),
- 'principaluri' => $query->createNamedParameter('principals/users/user002'),
- 'displayname' => $query->createNamedParameter('Displayname 99'),
- ])
- ->execute();
- $query = self::$realDatabase->getQueryBuilder();
- $query->insert('calendarobjects')
- ->values([
- 'id' => $query->createNamedParameter(1),
- 'calendardata' => $query->createNamedParameter('Calendar data 123'),
- 'calendarid' => $query->createNamedParameter(1),
- 'size' => $query->createNamedParameter(42),
- ])
- ->execute();
- $query = self::$realDatabase->getQueryBuilder();
- $query->insert('calendarobjects')
- ->values([
- 'id' => $query->createNamedParameter(2),
- 'calendardata' => $query->createNamedParameter('Calendar data 456'),
- 'calendarid' => $query->createNamedParameter(1),
- 'size' => $query->createNamedParameter(42),
- ])
- ->execute();
- $query = self::$realDatabase->getQueryBuilder();
- $query->insert('calendarobjects')
- ->values([
- 'id' => $query->createNamedParameter(10),
- 'calendardata' => $query->createNamedParameter('Calendar data 789'),
- 'calendarid' => $query->createNamedParameter(99),
- 'size' => $query->createNamedParameter(42),
- ])
- ->execute();
- $query = self::$realDatabase->getQueryBuilder();
- $query->insert('calendar_reminders')
- ->values([
- 'calendar_id' => $query->createNamedParameter(1),
- 'object_id' => $query->createNamedParameter(1),
- 'uid' => $query->createNamedParameter('asd'),
- 'is_recurring' => $query->createNamedParameter(0),
- 'recurrence_id' => $query->createNamedParameter(123458),
- 'is_recurrence_exception' => $query->createNamedParameter(0),
- 'event_hash' => $query->createNamedParameter('asd123'),
- 'alarm_hash' => $query->createNamedParameter('asd567'),
- 'type' => $query->createNamedParameter('EMAIL'),
- 'is_relative' => $query->createNamedParameter(1),
- 'notification_date' => $query->createNamedParameter(123456),
- 'is_repeat_based' => $query->createNamedParameter(0),
- ])
- ->execute();
- $query = self::$realDatabase->getQueryBuilder();
- $query->insert('calendar_reminders')
- ->values([
- 'calendar_id' => $query->createNamedParameter(1),
- 'object_id' => $query->createNamedParameter(1),
- 'uid' => $query->createNamedParameter('asd'),
- 'is_recurring' => $query->createNamedParameter(0),
- 'recurrence_id' => $query->createNamedParameter(123458),
- 'is_recurrence_exception' => $query->createNamedParameter(0),
- 'event_hash' => $query->createNamedParameter('asd123'),
- 'alarm_hash' => $query->createNamedParameter('asd567'),
- 'type' => $query->createNamedParameter('AUDIO'),
- 'is_relative' => $query->createNamedParameter(1),
- 'notification_date' => $query->createNamedParameter(123456),
- 'is_repeat_based' => $query->createNamedParameter(0),
- ])
- ->execute();
- $query = self::$realDatabase->getQueryBuilder();
- $query->insert('calendar_reminders')
- ->values([
- 'calendar_id' => $query->createNamedParameter(1),
- 'object_id' => $query->createNamedParameter(2),
- 'uid' => $query->createNamedParameter('asd'),
- 'is_recurring' => $query->createNamedParameter(0),
- 'recurrence_id' => $query->createNamedParameter(123900),
- 'is_recurrence_exception' => $query->createNamedParameter(0),
- 'event_hash' => $query->createNamedParameter('asd123'),
- 'alarm_hash' => $query->createNamedParameter('asd567'),
- 'type' => $query->createNamedParameter('EMAIL'),
- 'is_relative' => $query->createNamedParameter(1),
- 'notification_date' => $query->createNamedParameter(123499),
- 'is_repeat_based' => $query->createNamedParameter(0),
- ])
- ->execute();
- $query = self::$realDatabase->getQueryBuilder();
- $query->insert('calendar_reminders')
- ->values([
- 'calendar_id' => $query->createNamedParameter(99),
- 'object_id' => $query->createNamedParameter(10),
- 'uid' => $query->createNamedParameter('asd'),
- 'is_recurring' => $query->createNamedParameter(0),
- 'recurrence_id' => $query->createNamedParameter(123900),
- 'is_recurrence_exception' => $query->createNamedParameter(0),
- 'event_hash' => $query->createNamedParameter('asd123'),
- 'alarm_hash' => $query->createNamedParameter('asd567'),
- 'type' => $query->createNamedParameter('DISPLAY'),
- 'is_relative' => $query->createNamedParameter(1),
- 'notification_date' => $query->createNamedParameter(123600),
- 'is_repeat_based' => $query->createNamedParameter(0),
- ])
- ->execute();
- }
- }
|