From cceca9ea1d93ccf1420c2506330a16dc07f6433c Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 30 Oct 2015 20:57:10 +0100 Subject: [PATCH] Fix route parameters Improve export tests Improve CSV export --- .../Controller/ExportController.php | 11 ++- .../CoreBundle/Helper/EntriesExport.php | 12 ++- .../Tests/Controller/ExportControllerTest.php | 93 ++++++++++++++++++- 3 files changed, 108 insertions(+), 8 deletions(-) diff --git a/src/Wallabag/CoreBundle/Controller/ExportController.php b/src/Wallabag/CoreBundle/Controller/ExportController.php index dd3cb7ca5..c8ef49a2f 100644 --- a/src/Wallabag/CoreBundle/Controller/ExportController.php +++ b/src/Wallabag/CoreBundle/Controller/ExportController.php @@ -7,6 +7,10 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Wallabag\CoreBundle\Entity\Entry; +/** + * The try/catch can be removed once all formats will be implemented. + * Still need implementation: txt. + */ class ExportController extends Controller { /** @@ -14,7 +18,10 @@ class ExportController extends Controller * * @param Entry $entry * - * @Route("/export/{id}.{format}", requirements={"id" = "\d+"}, name="export_entry") + * @Route("/export/{id}.{format}", name="export_entry", requirements={ + * "format": "epub|mobi|pdf|json|xml|txt|csv", + * "id": "\d+" + * }) */ public function downloadEntryAction(Entry $entry, $format) { @@ -32,7 +39,7 @@ class ExportController extends Controller * Export all entries for current user. * * @Route("/export/{category}.{format}", name="export_entries", requirements={ - * "_format": "epub|mobi|pdf|json|xml|txt|csv", + * "format": "epub|mobi|pdf|json|xml|txt|csv", * "category": "all|unread|starred|archive" * }) */ diff --git a/src/Wallabag/CoreBundle/Helper/EntriesExport.php b/src/Wallabag/CoreBundle/Helper/EntriesExport.php index c14f9d72f..d6a4d094a 100644 --- a/src/Wallabag/CoreBundle/Helper/EntriesExport.php +++ b/src/Wallabag/CoreBundle/Helper/EntriesExport.php @@ -9,6 +9,9 @@ use JMS\Serializer; use JMS\Serializer\SerializerBuilder; use JMS\Serializer\SerializationContext; +/** + * This class doesn't have unit test BUT it's fully covered by a functional test with ExportControllerTest. + */ class EntriesExport { private $wallabagUrl; @@ -303,7 +306,8 @@ class EntriesExport array( $entry->getTitle(), $entry->getURL(), - $entry->getContent(), + // remove new line to avoid crazy results + str_replace(array("\r\n", "\r", "\n"), '', $entry->getContent()), implode(', ', $entry->getTags()->toArray()), $entry->getMimetype(), $entry->getLanguage(), @@ -363,7 +367,11 @@ class EntriesExport { $serializer = SerializerBuilder::create()->build(); - return $serializer->serialize($this->entries, $format, SerializationContext::create()->setGroups(array('entries_for_user'))); + return $serializer->serialize( + $this->entries, + $format, + SerializationContext::create()->setGroups(array('entries_for_user')) + ); } /** diff --git a/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php index febdf4d4c..3d3b97a99 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php @@ -21,7 +21,7 @@ class ExportControllerTest extends WallabagCoreTestCase $this->logInAs('admin'); $client = $this->getClient(); - $crawler = $client->request('GET', '/export/awesomeness.epub'); + $client->request('GET', '/export/awesomeness.epub'); $this->assertEquals(404, $client->getResponse()->getStatusCode()); } @@ -31,7 +31,34 @@ class ExportControllerTest extends WallabagCoreTestCase $this->logInAs('admin'); $client = $this->getClient(); - $crawler = $client->request('GET', '/export/unread.xslx'); + $client->request('GET', '/export/unread.xslx'); + + $this->assertEquals(404, $client->getResponse()->getStatusCode()); + } + + public function testUnsupportedFormatExport() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $client->request('GET', '/export/unread.txt'); + $this->assertEquals(404, $client->getResponse()->getStatusCode()); + + $content = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->findOneByUsernameAndNotArchived('admin'); + + $client->request('GET', '/export/'.$content->getId().'.txt'); + $this->assertEquals(404, $client->getResponse()->getStatusCode()); + } + + public function testBadEntryId() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $client->request('GET', '/export/0.mobi'); $this->assertEquals(404, $client->getResponse()->getStatusCode()); } @@ -97,20 +124,33 @@ class ExportControllerTest extends WallabagCoreTestCase $this->logInAs('admin'); $client = $this->getClient(); + // to be sure results are the same + $contentInDB = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->createQueryBuilder('e') + ->leftJoin('e.user', 'u') + ->where('u.username = :username')->setParameter('username', 'admin') + ->andWhere('e.isArchived = true') + ->getQuery() + ->getArrayResult(); + ob_start(); - $crawler = $client->request('GET', '/export/unread.csv'); + $crawler = $client->request('GET', '/export/archive.csv'); ob_end_clean(); $this->assertEquals(200, $client->getResponse()->getStatusCode()); $headers = $client->getResponse()->headers; $this->assertEquals('application/csv', $headers->get('content-type')); - $this->assertEquals('attachment; filename="Unread articles.csv"', $headers->get('content-disposition')); + $this->assertEquals('attachment; filename="Archive articles.csv"', $headers->get('content-disposition')); $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding')); $csv = str_getcsv($client->getResponse()->getContent(), "\n"); $this->assertGreaterThan(1, $csv); + // +1 for title line + $this->assertEquals(count($contentInDB)+1, count($csv)); $this->assertEquals('Title;URL;Content;Tags;"MIME Type";Language', $csv[0]); } @@ -119,6 +159,16 @@ class ExportControllerTest extends WallabagCoreTestCase $this->logInAs('admin'); $client = $this->getClient(); + // to be sure results are the same + $contentInDB = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->createQueryBuilder('e') + ->leftJoin('e.user', 'u') + ->where('u.username = :username')->setParameter('username', 'admin') + ->getQuery() + ->getArrayResult(); + ob_start(); $crawler = $client->request('GET', '/export/all.json'); ob_end_clean(); @@ -129,6 +179,21 @@ class ExportControllerTest extends WallabagCoreTestCase $this->assertEquals('application/json', $headers->get('content-type')); $this->assertEquals('attachment; filename="All articles.json"', $headers->get('content-disposition')); $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding')); + + $content = json_decode($client->getResponse()->getContent(), true); + $this->assertEquals(count($contentInDB), count($content)); + $this->assertArrayHasKey('id', $content[0]); + $this->assertArrayHasKey('title', $content[0]); + $this->assertArrayHasKey('url', $content[0]); + $this->assertArrayHasKey('is_archived', $content[0]); + $this->assertArrayHasKey('is_starred', $content[0]); + $this->assertArrayHasKey('content', $content[0]); + $this->assertArrayHasKey('mimetype', $content[0]); + $this->assertArrayHasKey('language', $content[0]); + $this->assertArrayHasKey('reading_time', $content[0]); + $this->assertArrayHasKey('domain_name', $content[0]); + $this->assertArrayHasKey('preview_picture', $content[0]); + $this->assertArrayHasKey('tags', $content[0]); } public function testXmlExport() @@ -136,6 +201,17 @@ class ExportControllerTest extends WallabagCoreTestCase $this->logInAs('admin'); $client = $this->getClient(); + // to be sure results are the same + $contentInDB = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->createQueryBuilder('e') + ->leftJoin('e.user', 'u') + ->where('u.username = :username')->setParameter('username', 'admin') + ->andWhere('e.isArchived = false') + ->getQuery() + ->getArrayResult(); + ob_start(); $crawler = $client->request('GET', '/export/unread.xml'); ob_end_clean(); @@ -146,5 +222,14 @@ class ExportControllerTest extends WallabagCoreTestCase $this->assertEquals('application/xml', $headers->get('content-type')); $this->assertEquals('attachment; filename="Unread articles.xml"', $headers->get('content-disposition')); $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding')); + + $content = new \SimpleXMLElement($client->getResponse()->getContent()); + $this->assertGreaterThan(0, $content->count()); + $this->assertEquals(count($contentInDB), $content->count()); + $this->assertNotEmpty('id', (string) $content->entry[0]->id); + $this->assertNotEmpty('title', (string) $content->entry[0]->title); + $this->assertNotEmpty('url', (string) $content->entry[0]->url); + $this->assertNotEmpty('content', (string) $content->entry[0]->content); + $this->assertNotEmpty('domain_name', (string) $content->entry[0]->domain_name); } }