fixed import/export unit tests + added specific method for getting segments for import

This commit is contained in:
Jonathan Labreuille
2016-09-30 15:52:17 +02:00
parent d32abff78d
commit b0ab9e0408
5 changed files with 37 additions and 13 deletions

View File

@@ -530,15 +530,11 @@ define(
width: '20em', width: '20em',
templateResult: function (item) { templateResult: function (item) {
item.subscriberCount = parseInt(item.subscriberCount); item.subscriberCount = parseInt(item.subscriberCount);
return (item.subscriberCount > 0) return item.name + ' (' + item.subscriberCount.toLocaleString() + ')';
? item.name + ' (' + item.subscriberCount.toLocaleString() + ')'
: item.name;
}, },
templateSelection: function (item) { templateSelection: function (item) {
item.subscriberCount = parseInt(item.subscriberCount); item.subscriberCount = parseInt(item.subscriberCount);
return (item.subscriberCount > 0) return item.name + ' (' + item.subscriberCount.toLocaleString() + ')';
? item.name + ' (' + item.subscriberCount.toLocaleString() + ')'
: item.name;
} }
}) })
.change(function () { .change(function () {

View File

@@ -169,6 +169,34 @@ class Segment extends Model {
return $query->findArray(); return $query->findArray();
} }
static function getSegmentsForImport() {
$query = self::selectMany(array(self::$_table.'.id', self::$_table.'.name'))
->selectExpr(
self::$_table.'.*, ' .
'COUNT(IF('.
MP_SUBSCRIBER_SEGMENT_TABLE.'.status="'.Subscriber::STATUS_SUBSCRIBED.'"'
.' AND '.
MP_SUBSCRIBERS_TABLE.'.deleted_at IS NULL'
.', 1, NULL)) `subscribers`'
)
->leftOuterJoin(
MP_SUBSCRIBER_SEGMENT_TABLE,
array(self::$_table.'.id', '=', MP_SUBSCRIBER_SEGMENT_TABLE.'.segment_id'))
->leftOuterJoin(
MP_SUBSCRIBERS_TABLE,
array(MP_SUBSCRIBER_SEGMENT_TABLE.'.subscriber_id', '=', MP_SUBSCRIBERS_TABLE.'.id'))
->groupBy(self::$_table.'.id')
->groupBy(self::$_table.'.name')
->orderByAsc(self::$_table.'.name')
->whereNull(self::$_table.'.deleted_at');
if(!empty($type)) {
$query->where(self::$_table.'.type', $type);
}
return $query->findArray();
}
static function getSegmentsForExport($withConfirmedSubscribers = false) { static function getSegmentsForExport($withConfirmedSubscribers = false) {
return self::raw_query( return self::raw_query(
'(SELECT segments.id, segments.name, COUNT(relation.subscriber_id) as subscribers ' . '(SELECT segments.id, segments.name, COUNT(relation.subscriber_id) as subscribers ' .

View File

@@ -14,7 +14,7 @@ class ImportExportFactory {
function getSegments($with_confirmed_subscribers = false) { function getSegments($with_confirmed_subscribers = false) {
$segments = ($this->action === 'import') ? $segments = ($this->action === 'import') ?
Segment::getSegmentsWithSubscriberCount() : Segment::getSegmentsForImport() :
Segment::getSegmentsForExport($with_confirmed_subscribers); Segment::getSegmentsForExport($with_confirmed_subscribers);
return array_map(function($segment) { return array_map(function($segment) {
if(!$segment['name']) $segment['name'] = __('Not In List'); if(!$segment['name']) $segment['name'] = __('Not In List');

View File

@@ -18,13 +18,13 @@ class SegmentTest extends MailPoetTest {
array( array(
'first_name' => 'John', 'first_name' => 'John',
'last_name' => 'Mailer', 'last_name' => 'Mailer',
'status' => 'unsubscribed', 'status' => Subscriber::STATUS_UNSUBSCRIBED,
'email' => 'john@mailpoet.com' 'email' => 'john@mailpoet.com'
), ),
array( array(
'first_name' => 'Mike', 'first_name' => 'Mike',
'last_name' => 'Smith', 'last_name' => 'Smith',
'status' => 'subscribed', 'status' => Subscriber::STATUS_SUBSCRIBED,
'email' => 'mike@maipoet.com' 'email' => 'mike@maipoet.com'
) )
); );
@@ -168,8 +168,8 @@ class SegmentTest extends MailPoetTest {
$association->segment_id = $this->segment->id; $association->segment_id = $this->segment->id;
$association->save(); $association->save();
} }
$segment = Segment::getSegmentsWithSubscriberCount(); $segments = Segment::getSegmentsWithSubscriberCount();
expect($segment[0]['subscribers'])->equals(2); expect($segments[0]['subscribers'])->equals(1);
} }
function testItCanGetSegmentsForExport() { function testItCanGetSegmentsForExport() {

View File

@@ -14,14 +14,14 @@ class ImportExportFactoryTest extends MailPoetTest {
$subscriber_1 = Subscriber::createOrUpdate(array( $subscriber_1 = Subscriber::createOrUpdate(array(
'first_name' => 'John', 'first_name' => 'John',
'last_name' => 'Mailer', 'last_name' => 'Mailer',
'status' => 'unconfirmed', 'status' => Subscriber::STATUS_UNCONFIRMED,
'email' => 'john@mailpoet.com' 'email' => 'john@mailpoet.com'
)); ));
$subscriber_2 = Subscriber::createOrUpdate(array( $subscriber_2 = Subscriber::createOrUpdate(array(
'first_name' => 'Mike', 'first_name' => 'Mike',
'last_name' => 'Smith', 'last_name' => 'Smith',
'status' => 'subscribed', 'status' => Subscriber::STATUS_SUBSCRIBED,
'email' => 'mike@maipoet.com' 'email' => 'mike@maipoet.com'
)); ));