aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJérémy Benoist <j0k3r@users.noreply.github.com>2019-02-25 10:56:07 +0100
committerGitHub <noreply@github.com>2019-02-25 10:56:07 +0100
commit4de99d1ab161fc378e0f7cc121c60396f86bc577 (patch)
tree7495caff11c8183fb1aeed9abebf38c6a4a2207f
parentb1992b340e21b6846a1ec2ae6ddd7217f3b24fb5 (diff)
parent12a97c352275d81e9252ca4179de51180403eb6f (diff)
downloadwallabag-4de99d1ab161fc378e0f7cc121c60396f86bc577.tar.gz
wallabag-4de99d1ab161fc378e0f7cc121c60396f86bc577.tar.zst
wallabag-4de99d1ab161fc378e0f7cc121c60396f86bc577.zip
Merge pull request #3887 from wallabag/issue-3711
Add support of expect parameter to change return object when deleting entry
-rw-r--r--src/Wallabag/ApiBundle/Controller/EntryRestController.php21
-rw-r--r--tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php68
2 files changed, 72 insertions, 17 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 2a1d2e15..46b5f460 100644
--- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
+++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
@@ -400,29 +400,71 @@ class EntryRestControllerTest extends WallabagApiTestCase
400 400
401 public function testDeleteEntry() 401 public function testDeleteEntry()
402 { 402 {
403 $entry = $this->client->getContainer() 403 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
404 ->get('doctrine.orm.entity_manager') 404 $entry = new Entry($em->getReference(User::class, 1));
405 ->getRepository('WallabagCoreBundle:Entry') 405 $entry->setUrl('http://0.0.0.0/test-delete-entry');
406 ->findOneByUser(1, ['id' => 'asc']); 406 $entry->setTitle('Test delete entry');
407 $em->persist($entry);
408 $em->flush();
407 409
408 if (!$entry) { 410 $em->clear();
409 $this->markTestSkipped('No content found in db.');
410 }
411 411
412 $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '.json'); 412 $e = [
413 'title' => $entry->getTitle(),
414 'url' => $entry->getUrl(),
415 'id' => $entry->getId(),
416 ];
417
418 $this->client->request('DELETE', '/api/entries/' . $e['id'] . '.json');
413 419
414 $this->assertSame(200, $this->client->getResponse()->getStatusCode()); 420 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
415 421
416 $content = json_decode($this->client->getResponse()->getContent(), true); 422 $content = json_decode($this->client->getResponse()->getContent(), true);
417 423
418 $this->assertSame($entry->getTitle(), $content['title']); 424 $this->assertSame($e['title'], $content['title']);
419 $this->assertSame($entry->getUrl(), $content['url']); 425 $this->assertSame($e['url'], $content['url']);
420 $this->assertSame($entry->getId(), $content['id']); 426 $this->assertSame($e['id'], $content['id']);
421 427
422 // We'll try to delete this entry again 428 // We'll try to delete this entry again
423 $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '.json'); 429 $client = $this->createAuthorizedClient();
430 $client->request('DELETE', '/api/entries/' . $e['id'] . '.json');
431
432 $this->assertSame(404, $client->getResponse()->getStatusCode());
433 }
424 434
425 $this->assertSame(404, $this->client->getResponse()->getStatusCode()); 435 public function testDeleteEntryExpectId()
436 {
437 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
438 $entry = new Entry($em->getReference(User::class, 1));
439 $entry->setUrl('http://0.0.0.0/test-delete-entry-id');
440 $em->persist($entry);
441 $em->flush();
442
443 $em->clear();
444
445 $id = $entry->getId();
446
447 $this->client->request('DELETE', '/api/entries/' . $id . '.json?expect=id');
448
449 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
450
451 $content = json_decode($this->client->getResponse()->getContent(), true);
452
453 $this->assertSame($id, $content['id']);
454 $this->assertArrayNotHasKey('url', $content);
455
456 // We'll try to delete this entry again
457 $client = $this->createAuthorizedClient();
458 $client->request('DELETE', '/api/entries/' . $id . '.json');
459
460 $this->assertSame(404, $client->getResponse()->getStatusCode());
461 }
462
463 public function testDeleteEntryExpectBadRequest()
464 {
465 $this->client->request('DELETE', '/api/entries/1.json?expect=badrequest');
466
467 $this->assertSame(400, $this->client->getResponse()->getStatusCode());
426 } 468 }
427 469
428 public function testPostEntry() 470 public function testPostEntry()