aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2015-10-30 20:57:10 +0100
committerNicolas LÅ“uillet <nicolas.loeuillet@smile.fr>2015-11-09 16:32:48 +0100
commitcceca9ea1d93ccf1420c2506330a16dc07f6433c (patch)
treee7105fc685d074ada622b55398dc9c7c2a475a2e /src/Wallabag/CoreBundle
parent268e9e7277d470dbd65b4eaa70c247ef35a95a3d (diff)
downloadwallabag-cceca9ea1d93ccf1420c2506330a16dc07f6433c.tar.gz
wallabag-cceca9ea1d93ccf1420c2506330a16dc07f6433c.tar.zst
wallabag-cceca9ea1d93ccf1420c2506330a16dc07f6433c.zip
Fix route parameters
Improve export tests Improve CSV export
Diffstat (limited to 'src/Wallabag/CoreBundle')
-rw-r--r--src/Wallabag/CoreBundle/Controller/ExportController.php11
-rw-r--r--src/Wallabag/CoreBundle/Helper/EntriesExport.php12
-rw-r--r--src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php93
3 files changed, 108 insertions, 8 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/ExportController.php b/src/Wallabag/CoreBundle/Controller/ExportController.php
index dd3cb7ca..c8ef49a2 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;
7use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 7use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
8use Wallabag\CoreBundle\Entity\Entry; 8use Wallabag\CoreBundle\Entity\Entry;
9 9
10/**
11 * The try/catch can be removed once all formats will be implemented.
12 * Still need implementation: txt.
13 */
10class ExportController extends Controller 14class ExportController extends Controller
11{ 15{
12 /** 16 /**
@@ -14,7 +18,10 @@ class ExportController extends Controller
14 * 18 *
15 * @param Entry $entry 19 * @param Entry $entry
16 * 20 *
17 * @Route("/export/{id}.{format}", requirements={"id" = "\d+"}, name="export_entry") 21 * @Route("/export/{id}.{format}", name="export_entry", requirements={
22 * "format": "epub|mobi|pdf|json|xml|txt|csv",
23 * "id": "\d+"
24 * })
18 */ 25 */
19 public function downloadEntryAction(Entry $entry, $format) 26 public function downloadEntryAction(Entry $entry, $format)
20 { 27 {
@@ -32,7 +39,7 @@ class ExportController extends Controller
32 * Export all entries for current user. 39 * Export all entries for current user.
33 * 40 *
34 * @Route("/export/{category}.{format}", name="export_entries", requirements={ 41 * @Route("/export/{category}.{format}", name="export_entries", requirements={
35 * "_format": "epub|mobi|pdf|json|xml|txt|csv", 42 * "format": "epub|mobi|pdf|json|xml|txt|csv",
36 * "category": "all|unread|starred|archive" 43 * "category": "all|unread|starred|archive"
37 * }) 44 * })
38 */ 45 */
diff --git a/src/Wallabag/CoreBundle/Helper/EntriesExport.php b/src/Wallabag/CoreBundle/Helper/EntriesExport.php
index c14f9d72..d6a4d094 100644
--- a/src/Wallabag/CoreBundle/Helper/EntriesExport.php
+++ b/src/Wallabag/CoreBundle/Helper/EntriesExport.php
@@ -9,6 +9,9 @@ use JMS\Serializer;
9use JMS\Serializer\SerializerBuilder; 9use JMS\Serializer\SerializerBuilder;
10use JMS\Serializer\SerializationContext; 10use JMS\Serializer\SerializationContext;
11 11
12/**
13 * This class doesn't have unit test BUT it's fully covered by a functional test with ExportControllerTest.
14 */
12class EntriesExport 15class EntriesExport
13{ 16{
14 private $wallabagUrl; 17 private $wallabagUrl;
@@ -303,7 +306,8 @@ class EntriesExport
303 array( 306 array(
304 $entry->getTitle(), 307 $entry->getTitle(),
305 $entry->getURL(), 308 $entry->getURL(),
306 $entry->getContent(), 309 // remove new line to avoid crazy results
310 str_replace(array("\r\n", "\r", "\n"), '', $entry->getContent()),
307 implode(', ', $entry->getTags()->toArray()), 311 implode(', ', $entry->getTags()->toArray()),
308 $entry->getMimetype(), 312 $entry->getMimetype(),
309 $entry->getLanguage(), 313 $entry->getLanguage(),
@@ -363,7 +367,11 @@ class EntriesExport
363 { 367 {
364 $serializer = SerializerBuilder::create()->build(); 368 $serializer = SerializerBuilder::create()->build();
365 369
366 return $serializer->serialize($this->entries, $format, SerializationContext::create()->setGroups(array('entries_for_user'))); 370 return $serializer->serialize(
371 $this->entries,
372 $format,
373 SerializationContext::create()->setGroups(array('entries_for_user'))
374 );
367 } 375 }
368 376
369 /** 377 /**
diff --git a/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php
index febdf4d4..3d3b97a9 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
21 $this->logInAs('admin'); 21 $this->logInAs('admin');
22 $client = $this->getClient(); 22 $client = $this->getClient();
23 23
24 $crawler = $client->request('GET', '/export/awesomeness.epub'); 24 $client->request('GET', '/export/awesomeness.epub');
25 25
26 $this->assertEquals(404, $client->getResponse()->getStatusCode()); 26 $this->assertEquals(404, $client->getResponse()->getStatusCode());
27 } 27 }
@@ -31,7 +31,34 @@ class ExportControllerTest extends WallabagCoreTestCase
31 $this->logInAs('admin'); 31 $this->logInAs('admin');
32 $client = $this->getClient(); 32 $client = $this->getClient();
33 33
34 $crawler = $client->request('GET', '/export/unread.xslx'); 34 $client->request('GET', '/export/unread.xslx');
35
36 $this->assertEquals(404, $client->getResponse()->getStatusCode());
37 }
38
39 public function testUnsupportedFormatExport()
40 {
41 $this->logInAs('admin');
42 $client = $this->getClient();
43
44 $client->request('GET', '/export/unread.txt');
45 $this->assertEquals(404, $client->getResponse()->getStatusCode());
46
47 $content = $client->getContainer()
48 ->get('doctrine.orm.entity_manager')
49 ->getRepository('WallabagCoreBundle:Entry')
50 ->findOneByUsernameAndNotArchived('admin');
51
52 $client->request('GET', '/export/'.$content->getId().'.txt');
53 $this->assertEquals(404, $client->getResponse()->getStatusCode());
54 }
55
56 public function testBadEntryId()
57 {
58 $this->logInAs('admin');
59 $client = $this->getClient();
60
61 $client->request('GET', '/export/0.mobi');
35 62
36 $this->assertEquals(404, $client->getResponse()->getStatusCode()); 63 $this->assertEquals(404, $client->getResponse()->getStatusCode());
37 } 64 }
@@ -97,20 +124,33 @@ class ExportControllerTest extends WallabagCoreTestCase
97 $this->logInAs('admin'); 124 $this->logInAs('admin');
98 $client = $this->getClient(); 125 $client = $this->getClient();
99 126
127 // to be sure results are the same
128 $contentInDB = $client->getContainer()
129 ->get('doctrine.orm.entity_manager')
130 ->getRepository('WallabagCoreBundle:Entry')
131 ->createQueryBuilder('e')
132 ->leftJoin('e.user', 'u')
133 ->where('u.username = :username')->setParameter('username', 'admin')
134 ->andWhere('e.isArchived = true')
135 ->getQuery()
136 ->getArrayResult();
137
100 ob_start(); 138 ob_start();
101 $crawler = $client->request('GET', '/export/unread.csv'); 139 $crawler = $client->request('GET', '/export/archive.csv');
102 ob_end_clean(); 140 ob_end_clean();
103 141
104 $this->assertEquals(200, $client->getResponse()->getStatusCode()); 142 $this->assertEquals(200, $client->getResponse()->getStatusCode());
105 143
106 $headers = $client->getResponse()->headers; 144 $headers = $client->getResponse()->headers;
107 $this->assertEquals('application/csv', $headers->get('content-type')); 145 $this->assertEquals('application/csv', $headers->get('content-type'));
108 $this->assertEquals('attachment; filename="Unread articles.csv"', $headers->get('content-disposition')); 146 $this->assertEquals('attachment; filename="Archive articles.csv"', $headers->get('content-disposition'));
109 $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding')); 147 $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding'));
110 148
111 $csv = str_getcsv($client->getResponse()->getContent(), "\n"); 149 $csv = str_getcsv($client->getResponse()->getContent(), "\n");
112 150
113 $this->assertGreaterThan(1, $csv); 151 $this->assertGreaterThan(1, $csv);
152 // +1 for title line
153 $this->assertEquals(count($contentInDB)+1, count($csv));
114 $this->assertEquals('Title;URL;Content;Tags;"MIME Type";Language', $csv[0]); 154 $this->assertEquals('Title;URL;Content;Tags;"MIME Type";Language', $csv[0]);
115 } 155 }
116 156
@@ -119,6 +159,16 @@ class ExportControllerTest extends WallabagCoreTestCase
119 $this->logInAs('admin'); 159 $this->logInAs('admin');
120 $client = $this->getClient(); 160 $client = $this->getClient();
121 161
162 // to be sure results are the same
163 $contentInDB = $client->getContainer()
164 ->get('doctrine.orm.entity_manager')
165 ->getRepository('WallabagCoreBundle:Entry')
166 ->createQueryBuilder('e')
167 ->leftJoin('e.user', 'u')
168 ->where('u.username = :username')->setParameter('username', 'admin')
169 ->getQuery()
170 ->getArrayResult();
171
122 ob_start(); 172 ob_start();
123 $crawler = $client->request('GET', '/export/all.json'); 173 $crawler = $client->request('GET', '/export/all.json');
124 ob_end_clean(); 174 ob_end_clean();
@@ -129,6 +179,21 @@ class ExportControllerTest extends WallabagCoreTestCase
129 $this->assertEquals('application/json', $headers->get('content-type')); 179 $this->assertEquals('application/json', $headers->get('content-type'));
130 $this->assertEquals('attachment; filename="All articles.json"', $headers->get('content-disposition')); 180 $this->assertEquals('attachment; filename="All articles.json"', $headers->get('content-disposition'));
131 $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding')); 181 $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding'));
182
183 $content = json_decode($client->getResponse()->getContent(), true);
184 $this->assertEquals(count($contentInDB), count($content));
185 $this->assertArrayHasKey('id', $content[0]);
186 $this->assertArrayHasKey('title', $content[0]);
187 $this->assertArrayHasKey('url', $content[0]);
188 $this->assertArrayHasKey('is_archived', $content[0]);
189 $this->assertArrayHasKey('is_starred', $content[0]);
190 $this->assertArrayHasKey('content', $content[0]);
191 $this->assertArrayHasKey('mimetype', $content[0]);
192 $this->assertArrayHasKey('language', $content[0]);
193 $this->assertArrayHasKey('reading_time', $content[0]);
194 $this->assertArrayHasKey('domain_name', $content[0]);
195 $this->assertArrayHasKey('preview_picture', $content[0]);
196 $this->assertArrayHasKey('tags', $content[0]);
132 } 197 }
133 198
134 public function testXmlExport() 199 public function testXmlExport()
@@ -136,6 +201,17 @@ class ExportControllerTest extends WallabagCoreTestCase
136 $this->logInAs('admin'); 201 $this->logInAs('admin');
137 $client = $this->getClient(); 202 $client = $this->getClient();
138 203
204 // to be sure results are the same
205 $contentInDB = $client->getContainer()
206 ->get('doctrine.orm.entity_manager')
207 ->getRepository('WallabagCoreBundle:Entry')
208 ->createQueryBuilder('e')
209 ->leftJoin('e.user', 'u')
210 ->where('u.username = :username')->setParameter('username', 'admin')
211 ->andWhere('e.isArchived = false')
212 ->getQuery()
213 ->getArrayResult();
214
139 ob_start(); 215 ob_start();
140 $crawler = $client->request('GET', '/export/unread.xml'); 216 $crawler = $client->request('GET', '/export/unread.xml');
141 ob_end_clean(); 217 ob_end_clean();
@@ -146,5 +222,14 @@ class ExportControllerTest extends WallabagCoreTestCase
146 $this->assertEquals('application/xml', $headers->get('content-type')); 222 $this->assertEquals('application/xml', $headers->get('content-type'));
147 $this->assertEquals('attachment; filename="Unread articles.xml"', $headers->get('content-disposition')); 223 $this->assertEquals('attachment; filename="Unread articles.xml"', $headers->get('content-disposition'));
148 $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding')); 224 $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding'));
225
226 $content = new \SimpleXMLElement($client->getResponse()->getContent());
227 $this->assertGreaterThan(0, $content->count());
228 $this->assertEquals(count($contentInDB), $content->count());
229 $this->assertNotEmpty('id', (string) $content->entry[0]->id);
230 $this->assertNotEmpty('title', (string) $content->entry[0]->title);
231 $this->assertNotEmpty('url', (string) $content->entry[0]->url);
232 $this->assertNotEmpty('content', (string) $content->entry[0]->content);
233 $this->assertNotEmpty('domain_name', (string) $content->entry[0]->domain_name);
149 } 234 }
150} 235}