diff --git a/assets/js/src/subscribers/form.jsx b/assets/js/src/subscribers/form.jsx index 9a6492a7e3..203cad7923 100644 --- a/assets/js/src/subscribers/form.jsx +++ b/assets/js/src/subscribers/form.jsx @@ -53,10 +53,6 @@ define( var custom_fields = window.mailpoet_custom_fields || []; custom_fields.map(custom_field => { - if(custom_field.type === 'input') { - custom_field.type = 'text'; - } - let field = { name: 'cf_' + custom_field.id, label: custom_field.name, diff --git a/lib/Form/Block/Input.php b/lib/Form/Block/Text.php similarity index 87% rename from lib/Form/Block/Input.php rename to lib/Form/Block/Text.php index 334f7e7a70..2de8edba92 100644 --- a/lib/Form/Block/Input.php +++ b/lib/Form/Block/Text.php @@ -1,7 +1,7 @@ asArray(); }, $collection); - wp_send_json($custom_fields); + return $custom_fields; } function delete($id) { $custom_field = CustomField::findOne($id); if($custom_field === false or !$custom_field->id()) { - wp_send_json(array( - 'result' => false - )); + return array('result' => false); } else { $custom_field->delete(); - wp_send_json(array( + return array( 'result' => true, 'field' => $custom_field->asArray() - )); + ); } } @@ -58,16 +56,15 @@ class CustomFields { } } - wp_send_json($result); + return $result; } function get($id) { $custom_field = CustomField::findOne($id); if($custom_field === false) { - wp_send_json(false); + return false; } else { - $custom_field = $custom_field->asArray(); - wp_send_json($custom_field); + return $custom_field->asArray(); } } } \ No newline at end of file diff --git a/lib/Router/Forms.php b/lib/Router/Forms.php index ce4c6cf097..8889e9a8b2 100644 --- a/lib/Router/Forms.php +++ b/lib/Router/Forms.php @@ -63,7 +63,7 @@ class Forms { array( 'id' => 'email', 'name' => __('Email'), - 'type' => 'input', + 'type' => 'text', 'static' => true, 'params' => array( 'label' => __('Email'), diff --git a/tests/unit/Router/CustomFieldsCest.php b/tests/unit/Router/CustomFieldsCest.php new file mode 100644 index 0000000000..d59600106b --- /dev/null +++ b/tests/unit/Router/CustomFieldsCest.php @@ -0,0 +1,125 @@ + 'CF: text', + 'type' => 'text', + 'params' => array( + 'required' => '1', + 'validate' => '', + 'label' => 'CF: text' + ) + ), + array( + 'name' => 'CF: textarea', + 'type' => 'textarea', + 'params' => array( + 'required' => '1', + 'validate' => '', + 'label' => 'CF: text area' + ) + ), + array( + 'name' => 'CF: radio', + 'type' => 'radio', + 'params' => array( + 'values' => + array( + array('value' => 'one'), + array('value' => 'two'), + array('value' => 'three') + ), + 'required' => '1', + 'label' => 'CF: radio' + ) + ), + array( + 'name' => 'CF: date', + 'type' => 'date', + 'params' => array( + 'required' => '1', + 'date_type' => 'year_month_day', + 'date_format' => '', + 'label' => 'CF: date' + ) + ) + ); + + function _before() { + foreach($this->custom_fields as $custom_field) { + CustomField::createOrUpdate($custom_field); + } + } + + function itCanGetAllCustomFields() { + $router = new CustomFields(); + $response = $router->getAll(); + expect($response)->count(count($this->custom_fields)); + + foreach($response as $custom_field) { + expect($custom_field['name'])->notEmpty(); + expect($custom_field['type'])->notEmpty(); + expect($custom_field['params'])->notEmpty(); + } + } + + function itCanDeleteACustomField() { + $custom_field = CustomField::where('type', 'date')->findOne(); + + $router = new CustomFields(); + $response = $router->delete($custom_field->id()); + expect($response['result'])->true(); + + $custom_field = CustomField::where('type', 'date')->findOne(); + expect($custom_field)->false(); + } + + function itCanSaveACustomField() { + $new_custom_field = array( + 'name' => 'New custom field', + 'type' => 'text' + ); + + $router = new CustomFields(); + $response = $router->save($new_custom_field); + expect($response['result'])->true(); + + // missing type + $response = $router->save(array('name' => 'New custom field')); + expect($response['result'])->false(); + expect($response['errors'][0])->equals('You need to specify a type.'); + + // missing name + $response = $router->save(array('type' => 'text')); + expect($response['result'])->false(); + expect($response['errors'][0])->equals('You need to specify a name.'); + + // missing data + $response = $router->save(); + expect($response['result'])->false(); + expect($response['errors'][0])->equals('You need to specify a name.'); + expect($response['errors'][1])->equals('You need to specify a type.'); + } + + function itCanGetACustomField() { + $custom_field = CustomField::where('name', 'CF: text')->findOne(); + + $router = new CustomFields(); + $response = $router->get($custom_field->id()); + expect($response)->notEmpty(); + expect($response['name'])->equals('CF: text'); + expect($response['type'])->equals('text'); + expect($response['params'])->notEmpty(); + + $response = $router->get('not_an_id'); + expect($response)->false(); + } + + function _after() { + ORM::forTable(CustomField::$_table)->deleteMany(); + } +} \ No newline at end of file diff --git a/views/form/editor.html b/views/form/editor.html index 4686e62222..a014ca7ef4 100644 --- a/views/form/editor.html +++ b/views/form/editor.html @@ -219,7 +219,7 @@ { id: "first_name", name: "<%= __('First name') %>", - type: 'input', + type: 'text', params: { label: "<%= __('First name') %>" }, @@ -228,7 +228,7 @@ { id: "last_name", name: "<%= __('Last name') %>", - type: 'input', + type: 'text', params: { label: "<%= __('Last name') %>" }, @@ -620,7 +620,7 @@ <%= partial('form_template_block', 'form/templates/blocks/container.hbs') %> <%= partial('form_template_divider', 'form/templates/blocks/divider.hbs') %> - <%= partial('form_template_input', 'form/templates/blocks/input.hbs') %> + <%= partial('form_template_text', 'form/templates/blocks/text.hbs') %> <%= partial('form_template_submit', 'form/templates/blocks/submit.hbs') %> <%= partial('form_template_segment', 'form/templates/blocks/segment.hbs') %> <%= partial('form_template_radio', 'form/templates/blocks/radio.hbs') %> @@ -705,7 +705,7 @@ ) %> - diff --git a/views/form/templates/blocks/input.hbs b/views/form/templates/blocks/text.hbs similarity index 100% rename from views/form/templates/blocks/input.hbs rename to views/form/templates/blocks/text.hbs diff --git a/views/form/templates/settings/field.hbs b/views/form/templates/settings/field.hbs index 6915687bf4..5c800acfc2 100644 --- a/views/form/templates/settings/field.hbs +++ b/views/form/templates/settings/field.hbs @@ -3,7 +3,7 @@ {{> _settings_label }} {{/ifCond}} - {{#ifCond type '==' 'input'}} + {{#ifCond type '==' 'text'}} {{> _settings_label }} {{> _settings_label_within }} {{#ifCond id 'in' 'first_name,last_name' }} diff --git a/views/form/templates/settings/field_form.hbs b/views/form/templates/settings/field_form.hbs index a546f6a1bc..8ce1ddc575 100644 --- a/views/form/templates/settings/field_form.hbs +++ b/views/form/templates/settings/field_form.hbs @@ -17,8 +17,8 @@ >