From 4e0ed3368d2bdc2fa0d330e58902e792a896be06 Mon Sep 17 00:00:00 2001 From: Kevin Decherf Date: Sun, 17 Feb 2019 15:25:21 +0100 Subject: tests: create entry for testDeleteEntry, fix missing id When using the entity manager to retrieve an already stored entry, the id disapears from $entry after the first delete call. This leads to testing a nonexistent endpoint (api/entries/.json) during the second delete call. This change now creates an entry specifically for the test. Signed-off-by: Kevin Decherf --- .../Controller/EntryRestControllerTest.php | 36 +++++++++++++++++----- 1 file changed, 29 insertions(+), 7 deletions(-) (limited to 'tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php') diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php index 2a1d2e15..ddeca4c0 100644 --- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php @@ -400,14 +400,36 @@ class EntryRestControllerTest extends WallabagApiTestCase public function testDeleteEntry() { - $entry = $this->client->getContainer() - ->get('doctrine.orm.entity_manager') - ->getRepository('WallabagCoreBundle:Entry') - ->findOneByUser(1, ['id' => 'asc']); + $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); + $entry = new Entry($em->getReference(User::class, 1)); + $entry->setUrl('http://0.0.0.0/test-delete-entry'); + $entry->setTitle('Test delete entry'); + $em->persist($entry); + $em->flush(); - if (!$entry) { - $this->markTestSkipped('No content found in db.'); - } + $em->clear(); + + $e = [ + 'title' => $entry->getTitle(), + 'url' => $entry->getUrl(), + 'id' => $entry->getId(), + ]; + + $this->client->request('DELETE', '/api/entries/' . $e['id'] . '.json'); + + $this->assertSame(200, $this->client->getResponse()->getStatusCode()); + + $content = json_decode($this->client->getResponse()->getContent(), true); + + $this->assertSame($e['title'], $content['title']); + $this->assertSame($e['url'], $content['url']); + $this->assertSame($e['id'], $content['id']); + + // We'll try to delete this entry again + $this->client->request('DELETE', '/api/entries/' . $e['id'] . '.json'); + + $this->assertSame(404, $this->client->getResponse()->getStatusCode()); + } $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '.json'); -- cgit v1.2.3 From 508302042f96ce771a36f0114acb0b3a89c18880 Mon Sep 17 00:00:00 2001 From: Kevin Decherf Date: Sun, 17 Feb 2019 15:30:42 +0100 Subject: 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 --- .../Controller/EntryRestControllerTest.php | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php') 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 $this->assertSame(404, $this->client->getResponse()->getStatusCode()); } - $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '.json'); + public function testDeleteEntryExpectId() + { + $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); + $entry = new Entry($em->getReference(User::class, 1)); + $entry->setUrl('http://0.0.0.0/test-delete-entry-id'); + $em->persist($entry); + $em->flush(); + + $em->clear(); + + $id = $entry->getId(); + + $this->client->request('DELETE', '/api/entries/' . $id . '.json?expect=id'); $this->assertSame(200, $this->client->getResponse()->getStatusCode()); $content = json_decode($this->client->getResponse()->getContent(), true); - $this->assertSame($entry->getTitle(), $content['title']); - $this->assertSame($entry->getUrl(), $content['url']); - $this->assertSame($entry->getId(), $content['id']); + $this->assertSame($id, $content['id']); + $this->assertArrayNotHasKey('url', $content); // We'll try to delete this entry again - $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '.json'); + $this->client->request('DELETE', '/api/entries/' . $id . '.json'); $this->assertSame(404, $this->client->getResponse()->getStatusCode()); } -- cgit v1.2.3 From 12a97c352275d81e9252ca4179de51180403eb6f Mon Sep 17 00:00:00 2001 From: Kevin Decherf Date: Sun, 17 Feb 2019 18:19:51 +0100 Subject: tests: fix broken session clients for rest entry deletion tests Signed-off-by: Kevin Decherf --- .../ApiBundle/Controller/EntryRestControllerTest.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php') diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php index 1489a472..46b5f460 100644 --- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php @@ -426,9 +426,10 @@ class EntryRestControllerTest extends WallabagApiTestCase $this->assertSame($e['id'], $content['id']); // We'll try to delete this entry again - $this->client->request('DELETE', '/api/entries/' . $e['id'] . '.json'); + $client = $this->createAuthorizedClient(); + $client->request('DELETE', '/api/entries/' . $e['id'] . '.json'); - $this->assertSame(404, $this->client->getResponse()->getStatusCode()); + $this->assertSame(404, $client->getResponse()->getStatusCode()); } public function testDeleteEntryExpectId() @@ -453,9 +454,17 @@ class EntryRestControllerTest extends WallabagApiTestCase $this->assertArrayNotHasKey('url', $content); // We'll try to delete this entry again - $this->client->request('DELETE', '/api/entries/' . $id . '.json'); + $client = $this->createAuthorizedClient(); + $client->request('DELETE', '/api/entries/' . $id . '.json'); + + $this->assertSame(404, $client->getResponse()->getStatusCode()); + } + + public function testDeleteEntryExpectBadRequest() + { + $this->client->request('DELETE', '/api/entries/1.json?expect=badrequest'); - $this->assertSame(404, $this->client->getResponse()->getStatusCode()); + $this->assertSame(400, $this->client->getResponse()->getStatusCode()); } public function testPostEntry() -- cgit v1.2.3