Remove old unused class for read subscribers
[MAILPOET-3376]
This commit is contained in:
@ -1,80 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace MailPoet\Subscribers\ImportExport\Export;
|
|
||||||
|
|
||||||
use MailPoet\Models\Segment;
|
|
||||||
use MailPoet\Models\Subscriber;
|
|
||||||
use MailPoet\Models\SubscriberSegment;
|
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets batches of subscribers from default segments.
|
|
||||||
*/
|
|
||||||
|
|
||||||
class DefaultSubscribersGetter extends SubscribersGetter {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var bool
|
|
||||||
*/
|
|
||||||
protected $getSubscribersWithoutSegment;
|
|
||||||
|
|
||||||
public function __construct($segmentsIds, $batchSize) {
|
|
||||||
parent::__construct($segmentsIds, $batchSize);
|
|
||||||
$this->getSubscribersWithoutSegment = (array_search(0, $segmentsIds) !== false);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function filter($subscribers) {
|
|
||||||
$subscribers = $subscribers
|
|
||||||
->selectMany(
|
|
||||||
[
|
|
||||||
'list_status' => SubscriberSegment::$_table . '.status',
|
|
||||||
]
|
|
||||||
)
|
|
||||||
->left_outer_join(
|
|
||||||
SubscriberSegment::$_table,
|
|
||||||
[
|
|
||||||
Subscriber::$_table . '.id',
|
|
||||||
'=',
|
|
||||||
SubscriberSegment::$_table . '.subscriber_id',
|
|
||||||
]
|
|
||||||
)
|
|
||||||
->left_outer_join(
|
|
||||||
Segment::$_table,
|
|
||||||
[
|
|
||||||
Segment::$_table . '.id',
|
|
||||||
'=',
|
|
||||||
SubscriberSegment::$_table . '.segment_id',
|
|
||||||
]
|
|
||||||
)
|
|
||||||
->groupBy(Segment::$_table . '.id');
|
|
||||||
|
|
||||||
if ($this->getSubscribersWithoutSegment !== false) {
|
|
||||||
// if there are subscribers who do not belong to any segment, use
|
|
||||||
// a CASE function to group them under "Not In Segment"
|
|
||||||
$subscribers = $subscribers
|
|
||||||
->selectExpr(
|
|
||||||
'MAX(CASE WHEN ' . Segment::$_table . '.name IS NOT NULL ' .
|
|
||||||
'THEN ' . Segment::$_table . '.name ' .
|
|
||||||
'ELSE "' . WPFunctions::get()->__('Not In Segment', 'mailpoet') . '" END) as segment_name'
|
|
||||||
)
|
|
||||||
->whereRaw(
|
|
||||||
SubscriberSegment::$_table . '.segment_id IN (' .
|
|
||||||
rtrim(str_repeat('?,', count($this->segmentsIds)), ',') . ') ' .
|
|
||||||
'OR ' . SubscriberSegment::$_table . '.segment_id IS NULL ',
|
|
||||||
$this->segmentsIds
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
// if all subscribers belong to at least one segment, select the segment name
|
|
||||||
$subscribers = $subscribers
|
|
||||||
->selectExpr('MAX(' . Segment::$_table . '.name) as segment_name')
|
|
||||||
->whereIn(SubscriberSegment::$_table . '.segment_id', $this->segmentsIds);
|
|
||||||
}
|
|
||||||
|
|
||||||
$subscribers = $subscribers
|
|
||||||
->offset($this->offset)
|
|
||||||
->limit($this->batchSize)
|
|
||||||
->findArray();
|
|
||||||
|
|
||||||
return $subscribers;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,77 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace MailPoet\Subscribers\ImportExport\Export;
|
|
||||||
|
|
||||||
use MailPoet\DI\ContainerWrapper;
|
|
||||||
use MailPoet\DynamicSegments\Persistence\Loading\SingleSegmentLoader;
|
|
||||||
use MailPoet\Models\Segment;
|
|
||||||
use MailPoet\Models\Subscriber;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets batches of subscribers from dynamic segments.
|
|
||||||
*/
|
|
||||||
class DynamicSubscribersGetter extends SubscribersGetter {
|
|
||||||
|
|
||||||
protected $segmentIndex = 0;
|
|
||||||
|
|
||||||
/** @var SingleSegmentLoader */
|
|
||||||
private $dynamicSegmentsLoader;
|
|
||||||
|
|
||||||
public function __construct($segmentsIds, $batchSize, SingleSegmentLoader $dynamicSegmentsLoader = null) {
|
|
||||||
parent::__construct($segmentsIds, $batchSize);
|
|
||||||
if ($dynamicSegmentsLoader === null) {
|
|
||||||
$dynamicSegmentsLoader = ContainerWrapper::getInstance()->get(SingleSegmentLoader::class);
|
|
||||||
}
|
|
||||||
$this->dynamicSegmentsLoader = $dynamicSegmentsLoader;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function reset() {
|
|
||||||
parent::reset();
|
|
||||||
$this->segmentIndex = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function filter($subscribers) {
|
|
||||||
$segmentId = $this->segmentsIds[$this->segmentIndex];
|
|
||||||
|
|
||||||
$filters = $this->dynamicSegmentsLoader->load($segmentId)->getFilters();
|
|
||||||
|
|
||||||
if (!is_array($filters) || empty($filters)) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
$segment = Segment::findOne($segmentId);
|
|
||||||
if (!$segment instanceof Segment) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
$name = $segment->name;
|
|
||||||
|
|
||||||
foreach ($filters as $filter) {
|
|
||||||
$subscribers = $filter->toSql($subscribers);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $subscribers
|
|
||||||
->selectMany([
|
|
||||||
'list_status' => Subscriber::$_table . '.status',
|
|
||||||
])
|
|
||||||
->selectExpr("'" . $name . "' AS segment_name")
|
|
||||||
->offset($this->offset)
|
|
||||||
->limit($this->batchSize)
|
|
||||||
->findArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function get() {
|
|
||||||
if ($this->segmentIndex >= count($this->segmentsIds)) {
|
|
||||||
$this->finished = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
$subscribers = parent::get();
|
|
||||||
|
|
||||||
if ($subscribers !== false && count($subscribers) < $this->batchSize) {
|
|
||||||
$this->segmentIndex ++;
|
|
||||||
$this->offset = 0;
|
|
||||||
$this->finished = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $subscribers;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,76 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace MailPoet\Subscribers\ImportExport\Export;
|
|
||||||
|
|
||||||
use MailPoet\Models\Subscriber;
|
|
||||||
use MailPoetVendor\Idiorm\ORM;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets batches of subscribers for export.
|
|
||||||
*/
|
|
||||||
abstract class SubscribersGetter {
|
|
||||||
|
|
||||||
protected $segmentsIds;
|
|
||||||
protected $batchSize;
|
|
||||||
protected $offset;
|
|
||||||
protected $finished;
|
|
||||||
|
|
||||||
public function __construct($segmentsIds, $batchSize) {
|
|
||||||
$this->segmentsIds = $segmentsIds;
|
|
||||||
$this->batchSize = $batchSize;
|
|
||||||
$this->reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function reset() {
|
|
||||||
$this->offset = 0;
|
|
||||||
$this->finished = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialize the query by selecting fields and ignoring trashed subscribers.
|
|
||||||
*
|
|
||||||
* @return ORM
|
|
||||||
*/
|
|
||||||
protected function select() {
|
|
||||||
return Subscriber::selectMany(
|
|
||||||
'first_name',
|
|
||||||
'last_name',
|
|
||||||
'email',
|
|
||||||
'subscribed_ip',
|
|
||||||
[
|
|
||||||
'global_status' => Subscriber::$_table . '.status',
|
|
||||||
]
|
|
||||||
)
|
|
||||||
->filter('filterWithCustomFieldsForExport')
|
|
||||||
->groupBy(Subscriber::$_table . '.id')
|
|
||||||
->whereNull(Subscriber::$_table . '.deleted_at');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filters the subscribers query based on the segments, offset and batch size.
|
|
||||||
*
|
|
||||||
* @param ORM $subscribers
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
abstract protected function filter($subscribers);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the next batch of subscribers or `false` if no more!
|
|
||||||
*/
|
|
||||||
public function get() {
|
|
||||||
if ($this->finished) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$subscribers = $this->select();
|
|
||||||
$subscribers = $this->filter($subscribers);
|
|
||||||
|
|
||||||
$this->offset += $this->batchSize;
|
|
||||||
|
|
||||||
if (count($subscribers) < $this->batchSize) {
|
|
||||||
$this->finished = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $subscribers;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,282 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace MailPoet\Test\Subscribers\ImportExport\Export;
|
|
||||||
|
|
||||||
use MailPoet\Models\CustomField;
|
|
||||||
use MailPoet\Models\Segment;
|
|
||||||
use MailPoet\Models\Subscriber;
|
|
||||||
use MailPoet\Models\SubscriberCustomField;
|
|
||||||
use MailPoet\Models\SubscriberSegment;
|
|
||||||
use MailPoet\Subscribers\ImportExport\Export\DefaultSubscribersGetter;
|
|
||||||
use MailPoetVendor\Idiorm\ORM;
|
|
||||||
|
|
||||||
class DefaultSubscribersGetterTest extends \MailPoetTest {
|
|
||||||
public $segmentsData;
|
|
||||||
public $customFieldsData;
|
|
||||||
public $subscribersData;
|
|
||||||
public $subscriberFields;
|
|
||||||
|
|
||||||
public function _before() {
|
|
||||||
parent::_before();
|
|
||||||
$this->subscriberFields = [
|
|
||||||
'first_name' => 'First name',
|
|
||||||
'last_name' => 'Last name',
|
|
||||||
'email' => 'Email',
|
|
||||||
1 => 'Country',
|
|
||||||
];
|
|
||||||
|
|
||||||
$this->subscribersData = [
|
|
||||||
[
|
|
||||||
'first_name' => 'Adam',
|
|
||||||
'last_name' => 'Smith',
|
|
||||||
'email' => 'adam@smith.com',
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'first_name' => 'Mary',
|
|
||||||
'last_name' => 'Jane',
|
|
||||||
'email' => 'mary@jane.com',
|
|
||||||
'status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
1 => 'Brazil',
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'first_name' => 'John',
|
|
||||||
'last_name' => 'Kookoo',
|
|
||||||
'email' => 'john@kookoo.com',
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'first_name' => 'Paul',
|
|
||||||
'last_name' => 'Newman',
|
|
||||||
'email' => 'paul@newman.com',
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
$this->customFieldsData = [
|
|
||||||
[
|
|
||||||
'name' => 'Country',
|
|
||||||
'type' => 'text',
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
$this->segmentsData = [
|
|
||||||
[
|
|
||||||
'name' => 'Newspapers',
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'name' => 'Journals',
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
foreach ($this->subscribersData as $subscriber) {
|
|
||||||
if (isset($subscriber[1])) {
|
|
||||||
unset($subscriber[1]);
|
|
||||||
}
|
|
||||||
$entity = Subscriber::create();
|
|
||||||
$entity->hydrate($subscriber);
|
|
||||||
$entity->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($this->segmentsData as $segment) {
|
|
||||||
$entity = Segment::create();
|
|
||||||
$entity->hydrate($segment);
|
|
||||||
$entity->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($this->customFieldsData as $customField) {
|
|
||||||
$entity = CustomField::create();
|
|
||||||
$entity->hydrate($customField);
|
|
||||||
$entity->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
$entity = SubscriberCustomField::create();
|
|
||||||
$entity->subscriberId = 2;
|
|
||||||
$entity->customFieldId = 1;
|
|
||||||
$entity->value = $this->subscribersData[1][1];
|
|
||||||
$entity->save();
|
|
||||||
$entity = SubscriberSegment::create();
|
|
||||||
$entity->subscriberId = 1;
|
|
||||||
$entity->segmentId = 1;
|
|
||||||
$entity->status = Subscriber::STATUS_UNSUBSCRIBED;
|
|
||||||
$entity->save();
|
|
||||||
$entity = SubscriberSegment::create();
|
|
||||||
$entity->subscriberId = 1;
|
|
||||||
$entity->segmentId = 2;
|
|
||||||
$entity->save();
|
|
||||||
$entity = SubscriberSegment::create();
|
|
||||||
$entity->subscriberId = 2;
|
|
||||||
$entity->segmentId = 1;
|
|
||||||
$entity->save();
|
|
||||||
$entity = SubscriberSegment::create();
|
|
||||||
$entity->subscriberId = 3;
|
|
||||||
$entity->segmentId = 2;
|
|
||||||
$entity->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function filterSubscribersData($subscribers) {
|
|
||||||
return array_map(function($subscriber) {
|
|
||||||
$data = [];
|
|
||||||
foreach ($subscriber as $key => $value) {
|
|
||||||
if (in_array($key, [
|
|
||||||
'first_name', 'last_name', 'email', 'global_status',
|
|
||||||
'status', 'list_status', 'segment_name', 1,
|
|
||||||
]))
|
|
||||||
$data[$key] = $value;
|
|
||||||
}
|
|
||||||
return $data;
|
|
||||||
}, $subscribers);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testItGetsSubscribersInOneSegment() {
|
|
||||||
$getter = new DefaultSubscribersGetter([1], 10);
|
|
||||||
$subscribers = $getter->get();
|
|
||||||
expect($this->filterSubscribersData($subscribers))->equals([
|
|
||||||
[
|
|
||||||
'first_name' => 'Adam',
|
|
||||||
'last_name' => 'Smith',
|
|
||||||
'email' => 'adam@smith.com',
|
|
||||||
'status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'global_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'list_status' => Subscriber::STATUS_UNSUBSCRIBED,
|
|
||||||
'segment_name' => 'Newspapers',
|
|
||||||
1 => null,
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'first_name' => 'Mary',
|
|
||||||
'last_name' => 'Jane',
|
|
||||||
'email' => 'mary@jane.com',
|
|
||||||
'status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'global_status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'list_status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'segment_name' => 'Newspapers',
|
|
||||||
1 => 'Brazil',
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
expect($getter->get())->equals(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testItGetsSubscribersInMultipleSegments() {
|
|
||||||
$getter = new DefaultSubscribersGetter([1, 2], 10);
|
|
||||||
$subscribers = $getter->get();
|
|
||||||
expect($this->filterSubscribersData($subscribers))->equals([
|
|
||||||
[
|
|
||||||
'first_name' => 'Adam',
|
|
||||||
'last_name' => 'Smith',
|
|
||||||
'email' => 'adam@smith.com',
|
|
||||||
'status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'global_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'list_status' => Subscriber::STATUS_UNSUBSCRIBED,
|
|
||||||
'segment_name' => 'Newspapers',
|
|
||||||
1 => null,
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'first_name' => 'Adam',
|
|
||||||
'last_name' => 'Smith',
|
|
||||||
'email' => 'adam@smith.com',
|
|
||||||
'status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'global_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'list_status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'segment_name' => 'Journals',
|
|
||||||
1 => null,
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'first_name' => 'Mary',
|
|
||||||
'last_name' => 'Jane',
|
|
||||||
'email' => 'mary@jane.com',
|
|
||||||
'status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'global_status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'list_status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'segment_name' => 'Newspapers',
|
|
||||||
1 => 'Brazil',
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'first_name' => 'John',
|
|
||||||
'last_name' => 'Kookoo',
|
|
||||||
'email' => 'john@kookoo.com',
|
|
||||||
'status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'global_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'list_status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'segment_name' => 'Journals',
|
|
||||||
1 => null,
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
expect($getter->get())->equals(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testItGetsSubscribersInBatches() {
|
|
||||||
$getter = new DefaultSubscribersGetter([1, 2], 2);
|
|
||||||
expect($this->filterSubscribersData($getter->get()))->equals([
|
|
||||||
[
|
|
||||||
'first_name' => 'Adam',
|
|
||||||
'last_name' => 'Smith',
|
|
||||||
'email' => 'adam@smith.com',
|
|
||||||
'status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'global_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'list_status' => Subscriber::STATUS_UNSUBSCRIBED,
|
|
||||||
'segment_name' => 'Newspapers',
|
|
||||||
1 => null,
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'first_name' => 'Adam',
|
|
||||||
'last_name' => 'Smith',
|
|
||||||
'email' => 'adam@smith.com',
|
|
||||||
'status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'global_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'list_status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'segment_name' => 'Journals',
|
|
||||||
1 => null,
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
expect($this->filterSubscribersData($getter->get()))->equals([
|
|
||||||
[
|
|
||||||
'first_name' => 'Mary',
|
|
||||||
'last_name' => 'Jane',
|
|
||||||
'email' => 'mary@jane.com',
|
|
||||||
'status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'global_status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'list_status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'segment_name' => 'Newspapers',
|
|
||||||
1 => 'Brazil',
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'first_name' => 'John',
|
|
||||||
'last_name' => 'Kookoo',
|
|
||||||
'email' => 'john@kookoo.com',
|
|
||||||
'status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'global_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'list_status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'segment_name' => 'Journals',
|
|
||||||
1 => null,
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
expect($getter->get())->equals([]);
|
|
||||||
expect($getter->get())->equals(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testItGetsSubscribersWithoutSegment() {
|
|
||||||
$getter = new DefaultSubscribersGetter([0], 10);
|
|
||||||
$subscribers = $getter->get();
|
|
||||||
expect($this->filterSubscribersData($subscribers))->equals([
|
|
||||||
[
|
|
||||||
'first_name' => 'Paul',
|
|
||||||
'last_name' => 'Newman',
|
|
||||||
'email' => 'paul@newman.com',
|
|
||||||
'status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'global_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'list_status' => null,
|
|
||||||
'segment_name' => 'Not In Segment',
|
|
||||||
1 => null,
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function _after() {
|
|
||||||
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
|
||||||
ORM::raw_execute('TRUNCATE ' . Segment::$_table);
|
|
||||||
ORM::raw_execute('TRUNCATE ' . SubscriberSegment::$_table);
|
|
||||||
ORM::raw_execute('TRUNCATE ' . CustomField::$_table);
|
|
||||||
ORM::raw_execute('TRUNCATE ' . SubscriberCustomField::$_table);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,291 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace MailPoet\Test\Subscribers\ImportExport\Export;
|
|
||||||
|
|
||||||
use MailPoet\DynamicSegments\Persistence\Loading\SingleSegmentLoader;
|
|
||||||
use MailPoet\Models\CustomField;
|
|
||||||
use MailPoet\Models\DynamicSegment;
|
|
||||||
use MailPoet\Models\Segment;
|
|
||||||
use MailPoet\Models\Subscriber;
|
|
||||||
use MailPoet\Models\SubscriberCustomField;
|
|
||||||
use MailPoet\Subscribers\ImportExport\Export\DynamicSubscribersGetter;
|
|
||||||
use MailPoetVendor\Idiorm\ORM;
|
|
||||||
use PHPUnit\Framework\MockObject\MockObject;
|
|
||||||
|
|
||||||
class DynamicSubscribersGetterTest extends \MailPoetTest {
|
|
||||||
public $segmentsData;
|
|
||||||
public $customFieldsData;
|
|
||||||
public $subscribersData;
|
|
||||||
public $subscriberFields;
|
|
||||||
|
|
||||||
/** @var SingleSegmentLoader & MockObject */
|
|
||||||
private $singleSegmentLoader;
|
|
||||||
|
|
||||||
public function _before() {
|
|
||||||
parent::_before();
|
|
||||||
$this->subscriberFields = [
|
|
||||||
'first_name' => 'First name',
|
|
||||||
'last_name' => 'Last name',
|
|
||||||
'email' => 'Email',
|
|
||||||
1 => 'Country',
|
|
||||||
];
|
|
||||||
|
|
||||||
$this->subscribersData = [
|
|
||||||
[
|
|
||||||
'first_name' => 'Adam',
|
|
||||||
'last_name' => 'Smith',
|
|
||||||
'email' => 'adam@smith.com',
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'first_name' => 'Mary',
|
|
||||||
'last_name' => 'Jane',
|
|
||||||
'email' => 'mary@jane.com',
|
|
||||||
'status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
1 => 'Brazil',
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'first_name' => 'John',
|
|
||||||
'last_name' => 'Kookoo',
|
|
||||||
'email' => 'john@kookoo.com',
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'first_name' => 'Paul',
|
|
||||||
'last_name' => 'Newman',
|
|
||||||
'email' => 'paul@newman.com',
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
$this->customFieldsData = [
|
|
||||||
[
|
|
||||||
'name' => 'Country',
|
|
||||||
'type' => 'text',
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
$this->segmentsData = [
|
|
||||||
[
|
|
||||||
'name' => 'Newspapers',
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'name' => 'Journals',
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
foreach ($this->subscribersData as $subscriber) {
|
|
||||||
if (isset($subscriber[1])) {
|
|
||||||
unset($subscriber[1]);
|
|
||||||
}
|
|
||||||
$entity = Subscriber::create();
|
|
||||||
$entity->hydrate($subscriber);
|
|
||||||
$entity->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($this->customFieldsData as $customField) {
|
|
||||||
$entity = CustomField::create();
|
|
||||||
$entity->hydrate($customField);
|
|
||||||
$entity->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($this->segmentsData as $segment) {
|
|
||||||
$entity = Segment::create();
|
|
||||||
$entity->hydrate($segment);
|
|
||||||
$entity->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
$entity = SubscriberCustomField::create();
|
|
||||||
$entity->subscriberId = 2;
|
|
||||||
$entity->customFieldId = 1;
|
|
||||||
$entity->value = $this->subscribersData[1][1];
|
|
||||||
$entity->save();
|
|
||||||
|
|
||||||
$this->singleSegmentLoader = $this->createMock(SingleSegmentLoader::class);
|
|
||||||
$this->singleSegmentLoader->method('load')
|
|
||||||
->willReturnCallback(function($segmentId) {
|
|
||||||
$segment = DynamicSegment::create();
|
|
||||||
$segment->name = 'Dynamic';
|
|
||||||
if ($segmentId == 1) {
|
|
||||||
$segment->setFilters([new \DynamicSegmentFilter([1, 2])]);
|
|
||||||
} else if ($segmentId == 2) {
|
|
||||||
$segment->setFilters([new \DynamicSegmentFilter([1, 3, 4])]);
|
|
||||||
}
|
|
||||||
return $segment;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function filterSubscribersData($subscribers) {
|
|
||||||
return array_map(function($subscriber) {
|
|
||||||
$data = [];
|
|
||||||
foreach ($subscriber as $key => $value) {
|
|
||||||
if (in_array($key, [
|
|
||||||
'first_name', 'last_name', 'email', 'global_status',
|
|
||||||
'status', 'list_status', 'segment_name', 1,
|
|
||||||
]))
|
|
||||||
$data[$key] = $value;
|
|
||||||
}
|
|
||||||
return $data;
|
|
||||||
}, $subscribers);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testItGetsSubscribersInOneSegment() {
|
|
||||||
$getter = new DynamicSubscribersGetter([1], 10, $this->singleSegmentLoader);
|
|
||||||
$subscribers = $getter->get();
|
|
||||||
expect($this->filterSubscribersData($subscribers))->equals([
|
|
||||||
[
|
|
||||||
'first_name' => 'Adam',
|
|
||||||
'last_name' => 'Smith',
|
|
||||||
'email' => 'adam@smith.com',
|
|
||||||
'status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'global_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'list_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'segment_name' => 'Newspapers',
|
|
||||||
1 => null,
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'first_name' => 'Mary',
|
|
||||||
'last_name' => 'Jane',
|
|
||||||
'email' => 'mary@jane.com',
|
|
||||||
'status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'global_status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'list_status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'segment_name' => 'Newspapers',
|
|
||||||
1 => 'Brazil',
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
expect($getter->get())->equals(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testItGetsSubscribersInMultipleSegments() {
|
|
||||||
$getter = new DynamicSubscribersGetter([1, 2], 10, $this->singleSegmentLoader);
|
|
||||||
expect($this->filterSubscribersData($getter->get()))->equals([
|
|
||||||
[
|
|
||||||
'first_name' => 'Adam',
|
|
||||||
'last_name' => 'Smith',
|
|
||||||
'email' => 'adam@smith.com',
|
|
||||||
'status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'global_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'list_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'segment_name' => 'Newspapers',
|
|
||||||
1 => null,
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'first_name' => 'Mary',
|
|
||||||
'last_name' => 'Jane',
|
|
||||||
'email' => 'mary@jane.com',
|
|
||||||
'status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'global_status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'list_status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'segment_name' => 'Newspapers',
|
|
||||||
1 => 'Brazil',
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
expect($this->filterSubscribersData($getter->get()))->equals([
|
|
||||||
[
|
|
||||||
'first_name' => 'Adam',
|
|
||||||
'last_name' => 'Smith',
|
|
||||||
'email' => 'adam@smith.com',
|
|
||||||
'status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'global_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'list_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'segment_name' => 'Journals',
|
|
||||||
1 => null,
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'first_name' => 'John',
|
|
||||||
'last_name' => 'Kookoo',
|
|
||||||
'email' => 'john@kookoo.com',
|
|
||||||
'status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'global_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'list_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'segment_name' => 'Journals',
|
|
||||||
1 => null,
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'first_name' => 'Paul',
|
|
||||||
'last_name' => 'Newman',
|
|
||||||
'email' => 'paul@newman.com',
|
|
||||||
'status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'global_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'list_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'segment_name' => 'Journals',
|
|
||||||
1 => null,
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
expect($getter->get())->equals(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testItGetsSubscribersInBatches() {
|
|
||||||
$getter = new DynamicSubscribersGetter([1, 2], 2, $this->singleSegmentLoader);
|
|
||||||
expect($this->filterSubscribersData($getter->get()))->equals([
|
|
||||||
[
|
|
||||||
'first_name' => 'Adam',
|
|
||||||
'last_name' => 'Smith',
|
|
||||||
'email' => 'adam@smith.com',
|
|
||||||
'status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'global_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'list_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'segment_name' => 'Newspapers',
|
|
||||||
1 => null,
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'first_name' => 'Mary',
|
|
||||||
'last_name' => 'Jane',
|
|
||||||
'email' => 'mary@jane.com',
|
|
||||||
'status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'global_status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'list_status' => Subscriber::STATUS_SUBSCRIBED,
|
|
||||||
'segment_name' => 'Newspapers',
|
|
||||||
1 => 'Brazil',
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
expect($this->filterSubscribersData($getter->get()))->equals([]);
|
|
||||||
|
|
||||||
expect($this->filterSubscribersData($getter->get()))->equals([
|
|
||||||
[
|
|
||||||
'first_name' => 'Adam',
|
|
||||||
'last_name' => 'Smith',
|
|
||||||
'email' => 'adam@smith.com',
|
|
||||||
'status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'global_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'list_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'segment_name' => 'Journals',
|
|
||||||
1 => null,
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'first_name' => 'John',
|
|
||||||
'last_name' => 'Kookoo',
|
|
||||||
'email' => 'john@kookoo.com',
|
|
||||||
'status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'global_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'list_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'segment_name' => 'Journals',
|
|
||||||
1 => null,
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
expect($this->filterSubscribersData($getter->get()))->equals([
|
|
||||||
[
|
|
||||||
'first_name' => 'Paul',
|
|
||||||
'last_name' => 'Newman',
|
|
||||||
'email' => 'paul@newman.com',
|
|
||||||
'status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'global_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'list_status' => Subscriber::STATUS_UNCONFIRMED,
|
|
||||||
'segment_name' => 'Journals',
|
|
||||||
1 => null,
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
expect($getter->get())->equals(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function _after() {
|
|
||||||
ORM::raw_execute('TRUNCATE ' . Subscriber::$_table);
|
|
||||||
ORM::raw_execute('TRUNCATE ' . Segment::$_table);
|
|
||||||
ORM::raw_execute('TRUNCATE ' . CustomField::$_table);
|
|
||||||
ORM::raw_execute('TRUNCATE ' . SubscriberCustomField::$_table);
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user