From 5ceb084c6453b7376bc7d2e02b204ac9cb24fa3b Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Fri, 2 Apr 2021 11:41:16 -0300 Subject: [PATCH] Refactor Forms::delete() to use Doctrine [MAILPOET-3039] --- lib/API/JSON/v1/Forms.php | 8 ++++---- lib/Form/FormsRepository.php | 5 +++++ tests/integration/API/JSON/v1/FormsTest.php | 7 +++++++ tests/integration/Form/FormsRepositoryTest.php | 7 +++++++ 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/API/JSON/v1/Forms.php b/lib/API/JSON/v1/Forms.php index 237a615758..603b9f0324 100644 --- a/lib/API/JSON/v1/Forms.php +++ b/lib/API/JSON/v1/Forms.php @@ -303,10 +303,10 @@ class Forms extends APIEndpoint { } public function delete($data = []) { - $id = (isset($data['id']) ? (int)$data['id'] : false); - $form = Form::findOne($id); - if ($form instanceof Form) { - $form->delete(); + $form = $this->getForm($data); + + if ($form instanceof FormEntity) { + $this->formsRepository->delete($form); return $this->successResponse(null, ['count' => 1]); } else { diff --git a/lib/Form/FormsRepository.php b/lib/Form/FormsRepository.php index 5701baed75..27f2b03218 100644 --- a/lib/Form/FormsRepository.php +++ b/lib/Form/FormsRepository.php @@ -36,6 +36,11 @@ class FormsRepository extends Repository { ->getSingleScalarResult(); } + public function delete(FormEntity $form) { + $this->remove($form); + $this->flush(); + } + public function trash(FormEntity $form) { $this->updateDeletedAt($form, Carbon::now()); } diff --git a/tests/integration/API/JSON/v1/FormsTest.php b/tests/integration/API/JSON/v1/FormsTest.php index 78b19601f8..a378941be0 100644 --- a/tests/integration/API/JSON/v1/FormsTest.php +++ b/tests/integration/API/JSON/v1/FormsTest.php @@ -239,6 +239,13 @@ class FormsTest extends \MailPoetTest { expect($response->meta['count'])->equals(1); } + public function testErrorWhenDeletingNonExistentForm() { + $response = $this->endpoint->delete(['id' => 'Invalid ID']); + expect($response->errors[0]['error'])->equals('not_found'); + expect($response->status)->equals(APIResponse::STATUS_NOT_FOUND); + expect($response->meta)->isEmpty(); + } + public function testItCanDuplicateAForm() { $response = $this->endpoint->duplicate(['id' => $this->form1->getId()]); expect($response->status)->equals(APIResponse::STATUS_OK); diff --git a/tests/integration/Form/FormsRepositoryTest.php b/tests/integration/Form/FormsRepositoryTest.php index 01ffe8b575..1e5f197458 100644 --- a/tests/integration/Form/FormsRepositoryTest.php +++ b/tests/integration/Form/FormsRepositoryTest.php @@ -13,6 +13,13 @@ class FormsRepositoryTest extends \MailPoetTest { $this->repository = $this->diContainer->get(FormsRepository::class); } + public function testItCanDeleteForm() { + $form = $this->createForm('Form 1'); + expect($this->repository->findOneById($form->getId()))->isInstanceOf(FormEntity::class); + $this->repository->delete($form); + expect($form->getId())->null(); + } + public function testItCanTrashForm() { $form = $this->createForm('Form 1'); expect($form->getDeletedAt())->null();