3 namespace Tests\Wallabag\CoreBundle\Controller
;
5 use Tests\Wallabag\CoreBundle\WallabagCoreTestCase
;
7 class ExportControllerTest
extends WallabagCoreTestCase
9 public function testLogin()
11 $client = $this->getClient();
13 $client->request('GET', '/export/unread.csv');
15 $this->assertSame(302, $client->getResponse()->getStatusCode());
16 $this->assertContains('login', $client->getResponse()->headers
->get('location'));
19 public function testUnknownCategoryExport()
21 $this->logInAs('admin');
22 $client = $this->getClient();
24 $client->request('GET', '/export/awesomeness.epub');
26 $this->assertSame(404, $client->getResponse()->getStatusCode());
29 public function testUnknownFormatExport()
31 $this->logInAs('admin');
32 $client = $this->getClient();
34 $client->request('GET', '/export/unread.xslx');
36 $this->assertSame(404, $client->getResponse()->getStatusCode());
39 public function testUnsupportedFormatExport()
41 $this->logInAs('admin');
42 $client = $this->getClient();
44 $client->request('GET', '/export/unread.doc');
45 $this->assertSame(404, $client->getResponse()->getStatusCode());
47 $content = $client->getContainer()
48 ->get('doctrine.orm.entity_manager')
49 ->getRepository('WallabagCoreBundle:Entry')
50 ->findOneByUsernameAndNotArchived('admin');
52 $client->request('GET', '/export/' . $content->getId() . '.doc');
53 $this->assertSame(404, $client->getResponse()->getStatusCode());
56 public function testBadEntryId()
58 $this->logInAs('admin');
59 $client = $this->getClient();
61 $client->request('GET', '/export/0.mobi');
63 $this->assertSame(404, $client->getResponse()->getStatusCode());
66 public function testEpubExport()
68 $this->logInAs('admin');
69 $client = $this->getClient();
72 $crawler = $client->request('GET', '/export/archive.epub');
75 $this->assertSame(200, $client->getResponse()->getStatusCode());
77 $headers = $client->getResponse()->headers
;
78 $this->assertSame('application/epub+zip', $headers->get('content-type'));
79 $this->assertSame('attachment; filename="Archive articles.epub"', $headers->get('content-disposition'));
80 $this->assertSame('binary', $headers->get('content-transfer-encoding'));
83 public function testMobiExport()
85 $this->logInAs('admin');
86 $client = $this->getClient();
88 $content = $client->getContainer()
89 ->get('doctrine.orm.entity_manager')
90 ->getRepository('WallabagCoreBundle:Entry')
91 ->findOneByUsernameAndNotArchived('admin');
94 $crawler = $client->request('GET', '/export/' . $content->getId() . '.mobi');
97 $this->assertSame(200, $client->getResponse()->getStatusCode());
99 $headers = $client->getResponse()->headers
;
100 $this->assertSame('application/x-mobipocket-ebook', $headers->get('content-type'));
101 $this->assertSame('attachment; filename="' . $this->getSanitizedFilename($content->getTitle()) . '.mobi"', $headers->get('content-disposition'));
102 $this->assertSame('binary', $headers->get('content-transfer-encoding'));
105 public function testPdfExport()
107 $this->logInAs('admin');
108 $client = $this->getClient();
111 $crawler = $client->request('GET', '/export/all.pdf');
114 $this->assertSame(200, $client->getResponse()->getStatusCode());
116 $headers = $client->getResponse()->headers
;
117 $this->assertSame('application/pdf', $headers->get('content-type'));
118 $this->assertSame('attachment; filename="All articles.pdf"', $headers->get('content-disposition'));
119 $this->assertSame('binary', $headers->get('content-transfer-encoding'));
122 $crawler = $client->request('GET', '/export/tag_entries.pdf?tag=foo-bar');
125 $this->assertSame(200, $client->getResponse()->getStatusCode());
127 $headers = $client->getResponse()->headers
;
128 $this->assertSame('application/pdf', $headers->get('content-type'));
129 $this->assertSame('attachment; filename="Tag foo bar articles.pdf"', $headers->get('content-disposition'));
130 $this->assertSame('binary', $headers->get('content-transfer-encoding'));
133 public function testTxtExport()
135 $this->logInAs('admin');
136 $client = $this->getClient();
139 $crawler = $client->request('GET', '/export/all.txt');
142 $this->assertSame(200, $client->getResponse()->getStatusCode());
144 $headers = $client->getResponse()->headers
;
145 $this->assertSame('text/plain; charset=UTF-8', $headers->get('content-type'));
146 $this->assertSame('attachment; filename="All articles.txt"', $headers->get('content-disposition'));
147 $this->assertSame('UTF-8', $headers->get('content-transfer-encoding'));
150 public function testCsvExport()
152 $this->logInAs('admin');
153 $client = $this->getClient();
155 // to be sure results are the same
156 $contentInDB = $client->getContainer()
157 ->get('doctrine.orm.entity_manager')
158 ->getRepository('WallabagCoreBundle:Entry')
159 ->createQueryBuilder('e')
161 ->leftJoin('e.user', 'u')
162 ->leftJoin('e.tags', 't')
163 ->where('u.username = :username')->setParameter('username', 'admin')
164 ->andWhere('e.isArchived = true')
169 $crawler = $client->request('GET', '/export/archive.csv');
172 $this->assertSame(200, $client->getResponse()->getStatusCode());
174 $headers = $client->getResponse()->headers
;
175 $this->assertSame('application/csv', $headers->get('content-type'));
176 $this->assertSame('attachment; filename="Archive articles.csv"', $headers->get('content-disposition'));
177 $this->assertSame('UTF-8', $headers->get('content-transfer-encoding'));
179 $csv = str_getcsv($client->getResponse()->getContent(), "\n");
181 $this->assertGreaterThan(1, $csv);
183 $this->assertCount(\
count($contentInDB) +
1, $csv);
184 $this->assertSame('Title;URL;Content;Tags;"MIME Type";Language;"Creation date"', $csv[0]);
185 $this->assertContains($contentInDB[0]['title'], $csv[1]);
186 $this->assertContains($contentInDB[0]['url'], $csv[1]);
187 $this->assertContains($contentInDB[0]['content'], $csv[1]);
188 $this->assertContains($contentInDB[0]['mimetype'], $csv[1]);
189 $this->assertContains($contentInDB[0]['language'], $csv[1]);
190 $this->assertContains($contentInDB[0]['createdAt']->format('d/m/Y h:i:s'), $csv[1]);
192 foreach ($contentInDB[0]['tags'] as $tag) {
193 $this->assertContains($tag['label'], $csv[1]);
197 public function testJsonExport()
199 $this->logInAs('admin');
200 $client = $this->getClient();
202 $contentInDB = $client->getContainer()
203 ->get('doctrine.orm.entity_manager')
204 ->getRepository('WallabagCoreBundle:Entry')
205 ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
208 $crawler = $client->request('GET', '/export/' . $contentInDB->getId() . '.json');
211 $this->assertSame(200, $client->getResponse()->getStatusCode());
213 $headers = $client->getResponse()->headers
;
214 $this->assertSame('application/json', $headers->get('content-type'));
215 $this->assertSame('attachment; filename="' . $this->getSanitizedFilename($contentInDB->getTitle()) . '.json"', $headers->get('content-disposition'));
216 $this->assertSame('UTF-8', $headers->get('content-transfer-encoding'));
218 $content = json_decode($client->getResponse()->getContent(), true);
219 $this->assertArrayHasKey('id', $content[0]);
220 $this->assertArrayHasKey('title', $content[0]);
221 $this->assertArrayHasKey('url', $content[0]);
222 $this->assertArrayHasKey('is_archived', $content[0]);
223 $this->assertArrayHasKey('is_starred', $content[0]);
224 $this->assertArrayHasKey('content', $content[0]);
225 $this->assertArrayHasKey('mimetype', $content[0]);
226 $this->assertArrayHasKey('language', $content[0]);
227 $this->assertArrayHasKey('reading_time', $content[0]);
228 $this->assertArrayHasKey('domain_name', $content[0]);
229 $this->assertArrayHasKey('tags', $content[0]);
230 $this->assertArrayHasKey('created_at', $content[0]);
231 $this->assertArrayHasKey('updated_at', $content[0]);
233 $this->assertSame((int) $contentInDB->isArchived(), $content[0]['is_archived']);
234 $this->assertSame((int) $contentInDB->isStarred(), $content[0]['is_starred']);
235 $this->assertSame($contentInDB->getTitle(), $content[0]['title']);
236 $this->assertSame($contentInDB->getUrl(), $content[0]['url']);
237 $this->assertSame([['text' => 'This is my annotation /o/', 'quote' => 'content']], $content[0]['annotations']);
238 $this->assertSame($contentInDB->getMimetype(), $content[0]['mimetype']);
239 $this->assertSame($contentInDB->getLanguage(), $content[0]['language']);
240 $this->assertSame($contentInDB->getReadingtime(), $content[0]['reading_time']);
241 $this->assertSame($contentInDB->getDomainname(), $content[0]['domain_name']);
242 $this->assertContains('baz', $content[0]['tags']);
243 $this->assertContains('foo', $content[0]['tags']);
246 public function testXmlExport()
248 $this->logInAs('admin');
249 $client = $this->getClient();
251 // to be sure results are the same
252 $contentInDB = $client->getContainer()
253 ->get('doctrine.orm.entity_manager')
254 ->getRepository('WallabagCoreBundle:Entry')
255 ->createQueryBuilder('e')
256 ->leftJoin('e.user', 'u')
257 ->where('u.username = :username')->setParameter('username', 'admin')
258 ->andWhere('e.isArchived = false')
263 $crawler = $client->request('GET', '/export/unread.xml');
266 $this->assertSame(200, $client->getResponse()->getStatusCode());
268 $headers = $client->getResponse()->headers
;
269 $this->assertSame('application/xml', $headers->get('content-type'));
270 $this->assertSame('attachment; filename="Unread articles.xml"', $headers->get('content-disposition'));
271 $this->assertSame('UTF-8', $headers->get('content-transfer-encoding'));
273 $content = new \
SimpleXMLElement($client->getResponse()->getContent());
274 $this->assertGreaterThan(0, $content->count());
275 $this->assertSame(\
count($contentInDB), $content->count());
276 $this->assertNotEmpty('id', (string) $content->entry
[0]->id
);
277 $this->assertNotEmpty('title', (string) $content->entry
[0]->title
);
278 $this->assertNotEmpty('url', (string) $content->entry
[0]->url
);
279 $this->assertNotEmpty('content', (string) $content->entry
[0]->content
);
280 $this->assertNotEmpty('domain_name', (string) $content->entry
[0]->domain_name
);
281 $this->assertNotEmpty('created_at', (string) $content->entry
[0]->created_at
);
282 $this->assertNotEmpty('updated_at', (string) $content->entry
[0]->updated_at
);
285 private function getSanitizedFilename($title)
287 return preg_replace('/[^A-Za-z0-9\- \']/', '', iconv('utf-8', 'us-ascii//TRANSLIT', $title));