- Updates export tests

This commit is contained in:
MrCasual
2015-11-17 12:41:51 -05:00
parent 832e5ef342
commit 82cf4a28fd
3 changed files with 227 additions and 106 deletions

View File

@@ -19,21 +19,100 @@ class Export {
$this->segments = $data['segments']; $this->segments = $data['segments'];
$this->subscribersWithoutSegment = array_search(0, $this->segments); $this->subscribersWithoutSegment = array_search(0, $this->segments);
$this->subscriberFields = $data['subscriberFields']; $this->subscriberFields = $data['subscriberFields'];
$this->exportFile = $this->getExportFile($this->exportFormatOption);
$this->exportFileURL = $this->getExportFileURL($this->exportFile);
$this->profilerStart = microtime(true); $this->profilerStart = microtime(true);
$this->exportFile = sprintf( }
Env::$temp_path . '/MailPoet_export_%s.%s',
substr(md5(time()), 0, 4), function process() {
$this->exportFormatOption $subscribers = $this->getSubscribers();
$subscriberCustomFields = $this->getSubscriberCustomFields();
$formattedSubscriberFields = $this->formatSubscriberFields(
$this->subscriberFields,
$subscriberCustomFields
); );
$this->exportFileURL = sprintf( try {
'%s/%s/%s', if($this->exportFormatOption === 'csv') {
Env::$plugin_url, $CSVFile = fopen($this->exportFile, 'w');
Env::$temp_name, $formatCSV = function ($row) {
basename($this->exportFile) return '"' . str_replace('"', '\"', $row) . '"';
};
// add UTF-8 BOM (3 bytes, hex EF BB BF) at the start of the file for
// Excel to automatically recognize the encoding
fwrite($CSVFile, chr(0xEF) . chr(0xBB) . chr(0xBF));
if($this->groupBySegmentOption) {
$formattedSubscriberFields[] = __('List');
}
fwrite(
$CSVFile,
implode(
',',
array_map(
$formatCSV,
$formattedSubscriberFields
)
) . "\n"
);
foreach ($subscribers as $subscriber) {
$row = $this->formatSubscriberData(
$subscriber,
$formattedSubscriberFields
);
if($this->groupBySegmentOption) {
$row[] = ucwords($subscriber['segment_name']);
}
fwrite($CSVFile, implode(',', array_map($formatCSV, $row)) . "\n");
}
fclose($CSVFile);
} else {
$writer = new XLSXWriter();
$writer->setAuthor('MailPoet (www.mailpoet.com)');
$headerRow = array($formattedSubscriberFields);
$lastSegment = false;
$rows = array();
foreach ($subscribers as $subscriber) {
if($lastSegment && $lastSegment !== $subscriber['segment_name'] &&
$this->groupBySegmentOption
) {
$writer->writeSheet(
array_merge($headerRow, $rows), ucwords($lastSegment)
);
$rows = array();
}
// detect RTL language and set Excel to properly display the sheet
$RTLRegex = '/\p{Arabic}|\p{Hebrew}/u';
if(!$writer->rtl && (
preg_grep($RTLRegex, $subscriber) ||
preg_grep($RTLRegex, $formattedSubscriberFields))
) {
$writer->rtl = true;
}
$rows[] = $this->formatSubscriberData(
$subscriber,
$formattedSubscriberFields
);
$lastSegment = $subscriber['segment_name'];
}
$writer->writeSheet(array_merge($headerRow, $rows), 'MailPoet');
$writer->writeToFile($this->exportFile);
}
} catch (Exception $e) {
return array(
'result' => false,
'error' => $e->getMessage()
);
}
return array(
'result' => true,
'data' => array(
'totalExported' => count($subscribers),
'exportFileURL' => $this->exportFileURL
),
'profiler' => $this->timeExecution()
); );
} }
function process() { function getSubscribers() {
$subscribers = Subscriber:: $subscribers = Subscriber::
left_outer_join( left_outer_join(
SubscriberSegment::$_table, SubscriberSegment::$_table,
@@ -58,8 +137,9 @@ class Export {
'ELSE "' . __('Not In List') . '" END as segment_name' 'ELSE "' . __('Not In List') . '" END as segment_name'
) )
->whereRaw( ->whereRaw(
SubscriberSegment::$_table . '.segment_id IN (' . rtrim(str_repeat('?,', count($this->segments)), ',') . ') OR ' . SubscriberSegment::$_table . '.segment_id IN (' .
SubscriberSegment::$_table . '.segment_id IS NULL ', rtrim(str_repeat('?,', count($this->segments)), ',') . ') ' .
'OR ' . SubscriberSegment::$_table . '.segment_id IS NULL ',
$this->segments $this->segments
); );
} else { } else {
@@ -67,89 +147,66 @@ class Export {
->select(Segment::$_table . '.name', 'segment_name') ->select(Segment::$_table . '.name', 'segment_name')
->whereIn(SubscriberSegment::$_table . '.segment_id', $this->segments); ->whereIn(SubscriberSegment::$_table . '.segment_id', $this->segments);
} }
if(!$this->groupBySegmentOption) $subscribers = $subscribers->groupBy(Subscriber::$_table . '.id'); if(!$this->groupBySegmentOption) {
if($this->exportConfirmedOption) $subscribers = $subscribers->where(Subscriber::$_table . '.status', 'confirmed'); $subscribers =
$subscribers = $subscribers->findArray(); $subscribers->groupBy(Subscriber::$_table . '.id');
$subscriberCustomFields = Helpers::arrayColumn( }
if($this->exportConfirmedOption) {
$subscribers =
$subscribers->where(Subscriber::$_table . '.status', 'confirmed');
}
return $subscribers->findArray();
}
function getExportFileURL($file) {
return sprintf(
'%s/%s/%s',
Env::$plugin_url,
Env::$temp_name,
basename($file)
);
}
function getExportFile($format) {
return sprintf(
Env::$temp_path . '/MailPoet_export_%s.%s',
substr(md5(time()), 0, 4),
$format
);
}
function getSubscriberCustomFields() {
return Helpers::arrayColumn(
CustomField::findArray(), CustomField::findArray(),
'name', 'name',
'id' 'id'
); );
$formattedSubscriberFields = $this->formatSubscriberFields($this->subscriberFields, $subscriberCustomFields);
try {
if($this->exportFormatOption === 'csv') {
$CSVFile = fopen($this->exportFile, 'w');
$formatCSV = function ($row) {
return '"' . str_replace('"', '\"', $row) . '"';
};
// add UTF-8 BOM (3 bytes, hex EF BB BF) at the start of the file for Excel to automatically recognize the encoding
fwrite($CSVFile, chr(0xEF) . chr(0xBB) . chr(0xBF));
if($this->groupBySegmentOption) $formattedSubscriberFields[] = __('List');
fwrite($CSVFile, implode(',', array_map($formatCSV, $formattedSubscriberFields)) . "\n");
foreach ($subscribers as $subscriber) {
$row = array_map(function ($field) use ($subscriber, $subscriberCustomFields) {
return (isset($subscriberCustomFields[$field])) ? $subscriberCustomFields[$field] : $subscriber[$field];
}, $this->subscriberFields);
if($this->groupBySegmentOption) {
$row[] = $subscriber['segment_name'];
}
fwrite($CSVFile, implode(',', array_map($formatCSV, $row)) . "\n");
}
fclose($CSVFile);
} else {
$writer = new XLSXWriter();
$writer->setAuthor('MailPoet (www.mailpoet.com)');
$headerRow = array($formattedSubscriberFields);
$lastSegment = false;
$rows = array();
foreach ($subscribers as $subscriber) {
if($lastSegment && $lastSegment !== $subscriber['segment_name'] && $this->groupBySegmentOption) {
$writer->writeSheet(array_merge($headerRow, $rows), ucwords($lastSegment));
$rows = array();
}
// detect RTL language and set Excel to properly display the sheet
$arabicRegex = '/\p{Arabic}|\p{Hebrew}/u';
if(!$writer->rtl && (
preg_grep($arabicRegex, $subscriber) ||
preg_grep($arabicRegex, $formattedSubscriberFields))
) {
$writer->rtl = true;
}
$rows[] = array_map(function ($field) use ($subscriber, $subscriberCustomFields) {
return (isset($subscriberCustomFields[$field])) ? $subscriber[$subscriberCustomFields[$field]] : $subscriber[$field];
}, $this->subscriberFields);
$lastSegment = $subscriber['segment_name'];
}
$writer->writeSheet(array_merge($headerRow, $rows), 'MailPoet');
$writer->writeToFile($this->exportFile);
}
} catch (Exception $e) {
return array(
'result' => false,
'error' => $e->getMessage()
);
}
return array(
'result' => true,
'data' => array(
'totalExported' => count($subscribers),
'exportFileURL' => $this->exportFileURL
),
'profiler' => $this->timeExecution()
);
} }
function formatSubscriberFields($subscriberFields, $subscriberCustomFields) { function formatSubscriberFields($subscriberFields, $subscriberCustomFields) {
$bootStrapMenu = new BootStrapMenu(); $bootStrapMenu = new BootStrapMenu();
$translatedFields = $bootStrapMenu->getSubscriberFields(); $translatedFields = $bootStrapMenu->getSubscriberFields();
return array_map(function ($field) use ($translatedFields, $subscriberCustomFields) { return array_map(function ($field) use (
$translatedFields, $subscriberCustomFields
) {
$field = (isset($translatedFields[$field])) ? $field = (isset($translatedFields[$field])) ?
ucfirst($translatedFields[$field]) : ucfirst($translatedFields[$field]) :
ucfirst($field); ucfirst($field);
return (isset($subscriberCustomFields[$field])) ? $subscriberCustomFields[$field] : $field; return (isset($subscriberCustomFields[$field])) ?
$subscriberCustomFields[$field] : $field;
}, $subscriberFields); }, $subscriberFields);
} }
function formatSubscriberData($subscriber, $subscriberCustomFields) {
return array_map(function ($field) use (
$subscriber, $subscriberCustomFields
) {
return (isset($subscriberCustomFields[$field])) ?
$subscriberCustomFields[$field] :
$subscriber[$field];
}, $this->subscriberFields);
}
function timeExecution() { function timeExecution() {
$profilerEnd = microtime(true); $profilerEnd = microtime(true);
return ($profilerEnd - $this->profilerStart) / 60; return ($profilerEnd - $this->profilerStart) / 60;

View File

@@ -174,6 +174,11 @@ class Import {
'1', '1',
'true' 'true'
), ),
'unconfirmed' => array(
'unconfirmed',
0,
"0"
),
'unsubscribed' => array( 'unsubscribed' => array(
'unsubscribed', 'unsubscribed',
-1, -1,
@@ -183,12 +188,15 @@ class Import {
); );
$subscribersData['status'] = array_map(function ($state) use ($statuses) { $subscribersData['status'] = array_map(function ($state) use ($statuses) {
if(in_array(strtolower($state), $statuses['subscribed'])) { if(in_array(strtolower($state), $statuses['subscribed'])) {
return 'confirmed'; return 'subscribed';
} }
if(in_array(strtolower($state), $statuses['unsubscribed'])) { if(in_array(strtolower($state), $statuses['unsubscribed'])) {
return 'unsubscribed'; return 'unsubscribed';
} }
return 'confirmed'; // make "subscribed" a default status if(in_array(strtolower($state), $statuses['unconfirmed'])) {
return 'unconfirmed';
}
return 'subscribed'; // make "subscribed" a default status
}, $subscribersData['status']); }, $subscribersData['status']);
return $subscribersData; return $subscribersData;
} }

