]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/CoreBundle/Controller/ExportControllerTest.php
Fix tests
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Controller / ExportControllerTest.php
CommitLineData
add597ba
JB
1<?php
2
23634d5d 3namespace Tests\Wallabag\CoreBundle\Controller;
add597ba 4
23634d5d 5use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
add597ba
JB
6
7class ExportControllerTest extends WallabagCoreTestCase
8{
9 public function testLogin()
10 {
11 $client = $this->getClient();
12
13 $client->request('GET', '/export/unread.csv');
14
f808b016 15 $this->assertSame(302, $client->getResponse()->getStatusCode());
add597ba
JB
16 $this->assertContains('login', $client->getResponse()->headers->get('location'));
17 }
18
19 public function testUnknownCategoryExport()
20 {
21 $this->logInAs('admin');
22 $client = $this->getClient();
23
cceca9ea 24 $client->request('GET', '/export/awesomeness.epub');
add597ba 25
f808b016 26 $this->assertSame(404, $client->getResponse()->getStatusCode());
add597ba
JB
27 }
28
29 public function testUnknownFormatExport()
30 {
31 $this->logInAs('admin');
32 $client = $this->getClient();
33
cceca9ea
JB
34 $client->request('GET', '/export/unread.xslx');
35
f808b016 36 $this->assertSame(404, $client->getResponse()->getStatusCode());
cceca9ea
JB
37 }
38
39 public function testUnsupportedFormatExport()
40 {
41 $this->logInAs('admin');
42 $client = $this->getClient();
43
3a2ada0b 44 $client->request('GET', '/export/unread.doc');
f808b016 45 $this->assertSame(404, $client->getResponse()->getStatusCode());
cceca9ea
JB
46
47 $content = $client->getContainer()
48 ->get('doctrine.orm.entity_manager')
49 ->getRepository('WallabagCoreBundle:Entry')
50 ->findOneByUsernameAndNotArchived('admin');
51
f808b016
JB
52 $client->request('GET', '/export/' . $content->getId() . '.doc');
53 $this->assertSame(404, $client->getResponse()->getStatusCode());
cceca9ea
JB
54 }
55
56 public function testBadEntryId()
57 {
58 $this->logInAs('admin');
59 $client = $this->getClient();
60
61 $client->request('GET', '/export/0.mobi');
add597ba 62
f808b016 63 $this->assertSame(404, $client->getResponse()->getStatusCode());
add597ba
JB
64 }
65
66 public function testEpubExport()
67 {
68 $this->logInAs('admin');
69 $client = $this->getClient();
70
71 ob_start();
72 $crawler = $client->request('GET', '/export/archive.epub');
73 ob_end_clean();
74
f808b016 75 $this->assertSame(200, $client->getResponse()->getStatusCode());
add597ba
JB
76
77 $headers = $client->getResponse()->headers;
f808b016
JB
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'));
add597ba
JB
81 }
82
83 public function testMobiExport()
84 {
85 $this->logInAs('admin');
86 $client = $this->getClient();
87
88 $content = $client->getContainer()
89 ->get('doctrine.orm.entity_manager')
90 ->getRepository('WallabagCoreBundle:Entry')
91 ->findOneByUsernameAndNotArchived('admin');
92
93 ob_start();
f808b016 94 $crawler = $client->request('GET', '/export/' . $content->getId() . '.mobi');
add597ba
JB
95 ob_end_clean();
96
f808b016 97 $this->assertSame(200, $client->getResponse()->getStatusCode());
add597ba
JB
98
99 $headers = $client->getResponse()->headers;
f808b016 100 $this->assertSame('application/x-mobipocket-ebook', $headers->get('content-type'));
dac93644 101 $this->assertSame('attachment; filename="' . $this->getSanitizedFilename($content->getTitle()) . '.mobi"', $headers->get('content-disposition'));
f808b016 102 $this->assertSame('binary', $headers->get('content-transfer-encoding'));
add597ba
JB
103 }
104
105 public function testPdfExport()
106 {
107 $this->logInAs('admin');
108 $client = $this->getClient();
109
110 ob_start();
111 $crawler = $client->request('GET', '/export/all.pdf');
112 ob_end_clean();
113
f808b016 114 $this->assertSame(200, $client->getResponse()->getStatusCode());
add597ba
JB
115
116 $headers = $client->getResponse()->headers;
f808b016
JB
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'));
794ac861
NL
120
121 ob_start();
c8de7ab9 122 $crawler = $client->request('GET', '/export/tag_entries.pdf?tag=foo-bar');
794ac861
NL
123 ob_end_clean();
124
f808b016 125 $this->assertSame(200, $client->getResponse()->getStatusCode());
794ac861
NL
126
127 $headers = $client->getResponse()->headers;
f808b016 128 $this->assertSame('application/pdf', $headers->get('content-type'));
dac93644 129 $this->assertSame('attachment; filename="Tag foo bar articles.pdf"', $headers->get('content-disposition'));
f808b016 130 $this->assertSame('binary', $headers->get('content-transfer-encoding'));
add597ba
JB
131 }
132
3a2ada0b
TC
133 public function testTxtExport()
134 {
135 $this->logInAs('admin');
136 $client = $this->getClient();
137
138 ob_start();
139 $crawler = $client->request('GET', '/export/all.txt');
140 ob_end_clean();
141
f808b016 142 $this->assertSame(200, $client->getResponse()->getStatusCode());
3a2ada0b
TC
143
144 $headers = $client->getResponse()->headers;
f808b016
JB
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'));
3a2ada0b
TC
148 }
149
add597ba
JB
150 public function testCsvExport()
151 {
152 $this->logInAs('admin');
153 $client = $this->getClient();
154
cceca9ea
JB
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')
b0458874 160 ->select('e, t')
cceca9ea 161 ->leftJoin('e.user', 'u')
b0458874 162 ->leftJoin('e.tags', 't')
cceca9ea
JB
163 ->where('u.username = :username')->setParameter('username', 'admin')
164 ->andWhere('e.isArchived = true')
165 ->getQuery()
166 ->getArrayResult();
167
add597ba 168 ob_start();
cceca9ea 169 $crawler = $client->request('GET', '/export/archive.csv');
add597ba
JB
170 ob_end_clean();
171
f808b016 172 $this->assertSame(200, $client->getResponse()->getStatusCode());
add597ba
JB
173
174 $headers = $client->getResponse()->headers;
f808b016
JB
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'));
add597ba
JB
178
179 $csv = str_getcsv($client->getResponse()->getContent(), "\n");
180
181 $this->assertGreaterThan(1, $csv);
cceca9ea 182 // +1 for title line
1e0d8ad7 183 $this->assertCount(\count($contentInDB) + 1, $csv);
f808b016 184 $this->assertSame('Title;URL;Content;Tags;"MIME Type";Language;"Creation date"', $csv[0]);
b0458874
JB
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]);
191
b0458874 192 foreach ($contentInDB[0]['tags'] as $tag) {
a162b1a9 193 $this->assertContains($tag['label'], $csv[1]);
b0458874 194 }
add597ba 195 }
b3cc1a14
TC
196
197 public function testJsonExport()
198 {
199 $this->logInAs('admin');
200 $client = $this->getClient();
201
cceca9ea
JB
202 $contentInDB = $client->getContainer()
203 ->get('doctrine.orm.entity_manager')
204 ->getRepository('WallabagCoreBundle:Entry')
74e1f743 205 ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
cceca9ea 206
b3cc1a14 207 ob_start();
f808b016 208 $crawler = $client->request('GET', '/export/' . $contentInDB->getId() . '.json');
b3cc1a14
TC
209 ob_end_clean();
210
f808b016 211 $this->assertSame(200, $client->getResponse()->getStatusCode());
b3cc1a14
TC
212
213 $headers = $client->getResponse()->headers;
f808b016 214 $this->assertSame('application/json', $headers->get('content-type'));
dac93644 215 $this->assertSame('attachment; filename="' . $this->getSanitizedFilename($contentInDB->getTitle()) . '.json"', $headers->get('content-disposition'));
f808b016 216 $this->assertSame('UTF-8', $headers->get('content-transfer-encoding'));
cceca9ea
JB
217
218 $content = json_decode($client->getResponse()->getContent(), true);
cceca9ea
JB
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]);
cceca9ea 229 $this->assertArrayHasKey('tags', $content[0]);
9401696f
JB
230 $this->assertArrayHasKey('created_at', $content[0]);
231 $this->assertArrayHasKey('updated_at', $content[0]);
b0458874 232
38520658
JB
233 $this->assertSame((int) $contentInDB->isArchived(), $content[0]['is_archived']);
234 $this->assertSame((int) $contentInDB->isStarred(), $content[0]['is_starred']);
f808b016
JB
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']);
33264c2d
JB
242 $this->assertContains('baz', $content[0]['tags']);
243 $this->assertContains('foo', $content[0]['tags']);
b3cc1a14
TC
244 }
245
246 public function testXmlExport()
247 {
248 $this->logInAs('admin');
249 $client = $this->getClient();
250
cceca9ea
JB
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')
259 ->getQuery()
260 ->getArrayResult();
261
b3cc1a14
TC
262 ob_start();
263 $crawler = $client->request('GET', '/export/unread.xml');
264 ob_end_clean();
265
f808b016 266 $this->assertSame(200, $client->getResponse()->getStatusCode());
b3cc1a14
TC
267
268 $headers = $client->getResponse()->headers;
f808b016
JB
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'));
cceca9ea
JB
272
273 $content = new \SimpleXMLElement($client->getResponse()->getContent());
274 $this->assertGreaterThan(0, $content->count());
2a1ceb67 275 $this->assertSame(\count($contentInDB), $content->count());
cceca9ea
JB
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);
9401696f
JB
281 $this->assertNotEmpty('created_at', (string) $content->entry[0]->created_at);
282 $this->assertNotEmpty('updated_at', (string) $content->entry[0]->updated_at);
b3cc1a14 283 }
dac93644
KD
284
285 private function getSanitizedFilename($title)
286 {
287 return preg_replace('/[^A-Za-z0-9\- \']/', '', iconv('utf-8', 'us-ascii//TRANSLIT', $title));
288 }
add597ba 289}