Improve error reporting in API
[MAILPOET-1290]
This commit is contained in:
@ -7,6 +7,7 @@ use MailPoet\Models\Subscriber;
|
|||||||
use MailPoet\Models\SubscriberSegment;
|
use MailPoet\Models\SubscriberSegment;
|
||||||
use MailPoet\Newsletter\Scheduler\Scheduler;
|
use MailPoet\Newsletter\Scheduler\Scheduler;
|
||||||
use MailPoet\Subscribers\Source;
|
use MailPoet\Subscribers\Source;
|
||||||
|
use MailPoet\Tasks\Sending;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
@ -186,7 +187,12 @@ class API {
|
|||||||
|
|
||||||
// send confirmation email
|
// send confirmation email
|
||||||
if($send_confirmation_email && $new_subscriber->status === Subscriber::STATUS_UNCONFIRMED) {
|
if($send_confirmation_email && $new_subscriber->status === Subscriber::STATUS_UNCONFIRMED) {
|
||||||
$this->_sendConfirmationEmail($new_subscriber);
|
$result = $this->_sendConfirmationEmail($new_subscriber);
|
||||||
|
if(!$result && $new_subscriber->getErrors()) {
|
||||||
|
throw new \Exception(
|
||||||
|
__(sprintf('Subscriber added, but confirmation email failed to send: %s', strtolower(implode(', ', $new_subscriber->getErrors()))), 'mailpoet')
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// schedule welcome email(s)
|
// schedule welcome email(s)
|
||||||
@ -242,6 +248,16 @@ class API {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected function _scheduleWelcomeNotification(Subscriber $subscriber, array $segments) {
|
protected function _scheduleWelcomeNotification(Subscriber $subscriber, array $segments) {
|
||||||
return Scheduler::scheduleSubscriberWelcomeNotification($subscriber->id, $segments);
|
$result = Scheduler::scheduleSubscriberWelcomeNotification($subscriber->id, $segments);
|
||||||
|
if(is_array($result)) {
|
||||||
|
foreach($result as $queue) {
|
||||||
|
if($queue instanceof Sending && $queue->getErrors()) {
|
||||||
|
throw new \Exception(
|
||||||
|
__(sprintf('Subscriber added, but welcome email failed to send: %s', strtolower(implode(', ', $queue->getErrors()))), 'mailpoet')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,13 +38,15 @@ class Scheduler {
|
|||||||
static function scheduleSubscriberWelcomeNotification($subscriber_id, $segments) {
|
static function scheduleSubscriberWelcomeNotification($subscriber_id, $segments) {
|
||||||
$newsletters = self::getNewsletters(Newsletter::TYPE_WELCOME);
|
$newsletters = self::getNewsletters(Newsletter::TYPE_WELCOME);
|
||||||
if(empty($newsletters)) return false;
|
if(empty($newsletters)) return false;
|
||||||
|
$result = array();
|
||||||
foreach($newsletters as $newsletter) {
|
foreach($newsletters as $newsletter) {
|
||||||
if($newsletter->event === 'segment' &&
|
if($newsletter->event === 'segment' &&
|
||||||
in_array($newsletter->segment, $segments)
|
in_array($newsletter->segment, $segments)
|
||||||
) {
|
) {
|
||||||
self::createWelcomeNotificationSendingTask($newsletter, $subscriber_id);
|
$result[] = self::createWelcomeNotificationSendingTask($newsletter, $subscriber_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static function scheduleAutomaticEmail($group, $event, $scheduling_condition = false, $subscriber_id = false, $meta = false) {
|
static function scheduleAutomaticEmail($group, $event, $scheduling_condition = false, $subscriber_id = false, $meta = false) {
|
||||||
|
@ -2,13 +2,16 @@
|
|||||||
|
|
||||||
namespace MailPoet\Test\API\MP;
|
namespace MailPoet\Test\API\MP;
|
||||||
|
|
||||||
|
use AspectMock\Test as Mock;
|
||||||
use Codeception\Util\Fixtures;
|
use Codeception\Util\Fixtures;
|
||||||
use Codeception\Util\Stub;
|
use Codeception\Util\Stub;
|
||||||
use MailPoet\API\API;
|
use MailPoet\API\API;
|
||||||
use MailPoet\Models\CustomField;
|
use MailPoet\Models\CustomField;
|
||||||
|
use MailPoet\Models\ScheduledTask;
|
||||||
use MailPoet\Models\Segment;
|
use MailPoet\Models\Segment;
|
||||||
use MailPoet\Models\SendingQueue;
|
use MailPoet\Models\SendingQueue;
|
||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Models\Subscriber;
|
||||||
|
use MailPoet\Tasks\Sending;
|
||||||
|
|
||||||
class APITest extends \MailPoetTest {
|
class APITest extends \MailPoetTest {
|
||||||
const VERSION = 'v1';
|
const VERSION = 'v1';
|
||||||
@ -298,6 +301,12 @@ class APITest extends \MailPoetTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function testItSubscribesToSegmentsWhenAddingSubscriber() {
|
function testItSubscribesToSegmentsWhenAddingSubscriber() {
|
||||||
|
$API = Stub::makeEmptyExcept(
|
||||||
|
new \MailPoet\API\MP\v1\API(),
|
||||||
|
'addSubscriber',
|
||||||
|
array(
|
||||||
|
'_sendConfirmationEmail' => Stub::once()
|
||||||
|
), $this);
|
||||||
$segment = Segment::createOrUpdate(
|
$segment = Segment::createOrUpdate(
|
||||||
array(
|
array(
|
||||||
'name' => 'Default',
|
'name' => 'Default',
|
||||||
@ -308,7 +317,7 @@ class APITest extends \MailPoetTest {
|
|||||||
'email' => 'test@example.com'
|
'email' => 'test@example.com'
|
||||||
);
|
);
|
||||||
|
|
||||||
$result = API::MP(self::VERSION)->addSubscriber($subscriber, array($segment->id));
|
$result = $API->addSubscriber($subscriber, array($segment->id));
|
||||||
expect($result['id'])->greaterThan(0);
|
expect($result['id'])->greaterThan(0);
|
||||||
expect($result['email'])->equals($subscriber['email']);
|
expect($result['email'])->equals($subscriber['email']);
|
||||||
expect($result['subscriptions'][0]['segment_id'])->equals($segment->id);
|
expect($result['subscriptions'][0]['segment_id'])->equals($segment->id);
|
||||||
@ -329,6 +338,30 @@ class APITest extends \MailPoetTest {
|
|||||||
$API->addSubscriber($subscriber, $segments);
|
$API->addSubscriber($subscriber, $segments);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testItThrowsIfWelcomeEmailFails() {
|
||||||
|
$task = ScheduledTask::create();
|
||||||
|
$task->type = 'sending';
|
||||||
|
$task->setError("Big Error");
|
||||||
|
$sendingStub = Sending::create($task, SendingQueue::create());
|
||||||
|
Mock::double('MailPoet\Newsletter\Scheduler\Scheduler', array(
|
||||||
|
'scheduleSubscriberWelcomeNotification' => array($sendingStub),
|
||||||
|
));
|
||||||
|
$segment = Segment::createOrUpdate(
|
||||||
|
array(
|
||||||
|
'name' => 'Default',
|
||||||
|
'type' => Segment::TYPE_DEFAULT
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$API = new \MailPoet\API\MP\v1\API();
|
||||||
|
$subscriber = array(
|
||||||
|
'email' => 'test@example.com',
|
||||||
|
'status' => Subscriber::STATUS_SUBSCRIBED
|
||||||
|
);
|
||||||
|
$segments = array($segment->id());
|
||||||
|
$this->setExpectedException('\Exception');
|
||||||
|
$API->addSubscriber($subscriber, $segments, array('schedule_welcome_email' => true, 'send_confirmation_email' => false));
|
||||||
|
}
|
||||||
|
|
||||||
function testItDoesNotScheduleWelcomeNotificationAfterAddingSubscriberIfStatusIsNotSubscribed() {
|
function testItDoesNotScheduleWelcomeNotificationAfterAddingSubscriberIfStatusIsNotSubscribed() {
|
||||||
$API = Stub::makeEmptyExcept(
|
$API = Stub::makeEmptyExcept(
|
||||||
new \MailPoet\API\MP\v1\API(),
|
new \MailPoet\API\MP\v1\API(),
|
||||||
@ -373,6 +406,29 @@ class APITest extends \MailPoetTest {
|
|||||||
$API->addSubscriber($subscriber, $segments);
|
$API->addSubscriber($subscriber, $segments);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testItThrowsWhenConfirmationEmailFailsToSend() {
|
||||||
|
$API = Stub::makeEmptyExcept(
|
||||||
|
new \MailPoet\API\MP\v1\API(),
|
||||||
|
'addSubscriber',
|
||||||
|
array(
|
||||||
|
'_sendConfirmationEmail' => function($subscriber) {
|
||||||
|
$subscriber->setError('Big Error');
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
), $this);
|
||||||
|
$segment = Segment::createOrUpdate(
|
||||||
|
array(
|
||||||
|
'name' => 'Default',
|
||||||
|
'type' => Segment::TYPE_DEFAULT
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$subscriber = array(
|
||||||
|
'email' => 'test@example.com'
|
||||||
|
);
|
||||||
|
$this->setExpectedException('\Exception', 'Subscriber added, but confirmation email failed to send: big error');
|
||||||
|
$API->addSubscriber($subscriber, array($segment->id), array('send_confirmation_email' => true));
|
||||||
|
}
|
||||||
|
|
||||||
function testItDoesNotSendConfirmationEmailAfterAddingSubscriberWhenOptionIsSet() {
|
function testItDoesNotSendConfirmationEmailAfterAddingSubscriberWhenOptionIsSet() {
|
||||||
$API = Stub::makeEmptyExcept(
|
$API = Stub::makeEmptyExcept(
|
||||||
new \MailPoet\API\MP\v1\API(),
|
new \MailPoet\API\MP\v1\API(),
|
||||||
@ -562,6 +618,7 @@ class APITest extends \MailPoetTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function _after() {
|
function _after() {
|
||||||
|
Mock::clean();
|
||||||
\ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
\ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
||||||
\ORM::raw_execute('TRUNCATE ' . CustomField::$_table);
|
\ORM::raw_execute('TRUNCATE ' . CustomField::$_table);
|
||||||
\ORM::raw_execute('TRUNCATE ' . Segment::$_table);
|
\ORM::raw_execute('TRUNCATE ' . Segment::$_table);
|
||||||
|
@ -250,7 +250,7 @@ class SchedulerTest extends \MailPoetTest {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// queue is created and scheduled for delivery one day later
|
// queue is created and scheduled for delivery one day later
|
||||||
Scheduler::scheduleSubscriberWelcomeNotification(
|
$result = Scheduler::scheduleSubscriberWelcomeNotification(
|
||||||
$subscriber_id = 10,
|
$subscriber_id = 10,
|
||||||
$segments = array(
|
$segments = array(
|
||||||
3,
|
3,
|
||||||
@ -263,6 +263,7 @@ class SchedulerTest extends \MailPoetTest {
|
|||||||
->findOne();
|
->findOne();
|
||||||
expect(Carbon::parse($queue->scheduled_at)->format('Y-m-d H:i'))
|
expect(Carbon::parse($queue->scheduled_at)->format('Y-m-d H:i'))
|
||||||
->equals($current_time->addDay()->format('Y-m-d H:i'));
|
->equals($current_time->addDay()->format('Y-m-d H:i'));
|
||||||
|
expect($result[0]->id())->equals($queue->id());
|
||||||
}
|
}
|
||||||
|
|
||||||
function itDoesNotScheduleAnythingWhenNewsletterDoesNotExist() {
|
function itDoesNotScheduleAnythingWhenNewsletterDoesNotExist() {
|
||||||
|
Reference in New Issue
Block a user