Added unit tests for SubscriberSegment / Subscriber models

This commit is contained in:
Jonathan Labreuille
2016-05-24 17:35:44 +02:00
parent da755b7902
commit 4fa8a650b8
5 changed files with 251 additions and 22 deletions

View File

@@ -184,7 +184,8 @@ class Segment extends Model {
$segment->set($data);
}
return $segment->save();
$segment->save();
return $segment;
}
static function getPublic() {

View File

@@ -47,7 +47,7 @@ class Subscriber extends Model {
return false;
} else {
// delete all relations to segments
SubscriberSegment::where('subscriber_id', $this->id)->deleteMany();
SubscriberSegment::deleteSubscriptions($this);
return parent::delete();
}
@@ -590,14 +590,14 @@ class Subscriber extends Model {
}
static function bulkTrash($orm) {
return parent::bulkAction($orm, function($ids) {
return parent::bulkAction($orm, function($subscriber_ids) use($orm) {
parent::rawExecute(join(' ', array(
'UPDATE `'.self::$_table.'`',
'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'
)),
$ids
$subscriber_ids
);
});
}
@@ -605,7 +605,7 @@ class Subscriber extends Model {
static function bulkDelete($orm) {
return parent::bulkAction($orm, function($subscriber_ids) {
// delete all subscriber/segment relationships
SubscriberSegment::deleteSubscriptionsForAll($subscriber_ids);
SubscriberSegment::deleteManySubscriptions($subscriber_ids);
// delete subscribers (except WP Users)
Subscriber::whereIn('id', $subscriber_ids)

View File

@@ -41,7 +41,6 @@ class SubscriberSegment extends Model {
));
}
}
return true;
} else {
// unsubscribe from all segments (except the WP users segment)
$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)
->save();
}
return true;
}
return false;
}
@@ -73,7 +73,6 @@ class SubscriberSegment extends Model {
));
}
}
return true;
} else {
// subscribe to all segments
@@ -91,10 +90,12 @@ class SubscriberSegment extends Model {
return self::subscribeToSegments($subscriber, $segment_ids);
}
static function deleteSubscriptionsForAll($subscriber_ids = array()) {
static function deleteManySubscriptions($subscriber_ids = array()) {
if(!empty($subscriber_ids)) {
// 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();
if($wp_segment !== false) {
@@ -109,17 +110,8 @@ class SubscriberSegment extends Model {
static function deleteSubscriptions($subscriber) {
if($subscriber !== false && $subscriber->id > 0) {
// delete all relationships to segments (except the WP users segment)
$subscriptions = self::where('subscriber_id', $subscriber->id);
$wp_segment = Segment::getWPSegment();
if($wp_segment !== false) {
$subscriptions = $subscriptions->whereNotEqual(
'segment_id', $wp_segment->id
);
}
return $subscriptions->delete();
// delete all relationships to segments
return self::where('subscriber_id', $subscriber->id)->deleteMany();
}
return false;
}
@@ -152,6 +144,7 @@ class SubscriberSegment extends Model {
return $subscription->save();
}
// TO BE REVIEWED
static function createMultiple($segmnets, $subscribers) {
$values = Helpers::flattenArray(
array_map(function ($segment) use ($subscribers) {