- Cleans up import
- Adds tests for modified models - Adds tests for import BootStrapMenu and MailChimp classes
This commit is contained in:
6
assets/css/src/import.styl
vendored
6
assets/css/src/import.styl
vendored
@ -71,6 +71,8 @@ tr
|
||||
& > a
|
||||
margin-left 15px
|
||||
|
||||
span
|
||||
&.select2-search
|
||||
&.select2-search--dropdown
|
||||
display none !important
|
||||
|
||||
.select2-dropdown--below
|
||||
display none
|
||||
|
@ -16,7 +16,7 @@ define(
|
||||
Handlebars,
|
||||
Papa
|
||||
) {
|
||||
if (typeof(importData) === 'undefined') {
|
||||
if (!jQuery("#mailpoet_subscribers_import").length) {
|
||||
return;
|
||||
}
|
||||
jQuery(document).ready(function () {
|
||||
@ -182,7 +182,7 @@ define(
|
||||
}).done(function (request) {
|
||||
if (request.result === false) {
|
||||
MailPoet.Notice.hide();
|
||||
MailPoet.Notice.error(request.message);
|
||||
MailPoet.Notice.error(request.error);
|
||||
jQuery('.mailpoet_mailchimp-key-status')
|
||||
.removeClass()
|
||||
.addClass('mailpoet_mailchimp-key-status mailpoet_mailchimp-error');
|
||||
|
@ -31,14 +31,15 @@ class BootstrapMenu {
|
||||
'name' => $segment['name'],
|
||||
'subscriberCount' => $segment['subscribers']
|
||||
);
|
||||
}, Segment::filter('filterWithSubscriberCount')->findArray());
|
||||
}, Segment::filter('filterWithSubscriberCount')
|
||||
->findArray());
|
||||
}
|
||||
|
||||
function getSubscriberCustomFields() {
|
||||
return CustomField::findArray();
|
||||
}
|
||||
|
||||
function formatSubscriberFields() {
|
||||
function formatSubscriberFields($subscriberFields) {
|
||||
return array_map(function ($fieldId, $fieldName) {
|
||||
return array(
|
||||
'id' => $fieldId,
|
||||
@ -46,22 +47,23 @@ class BootstrapMenu {
|
||||
'type' => ($fieldId === 's_confirmed_at') ? 'date' : null,
|
||||
'custom' => false
|
||||
);
|
||||
}, array_keys($this->subscriberFields), $this->subscriberFields);
|
||||
}, array_keys($subscriberFields), $subscriberFields);
|
||||
}
|
||||
|
||||
function formatSubscriberCustomFields() {
|
||||
function formatSubscriberCustomFields($subscriberCustomFields) {
|
||||
return array_map(function ($field) {
|
||||
return array(
|
||||
'id' => $field['id'],
|
||||
'name' => $field['name'],
|
||||
'label' => $field['name'],
|
||||
'type' => $field['type'],
|
||||
'custom' => true
|
||||
);
|
||||
}, $this->subscriberCustomFields);
|
||||
}, $subscriberCustomFields);
|
||||
}
|
||||
|
||||
function formatSubscriberFieldsSelect2() {
|
||||
function formatFieldsForSelect2(
|
||||
$subscriberFields,
|
||||
$subscriberCustomFields) {
|
||||
$select2Fields = array(
|
||||
array(
|
||||
'name' => __('Actions'),
|
||||
@ -78,13 +80,15 @@ class BootstrapMenu {
|
||||
),
|
||||
array(
|
||||
'name' => __('System columns'),
|
||||
'children' => $this->formatSubscriberFields()
|
||||
'children' => $this->formatSubscriberFields($subscriberFields)
|
||||
)
|
||||
);
|
||||
if($this->subscriberCustomFields) {
|
||||
array_push($select2Fields, array(
|
||||
'name' => __('User columns'),
|
||||
'children' => $this->formatSubscriberCustomFields()
|
||||
'children' => $this->formatSubscriberCustomFields(
|
||||
$subscriberCustomFields
|
||||
)
|
||||
));
|
||||
}
|
||||
return $select2Fields;
|
||||
@ -92,14 +96,14 @@ class BootstrapMenu {
|
||||
|
||||
function bootstrap() {
|
||||
$data['segments'] = $this->segments;
|
||||
|
||||
$data['subscriberFields'] = array_merge(
|
||||
$this->formatSubscriberFields(),
|
||||
$this->formatSubscriberCustomFields()
|
||||
$this->formatSubscriberFields($this->subscriberFields),
|
||||
$this->formatSubscriberCustomFields($this->subscriberCustomFields)
|
||||
);
|
||||
$data['subscriberFieldsSelect2'] = $this->formatFieldsForSelect2(
|
||||
$this->subscriberFields,
|
||||
$this->subscriberCustomFields
|
||||
);
|
||||
|
||||
$data['subscriberFieldsSelect2'] = $this->formatSubscriberFieldsSelect2();
|
||||
|
||||
$data = array_map('json_encode', $data);
|
||||
$data['maxPostSizeBytes'] = Helpers::getMaxPostSize('bytes');
|
||||
$data['maxPostSize'] = Helpers::getMaxPostSize();
|
||||
|
@ -4,16 +4,15 @@ namespace MailPoet\Import;
|
||||
use MailPoet\Util\Helpers;
|
||||
|
||||
class MailChimp {
|
||||
public function __construct($APIKey, $lists = false) {
|
||||
function __construct($APIKey, $lists = false) {
|
||||
$this->APIKey = $this->getAPIKey($APIKey);
|
||||
$this->maxPostSize = Helpers::getMaxPostSize('bytes');
|
||||
$this->dataCenter = $this->getDataCenter();
|
||||
$this->lists = $lists;
|
||||
$this->dataCenter = $this->getDataCenter($this->APIKey);
|
||||
$this->listsURL = 'https://%s.api.mailchimp.com/2.0/lists/list?apikey=%s';
|
||||
$this->exportURL = 'https://%s.api.mailchimp.com/export/1.0/list/?apikey=%s&id=%s';
|
||||
}
|
||||
|
||||
public function getLists() {
|
||||
function getLists() {
|
||||
if(!$this->APIKey || !$this->dataCenter) {
|
||||
return $this->processError('API');
|
||||
}
|
||||
@ -52,17 +51,17 @@ class MailChimp {
|
||||
);
|
||||
}
|
||||
|
||||
public function getSubscribers() {
|
||||
function getSubscribers($lists = array()) {
|
||||
if(!$this->APIKey || !$this->dataCenter) {
|
||||
return $this->processError('API');
|
||||
}
|
||||
|
||||
if(!is_array($this->lists)) {
|
||||
if(!$lists) {
|
||||
return $this->processError('lists');
|
||||
}
|
||||
|
||||
$bytesFetched = 0;
|
||||
foreach ($this->lists as $list) {
|
||||
foreach ($lists as $list) {
|
||||
$url = sprintf($this->exportURL, $this->dataCenter, $this->APIKey, $list);
|
||||
$connection = @fopen($url, 'r');
|
||||
if(!$connection) {
|
||||
@ -77,7 +76,7 @@ class MailChimp {
|
||||
if($i === 0) {
|
||||
$header = $obj;
|
||||
if(is_object($header) && isset($header->error)) {
|
||||
return $this->processError('API');
|
||||
return $this->processError('lists');
|
||||
}
|
||||
if(!isset($headerHash)) {
|
||||
$headerHash = md5(implode(',', $header));
|
||||
@ -119,39 +118,39 @@ class MailChimp {
|
||||
);
|
||||
}
|
||||
|
||||
private function getDataCenter() {
|
||||
function getDataCenter($APIKey) {
|
||||
// double parantheses: http://phpsadness.com/sad/51
|
||||
return ($this->APIKey) ? end((explode('-', $this->APIKey))) : false;
|
||||
return ($APIKey) ? end((explode('-', $APIKey))) : false;
|
||||
}
|
||||
|
||||
private function getAPIKey($APIKey) {
|
||||
function getAPIKey($APIKey) {
|
||||
return (preg_match('/[a-zA-Z0-9]{32}-[a-zA-Z0-9]{3,}/', $APIKey)) ? $APIKey : false;
|
||||
}
|
||||
|
||||
private function processError($error) {
|
||||
function processError($error) {
|
||||
switch ($error) {
|
||||
case 'API':
|
||||
$message = __('Invalid API key.');
|
||||
$errorMessage = __('Invalid API key.');
|
||||
break;
|
||||
case 'connection':
|
||||
$message = __('Could not connect to your MailChimp account.');
|
||||
$errorMessage = __('Could not connect to your MailChimp account.');
|
||||
break;
|
||||
case 'headers':
|
||||
$message = __('The selected lists do not have matching columns (headers).');
|
||||
$errorMessage = __('The selected lists do not have matching columns (headers).');
|
||||
break;
|
||||
case 'size':
|
||||
$message = __('Information received from MailChimp is too large for processing. Please limit the number of lists.');
|
||||
$errorMessage = __('Information received from MailChimp is too large for processing. Please limit the number of lists.');
|
||||
break;
|
||||
case 'subscribers':
|
||||
$message = __('Did not find any active subscribers.');
|
||||
$errorMessage = __('Did not find any active subscribers.');
|
||||
break;
|
||||
case 'lists':
|
||||
$message = __('Did not find any valid lists');
|
||||
$errorMessage = __('Did not find any valid lists');
|
||||
break;
|
||||
}
|
||||
return array(
|
||||
'status' => 'error',
|
||||
'message' => $message
|
||||
'result' => false,
|
||||
'error' => $errorMessage
|
||||
);
|
||||
}
|
||||
}
|
@ -318,11 +318,12 @@ class Subscriber extends Model {
|
||||
);
|
||||
}
|
||||
|
||||
static function updateMultiple($columns, $subscribers, $currentTime) {
|
||||
static function updateMultiple($columns, $subscribers, $currentTime = false) {
|
||||
$ignoreColumnsOnUpdate = array(
|
||||
'email',
|
||||
'created_at'
|
||||
);
|
||||
$subscribers = array_map('array_values', $subscribers);
|
||||
$emailPosition = array_search('email', $columns);
|
||||
$sql =
|
||||
function ($type) use (
|
||||
@ -356,10 +357,10 @@ class Subscriber extends Model {
|
||||
};
|
||||
return self::rawExecute(
|
||||
'UPDATE `' . self::$_table . '` ' .
|
||||
'SET ' . implode(', ', $sql('statement')) . ', ' .
|
||||
'updated_at = "' . $currentTime . '" ' .
|
||||
'WHERE email IN ' .
|
||||
'(' . rtrim(str_repeat('?,', count($subscribers)), ',') . ')',
|
||||
'SET ' . implode(', ', $sql('statement')) . ' '.
|
||||
(($currentTime) ? ', updated_at = "' . $currentTime . '" ' : '') .
|
||||
'WHERE email IN ' .
|
||||
'(' . rtrim(str_repeat('?,', count($subscribers)), ',') . ')',
|
||||
array_merge(
|
||||
Helpers::flattenArray($sql('values')),
|
||||
Helpers::arrayColumn($subscribers, $emailPosition)
|
||||
|
@ -13,6 +13,7 @@ class SubscriberCustomField extends Model {
|
||||
}
|
||||
|
||||
static function createMultiple($values) {
|
||||
$values = array_map('array_values', $values);
|
||||
return self::rawExecute(
|
||||
'INSERT IGNORE INTO `' . self::$_table . '` ' .
|
||||
'(custom_field_id, subscriber_id, value) ' .
|
||||
@ -26,23 +27,24 @@ class SubscriberCustomField extends Model {
|
||||
);
|
||||
}
|
||||
|
||||
static function updateMultiple($subscribers) {
|
||||
self::createMultiple($subscribers);
|
||||
static function updateMultiple($values) {
|
||||
self::createMultiple($values);
|
||||
$values = array_map('array_values', $values);
|
||||
self::rawExecute(
|
||||
'UPDATE `' . self::$_table . '` ' .
|
||||
'SET value = ' .
|
||||
'(CASE ' .
|
||||
str_repeat(
|
||||
'WHEN custom_field_id = ? AND subscriber_id = ? THEN ? ',
|
||||
count($subscribers)
|
||||
count($values)
|
||||
) .
|
||||
'END) ' .
|
||||
'WHERE subscriber_id IN (' .
|
||||
implode(', ', Helpers::arrayColumn($subscribers, 1)) .
|
||||
implode(', ', Helpers::arrayColumn($values, 1)) .
|
||||
') AND custom_field_id IN (' .
|
||||
implode(', ', array_unique(Helpers::arrayColumn($subscribers, 0)))
|
||||
implode(', ', array_unique(Helpers::arrayColumn($values, 0)))
|
||||
. ') ',
|
||||
Helpers::flattenArray($subscribers)
|
||||
Helpers::flattenArray($values)
|
||||
);
|
||||
}
|
||||
}
|
@ -14,8 +14,8 @@ class Import {
|
||||
}
|
||||
|
||||
function getMailChimpSubscribers($data) {
|
||||
$mailChimp = new MailChimp($data['api_key'], $data['lists']);
|
||||
wp_send_json($mailChimp->getSubscribers());
|
||||
$mailChimp = new MailChimp($data['api_key']);
|
||||
wp_send_json($mailChimp->getSubscribers($data['lists']));
|
||||
}
|
||||
|
||||
function addSegment($data) {
|
||||
|
@ -76,17 +76,13 @@ class Helpers {
|
||||
static function getMaxPostSize($bytes = false) {
|
||||
$maxPostSize = ini_get('post_max_size');
|
||||
if(!$bytes) return $maxPostSize;
|
||||
$maxPostSizeBytes = (int) $maxPostSize;
|
||||
$unit = strtolower($maxPostSize[strlen($maxPostSize) - 1]);
|
||||
switch ($unit) {
|
||||
case 'g':
|
||||
$maxPostSizeBytes *= 1024;
|
||||
case 'm':
|
||||
$maxPostSizeBytes *= 1024;
|
||||
case 'k':
|
||||
$maxPostSizeBytes *= 1024;
|
||||
switch (substr ($maxPostSize, -1))
|
||||
{
|
||||
case 'M': case 'm': return (int)$maxPostSize * 1048576;
|
||||
case 'K': case 'k': return (int)$maxPostSize * 1024;
|
||||
case 'G': case 'g': return (int)$maxPostSize * 1073741824;
|
||||
default: return $maxPostSize;
|
||||
}
|
||||
return $maxPostSizeBytes;
|
||||
}
|
||||
|
||||
static function flattenArray($array) {
|
||||
|
199
tests/unit/Import/BootStrapMenuCest.php
Normal file
199
tests/unit/Import/BootStrapMenuCest.php
Normal file
@ -0,0 +1,199 @@
|
||||
<?php
|
||||
|
||||
use MailPoet\Import\BootstrapMenu;
|
||||
use MailPoet\Models\CustomField;
|
||||
use MailPoet\Models\Segment;
|
||||
use MailPoet\Models\Subscriber;
|
||||
use MailPoet\Models\SubscriberSegment;
|
||||
|
||||
class BootStrapMenuCest {
|
||||
function _before() {
|
||||
$this->segmentsData = array(
|
||||
array(
|
||||
'name' => 'first',
|
||||
'description' => 'some description'
|
||||
),
|
||||
array(
|
||||
'name' => 'second',
|
||||
'description' => 'some description'
|
||||
)
|
||||
);
|
||||
$this->subscribersData = array(
|
||||
array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Mailer',
|
||||
'email' => 'john@mailpoet.com'
|
||||
),
|
||||
array(
|
||||
'first_name' => 'Mike',
|
||||
'last_name' => 'Smith',
|
||||
'email' => 'mike@maipoet.com'
|
||||
)
|
||||
);
|
||||
$this->customFieldsData = array(
|
||||
'name' => 'DOB',
|
||||
'type' => 'date',
|
||||
);
|
||||
$this->bootStrapMenu = new BootstrapMenu();
|
||||
}
|
||||
|
||||
function itCanGetSegments() {
|
||||
$this->_createSegmentsAndSubscribers();
|
||||
$segments = $this->bootStrapMenu->getSegments();
|
||||
expect(count($segments))->equals(2);
|
||||
expect($segments[0]['name'])->equals($this->segmentsData[0]['name']);
|
||||
expect($segments[0]['subscriberCount'])->equals(1);
|
||||
expect($segments[1]['subscriberCount'])->equals(0);
|
||||
}
|
||||
|
||||
function itCanGetSubscriberFields() {
|
||||
$subsriberFields = $this->bootStrapMenu->getSubscriberFields();
|
||||
$fields = array(
|
||||
's_email',
|
||||
's_first_name',
|
||||
's_last_name',
|
||||
's_status'
|
||||
);
|
||||
foreach ($fields as $field) {
|
||||
expect(in_array($field, array_keys($subsriberFields)))->true();
|
||||
}
|
||||
}
|
||||
|
||||
function itCanFormatSubsciberFields() {
|
||||
$formattedSubscriberFields =
|
||||
$this->bootStrapMenu->formatSubscriberFields(
|
||||
$this->bootStrapMenu->getSubscriberFields()
|
||||
);
|
||||
$fields = array(
|
||||
'id',
|
||||
'name',
|
||||
'type',
|
||||
'custom'
|
||||
);
|
||||
foreach ($fields as $field) {
|
||||
expect(in_array($field, array_keys($formattedSubscriberFields[0])))
|
||||
->true();
|
||||
}
|
||||
expect($formattedSubscriberFields[0]['custom'])->false();
|
||||
}
|
||||
|
||||
function itCanGetSubsciberCustomFields() {
|
||||
$customField = CustomField::create();
|
||||
$customField->hydrate($this->customFieldsData);
|
||||
$customField->save();
|
||||
$subscriberCustomFields =
|
||||
$this->bootStrapMenu
|
||||
->getSubscriberCustomFields();
|
||||
expect($subscriberCustomFields[0]['type'])
|
||||
->equals($this->customFieldsData['type']);
|
||||
}
|
||||
|
||||
function itCanFormatSubsciberCustomFields() {
|
||||
$customField = CustomField::create();
|
||||
$customField->hydrate($this->customFieldsData);
|
||||
$customField->save();
|
||||
$formattedSubscriberCustomFields =
|
||||
$this->bootStrapMenu->formatSubscriberCustomFields(
|
||||
$this->bootStrapMenu->getSubscriberCustomFields()
|
||||
);
|
||||
$fields = array(
|
||||
'id',
|
||||
'name',
|
||||
'type',
|
||||
'custom'
|
||||
);
|
||||
foreach ($fields as $field) {
|
||||
expect(in_array($field, array_keys($formattedSubscriberCustomFields[0])))
|
||||
->true();
|
||||
}
|
||||
expect($formattedSubscriberCustomFields[0]['custom'])->true();
|
||||
}
|
||||
|
||||
function itCanFormatFieldsForSelect2() {
|
||||
$bootStrapMenu = clone($this->bootStrapMenu);
|
||||
$select2FieldsWithouCustomFields = array(
|
||||
array(
|
||||
'name' => 'Actions',
|
||||
'children' => array(
|
||||
array(
|
||||
'id' => 'ignore',
|
||||
'name' => 'Ignore column...',
|
||||
),
|
||||
array(
|
||||
'id' => 'create',
|
||||
'name' => 'Create new column...'
|
||||
),
|
||||
)
|
||||
),
|
||||
array(
|
||||
'name' => 'System columns',
|
||||
'children' => $bootStrapMenu->formatSubscriberFields(
|
||||
$bootStrapMenu->subscriberFields
|
||||
)
|
||||
)
|
||||
);
|
||||
$select2FieldsWithCustomFields = array_merge(
|
||||
$select2FieldsWithouCustomFields,
|
||||
array(
|
||||
array(
|
||||
'name' => __('User columns'),
|
||||
'children' => $bootStrapMenu->formatSubscriberCustomFields(
|
||||
$bootStrapMenu->subscriberCustomFields
|
||||
)
|
||||
)
|
||||
));
|
||||
$formattedFieldsForSelect2 = $bootStrapMenu->formatFieldsForSelect2(
|
||||
$bootStrapMenu->subscriberFields,
|
||||
$bootStrapMenu->subscriberCustomFields
|
||||
);
|
||||
expect($formattedFieldsForSelect2)->equals($select2FieldsWithCustomFields);
|
||||
$bootStrapMenu->subscriberCustomFields = false;
|
||||
$formattedFieldsForSelect2 = $bootStrapMenu->formatFieldsForSelect2(
|
||||
$bootStrapMenu->subscriberFields,
|
||||
$bootStrapMenu->subscriberCustomFields
|
||||
);
|
||||
expect($formattedFieldsForSelect2)->equals($select2FieldsWithouCustomFields);
|
||||
}
|
||||
|
||||
function itCanBootstrap() {
|
||||
$bootstrap = clone($this->bootStrapMenu);
|
||||
$this->_createSegmentsAndSubscribers();
|
||||
$bootstrap->segments = $bootstrap->getSegments();
|
||||
$menu = $bootstrap->bootstrap();
|
||||
expect(count(json_decode($menu['segments'], true)))->equals(2);
|
||||
// email, first_name, last_name, status + 1 custom field
|
||||
expect(count(json_decode($menu['subscriberFields'], true)))->equals(5);
|
||||
// action, system columns, user columns
|
||||
expect(count(json_decode($menu['subscriberFieldsSelect2'], true)))->equals(3);
|
||||
expect($menu['maxPostSize'])->equals(ini_get('post_max_size'));
|
||||
expect($menu['maxPostSizeBytes'])->equals(
|
||||
(int) ini_get('post_max_size') * 1048576
|
||||
);
|
||||
}
|
||||
|
||||
function _createSegmentsAndSubscribers() {
|
||||
foreach ($this->segmentsData as $segmentData) {
|
||||
$segment = Segment::create();
|
||||
$segment->hydrate($segmentData);
|
||||
$segment->save();
|
||||
}
|
||||
foreach ($this->subscribersData as $index => $subscriberData) {
|
||||
$subscriber = Subscriber::create();
|
||||
$subscriber->hydrate($subscriberData);
|
||||
$subscriber->save();
|
||||
$association = SubscriberSegment::create();
|
||||
$association->subscriber_id = $subscriber->id;
|
||||
$association->segment_id = $index;
|
||||
$association->save();
|
||||
};
|
||||
}
|
||||
|
||||
function _after() {
|
||||
ORM::forTable(Subscriber::$_table)
|
||||
->deleteMany();
|
||||
ORM::forTable(Segment::$_table)
|
||||
->deleteMany();
|
||||
ORM::forTable(SubscriberSegment::$_table)
|
||||
->deleteMany();
|
||||
}
|
||||
}
|
6
tests/unit/Import/ImportCest.php
Normal file
6
tests/unit/Import/ImportCest.php
Normal file
@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
class ImportCest {
|
||||
function _before() {
|
||||
}
|
||||
}
|
5003
tests/unit/Import/ImportTestData.json
Normal file
5003
tests/unit/Import/ImportTestData.json
Normal file
File diff suppressed because it is too large
Load Diff
77
tests/unit/Import/MailChimpCest.php
Normal file
77
tests/unit/Import/MailChimpCest.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
use MailPoet\Import\MailChimp;
|
||||
|
||||
class MailChimpCest {
|
||||
function __construct() {
|
||||
$this->APIKey = 'd91ae3861c4829c40bd469e40d6c0e7e-us6';
|
||||
$this->mailChimp = new MailChimp($this->APIKey);
|
||||
$this->lists = array(
|
||||
'edf74586e9',
|
||||
'8b66f7fac8'
|
||||
);
|
||||
}
|
||||
|
||||
function itCanGetAPIKey() {
|
||||
expect($this->mailChimp->getAPIKey($this->APIKey))->equals($this->APIKey);
|
||||
expect($this->mailChimp->getAPIKey('somekey'))->false();
|
||||
}
|
||||
|
||||
function itCanGetDatacenter() {
|
||||
expect($this->mailChimp->getDataCenter($this->APIKey))->equals(
|
||||
explode('-', $this->APIKey)[1]
|
||||
);
|
||||
}
|
||||
|
||||
function itFailsWithIncorrectAPIKey() {
|
||||
$mailChimp = clone($this->mailChimp);
|
||||
$mailChimp->APIKey = false;
|
||||
$lists = $mailChimp->getLists();
|
||||
expect($lists['result'])->false();
|
||||
expect($lists['error'])->contains('API');
|
||||
$subscribers = $mailChimp->getLists();
|
||||
expect($subscribers['result'])->false();
|
||||
expect($subscribers['error'])->contains('API');
|
||||
}
|
||||
|
||||
function itCanGetLists() {
|
||||
$lists = $this->mailChimp->getLists();
|
||||
expect($lists['result'])->true();
|
||||
expect(count($lists['data']))->equals(2);
|
||||
expect(isset($lists['data'][0]['id']))->true();
|
||||
expect(isset($lists['data'][0]['name']))->true();
|
||||
}
|
||||
|
||||
function itFailsWithIncorrectLists() {
|
||||
$subscribers = $this->mailChimp->getSubscribers();
|
||||
expect($subscribers['result'])->false();
|
||||
expect($subscribers['error'])->contains('lists');
|
||||
$subscribers = $this->mailChimp->getSubscribers(array(12));
|
||||
expect($subscribers['result'])->false();
|
||||
expect($subscribers['error'])->contains('lists');
|
||||
}
|
||||
|
||||
function itCanGetSubscribers() {
|
||||
$subscribers = $this->mailChimp->getSubscribers(array($this->lists[0]));
|
||||
expect($subscribers['result'])->true();
|
||||
expect(isset($subscribers['data']['invalid']))->true();
|
||||
expect(isset($subscribers['data']['duplicate']))->true();
|
||||
expect(isset($subscribers['data']['header']))->true();
|
||||
expect(count($subscribers['data']['subscribers']))->equals(1);
|
||||
expect($subscribers['data']['count'])->equals(1);
|
||||
}
|
||||
|
||||
function itFailsWhenListHeadersDontMatch() {
|
||||
$subscribers = $this->mailChimp->getSubscribers($this->lists);
|
||||
expect($subscribers['result'])->false();
|
||||
expect($subscribers['error'])->contains('header');
|
||||
}
|
||||
|
||||
function itFailWhenSubscribersDataTooLarge() {
|
||||
$mailChimp = clone($this->mailChimp);
|
||||
$mailChimp->maxPostSize = 10;
|
||||
$subscribers = $mailChimp->getSubscribers(array('8b66f7fac8'));
|
||||
expect($subscribers['result'])->false();
|
||||
expect($subscribers['error'])->contains('large');
|
||||
}
|
||||
}
|
@ -10,6 +10,7 @@ class CustomFieldCest {
|
||||
$this->data = array(
|
||||
'name' => 'DOB',
|
||||
'type' => 'date',
|
||||
'params' => 'none'
|
||||
);
|
||||
$this->customField = CustomField::create();
|
||||
$this->customField->hydrate($this->data);
|
||||
@ -32,20 +33,19 @@ class CustomFieldCest {
|
||||
expect($this->saved)->equals(true);
|
||||
}
|
||||
|
||||
function itHasName() {
|
||||
$customField = CustomField::where('name', $this->data['name'])
|
||||
->findOne();
|
||||
expect($customField->name)->equals($this->data['name']);
|
||||
function itCanHaveName() {
|
||||
expect($this->customField->name)->equals($this->data['name']);
|
||||
}
|
||||
|
||||
function itHasType() {
|
||||
$customField = CustomField::where('name', $this->data['name'])
|
||||
->findOne();
|
||||
expect($customField->type)->equals($this->data['type']);
|
||||
function itCanHaveType() {
|
||||
expect($this->customField->type)->equals($this->data['type']);
|
||||
}
|
||||
|
||||
function itCanHaveParams() {
|
||||
expect(unserialize($this->customField->params))->equals($this->data['params']);
|
||||
}
|
||||
|
||||
function itHasToBeValid() {
|
||||
expect($this->saved)->equals(true);
|
||||
$empty_model = CustomField::create();
|
||||
expect($empty_model->save())->notEquals(true);
|
||||
$validations = $empty_model->getValidationErrors();
|
||||
|
@ -13,7 +13,26 @@ class SegmentCest {
|
||||
'name' => 'some name',
|
||||
'description' => 'some description'
|
||||
);
|
||||
|
||||
$this->subscribersData = array(
|
||||
array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Mailer',
|
||||
'email' => 'john@mailpoet.com'
|
||||
),
|
||||
array(
|
||||
'first_name' => 'Mike',
|
||||
'last_name' => 'Smith',
|
||||
'email' => 'mike@maipoet.com'
|
||||
)
|
||||
);
|
||||
$this->newslettersData = array(
|
||||
array(
|
||||
'subject' => 'My first newsletter'
|
||||
),
|
||||
array(
|
||||
'subject' => 'My second newsletter'
|
||||
)
|
||||
);
|
||||
$this->segment = Segment::create();
|
||||
$this->segment->hydrate($this->data);
|
||||
$this->saved = $this->segment->save();
|
||||
@ -97,20 +116,7 @@ class SegmentCest {
|
||||
}
|
||||
|
||||
function itCanHaveManySubscribers() {
|
||||
$subscribersData = array(
|
||||
array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Mailer',
|
||||
'email' => 'john@mailpoet.com'
|
||||
),
|
||||
array(
|
||||
'first_name' => 'Mike',
|
||||
'last_name' => 'Smith',
|
||||
'email' => 'mike@maipoet.com'
|
||||
)
|
||||
);
|
||||
|
||||
foreach($subscribersData as $subscriberData) {
|
||||
foreach($this->subscribersData as $subscriberData) {
|
||||
$subscriber = Subscriber::create();
|
||||
$subscriber->hydrate($subscriberData);
|
||||
$subscriber->save();
|
||||
@ -127,16 +133,7 @@ class SegmentCest {
|
||||
}
|
||||
|
||||
function itCanHaveManyNewsletters() {
|
||||
$newslettersData = array(
|
||||
array(
|
||||
'subject' => 'My first newsletter'
|
||||
),
|
||||
array(
|
||||
'subject' => 'My second newsletter'
|
||||
)
|
||||
);
|
||||
|
||||
foreach($newslettersData as $newsletterData) {
|
||||
foreach($this->newslettersData as $newsletterData) {
|
||||
$newsletter = Newsletter::create();
|
||||
$newsletter->hydrate($newsletterData);
|
||||
$newsletter->save();
|
||||
@ -152,6 +149,21 @@ class SegmentCest {
|
||||
expect(count($newsletters))->equals(2);
|
||||
}
|
||||
|
||||
function itCanReturnSubscriberCount() {
|
||||
foreach($this->subscribersData as $subscriberData) {
|
||||
$subscriber = Subscriber::create();
|
||||
$subscriber->hydrate($subscriberData);
|
||||
$subscriber->save();
|
||||
$association = SubscriberSegment::create();
|
||||
$association->subscriber_id = $subscriber->id;
|
||||
$association->segment_id = $this->segment->id;
|
||||
$association->save();
|
||||
}
|
||||
|
||||
$segment = Segment::filter('filterWithSubscriberCount')->findArray();
|
||||
expect($segment[0]['subscribers'])->equals(2);
|
||||
}
|
||||
|
||||
function _after() {
|
||||
ORM::forTable(Segment::$_table)
|
||||
->deleteMany();
|
||||
@ -160,6 +172,4 @@ class SegmentCest {
|
||||
ORM::forTable(SubscriberSegment::$_table)
|
||||
->deleteMany();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
use MailPoet\Models\CustomField;
|
||||
use MailPoet\Models\Segment;
|
||||
use MailPoet\Models\Subscriber;
|
||||
@ -19,7 +20,7 @@ class SubscriberCest {
|
||||
}
|
||||
|
||||
function itCanBeCreated() {
|
||||
expect($this->saved)->equals(true);
|
||||
expect($this->saved)->true();
|
||||
}
|
||||
|
||||
function itHasFirstName() {
|
||||
@ -53,7 +54,7 @@ class SubscriberCest {
|
||||
expect($saved)->notEquals(true);
|
||||
}
|
||||
|
||||
function itHasStatus() {
|
||||
function itHasStatusDefaultStatusOfUnconfirmed() {
|
||||
$subscriber =
|
||||
Subscriber::where('email', $this->data['email'])
|
||||
->findOne();
|
||||
@ -286,6 +287,36 @@ class SubscriberCest {
|
||||
expect($record->last_name)->equals('Mailer');
|
||||
}
|
||||
|
||||
function itCanCreateOrUpdateMultipleRecords() {
|
||||
ORM::forTable(Subscriber::$_table)->deleteMany();
|
||||
$columns = array(
|
||||
'first_name',
|
||||
'last_name',
|
||||
'email'
|
||||
);
|
||||
$values = array(
|
||||
array(
|
||||
'first_name' => 'Adam',
|
||||
'last_name' => 'Smith',
|
||||
'email' => 'adam@smith.com'
|
||||
),
|
||||
array(
|
||||
'first_name' => 'Mary',
|
||||
'last_name' => 'Jane',
|
||||
'email' => 'mary@jane.com'
|
||||
)
|
||||
);
|
||||
Subscriber::createMultiple($columns, $values);
|
||||
$subscribers = Subscriber::findArray();
|
||||
expect(count($subscribers))->equals(2);
|
||||
expect($subscribers[1]['email'])->equals($values[1]['email']);
|
||||
|
||||
$values[0]['first_name'] = 'John';
|
||||
Subscriber::updateMultiple($columns, $values);
|
||||
$subscribers = Subscriber::findArray();
|
||||
expect($subscribers[0]['first_name'])->equals($values[0]['first_name']);
|
||||
}
|
||||
|
||||
function _after() {
|
||||
ORM::forTable(Subscriber::$_table)
|
||||
->deleteMany();
|
||||
|
42
tests/unit/Models/SubscriberCustomFieldCest.php
Normal file
42
tests/unit/Models/SubscriberCustomFieldCest.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
use MailPoet\Models\SubscriberCustomField;
|
||||
|
||||
class SubscriberCustomFieldCest {
|
||||
function __construct() {
|
||||
$this->data = array(
|
||||
array(
|
||||
'custom_field_id' => 10,
|
||||
'subscriber_id' => 12,
|
||||
'value' => 'Test 1'
|
||||
),
|
||||
array(
|
||||
'custom_field_id' => 10,
|
||||
'subscriber_id' => 13,
|
||||
'value' => 'Test 2'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function itCanBeCreated() {
|
||||
$subscriberCustomField = SubscriberCustomField::create();
|
||||
$subscriberCustomField->hydrate($this->data[0]);
|
||||
expect($subscriberCustomField->save())->true();
|
||||
}
|
||||
|
||||
function itCanCreateOrUpdateMultipleRecords() {
|
||||
SubscriberCustomField::createMultiple($this->data);
|
||||
$records = SubscriberCustomField::findArray();
|
||||
expect(count($records))->equals(2);
|
||||
expect($records[1]['value'])->equals($this->data[1]['value']);
|
||||
$updatedData = $this->data;
|
||||
$updatedData[0]['value'] = 'updated';
|
||||
SubscriberCustomField::updateMultiple($updatedData);
|
||||
$records = SubscriberCustomField::findArray();
|
||||
expect($records[0]['value'])->equals($updatedData[0]['value']);
|
||||
}
|
||||
|
||||
function _after() {
|
||||
ORM::forTable(SubscriberCustomField::$_table)
|
||||
->deleteMany();
|
||||
}
|
||||
}
|
@ -2,7 +2,6 @@
|
||||
use MailPoet\Router\Mailer;
|
||||
|
||||
class MailerCest {
|
||||
|
||||
function __construct() {
|
||||
$this->router = new Mailer();
|
||||
}
|
||||
|
Reference in New Issue
Block a user