Files
piratepoet/tests/unit/Subscribers/ImportExport/BootStrapMenuCest.php
2016-02-04 19:04:52 +01:00

285 lines
8.8 KiB
PHP

<?php
use MailPoet\Models\CustomField;
use MailPoet\Models\Segment;
use MailPoet\Models\Subscriber;
use MailPoet\Models\SubscriberSegment;
use MailPoet\Subscribers\ImportExport\BootStrapMenu;
class BootStrapMenuCest {
function _before() {
$segment_1 = Segment::createOrUpdate(array('name' => 'Unconfirmed Segment'));
$segment_2 = Segment::createOrUpdate(array('name' => 'Confirmed Segment'));
$subscriber_1 = Subscriber::createOrUpdate(array(
'first_name' => 'John',
'last_name' => 'Mailer',
'status' => 'unconfirmed',
'email' => 'john@mailpoet.com'
));
$subscriber_2 = Subscriber::createOrUpdate(array(
'first_name' => 'Mike',
'last_name' => 'Smith',
'status' => 'subscribed',
'email' => 'mike@maipoet.com'
));
$association = SubscriberSegment::create();
$association->subscriber_id = $subscriber_1->id;
$association->segment_id = $segment_1->id;
$association->save();
$association = SubscriberSegment::create();
$association->subscriber_id = $subscriber_2->id;
$association->segment_id = $segment_2->id;
$association->save();
CustomField::createOrUpdate(array(
'name' => 'Birthday',
'type' => 'date'
));
$this->bootStrapImportMenu = new BootStrapMenu('import');
$this->bootStrapExportMenu = new BootStrapMenu('export');
}
function itCanGetSegmentsWithSubscriberCount() {
$segments = $this->bootStrapImportMenu->getSegments();
expect(count($segments))->equals(2);
expect($segments[0]['name'])->equals('Unconfirmed Segment');
expect($segments[0]['subscriberCount'])->equals(1);
expect($segments[1]['name'])->equals('Confirmed Segment');
expect($segments[1]['subscriberCount'])->equals(1);
}
function itCanGetPublicSegmentsForImport() {
$segments = $this->bootStrapImportMenu->getSegments();
expect(count($segments))->equals(2);
$subscriber = Subscriber::where(
'email', 'john@mailpoet.com'
)->findOne();
expect($subscriber->deleted_at)->null();
$subscriber->trash();
$subscriber = Subscriber::where(
'email', 'john@mailpoet.com'
)->whereNull('deleted_at')->findOne();
expect($subscriber)->false();
$segments = $this->bootStrapImportMenu->getSegments();
expect(count($segments))->equals(1);
}
function itCanGetPublicSegmentsForExport() {
$segments = $this->bootStrapExportMenu->getSegments();
expect(count($segments))->equals(2);
$subscriber = Subscriber::where('email', 'john@mailpoet.com')
->findOne();
$subscriber->deleted_at = date('Y-m-d H:i:s');
$subscriber->save();
$segments = $this->bootStrapExportMenu->getSegments();
expect(count($segments))->equals(1);
}
function itCanGetSegmentsForExport() {
$segments = $this->bootStrapExportMenu->getSegments();
expect(count($segments))->equals(2);
expect($segments[0]['name'])->equals('Confirmed Segment');
expect($segments[0]['subscriberCount'])->equals(1);
expect($segments[1]['name'])->equals('Unconfirmed Segment');
expect($segments[1]['subscriberCount'])->equals(1);
}
function itCanGetSegmentsWithConfirmedSubscribersForExport() {
$segments = $this->bootStrapExportMenu->getSegments(
$withConfirmedSubscribers = true
);
expect(count($segments))->equals(1);
expect($segments[0]['name'])->equals('Confirmed Segment');
}
function itCanGetSubscriberFields() {
$subsriberFields = $this->bootStrapImportMenu->getSubscriberFields();
$fields = array(
'email',
'first_name',
'last_name',
'status'
);
foreach($fields as $field) {
expect(in_array($field, array_keys($subsriberFields)))->true();
}
}
function itCanFormatSubsciberFields() {
$formattedSubscriberFields =
$this->bootStrapImportMenu->formatSubscriberFields(
$this->bootStrapImportMenu->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() {
$subscriberCustomFields =
$this->bootStrapImportMenu
->getSubscriberCustomFields();
expect($subscriberCustomFields[0]['type'])
->equals('date');
}
function itCanFormatSubsciberCustomFields() {
$formattedSubscriberCustomFields =
$this->bootStrapImportMenu->formatSubscriberCustomFields(
$this->bootStrapImportMenu->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 itCanFormatFieldsForSelect2Import() {
$bootStrapMenu = clone($this->bootStrapImportMenu);
$select2FieldsWithoutCustomFields = 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->getSubscriberFields()
)
)
);
$select2FieldsWithCustomFields = array_merge(
$select2FieldsWithoutCustomFields,
array(
array(
'name' => __('User columns'),
'children' => $bootStrapMenu->formatSubscriberCustomFields(
$bootStrapMenu->getSubscriberCustomFields()
)
)
));
$formattedFieldsForSelect2 = $bootStrapMenu->formatFieldsForSelect2(
$bootStrapMenu->getSubscriberFields(),
$bootStrapMenu->getSubscriberCustomFields()
);
expect($formattedFieldsForSelect2)->equals($select2FieldsWithCustomFields);
$formattedFieldsForSelect2 = $bootStrapMenu->formatFieldsForSelect2(
$bootStrapMenu->getSubscriberFields(),
array()
);
expect($formattedFieldsForSelect2)->equals($select2FieldsWithoutCustomFields);
}
function itCanFormatFieldsForSelect2Export() {
$bootStrapMenu = clone($this->bootStrapExportMenu);
$select2FieldsWithoutCustomFields = array(
array(
'name' => 'Actions',
'children' => array(
array(
'id' => 'select',
'name' => __('Select all...'),
),
array(
'id' => 'deselect',
'name' => __('Deselect all...')
),
)
),
array(
'name' => 'System columns',
'children' => $bootStrapMenu->formatSubscriberFields(
$bootStrapMenu->getSubscriberFields()
)
)
);
$select2FieldsWithCustomFields = array_merge(
$select2FieldsWithoutCustomFields,
array(
array(
'name' => __('User columns'),
'children' => $bootStrapMenu->formatSubscriberCustomFields(
$bootStrapMenu->getSubscriberCustomFields()
)
)
));
$formattedFieldsForSelect2 = $bootStrapMenu->formatFieldsForSelect2(
$bootStrapMenu->getSubscriberFields(),
$bootStrapMenu->getSubscriberCustomFields()
);
expect($formattedFieldsForSelect2)->equals($select2FieldsWithCustomFields);
$formattedFieldsForSelect2 = $bootStrapMenu->formatFieldsForSelect2(
$bootStrapMenu->getSubscriberFields(),
array()
);
expect($formattedFieldsForSelect2)->equals($select2FieldsWithoutCustomFields);
}
function itCanBootStrapImport() {
$import = clone($this->bootStrapImportMenu);
$importMenu = $import->bootstrap();
expect(count(json_decode($importMenu['segments'], true)))
->equals(2);
// email, first_name, last_name, status + 1 custom field
expect(count(json_decode($importMenu['subscriberFields'], true)))
->equals(5);
// action, system columns, user columns
expect(count(json_decode($importMenu['subscriberFieldsSelect2'], true)))
->equals(3);
expect($importMenu['maxPostSize'])->equals(ini_get('post_max_size'));
expect($importMenu['maxPostSizeBytes'])->equals(
(int)ini_get('post_max_size') * 1048576
);
}
function itCanBootStrapExport() {
$export = clone($this->bootStrapImportMenu);
$exportMenu = $export->bootstrap();
expect(count(json_decode($exportMenu['segments'], true)))
->equals(2);
// action, system columns, user columns
expect(count(json_decode($exportMenu['subscriberFieldsSelect2'], true)))
->equals(3);
}
function _after() {
Subscriber::deleteMany();
Segment::deleteMany();
SubscriberSegment::deleteMany();
CustomField::deleteMany();
}
}