From 8ade2cf58845c6a5a8cddf13c92cb1faa4be024e Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Tue, 11 Aug 2020 09:19:51 +0200 Subject: [PATCH] Migrate to Nextcloud 20's bootstrap mechanism Signed-off-by: Christoph Wurst --- .travis.yml | 4 - appinfo/app.php | 46 --------- appinfo/info.xml | 10 +- lib/AppInfo/Application.php | 94 ++++++++++--------- lib/Service/AutoConfig/SmtpServerDetector.php | 13 +-- lib/Service/UserPreferenceSevice.php | 4 +- lib/SystemConfig.php | 42 +++++++++ .../Autoconfig/SmtpServerDetectorTest.php | 28 +++++- 8 files changed, 135 insertions(+), 106 deletions(-) delete mode 100755 appinfo/app.php create mode 100644 lib/SystemConfig.php diff --git a/.travis.yml b/.travis.yml index ce14d33a6..f0aae8943 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,10 +30,6 @@ matrix: env: "TEST_SUITE=PACKAGE" - php: 7.2 env: "PHP_COVERAGE=TRUE" - - php: 7.3 - env: "CORE_BRANCH=stable18" - - php: 7.4 - env: "CORE_BRANCH=stable19" - php: nightly env: "DB=sqlite PATCH_VERSION_CHECK=TRUE" fast_finish: true diff --git a/appinfo/app.php b/appinfo/app.php deleted file mode 100755 index d66f63c7a..000000000 --- a/appinfo/app.php +++ /dev/null @@ -1,46 +0,0 @@ - - * @author Jan-Christoph Borchardt - * @author Thomas Imbreckx - * @author Thomas Müller - * - * Mail - * - * 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 - * - */ -use OCA\Mail\HordeTranslationHandler; - -if ((@include_once __DIR__ . '/../vendor/autoload.php')===false) { - throw new Exception('Cannot include autoload. Did you run install dependencies using composer?'); -} - -// bypass Horde Translation system -Horde_Translation::setHandler('Horde_Imap_Client', new HordeTranslationHandler()); -Horde_Translation::setHandler('Horde_Mime', new HordeTranslationHandler()); -Horde_Translation::setHandler('Horde_Smtp', new HordeTranslationHandler()); - -$l = \OC::$server->getL10N('mail'); -$g = \OC::$server->getURLGenerator(); - -\OC::$server->getNavigationManager()->add( - [ - 'id' => 'mail', - 'order' => 3, - 'href' => $g->linkToRoute('mail.page.index'), - 'icon' => $g->imagePath('mail', 'mail.svg'), - 'name' => $l->t('Mail'), - ] -); diff --git a/appinfo/info.xml b/appinfo/info.xml index f5484259f..665b7cc40 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -31,7 +31,7 @@ https://user-images.githubusercontent.com/1374172/79554966-278e1600-809f-11ea-82ea-7a0d72a2704f.png - + @@ -53,4 +53,12 @@ OCA\Mail\Settings\AdminSettings + + + Mail + mail.page.index + mail.svg + 3 + + diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 10002803d..76f0b9a11 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -24,12 +24,15 @@ declare(strict_types=1); namespace OCA\Mail\AppInfo; +use Exception; +use Horde_Translation; use OCA\Mail\Contracts\IAttachmentService; use OCA\Mail\Contracts\IAvatarService; use OCA\Mail\Contracts\IMailManager; use OCA\Mail\Contracts\IMailSearch; use OCA\Mail\Contracts\IMailTransmission; use OCA\Mail\Contracts\IUserPreferences; +use OCA\Mail\Dashboard\MailWidget; use OCA\Mail\Events\BeforeMessageDeletedEvent; use OCA\Mail\Events\DraftSavedEvent; use OCA\Mail\Events\SynchronizationEvent; @@ -38,10 +41,10 @@ use OCA\Mail\Events\MessageFlaggedEvent; use OCA\Mail\Events\MessageSentEvent; use OCA\Mail\Events\NewMessagesSynchronized; use OCA\Mail\Events\SaveDraftEvent; +use OCA\Mail\HordeTranslationHandler; use OCA\Mail\Http\Middleware\ErrorMiddleware; use OCA\Mail\Http\Middleware\ProvisioningMiddleware; use OCA\Mail\Listener\AddressCollectionListener; -use OCA\Mail\Listener\DashboardPanelListener; use OCA\Mail\Listener\DeleteDraftListener; use OCA\Mail\Listener\DraftMailboxCreatorListener; use OCA\Mail\Listener\FlagRepliedMessageListener; @@ -59,64 +62,67 @@ use OCA\Mail\Service\MailTransmission; use OCA\Mail\Service\Search\MailSearch; use OCA\Mail\Service\UserPreferenceSevice; use OCP\AppFramework\App; -use OCP\AppFramework\IAppContainer; -use OCP\Dashboard\RegisterWidgetEvent; -use OCP\EventDispatcher\IEventDispatcher; +use OCP\AppFramework\Bootstrap\IBootContext; +use OCP\AppFramework\Bootstrap\IBootstrap; +use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\IServerContainer; use OCP\User\Events\UserDeletedEvent; use OCP\Util; +use Psr\Container\ContainerInterface; -class Application extends App { +class Application extends App implements IBootstrap { public const APP_ID = 'mail'; public function __construct(array $urlParams = []) { parent::__construct(self::APP_ID, $urlParams); - - $this->initializeAppContainer($this->getContainer()); - $this->registerEvents($this->getContainer()); } - private function initializeAppContainer(IAppContainer $container): void { - $transport = $container->getServer()->getConfig()->getSystemValue('app.mail.transport', 'smtp'); - $testSmtp = $transport === 'smtp'; + public function register(IRegistrationContext $context): void { + if ((@include_once __DIR__ . '/../../vendor/autoload.php') === false) { + throw new Exception('Cannot include autoload. Did you run install dependencies using composer?'); + } - $container->registerAlias(IAvatarService::class, AvatarService::class); - $container->registerAlias(IAttachmentService::class, AttachmentService::class); - $container->registerAlias(IMailManager::class, MailManager::class); - $container->registerAlias(IMailSearch::class, MailSearch::class); - $container->registerAlias(IMailTransmission::class, MailTransmission::class); - $container->registerAlias(IUserPreferences::class, UserPreferenceSevice::class); + $context->registerParameter('hostname', Util::getServerHostName()); - $container->registerService("userFolder", function () use ($container) { - $user = $container->query("UserId"); - return $container->getServer()->getUserFolder($user); + $context->registerService('userFolder', function (ContainerInterface $c) { + $userContainer = $c->get(IServerContainer::class); + $uid = $c->get('UserId'); + + return $userContainer->getUserFolder($uid); }); - $container->registerParameter('testSmtp', $testSmtp); - $container->registerParameter('hostname', Util::getServerHostName()); + $context->registerServiceAlias(IAvatarService::class, AvatarService::class); + $context->registerServiceAlias(IAttachmentService::class, AttachmentService::class); + $context->registerServiceAlias(IMailManager::class, MailManager::class); + $context->registerServiceAlias(IMailSearch::class, MailSearch::class); + $context->registerServiceAlias(IMailTransmission::class, MailTransmission::class); + $context->registerServiceAlias(IUserPreferences::class, UserPreferenceSevice::class); - $container->registerAlias('ErrorMiddleware', ErrorMiddleware::class); - $container->registerMiddleWare('ErrorMiddleware'); - $container->registerAlias('ProvisioningMiddleware', ProvisioningMiddleware::class); - $container->registerMiddleWare('ProvisioningMiddleware'); + $context->registerEventListener(BeforeMessageDeletedEvent::class, TrashMailboxCreatorListener::class); + $context->registerEventListener(DraftSavedEvent::class, DeleteDraftListener::class); + $context->registerEventListener(MessageFlaggedEvent::class, MessageCacheUpdaterListener::class); + $context->registerEventListener(MessageDeletedEvent::class, MessageCacheUpdaterListener::class); + $context->registerEventListener(MessageSentEvent::class, AddressCollectionListener::class); + $context->registerEventListener(MessageSentEvent::class, DeleteDraftListener::class); + $context->registerEventListener(MessageSentEvent::class, FlagRepliedMessageListener::class); + $context->registerEventListener(MessageSentEvent::class, InteractionListener::class); + $context->registerEventListener(MessageSentEvent::class, SaveSentMessageListener::class); + $context->registerEventListener(NewMessagesSynchronized::class, NewMessageClassificationListener::class); + $context->registerEventListener(SaveDraftEvent::class, DraftMailboxCreatorListener::class); + $context->registerEventListener(SynchronizationEvent::class, AccountSynchronizedThreadUpdaterListener::class); + $context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class); + + $context->registerMiddleWare(ErrorMiddleware::class); + $context->registerMiddleWare(ProvisioningMiddleware::class); + + $context->registerDashboardWidget(MailWidget::class); + + // bypass Horde Translation system + Horde_Translation::setHandler('Horde_Imap_Client', new HordeTranslationHandler()); + Horde_Translation::setHandler('Horde_Mime', new HordeTranslationHandler()); + Horde_Translation::setHandler('Horde_Smtp', new HordeTranslationHandler()); } - private function registerEvents(IAppContainer $container): void { - /** @var IEventDispatcher $dispatcher */ - $dispatcher = $container->query(IEventDispatcher::class); - - $dispatcher->addServiceListener(BeforeMessageDeletedEvent::class, TrashMailboxCreatorListener::class); - $dispatcher->addServiceListener(DraftSavedEvent::class, DeleteDraftListener::class); - $dispatcher->addServiceListener(MessageFlaggedEvent::class, MessageCacheUpdaterListener::class); - $dispatcher->addServiceListener(MessageDeletedEvent::class, MessageCacheUpdaterListener::class); - $dispatcher->addServiceListener(MessageSentEvent::class, AddressCollectionListener::class); - $dispatcher->addServiceListener(MessageSentEvent::class, DeleteDraftListener::class); - $dispatcher->addServiceListener(MessageSentEvent::class, FlagRepliedMessageListener::class); - $dispatcher->addServiceListener(MessageSentEvent::class, InteractionListener::class); - $dispatcher->addServiceListener(MessageSentEvent::class, SaveSentMessageListener::class); - $dispatcher->addServiceListener(NewMessagesSynchronized::class, NewMessageClassificationListener::class); - $dispatcher->addServiceListener(SaveDraftEvent::class, DraftMailboxCreatorListener::class); - $dispatcher->addServiceListener(SynchronizationEvent::class, AccountSynchronizedThreadUpdaterListener::class); - $dispatcher->addServiceListener(UserDeletedEvent::class, UserDeletedListener::class); - $dispatcher->addServiceListener(RegisterWidgetEvent::class, DashboardPanelListener::class); + public function boot(IBootContext $context): void { } } diff --git a/lib/Service/AutoConfig/SmtpServerDetector.php b/lib/Service/AutoConfig/SmtpServerDetector.php index 67741d228..a323ded98 100644 --- a/lib/Service/AutoConfig/SmtpServerDetector.php +++ b/lib/Service/AutoConfig/SmtpServerDetector.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace OCA\Mail\Service\AutoConfig; use OCA\Mail\Db\MailAccount; +use OCA\Mail\SystemConfig; class SmtpServerDetector { @@ -33,20 +34,20 @@ class SmtpServerDetector { /** @var SmtpConnectivityTester */ private $smtpConnectivityTester; - /** @var bool */ - private $testSmtp; + /** @var SystemConfig */ + private $systemConfig; /** * @param MxRecord $mxRecord * @param SmtpConnectivityTester $smtpTester - * @param bool $testSmtp + * @param */ public function __construct(MxRecord $mxRecord, SmtpConnectivityTester $smtpTester, - bool $testSmtp) { + SystemConfig $systemConfig) { $this->mxRecord = $mxRecord; $this->smtpConnectivityTester = $smtpTester; - $this->testSmtp = $testSmtp; + $this->systemConfig = $systemConfig; } /** @@ -58,7 +59,7 @@ class SmtpServerDetector { public function detect(MailAccount $account, string $email, string $password): bool { - if (!$this->testSmtp) { + if (!$this->systemConfig->hasWorkingSmtp()) { return true; } diff --git a/lib/Service/UserPreferenceSevice.php b/lib/Service/UserPreferenceSevice.php index f6881fbc6..aff05ace0 100644 --- a/lib/Service/UserPreferenceSevice.php +++ b/lib/Service/UserPreferenceSevice.php @@ -1,5 +1,7 @@ * @@ -40,8 +42,8 @@ class UserPreferenceSevice implements IUserPreferences { * @param string $UserId */ public function __construct(IConfig $config, $UserId) { - $this->UserId = $UserId; $this->config = $config; + $this->UserId = $UserId; } /** diff --git a/lib/SystemConfig.php b/lib/SystemConfig.php new file mode 100644 index 000000000..25375ae0f --- /dev/null +++ b/lib/SystemConfig.php @@ -0,0 +1,42 @@ + + * + * @author 2020 Christoph Wurst + * + * @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 . + */ + +namespace OCA\Mail; + +use OCP\IConfig; + +class SystemConfig { + + /** @var IConfig */ + private $config; + + public function __construct(IConfig $config) { + $this->config = $config; + } + + public function hasWorkingSmtp(): bool { + return $this->config->getSystemValue('app.mail.transport', 'smtp') === 'smtp'; + } +} diff --git a/tests/Unit/Service/Autoconfig/SmtpServerDetectorTest.php b/tests/Unit/Service/Autoconfig/SmtpServerDetectorTest.php index fee8949d1..0dba31e01 100644 --- a/tests/Unit/Service/Autoconfig/SmtpServerDetectorTest.php +++ b/tests/Unit/Service/Autoconfig/SmtpServerDetectorTest.php @@ -1,5 +1,7 @@ * @@ -26,16 +28,20 @@ use OCA\Mail\Db\MailAccount; use OCA\Mail\Service\AutoConfig\MxRecord; use OCA\Mail\Service\AutoConfig\SmtpConnectivityTester; use OCA\Mail\Service\AutoConfig\SmtpServerDetector; -use PHPUnit_Framework_MockObject_MockObject; +use OCA\Mail\SystemConfig; +use PHPUnit\Framework\MockObject\MockObject; class SmtpServerDetectorTest extends TestCase { - /** @var MxRecord|PHPUnit_Framework_MockObject_MockObject */ + /** @var MxRecord|MockObject */ private $mxRecord; - /** @var SmtpConnectivityTester|PHPUnit_Framework_MockObject_MockObject */ + /** @var SmtpConnectivityTester|MockObject */ private $smtpConnectivityTester; + /** @var SystemConfig|MockObject */ + private $systemConfig; + /** @var SmtpServerDetector */ private $detector; @@ -44,14 +50,22 @@ class SmtpServerDetectorTest extends TestCase { $this->mxRecord = $this->createMock(MxRecord::class); $this->smtpConnectivityTester = $this->createMock(SmtpConnectivityTester::class); + $this->systemConfig = $this->createMock(SystemConfig::class); - $this->detector = new SmtpServerDetector($this->mxRecord, $this->smtpConnectivityTester, true); + $this->detector = new SmtpServerDetector( + $this->mxRecord, + $this->smtpConnectivityTester, + $this->systemConfig + ); } public function testDetectNo() { $account = $this->createMock(MailAccount::class); $email = 'user@domain.tld'; $password = 'mypassword'; + $this->systemConfig->expects($this->once()) + ->method('hasWorkingSmtp') + ->willReturn(true); $this->mxRecord->expects($this->once()) ->method('query') ->with($this->equalTo('domain.tld')) @@ -70,6 +84,9 @@ class SmtpServerDetectorTest extends TestCase { $account = $this->createMock(MailAccount::class); $email = 'user@domain.tld'; $password = 'mypassword'; + $this->systemConfig->expects($this->once()) + ->method('hasWorkingSmtp') + ->willReturn(true); $this->mxRecord->expects($this->once()) ->method('query') ->with($this->equalTo('domain.tld')) @@ -88,6 +105,9 @@ class SmtpServerDetectorTest extends TestCase { $account = $this->createMock(MailAccount::class); $email = 'user@domain.tld'; $password = 'mypassword'; + $this->systemConfig->expects($this->once()) + ->method('hasWorkingSmtp') + ->willReturn(true); $this->mxRecord->expects($this->once()) ->method('query') ->with($this->equalTo('domain.tld'))