From 61c3630addbfd963c99db78b865bd3efe5ca319e Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Mon, 27 May 2019 13:49:51 +0200 Subject: [PATCH] Add MailerLog error reset on successful authorization email validation [MAILPOET-2022] --- lib/Services/AuthorizedEmailsController.php | 17 ++++++++ .../AuthorizedEmailsControllerTest.php | 41 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/lib/Services/AuthorizedEmailsController.php b/lib/Services/AuthorizedEmailsController.php index 8dbf5f3349..436ce0fc70 100644 --- a/lib/Services/AuthorizedEmailsController.php +++ b/lib/Services/AuthorizedEmailsController.php @@ -3,6 +3,8 @@ namespace MailPoet\Services; use Carbon\Carbon; +use MailPoet\Mailer\MailerError; +use MailPoet\Mailer\MailerLog; use MailPoet\Models\Newsletter; use MailPoet\Settings\SettingsController; @@ -27,6 +29,7 @@ class AuthorizedEmailsController { $authorized_emails_release_date = new Carbon('2019-03-06'); if (!Bridge::isMPSendingServiceEnabled() || $installed_at < $authorized_emails_release_date) { $this->settings->set(self::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING, null); + $this->updateMailerLog(); return; } @@ -41,6 +44,7 @@ class AuthorizedEmailsController { $result = $this->validateAddressesInSettings($authorized_emails, $result); $result = $this->validateAddressesInScheduledAndAutomaticEmails($authorized_emails, $result); $this->settings->set(self::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING, $result ?: null); + $this->updateMailerLog($result); } function onSettingsSave($settings) { @@ -96,6 +100,19 @@ class AuthorizedEmailsController { return $result; } + /** + * @param array|null $error + */ + private function updateMailerLog(array $error = null) { + if ($error) { + return; + } + $mailer_log_error = MailerLog::getError(); + if ($mailer_log_error && $mailer_log_error['operation'] === MailerError::OPERATION_AUTHORIZATION) { + MailerLog::resumeSending(); + } + } + private function validateAuthorizedEmail($authorized_emails, $email) { return in_array(strtolower($email), $authorized_emails, true); } diff --git a/tests/integration/Services/AuthorizedEmailsControllerTest.php b/tests/integration/Services/AuthorizedEmailsControllerTest.php index 4b4eb8057b..b3baf358f0 100644 --- a/tests/integration/Services/AuthorizedEmailsControllerTest.php +++ b/tests/integration/Services/AuthorizedEmailsControllerTest.php @@ -5,6 +5,8 @@ namespace MailPoet\Test\Services; use Carbon\Carbon; use Codeception\Stub\Expected; use MailPoet\Mailer\Mailer; +use MailPoet\Mailer\MailerError; +use MailPoet\Mailer\MailerLog; use MailPoet\Models\Newsletter; use MailPoet\Models\Setting; use MailPoet\Services\AuthorizedEmailsController; @@ -118,6 +120,45 @@ class AuthorizedEmailsControllerTest extends \MailPoetTest { expect($error)->null(); } + function testItResetsUnauthorizedErrorInMailerLog() { + $log = MailerLog::setError(MailerLog::getMailerLog(), MailerError::OPERATION_AUTHORIZATION, 'message'); + MailerLog::updateMailerLog($log); + $this->settings->set('installed_at', new Carbon()); + $this->settings->set('sender.address', 'auth@email.com'); + $this->settings->set('signup_confirmation.from.address', 'auth@email.com'); + $this->setMailPoetSendingMethod(); + $controller = $this->getController($authorized_emails_from_api = ['auth@email.com']); + $controller->checkAuthorizedEmailAddresses(); + $error = MailerLog::getError(); + expect($error)->null(); + } + + function testItDoesNotResetOtherErrorInMailerLog() { + $log = MailerLog::setError(MailerLog::getMailerLog(), MailerError::OPERATION_SEND, 'message'); + MailerLog::updateMailerLog($log); + $this->settings->set('installed_at', new Carbon()); + $this->settings->set('sender.address', 'auth@email.com'); + $this->settings->set('signup_confirmation.from.address', 'auth@email.com'); + $this->setMailPoetSendingMethod(); + $controller = $this->getController($authorized_emails_from_api = ['auth@email.com']); + $controller->checkAuthorizedEmailAddresses(); + $error = MailerLog::getError(); + expect($error['operation'])->equals(MailerError::OPERATION_SEND); + } + + function testItDoesNotResetMailerLogItErrorPersists() { + $log = MailerLog::setError(MailerLog::getMailerLog(), MailerError::OPERATION_AUTHORIZATION, 'message'); + MailerLog::updateMailerLog($log); + $this->settings->set('installed_at', new Carbon()); + $this->settings->set('sender.address', 'invalid@email.com'); + $this->settings->set('signup_confirmation.from.address', 'invalid@email.com'); + $this->setMailPoetSendingMethod(); + $controller = $this->getController($authorized_emails_from_api = ['auth@email.com']); + $controller->checkAuthorizedEmailAddresses(); + $error = MailerLog::getError(); + expect($error['operation'])->equals(MailerError::OPERATION_AUTHORIZATION); + } + private function checkUnauthorizedInNewsletter($type, $status) { $newsletter = Newsletter::createOrUpdate([ 'subject' => 'Subject',