Added unit tests for SubscriberSegment / Subscriber models
This commit is contained in:
@ -184,7 +184,8 @@ class Segment extends Model {
|
|||||||
$segment->set($data);
|
$segment->set($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $segment->save();
|
$segment->save();
|
||||||
|
return $segment;
|
||||||
}
|
}
|
||||||
|
|
||||||
static function getPublic() {
|
static function getPublic() {
|
||||||
|
@ -47,7 +47,7 @@ class Subscriber extends Model {
|
|||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
// delete all relations to segments
|
// delete all relations to segments
|
||||||
SubscriberSegment::where('subscriber_id', $this->id)->deleteMany();
|
SubscriberSegment::deleteSubscriptions($this);
|
||||||
|
|
||||||
return parent::delete();
|
return parent::delete();
|
||||||
}
|
}
|
||||||
@ -590,14 +590,14 @@ class Subscriber extends Model {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static function bulkTrash($orm) {
|
static function bulkTrash($orm) {
|
||||||
return parent::bulkAction($orm, function($ids) {
|
return parent::bulkAction($orm, function($subscriber_ids) use($orm) {
|
||||||
parent::rawExecute(join(' ', array(
|
parent::rawExecute(join(' ', array(
|
||||||
'UPDATE `'.self::$_table.'`',
|
'UPDATE `'.self::$_table.'`',
|
||||||
'SET `deleted_at` = NOW()',
|
'SET `deleted_at` = NOW()',
|
||||||
'WHERE `id` IN ('.rtrim(str_repeat('?,', count($ids)), ',').')',
|
'WHERE `id` IN ('.rtrim(str_repeat('?,', count($subscriber_ids)), ',').')',
|
||||||
'AND `wp_user_id` IS NULL'
|
'AND `wp_user_id` IS NULL'
|
||||||
)),
|
)),
|
||||||
$ids
|
$subscriber_ids
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -605,7 +605,7 @@ class Subscriber extends Model {
|
|||||||
static function bulkDelete($orm) {
|
static function bulkDelete($orm) {
|
||||||
return parent::bulkAction($orm, function($subscriber_ids) {
|
return parent::bulkAction($orm, function($subscriber_ids) {
|
||||||
// delete all subscriber/segment relationships
|
// delete all subscriber/segment relationships
|
||||||
SubscriberSegment::deleteSubscriptionsForAll($subscriber_ids);
|
SubscriberSegment::deleteManySubscriptions($subscriber_ids);
|
||||||
|
|
||||||
// delete subscribers (except WP Users)
|
// delete subscribers (except WP Users)
|
||||||
Subscriber::whereIn('id', $subscriber_ids)
|
Subscriber::whereIn('id', $subscriber_ids)
|
||||||
|
@ -41,7 +41,6 @@ class SubscriberSegment extends Model {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
} else {
|
} else {
|
||||||
// unsubscribe from all segments (except the WP users segment)
|
// unsubscribe from all segments (except the WP users segment)
|
||||||
$subscriptions = self::where('subscriber_id', $subscriber->id);
|
$subscriptions = self::where('subscriber_id', $subscriber->id);
|
||||||
@ -52,10 +51,11 @@ class SubscriberSegment extends Model {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $subscriptions->findResultSet()
|
$subscriptions->findResultSet()
|
||||||
->set('status', Subscriber::STATUS_UNSUBSCRIBED)
|
->set('status', Subscriber::STATUS_UNSUBSCRIBED)
|
||||||
->save();
|
->save();
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -73,7 +73,6 @@ class SubscriberSegment extends Model {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
// subscribe to all segments
|
// subscribe to all segments
|
||||||
@ -91,10 +90,12 @@ class SubscriberSegment extends Model {
|
|||||||
return self::subscribeToSegments($subscriber, $segment_ids);
|
return self::subscribeToSegments($subscriber, $segment_ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
static function deleteSubscriptionsForAll($subscriber_ids = array()) {
|
static function deleteManySubscriptions($subscriber_ids = array()) {
|
||||||
if(!empty($subscriber_ids)) {
|
if(!empty($subscriber_ids)) {
|
||||||
// delete subscribers' relations to segments (except WP Users' segment)
|
// delete subscribers' relations to segments (except WP Users' segment)
|
||||||
$subscriptions = SubscriberSegment::whereIn('subscriber_id', $subscriber_ids);
|
$subscriptions = SubscriberSegment::whereIn(
|
||||||
|
'subscriber_id', $subscriber_ids
|
||||||
|
);
|
||||||
|
|
||||||
$wp_segment = Segment::getWPSegment();
|
$wp_segment = Segment::getWPSegment();
|
||||||
if($wp_segment !== false) {
|
if($wp_segment !== false) {
|
||||||
@ -109,17 +110,8 @@ class SubscriberSegment extends Model {
|
|||||||
|
|
||||||
static function deleteSubscriptions($subscriber) {
|
static function deleteSubscriptions($subscriber) {
|
||||||
if($subscriber !== false && $subscriber->id > 0) {
|
if($subscriber !== false && $subscriber->id > 0) {
|
||||||
// delete all relationships to segments (except the WP users segment)
|
// delete all relationships to segments
|
||||||
$subscriptions = self::where('subscriber_id', $subscriber->id);
|
return self::where('subscriber_id', $subscriber->id)->deleteMany();
|
||||||
|
|
||||||
$wp_segment = Segment::getWPSegment();
|
|
||||||
if($wp_segment !== false) {
|
|
||||||
$subscriptions = $subscriptions->whereNotEqual(
|
|
||||||
'segment_id', $wp_segment->id
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $subscriptions->delete();
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -152,6 +144,7 @@ class SubscriberSegment extends Model {
|
|||||||
return $subscription->save();
|
return $subscription->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TO BE REVIEWED
|
||||||
static function createMultiple($segmnets, $subscribers) {
|
static function createMultiple($segmnets, $subscribers) {
|
||||||
$values = Helpers::flattenArray(
|
$values = Helpers::flattenArray(
|
||||||
array_map(function ($segment) use ($subscribers) {
|
array_map(function ($segment) use ($subscribers) {
|
||||||
|
211
tests/unit/Models/SubscriberSegmentTest.php
Normal file
211
tests/unit/Models/SubscriberSegmentTest.php
Normal file
@ -0,0 +1,211 @@
|
|||||||
|
<?php
|
||||||
|
use MailPoet\Models\Subscriber;
|
||||||
|
use MailPoet\Models\Segment;
|
||||||
|
use MailPoet\Models\SubscriberSegment;
|
||||||
|
|
||||||
|
class SubscriberSegmentTest extends MailPoetTest {
|
||||||
|
|
||||||
|
function _before() {
|
||||||
|
$this->subscriber = Subscriber::createOrUpdate(array(
|
||||||
|
'email' => 'john.doe@mailpoet.com',
|
||||||
|
'status' => Subscriber::STATUS_SUBSCRIBED
|
||||||
|
));
|
||||||
|
$this->segment_1 = Segment::createOrUpdate(array('name' => 'Segment 1'));
|
||||||
|
$this->segment_2 = Segment::createOrUpdate(array('name' => 'Segment 2'));
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItCanSubscribeToSegments() {
|
||||||
|
$result = SubscriberSegment::subscribeToSegments($this->subscriber, array(
|
||||||
|
$this->segment_1->id,
|
||||||
|
$this->segment_2->id
|
||||||
|
));
|
||||||
|
expect($result)->true();
|
||||||
|
|
||||||
|
$subscribed_segments = $this->subscriber->segments()->findArray();
|
||||||
|
expect($subscribed_segments)->count(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItCanResetSubscriptions() {
|
||||||
|
// subscribe to the first segment
|
||||||
|
$result = SubscriberSegment::subscribeToSegments($this->subscriber, array(
|
||||||
|
$this->segment_1->id
|
||||||
|
));
|
||||||
|
expect($result)->true();
|
||||||
|
|
||||||
|
$subscribed_segments = $this->subscriber->segments()->findArray();
|
||||||
|
expect($subscribed_segments)->count(1);
|
||||||
|
expect($subscribed_segments[0]['name'])->equals($this->segment_1->name);
|
||||||
|
|
||||||
|
// reset subscriptions to second segment
|
||||||
|
SubscriberSegment::resetSubscriptions($this->subscriber, array(
|
||||||
|
$this->segment_2->id
|
||||||
|
));
|
||||||
|
|
||||||
|
$subscribed_segments = $this->subscriber->segments()->findArray();
|
||||||
|
expect($subscribed_segments)->count(1);
|
||||||
|
expect($subscribed_segments[0]['name'])->equals($this->segment_2->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItCanUnsubscribeFromSegments() {
|
||||||
|
SubscriberSegment::createOrUpdate(array(
|
||||||
|
'subscriber_id' => $this->subscriber->id,
|
||||||
|
'segment_id' => $this->segment_1->id,
|
||||||
|
'status' => Subscriber::STATUS_SUBSCRIBED
|
||||||
|
));
|
||||||
|
SubscriberSegment::createOrUpdate(array(
|
||||||
|
'subscriber_id' => $this->subscriber->id,
|
||||||
|
'segment_id' => $this->segment_2->id,
|
||||||
|
'status' => Subscriber::STATUS_SUBSCRIBED
|
||||||
|
));
|
||||||
|
|
||||||
|
// unsubscribe subscriber from first segment
|
||||||
|
$result = SubscriberSegment::unsubscribeFromSegments($this->subscriber,
|
||||||
|
array(
|
||||||
|
$this->segment_1->id
|
||||||
|
)
|
||||||
|
);
|
||||||
|
expect($result)->true();
|
||||||
|
|
||||||
|
$subscribed_segments = $this->subscriber->segments()->findArray();
|
||||||
|
expect($subscribed_segments)->count(1);
|
||||||
|
expect($subscribed_segments[0]['name'])->equals($this->segment_2->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItCanUnsubscribeFromAllSegments() {
|
||||||
|
SubscriberSegment::createOrUpdate(array(
|
||||||
|
'subscriber_id' => $this->subscriber->id,
|
||||||
|
'segment_id' => $this->segment_1->id,
|
||||||
|
'status' => Subscriber::STATUS_SUBSCRIBED
|
||||||
|
));
|
||||||
|
SubscriberSegment::createOrUpdate(array(
|
||||||
|
'subscriber_id' => $this->subscriber->id,
|
||||||
|
'segment_id' => $this->segment_2->id,
|
||||||
|
'status' => Subscriber::STATUS_SUBSCRIBED
|
||||||
|
));
|
||||||
|
|
||||||
|
// unsubscribe subscriber from all segments
|
||||||
|
$result = SubscriberSegment::unsubscribeFromSegments($this->subscriber);
|
||||||
|
expect($result)->true();
|
||||||
|
|
||||||
|
$subscribed_segments = $this->subscriber->segments()->findArray();
|
||||||
|
expect($subscribed_segments)->isEmpty();
|
||||||
|
|
||||||
|
// the relations still exist but now have a status of "unsubscribed"
|
||||||
|
$subscriptions_count = SubscriberSegment::where(
|
||||||
|
'subscriber_id', $this->subscriber->id
|
||||||
|
)
|
||||||
|
->where('status', Subscriber::STATUS_UNSUBSCRIBED)
|
||||||
|
->count();
|
||||||
|
expect($subscriptions_count)->equals(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItCanDeleteSubscriptions() {
|
||||||
|
SubscriberSegment::createOrUpdate(array(
|
||||||
|
'subscriber_id' => $this->subscriber->id,
|
||||||
|
'segment_id' => $this->segment_1->id,
|
||||||
|
'status' => Subscriber::STATUS_SUBSCRIBED
|
||||||
|
));
|
||||||
|
SubscriberSegment::createOrUpdate(array(
|
||||||
|
'subscriber_id' => $this->subscriber->id,
|
||||||
|
'segment_id' => $this->segment_2->id,
|
||||||
|
'status' => Subscriber::STATUS_SUBSCRIBED
|
||||||
|
));
|
||||||
|
|
||||||
|
$subscribed_segments = $this->subscriber->segments()->findArray();
|
||||||
|
expect($subscribed_segments)->count(2);
|
||||||
|
|
||||||
|
// completely remove all subscriptions
|
||||||
|
SubscriberSegment::deleteSubscriptions($this->subscriber);
|
||||||
|
|
||||||
|
$subscriptions_count = SubscriberSegment::where(
|
||||||
|
'subscriber_id', $this->subscriber->id
|
||||||
|
)->count();
|
||||||
|
expect($subscriptions_count)->equals(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItCanDeleteManySubscriptions() {
|
||||||
|
// subscribe first subscriber to segments
|
||||||
|
SubscriberSegment::subscribeToSegments($this->subscriber, array(
|
||||||
|
$this->segment_1->id, $this->segment_2->id
|
||||||
|
));
|
||||||
|
// create a second subscriber
|
||||||
|
$subscriber_2 = Subscriber::createOrUpdate(array(
|
||||||
|
'email' => 'jane.doe@mailpoet.com',
|
||||||
|
'status' => Subscriber::STATUS_SUBSCRIBED
|
||||||
|
));
|
||||||
|
// subscribe her to segments
|
||||||
|
SubscriberSegment::subscribeToSegments($subscriber_2, array(
|
||||||
|
$this->segment_1->id, $this->segment_2->id
|
||||||
|
));
|
||||||
|
|
||||||
|
expect(SubscriberSegment::count())->equals(4);
|
||||||
|
|
||||||
|
$result = SubscriberSegment::deleteManySubscriptions(array(
|
||||||
|
$this->subscriber->id, $subscriber_2->id
|
||||||
|
));
|
||||||
|
expect($result)->true();
|
||||||
|
|
||||||
|
expect(SubscriberSegment::count())->equals(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItCanCreateOrUpdate() {
|
||||||
|
// create relationship between subscriber and a segment
|
||||||
|
$result = SubscriberSegment::createOrUpdate(array(
|
||||||
|
'subscriber_id' => $this->subscriber->id,
|
||||||
|
'segment_id' => $this->segment_1->id,
|
||||||
|
'status' => Subscriber::STATUS_SUBSCRIBED
|
||||||
|
));
|
||||||
|
expect($result->id > 0)->true();
|
||||||
|
expect($result->getErrors())->false();
|
||||||
|
|
||||||
|
// check that we have the proper status
|
||||||
|
$created = SubscriberSegment::findOne($result->id);
|
||||||
|
expect($created->status)->equals(Subscriber::STATUS_SUBSCRIBED);
|
||||||
|
|
||||||
|
// update same combination of subscriber/segment with a different status
|
||||||
|
$result = SubscriberSegment::createOrUpdate(array(
|
||||||
|
'subscriber_id' => $this->subscriber->id,
|
||||||
|
'segment_id' => $this->segment_1->id,
|
||||||
|
'status' => Subscriber::STATUS_UNSUBSCRIBED
|
||||||
|
));
|
||||||
|
expect($result->id > 0)->true();
|
||||||
|
expect($result->getErrors())->false();
|
||||||
|
|
||||||
|
// check updated status
|
||||||
|
$updated = SubscriberSegment::findOne($created->id);
|
||||||
|
expect($updated->status)->equals(Subscriber::STATUS_UNSUBSCRIBED);
|
||||||
|
|
||||||
|
// we should have only one relationship for that user
|
||||||
|
$subscriptions_count = SubscriberSegment::where(
|
||||||
|
'subscriber_id', $this->subscriber->id
|
||||||
|
)
|
||||||
|
->where('segment_id', $this->segment_1->id)
|
||||||
|
->count();
|
||||||
|
expect($subscriptions_count)->equals(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItCanFilterBySubscribedStatus() {
|
||||||
|
SubscriberSegment::createOrUpdate(array(
|
||||||
|
'subscriber_id' => $this->subscriber->id,
|
||||||
|
'segment_id' => $this->segment_1->id,
|
||||||
|
'status' => Subscriber::STATUS_SUBSCRIBED
|
||||||
|
));
|
||||||
|
SubscriberSegment::createOrUpdate(array(
|
||||||
|
'subscriber_id' => $this->subscriber->id,
|
||||||
|
'segment_id' => $this->segment_2->id,
|
||||||
|
'status' => Subscriber::STATUS_UNSUBSCRIBED
|
||||||
|
));
|
||||||
|
|
||||||
|
$subscriptions_count = SubscriberSegment::count();
|
||||||
|
expect($subscriptions_count)->equals(2);
|
||||||
|
|
||||||
|
$subscriptions_count = SubscriberSegment::filter('subscribed')->count();
|
||||||
|
expect($subscriptions_count)->equals(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
function _after() {
|
||||||
|
Segment::deleteMany();
|
||||||
|
Subscriber::deleteMany();
|
||||||
|
SubscriberSegment::deleteMany();
|
||||||
|
}
|
||||||
|
}
|
@ -423,6 +423,30 @@ class SubscriberTest extends MailPoetTest {
|
|||||||
Subscriber::getSubscribedInSegments(array(1))->findArray();
|
Subscriber::getSubscribedInSegments(array(1))->findArray();
|
||||||
expect(count($subscribed_subscribers_in_segment))->equals(2);
|
expect(count($subscribed_subscribers_in_segment))->equals(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testItCannotTrashAWPUser() {
|
||||||
|
$wp_subscriber = Subscriber::createOrUpdate(array(
|
||||||
|
'email' => 'some.wp.user@mailpoet.com',
|
||||||
|
'wp_user_id' => 1
|
||||||
|
));
|
||||||
|
expect($wp_subscriber->trash())->equals(false);
|
||||||
|
|
||||||
|
$subscriber = Subscriber::findOne($wp_subscriber->id);
|
||||||
|
expect($subscriber)->notEquals(false);
|
||||||
|
expect($subscriber->deleted_at)->equals(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItCannotDeleteAWPUser() {
|
||||||
|
$wp_subscriber = Subscriber::createOrUpdate(array(
|
||||||
|
'email' => 'some.wp.user@mailpoet.com',
|
||||||
|
'wp_user_id' => 1
|
||||||
|
));
|
||||||
|
expect($wp_subscriber->delete())->equals(false);
|
||||||
|
|
||||||
|
$subscriber = Subscriber::findOne($wp_subscriber->id);
|
||||||
|
expect($subscriber)->notEquals(false);
|
||||||
|
}
|
||||||
|
|
||||||
function _after() {
|
function _after() {
|
||||||
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
||||||
ORM::raw_execute('TRUNCATE ' . Segment::$_table);
|
ORM::raw_execute('TRUNCATE ' . Segment::$_table);
|
||||||
|
Reference in New Issue
Block a user