Move Mailer error tests to unit [MAILPOET-2009]

This commit is contained in:
wxa
2019-04-25 11:10:29 +03:00
committed by M. Shull
parent f2e0dc7d2f
commit 994fae79d2
14 changed files with 59 additions and 24 deletions

View File

@ -8,8 +8,6 @@ use MailPoet\Mailer\Methods\ErrorMappers\SendGridMapper;
use MailPoet\Mailer\Methods\ErrorMappers\SMTPMapper;
use MailPoet\Settings\SettingsController;
if (!defined('ABSPATH')) exit;
class Mailer {
public $mailer_config;
public $sender;

View File

@ -3,8 +3,6 @@ namespace MailPoet\Mailer;
use MailPoet\Settings\SettingsController;
if (!defined('ABSPATH')) exit;
class MailerLog {
const SETTING_NAME = 'mta_log';
const STATUS_PAUSED = 'paused';

View File

@ -4,6 +4,7 @@ namespace MailPoet\Mailer\Methods\ErrorMappers;
use MailPoet\Mailer\MailerError;
use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\SubscriberError;
use MailPoet\WP\Functions as WPFunctions;
class AmazonSESMapper {
use ConnectionErrorMapperTrait;
@ -24,7 +25,7 @@ class AmazonSESMapper {
function getErrorFromResponse($response, $subscriber) {
$message = ($response) ?
$response->Error->Message->__toString() :
sprintf(__('%s has returned an unknown error.', 'mailpoet'), Mailer::METHOD_AMAZONSES);
sprintf(WPFunctions::get()->__('%s has returned an unknown error.', 'mailpoet'), Mailer::METHOD_AMAZONSES);
$level = MailerError::LEVEL_HARD;
if ($response && $response->Error->Code->__toString() === 'MessageRejected') {

View File

@ -92,20 +92,20 @@ class MailPoetMapper {
private function getUnauthorizedEmailMessage($sender, $newsletter) {
$email = $sender ? $sender['from_email'] : null;
if ($email && (new FreeDomains())->isEmailOnFreeDomain($email)) {
$message = '<p>' . sprintf(__('The MailPoet Sending Service cant send email with the email address <i>%s</i>. You need to use an address like <i>you@yourdomain.com</i>.', 'mailpoet'), $email) . '</p>';
$message = '<p>' . sprintf(WPFunctions::get()->__('The MailPoet Sending Service cant send email with the email address <i>%s</i>. You need to use an address like <i>you@yourdomain.com</i>.', 'mailpoet'), $email) . '</p>';
$message .= '<p>';
if ($newsletter && $newsletter['id']) {
$message .= '<a class="button button-primary" href="admin.php?page=mailpoet-newsletters#/send/' . $newsletter['id'] .'">';
} else {
$message .= '<a class="button button-primary" href="admin.php?page=mailpoet-settings">';
}
$message .= __('Change my email address', 'mailpoet');
$message .= WPFunctions::get()->__('Change my email address', 'mailpoet');
$message .= '</a>';
} else {
$message = sprintf(__('<p>The MailPoet Sending Service did not send your latest email because the address <i>%s</i> is not yet authorized.</p>', 'mailpoet'), $email ?: __('Unknown address'));
$message = sprintf(WPFunctions::get()->__('<p>The MailPoet Sending Service did not send your latest email because the address <i>%s</i> is not yet authorized.</p>', 'mailpoet'), $email ?: WPFunctions::get()->__('Unknown address'));
$message .= '<p>';
$message .= Helpers::replaceLinkTags(
__('[link]Authorize your email in your account now.[/link]', 'mailpoet'),
WPFunctions::get()->__('[link]Authorize your email in your account now.[/link]', 'mailpoet'),
'https://account.mailpoet.com/authorization',
array(
'class' => 'button button-primary',
@ -114,15 +114,15 @@ class MailPoetMapper {
)
);
}
$message .= ' &nbsp; <button class="button js-button-resume-sending">' . __('Resume sending', 'mailpoet') . '</button>';
$message .= ' &nbsp; <button class="button js-button-resume-sending">' . WPFunctions::get()->__('Resume sending', 'mailpoet') . '</button>';
$message .= '</p>';
$message .= "<script>jQuery('.js-button-resume-sending').on('click', function() { MailPoet.Ajax.post({ api_version: window.mailpoet_api_version, endpoint: 'mailer', action: 'resumeSending' }).done(function() { jQuery('.js-error-unauthorized-email').slideUp(); MailPoet.Notice.success('" . __('Sending has been resumed.') . "'); if (window.mailpoet_listing) { window.mailpoet_listing.forceUpdate(); }}).fail(function(response) { if (response.errors.length > 0) { MailPoet.Notice.error(response.errors.map(function(error) { return error.message }), { scroll: true }); }}); })</script>";
$message .= "<script>jQuery('.js-button-resume-sending').on('click', function() { MailPoet.Ajax.post({ api_version: window.mailpoet_api_version, endpoint: 'mailer', action: 'resumeSending' }).done(function() { jQuery('.js-error-unauthorized-email').slideUp(); MailPoet.Notice.success('" . WPFunctions::get()->__('Sending has been resumed.') . "'); if (window.mailpoet_listing) { window.mailpoet_listing.forceUpdate(); }}).fail(function(response) { if (response.errors.length > 0) { MailPoet.Notice.error(response.errors.map(function(error) { return error.message }), { scroll: true }); }}); })</script>";
return $message;
}
private function getAccountBannedMessage() {
return Helpers::replaceLinkTags(
__('You currently are not permitted to send any emails with MailPoet Sending Service, which may have happened due to poor deliverability. Please [link]contact our support team[/link] to resolve the issue.', 'mailpoet'),
WPFunctions::get()->__('You currently are not permitted to send any emails with MailPoet Sending Service, which may have happened due to poor deliverability. Please [link]contact our support team[/link] to resolve the issue.', 'mailpoet'),
'https://www.mailpoet.com/support/',
array(
'target' => '_blank',

View File

@ -4,6 +4,7 @@ namespace MailPoet\Mailer\Methods\ErrorMappers;
use MailPoet\Mailer\MailerError;
use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\SubscriberError;
use MailPoet\WP\Functions as WPFunctions;
class PHPMailMapper {
use ConnectionErrorMapperTrait;
@ -19,7 +20,7 @@ class PHPMailMapper {
}
function getErrorForSubscriber($subscriber) {
$message = sprintf(__('%s has returned an unknown error.', 'mailpoet'), Mailer::METHOD_PHPMAIL);
$message = sprintf(WPFunctions::get()->__('%s has returned an unknown error.', 'mailpoet'), Mailer::METHOD_PHPMAIL);
$subscriber_errors = [new SubscriberError($subscriber, null)];
return new MailerError(MailerError::OPERATION_SEND, MailerError::LEVEL_HARD, $message, null, $subscriber_errors);
}

View File

@ -4,6 +4,7 @@ namespace MailPoet\Mailer\Methods\ErrorMappers;
use MailPoet\Mailer\MailerError;
use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\SubscriberError;
use MailPoet\WP\Functions as WPFunctions;
class SMTPMapper {
use ConnectionErrorMapperTrait;
@ -32,7 +33,7 @@ class SMTPMapper {
// remove line breaks from the message due to how logger's dump() method works
$message = preg_replace('/\r|\n/', '', $message);
} else {
$message = sprintf(__('%s has returned an unknown error.', 'mailpoet'), Mailer::METHOD_SMTP);
$message = sprintf(WPFunctions::get()->__('%s has returned an unknown error.', 'mailpoet'), Mailer::METHOD_SMTP);
}
$subscriber_errors = [new SubscriberError($subscriber, null)];
return new MailerError(MailerError::OPERATION_SEND, MailerError::LEVEL_HARD, $message, null, $subscriber_errors);

View File

@ -4,6 +4,7 @@ namespace MailPoet\Mailer\Methods\ErrorMappers;
use MailPoet\Mailer\MailerError;
use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\SubscriberError;
use MailPoet\WP\Functions as WPFunctions;
class SendGridMapper {
use ConnectionErrorMapperTrait;
@ -11,7 +12,7 @@ class SendGridMapper {
function getErrorFromResponse($response, $subscriber) {
$response = (!empty($response['errors'][0])) ?
$response['errors'][0] :
sprintf(__('%s has returned an unknown error.', 'mailpoet'), Mailer::METHOD_SENDGRID);
sprintf(WPFunctions::get()->__('%s has returned an unknown error.', 'mailpoet'), Mailer::METHOD_SENDGRID);
$level = MailerError::LEVEL_HARD;
if (strpos($response, 'Invalid email address') === 0) {

View File

@ -3,8 +3,6 @@
namespace MailPoet\Services\Bridge;
use MailPoet\WP\Functions as WPFunctions;
if (!defined('ABSPATH')) exit;
class API {
const SENDING_STATUS_OK = 'ok';
const SENDING_STATUS_CONNECTION_ERROR = 'connection_error';

View File

@ -1,10 +1,20 @@
<?php
namespace MailPoet\Test\Mailer;
use Codeception\Stub;
use MailPoet\Mailer\MailerError;
use MailPoet\Mailer\SubscriberError;
use MailPoet\WP\Functions as WPFunctions;
class MailerErrorTest extends \MailPoetTest {
class MailerErrorTest extends \MailPoetUnitTest {
function _before() {
WPFunctions::set(Stub::make(new WPFunctions, [
'__' => function ($value) {
return $value;
}
]));
}
function testItCanComposeErrorMessageWithoutSubscribers() {
$error = new MailerError(MailerError::OPERATION_SEND, MailerError::LEVEL_HARD, 'Some Message');
@ -37,4 +47,8 @@ class MailerErrorTest extends \MailPoetTest {
'Some Message Unprocessed subscribers: (email1@example.com: Subscriber 1 message), (email2@example.com)'
);
}
function _after() {
WPFunctions::set(new WPFunctions);
}
}

View File

@ -5,7 +5,7 @@ use MailPoet\Mailer\MailerError;
use MailPoet\Mailer\Methods\ErrorMappers\AmazonSESMapper;
use SimpleXMLElement;
class AmazonSESMapperTest extends \MailPoetTest {
class AmazonSESMapperTest extends \MailPoetUnitTest {
/** @var AmazonSESMapper*/
private $mapper;

View File

@ -1,4 +1,5 @@
<?php
__halt_compiler();
namespace MailPoet\Test\Mailer\Methods\ErrorMappers;
use MailPoet\Mailer\MailerError;
@ -6,7 +7,7 @@ use MailPoet\Mailer\Methods\ErrorMappers\MailPoetMapper;
use MailPoet\Services\Bridge\API;
use MailPoet\Util\Helpers;
class MailPoetMapperTest extends \MailPoetTest {
class MailPoetMapperTest extends \MailPoetUnitTest {
/** @var MailPoetMapper */
private $mapper;

View File

@ -1,10 +1,12 @@
<?php
namespace MailPoet\Test\Mailer\Methods\ErrorMappers;
use Codeception\Stub;
use MailPoet\Mailer\MailerError;
use MailPoet\Mailer\Methods\ErrorMappers\PHPMailMapper;
use MailPoet\WP\Functions as WPFunctions;
class PHPMailMapperTest extends \MailPoetTest {
class PHPMailMapperTest extends \MailPoetUnitTest {
/** @var PHPMailMapper*/
private $mapper;
@ -12,6 +14,11 @@ class PHPMailMapperTest extends \MailPoetTest {
function _before() {
parent::_before();
$this->mapper = new PHPMailMapper();
WPFunctions::set(Stub::make(new WPFunctions, [
'__' => function ($value) {
return $value;
}
]));
}
function testGetProperErrorForSubscriber() {
@ -32,4 +39,8 @@ class PHPMailMapperTest extends \MailPoetTest {
$error = $this->mapper->getErrorFromException(new \Exception('Invalid address. (Add ...'), 'john@rambo.com');
expect($error->getLevel())->equals(MailerError::LEVEL_SOFT);
}
function _after() {
WPFunctions::set(new WPFunctions);
}
}

View File

@ -1,11 +1,13 @@
<?php
namespace MailPoet\Test\Mailer\Methods\ErrorMappers;
use Codeception\Stub;
use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\MailerError;
use MailPoet\Mailer\Methods\ErrorMappers\SMTPMapper;
use MailPoet\WP\Functions as WPFunctions;
class SMTPMapperTest extends \MailPoetTest {
class SMTPMapperTest extends \MailPoetUnitTest {
/** @var SMTPMapper */
private $mapper;
@ -13,6 +15,11 @@ class SMTPMapperTest extends \MailPoetTest {
function _before() {
parent::_before();
$this->mapper = new SMTPMapper();
WPFunctions::set(Stub::make(new WPFunctions, [
'__' => function ($value) {
return $value;
}
]));
}
function testItCanProcessExceptionMessage() {
@ -55,4 +62,8 @@ class SMTPMapperTest extends \MailPoetTest {
->equals(Mailer::METHOD_SMTP . ' has returned an unknown error.');
expect($error->getSubscriberErrors()[0]->getEmail('moi@mrcasual.com'));
}
function _after() {
WPFunctions::set(new WPFunctions);
}
}

View File

@ -4,7 +4,7 @@ namespace MailPoet\Test\Mailer\Methods\ErrorMappers;
use MailPoet\Mailer\MailerError;
use MailPoet\Mailer\Methods\ErrorMappers\SendGridMapper;
class SendGridMapperTest extends \MailPoetTest {
class SendGridMapperTest extends \MailPoetUnitTest {
/** @var SendGridMapper*/
private $mapper;