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

@ -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);
}
}