Refactor Forms::restore() to use Doctrine
[MAILPOET-3039]
This commit is contained in:
@ -271,14 +271,12 @@ class Forms extends APIEndpoint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function restore($data = []) {
|
public function restore($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) {
|
||||||
$form->restore();
|
$this->formsRepository->restore($form);
|
||||||
$form = Form::findOne($form->id);
|
|
||||||
if(!$form instanceof Form) return $this->errorResponse();
|
|
||||||
return $this->successResponse(
|
return $this->successResponse(
|
||||||
$form->asArray(),
|
$form->toArray(),
|
||||||
['count' => 1]
|
['count' => 1]
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
@ -37,7 +37,15 @@ class FormsRepository extends Repository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function trash(FormEntity $form) {
|
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->persist($form);
|
||||||
$this->flush();
|
$this->flush();
|
||||||
}
|
}
|
||||||
|
@ -202,12 +202,19 @@ class FormsTest extends \MailPoetTest {
|
|||||||
$response = $this->endpoint->restore(['id' => $this->form1->getId()]);
|
$response = $this->endpoint->restore(['id' => $this->form1->getId()]);
|
||||||
expect($response->status)->equals(APIResponse::STATUS_OK);
|
expect($response->status)->equals(APIResponse::STATUS_OK);
|
||||||
expect($response->data)->equals(
|
expect($response->data)->equals(
|
||||||
$this->reloadForm((int)$this->form1->getId())->asArray()
|
$this->form1->toArray()
|
||||||
);
|
);
|
||||||
expect($response->data['deleted_at'])->null();
|
expect($response->data['deleted_at'])->null();
|
||||||
expect($response->meta['count'])->equals(1);
|
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() {
|
public function testItCanTrashAForm() {
|
||||||
$response = $this->endpoint->trash(['id' => $this->form2->getId()]);
|
$response = $this->endpoint->trash(['id' => $this->form2->getId()]);
|
||||||
expect($response->status)->equals(APIResponse::STATUS_OK);
|
expect($response->status)->equals(APIResponse::STATUS_OK);
|
||||||
|
@ -20,6 +20,14 @@ class FormsRepositoryTest extends \MailPoetTest {
|
|||||||
expect($form->getDeletedAt())->notNull();
|
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() {
|
public function _after() {
|
||||||
$this->truncateEntity(FormEntity::class);
|
$this->truncateEntity(FormEntity::class);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user