Refactor Forms::duplicate() to use Doctrine instead of Paris
[MAILPOET-3038]
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace MailPoet\API\JSON\v1;
|
namespace MailPoet\API\JSON\v1;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use MailPoet\API\JSON\Endpoint as APIEndpoint;
|
use MailPoet\API\JSON\Endpoint as APIEndpoint;
|
||||||
use MailPoet\API\JSON\Error;
|
use MailPoet\API\JSON\Error;
|
||||||
use MailPoet\API\JSON\Error as APIError;
|
use MailPoet\API\JSON\Error as APIError;
|
||||||
@@ -12,6 +13,7 @@ use MailPoet\Entities\FormEntity;
|
|||||||
use MailPoet\Form\ApiDataSanitizer;
|
use MailPoet\Form\ApiDataSanitizer;
|
||||||
use MailPoet\Form\DisplayFormInWPContent;
|
use MailPoet\Form\DisplayFormInWPContent;
|
||||||
use MailPoet\Form\FormFactory;
|
use MailPoet\Form\FormFactory;
|
||||||
|
use MailPoet\Form\FormSaveController;
|
||||||
use MailPoet\Form\FormsRepository;
|
use MailPoet\Form\FormsRepository;
|
||||||
use MailPoet\Form\Listing\FormListingRepository;
|
use MailPoet\Form\Listing\FormListingRepository;
|
||||||
use MailPoet\Form\PreviewPage;
|
use MailPoet\Form\PreviewPage;
|
||||||
@@ -59,6 +61,9 @@ class Forms extends APIEndpoint {
|
|||||||
/** @var ApiDataSanitizer */
|
/** @var ApiDataSanitizer */
|
||||||
private $dataSanitizer;
|
private $dataSanitizer;
|
||||||
|
|
||||||
|
/** @var FormSaveController */
|
||||||
|
private $formSaveController;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
Listing\BulkActionController $bulkAction,
|
Listing\BulkActionController $bulkAction,
|
||||||
Listing\Handler $listingHandler,
|
Listing\Handler $listingHandler,
|
||||||
@@ -69,7 +74,8 @@ class Forms extends APIEndpoint {
|
|||||||
FormsResponseBuilder $formsResponseBuilder,
|
FormsResponseBuilder $formsResponseBuilder,
|
||||||
WPFunctions $wp,
|
WPFunctions $wp,
|
||||||
Emoji $emoji,
|
Emoji $emoji,
|
||||||
ApiDataSanitizer $dataSanitizer
|
ApiDataSanitizer $dataSanitizer,
|
||||||
|
FormSaveController $formSaveController
|
||||||
) {
|
) {
|
||||||
$this->bulkAction = $bulkAction;
|
$this->bulkAction = $bulkAction;
|
||||||
$this->listingHandler = $listingHandler;
|
$this->listingHandler = $listingHandler;
|
||||||
@@ -81,6 +87,7 @@ class Forms extends APIEndpoint {
|
|||||||
$this->formsResponseBuilder = $formsResponseBuilder;
|
$this->formsResponseBuilder = $formsResponseBuilder;
|
||||||
$this->emoji = $emoji;
|
$this->emoji = $emoji;
|
||||||
$this->dataSanitizer = $dataSanitizer;
|
$this->dataSanitizer = $dataSanitizer;
|
||||||
|
$this->formSaveController = $formSaveController;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get($data = []) {
|
public function get($data = []) {
|
||||||
@@ -314,27 +321,20 @@ class Forms extends APIEndpoint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function duplicate($data = []) {
|
public function duplicate($data = []) {
|
||||||
$id = (isset($data['id']) ? (int)$data['id'] : false);
|
$form = $this->getForm($data);
|
||||||
$form = Form::findOne($id);
|
|
||||||
|
|
||||||
if ($form instanceof Form) {
|
if ($form instanceof FormEntity) {
|
||||||
$formName = $form->name ? sprintf(__('Copy of %s', 'mailpoet'), $form->name) : '';
|
try {
|
||||||
$data = [
|
$duplicate = $this->formSaveController->duplicate($form);
|
||||||
'name' => $formName,
|
} catch (Exception $e) {
|
||||||
];
|
return $this->errorResponse([
|
||||||
$duplicate = $form->duplicate($data);
|
APIError::UNKNOWN => __('Duplicating form failed.', 'mailpoet'),
|
||||||
$errors = $duplicate->getErrors();
|
], [], Response::STATUS_UNKNOWN);
|
||||||
|
|
||||||
if (!empty($errors)) {
|
|
||||||
return $this->errorResponse($errors);
|
|
||||||
} else {
|
|
||||||
$duplicate = Form::findOne($duplicate->id);
|
|
||||||
if(!$duplicate instanceof Form) return $this->errorResponse();
|
|
||||||
return $this->successResponse(
|
|
||||||
$duplicate->asArray(),
|
|
||||||
['count' => 1]
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
return $this->successResponse(
|
||||||
|
$this->formsResponseBuilder->build($duplicate),
|
||||||
|
['count' => 1]
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
return $this->errorResponse([
|
return $this->errorResponse([
|
||||||
APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet'),
|
APIError::NOT_FOUND => WPFunctions::get()->__('This form does not exist.', 'mailpoet'),
|
||||||
@@ -352,4 +352,10 @@ class Forms extends APIEndpoint {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getForm(array $data): ?FormEntity {
|
||||||
|
return isset($data['id'])
|
||||||
|
? $this->formsRepository->findOneById((int)$data['id'])
|
||||||
|
: null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -199,6 +199,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
|||||||
$container->autowire(\MailPoet\Form\Block\Textarea::class);
|
$container->autowire(\MailPoet\Form\Block\Textarea::class);
|
||||||
$container->autowire(\MailPoet\Form\FormFactory::class)->setPublic(true);
|
$container->autowire(\MailPoet\Form\FormFactory::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Form\FormHtmlSanitizer::class)->setPublic(true);
|
$container->autowire(\MailPoet\Form\FormHtmlSanitizer::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\Form\FormSaveController::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Form\Listing\FormListingRepository::class)->setPublic(true);
|
$container->autowire(\MailPoet\Form\Listing\FormListingRepository::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Form\PreviewPage::class);
|
$container->autowire(\MailPoet\Form\PreviewPage::class);
|
||||||
$container->autowire(\MailPoet\Form\Templates\TemplateRepository::class);
|
$container->autowire(\MailPoet\Form\Templates\TemplateRepository::class);
|
||||||
|
40
lib/Form/FormSaveController.php
Normal file
40
lib/Form/FormSaveController.php
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Form;
|
||||||
|
|
||||||
|
use MailPoet\Entities\FormEntity;
|
||||||
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
use MailPoetVendor\Carbon\Carbon;
|
||||||
|
use MailPoetVendor\Doctrine\ORM\EntityManager;
|
||||||
|
|
||||||
|
class FormSaveController {
|
||||||
|
/** @var EntityManager */
|
||||||
|
private $entityManager;
|
||||||
|
|
||||||
|
/** @var WPFunctions */
|
||||||
|
private $wp;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
EntityManager $entityManager,
|
||||||
|
WPFunctions $wp
|
||||||
|
) {
|
||||||
|
$this->entityManager = $entityManager;
|
||||||
|
$this->wp = $wp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function duplicate(FormEntity $formEntity): FormEntity {
|
||||||
|
$duplicate = clone $formEntity;
|
||||||
|
$duplicate->setName(sprintf(__('Copy of %s', 'mailpoet'), $formEntity->getName()));
|
||||||
|
|
||||||
|
// reset timestamps
|
||||||
|
$now = Carbon::createFromTimestamp($this->wp->currentTime('timestamp'));
|
||||||
|
$duplicate->setCreatedAt($now);
|
||||||
|
$duplicate->setUpdatedAt($now);
|
||||||
|
$duplicate->setDeletedAt(null);
|
||||||
|
|
||||||
|
$this->entityManager->persist($duplicate);
|
||||||
|
$this->entityManager->flush();
|
||||||
|
|
||||||
|
return $duplicate;
|
||||||
|
}
|
||||||
|
}
|
@@ -228,10 +228,10 @@ class FormsTest extends \MailPoetTest {
|
|||||||
public function testItCanDuplicateAForm() {
|
public function testItCanDuplicateAForm() {
|
||||||
$response = $this->endpoint->duplicate(['id' => $this->form1->getId()]);
|
$response = $this->endpoint->duplicate(['id' => $this->form1->getId()]);
|
||||||
expect($response->status)->equals(APIResponse::STATUS_OK);
|
expect($response->status)->equals(APIResponse::STATUS_OK);
|
||||||
$form = Form::where('name', 'Copy of Form 1')->findOne();
|
$form = $this->formsRepository->findOneBy(['name' => 'Copy of Form 1']);
|
||||||
assert($form instanceof Form);
|
$this->assertInstanceOf(FormEntity::class, $form);
|
||||||
expect($response->data)->equals(
|
expect($response->data)->equals(
|
||||||
$form->asArray()
|
$form->toArray()
|
||||||
);
|
);
|
||||||
expect($response->meta['count'])->equals(1);
|
expect($response->meta['count'])->equals(1);
|
||||||
}
|
}
|
||||||
|
41
tests/integration/Form/FormSaveControllerTest.php
Normal file
41
tests/integration/Form/FormSaveControllerTest.php
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Form;
|
||||||
|
|
||||||
|
use Codeception\Util\Fixtures;
|
||||||
|
use MailPoet\Entities\FormEntity;
|
||||||
|
|
||||||
|
class FormSaveControllerTest extends \MailPoetTest {
|
||||||
|
/** @var FormSaveController */
|
||||||
|
private $saveController;
|
||||||
|
|
||||||
|
public function _before() {
|
||||||
|
parent::_before();
|
||||||
|
$this->saveController = $this->diContainer->get(FormSaveController::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItDuplicatesForms() {
|
||||||
|
$form = $this->createForm();
|
||||||
|
$duplicate = $this->saveController->duplicate($form);
|
||||||
|
expect($duplicate->getName())->equals('Copy of ' . $form->getName());
|
||||||
|
expect($duplicate->getDeletedAt())->equals(null);
|
||||||
|
expect($duplicate->getBody())->equals($form->getBody());
|
||||||
|
expect($duplicate->getStatus())->equals($form->getStatus());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function _after() {
|
||||||
|
$this->cleanup();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createForm(): FormEntity {
|
||||||
|
$form = new FormEntity('My Form');
|
||||||
|
$form->setBody(Fixtures::get('form_body_template'));
|
||||||
|
$this->entityManager->persist($form);
|
||||||
|
$this->entityManager->flush();
|
||||||
|
return $form;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function cleanup() {
|
||||||
|
$this->truncateEntity(FormEntity::class);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user