Refactor authorized emails validation from Bridge to Controller

[MAILPOET-2022]
This commit is contained in:
Rostislav Wolny
2019-05-23 17:52:45 +02:00
committed by M. Shull
parent f3d8ac4c7d
commit f86c0c9612
18 changed files with 246 additions and 139 deletions

View File

@ -6,7 +6,7 @@ use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\API\JSON\Error as APIError; use MailPoet\API\JSON\Error as APIError;
use MailPoet\Config\AccessControl; use MailPoet\Config\AccessControl;
use MailPoet\Mailer\MailerLog; use MailPoet\Mailer\MailerLog;
use MailPoet\Services\Bridge; use MailPoet\Services\AuthorizedEmailsController;
use MailPoet\Settings\SettingsController; use MailPoet\Settings\SettingsController;
use MailPoet\WP\Functions as WPFunctions; use MailPoet\WP\Functions as WPFunctions;
@ -14,8 +14,8 @@ if (!defined('ABSPATH')) exit;
class Mailer extends APIEndpoint { class Mailer extends APIEndpoint {
/** @var Bridge */ /** @var AuthorizedEmailsController */
private $bridge; private $authorized_emails_controller;
/** @var SettingsController */ /** @var SettingsController */
private $settings; private $settings;
@ -24,8 +24,8 @@ class Mailer extends APIEndpoint {
'global' => AccessControl::PERMISSION_MANAGE_EMAILS, 'global' => AccessControl::PERMISSION_MANAGE_EMAILS,
]; ];
function __construct(Bridge $bridge, SettingsController $settings) { function __construct(AuthorizedEmailsController $authorized_emails_controller, SettingsController $settings) {
$this->bridge = $bridge; $this->authorized_emails_controller = $authorized_emails_controller;
$this->settings = $settings; $this->settings = $settings;
} }
@ -55,8 +55,8 @@ class Mailer extends APIEndpoint {
} }
function resumeSending() { function resumeSending() {
if ($this->settings->get(Bridge::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING_NAME)) { if ($this->settings->get(AuthorizedEmailsController::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING)) {
$this->bridge->checkAuthorizedEmailAddresses(); $this->authorized_emails_controller->checkAuthorizedEmailAddresses();
} }
MailerLog::resumeSending(); MailerLog::resumeSending();
return $this->successResponse(null); return $this->successResponse(null);

View File

@ -9,6 +9,7 @@ use MailPoet\Config\AccessControl;
use MailPoet\Cron\Workers\InactiveSubscribers; use MailPoet\Cron\Workers\InactiveSubscribers;
use MailPoet\Cron\Workers\WooCommerceSync; use MailPoet\Cron\Workers\WooCommerceSync;
use MailPoet\Models\ScheduledTask; use MailPoet\Models\ScheduledTask;
use MailPoet\Services\AuthorizedEmailsController;
use MailPoet\Services\Bridge; use MailPoet\Services\Bridge;
use MailPoet\Settings\SettingsController; use MailPoet\Settings\SettingsController;
use MailPoet\WP\Functions as WPFunctions; use MailPoet\WP\Functions as WPFunctions;
@ -21,12 +22,25 @@ class Settings extends APIEndpoint {
/** @var SettingsController */ /** @var SettingsController */
private $settings; private $settings;
/** @var Bridge */
private $bridge;
/** @var AuthorizedEmailsController */
private $authorized_emails_controller;
public $permissions = [ public $permissions = [
'global' => AccessControl::PERMISSION_MANAGE_SETTINGS, 'global' => AccessControl::PERMISSION_MANAGE_SETTINGS,
]; ];
function __construct(SettingsController $settings) {
public function __construct(
SettingsController $settings,
Bridge $bridge,
AuthorizedEmailsController $authorized_emails_controller
) {
$this->settings = $settings; $this->settings = $settings;
$this->bridge = $bridge;
$this->authorized_emails_controller = $authorized_emails_controller;
} }
function get() { function get() {
@ -50,8 +64,8 @@ class Settings extends APIEndpoint {
$this->onSettingsChange($old_settings, $this->settings->getAll()); $this->onSettingsChange($old_settings, $this->settings->getAll());
$bridge = new Bridge(); $this->bridge->onSettingsSave($settings);
$bridge->onSettingsSave($settings); $this->authorized_emails_controller->onSettingsSave($settings);
// Will be uncommented on task [MAILPOET-1998] // Will be uncommented on task [MAILPOET-1998]
// if ($signup_confirmation !== $this->settings->get('signup_confirmation.enabled')) { // if ($signup_confirmation !== $this->settings->get('signup_confirmation.enabled')) {
// Form::updateSuccessMessages(); // Form::updateSuccessMessages();

View File

@ -3,6 +3,7 @@ namespace MailPoet\Cron\Workers;
use MailPoet\Cron\CronHelper; use MailPoet\Cron\CronHelper;
use MailPoet\Models\ScheduledTask; use MailPoet\Models\ScheduledTask;
use MailPoet\Services\AuthorizedEmailsController;
use MailPoet\Services\Bridge; use MailPoet\Services\Bridge;
use MailPoet\Settings\SettingsController; use MailPoet\Settings\SettingsController;
use MailPoet\Subscribers\InactiveSubscribersController; use MailPoet\Subscribers\InactiveSubscribersController;
@ -13,11 +14,11 @@ class AuthorizedSendingEmailsCheck extends SimpleWorker {
const TASK_TYPE = 'authorized_email_addresses_check'; const TASK_TYPE = 'authorized_email_addresses_check';
const AUTOMATIC_SCHEDULING = false; const AUTOMATIC_SCHEDULING = false;
/** @var Bridge */ /** @var AuthorizedEmailsController */
private $bridge; private $authorized_emails_controller;
function __construct(Bridge $bridge, $timer = false) { function __construct(AuthorizedEmailsController $authorized_emails_controller, $timer = false) {
$this->bridge = $bridge; $this->authorized_emails_controller = $authorized_emails_controller;
parent::__construct($timer); parent::__construct($timer);
} }
@ -26,7 +27,7 @@ class AuthorizedSendingEmailsCheck extends SimpleWorker {
} }
function processTaskStrategy(ScheduledTask $task) { function processTaskStrategy(ScheduledTask $task) {
$this->bridge->checkAuthorizedEmailAddresses(); $this->authorized_emails_controller->checkAuthorizedEmailAddresses();
return true; return true;
} }
} }

View File

@ -15,7 +15,7 @@ use MailPoet\Cron\Workers\WooCommerceSync as WooCommerceSyncWorker;
use MailPoet\Cron\Workers\SendingQueue\SendingErrorHandler; use MailPoet\Cron\Workers\SendingQueue\SendingErrorHandler;
use MailPoet\Features\FeaturesController; use MailPoet\Features\FeaturesController;
use MailPoet\Segments\WooCommerce as WooCommerceSegment; use MailPoet\Segments\WooCommerce as WooCommerceSegment;
use MailPoet\Services\Bridge; use MailPoet\Services\AuthorizedEmailsController;
use MailPoet\WooCommerce\Helper as WooCommerceHelper; use MailPoet\WooCommerce\Helper as WooCommerceHelper;
use MailPoet\Mailer\Mailer; use MailPoet\Mailer\Mailer;
use MailPoet\Settings\SettingsController; use MailPoet\Settings\SettingsController;
@ -47,8 +47,8 @@ class WorkersFactory {
/** @var WooCommerceHelper */ /** @var WooCommerceHelper */
private $woocommerce_helper; private $woocommerce_helper;
/** @var Bridge */ /** @var AuthorizedEmailsController */
private $bridge; private $authorized_emails_controller;
/** /**
* @var Renderer * @var Renderer
@ -65,7 +65,7 @@ class WorkersFactory {
WooCommerceSegment $woocommerce_segment, WooCommerceSegment $woocommerce_segment,
InactiveSubscribersController $inactive_subscribers_controller, InactiveSubscribersController $inactive_subscribers_controller,
WooCommerceHelper $woocommerce_helper, WooCommerceHelper $woocommerce_helper,
Bridge $bridge AuthorizedEmailsController $authorized_emails_controller
) { ) {
$this->sending_error_handler = $sending_error_handler; $this->sending_error_handler = $sending_error_handler;
$this->scheduler = $scheduler; $this->scheduler = $scheduler;
@ -76,7 +76,7 @@ class WorkersFactory {
$this->woocommerce_segment = $woocommerce_segment; $this->woocommerce_segment = $woocommerce_segment;
$this->inactive_subscribers_controller = $inactive_subscribers_controller; $this->inactive_subscribers_controller = $inactive_subscribers_controller;
$this->woocommerce_helper = $woocommerce_helper; $this->woocommerce_helper = $woocommerce_helper;
$this->bridge = $bridge; $this->authorized_emails_controller = $authorized_emails_controller;
} }
/** @return SchedulerWorker */ /** @return SchedulerWorker */
@ -130,7 +130,7 @@ class WorkersFactory {
/** @return AuthorizedSendingEmailsCheck */ /** @return AuthorizedSendingEmailsCheck */
function createAuthorizedSendingEmailsCheckWorker($timer) { function createAuthorizedSendingEmailsCheckWorker($timer) {
return new AuthorizedSendingEmailsCheck($this->bridge, $timer); return new AuthorizedSendingEmailsCheck($this->authorized_emails_controller, $timer);
} }
} }

View File

@ -106,6 +106,7 @@ class ContainerConfigurator implements IContainerConfigurator {
$container->autowire(\MailPoet\Segments\WooCommerce::class)->setPublic(true); $container->autowire(\MailPoet\Segments\WooCommerce::class)->setPublic(true);
// Services // Services
$container->autowire(\MailPoet\Services\Bridge::class); $container->autowire(\MailPoet\Services\Bridge::class);
$container->autowire(\MailPoet\Services\AuthorizedEmailsController::class);
// Settings // Settings
$container->autowire(\MailPoet\Settings\SettingsController::class)->setPublic(true); $container->autowire(\MailPoet\Settings\SettingsController::class)->setPublic(true);
// User Flags // User Flags

View File

@ -6,6 +6,7 @@ use MailPoet\Mailer\Methods\ErrorMappers\MailPoetMapper;
use MailPoet\Mailer\Methods\ErrorMappers\PHPMailMapper; use MailPoet\Mailer\Methods\ErrorMappers\PHPMailMapper;
use MailPoet\Mailer\Methods\ErrorMappers\SendGridMapper; use MailPoet\Mailer\Methods\ErrorMappers\SendGridMapper;
use MailPoet\Mailer\Methods\ErrorMappers\SMTPMapper; use MailPoet\Mailer\Methods\ErrorMappers\SMTPMapper;
use MailPoet\Services\AuthorizedEmailsController;
use MailPoet\Services\Bridge; use MailPoet\Services\Bridge;
use MailPoet\Settings\SettingsController; use MailPoet\Settings\SettingsController;
@ -58,7 +59,7 @@ class Mailer {
$this->sender, $this->sender,
$this->reply_to, $this->reply_to,
new MailPoetMapper(), new MailPoetMapper(),
new Bridge() new AuthorizedEmailsController(new SettingsController, new Bridge)
); );
break; break;
case self::METHOD_SENDGRID: case self::METHOD_SENDGRID:

View File

@ -5,6 +5,7 @@ use MailPoet\Mailer\Mailer;
use MailPoet\Config\ServicesChecker; use MailPoet\Config\ServicesChecker;
use MailPoet\Mailer\MailerError; use MailPoet\Mailer\MailerError;
use MailPoet\Mailer\Methods\ErrorMappers\MailPoetMapper; use MailPoet\Mailer\Methods\ErrorMappers\MailPoetMapper;
use MailPoet\Services\AuthorizedEmailsController;
use MailPoet\Services\Bridge; use MailPoet\Services\Bridge;
use MailPoet\Services\Bridge\API; use MailPoet\Services\Bridge\API;
@ -16,19 +17,19 @@ class MailPoet {
public $reply_to; public $reply_to;
public $services_checker; public $services_checker;
/** @var Bridge */ /** @var AuthorizedEmailsController */
private $bridge; private $authorized_emails_controller;
/** @var MailPoetMapper */ /** @var MailPoetMapper */
private $error_mapper; private $error_mapper;
function __construct($api_key, $sender, $reply_to, MailPoetMapper $error_mapper, Bridge $bridge) { function __construct($api_key, $sender, $reply_to, MailPoetMapper $error_mapper, AuthorizedEmailsController $authorized_emails_controller) {
$this->api = new API($api_key); $this->api = new API($api_key);
$this->sender = $sender; $this->sender = $sender;
$this->reply_to = $reply_to; $this->reply_to = $reply_to;
$this->services_checker = new ServicesChecker(); $this->services_checker = new ServicesChecker();
$this->error_mapper = $error_mapper; $this->error_mapper = $error_mapper;
$this->bridge = $bridge; $this->authorized_emails_controller = $authorized_emails_controller;
} }
function send($newsletter, $subscriber, $extra_params = []) { function send($newsletter, $subscriber, $extra_params = []) {
@ -59,7 +60,7 @@ class MailPoet {
&& $result['code'] === API::RESPONSE_CODE_CAN_NOT_SEND && $result['code'] === API::RESPONSE_CODE_CAN_NOT_SEND
&& $result['message'] === MailerError::MESSAGE_EMAIL_NOT_AUTHORIZED && $result['message'] === MailerError::MESSAGE_EMAIL_NOT_AUTHORIZED
) { ) {
$this->bridge->checkAuthorizedEmailAddresses(); $this->authorized_emails_controller->checkAuthorizedEmailAddresses();
} }
return $this->error_mapper->getErrorForResult($result, $subscriber, $this->sender, $newsletter); return $this->error_mapper->getErrorForResult($result, $subscriber, $this->sender, $newsletter);
} }

View File

@ -0,0 +1,64 @@
<?php
namespace MailPoet\Services;
use Carbon\Carbon;
use MailPoet\Settings\SettingsController;
if (!defined('ABSPATH')) exit;
class AuthorizedEmailsController {
const AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING = 'authorized_emails_addresses_check';
/** @var Bridge */
public $bridge;
/** @var SettingsController */
private $settings;
function __construct(SettingsController $settingsController, Bridge $bridge) {
$this->settings = $settingsController;
$this->bridge = $bridge;
}
function checkAuthorizedEmailAddresses() {
$installed_at = new Carbon($this->settings->get('installed_at'));
$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);
return;
}
$authorized_emails = $this->bridge->getAuthorizedEmailAddresses();
// Keep previous check result for an invalid response from API
if ($authorized_emails === false) {
return;
}
$result = $this->validateAddressesInSettings($authorized_emails);
$this->settings->set(self::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING, $result ?: null);
}
function onSettingsSave($settings) {
$sender_address_set = !empty($settings['sender']['address']);
$confirmation_address_set = !empty($settings['signup_confirmation']['from']['address']);
if ($sender_address_set || $confirmation_address_set) {
$this->checkAuthorizedEmailAddresses();
}
}
private function validateAddressesInSettings($authorized_emails, $result = []) {
$default_sender_address = $this->settings->get('sender.address');
$signup_confirmation_address = $this->settings->get('signup_confirmation.from.address');
$authorized_emails = array_map('strtolower', $authorized_emails);
if (!in_array(strtolower($default_sender_address), $authorized_emails, true)) {
$result['invalid_sender_address'] = $default_sender_address;
}
if (!in_array(strtolower($signup_confirmation_address), $authorized_emails, true)) {
$result['invalid_confirmation_address'] = $signup_confirmation_address;
}
return $result;
}
}

View File

@ -86,33 +86,9 @@ class Bridge {
} }
} }
function checkAuthorizedEmailAddresses() { function getAuthorizedEmailAddresses() {
$installed_at = new Carbon($this->settings->get('installed_at'));
$authorized_emails_release_date = new Carbon('2019-03-06');
if (!self::isMPSendingServiceEnabled() || $installed_at < $authorized_emails_release_date) {
$this->settings->set(self::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING_NAME, null);
return;
}
$this->initApi($this->settings->get(self::API_KEY_SETTING_NAME)); $this->initApi($this->settings->get(self::API_KEY_SETTING_NAME));
$authorized_emails = $this->api->getAuthorizedEmailAddresses(); return $this->api->getAuthorizedEmailAddresses();
// Keep previous check result for an invalid response from API
if ($authorized_emails === false) {
return;
}
$default_sender_address = $this->settings->get('sender.address');
$signup_confirmation_address = $this->settings->get('signup_confirmation.from.address');
$authorized_emails = array_map('strtolower', $authorized_emails);
$result = [];
if (!in_array(strtolower($default_sender_address), $authorized_emails, true)) {
$result['invalid_sender_address'] = $default_sender_address;
}
if (!in_array(strtolower($signup_confirmation_address), $authorized_emails, true)) {
$result['invalid_confirmation_address'] = $signup_confirmation_address;
}
$this->settings->set(self::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING_NAME, $result ?: null);
} }
function checkMSSKey($api_key) { function checkMSSKey($api_key) {
@ -224,8 +200,6 @@ class Bridge {
function onSettingsSave($settings) { function onSettingsSave($settings) {
$api_key_set = !empty($settings[Mailer::MAILER_CONFIG_SETTING_NAME]['mailpoet_api_key']); $api_key_set = !empty($settings[Mailer::MAILER_CONFIG_SETTING_NAME]['mailpoet_api_key']);
$premium_key_set = !empty($settings['premium']['premium_key']); $premium_key_set = !empty($settings['premium']['premium_key']);
$sender_address_set = !empty($settings['sender']['address']);
$confirmation_address_set = !empty($settings['signup_confirmation']['from']['address']);
if ($api_key_set) { if ($api_key_set) {
$api_key = $settings[Mailer::MAILER_CONFIG_SETTING_NAME]['mailpoet_api_key']; $api_key = $settings[Mailer::MAILER_CONFIG_SETTING_NAME]['mailpoet_api_key'];
$state = $this->checkMSSKey($api_key); $state = $this->checkMSSKey($api_key);
@ -239,8 +213,5 @@ class Bridge {
$state = $this->checkPremiumKey($premium_key); $state = $this->checkPremiumKey($premium_key);
$this->storePremiumKeyAndState($premium_key, $state); $this->storePremiumKeyAndState($premium_key, $state);
} }
if ($sender_address_set || $confirmation_address_set) {
$this->checkAuthorizedEmailAddresses();
}
} }
} }

View File

@ -6,6 +6,7 @@ use Html2Text\Html2Text;
use MailPoet\Mailer\Mailer; use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\MailerError; use MailPoet\Mailer\MailerError;
use MailPoet\Models\Subscriber; use MailPoet\Models\Subscriber;
use MailPoet\Services\AuthorizedEmailsController;
use MailPoet\Services\Bridge; use MailPoet\Services\Bridge;
use MailPoet\Settings\SettingsController; use MailPoet\Settings\SettingsController;
use MailPoet\Subscription\Url; use MailPoet\Subscription\Url;
@ -53,7 +54,7 @@ class ConfirmationEmailMailer {
return false; return false;
} }
$authorization_emails_validation = $this->settings->get(Bridge::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING_NAME); $authorization_emails_validation = $this->settings->get(AuthorizedEmailsController::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING);
$unauthorized_confirmation_email = isset($authorization_emails_validation['invalid_confirmation_address']); $unauthorized_confirmation_email = isset($authorization_emails_validation['invalid_confirmation_address']);
if (Bridge::isMPSendingServiceEnabled() && $unauthorized_confirmation_email) { if (Bridge::isMPSendingServiceEnabled() && $unauthorized_confirmation_email) {
return false; return false;

View File

@ -2,6 +2,7 @@
namespace MailPoet\Util\Notices; namespace MailPoet\Util\Notices;
use MailPoet\Services\AuthorizedEmailsController;
use MailPoet\Services\Bridge; use MailPoet\Services\Bridge;
use MailPoet\Settings\SettingsController; use MailPoet\Settings\SettingsController;
use MailPoet\Util\Helpers; use MailPoet\Util\Helpers;
@ -25,7 +26,7 @@ class UnauthorizedEmailNotice {
} }
function init($should_display) { function init($should_display) {
$validation_error = $this->settings->get(Bridge::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING_NAME); $validation_error = $this->settings->get(AuthorizedEmailsController::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING);
if ($should_display && $validation_error) { if ($should_display && $validation_error) {
return $this->display($validation_error); return $this->display($validation_error);
} }

View File

@ -5,7 +5,7 @@ use Codeception\Stub\Expected;
use MailPoet\API\JSON\v1\Mailer; use MailPoet\API\JSON\v1\Mailer;
use MailPoet\API\JSON\Response as APIResponse; use MailPoet\API\JSON\Response as APIResponse;
use MailPoet\Mailer\MailerLog; use MailPoet\Mailer\MailerLog;
use MailPoet\Services\Bridge; use MailPoet\Services\AuthorizedEmailsController;
use MailPoet\Settings\SettingsController; use MailPoet\Settings\SettingsController;
class MailerTest extends \MailPoetTest { class MailerTest extends \MailPoetTest {
@ -16,9 +16,9 @@ class MailerTest extends \MailPoetTest {
$mailer_log = MailerLog::getMailerLog(); $mailer_log = MailerLog::getMailerLog();
expect($mailer_log['status'])->equals(MailerLog::STATUS_PAUSED); expect($mailer_log['status'])->equals(MailerLog::STATUS_PAUSED);
$settings = new SettingsController(); $settings = new SettingsController();
$bridge = $this->makeEmpty(Bridge::class, ['checkAuthorizedEmailAddresses' => Expected::never()]); $authorized_emails_controller = $this->makeEmpty(AuthorizedEmailsController::class, ['checkAuthorizedEmailAddresses' => Expected::never()]);
// resumeSending() method should clear the mailer log's status // resumeSending() method should clear the mailer log's status
$mailer_endpoint = new Mailer($bridge, $settings); $mailer_endpoint = new Mailer($authorized_emails_controller, $settings);
$response = $mailer_endpoint->resumeSending(); $response = $mailer_endpoint->resumeSending();
expect($response->status)->equals(APIResponse::STATUS_OK); expect($response->status)->equals(APIResponse::STATUS_OK);
$mailer_log = MailerLog::getMailerLog(); $mailer_log = MailerLog::getMailerLog();
@ -27,9 +27,9 @@ class MailerTest extends \MailPoetTest {
function testItRunsAuhtorizedEmailsCheckIfErrorIsPresent() { function testItRunsAuhtorizedEmailsCheckIfErrorIsPresent() {
$settings = new SettingsController(); $settings = new SettingsController();
$settings->set(Bridge::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING_NAME, ['invalid_sender_address' => 'a@b.c']); $settings->set(AuthorizedEmailsController::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING, ['invalid_sender_address' => 'a@b.c']);
$bridge = $this->makeEmpty(Bridge::class, ['checkAuthorizedEmailAddresses' => Expected::once()]); $authorized_emails_controller = $this->makeEmpty(AuthorizedEmailsController::class, ['checkAuthorizedEmailAddresses' => Expected::once()]);
$mailer_endpoint = new Mailer($bridge, $settings); $mailer_endpoint = new Mailer($authorized_emails_controller, $settings);
$mailer_endpoint->resumeSending(); $mailer_endpoint->resumeSending();
} }
} }

View File

@ -2,12 +2,15 @@
namespace MailPoet\Test\API\JSON\v1; namespace MailPoet\Test\API\JSON\v1;
use Carbon\Carbon; use Carbon\Carbon;
use Codeception\Stub\Expected;
use MailPoet\API\JSON\Response as APIResponse; use MailPoet\API\JSON\Response as APIResponse;
use MailPoet\API\JSON\Error as APIError; use MailPoet\API\JSON\Error as APIError;
use MailPoet\API\JSON\v1\Settings; use MailPoet\API\JSON\v1\Settings;
use MailPoet\Cron\Workers\InactiveSubscribers; use MailPoet\Cron\Workers\InactiveSubscribers;
use MailPoet\Models\ScheduledTask; use MailPoet\Models\ScheduledTask;
use MailPoet\Models\Setting; use MailPoet\Models\Setting;
use MailPoet\Services\AuthorizedEmailsController;
use MailPoet\Services\Bridge;
use MailPoet\Settings\SettingsController; use MailPoet\Settings\SettingsController;
class SettingsTest extends \MailPoetTest { class SettingsTest extends \MailPoetTest {
@ -23,7 +26,11 @@ class SettingsTest extends \MailPoetTest {
\ORM::raw_execute('TRUNCATE ' . ScheduledTask::$_table); \ORM::raw_execute('TRUNCATE ' . ScheduledTask::$_table);
$this->settings = new SettingsController(); $this->settings = new SettingsController();
$this->settings->set('some.setting.key', true); $this->settings->set('some.setting.key', true);
$this->endpoint = new Settings($this->settings); $this->endpoint = new Settings(
$this->settings,
new Bridge,
$this->make(AuthorizedEmailsController::class, ['onSettingsSave' => true ])
);
} }
function testItCanGetSettings() { function testItCanGetSettings() {
@ -50,6 +57,12 @@ class SettingsTest extends \MailPoetTest {
], ],
]; ];
$this->endpoint = new Settings(
$this->settings,
$this->make(Bridge::class, ['onSettingsSave' => Expected::once()]),
$this->make(AuthorizedEmailsController::class, ['onSettingsSave' => Expected::once()])
);
$response = $this->endpoint->set(/* missing data */); $response = $this->endpoint->set(/* missing data */);
expect($response->errors[0]['error'])->equals(APIError::BAD_REQUEST); expect($response->errors[0]['error'])->equals(APIError::BAD_REQUEST);
expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST); expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST);

View File

@ -4,7 +4,7 @@ namespace MailPoet\Test\Cron\Workers;
use Codeception\Stub; use Codeception\Stub;
use MailPoet\Cron\Workers\AuthorizedSendingEmailsCheck; use MailPoet\Cron\Workers\AuthorizedSendingEmailsCheck;
use MailPoet\Models\ScheduledTask; use MailPoet\Models\ScheduledTask;
use MailPoet\Services\Bridge; use MailPoet\Services\AuthorizedEmailsController;
use MailPoet\Settings\SettingsController; use MailPoet\Settings\SettingsController;
class AuthorizedSendingEmailsCheckTest extends \MailPoetTest { class AuthorizedSendingEmailsCheckTest extends \MailPoetTest {
@ -19,7 +19,7 @@ class AuthorizedSendingEmailsCheckTest extends \MailPoetTest {
} }
function testItRunsCheckOnBridge() { function testItRunsCheckOnBridge() {
$bridge_mock = $this->makeEmpty(Bridge::class, ['checkAuthorizedEmailAddresses' => Stub\Expected::once()]); $bridge_mock = $this->makeEmpty(AuthorizedEmailsController::class, ['checkAuthorizedEmailAddresses' => Stub\Expected::once()]);
$worker = new AuthorizedSendingEmailsCheck($bridge_mock); $worker = new AuthorizedSendingEmailsCheck($bridge_mock);
$worker->processTaskStrategy(ScheduledTask::createOrUpdate([])); $worker->processTaskStrategy(ScheduledTask::createOrUpdate([]));
} }
@ -27,7 +27,7 @@ class AuthorizedSendingEmailsCheckTest extends \MailPoetTest {
function testItDoesNotScheduleAutomatically() { function testItDoesNotScheduleAutomatically() {
$this->settings->set('mta_group', 'mailpoet'); $this->settings->set('mta_group', 'mailpoet');
$this->settings->set('mta.method', 'MailPoet'); $this->settings->set('mta.method', 'MailPoet');
$bridge_mock = $this->makeEmpty(Bridge::class, ['checkAuthorizedEmailAddresses' => Stub\Expected::never()]); $bridge_mock = $this->makeEmpty(AuthorizedEmailsController::class, ['checkAuthorizedEmailAddresses' => Stub\Expected::never()]);
$worker = new AuthorizedSendingEmailsCheck($bridge_mock); $worker = new AuthorizedSendingEmailsCheck($bridge_mock);
$worker->process(); $worker->process();

View File

@ -7,7 +7,7 @@ use MailPoet\Config\ServicesChecker;
use MailPoet\Mailer\MailerError; use MailPoet\Mailer\MailerError;
use MailPoet\Mailer\Methods\ErrorMappers\MailPoetMapper; use MailPoet\Mailer\Methods\ErrorMappers\MailPoetMapper;
use MailPoet\Mailer\Methods\MailPoet; use MailPoet\Mailer\Methods\MailPoet;
use MailPoet\Services\Bridge; use MailPoet\Services\AuthorizedEmailsController;
use MailPoet\Services\Bridge\API; use MailPoet\Services\Bridge\API;
class MailPoetAPITest extends \MailPoetTest { class MailPoetAPITest extends \MailPoetTest {
@ -34,7 +34,7 @@ class MailPoetAPITest extends \MailPoetTest {
$this->sender, $this->sender,
$this->reply_to, $this->reply_to,
new MailPoetMapper(), new MailPoetMapper(),
$this->makeEmpty(Bridge::class) $this->makeEmpty(AuthorizedEmailsController::class)
); );
$this->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>'; $this->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>';
$this->newsletter = [ $this->newsletter = [
@ -242,7 +242,7 @@ class MailPoetAPITest extends \MailPoetTest {
$this->sender, $this->sender,
$this->reply_to, $this->reply_to,
new MailPoetMapper(), new MailPoetMapper(),
$this->makeEmpty(Bridge::class, ['checkAuthorizedEmailAddresses' => Expected::once()]) $this->makeEmpty(AuthorizedEmailsController::class, ['checkAuthorizedEmailAddresses' => Expected::once()])
); );
$mailer->api = $this->makeEmpty( $mailer->api = $this->makeEmpty(
API::class, API::class,

View File

@ -0,0 +1,102 @@
<?php
namespace MailPoet\Test\Services;
use Carbon\Carbon;
use Codeception\Stub\Expected;
use MailPoet\Mailer\Mailer;
use MailPoet\Models\Setting;
use MailPoet\Services\AuthorizedEmailsController;
use MailPoet\Services\Bridge;
use MailPoet\Settings\SettingsController;
class AuthorizedEmailsControllerTest extends \MailPoetTest {
/** @var SettingsController */
private $settings;
function _before() {
parent::_before();
$this->settings = new SettingsController();
}
function testItResetsAuthorisedEmailsErrorIfMssIsNotActive() {
$this->settings->set('installed_at', new Carbon());
$this->settings->set(AuthorizedEmailsController::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING, 'Error');
$controller = $this->getController($authorized_emails_from_api = null);
$controller->checkAuthorizedEmailAddresses();
expect($this->settings->get(AuthorizedEmailsController::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING))->null();
}
function testItResetsAuthorisedEmailsErrorIfIntalationDateIsOlderThanAuthEmailsFeature() {
$this->settings->set('installed_at', '2018-03-04');
$this->setMailPoetSendingMethod();
$controller = $this->getController($authorized_emails_from_api = null);
$controller->checkAuthorizedEmailAddresses();
expect($this->settings->get(AuthorizedEmailsController::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING))->null();
}
function testItSetProperErrorForInvalidDefaultSender() {
$this->settings->set('installed_at', new Carbon());
$this->settings->set('sender.address', 'invalid@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();
expect($this->settings->get(AuthorizedEmailsController::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING))->equals(['invalid_sender_address' => 'invalid@email.com']);
}
function testItSetProperErrorForInvalidConfirmationSender() {
$this->settings->set('installed_at', new Carbon());
$this->settings->set('sender.address', 'auth@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();
expect($this->settings->get(AuthorizedEmailsController::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING))->equals(['invalid_confirmation_address' => 'invalid@email.com']);
}
function testItSetProperErrorForConfirmationAddressAndDefaultSender() {
$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();
expect($this->settings->get(AuthorizedEmailsController::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING))->equals(['invalid_sender_address' => 'invalid@email.com', 'invalid_confirmation_address' => 'invalid@email.com']);
}
function testItSetEmptyErrorWhenBothAdressesAreCorrect() {
$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();
expect($this->settings->get(AuthorizedEmailsController::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING))->null();
}
private function setMailPoetSendingMethod() {
$this->settings->set(
Mailer::MAILER_CONFIG_SETTING_NAME,
[
'method' => 'MailPoet',
'mailpoet_api_key' => 'some_key',
]
);
}
private function getController($authorized_emails) {
if ($authorized_emails === null) {
$get_emails_expectaton = Expected::never();
} else {
$get_emails_expectaton = Expected::once($authorized_emails);
}
$bridge_mock = $this->make(Bridge::class, ['getAuthorizedEmailAddresses' => $get_emails_expectaton]);
return new AuthorizedEmailsController($this->settings, $bridge_mock);
}
function _after() {
\ORM::raw_execute('TRUNCATE ' . Setting::$_table);
}
}

View File

@ -2,8 +2,6 @@
namespace MailPoet\Test\Services; namespace MailPoet\Test\Services;
use Carbon\Carbon;
use Codeception\Stub\Expected;
use Codeception\Util\Stub; use Codeception\Util\Stub;
use MailPoet\Mailer\Mailer; use MailPoet\Mailer\Mailer;
use MailPoet\Models\Setting; use MailPoet\Models\Setting;
@ -291,68 +289,6 @@ class BridgeTest extends \MailPoetTest {
$wp->removeFilter('mailpoet_bridge_api_request_timeout', $filter); $wp->removeFilter('mailpoet_bridge_api_request_timeout', $filter);
} }
function testItResetsAuthorisedEmailsErrorIfMssIsNotActive() {
$this->settings->set('installed_at', new Carbon());
$this->settings->set(Bridge::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING_NAME, 'Error');
$api = $this->make(API::class, ['getAuthorizedEmailAddresses' => Expected::never()]);
$this->bridge->api = $api;
$this->bridge->checkAuthorizedEmailAddresses();
expect($this->settings->get(Bridge::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING_NAME))->null();
}
function testItResetsAuthorisedEmailsErrorIfIntalationDateIsOlderThanAuthEmailsFeature() {
$this->settings->set('installed_at', '2018-03-04');
$this->setMailPoetSendingMethod();
$api = $this->make(API::class, ['getAuthorizedEmailAddresses' => Expected::never()]);
$this->bridge->api = $api;
$this->bridge->checkAuthorizedEmailAddresses();
expect($this->settings->get(Bridge::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING_NAME))->null();
}
function testItSetProperErrorForInvalidDefaultSender() {
$this->settings->set('installed_at', new Carbon());
$this->settings->set('sender.address', 'invalid@email.com');
$this->settings->set('signup_confirmation.from.address', 'auth@email.com');
$this->setMailPoetSendingMethod();
$api = $this->make(API::class, ['getAuthorizedEmailAddresses' => Expected::once(['auth@email.com'])]);
$this->bridge->api = $api;
$this->bridge->checkAuthorizedEmailAddresses();
expect($this->settings->get(Bridge::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING_NAME))->equals(['invalid_sender_address' => 'invalid@email.com']);
}
function testItSetProperErrorForInvalidConfirmationSender() {
$this->settings->set('installed_at', new Carbon());
$this->settings->set('sender.address', 'auth@email.com');
$this->settings->set('signup_confirmation.from.address', 'invalid@email.com');
$this->setMailPoetSendingMethod();
$api = $this->make(API::class, ['getAuthorizedEmailAddresses' => Expected::once(['auth@email.com'])]);
$this->bridge->api = $api;
$this->bridge->checkAuthorizedEmailAddresses();
expect($this->settings->get(Bridge::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING_NAME))->equals(['invalid_confirmation_address' => 'invalid@email.com']);
}
function testItSetProperErrorForConfirmationAddressAndDefaultSender() {
$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();
$api = $this->make(API::class, ['getAuthorizedEmailAddresses' => Expected::once(['auth@email.com'])]);
$this->bridge->api = $api;
$this->bridge->checkAuthorizedEmailAddresses();
expect($this->settings->get(Bridge::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING_NAME))->equals(['invalid_sender_address' => 'invalid@email.com', 'invalid_confirmation_address' => 'invalid@email.com']);
}
function testItSetEmptyErrorWhenBothAdressesAreCorrect() {
$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();
$api = $this->make(API::class, ['getAuthorizedEmailAddresses' => Expected::once(['auth@email.com'])]);
$this->bridge->api = $api;
$this->bridge->checkAuthorizedEmailAddresses();
expect($this->settings->get(Bridge::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING_NAME))->null();
}
private function setMailPoetSendingMethod() { private function setMailPoetSendingMethod() {
$this->settings->set( $this->settings->set(
Mailer::MAILER_CONFIG_SETTING_NAME, Mailer::MAILER_CONFIG_SETTING_NAME,

View File

@ -8,6 +8,7 @@ use MailPoet\Mailer\Mailer;
use MailPoet\Models\Segment; use MailPoet\Models\Segment;
use MailPoet\Models\Subscriber; use MailPoet\Models\Subscriber;
use MailPoet\Models\SubscriberSegment; use MailPoet\Models\SubscriberSegment;
use MailPoet\Services\AuthorizedEmailsController;
use MailPoet\Services\Bridge; use MailPoet\Services\Bridge;
use MailPoet\Settings\SettingsController; use MailPoet\Settings\SettingsController;
use MailPoet\WP\Functions as WPFunctions; use MailPoet\WP\Functions as WPFunctions;
@ -84,13 +85,13 @@ class ConfirmationEmailMailerTest extends \MailPoetTest {
]); ]);
$settings = new SettingsController; $settings = new SettingsController;
$settings->set(Bridge::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING_NAME, ['invalid_confirmation_address' => 'email@email.com']); $settings->set(AuthorizedEmailsController::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING, ['invalid_confirmation_address' => 'email@email.com']);
$settings->set(Mailer::MAILER_CONFIG_SETTING_NAME, ['method' => Mailer::METHOD_MAILPOET]); $settings->set(Mailer::MAILER_CONFIG_SETTING_NAME, ['method' => Mailer::METHOD_MAILPOET]);
$sender = new ConfirmationEmailMailer($mailer); $sender = new ConfirmationEmailMailer($mailer);
$result = $sender->sendConfirmationEmail($subscriber); $result = $sender->sendConfirmationEmail($subscriber);
expect($result)->equals(false); expect($result)->equals(false);
$settings->set(Bridge::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING_NAME, null); $settings->set(AuthorizedEmailsController::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING, null);
} }
function testItLimitsNumberOfConfirmationEmailsForNotLoggedInUser() { function testItLimitsNumberOfConfirmationEmailsForNotLoggedInUser() {