3 namespace Tests\Wallabag\CoreBundle\Controller
;
5 use Tests\Wallabag\CoreBundle\WallabagCoreTestCase
;
6 use Wallabag\CoreBundle\Entity\Entry
;
8 class ExportControllerTest
extends WallabagCoreTestCase
13 public function testLogin()
15 $client = $this->getClient();
17 $client->request('GET', '/export/unread.csv');
19 $this->assertSame(302, $client->getResponse()->getStatusCode());
20 $this->assertContains('login', $client->getResponse()->headers
->get('location'));
23 public function testUnknownCategoryExport()
25 $this->logInAs('admin');
26 $client = $this->getClient();
28 $client->request('GET', '/export/awesomeness.epub');
30 $this->assertSame(404, $client->getResponse()->getStatusCode());
33 public function testUnknownFormatExport()
35 $this->logInAs('admin');
36 $client = $this->getClient();
38 $client->request('GET', '/export/unread.xslx');
40 $this->assertSame(404, $client->getResponse()->getStatusCode());
43 public function testUnsupportedFormatExport()
45 $this->logInAs('admin');
46 $client = $this->getClient();
48 $client->request('GET', '/export/unread.doc');
49 $this->assertSame(404, $client->getResponse()->getStatusCode());
51 $content = $client->getContainer()
52 ->get('doctrine.orm.entity_manager')
53 ->getRepository('WallabagCoreBundle:Entry')
54 ->findOneByUsernameAndNotArchived('admin');
56 $client->request('GET', '/export/' . $content->getId() . '.doc');
57 $this->assertSame(404, $client->getResponse()->getStatusCode());
60 public function testBadEntryId()
62 $this->logInAs('admin');
63 $client = $this->getClient();
65 $client->request('GET', '/export/0.mobi');
67 $this->assertSame(404, $client->getResponse()->getStatusCode());
70 public function testEpubExport()
72 $this->logInAs('admin');
73 $client = $this->getClient();
76 $crawler = $client->request('GET', '/export/archive.epub');
79 $this->assertSame(200, $client->getResponse()->getStatusCode());
81 $headers = $client->getResponse()->headers
;
82 $this->assertSame('application/epub+zip', $headers->get('content-type'));
83 $this->assertSame('attachment; filename="Archive articles.epub"', $headers->get('content-disposition'));
84 $this->assertSame('binary', $headers->get('content-transfer-encoding'));
87 public function testMobiExport()
89 $this->logInAs('admin');
90 $client = $this->getClient();
92 $content = $client->getContainer()
93 ->get('doctrine.orm.entity_manager')
94 ->getRepository('WallabagCoreBundle:Entry')
95 ->findOneByUsernameAndNotArchived('admin');
98 $crawler = $client->request('GET', '/export/' . $content->getId() . '.mobi');
101 $this->assertSame(200, $client->getResponse()->getStatusCode());
103 $headers = $client->getResponse()->headers
;
104 $this->assertSame('application/x-mobipocket-ebook', $headers->get('content-type'));
105 $this->assertSame('attachment; filename="' . $this->getSanitizedFilename($content->getTitle()) . '.mobi"', $headers->get('content-disposition'));
106 $this->assertSame('binary', $headers->get('content-transfer-encoding'));
109 public function testPdfExport()
111 $this->logInAs('admin');
112 $client = $this->getClient();
115 $crawler = $client->request('GET', '/export/all.pdf');
118 $this->assertSame(200, $client->getResponse()->getStatusCode());
120 $headers = $client->getResponse()->headers
;
121 $this->assertSame('application/pdf', $headers->get('content-type'));
122 $this->assertSame('attachment; filename="All articles.pdf"', $headers->get('content-disposition'));
123 $this->assertSame('binary', $headers->get('content-transfer-encoding'));
126 $crawler = $client->request('GET', '/export/tag_entries.pdf?tag=foo-bar');
129 $this->assertSame(200, $client->getResponse()->getStatusCode());
131 $headers = $client->getResponse()->headers
;
132 $this->assertSame('application/pdf', $headers->get('content-type'));
133 $this->assertSame('attachment; filename="Tag foo bar articles.pdf"', $headers->get('content-disposition'));
134 $this->assertSame('binary', $headers->get('content-transfer-encoding'));
137 public function testTxtExport()
139 $this->logInAs('admin');
140 $client = $this->getClient();
143 $crawler = $client->request('GET', '/export/all.txt');
146 $this->assertSame(200, $client->getResponse()->getStatusCode());
148 $headers = $client->getResponse()->headers
;
149 $this->assertSame('text/plain; charset=UTF-8', $headers->get('content-type'));
150 $this->assertSame('attachment; filename="All articles.txt"', $headers->get('content-disposition'));
151 $this->assertSame('UTF-8', $headers->get('content-transfer-encoding'));
154 public function testCsvExport()
156 $this->logInAs('admin');
157 $client = $this->getClient();
159 // to be sure results are the same
160 $contentInDB = $client->getContainer()
161 ->get('doctrine.orm.entity_manager')
162 ->getRepository('WallabagCoreBundle:Entry')
163 ->createQueryBuilder('e')
165 ->leftJoin('e.user', 'u')
166 ->leftJoin('e.tags', 't')
167 ->where('u.username = :username')->setParameter('username', 'admin')
168 ->andWhere('e.isArchived = true')
173 $crawler = $client->request('GET', '/export/archive.csv');
176 $this->assertSame(200, $client->getResponse()->getStatusCode());
178 $headers = $client->getResponse()->headers
;
179 $this->assertSame('application/csv', $headers->get('content-type'));
180 $this->assertSame('attachment; filename="Archive articles.csv"', $headers->get('content-disposition'));
181 $this->assertSame('UTF-8', $headers->get('content-transfer-encoding'));
183 $csv = str_getcsv($client->getResponse()->getContent(), "\n");
185 $this->assertGreaterThan(1, $csv);
187 $this->assertCount(\
count($contentInDB) +
1, $csv);
188 $this->assertSame('Title;URL;Content;Tags;"MIME Type";Language;"Creation date"', $csv[0]);
189 $this->assertContains($contentInDB[0]['title'], $csv[1]);
190 $this->assertContains($contentInDB[0]['url'], $csv[1]);
191 $this->assertContains($contentInDB[0]['content'], $csv[1]);
192 $this->assertContains($contentInDB[0]['mimetype'], $csv[1]);
193 $this->assertContains($contentInDB[0]['language'], $csv[1]);
194 $this->assertContains($contentInDB[0]['createdAt']->format('d/m/Y h:i:s'), $csv[1]);
196 foreach ($contentInDB[0]['tags'] as $tag) {
197 $this->assertContains($tag['label'], $csv[1]);
201 public function testJsonExport()
203 $this->logInAs('admin');
204 $client = $this->getClient();
206 $contentInDB = $client->getContainer()
207 ->get('doctrine.orm.entity_manager')
208 ->getRepository('WallabagCoreBundle:Entry')
209 ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
212 $crawler = $client->request('GET', '/export/' . $contentInDB->getId() . '.json');
215 $this->assertSame(200, $client->getResponse()->getStatusCode());
217 $headers = $client->getResponse()->headers
;
218 $this->assertSame('application/json', $headers->get('content-type'));
219 $this->assertSame('attachment; filename="' . $this->getSanitizedFilename($contentInDB->getTitle()) . '.json"', $headers->get('content-disposition'));
220 $this->assertSame('UTF-8', $headers->get('content-transfer-encoding'));
222 $content = json_decode($client->getResponse()->getContent(), true);
223 $this->assertArrayHasKey('id', $content[0]);
224 $this->assertArrayHasKey('title', $content[0]);
225 $this->assertArrayHasKey('url', $content[0]);
226 $this->assertArrayHasKey('is_archived', $content[0]);
227 $this->assertArrayHasKey('is_starred', $content[0]);
228 $this->assertArrayHasKey('content', $content[0]);
229 $this->assertArrayHasKey('mimetype', $content[0]);
230 $this->assertArrayHasKey('language', $content[0]);
231 $this->assertArrayHasKey('reading_time', $content[0]);
232 $this->assertArrayHasKey('domain_name', $content[0]);
233 $this->assertArrayHasKey('tags', $content[0]);
234 $this->assertArrayHasKey('created_at', $content[0]);
235 $this->assertArrayHasKey('updated_at', $content[0]);
237 $this->assertSame((int) $contentInDB->isArchived(), $content[0]['is_archived']);
238 $this->assertSame((int) $contentInDB->isStarred(), $content[0]['is_starred']);
239 $this->assertSame($contentInDB->getTitle(), $content[0]['title']);
240 $this->assertSame($contentInDB->getUrl(), $content[0]['url']);
241 $this->assertSame([['text' => 'This is my annotation /o/', 'quote' => 'content']], $content[0]['annotations']);
242 $this->assertSame($contentInDB->getMimetype(), $content[0]['mimetype']);
243 $this->assertSame($contentInDB->getLanguage(), $content[0]['language']);
244 $this->assertSame($contentInDB->getReadingtime(), $content[0]['reading_time']);
245 $this->assertSame($contentInDB->getDomainname(), $content[0]['domain_name']);
246 $this->assertContains('baz', $content[0]['tags']);
247 $this->assertContains('foo', $content[0]['tags']);
250 public function testJsonExportFromSearch()
252 $this->setUpForJsonExportFromSearch();
254 $this->logInAs('admin');
255 $client = $this->getClient();
258 $crawler = $client->request('GET', '/export/search.json?search_entry[term]=entry+search¤tRoute=homepage');
261 $this->assertSame(200, $client->getResponse()->getStatusCode());
263 $headers = $client->getResponse()->headers
;
264 $this->assertSame('application/json', $headers->get('content-type'));
265 $this->assertSame('attachment; filename="Search entry search articles.json"', $headers->get('content-disposition'));
266 $this->assertSame('UTF-8', $headers->get('content-transfer-encoding'));
268 $content = json_decode($client->getResponse()->getContent(), true);
269 $this->assertCount(1, $content);
271 $this->tearDownForJsonExportFromSearch();
274 public function testXmlExport()
276 $this->logInAs('admin');
277 $client = $this->getClient();
279 // to be sure results are the same
280 $contentInDB = $client->getContainer()
281 ->get('doctrine.orm.entity_manager')
282 ->getRepository('WallabagCoreBundle:Entry')
283 ->createQueryBuilder('e')
284 ->leftJoin('e.user', 'u')
285 ->where('u.username = :username')->setParameter('username', 'admin')
286 ->andWhere('e.isArchived = false')
291 $crawler = $client->request('GET', '/export/unread.xml');
294 $this->assertSame(200, $client->getResponse()->getStatusCode());
296 $headers = $client->getResponse()->headers
;
297 $this->assertSame('application/xml', $headers->get('content-type'));
298 $this->assertSame('attachment; filename="Unread articles.xml"', $headers->get('content-disposition'));
299 $this->assertSame('UTF-8', $headers->get('content-transfer-encoding'));
301 $content = new \
SimpleXMLElement($client->getResponse()->getContent());
302 $this->assertGreaterThan(0, $content->count());
303 $this->assertSame(\
count($contentInDB), $content->count());
304 $this->assertNotEmpty('id', (string) $content->entry
[0]->id
);
305 $this->assertNotEmpty('title', (string) $content->entry
[0]->title
);
306 $this->assertNotEmpty('url', (string) $content->entry
[0]->url
);
307 $this->assertNotEmpty('content', (string) $content->entry
[0]->content
);
308 $this->assertNotEmpty('domain_name', (string) $content->entry
[0]->domain_name
);
309 $this->assertNotEmpty('created_at', (string) $content->entry
[0]->created_at
);
310 $this->assertNotEmpty('updated_at', (string) $content->entry
[0]->updated_at
);
313 private function setUpForJsonExportFromSearch()
315 $client = $this->getClient();
316 $em = $this->getEntityManager();
318 $userRepository = $client->getContainer()
319 ->get('wallabag_user.user_repository.test');
321 $user = $userRepository->findOneByUserName('admin');
322 $this->adminEntry
= new Entry($user);
323 $this->adminEntry
->setUrl('http://0.0.0.0/entry-search-admin');
324 $this->adminEntry
->setTitle('test title entry search admin');
325 $this->adminEntry
->setContent('this is my content /o/');
326 $em->persist($this->adminEntry
);
328 $user = $userRepository->findOneByUserName('bob');
329 $this->bobEntry
= new Entry($user);
330 $this->bobEntry
->setUrl('http://0.0.0.0/entry-search-bob');
331 $this->bobEntry
->setTitle('test title entry search bob');
332 $this->bobEntry
->setContent('this is my content /o/');
333 $em->persist($this->bobEntry
);
338 private function tearDownForJsonExportFromSearch()
340 $em = $this->getEntityManager();
342 $em->remove($this->adminEntry
);
343 $em->remove($this->bobEntry
);
348 private function getSanitizedFilename($title)
350 return preg_replace('/[^A-Za-z0-9\- \']/', '', iconv('utf-8', 'us-ascii//TRANSLIT', $title));