Update usages of form factory
[MAILPOET-2985]
This commit is contained in:
committed by
Veljko V
parent
7d62b15979
commit
c4628d4f78
@ -2,7 +2,6 @@
|
||||
|
||||
namespace MailPoet\API\JSON\v1;
|
||||
|
||||
use MailPoet\AdminPages\Pages\FormEditor;
|
||||
use MailPoet\API\JSON\Endpoint as APIEndpoint;
|
||||
use MailPoet\API\JSON\Error as APIError;
|
||||
use MailPoet\Config\AccessControl;
|
||||
@ -88,25 +87,15 @@ class Forms extends APIEndpoint {
|
||||
}
|
||||
|
||||
public function create($data = []) {
|
||||
if (isset($data['template-id']) && isset(FormEditor::TEMPLATES[$data['template-id']])) {
|
||||
return $this->save(
|
||||
$this->formFactory->createFormFromTemplate(
|
||||
FormEditor::TEMPLATES[$data['template-id']]
|
||||
)
|
||||
);
|
||||
if (isset($data['template-id'])) {
|
||||
$formEntity = $this->formFactory->createFormFromTemplate($data['template-id']);
|
||||
} else {
|
||||
$formEntity = $this->formFactory->createEmptyForm();
|
||||
}
|
||||
return $this->save($this->formFactory->createEmptyForm());
|
||||
}
|
||||
|
||||
public function save(Form $form) {
|
||||
$errors = $form->getErrors();
|
||||
|
||||
if (empty($errors)) {
|
||||
$form = Form::findOne($form->id);
|
||||
if(!$form instanceof Form) return $this->errorResponse();
|
||||
return $this->successResponse($form->asArray());
|
||||
}
|
||||
return $this->badRequest($errors);
|
||||
$form = Form::findOne($formEntity->getId());
|
||||
if(!$form instanceof Form) return $this->errorResponse();
|
||||
return $this->successResponse($form->asArray());
|
||||
}
|
||||
|
||||
public function previewEditor($data = []) {
|
||||
|
@ -240,7 +240,7 @@ class FormEditor {
|
||||
|
||||
$this->wp->wpSafeRedirect(
|
||||
$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;
|
||||
|
@ -27,7 +27,7 @@ parameters:
|
||||
|
||||
-
|
||||
message: "#^Cannot call method asArray\\(\\) on MailPoet\\\\Models\\\\Form\\|false\\.$#"
|
||||
count: 8
|
||||
count: 7
|
||||
path: ../../tests/integration/API/JSON/v1/FormsTest.php
|
||||
|
||||
-
|
||||
|
@ -72,18 +72,6 @@ class FormsTest extends \MailPoetTest {
|
||||
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() {
|
||||
$response = $this->endpoint->create();
|
||||
$formId = $response->data['id'];
|
||||
|
Reference in New Issue
Block a user