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;
|
||||
|
||||
use Exception;
|
||||
use MailPoet\API\JSON\Endpoint as APIEndpoint;
|
||||
use MailPoet\API\JSON\Error;
|
||||
use MailPoet\API\JSON\Error as APIError;
|
||||
@ -12,6 +13,7 @@ use MailPoet\Entities\FormEntity;
|
||||
use MailPoet\Form\ApiDataSanitizer;
|
||||
use MailPoet\Form\DisplayFormInWPContent;
|
||||
use MailPoet\Form\FormFactory;
|
||||
use MailPoet\Form\FormSaveController;
|
||||
use MailPoet\Form\FormsRepository;
|
||||
use MailPoet\Form\Listing\FormListingRepository;
|
||||
use MailPoet\Form\PreviewPage;
|
||||
@ -59,6 +61,9 @@ class Forms extends APIEndpoint {
|
||||
/** @var ApiDataSanitizer */
|
||||
private $dataSanitizer;
|
||||
|
||||
/** @var FormSaveController */
|
||||
private $formSaveController;
|
||||
|
||||
public function __construct(
|
||||
Listing\BulkActionController $bulkAction,
|
||||
Listing\Handler $listingHandler,
|
||||
@ -69,7 +74,8 @@ class Forms extends APIEndpoint {
|
||||
FormsResponseBuilder $formsResponseBuilder,
|
||||
WPFunctions $wp,
|
||||
Emoji $emoji,
|
||||
ApiDataSanitizer $dataSanitizer
|
||||
ApiDataSanitizer $dataSanitizer,
|
||||
FormSaveController $formSaveController
|
||||
) {
|
||||
$this->bulkAction = $bulkAction;
|
||||
$this->listingHandler = $listingHandler;
|
||||
@ -81,6 +87,7 @@ class Forms extends APIEndpoint {
|
||||
$this->formsResponseBuilder = $formsResponseBuilder;
|
||||
$this->emoji = $emoji;
|
||||
$this->dataSanitizer = $dataSanitizer;
|
||||
$this->formSaveController = $formSaveController;
|
||||
}
|
||||
|
||||
public function get($data = []) {
|
||||
@ -314,27 +321,20 @@ class Forms extends APIEndpoint {
|
||||
}
|
||||
|
||||
public function duplicate($data = []) {
|
||||
$id = (isset($data['id']) ? (int)$data['id'] : false);
|
||||
$form = Form::findOne($id);
|
||||
$form = $this->getForm($data);
|
||||
|
||||
if ($form instanceof Form) {
|
||||
$formName = $form->name ? sprintf(__('Copy of %s', 'mailpoet'), $form->name) : '';
|
||||
$data = [
|
||||
'name' => $formName,
|
||||
];
|
||||
$duplicate = $form->duplicate($data);
|
||||
$errors = $duplicate->getErrors();
|
||||
|
||||
if (!empty($errors)) {
|
||||
return $this->errorResponse($errors);
|
||||
} else {
|
||||
$duplicate = Form::findOne($duplicate->id);
|
||||
if(!$duplicate instanceof Form) return $this->errorResponse();
|
||||
if ($form instanceof FormEntity) {
|
||||
try {
|
||||
$duplicate = $this->formSaveController->duplicate($form);
|
||||
} catch (Exception $e) {
|
||||
return $this->errorResponse([
|
||||
APIError::UNKNOWN => __('Duplicating form failed.', 'mailpoet'),
|
||||
], [], Response::STATUS_UNKNOWN);
|
||||
}
|
||||
return $this->successResponse(
|
||||
$duplicate->asArray(),
|
||||
$this->formsResponseBuilder->build($duplicate),
|
||||
['count' => 1]
|
||||
);
|
||||
}
|
||||
} else {
|
||||
return $this->errorResponse([
|
||||
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\FormFactory::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\PreviewPage::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() {
|
||||
$response = $this->endpoint->duplicate(['id' => $this->form1->getId()]);
|
||||
expect($response->status)->equals(APIResponse::STATUS_OK);
|
||||
$form = Form::where('name', 'Copy of Form 1')->findOne();
|
||||
assert($form instanceof Form);
|
||||
$form = $this->formsRepository->findOneBy(['name' => 'Copy of Form 1']);
|
||||
$this->assertInstanceOf(FormEntity::class, $form);
|
||||
expect($response->data)->equals(
|
||||
$form->asArray()
|
||||
$form->toArray()
|
||||
);
|
||||
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