|
|
@ -6,24 +6,34 @@ use AspectMock\Test as Mock;
|
|
|
|
use Codeception\Util\Fixtures;
|
|
|
|
use Codeception\Util\Fixtures;
|
|
|
|
use Codeception\Stub;
|
|
|
|
use Codeception\Stub;
|
|
|
|
use Codeception\Stub\Expected;
|
|
|
|
use Codeception\Stub\Expected;
|
|
|
|
use MailPoet\API\API;
|
|
|
|
|
|
|
|
use MailPoet\Models\CustomField;
|
|
|
|
use MailPoet\Models\CustomField;
|
|
|
|
use MailPoet\Models\ScheduledTask;
|
|
|
|
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\Subscribers\ConfirmationEmailMailer;
|
|
|
|
|
|
|
|
use MailPoet\Subscribers\NewSubscriberNotificationMailer;
|
|
|
|
|
|
|
|
use MailPoet\Subscribers\RequiredCustomFieldValidator;
|
|
|
|
use MailPoet\Tasks\Sending;
|
|
|
|
use MailPoet\Tasks\Sending;
|
|
|
|
|
|
|
|
|
|
|
|
class APITest extends \MailPoetTest {
|
|
|
|
class APITest extends \MailPoetTest {
|
|
|
|
const VERSION = 'v1';
|
|
|
|
const VERSION = 'v1';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private function getApi() {
|
|
|
|
|
|
|
|
return new \MailPoet\API\MP\v1\API(
|
|
|
|
|
|
|
|
Stub::makeEmpty(NewSubscriberNotificationMailer::class, ['send']),
|
|
|
|
|
|
|
|
Stub::makeEmpty(ConfirmationEmailMailer::class, ['sendConfirmationEmail']),
|
|
|
|
|
|
|
|
Stub::makeEmptyExcept(RequiredCustomFieldValidator::class, 'validate')
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function testItReturnsSubscriberFields() {
|
|
|
|
function testItReturnsSubscriberFields() {
|
|
|
|
$custom_field = CustomField::create();
|
|
|
|
$custom_field = CustomField::create();
|
|
|
|
$custom_field->name = 'test custom field';
|
|
|
|
$custom_field->name = 'test custom field';
|
|
|
|
$custom_field->type = CustomField::TYPE_TEXT;
|
|
|
|
$custom_field->type = CustomField::TYPE_TEXT;
|
|
|
|
$custom_field->save();
|
|
|
|
$custom_field->save();
|
|
|
|
|
|
|
|
|
|
|
|
$response = API::MP(self::VERSION)->getSubscriberFields();
|
|
|
|
$response = $this->getApi()->getSubscriberFields();
|
|
|
|
|
|
|
|
|
|
|
|
expect($response)->equals(
|
|
|
|
expect($response)->equals(
|
|
|
|
array(
|
|
|
|
array(
|
|
|
@ -49,7 +59,7 @@ class APITest extends \MailPoetTest {
|
|
|
|
|
|
|
|
|
|
|
|
function testItDoesNotSubscribeMissingSubscriberToLists() {
|
|
|
|
function testItDoesNotSubscribeMissingSubscriberToLists() {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
API::MP(self::VERSION)->subscribeToLists(false, array(1,2,3));
|
|
|
|
$this->getApi()->subscribeToLists(false, array(1,2,3));
|
|
|
|
$this->fail('Subscriber does not exist exception should have been thrown.');
|
|
|
|
$this->fail('Subscriber does not exist exception should have been thrown.');
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
expect($e->getMessage())->equals('This subscriber does not exist.');
|
|
|
|
expect($e->getMessage())->equals('This subscriber does not exist.');
|
|
|
@ -62,14 +72,14 @@ class APITest extends \MailPoetTest {
|
|
|
|
$subscriber->save();
|
|
|
|
$subscriber->save();
|
|
|
|
// multiple lists error message
|
|
|
|
// multiple lists error message
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
API::MP(self::VERSION)->subscribeToLists($subscriber->id, array(1,2,3));
|
|
|
|
$this->getApi()->subscribeToLists($subscriber->id, array(1,2,3));
|
|
|
|
$this->fail('Missing segments exception should have been thrown.');
|
|
|
|
$this->fail('Missing segments exception should have been thrown.');
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
expect($e->getMessage())->equals('These lists do not exist.');
|
|
|
|
expect($e->getMessage())->equals('These lists do not exist.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// single list error message
|
|
|
|
// single list error message
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
API::MP(self::VERSION)->subscribeToLists($subscriber->id, array(1));
|
|
|
|
$this->getApi()->subscribeToLists($subscriber->id, array(1));
|
|
|
|
$this->fail('Missing segments exception should have been thrown.');
|
|
|
|
$this->fail('Missing segments exception should have been thrown.');
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
expect($e->getMessage())->equals('This list does not exist.');
|
|
|
|
expect($e->getMessage())->equals('This list does not exist.');
|
|
|
@ -87,7 +97,7 @@ class APITest extends \MailPoetTest {
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
API::MP(self::VERSION)->subscribeToLists($subscriber->id, array($segment->id));
|
|
|
|
$this->getApi()->subscribeToLists($subscriber->id, array($segment->id));
|
|
|
|
$this->fail('WP Users segment exception should have been thrown.');
|
|
|
|
$this->fail('WP Users segment exception should have been thrown.');
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
expect($e->getMessage())->equals("Can't subscribe to a WordPress Users list with ID {$segment->id}.");
|
|
|
|
expect($e->getMessage())->equals("Can't subscribe to a WordPress Users list with ID {$segment->id}.");
|
|
|
@ -106,14 +116,14 @@ class APITest extends \MailPoetTest {
|
|
|
|
);
|
|
|
|
);
|
|
|
|
// multiple lists error message
|
|
|
|
// multiple lists error message
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
API::MP(self::VERSION)->subscribeToLists($subscriber->id, array($segment->id, 90, 100));
|
|
|
|
$this->getApi()->subscribeToLists($subscriber->id, array($segment->id, 90, 100));
|
|
|
|
$this->fail('Missing segments with IDs exception should have been thrown.');
|
|
|
|
$this->fail('Missing segments with IDs exception should have been thrown.');
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
expect($e->getMessage())->equals('Lists with IDs 90, 100 do not exist.');
|
|
|
|
expect($e->getMessage())->equals('Lists with IDs 90, 100 do not exist.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// single list error message
|
|
|
|
// single list error message
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
API::MP(self::VERSION)->subscribeToLists($subscriber->id, array($segment->id, 90));
|
|
|
|
$this->getApi()->subscribeToLists($subscriber->id, array($segment->id, 90));
|
|
|
|
$this->fail('Missing segments with IDs exception should have been thrown.');
|
|
|
|
$this->fail('Missing segments with IDs exception should have been thrown.');
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
expect($e->getMessage())->equals('List with ID 90 does not exist.');
|
|
|
|
expect($e->getMessage())->equals('List with ID 90 does not exist.');
|
|
|
@ -123,7 +133,7 @@ class APITest extends \MailPoetTest {
|
|
|
|
function testItUsesMultipleListsSubscribeMethodWhenSubscribingToSingleList() {
|
|
|
|
function testItUsesMultipleListsSubscribeMethodWhenSubscribingToSingleList() {
|
|
|
|
// subscribing to single list = converting list ID to an array and using
|
|
|
|
// subscribing to single list = converting list ID to an array and using
|
|
|
|
// multiple lists subscription method
|
|
|
|
// multiple lists subscription method
|
|
|
|
$API = Stub::make(new \MailPoet\API\MP\v1\API(), array(
|
|
|
|
$API = Stub::make($this->getApi(), array(
|
|
|
|
'subscribeToLists' => function() {
|
|
|
|
'subscribeToLists' => function() {
|
|
|
|
return func_get_args();
|
|
|
|
return func_get_args();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -152,13 +162,13 @@ class APITest extends \MailPoetTest {
|
|
|
|
|
|
|
|
|
|
|
|
// test if segments are specified
|
|
|
|
// test if segments are specified
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
API::MP(self::VERSION)->subscribeToLists($subscriber->id, array());
|
|
|
|
$this->getApi()->subscribeToLists($subscriber->id, array());
|
|
|
|
$this->fail('Segments are required exception should have been thrown.');
|
|
|
|
$this->fail('Segments are required exception should have been thrown.');
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
expect($e->getMessage())->equals('At least one segment ID is required.');
|
|
|
|
expect($e->getMessage())->equals('At least one segment ID is required.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$result = API::MP(self::VERSION)->subscribeToLists($subscriber->id, array($segment->id));
|
|
|
|
$result = $this->getApi()->subscribeToLists($subscriber->id, array($segment->id));
|
|
|
|
expect($result['id'])->equals($subscriber->id);
|
|
|
|
expect($result['id'])->equals($subscriber->id);
|
|
|
|
expect($result['subscriptions'][0]['segment_id'])->equals($segment->id);
|
|
|
|
expect($result['subscriptions'][0]['segment_id'])->equals($segment->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -173,7 +183,7 @@ class APITest extends \MailPoetTest {
|
|
|
|
'type' => Segment::TYPE_DEFAULT
|
|
|
|
'type' => Segment::TYPE_DEFAULT
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
);
|
|
|
|
$result = API::MP(self::VERSION)->subscribeToList($subscriber->email, $segment->id);
|
|
|
|
$result = $this->getApi()->subscribeToList($subscriber->email, $segment->id);
|
|
|
|
expect($result['id'])->equals($subscriber->id);
|
|
|
|
expect($result['id'])->equals($subscriber->id);
|
|
|
|
expect($result['subscriptions'])->notEmpty();
|
|
|
|
expect($result['subscriptions'])->notEmpty();
|
|
|
|
expect($result['subscriptions'][0]['segment_id'])->equals($segment->id);
|
|
|
|
expect($result['subscriptions'][0]['segment_id'])->equals($segment->id);
|
|
|
@ -181,7 +191,7 @@ class APITest extends \MailPoetTest {
|
|
|
|
|
|
|
|
|
|
|
|
function testItSchedulesWelcomeNotificationByDefaultAfterSubscriberSubscriberToLists() {
|
|
|
|
function testItSchedulesWelcomeNotificationByDefaultAfterSubscriberSubscriberToLists() {
|
|
|
|
$API = Stub::makeEmptyExcept(
|
|
|
|
$API = Stub::makeEmptyExcept(
|
|
|
|
new \MailPoet\API\MP\v1\API(),
|
|
|
|
\MailPoet\API\MP\v1\API::class,
|
|
|
|
'subscribeToLists',
|
|
|
|
'subscribeToLists',
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'_scheduleWelcomeNotification' => Expected::once()
|
|
|
|
'_scheduleWelcomeNotification' => Expected::once()
|
|
|
@ -201,7 +211,7 @@ class APITest extends \MailPoetTest {
|
|
|
|
|
|
|
|
|
|
|
|
function testItDoesNotScheduleWelcomeNotificationAfterSubscribingSubscriberToListsIfStatusIsNotSubscribed() {
|
|
|
|
function testItDoesNotScheduleWelcomeNotificationAfterSubscribingSubscriberToListsIfStatusIsNotSubscribed() {
|
|
|
|
$API = Stub::makeEmptyExcept(
|
|
|
|
$API = Stub::makeEmptyExcept(
|
|
|
|
new \MailPoet\API\MP\v1\API(),
|
|
|
|
\MailPoet\API\MP\v1\API::class,
|
|
|
|
'subscribeToLists',
|
|
|
|
'subscribeToLists',
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'_scheduleWelcomeNotification' => Expected::never()
|
|
|
|
'_scheduleWelcomeNotification' => Expected::never()
|
|
|
@ -220,7 +230,7 @@ class APITest extends \MailPoetTest {
|
|
|
|
|
|
|
|
|
|
|
|
function testItDoesNotScheduleWelcomeNotificationAfterSubscribingSubscriberToListsWhenDisabledByOption() {
|
|
|
|
function testItDoesNotScheduleWelcomeNotificationAfterSubscribingSubscriberToListsWhenDisabledByOption() {
|
|
|
|
$API = Stub::makeEmptyExcept(
|
|
|
|
$API = Stub::makeEmptyExcept(
|
|
|
|
new \MailPoet\API\MP\v1\API(),
|
|
|
|
\MailPoet\API\MP\v1\API::class,
|
|
|
|
'subscribeToLists',
|
|
|
|
'subscribeToLists',
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'_scheduleWelcomeNotification' => Expected::never()
|
|
|
|
'_scheduleWelcomeNotification' => Expected::never()
|
|
|
@ -246,7 +256,7 @@ class APITest extends \MailPoetTest {
|
|
|
|
'type' => Segment::TYPE_DEFAULT
|
|
|
|
'type' => Segment::TYPE_DEFAULT
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
);
|
|
|
|
$result = API::MP(self::VERSION)->getLists();
|
|
|
|
$result = $this->getApi()->getLists();
|
|
|
|
expect($result)->count(1);
|
|
|
|
expect($result)->count(1);
|
|
|
|
expect($result[0]['id'])->equals($segment->id);
|
|
|
|
expect($result[0]['id'])->equals($segment->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -264,14 +274,14 @@ class APITest extends \MailPoetTest {
|
|
|
|
'type' => Segment::TYPE_WP_USERS
|
|
|
|
'type' => Segment::TYPE_WP_USERS
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
);
|
|
|
|
$result = API::MP(self::VERSION)->getLists();
|
|
|
|
$result = $this->getApi()->getLists();
|
|
|
|
expect($result)->count(1);
|
|
|
|
expect($result)->count(1);
|
|
|
|
expect($result[0]['id'])->equals($default_segment->id);
|
|
|
|
expect($result[0]['id'])->equals($default_segment->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function testItRequiresEmailAddressToAddSubscriber() {
|
|
|
|
function testItRequiresEmailAddressToAddSubscriber() {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
API::MP(self::VERSION)->addSubscriber(array());
|
|
|
|
$this->getApi()->addSubscriber(array());
|
|
|
|
$this->fail('Subscriber email address required exception should have been thrown.');
|
|
|
|
$this->fail('Subscriber email address required exception should have been thrown.');
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
expect($e->getMessage())->equals('Subscriber email address is required.');
|
|
|
|
expect($e->getMessage())->equals('Subscriber email address is required.');
|
|
|
@ -283,7 +293,7 @@ class APITest extends \MailPoetTest {
|
|
|
|
$subscriber->hydrate(Fixtures::get('subscriber_template'));
|
|
|
|
$subscriber->hydrate(Fixtures::get('subscriber_template'));
|
|
|
|
$subscriber->save();
|
|
|
|
$subscriber->save();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
API::MP(self::VERSION)->addSubscriber(array('email' => $subscriber->email));
|
|
|
|
$this->getApi()->addSubscriber(array('email' => $subscriber->email));
|
|
|
|
$this->fail('Subscriber exists exception should have been thrown.');
|
|
|
|
$this->fail('Subscriber exists exception should have been thrown.');
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
expect($e->getMessage())->equals('This subscriber already exists.');
|
|
|
|
expect($e->getMessage())->equals('This subscriber already exists.');
|
|
|
@ -295,7 +305,7 @@ class APITest extends \MailPoetTest {
|
|
|
|
'email' => 'test' // invalid email
|
|
|
|
'email' => 'test' // invalid email
|
|
|
|
);
|
|
|
|
);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
API::MP(self::VERSION)->addSubscriber($subscriber);
|
|
|
|
$this->getApi()->addSubscriber($subscriber);
|
|
|
|
$this->fail('Failed to add subscriber exception should have been thrown.');
|
|
|
|
$this->fail('Failed to add subscriber exception should have been thrown.');
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
expect($e->getMessage())->contains('Failed to add subscriber:');
|
|
|
|
expect($e->getMessage())->contains('Failed to add subscriber:');
|
|
|
@ -315,7 +325,7 @@ class APITest extends \MailPoetTest {
|
|
|
|
'cf_' . $custom_field->id => 'test'
|
|
|
|
'cf_' . $custom_field->id => 'test'
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
$result = API::MP(self::VERSION)->addSubscriber($subscriber);
|
|
|
|
$result = $this->getApi()->addSubscriber($subscriber);
|
|
|
|
expect($result['id'])->greaterThan(0);
|
|
|
|
expect($result['id'])->greaterThan(0);
|
|
|
|
expect($result['email'])->equals($subscriber['email']);
|
|
|
|
expect($result['email'])->equals($subscriber['email']);
|
|
|
|
expect($result['cf_' . $custom_field->id])->equals('test');
|
|
|
|
expect($result['cf_' . $custom_field->id])->equals('test');
|
|
|
@ -334,16 +344,10 @@ class APITest extends \MailPoetTest {
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
$this->setExpectedException('Exception');
|
|
|
|
$this->setExpectedException('Exception');
|
|
|
|
API::MP(self::VERSION)->addSubscriber($subscriber);
|
|
|
|
$this->getApi()->addSubscriber($subscriber);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function testItSubscribesToSegmentsWhenAddingSubscriber() {
|
|
|
|
function testItSubscribesToSegmentsWhenAddingSubscriber() {
|
|
|
|
$API = Stub::makeEmptyExcept(
|
|
|
|
|
|
|
|
new \MailPoet\API\MP\v1\API(),
|
|
|
|
|
|
|
|
'addSubscriber',
|
|
|
|
|
|
|
|
array(
|
|
|
|
|
|
|
|
'_sendConfirmationEmail' => Expected::once()
|
|
|
|
|
|
|
|
), $this);
|
|
|
|
|
|
|
|
$segment = Segment::createOrUpdate(
|
|
|
|
$segment = Segment::createOrUpdate(
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'name' => 'Default',
|
|
|
|
'name' => 'Default',
|
|
|
@ -354,7 +358,7 @@ class APITest extends \MailPoetTest {
|
|
|
|
'email' => 'test@example.com'
|
|
|
|
'email' => 'test@example.com'
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
$result = $API->addSubscriber($subscriber, array($segment->id));
|
|
|
|
$result = $this->getApi()->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);
|
|
|
@ -362,16 +366,18 @@ class APITest extends \MailPoetTest {
|
|
|
|
|
|
|
|
|
|
|
|
function testItSchedulesWelcomeNotificationByDefaultAfterAddingSubscriber() {
|
|
|
|
function testItSchedulesWelcomeNotificationByDefaultAfterAddingSubscriber() {
|
|
|
|
$API = Stub::makeEmptyExcept(
|
|
|
|
$API = Stub::makeEmptyExcept(
|
|
|
|
new \MailPoet\API\MP\v1\API(),
|
|
|
|
\MailPoet\API\MP\v1\API::class,
|
|
|
|
'addSubscriber',
|
|
|
|
'addSubscriber',
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'_scheduleWelcomeNotification' => Expected::once()
|
|
|
|
'new_subscribe_notification_mailer'=> Stub::makeEmpty(NewSubscriberNotificationMailer::class, ['send']),
|
|
|
|
|
|
|
|
'required_custom_field_validator' => Stub::makeEmpty(RequiredCustomFieldValidator::class, ['validate']),
|
|
|
|
|
|
|
|
'_scheduleWelcomeNotification' => Expected::once(),
|
|
|
|
), $this);
|
|
|
|
), $this);
|
|
|
|
$subscriber = array(
|
|
|
|
$subscriber = array(
|
|
|
|
'email' => 'test@example.com',
|
|
|
|
'email' => 'test@example.com',
|
|
|
|
'status' => Subscriber::STATUS_SUBSCRIBED
|
|
|
|
'status' => Subscriber::STATUS_SUBSCRIBED
|
|
|
|
);
|
|
|
|
);
|
|
|
|
$segments = array(1);
|
|
|
|
$segments = [1];
|
|
|
|
$API->addSubscriber($subscriber, $segments);
|
|
|
|
$API->addSubscriber($subscriber, $segments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -389,7 +395,7 @@ class APITest extends \MailPoetTest {
|
|
|
|
'type' => Segment::TYPE_DEFAULT
|
|
|
|
'type' => Segment::TYPE_DEFAULT
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
);
|
|
|
|
$API = new \MailPoet\API\MP\v1\API();
|
|
|
|
$API = $this->getApi();
|
|
|
|
$subscriber = array(
|
|
|
|
$subscriber = array(
|
|
|
|
'email' => 'test@example.com',
|
|
|
|
'email' => 'test@example.com',
|
|
|
|
'status' => Subscriber::STATUS_SUBSCRIBED
|
|
|
|
'status' => Subscriber::STATUS_SUBSCRIBED
|
|
|
@ -401,10 +407,12 @@ class APITest extends \MailPoetTest {
|
|
|
|
|
|
|
|
|
|
|
|
function testItDoesNotScheduleWelcomeNotificationAfterAddingSubscriberIfStatusIsNotSubscribed() {
|
|
|
|
function testItDoesNotScheduleWelcomeNotificationAfterAddingSubscriberIfStatusIsNotSubscribed() {
|
|
|
|
$API = Stub::makeEmptyExcept(
|
|
|
|
$API = Stub::makeEmptyExcept(
|
|
|
|
new \MailPoet\API\MP\v1\API(),
|
|
|
|
\MailPoet\API\MP\v1\API::class,
|
|
|
|
'addSubscriber',
|
|
|
|
'addSubscriber',
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'_scheduleWelcomeNotification' => Expected::never()
|
|
|
|
'_scheduleWelcomeNotification' => Expected::never(),
|
|
|
|
|
|
|
|
'new_subscribe_notification_mailer'=> Stub::makeEmpty(NewSubscriberNotificationMailer::class, ['send']),
|
|
|
|
|
|
|
|
'required_custom_field_validator' => Stub::makeEmpty(RequiredCustomFieldValidator::class, ['validate']),
|
|
|
|
), $this);
|
|
|
|
), $this);
|
|
|
|
$subscriber = array(
|
|
|
|
$subscriber = array(
|
|
|
|
'email' => 'test@example.com'
|
|
|
|
'email' => 'test@example.com'
|
|
|
@ -415,10 +423,12 @@ class APITest extends \MailPoetTest {
|
|
|
|
|
|
|
|
|
|
|
|
function testItDoesNotScheduleWelcomeNotificationAfterAddingSubscriberWhenDisabledByOption() {
|
|
|
|
function testItDoesNotScheduleWelcomeNotificationAfterAddingSubscriberWhenDisabledByOption() {
|
|
|
|
$API = Stub::makeEmptyExcept(
|
|
|
|
$API = Stub::makeEmptyExcept(
|
|
|
|
new \MailPoet\API\MP\v1\API(),
|
|
|
|
\MailPoet\API\MP\v1\API::class,
|
|
|
|
'addSubscriber',
|
|
|
|
'addSubscriber',
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'_scheduleWelcomeNotification' => Expected::never()
|
|
|
|
'_scheduleWelcomeNotification' => Expected::never(),
|
|
|
|
|
|
|
|
'new_subscribe_notification_mailer'=> Stub::makeEmpty(NewSubscriberNotificationMailer::class, ['send']),
|
|
|
|
|
|
|
|
'required_custom_field_validator' => Stub::makeEmpty(RequiredCustomFieldValidator::class, ['validate'])
|
|
|
|
), $this);
|
|
|
|
), $this);
|
|
|
|
$subscriber = array(
|
|
|
|
$subscriber = array(
|
|
|
|
'email' => 'test@example.com',
|
|
|
|
'email' => 'test@example.com',
|
|
|
@ -431,10 +441,12 @@ class APITest extends \MailPoetTest {
|
|
|
|
|
|
|
|
|
|
|
|
function testByDefaultItSendsConfirmationEmailAfterAddingSubscriber() {
|
|
|
|
function testByDefaultItSendsConfirmationEmailAfterAddingSubscriber() {
|
|
|
|
$API = Stub::makeEmptyExcept(
|
|
|
|
$API = Stub::makeEmptyExcept(
|
|
|
|
new \MailPoet\API\MP\v1\API(),
|
|
|
|
\MailPoet\API\MP\v1\API::class,
|
|
|
|
'addSubscriber',
|
|
|
|
'addSubscriber',
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'_sendConfirmationEmail' => Expected::once()
|
|
|
|
'_sendConfirmationEmail' => Expected::once(),
|
|
|
|
|
|
|
|
'required_custom_field_validator' => Stub::makeEmpty(RequiredCustomFieldValidator::class, ['validate']),
|
|
|
|
|
|
|
|
'new_subscribe_notification_mailer'=> Stub::makeEmpty(NewSubscriberNotificationMailer::class, ['send'])
|
|
|
|
), $this);
|
|
|
|
), $this);
|
|
|
|
$subscriber = array(
|
|
|
|
$subscriber = array(
|
|
|
|
'email' => 'test@example.com'
|
|
|
|
'email' => 'test@example.com'
|
|
|
@ -444,14 +456,17 @@ class APITest extends \MailPoetTest {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function testItThrowsWhenConfirmationEmailFailsToSend() {
|
|
|
|
function testItThrowsWhenConfirmationEmailFailsToSend() {
|
|
|
|
$API = new \MailPoet\API\MP\v1\API();
|
|
|
|
$confirmation_mailer = $this->createMock(ConfirmationEmailMailer::class);
|
|
|
|
Mock::double($API, array(
|
|
|
|
$confirmation_mailer->expects($this->once())
|
|
|
|
'_sendConfirmationEmail' => function ($subscriber) {
|
|
|
|
->method('sendConfirmationEmail')
|
|
|
|
|
|
|
|
->willReturnCallback(function (Subscriber $subscriber) {
|
|
|
|
$subscriber->setError('Big Error');
|
|
|
|
$subscriber->setError('Big Error');
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
)
|
|
|
|
|
|
|
|
);
|
|
|
|
$API = Stub::copy($this->getApi(), [
|
|
|
|
|
|
|
|
'confirmation_email_mailer' => $confirmation_mailer,
|
|
|
|
|
|
|
|
]);
|
|
|
|
$segment = Segment::createOrUpdate(
|
|
|
|
$segment = Segment::createOrUpdate(
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'name' => 'Default',
|
|
|
|
'name' => 'Default',
|
|
|
@ -467,10 +482,12 @@ class APITest extends \MailPoetTest {
|
|
|
|
|
|
|
|
|
|
|
|
function testItDoesNotSendConfirmationEmailAfterAddingSubscriberWhenOptionIsSet() {
|
|
|
|
function testItDoesNotSendConfirmationEmailAfterAddingSubscriberWhenOptionIsSet() {
|
|
|
|
$API = Stub::makeEmptyExcept(
|
|
|
|
$API = Stub::makeEmptyExcept(
|
|
|
|
new \MailPoet\API\MP\v1\API(),
|
|
|
|
\MailPoet\API\MP\v1\API::class,
|
|
|
|
'addSubscriber',
|
|
|
|
'addSubscriber',
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'_sendConfirmationEmail' => Expected::never()
|
|
|
|
'__sendConfirmationEmail' => Expected::never(),
|
|
|
|
|
|
|
|
'required_custom_field_validator' => Stub::makeEmpty(RequiredCustomFieldValidator::class, ['validate']),
|
|
|
|
|
|
|
|
'new_subscribe_notification_mailer'=> Stub::makeEmpty(NewSubscriberNotificationMailer::class, ['send'])
|
|
|
|
), $this);
|
|
|
|
), $this);
|
|
|
|
$subscriber = array(
|
|
|
|
$subscriber = array(
|
|
|
|
'email' => 'test@example.com'
|
|
|
|
'email' => 'test@example.com'
|
|
|
@ -482,7 +499,7 @@ class APITest extends \MailPoetTest {
|
|
|
|
|
|
|
|
|
|
|
|
function testItRequiresNameToAddList() {
|
|
|
|
function testItRequiresNameToAddList() {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
API::MP(self::VERSION)->addList(array());
|
|
|
|
$this->getApi()->addList(array());
|
|
|
|
$this->fail('List name required exception should have been thrown.');
|
|
|
|
$this->fail('List name required exception should have been thrown.');
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
expect($e->getMessage())->equals('List name is required.');
|
|
|
|
expect($e->getMessage())->equals('List name is required.');
|
|
|
@ -494,7 +511,7 @@ class APITest extends \MailPoetTest {
|
|
|
|
$segment->name = 'Test segment';
|
|
|
|
$segment->name = 'Test segment';
|
|
|
|
$segment->save();
|
|
|
|
$segment->save();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
API::MP(self::VERSION)->addList(array('name' => $segment->name));
|
|
|
|
$this->getApi()->addList(array('name' => $segment->name));
|
|
|
|
$this->fail('List exists exception should have been thrown.');
|
|
|
|
$this->fail('List exists exception should have been thrown.');
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
expect($e->getMessage())->equals('This list already exists.');
|
|
|
|
expect($e->getMessage())->equals('This list already exists.');
|
|
|
@ -506,14 +523,14 @@ class APITest extends \MailPoetTest {
|
|
|
|
'name' => 'Test segment'
|
|
|
|
'name' => 'Test segment'
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
$result = API::MP(self::VERSION)->addList($segment);
|
|
|
|
$result = $this->getApi()->addList($segment);
|
|
|
|
expect($result['id'])->greaterThan(0);
|
|
|
|
expect($result['id'])->greaterThan(0);
|
|
|
|
expect($result['name'])->equals($segment['name']);
|
|
|
|
expect($result['name'])->equals($segment['name']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function testItDoesNotUnsubscribeMissingSubscriberFromLists() {
|
|
|
|
function testItDoesNotUnsubscribeMissingSubscriberFromLists() {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
API::MP(self::VERSION)->unsubscribeFromLists(false, array(1,2,3));
|
|
|
|
$this->getApi()->unsubscribeFromLists(false, array(1,2,3));
|
|
|
|
$this->fail('Subscriber does not exist exception should have been thrown.');
|
|
|
|
$this->fail('Subscriber does not exist exception should have been thrown.');
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
expect($e->getMessage())->equals('This subscriber does not exist.');
|
|
|
|
expect($e->getMessage())->equals('This subscriber does not exist.');
|
|
|
@ -526,14 +543,14 @@ class APITest extends \MailPoetTest {
|
|
|
|
$subscriber->save();
|
|
|
|
$subscriber->save();
|
|
|
|
// multiple lists error message
|
|
|
|
// multiple lists error message
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
API::MP(self::VERSION)->unsubscribeFromLists($subscriber->id, array(1,2,3));
|
|
|
|
$this->getApi()->unsubscribeFromLists($subscriber->id, array(1,2,3));
|
|
|
|
$this->fail('Missing segments exception should have been thrown.');
|
|
|
|
$this->fail('Missing segments exception should have been thrown.');
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
expect($e->getMessage())->equals('These lists do not exist.');
|
|
|
|
expect($e->getMessage())->equals('These lists do not exist.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// single list error message
|
|
|
|
// single list error message
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
API::MP(self::VERSION)->unsubscribeFromLists($subscriber->id, array(1));
|
|
|
|
$this->getApi()->unsubscribeFromLists($subscriber->id, array(1));
|
|
|
|
$this->fail('Missing segments exception should have been thrown.');
|
|
|
|
$this->fail('Missing segments exception should have been thrown.');
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
expect($e->getMessage())->equals('This list does not exist.');
|
|
|
|
expect($e->getMessage())->equals('This list does not exist.');
|
|
|
@ -552,14 +569,14 @@ class APITest extends \MailPoetTest {
|
|
|
|
);
|
|
|
|
);
|
|
|
|
// multiple lists error message
|
|
|
|
// multiple lists error message
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
API::MP(self::VERSION)->unsubscribeFromLists($subscriber->id, array($segment->id, 90, 100));
|
|
|
|
$this->getApi()->unsubscribeFromLists($subscriber->id, array($segment->id, 90, 100));
|
|
|
|
$this->fail('Missing segments with IDs exception should have been thrown.');
|
|
|
|
$this->fail('Missing segments with IDs exception should have been thrown.');
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
expect($e->getMessage())->equals('Lists with IDs 90, 100 do not exist.');
|
|
|
|
expect($e->getMessage())->equals('Lists with IDs 90, 100 do not exist.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// single list error message
|
|
|
|
// single list error message
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
API::MP(self::VERSION)->unsubscribeFromLists($subscriber->id, array($segment->id, 90));
|
|
|
|
$this->getApi()->unsubscribeFromLists($subscriber->id, array($segment->id, 90));
|
|
|
|
$this->fail('Missing segments with IDs exception should have been thrown.');
|
|
|
|
$this->fail('Missing segments with IDs exception should have been thrown.');
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
expect($e->getMessage())->equals('List with ID 90 does not exist.');
|
|
|
|
expect($e->getMessage())->equals('List with ID 90 does not exist.');
|
|
|
@ -577,7 +594,7 @@ class APITest extends \MailPoetTest {
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
API::MP(self::VERSION)->unsubscribeFromLists($subscriber->id, array($segment->id));
|
|
|
|
$this->getApi()->unsubscribeFromLists($subscriber->id, array($segment->id));
|
|
|
|
$this->fail('WP Users segment exception should have been thrown.');
|
|
|
|
$this->fail('WP Users segment exception should have been thrown.');
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
expect($e->getMessage())->equals("Can't subscribe to a WordPress Users list with ID {$segment->id}.");
|
|
|
|
expect($e->getMessage())->equals("Can't subscribe to a WordPress Users list with ID {$segment->id}.");
|
|
|
@ -587,7 +604,7 @@ class APITest extends \MailPoetTest {
|
|
|
|
function testItUsesMultipleListsUnsubscribeMethodWhenUnsubscribingFromSingleList() {
|
|
|
|
function testItUsesMultipleListsUnsubscribeMethodWhenUnsubscribingFromSingleList() {
|
|
|
|
// unsubscribing from single list = converting list ID to an array and using
|
|
|
|
// unsubscribing from single list = converting list ID to an array and using
|
|
|
|
// multiple lists unsubscribe method
|
|
|
|
// multiple lists unsubscribe method
|
|
|
|
$API = Stub::make(new \MailPoet\API\MP\v1\API(), array(
|
|
|
|
$API = Stub::make(\MailPoet\API\MP\v1\API::class, array(
|
|
|
|
'unsubscribeFromLists' => function() {
|
|
|
|
'unsubscribeFromLists' => function() {
|
|
|
|
return func_get_args();
|
|
|
|
return func_get_args();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -615,15 +632,15 @@ class APITest extends \MailPoetTest {
|
|
|
|
|
|
|
|
|
|
|
|
// test if segments are specified
|
|
|
|
// test if segments are specified
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
API::MP(self::VERSION)->unsubscribeFromLists($subscriber->id, array());
|
|
|
|
$this->getApi()->unsubscribeFromLists($subscriber->id, array());
|
|
|
|
$this->fail('Segments are required exception should have been thrown.');
|
|
|
|
$this->fail('Segments are required exception should have been thrown.');
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
expect($e->getMessage())->equals('At least one segment ID is required.');
|
|
|
|
expect($e->getMessage())->equals('At least one segment ID is required.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$result = API::MP(self::VERSION)->subscribeToLists($subscriber->id, array($segment->id));
|
|
|
|
$result = $this->getApi()->subscribeToLists($subscriber->id, array($segment->id));
|
|
|
|
expect($result['subscriptions'][0]['status'])->equals(Subscriber::STATUS_SUBSCRIBED);
|
|
|
|
expect($result['subscriptions'][0]['status'])->equals(Subscriber::STATUS_SUBSCRIBED);
|
|
|
|
$result = API::MP(self::VERSION)->unsubscribeFromLists($subscriber->id, array($segment->id));
|
|
|
|
$result = $this->getApi()->unsubscribeFromLists($subscriber->id, array($segment->id));
|
|
|
|
expect($result['subscriptions'][0]['status'])->equals(Subscriber::STATUS_UNSUBSCRIBED);
|
|
|
|
expect($result['subscriptions'][0]['status'])->equals(Subscriber::STATUS_UNSUBSCRIBED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -637,16 +654,16 @@ class APITest extends \MailPoetTest {
|
|
|
|
'type' => Segment::TYPE_DEFAULT
|
|
|
|
'type' => Segment::TYPE_DEFAULT
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
);
|
|
|
|
API::MP(self::VERSION)->subscribeToList($subscriber->id, $segment->id);
|
|
|
|
$this->getApi()->subscribeToList($subscriber->id, $segment->id);
|
|
|
|
|
|
|
|
|
|
|
|
// successful response
|
|
|
|
// successful response
|
|
|
|
$result = API::MP(self::VERSION)->getSubscriber($subscriber->email);
|
|
|
|
$result = $this->getApi()->getSubscriber($subscriber->email);
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
// error response
|
|
|
|
// error response
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
API::MP(self::VERSION)->getSubscriber('some_fake_email');
|
|
|
|
$this->getApi()->getSubscriber('some_fake_email');
|
|
|
|
$this->fail('Subscriber does not exist exception should have been thrown.');
|
|
|
|
$this->fail('Subscriber does not exist exception should have been thrown.');
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
expect($e->getMessage())->equals('This subscriber does not exist.');
|
|
|
|
expect($e->getMessage())->equals('This subscriber does not exist.');
|
|
|
|