Update usages of form factory

[MAILPOET-2985]
This commit is contained in:
Rostislav Wolny
2020-06-30 15:58:06 +02:00
committed by Veljko V
parent 7d62b15979
commit c4628d4f78
4 changed files with 9 additions and 32 deletions

View File

@ -2,7 +2,6 @@
namespace MailPoet\API\JSON\v1; namespace MailPoet\API\JSON\v1;
use MailPoet\AdminPages\Pages\FormEditor;
use MailPoet\API\JSON\Endpoint as APIEndpoint; use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\API\JSON\Error as APIError; use MailPoet\API\JSON\Error as APIError;
use MailPoet\Config\AccessControl; use MailPoet\Config\AccessControl;
@ -88,26 +87,16 @@ class Forms extends APIEndpoint {
} }
public function create($data = []) { public function create($data = []) {
if (isset($data['template-id']) && isset(FormEditor::TEMPLATES[$data['template-id']])) { if (isset($data['template-id'])) {
return $this->save( $formEntity = $this->formFactory->createFormFromTemplate($data['template-id']);
$this->formFactory->createFormFromTemplate( } else {
FormEditor::TEMPLATES[$data['template-id']] $formEntity = $this->formFactory->createEmptyForm();
)
);
}
return $this->save($this->formFactory->createEmptyForm());
} }
public function save(Form $form) { $form = Form::findOne($formEntity->getId());
$errors = $form->getErrors();
if (empty($errors)) {
$form = Form::findOne($form->id);
if(!$form instanceof Form) return $this->errorResponse(); if(!$form instanceof Form) return $this->errorResponse();
return $this->successResponse($form->asArray()); return $this->successResponse($form->asArray());
} }
return $this->badRequest($errors);
}
public function previewEditor($data = []) { public function previewEditor($data = []) {
$formId = $data['id'] ?? null; $formId = $data['id'] ?? null;

View File

@ -240,7 +240,7 @@ class FormEditor {
$this->wp->wpSafeRedirect( $this->wp->wpSafeRedirect(
$this->wp->getSiteUrl(null, $this->wp->getSiteUrl(null,
'/wp-admin/admin.php?page=mailpoet-form-editor&id=' . $form->id() '/wp-admin/admin.php?page=mailpoet-form-editor&id=' . $form->getId()
) )
); );
exit; exit;

View File

@ -27,7 +27,7 @@ parameters:
- -
message: "#^Cannot call method asArray\\(\\) on MailPoet\\\\Models\\\\Form\\|false\\.$#" message: "#^Cannot call method asArray\\(\\) on MailPoet\\\\Models\\\\Form\\|false\\.$#"
count: 8 count: 7
path: ../../tests/integration/API/JSON/v1/FormsTest.php path: ../../tests/integration/API/JSON/v1/FormsTest.php
- -

View File

@ -72,18 +72,6 @@ class FormsTest extends \MailPoetTest {
expect($response->data['name'])->equals(''); expect($response->data['name'])->equals('');
} }
public function testItCanSaveAForm() {
$formData = [
'name' => 'My First Form',
];
$response = $this->endpoint->save(Form::createOrUpdate($formData));
expect($response->status)->equals(APIResponse::STATUS_OK);
expect($response->data)->equals(
Form::where('name', 'My First Form')->findOne()->asArray()
);
}
public function testItCanStoreDataForPreview() { public function testItCanStoreDataForPreview() {
$response = $this->endpoint->create(); $response = $this->endpoint->create();
$formId = $response->data['id']; $formId = $response->data['id'];