diff options
Diffstat (limited to 'apps/dav/tests')
8 files changed, 1393 insertions, 0 deletions
diff --git a/apps/dav/tests/unit/BackgroundJob/EventReminderJobTest.php b/apps/dav/tests/unit/BackgroundJob/EventReminderJobTest.php new file mode 100644 index 00000000000..960dd481dd2 --- /dev/null +++ b/apps/dav/tests/unit/BackgroundJob/EventReminderJobTest.php @@ -0,0 +1,68 @@ +<?php +declare(strict_types=1); +/** + * @copyright 2018, Thomas Citharel <tcit@tcit.fr> + * + * @author Thomas Citharel <tcit@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\BackgroundJob; + +use OCA\DAV\BackgroundJob\EventReminderJob; +use OCA\DAV\CalDAV\Reminder\ReminderService; +use OCP\IConfig; +use Test\TestCase; + +class EventReminderJobTest extends TestCase { + + /** @var ReminderService|\PHPUnit\Framework\MockObject\MockObject */ + private $reminderService; + + /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ + private $config; + + /** @var EventReminderJob|\PHPUnit\Framework\MockObject\MockObject */ + private $backgroundJob; + + protected function setUp() { + parent::setUp(); + + $this->reminderService = $this->createMock(ReminderService::class); + $this->config = $this->createMock(IConfig::class); + + $this->backgroundJob = new EventReminderJob($this->reminderService, $this->config); + } + + public function data(): array + { + return [[true], [false]]; + } + + /** + * @dataProvider data + * @param bool $sendEventReminders + */ + public function testRun(bool $sendEventReminders): void + { + $this->config->expects($this->once())->method('getAppValue')->with('dav', 'sendEventReminders', 'yes')->willReturn($sendEventReminders ? 'yes' : 'no'); + $this->reminderService->expects($this->exactly($sendEventReminders ? 1 : 0))->method('processReminders'); + + $this->backgroundJob->run([]); + } +} diff --git a/apps/dav/tests/unit/CalDAV/Reminder/AbstractNotificationProviderTest.php b/apps/dav/tests/unit/CalDAV/Reminder/AbstractNotificationProviderTest.php new file mode 100644 index 00000000000..ba2e54af33c --- /dev/null +++ b/apps/dav/tests/unit/CalDAV/Reminder/AbstractNotificationProviderTest.php @@ -0,0 +1,87 @@ +<?php +/** + * @copyright Copyright (c) 2019, Thomas Citharel + * + * @author Thomas Citharel <tcit@tcit.fr> + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OCA\DAV\Tests\unit\CalDAV\Reminder; + +use OCA\DAV\CalDAV\Reminder\AbstractNotificationProvider; +use OCP\IConfig; +use OCP\IL10N; +use OCP\ILogger; +use OCP\IURLGenerator; +use OCP\L10N\IFactory as L10NFactory; +use OCP\IUser; +use Test\TestCase; +use Sabre\VObject\Component\VCalendar; + +abstract class AbstractNotificationProviderTest extends TestCase { + + /** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */ + protected $logger; + + /** @var L10NFactory|\PHPUnit\Framework\MockObject\MockObject */ + protected $l10nFactory; + + /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */ + protected $l10n; + + /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */ + protected $urlGenerator; + + /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ + protected $config; + + /** @var AbstractNotificationProvider|\PHPUnit\Framework\MockObject\MockObject */ + protected $provider; + + /** + * @var VCalendar + */ + protected $vcalendar; + + /** + * @var string + */ + protected $calendarDisplayName; + + /** + * @var IUser|\PHPUnit\Framework\MockObject\MockObject + */ + protected $user; + + public function setUp() { + parent::setUp(); + + $this->logger = $this->createMock(ILogger::class); + $this->l10nFactory = $this->createMock(L10NFactory::class); + $this->l10n = $this->createMock(IL10N::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->config = $this->createMock(IConfig::class); + + $this->vcalendar = new VCalendar(); + $this->vcalendar->add('VEVENT', [ + 'SUMMARY' => 'Fellowship meeting', + 'DTSTART' => new \DateTime('2017-01-01 00:00:00') // 1483228800 + ]); + $this->calendarDisplayName = 'Personal'; + + $this->user = $this->createMock(IUser::class); + } +} diff --git a/apps/dav/tests/unit/CalDAV/Reminder/BackendTest.php b/apps/dav/tests/unit/CalDAV/Reminder/BackendTest.php new file mode 100644 index 00000000000..1cd979dc5d9 --- /dev/null +++ b/apps/dav/tests/unit/CalDAV/Reminder/BackendTest.php @@ -0,0 +1,313 @@ +<?php +/** + * @copyright Copyright (c) 2018, Thomas Citharel + * + * @author Thomas Citharel <tcit@tcit.fr> + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OCA\DAV\Tests\unit\CalDAV\Reminder; + +use OCP\IDBConnection; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\AppFramework\Utility\ITimeFactory; +use OCA\DAV\CalDAV\Reminder\Backend as ReminderBackend; +use Test\TestCase; + +class BackendTest extends TestCase { + + /** + * Reminder Backend + * + * @var ReminderBackend|\PHPUnit\Framework\MockObject\MockObject + */ + private $reminderBackend; + + /** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */ + private $dbConnection; + + /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */ + private $timeFactory; + + public function setUp() { + parent::setUp(); + + $this->dbConnection = $this->createMock(IDBConnection::class); + $this->timeFactory = $this->createMock(ITimeFactory::class); + $this->reminderBackend = new ReminderBackend($this->dbConnection, $this->timeFactory); + } + + public function testCleanRemindersForEvent(): void + { + /** @var IQueryBuilder|\PHPUnit\Framework\MockObject\MockObject $queryBuilder */ + $queryBuilder = $this->createMock(IQueryBuilder::class); + $stmt = $this->createMock(\Doctrine\DBAL\Driver\Statement::class); + $expr = $this->createMock(\OCP\DB\QueryBuilder\IExpressionBuilder::class); + + $this->dbConnection->expects($this->once()) + ->method('getQueryBuilder') + ->with() + ->will($this->returnValue($queryBuilder)); + $queryBuilder->method('expr') + ->will($this->returnValue($expr)); + + $expr->method('eq') + ->will($this->returnValueMap([ + ['calendarid', 'createNamedParameter-1', null, 'WHERE_CLAUSE_1'], + ['objecturi', 'createNamedParameter-2', null, 'WHERE_CLAUSE_2'], + ])); + $queryBuilder->method('createNamedParameter') + ->will($this->returnValueMap([ + [1, \PDO::PARAM_STR, null, 'createNamedParameter-1'], + ['object.ics', \PDO::PARAM_STR, null, 'createNamedParameter-2'], + ])); + + $queryBuilder->expects($this->at(0)) + ->method('delete') + ->with('calendar_reminders') + ->willReturn($queryBuilder); + $queryBuilder->expects($this->at(3)) + ->method('where') + ->with('WHERE_CLAUSE_1') + ->will($this->returnValue($queryBuilder)); + $queryBuilder->expects($this->at(6)) + ->method('andWhere') + ->with('WHERE_CLAUSE_2') + ->will($this->returnValue($queryBuilder)); + $queryBuilder->expects($this->at(7)) + ->method('execute') + ->with() + ->willReturn($stmt); + + $this->reminderBackend->cleanRemindersForEvent(1, 'object.ics'); + } + + public function testCleanRemindersForCalendar(): void + { + /** @var IQueryBuilder|\PHPUnit\Framework\MockObject\MockObject $queryBuilder */ + $queryBuilder = $this->createMock(IQueryBuilder::class); + $stmt = $this->createMock(\Doctrine\DBAL\Driver\Statement::class); + $expr = $this->createMock(\OCP\DB\QueryBuilder\IExpressionBuilder::class); + + $this->dbConnection->expects($this->once()) + ->method('getQueryBuilder') + ->with() + ->will($this->returnValue($queryBuilder)); + $queryBuilder->method('expr') + ->will($this->returnValue($expr)); + + $expr->method('eq') + ->will($this->returnValueMap([ + ['calendarid', 'createNamedParameter-1', null, 'WHERE_CLAUSE_1'], + ])); + $queryBuilder->method('createNamedParameter') + ->will($this->returnValueMap([ + [1337, \PDO::PARAM_STR, null, 'createNamedParameter-1'], + ])); + + $queryBuilder->expects($this->at(0)) + ->method('delete') + ->with('calendar_reminders') + ->willReturn($queryBuilder); + $queryBuilder->expects($this->at(3)) + ->method('where') + ->with('WHERE_CLAUSE_1') + ->will($this->returnValue($queryBuilder)); + $queryBuilder->expects($this->at(4)) + ->method('execute') + ->with() + ->willReturn($stmt); + + $this->reminderBackend->cleanRemindersForCalendar(1337); + } + + public function testRemoveReminder(): void + { + /** @var IQueryBuilder|\PHPUnit\Framework\MockObject\MockObject $queryBuilder */ + $queryBuilder = $this->createMock(IQueryBuilder::class); + $stmt = $this->createMock(\Doctrine\DBAL\Driver\Statement::class); + $expr = $this->createMock(\OCP\DB\QueryBuilder\IExpressionBuilder::class); + + $this->dbConnection->expects($this->once()) + ->method('getQueryBuilder') + ->with() + ->will($this->returnValue($queryBuilder)); + $queryBuilder->method('expr') + ->will($this->returnValue($expr)); + + $expr->method('eq') + ->will($this->returnValueMap([ + ['id', 'createNamedParameter-1', null, 'WHERE_CLAUSE_1'], + ])); + $queryBuilder->method('createNamedParameter') + ->will($this->returnValueMap([ + [16, \PDO::PARAM_STR, null, 'createNamedParameter-1'], + ])); + + $queryBuilder->expects($this->at(0)) + ->method('delete') + ->with('calendar_reminders') + ->willReturn($queryBuilder); + $queryBuilder->expects($this->at(3)) + ->method('where') + ->with('WHERE_CLAUSE_1') + ->will($this->returnValue($queryBuilder)); + $queryBuilder->expects($this->at(4)) + ->method('execute') + ->with() + ->willReturn($stmt); + + $this->reminderBackend->removeReminder(16); + } + + public function testGetRemindersToProcess(): void + { + $dbData = [[ + 'cr.id' => 30, + 'cr.calendarid' => 3, + 'cr.objecturi' => 'object.ics', + 'cr.type' => 'EMAIL', + 'cr.notificationdate' => 1337, + 'cr.uid' => 'user1', + 'co.calendardata' => 'BEGIN:VCALENDAR', + 'c.displayname' => 'My Calendar' + ]]; + + $this->timeFactory->expects($this->exactly(2)) + ->method('getTime') + ->with() + ->willReturn(1337); + + /** @var IQueryBuilder|\PHPUnit\Framework\MockObject\MockObject $queryBuilder */ + $queryBuilder = $this->createMock(IQueryBuilder::class); + $stmt = $this->createMock(\Doctrine\DBAL\Driver\Statement::class); + $expr = $this->createMock(\OCP\DB\QueryBuilder\IExpressionBuilder::class); + + $this->dbConnection->expects($this->once()) + ->method('getQueryBuilder') + ->with() + ->willReturn($queryBuilder); + $queryBuilder->method('expr') + ->willReturn($expr); + + $expr->method('eq') + ->willReturnMap([ + ['cr.calendarid', 'c.id', null, 'EQ_CLAUSE_1'], + ['co.uri', 'cr.objecturi', null, 'EQ_CLAUSE_2'], + ]); + $expr->method('andX') + ->willReturnMap([ + ['EQ_CLAUSE_1', 'EQ_CLAUSE_2', 'ANDX_CLAUSE'], + ]); + + $expr->method('lte') + ->with('cr.notificationdate', 'createNamedParameter-1', null) + ->willReturn('LTE_CLAUSE_1'); + + $expr->method('gte') + ->with('cr.eventstartdate', 'createNamedParameter-1', null) + ->willReturn('GTE_CLAUSE_2'); + + $queryBuilder->method('createNamedParameter') + ->willReturnMap([ + [1337, \PDO::PARAM_STR, null, 'createNamedParameter-1'], + ]); + + $queryBuilder->expects($this->at(0)) + ->method('select') + ->with(['cr.id', 'cr.calendarid', 'cr.objecturi', 'cr.type', 'cr.notificationdate', 'cr.uid', 'co.calendardata', 'c.displayname']) + ->willReturn($queryBuilder); + $queryBuilder->expects($this->at(1)) + ->method('from') + ->with('calendar_reminders', 'cr') + ->willReturn($queryBuilder); + $queryBuilder->expects($this->at(4)) + ->method('where') + ->with('LTE_CLAUSE_1') + ->willReturn($queryBuilder); + $queryBuilder->expects($this->at(7)) + ->method('andWhere') + ->with('GTE_CLAUSE_2') + ->willReturn($queryBuilder); + $queryBuilder->expects($this->at(9)) + ->method('leftJoin') + ->with('cr', 'calendars', 'c', 'EQ_CLAUSE_1') + ->willReturn($queryBuilder); + $queryBuilder->expects($this->at(13)) + ->method('leftJoin') + ->with('cr', 'calendarobjects', 'co', 'ANDX_CLAUSE') + ->willReturn($queryBuilder); + $queryBuilder->expects($this->at(14)) + ->method('execute') + ->with() + ->willReturn($stmt); + + $stmt->expects($this->once()) + ->method('fetchAll') + ->with() + ->willReturn($dbData); + + $actual = $this->reminderBackend->getRemindersToProcess(); + $this->assertEquals($dbData, $actual); + } + + public function testInsertReminder(): void + { + /** @var IQueryBuilder|\PHPUnit\Framework\MockObject\MockObject $queryBuilder */ + $queryBuilder = $this->createMock(IQueryBuilder::class); + $stmt = $this->createMock(\Doctrine\DBAL\Driver\Statement::class); + $expr = $this->createMock(\OCP\DB\QueryBuilder\IExpressionBuilder::class); + + $this->dbConnection->expects($this->once()) + ->method('getQueryBuilder') + ->with() + ->will($this->returnValue($queryBuilder)); + $queryBuilder->method('expr') + ->will($this->returnValue($expr)); + + $queryBuilder->method('createNamedParameter') + ->will($this->returnValueMap([ + ['user1', \PDO::PARAM_STR, null, 'createNamedParameter-1'], + ['1', \PDO::PARAM_STR, null, 'createNamedParameter-2'], + ['object.ics', \PDO::PARAM_STR, null, 'createNamedParameter-3'], + ['EMAIL', \PDO::PARAM_STR, null, 'createNamedParameter-4'], + [1227, \PDO::PARAM_STR, null, 'createNamedParameter-5'], + [1337, \PDO::PARAM_STR, null, 'createNamedParameter-6'], + ])); + + $queryBuilder->expects($this->at(0)) + ->method('insert') + ->with('calendar_reminders') + ->willReturn($queryBuilder); + $queryBuilder->expects($this->at(7)) + ->method('values') + ->with([ + 'uid' => 'createNamedParameter-1', + 'calendarid' => 'createNamedParameter-2', + 'objecturi' => 'createNamedParameter-3', + 'type' => 'createNamedParameter-4', + 'notificationdate' => 'createNamedParameter-5', + 'eventstartdate' => 'createNamedParameter-6', + ]) + ->willReturn($queryBuilder); + $queryBuilder->expects($this->at(8)) + ->method('execute') + ->with() + ->willReturn($stmt); + + $actual = $this->reminderBackend->insertReminder('user1', '1', 'object.ics', 'EMAIL', 1227, 1337); + } +} diff --git a/apps/dav/tests/unit/CalDAV/Reminder/NotificationProvider/EmailProviderTest.php b/apps/dav/tests/unit/CalDAV/Reminder/NotificationProvider/EmailProviderTest.php new file mode 100644 index 00000000000..34a61b34fc5 --- /dev/null +++ b/apps/dav/tests/unit/CalDAV/Reminder/NotificationProvider/EmailProviderTest.php @@ -0,0 +1,226 @@ +<?php +/** + * @copyright Copyright (c) 2019, Thomas Citharel + * + * @author Thomas Citharel <tcit@tcit.fr> + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OCA\DAV\Tests\unit\CalDAV\Reminder\NotificationProvider; + +use OCA\DAV\CalDAV\Reminder\NotificationProvider\EmailProvider; +use OCA\DAV\CalDAV\Reminder\AbstractNotificationProvider; +use OCP\IConfig; +use OCP\IL10N; +use OCP\ILogger; +use OCP\IURLGenerator; +use OCP\L10N\IFactory as L10NFactory; +use OCP\IUser; +use OCP\Mail\IEMailTemplate; +use OCP\Mail\IMailer; +use OCP\Mail\IAttachment; +use OCP\Mail\IMessage; +use Test\TestCase; +use OCA\DAV\Tests\unit\CalDAV\Reminder\AbstractNotificationProviderTest; + +class EmailProviderTest extends AbstractNotificationProviderTest { + + const USER_EMAIL = 'frodo@hobb.it'; + + /** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */ + protected $logger; + + /** @var L10NFactory|\PHPUnit\Framework\MockObject\MockObject */ + protected $l10nFactory; + + /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */ + protected $l10n; + + /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */ + protected $urlGenerator; + + /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ + protected $config; + + /** @var IMailer|\PHPUnit\Framework\MockObject\MockObject */ + private $mailer; + + public function setUp() { + parent::setUp(); + + $this->mailer = $this->createMock(IMailer::class); + + $this->provider = new EmailProvider( + $this->config, + $this->mailer, + $this->logger, + $this->l10nFactory, + $this->urlGenerator + ); + } + + public function testSendWithNoUserEmail(): void + { + $this->user->expects($this->once()) + ->method('getEMailAddress') + ->with() + ->willReturn(null); + + $this->mailer + ->expects($this->never()) + ->method('send'); + + $this->provider->send($this->vcalendar, $this->calendarDisplayName, $this->user); + } + + public function testSendWithFailedRecipients(): void + { + $this->user->expects($this->exactly(2)) + ->method('getEMailAddress') + ->with() + ->willReturn(self::USER_EMAIL); + + $this->mailer + ->expects($this->once()) + ->method('send') + ->willReturn([self::USER_EMAIL]) + ; + + $this->logger + ->expects($this->once()) + ->method('error'); + + $l10n = $this->createMock(IL10N::class); + $this->l10nFactory + ->method('get') + ->willReturn($l10n); + + $this->provider->send($this->vcalendar, $this->calendarDisplayName, $this->user); + } + + public function testSendWithMailerFailure(): void + { + $this->user->expects($this->exactly(2)) + ->method('getEMailAddress') + ->with() + ->willReturn(self::USER_EMAIL); + + $ex = new \Exception(); + + $this->mailer + ->expects($this->once()) + ->method('send') + ->will($this->throwException($ex)) + ; + + $this->logger + ->expects($this->once()) + ->method('logException') + ->with($ex, ['app' => 'dav']); + + $l10n = $this->createMock(IL10N::class); + $this->l10nFactory + ->method('get') + ->willReturn($l10n); + + $this->provider->send($this->vcalendar, $this->calendarDisplayName, $this->user); + } + + public function testSend(): void + { + $this->user->expects($this->exactly(2)) + ->method('getEMailAddress') + ->with() + ->willReturn(self::USER_EMAIL); + + $this->user->expects($this->once()) + ->method('getDisplayName') + ->with() + ->willReturn('Frodo'); + + $this->urlGenerator + ->expects($this->exactly(2)) + ->method('getAbsoluteURL'); + + $this->urlGenerator + ->expects($this->exactly(2)) + ->method('imagePath'); + + $mailMessage = $this->createMock(IMessage::class); + $mailMessage->expects($this->once()) + ->method('setFrom') + ->with([\OCP\Util::getDefaultEmailAddress('invitations-noreply') => 'Nextcloud']) + ->willReturn($mailMessage); + + $mailMessage->expects($this->once()) + ->method('setTo') + ->with([self::USER_EMAIL => 'Frodo']) + ->willReturn($mailMessage); + + $mailMessage + ->expects($this->never()) + ->method('setReplyTo') + ->willReturn($mailMessage); + + $emailTemplate = $this->createMock(IEMailTemplate::class); + $this->mailer + ->expects($this->once()) + ->method('createEMailTemplate') + ->willReturn($emailTemplate); + + $emailTemplate->expects($this->once()) + ->method('setSubject') + ->with('Notification: Fellowship meeting'); + + $emailTemplate->expects($this->once()) + ->method('addHeader'); + + $emailTemplate->expects($this->once()) + ->method('addHeading'); + + $emailTemplate->expects($this->exactly(2)) + ->method('addBodyListItem'); + + $emailTemplate->expects($this->once()) + ->method('addFooter'); + + $mailMessage->expects($this->once()) + ->method('useTemplate') + ->with($emailTemplate); + + $this->mailer + ->expects($this->once()) + ->method('createMessage') + ->willReturn($mailMessage); + + $emailAttachment = $this->createMock(IAttachment::class); + $this->mailer + ->expects($this->once()) + ->method('createAttachment') + ->willReturn($emailAttachment); + + $this->mailer + ->expects($this->once()) + ->method('send'); + + $l10n = $this->createMock(IL10N::class); + $this->l10nFactory + ->method('get') + ->willReturn($l10n); + + $this->provider->send($this->vcalendar, $this->calendarDisplayName, $this->user); + } +} diff --git a/apps/dav/tests/unit/CalDAV/Reminder/NotificationProvider/PushProviderTest.php b/apps/dav/tests/unit/CalDAV/Reminder/NotificationProvider/PushProviderTest.php new file mode 100644 index 00000000000..e10afb44d27 --- /dev/null +++ b/apps/dav/tests/unit/CalDAV/Reminder/NotificationProvider/PushProviderTest.php @@ -0,0 +1,139 @@ +<?php +/** + * @copyright Copyright (c) 2019, Thomas Citharel + * + * @author Thomas Citharel <tcit@tcit.fr> + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OCA\DAV\Tests\unit\CalDAV\Reminder\NotificationProvider; + +use OCA\DAV\AppInfo\Application; +use OCA\DAV\CalDAV\Reminder\NotificationProvider\PushProvider; +use OCA\DAV\CalDAV\Reminder\AbstractNotificationProvider; +use OCP\IConfig; +use OCP\IL10N; +use OCP\ILogger; +use OCP\IURLGenerator; +use OCP\L10N\IFactory as L10NFactory; +use OCP\IUser; +use OCP\Notification\IManager; +use OCP\Notification\INotification; +use OCP\AppFramework\Utility\ITimeFactory; +use Test\TestCase; +use OCA\DAV\Tests\unit\CalDAV\Reminder\AbstractNotificationProviderTest; + +class PushProviderTest extends AbstractNotificationProviderTest { + + /** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */ + protected $logger; + + /** @var L10NFactory|\PHPUnit\Framework\MockObject\MockObject */ + protected $l10nFactory; + + /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */ + protected $l10n; + + /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */ + protected $urlGenerator; + + /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ + protected $config; + + /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ + private $manager; + + /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */ + private $timeFactory; + + public function setUp() { + parent::setUp(); + + $this->manager = $this->createMock(IManager::class); + $this->timeFactory = $this->createMock(ITimeFactory::class); + + $this->provider = new PushProvider( + $this->config, + $this->manager, + $this->logger, + $this->l10nFactory, + $this->urlGenerator, + $this->timeFactory + ); + } + + public function testSend(): void + { + $notification = $this->createMock(INotification::class); + $notification + ->expects($this->once()) + ->method('setApp') + ->with(Application::APP_ID) + ->willReturn($notification); + + $notification + ->expects($this->once()) + ->method('setUser') + ->willReturn($notification) + ; + + $notification + ->expects($this->once()) + ->method('setDateTime') + ->willReturn($notification) + ; + + $notification + ->expects($this->once()) + ->method('setObject') + ->willReturn($notification) + ; + + $notification + ->expects($this->once()) + ->method('setSubject') + ->willReturn($notification) + ; + + $notification + ->expects($this->once()) + ->method('setMessage') + ->willReturn($notification) + ; + + $this->manager + ->expects($this->once()) + ->method('createNotification') + ->willReturn($notification); + + $this->manager + ->expects($this->once()) + ->method('notify') + ->with($notification); + + $l10n = $this->createMock(IL10N::class); + $this->l10nFactory + ->method('get') + ->willReturn($l10n); + + $this->timeFactory->expects($this->once()) + ->method('getDateTime') + ->with() + ->willReturn(new \DateTime()); + + $this->provider->send($this->vcalendar, $this->calendarDisplayName, $this->user); + } +} diff --git a/apps/dav/tests/unit/CalDAV/Reminder/NotificationProviderManagerTest.php b/apps/dav/tests/unit/CalDAV/Reminder/NotificationProviderManagerTest.php new file mode 100644 index 00000000000..d962b631c10 --- /dev/null +++ b/apps/dav/tests/unit/CalDAV/Reminder/NotificationProviderManagerTest.php @@ -0,0 +1,100 @@ +<?php +/** + * @copyright Copyright (c) 2019, Thomas Citharel + * + * @author Thomas Citharel <tcit@tcit.fr> + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OCA\DAV\Tests\unit\CalDAV\Reminder; + +use OCA\DAV\CalDAV\Reminder\NotificationProvider\EmailProvider; +use OCA\DAV\CalDAV\Reminder\NotificationProvider\ProviderNotAvailableException; +use OCA\DAV\CalDAV\Reminder\NotificationProvider\PushProvider; +use OCA\DAV\CalDAV\Reminder\NotificationProviderManager; +use OCA\DAV\CalDAV\Reminder\NotificationTypeDoesNotExistException; +use OCA\DAV\Capabilities; +use Test\TestCase; + +class NotificationProviderManagerTest extends TestCase { + + /** @var NotificationProviderManager|\PHPUnit\Framework\MockObject\MockObject */ + private $providerManager; + + /** + * @throws \OCP\AppFramework\QueryException + */ + public function setUp() { + parent::setUp(); + + $this->providerManager = new NotificationProviderManager(); + $this->providerManager->registerProvider(EmailProvider::class); + } + + /** + * @expectedException OCA\DAV\CalDAV\Reminder\NotificationTypeDoesNotExistException + * @expectedExceptionMessage Type NOT EXISTENT is not an accepted type of notification + * @throws ProviderNotAvailableException + * @throws NotificationTypeDoesNotExistException + */ + public function testGetProviderForUnknownType(): void + { + $this->providerManager->getProvider('NOT EXISTENT'); + } + + /** + * @expectedException OCA\DAV\CalDAV\Reminder\NotificationProvider\ProviderNotAvailableException + * @expectedExceptionMessage No notification provider for type AUDIO available + * @throws NotificationTypeDoesNotExistException + * @throws ProviderNotAvailableException + */ + public function testGetProviderForUnRegisteredType(): void + { + $this->providerManager->getProvider('AUDIO'); + } + + /** + * @throws NotificationTypeDoesNotExistException + * @throws ProviderNotAvailableException + */ + public function testGetProvider(): void + { + $provider = $this->providerManager->getProvider('EMAIL'); + $this->assertInstanceOf(EmailProvider::class, $provider); + } + + /** + * @throws NotificationTypeDoesNotExistException + * @throws ProviderNotAvailableException + * @throws \OCP\AppFramework\QueryException + */ + public function testRegisterProvider(): void + { + $this->providerManager->registerProvider(PushProvider::class); + $provider = $this->providerManager->getProvider('DISPLAY'); + $this->assertInstanceOf(PushProvider::class, $provider); + } + + /** + * @expectedExceptionMessage Invalid notification provider registered + * @expectedException \InvalidArgumentException + * @throws \OCP\AppFramework\QueryException + */ + public function testRegisterBadProvider(): void + { + $this->providerManager->registerProvider(Capabilities::class); + } +} diff --git a/apps/dav/tests/unit/CalDAV/Reminder/NotifierTest.php b/apps/dav/tests/unit/CalDAV/Reminder/NotifierTest.php new file mode 100644 index 00000000000..b9695b33fdc --- /dev/null +++ b/apps/dav/tests/unit/CalDAV/Reminder/NotifierTest.php @@ -0,0 +1,184 @@ +<?php +/** + * @copyright Copyright (c) 2019 Thomas Citharel <tcit@tcit.fr> + * + * @author Thomas Citharel <tcit@tcit.fr> + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\DAV\Tests\unit\CalDAV\Reminder; + +use OCA\DAV\AppInfo\Application; +use OCA\DAV\CalDAV\Reminder\Notifier; +use OCP\IL10N; +use OCP\IURLGenerator; +use OCP\L10N\IFactory; +use OCP\Notification\INotification; +use Test\TestCase; + +class NotifierTest extends TestCase { + /** @var Notifier */ + protected $notifier; + + /** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */ + protected $factory; + /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */ + protected $urlGenerator; + /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */ + protected $l; + + protected function setUp() { + parent::setUp(); + + $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->l = $this->createMock(IL10N::class); + $this->l->expects($this->any()) + ->method('t') + ->willReturnCallback(function($string, $args) { + return vsprintf($string, $args); + }); + $this->l->expects($this->any()) + ->method('n') + ->willReturnCallback(function($textSingular, $textPlural, $count, $args) { + $text = $count === 1 ? $textSingular : $textPlural; + $text = str_replace('%n', (string)$count, $text); + return vsprintf($text, $args); + }); + $this->factory = $this->createMock(IFactory::class); + $this->factory->expects($this->any()) + ->method('get') + ->willReturn($this->l); + + $this->notifier = new Notifier( + $this->factory, + $this->urlGenerator + ); + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Notification not from this app + */ + public function testPrepareWrongApp(): void + { + /** @var INotification|\PHPUnit\Framework\MockObject\MockObject $notification */ + $notification = $this->createMock(INotification::class); + + $notification->expects($this->once()) + ->method('getApp') + ->willReturn('notifications'); + $notification->expects($this->never()) + ->method('getSubject'); + + $this->notifier->prepare($notification, 'en'); + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Unknown subject + */ + public function testPrepareWrongSubject() { + /** @var INotification|\PHPUnit\Framework\MockObject\MockObject $notification */ + $notification = $this->createMock(INotification::class); + + $notification->expects($this->once()) + ->method('getApp') + ->willReturn(Application::APP_ID); + $notification->expects($this->once()) + ->method('getSubject') + ->willReturn('wrong subject'); + + $this->notifier->prepare($notification, 'en'); + } + + public function dataPrepare(): array + { + return [ + [ + 'calendar_reminder', + [ + 'title' => 'foo', + 'start' => time() - 60 * 60 * 24 + ], + 'foo (one day ago)', + [ + 'when' => 'foo', + 'description' => 'bar', + 'location' => 'NC Headquarters', + 'calendar' => 'Personal' + ], + 'Calendar: Personal<br>Date: foo<br>Description: bar<br>Where: NC Headquarters' + ], + ]; + } + + /** + * @dataProvider dataPrepare + * + * @param string $subjectType + * @param array $subjectParams + * @param string $subject + * @param array $messageParams + * @param string $message + * @throws \Exception + */ + public function testPrepare(string $subjectType, array $subjectParams, string $subject, array $messageParams, string $message): void + { + /** @var INotification|\PHPUnit\Framework\MockObject\MockObject $notification */ + $notification = $this->createMock(INotification::class); + + $notification->expects($this->once()) + ->method('getApp') + ->willReturn(Application::APP_ID); + $notification->expects($this->once()) + ->method('getSubject') + ->willReturn($subjectType); + $notification->expects($this->once()) + ->method('getSubjectParameters') + ->willReturn($subjectParams); + $notification->expects($this->once()) + ->method('getMessageParameters') + ->willReturn($messageParams); + + $notification->expects($this->once()) + ->method('setParsedSubject') + ->with($subject) + ->willReturnSelf(); + + $notification->expects($this->once()) + ->method('setParsedMessage') + ->with($message) + ->willReturnSelf(); + + $this->urlGenerator->expects($this->once()) + ->method('imagePath') + ->with('core', 'places/calendar.svg') + ->willReturn('icon-url'); + $this->urlGenerator->expects($this->once()) + ->method('getAbsoluteURL') + ->with('icon-url') + ->willReturn('absolute-icon-url'); + $notification->expects($this->once()) + ->method('setIcon') + ->with('absolute-icon-url') + ->willReturnSelf(); + + $return = $this->notifier->prepare($notification, 'en'); + + $this->assertEquals($notification, $return); + } +} diff --git a/apps/dav/tests/unit/CalDAV/Reminder/ReminderServiceTest.php b/apps/dav/tests/unit/CalDAV/Reminder/ReminderServiceTest.php new file mode 100644 index 00000000000..061c3f16e38 --- /dev/null +++ b/apps/dav/tests/unit/CalDAV/Reminder/ReminderServiceTest.php @@ -0,0 +1,276 @@ +<?php +/** + * @copyright Copyright (c) 2019, Thomas Citharel + * + * @author Thomas Citharel <tcit@tcit.fr> + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OCA\DAV\Tests\unit\CalDAV\Reminder; + +use OCA\DAV\CalDAV\Reminder\AbstractNotificationProvider; +use OCA\DAV\CalDAV\Reminder\Backend; +use OCA\DAV\CalDAV\Reminder\NotificationProviderManager; +use OCA\DAV\CalDAV\Reminder\NotificationProvider\EmailProvider; +use OCA\DAV\CalDAV\Reminder\NotificationProvider\PushProvider; +use OCA\DAV\CalDAV\Reminder\ReminderService; +use OCP\IGroup; +use OCP\IGroupManager; +use OCP\IUser; +use OCP\IUserManager; +use OCP\IUserSession; +use Test\TestCase; + +class ReminderServiceTest extends TestCase { + + /** @var Backend|\PHPUnit\Framework\MockObject\MockObject */ + private $backend; + + /** @var NotificationProviderManager|\PHPUnit\Framework\MockObject\MockObject */ + private $notificationProviderManager; + + /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */ + private $userManager; + + /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject*/ + private $groupManager; + + /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */ + private $userSession; + + public const CALENDAR_DATA = <<<EOD +BEGIN:VCALENDAR +PRODID:-//Nextcloud calendar v1.6.4 +BEGIN:VEVENT +CREATED:20160602T133732 +DTSTAMP:20160602T133732 +LAST-MODIFIED:20160602T133732 +UID:wej2z68l9h +SUMMARY:Test Event +LOCATION:Somewhere ... +DESCRIPTION:maybe .... +DTSTART;TZID=Europe/Berlin;VALUE=DATE:20160609 +DTEND;TZID=Europe/Berlin;VALUE=DATE:20160610 +BEGIN:VALARM +ACTION:EMAIL +TRIGGER:-PT15M +END:VALARM +END:VEVENT +END:VCALENDAR +EOD; + + public function setUp() { + parent::setUp(); + + $this->backend = $this->createMock(Backend::class); + $this->notificationProviderManager = $this->createMock(NotificationProviderManager::class); + $this->userManager = $this->createMock(IUserManager::class); + $this->groupManager = $this->createMock(IGroupManager::class); + $this->userSession = $this->createMock(IUserSession::class); + } + + public function dataTestProcessReminders(): array + { + return [ + [ + [], null + ], + [ + [ + [ + 'calendardata' => self::CALENDAR_DATA, + 'displayname' => 'Personal', + 'type' => 'EMAIL', + 'uid' => 1, + 'id' => 1, + ], + ], + $this->createMock(EmailProvider::class), + ], + [ + [ + [ + 'calendardata' => self::CALENDAR_DATA, + 'displayname' => 'Personal', + 'type' => 'DISPLAY', + 'uid' => 1, + 'id' => 1, + ], + ], + $this->createMock(PushProvider::class), + ] + ]; + } + + /** + * @dataProvider dataTestProcessReminders + * @param array $reminders + * @param AbstractNotificationProvider|null $notificationProvider + * @throws \OCA\DAV\CalDAV\Reminder\NotificationProvider\ProviderNotAvailableException + * @throws \OCA\DAV\CalDAV\Reminder\NotificationTypeDoesNotExistException + * @throws \OC\User\NoUserException + */ + public function testProcessReminders(array $reminders, ?AbstractNotificationProvider $notificationProvider): void + { + $user = $this->createMock(IUser::class); + + $this->backend->expects($this->once())->method('getRemindersToProcess')->willReturn($reminders); + if (count($reminders) > 0) { + $this->userManager->expects($this->exactly(count($reminders)))->method('get')->willReturn($user); + $this->backend->expects($this->exactly(count($reminders)))->method('removeReminder'); + $this->notificationProviderManager->expects($this->exactly(count($reminders)))->method('getProvider')->willReturn($notificationProvider); + } + + $reminderService = new ReminderService($this->backend, $this->notificationProviderManager, $this->userManager, $this->groupManager, $this->userSession); + $reminderService->processReminders(); + } + + /** + * @expectedException OC\User\NoUserException + */ + public function testProcessReminderWithBadUser(): void + { + $this->backend->expects($this->once())->method('getRemindersToProcess')->willReturn([ + [ + 'calendardata' => self::CALENDAR_DATA, + 'type' => 'DISPLAY', + 'uid' => 1, + 'id' => 1, + ] + ]); + $this->userManager->expects($this->once())->method('get')->with(1)->willReturn(null); + $reminderService = new ReminderService($this->backend, $this->notificationProviderManager, $this->userManager, $this->groupManager, $this->userSession); + $reminderService->processReminders(); + } + + public function providesTouchCalendarObject(): array + { + return [ + [ + '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', + [ + 'principaluri' => 'principals/users/personal' + ], + [], + [ + 'calendarid' => 1, + 'uri' => 'something.ics', + ], + 0 + ], + [ + '\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', + [ + 'principaluri' => 'principals/users/personal' + ], + [], + [ + 'calendarid' => 1, + 'uri' => 'something.ics', + 'calendardata' => self::CALENDAR_DATA + ], + 0 + ], + [ + '\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', + [ + 'principaluri' => 'principals/users/someone', + 'uri' => 'personal' + ], + [ + [ + '{http://owncloud.org/ns}principal' => 'principals/users/someone' + ] + ], + [ + 'calendarid' => 1, + 'uri' => 'something.ics', + 'calendardata' => self::CALENDAR_DATA + ], + 0 + ], + [ + '\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', + [ + 'principaluri' => 'principals/users/someone', + 'uri' => 'personal' + ], + [ + [ + '{http://owncloud.org/ns}principal' => 'principals/groups/somegroup' + ] + ], + [ + 'calendarid' => 1, + 'uri' => 'something.ics', + 'calendardata' => self::CALENDAR_DATA + ], + 1 + ] + ]; + } + + /** + * @dataProvider providesTouchCalendarObject + * @param string $action + * @param array $calendarData + * @param array $shares + * @param array $objectData + * @param int $numberOfGroups + * @throws \OC\User\NoUserException + * @throws \Sabre\VObject\InvalidDataException + */ + public function testOnTouchCalendarObject(string $action, array $calendarData, array $shares, array $objectData, int $numberOfGroups): void + { + $this->backend->expects($this->once())->method('cleanRemindersForEvent')->with($objectData['calendarid'], $objectData['uri']); + + if ($action !== '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject') { + $user = $this->createMock(IUser::class); + $user->expects($this->once())->method('getUID')->willReturn('user'); + + $this->userSession->expects($this->once())->method('getUser')->willReturn($user); + if ($numberOfGroups === 0) { + $this->backend->expects($this->exactly(count($shares) + 1))->method('insertReminder'); + } else { + $group = $this->createMock(IGroup::class); + $groupUser = $this->createMock(IUser::class); + $groupUser->expects($this->once())->method('getUID')->willReturn('groupuser'); + $group->expects($this->once())->method('getUsers')->willReturn([$groupUser]); + $this->groupManager->expects($this->exactly($numberOfGroups))->method('get')->willReturn($group); + } + } + $reminderService = new ReminderService($this->backend, $this->notificationProviderManager, $this->userManager, $this->groupManager, $this->userSession); + $reminderService->onTouchCalendarObject($action, $calendarData, $shares, $objectData); + } + + /** + * @expectedException OC\User\NoUserException + */ + public function testOnTouchCalendarObjectWithNoSession(): void + { + $this->backend->expects($this->once())->method('cleanRemindersForEvent'); + $this->userSession->expects($this->once())->method('getUser')->willReturn(null); + + $reminderService = new ReminderService($this->backend, $this->notificationProviderManager, $this->userManager, $this->groupManager, $this->userSession); + $reminderService->onTouchCalendarObject('', ['principaluri' => 'foo'], [], ['calendarid' => 1, 'uri' => 'bar']); + } + + public function testOnTouchCalendarObjectWithNoCalendarURI(): void + { + $reminderService = new ReminderService($this->backend, $this->notificationProviderManager, $this->userManager, $this->groupManager, $this->userSession); + $this->assertNull($reminderService->onTouchCalendarObject('', [], [], [])); + } +} |