Return all information in getSubcriberFields response

[MAILPOET-2099]
This commit is contained in:
Pavel Dohnal
2019-05-27 16:07:34 +02:00
committed by M. Shull
parent 7f7c98d8cb
commit 79bda9756e
2 changed files with 79 additions and 28 deletions

View File

@ -41,23 +41,42 @@ class API {
[ [
'id' => 'email', 'id' => 'email',
'name' => WPFunctions::get()->__('Email', 'mailpoet'), 'name' => WPFunctions::get()->__('Email', 'mailpoet'),
'type' => 'text',
'params' => [
'required' => '1',
],
], ],
[ [
'id' => 'first_name', 'id' => 'first_name',
'name' => WPFunctions::get()->__('First name', 'mailpoet'), 'name' => WPFunctions::get()->__('First name', 'mailpoet'),
'type' => 'text',
'params' => [
'required' => '',
],
], ],
[ [
'id' => 'last_name', 'id' => 'last_name',
'name' => WPFunctions::get()->__('Last name', 'mailpoet'), 'name' => WPFunctions::get()->__('Last name', 'mailpoet'),
'type' => 'text',
'params' => [
'required' => '',
],
], ],
]; ];
$custom_fields = CustomField::selectMany(['id', 'name'])->findMany(); $custom_fields = CustomField::selectMany(['id', 'name', 'type', 'params'])->findMany();
foreach ($custom_fields as $custom_field) { foreach ($custom_fields as $custom_field) {
$data[] = [ $result = [
'id' => 'cf_' . $custom_field->id, 'id' => 'cf_' . $custom_field->id,
'name' => $custom_field->name, 'name' => $custom_field->name,
'type' => $custom_field->type,
]; ];
if (is_serialized($custom_field->params)) {
$result['params'] = unserialize($custom_field->params);
} else {
$result['params'] = $custom_field->params;
}
$data[] = $result;
} }
return $data; return $data;

View File

@ -28,34 +28,66 @@ class APITest extends \MailPoetTest {
); );
} }
function testItReturnsSubscriberFields() { function testItReturnsDefaultSubscriberFields() {
$custom_field = CustomField::create();
$custom_field->name = 'test custom field';
$custom_field->type = CustomField::TYPE_TEXT;
$custom_field->save();
$response = $this->getApi()->getSubscriberFields(); $response = $this->getApi()->getSubscriberFields();
expect($response)->equals( expect($response)->contains([
[
[
'id' => 'email', 'id' => 'email',
'name' => __('Email', 'mailpoet'), 'name' => __('Email', 'mailpoet'),
'type' => 'text',
'params' => [
'required' => '1',
], ],
[ ]);
expect($response)->contains([
'id' => 'first_name', 'id' => 'first_name',
'name' => __('First name', 'mailpoet'), 'name' => __('First name', 'mailpoet'),
'type' => 'text',
'params' => [
'required' => '',
], ],
[ ]);
expect($response)->contains([
'id' => 'last_name', 'id' => 'last_name',
'name' => __('Last name', 'mailpoet'), 'name' => __('Last name', 'mailpoet'),
'type' => 'text',
'params' => [
'required' => '',
], ],
[ ]);
'id' => 'cf_' . $custom_field->id, }
'name' => $custom_field->name,
function testItReturnsCustomFields() {
$custom_field1 = CustomField::createOrUpdate([
'name' => 'text custom field',
'type' => CustomField::TYPE_TEXT,
'params' => ['required' => '1', 'date_type' => 'year_month_day'],
]);
$custom_field2 = CustomField::createOrUpdate([
'name' => 'checkbox custom field',
'type' => CustomField::TYPE_CHECKBOX,
'params' => ['required' => ''],
]);
$response = $this->getApi()->getSubscriberFields();
expect($response)->contains([
'id' => 'cf_' . $custom_field1->id,
'name' => 'text custom field',
'type' => 'text',
'params' => [
'required' => '1',
'label' => 'text custom field',
'date_type' => 'year_month_day',
], ],
] ]);
); expect($response)->contains([
'id' => 'cf_' . $custom_field2->id,
'name' => 'checkbox custom field',
'type' => 'checkbox',
'params' => [
'required' => '',
'label' => 'checkbox custom field',
],
]);
} }
function testItDoesNotSubscribeMissingSubscriberToLists() { function testItDoesNotSubscribeMissingSubscriberToLists() {