Adds methods to unsubscribe from list(s)
This commit is contained in:
@ -79,6 +79,44 @@ class API {
|
|||||||
return $subscriber->withCustomFields()->withSubscriptions()->asArray();
|
return $subscriber->withCustomFields()->withSubscriptions()->asArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function unsubscribeFromList($subscriber_id, $segment_id) {
|
||||||
|
return $this->unsubscribeFromLists($subscriber_id, array($segment_id));
|
||||||
|
}
|
||||||
|
|
||||||
|
function unsubscribeFromLists($subscriber_id, array $segments_ids) {
|
||||||
|
$subscriber = Subscriber::findOne($subscriber_id);
|
||||||
|
// throw exception when subscriber does not exist
|
||||||
|
if(!$subscriber) {
|
||||||
|
throw new \Exception(__('This subscriber does not exist.', 'mailpoet'));
|
||||||
|
}
|
||||||
|
|
||||||
|
// throw exception when none of the segments exist
|
||||||
|
$found_segments = Segment::whereIn('id', $segments_ids)->findMany();
|
||||||
|
if(!$found_segments) {
|
||||||
|
$exception = (count($segments_ids) === 1) ?
|
||||||
|
__('This list does not exist.', 'mailpoet') :
|
||||||
|
__('These lists do not exist.', 'mailpoet');
|
||||||
|
throw new \Exception($exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
// throw exception when trying to unsubscribe from a WP Users segment
|
||||||
|
$found_segments_ids = array_map(function($segment) {
|
||||||
|
return $segment->id;
|
||||||
|
}, $found_segments);
|
||||||
|
|
||||||
|
// throw an exception when one or more segments do not exist
|
||||||
|
if(count($found_segments_ids) !== count($segments_ids)) {
|
||||||
|
$missing_ids = array_values(array_diff($segments_ids, $found_segments_ids));
|
||||||
|
$exception = (count($missing_ids) === 1) ?
|
||||||
|
__('List with ID %s does not exist.', 'mailpoet') :
|
||||||
|
__('Lists with IDs %s do not exist.', 'mailpoet');
|
||||||
|
throw new \Exception(sprintf($exception, implode(', ', $missing_ids)));
|
||||||
|
}
|
||||||
|
|
||||||
|
SubscriberSegment::unsubscribeFromSegments($subscriber, $found_segments_ids);
|
||||||
|
return $subscriber->withCustomFields()->withSubscriptions()->asArray();
|
||||||
|
}
|
||||||
|
|
||||||
function getLists() {
|
function getLists() {
|
||||||
return Segment::whereNotEqual('type', Segment::TYPE_WP_USERS)->findArray();
|
return Segment::whereNotEqual('type', Segment::TYPE_WP_USERS)->findArray();
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ class APITest extends \MailPoetTest {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItDoesNotSubscribeMissingSusbcriberToLists() {
|
function testItDoesNotSubscribeMissingSubscriberToLists() {
|
||||||
try {
|
try {
|
||||||
API::MP(self::VERSION)->subscribeToLists(false, array(1,2,3));
|
API::MP(self::VERSION)->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.');
|
||||||
@ -124,7 +124,7 @@ class APITest extends \MailPoetTest {
|
|||||||
return func_get_args();
|
return func_get_args();
|
||||||
}
|
}
|
||||||
));
|
));
|
||||||
expect($API->subscribeToList(1,2))->equals(
|
expect($API->subscribeToList(1, 2))->equals(
|
||||||
array(
|
array(
|
||||||
1,
|
1,
|
||||||
array(
|
array(
|
||||||
@ -385,7 +385,36 @@ class APITest extends \MailPoetTest {
|
|||||||
expect($result['name'])->equals($segment['name']);
|
expect($result['name'])->equals($segment['name']);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItGetsSubscriber() {
|
function testItDoesNotUnsubscribeMissingSusbcriberFromLists() {
|
||||||
|
try {
|
||||||
|
API::MP(self::VERSION)->unsubscribeFromLists(false, array(1,2,3));
|
||||||
|
$this->fail('Subscriber does not exist exception should have been thrown.');
|
||||||
|
} catch(\Exception $e) {
|
||||||
|
expect($e->getMessage())->equals('This subscriber does not exist.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItDoesNotUnsubscribeSubscriberFromMissingLists() {
|
||||||
|
$subscriber = Subscriber::create();
|
||||||
|
$subscriber->hydrate(Fixtures::get('subscriber_template'));
|
||||||
|
$subscriber->save();
|
||||||
|
// multiple lists error message
|
||||||
|
try {
|
||||||
|
API::MP(self::VERSION)->unsubscribeFromLists($subscriber->id, array(1,2,3));
|
||||||
|
$this->fail('Missing segments exception should have been thrown.');
|
||||||
|
} catch(\Exception $e) {
|
||||||
|
expect($e->getMessage())->equals('These lists do not exist.');
|
||||||
|
}
|
||||||
|
// single list error message
|
||||||
|
try {
|
||||||
|
API::MP(self::VERSION)->unsubscribeFromLists($subscriber->id, array(1));
|
||||||
|
$this->fail('Missing segments exception should have been thrown.');
|
||||||
|
} catch(\Exception $e) {
|
||||||
|
expect($e->getMessage())->equals('This list does not exist.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItDoesNotUnsubscribeSubscriberFromListsWhenOneOrMoreListsAreMissing() {
|
||||||
$subscriber = Subscriber::create();
|
$subscriber = Subscriber::create();
|
||||||
$subscriber->hydrate(Fixtures::get('subscriber_template'));
|
$subscriber->hydrate(Fixtures::get('subscriber_template'));
|
||||||
$subscriber->save();
|
$subscriber->save();
|
||||||
@ -395,20 +424,54 @@ class APITest extends \MailPoetTest {
|
|||||||
'type' => Segment::TYPE_DEFAULT
|
'type' => Segment::TYPE_DEFAULT
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
API::MP(self::VERSION)->subscribeToList($subscriber->id, $segment->id);
|
// multiple lists error message
|
||||||
|
|
||||||
// successful response
|
|
||||||
$result = API::MP(self::VERSION)->getSubscriber($subscriber->email);
|
|
||||||
expect($result['email'])->equals($subscriber->email);
|
|
||||||
expect($result['subscriptions'][0]['segment_id'])->equals($segment->id);
|
|
||||||
|
|
||||||
// error response
|
|
||||||
try {
|
try {
|
||||||
API::MP(self::VERSION)->getSubscriber('some_fake_email');
|
API::MP(self::VERSION)->unsubscribeFromLists($subscriber->id, array($segment->id, 90, 100));
|
||||||
$this->fail('Subscriber does not exist 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('This subscriber does not exist.');
|
expect($e->getMessage())->equals('Lists with IDs 90, 100 do not exist.');
|
||||||
}
|
}
|
||||||
|
// single list error message
|
||||||
|
try {
|
||||||
|
API::MP(self::VERSION)->unsubscribeFromLists($subscriber->id, array($segment->id, 90));
|
||||||
|
$this->fail('Missing segments with IDs exception should have been thrown.');
|
||||||
|
} catch(\Exception $e) {
|
||||||
|
expect($e->getMessage())->equals('List with ID 90 does not exist.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItUsesMultipleListsUnsubscribeMethodWhenUnsubscribingFromSingleList() {
|
||||||
|
// unsubscribing from single list = converting list ID to an array and using
|
||||||
|
// multiple lists unsubscribe method
|
||||||
|
$API = Stub::make(new \MailPoet\API\MP\v1\API(), array(
|
||||||
|
'unsubscribeFromLists' => function() {
|
||||||
|
return func_get_args();
|
||||||
|
}
|
||||||
|
));
|
||||||
|
expect($API->unsubscribeFromList(1, 2))
|
||||||
|
->equals(array(
|
||||||
|
1,
|
||||||
|
array(
|
||||||
|
2
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItUnsubscribesSubscriberFromMultipleLists() {
|
||||||
|
$subscriber = Subscriber::create();
|
||||||
|
$subscriber->hydrate(Fixtures::get('subscriber_template'));
|
||||||
|
$subscriber->save();
|
||||||
|
$segment = Segment::createOrUpdate(
|
||||||
|
array(
|
||||||
|
'name' => 'Default',
|
||||||
|
'type' => Segment::TYPE_DEFAULT
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$result = API::MP(self::VERSION)->subscribeToLists($subscriber->id, array($segment->id));
|
||||||
|
expect($result['subscriptions'][0]['status'])->equals(Subscriber::STATUS_SUBSCRIBED);
|
||||||
|
$result = API::MP(self::VERSION)->unsubscribeFromLists($subscriber->id, array($segment->id));
|
||||||
|
expect($result['subscriptions'][0]['status'])->equals(Subscriber::STATUS_UNSUBSCRIBED);
|
||||||
}
|
}
|
||||||
|
|
||||||
function _after() {
|
function _after() {
|
||||||
|
Reference in New Issue
Block a user