aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorKevin Decherf <kevin@kdecherf.com>2019-02-17 15:30:42 +0100
committerKevin Decherf <kevin@kdecherf.com>2019-02-20 15:57:50 +0100
commit508302042f96ce771a36f0114acb0b3a89c18880 (patch)
treef599aafa60d81b1dbf473fc7d586abc84a18d1cf
parent4e0ed3368d2bdc2fa0d330e58902e792a896be06 (diff)
downloadwallabag-508302042f96ce771a36f0114acb0b3a89c18880.tar.gz
wallabag-508302042f96ce771a36f0114acb0b3a89c18880.tar.zst
wallabag-508302042f96ce771a36f0114acb0b3a89c18880.zip
EntryRestController: add support of expect parameter to delete action
The expect parameter enables an application to request the whole entry or only the id when requesting its deletion. `expects` defaults to `entry` to prevent any API breakage. Fixes #3711 Signed-off-by: Kevin Decherf <kevin@kdecherf.com>
-rw-r--r--src/Wallabag/ApiBundle/Controller/EntryRestController.php21
-rw-r--r--tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php21
2 files changed, 33 insertions, 9 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
index b2bad406..33b75665 100644
--- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
@@ -570,18 +570,31 @@ class EntryRestController extends WallabagRestController
570 * @ApiDoc( 570 * @ApiDoc(
571 * requirements={ 571 * requirements={
572 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} 572 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
573 * },
574 * parameters={
575 * {"name"="expect", "dataType"="string", "required"=false, "format"="id or entry", "description"="Only returns the id instead of the deleted entry's full entity if 'id' is specified. Default to entry"},
573 * } 576 * }
574 * ) 577 * )
575 * 578 *
576 * @return JsonResponse 579 * @return JsonResponse
577 */ 580 */
578 public function deleteEntriesAction(Entry $entry) 581 public function deleteEntriesAction(Entry $entry, Request $request)
579 { 582 {
583 $expect = $request->query->get('expect', 'entry');
584 if (!\in_array($expect, ['id', 'entry'], true)) {
585 throw new BadRequestHttpException(sprintf("expect: 'id' or 'entry' expected, %s given", $expect));
586 }
580 $this->validateAuthentication(); 587 $this->validateAuthentication();
581 $this->validateUserAccess($entry->getUser()->getId()); 588 $this->validateUserAccess($entry->getUser()->getId());
582 589
583 // We copy $entry to keep id in returned object 590 $response = $this->sendResponse([
584 $e = $entry; 591 'id' => $entry->getId(),
592 ]);
593 // We clone $entry to keep id in returned object
594 if ('entry' === $expect) {
595 $e = clone $entry;
596 $response = $this->sendResponse($e);
597 }
585 598
586 $em = $this->getDoctrine()->getManager(); 599 $em = $this->getDoctrine()->getManager();
587 $em->remove($entry); 600 $em->remove($entry);
@@ -590,7 +603,7 @@ class EntryRestController extends WallabagRestController
590 // entry deleted, dispatch event about it! 603 // entry deleted, dispatch event about it!
591 $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); 604 $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry));
592 605
593 return $this->sendResponse($e); 606 return $response;
594 } 607 }
595 608
596 /** 609 /**
diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
index ddeca4c0..1489a472 100644
--- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
+++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
@@ -431,18 +431,29 @@ class EntryRestControllerTest extends WallabagApiTestCase
431 $this->assertSame(404, $this->client->getResponse()->getStatusCode()); 431 $this->assertSame(404, $this->client->getResponse()->getStatusCode());
432 } 432 }
433 433
434 $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '.json'); 434 public function testDeleteEntryExpectId()
435 {
436 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
437 $entry = new Entry($em->getReference(User::class, 1));
438 $entry->setUrl('http://0.0.0.0/test-delete-entry-id');
439 $em->persist($entry);
440 $em->flush();
441
442 $em->clear();
443
444 $id = $entry->getId();
445
446 $this->client->request('DELETE', '/api/entries/' . $id . '.json?expect=id');
435 447
436 $this->assertSame(200, $this->client->getResponse()->getStatusCode()); 448 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
437 449
438 $content = json_decode($this->client->getResponse()->getContent(), true); 450 $content = json_decode($this->client->getResponse()->getContent(), true);
439 451
440 $this->assertSame($entry->getTitle(), $content['title']); 452 $this->assertSame($id, $content['id']);
441 $this->assertSame($entry->getUrl(), $content['url']); 453 $this->assertArrayNotHasKey('url', $content);
442 $this->assertSame($entry->getId(), $content['id']);
443 454
444 // We'll try to delete this entry again 455 // We'll try to delete this entry again
445 $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '.json'); 456 $this->client->request('DELETE', '/api/entries/' . $id . '.json');
446 457
447 $this->assertSame(404, $this->client->getResponse()->getStatusCode()); 458 $this->assertSame(404, $this->client->getResponse()->getStatusCode());
448 } 459 }