Refactor Forms::restore() to use Doctrine

[MAILPOET-3039]
This commit is contained in:
Rodrigo Primo
2021-04-02 11:23:07 -03:00
committed by Veljko V
parent e054f05222
commit 49f0c054fd
4 changed files with 30 additions and 9 deletions

View File

@ -271,14 +271,12 @@ class Forms extends APIEndpoint {
}
public function restore($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false);
$form = Form::findOne($id);
if ($form instanceof Form) {
$form->restore();
$form = Form::findOne($form->id);
if(!$form instanceof Form) return $this->errorResponse();
$form = $this->getForm($data);
if ($form instanceof FormEntity) {
$this->formsRepository->restore($form);
return $this->successResponse(
$form->asArray(),
$form->toArray(),
['count' => 1]
);
} else {

View File

@ -37,7 +37,15 @@ class FormsRepository extends Repository {
}
public function trash(FormEntity $form) {
$form->setDeletedAt(Carbon::now());
$this->updateDeletedAt($form, Carbon::now());
}
public function restore(FormEntity $form) {
$this->updateDeletedAt($form, null);
}
private function updateDeletedAt(FormEntity $form, ?Carbon $value) {
$form->setDeletedAt($value);
$this->persist($form);
$this->flush();
}

View File

@ -202,12 +202,19 @@ class FormsTest extends \MailPoetTest {
$response = $this->endpoint->restore(['id' => $this->form1->getId()]);
expect($response->status)->equals(APIResponse::STATUS_OK);
expect($response->data)->equals(
$this->reloadForm((int)$this->form1->getId())->asArray()
$this->form1->toArray()
);
expect($response->data['deleted_at'])->null();
expect($response->meta['count'])->equals(1);
}
public function testErrorWhenRestoringNonExistentForm() {
$response = $this->endpoint->restore(['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 testItCanTrashAForm() {
$response = $this->endpoint->trash(['id' => $this->form2->getId()]);
expect($response->status)->equals(APIResponse::STATUS_OK);

View File

@ -20,6 +20,14 @@ class FormsRepositoryTest extends \MailPoetTest {
expect($form->getDeletedAt())->notNull();
}
public function testItCanRestoreForm() {
$form = $this->createForm('Form 1');
$this->repository->trash($form);
expect($form->getDeletedAt())->notNull();
$this->repository->restore($form);
expect($form->getDeletedAt())->null();
}
public function _after() {
$this->truncateEntity(FormEntity::class);
}