View File

@@ -1,6 +1,7 @@
<?php <?php
use MailPoet\Config\Env; use MailPoet\Config\Env;
use MailPoet\Models\CustomField;
use MailPoet\Models\Segment; use MailPoet\Models\Segment;
use MailPoet\Models\Subscriber; use MailPoet\Models\Subscriber;
use MailPoet\Models\SubscriberCustomField; use MailPoet\Models\SubscriberCustomField;
@@ -11,29 +12,35 @@ class ExportCest {
function __construct() { function __construct() {
$this->JSONdata = json_decode(file_get_contents(dirname(__FILE__) . '/ExportTestData.json'), true); $this->JSONdata = json_decode(file_get_contents(dirname(__FILE__) . '/ExportTestData.json'), true);
$this->subscribersData = array( $this->subscribersData = array(
'first_name' => array( array(
'Adam', 'first_name' => 'Adam',
'Mary', 'last_name' => 'Smith',
'John', 'email' => 'adam@smith.com',
'Paul' 1 => 'Brazil'
), ),
'last_name' => array( array(
'Smith', 'first_name' => 'Mary',
'Jane', 'last_name' => 'Jane',
'Kookoo', 'email' => 'mary@jane.com'
'Newman'
), ),
'email' => array( array(
'adam@smith.com', 'first_name' => 'John',
'mary@jane.com', 'last_name' => 'Kookoo',
'john@kookoo.com', 'email' => 'john@kookoo.com'
'paul@newman.com'
), ),
1 => array( array(
'Brazil' 'first_name' => 'Paul',
'last_name' => 'Newman',
'email' => 'paul@newman.com'
) )
); );
$this->segments = array( $this->customFieldsData = array(
array(
'name' => 'Country',
'type' => 'text'
)
);
$this->segmentsData = array(
array( array(
'name' => 'Newspapers' 'name' => 'Newspapers'
), ),
@@ -41,9 +48,29 @@ class ExportCest {
'name' => 'Journals' 'name' => 'Journals'
) )
); );
foreach ($this->subscribersData as $subscriber) {
$entity = Subscriber::create();
$entity->hydrate($subscriber);
$entity->save();
}
foreach ($this->segmentsData as $customField) {
$entity = Segment::create();
$entity->hydrate($customField);
$entity->save();
}
foreach ($this->customFieldsData as $customField) {
$entity = CustomField::create();
$entity->hydrate($customField);
$entity->save();
}
$this->export = new Export($this->JSONdata); $this->export = new Export($this->JSONdata);
} }
/* function _before() {
}*/
function itCanConstruct() { function itCanConstruct() {
expect($this->export->exportConfirmedOption) expect($this->export->exportConfirmedOption)
->equals(false); ->equals(false);
@@ -52,8 +79,13 @@ class ExportCest {
expect($this->export->groupBySegmentOption) expect($this->export->groupBySegmentOption)
->equals(true); ->equals(true);
expect($this->export->segments) expect($this->export->segments)
->equals(array(0, 1)); ->equals(
expect($this->export->subscribersWithoutSegment) array(
0,
1
)
);
expect($this->export->subscribersWithoutSegment)
->equals(0); ->equals(0);
expect($this->export->subscriberFields) expect($this->export->subscriberFields)
->equals( ->equals(
@@ -65,14 +97,14 @@ class ExportCest {
); );
expect( expect(
preg_match( preg_match(
'|'. '|' .
Env::$temp_path.'/MailPoet_export_[a-f0-9]{4}.'. Env::$temp_path . '/MailPoet_export_[a-f0-9]{4}.' .
$this->export->exportFormatOption . $this->export->exportFormatOption .
'|', $this->export->exportFile) '|', $this->export->exportFile)
)->equals(1); )->equals(1);
expect( expect(
preg_match( preg_match(
'|'. '|' .
Env::$plugin_url . '/' . Env::$plugin_url . '/' .
Env::$temp_name . '/' . Env::$temp_name . '/' .
basename($this->export->exportFile) . basename($this->export->exportFile) .
@@ -81,6 +113,30 @@ class ExportCest {
)->equals(1); )->equals(1);
} }
function itCanGetSubscribers() {
}
function itCanGetSubscriberCustomFields() {
$customFields = $this->export->getSubscriberCustomFields();
expect($customFields)->equals(
array(
1 => $this->customFieldsData[0]['name']
)
);
}
function itCanFormatSubscriberFields() {
$formattedSubscriberFields = $this->export->formatSubscriberFields(
$this->subscriberFields,
$this->export->getSubscriberCustomFields()
);
!d($formattedSubscriberFields);exit;
}
function itCanProcess() { function itCanProcess() {
} }