]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/Controller/ExportControllerTest.php
Merge pull request #4438 from wallabag/dependabot/composer/scheb/two-factor-bundle...
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Controller / ExportControllerTest.php
1 <?php
2
3 namespace Tests\Wallabag\CoreBundle\Controller;
4
5 use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
6 use Wallabag\CoreBundle\Entity\Entry;
7
8 class ExportControllerTest extends WallabagCoreTestCase
9 {
10 private $adminEntry;
11 private $bobEntry;
12
13 public function testLogin()
14 {
15 $client = $this->getClient();
16
17 $client->request('GET', '/export/unread.csv');
18
19 $this->assertSame(302, $client->getResponse()->getStatusCode());
20 $this->assertContains('login', $client->getResponse()->headers->get('location'));
21 }
22
23 public function testUnknownCategoryExport()
24 {
25 $this->logInAs('admin');
26 $client = $this->getClient();
27
28 $client->request('GET', '/export/awesomeness.epub');
29
30 $this->assertSame(404, $client->getResponse()->getStatusCode());
31 }
32
33 public function testUnknownFormatExport()
34 {
35 $this->logInAs('admin');
36 $client = $this->getClient();
37
38 $client->request('GET', '/export/unread.xslx');
39
40 $this->assertSame(404, $client->getResponse()->getStatusCode());
41 }
42
43 public function testUnsupportedFormatExport()
44 {
45 $this->logInAs('admin');
46 $client = $this->getClient();
47
48 $client->request('GET', '/export/unread.doc');
49 $this->assertSame(404, $client->getResponse()->getStatusCode());
50
51 $content = $client->getContainer()
52 ->get('doctrine.orm.entity_manager')
53 ->getRepository('WallabagCoreBundle:Entry')
54 ->findOneByUsernameAndNotArchived('admin');
55
56 $client->request('GET', '/export/' . $content->getId() . '.doc');
57 $this->assertSame(404, $client->getResponse()->getStatusCode());
58 }
59
60 public function testBadEntryId()
61 {
62 $this->logInAs('admin');
63 $client = $this->getClient();
64
65 $client->request('GET', '/export/0.mobi');
66
67 $this->assertSame(404, $client->getResponse()->getStatusCode());
68 }
69
70 public function testEpubExport()
71 {
72 $this->logInAs('admin');
73 $client = $this->getClient();
74
75 ob_start();
76 $crawler = $client->request('GET', '/export/archive.epub');
77 ob_end_clean();
78
79 $this->assertSame(200, $client->getResponse()->getStatusCode());
80
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'));
85 }
86
87 public function testMobiExport()
88 {
89 $this->logInAs('admin');
90 $client = $this->getClient();
91
92 $content = $client->getContainer()
93 ->get('doctrine.orm.entity_manager')
94 ->getRepository('WallabagCoreBundle:Entry')
95 ->findOneByUsernameAndNotArchived('admin');
96
97 ob_start();
98 $crawler = $client->request('GET', '/export/' . $content->getId() . '.mobi');
99 ob_end_clean();
100
101 $this->assertSame(200, $client->getResponse()->getStatusCode());
102
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'));
107 }
108
109 public function testPdfExport()
110 {
111 $this->logInAs('admin');
112 $client = $this->getClient();
113
114 ob_start();
115 $crawler = $client->request('GET', '/export/all.pdf');
116 ob_end_clean();
117
118 $this->assertSame(200, $client->getResponse()->getStatusCode());
119
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'));
124
125 ob_start();
126 $crawler = $client->request('GET', '/export/tag_entries.pdf?tag=foo-bar');
127 ob_end_clean();
128
129 $this->assertSame(200, $client->getResponse()->getStatusCode());
130
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'));
135 }
136
137 public function testTxtExport()
138 {
139 $this->logInAs('admin');
140 $client = $this->getClient();
141
142 ob_start();
143 $crawler = $client->request('GET', '/export/all.txt');
144 ob_end_clean();
145
146 $this->assertSame(200, $client->getResponse()->getStatusCode());
147
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'));
152 }
153
154 public function testCsvExport()
155 {
156 $this->logInAs('admin');
157 $client = $this->getClient();
158
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')
164 ->select('e, t')
165 ->leftJoin('e.user', 'u')
166 ->leftJoin('e.tags', 't')
167 ->where('u.username = :username')->setParameter('username', 'admin')
168 ->andWhere('e.isArchived = true')
169 ->getQuery()
170 ->getArrayResult();
171
172 ob_start();
173 $crawler = $client->request('GET', '/export/archive.csv');
174 ob_end_clean();
175
176 $this->assertSame(200, $client->getResponse()->getStatusCode());
177
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'));
182
183 $csv = str_getcsv($client->getResponse()->getContent(), "\n");
184
185 $this->assertGreaterThan(1, $csv);
186 // +1 for title line
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]);
195
196 foreach ($contentInDB[0]['tags'] as $tag) {
197 $this->assertContains($tag['label'], $csv[1]);
198 }
199 }
200
201 public function testJsonExport()
202 {
203 $this->logInAs('admin');
204 $client = $this->getClient();
205
206 $contentInDB = $client->getContainer()
207 ->get('doctrine.orm.entity_manager')
208 ->getRepository('WallabagCoreBundle:Entry')
209 ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
210
211 ob_start();
212 $crawler = $client->request('GET', '/export/' . $contentInDB->getId() . '.json');
213 ob_end_clean();
214
215 $this->assertSame(200, $client->getResponse()->getStatusCode());
216
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'));
221
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]);
236
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']);
248 }
249
250 public function testJsonExportFromSearch()
251 {
252 $this->setUpForJsonExportFromSearch();
253
254 $this->logInAs('admin');
255 $client = $this->getClient();
256
257 ob_start();
258 $crawler = $client->request('GET', '/export/search.json?search_entry[term]=entry+search&currentRoute=homepage');
259 ob_end_clean();
260
261 $this->assertSame(200, $client->getResponse()->getStatusCode());
262
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'));
267
268 $content = json_decode($client->getResponse()->getContent(), true);
269 $this->assertCount(1, $content);
270
271 $this->tearDownForJsonExportFromSearch();
272 }
273
274 public function testXmlExport()
275 {
276 $this->logInAs('admin');
277 $client = $this->getClient();
278
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')
287 ->getQuery()
288 ->getArrayResult();
289
290 ob_start();
291 $crawler = $client->request('GET', '/export/unread.xml');
292 ob_end_clean();
293
294 $this->assertSame(200, $client->getResponse()->getStatusCode());
295
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'));
300
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);
311 }
312
313 private function setUpForJsonExportFromSearch()
314 {
315 $client = $this->getClient();
316 $em = $this->getEntityManager();
317
318 $userRepository = $client->getContainer()
319 ->get('wallabag_user.user_repository.test');
320
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);
327
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);
334
335 $em->flush();
336 }
337
338 private function tearDownForJsonExportFromSearch()
339 {
340 $em = $this->getEntityManager();
341
342 $em->remove($this->adminEntry);
343 $em->remove($this->bobEntry);
344
345 $em->flush();
346 }
347
348 private function getSanitizedFilename($title)
349 {
350 return preg_replace('/[^A-Za-z0-9\- \']/', '', iconv('utf-8', 'us-ascii//TRANSLIT', $title));
351 }
352 }