]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/Controller/ExportControllerTest.php
Merge pull request #3076 from wallabag/hotfix/2979
[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
7 class ExportControllerTest extends WallabagCoreTestCase
8 {
9 public function testLogin()
10 {
11 $client = $this->getClient();
12
13 $client->request('GET', '/export/unread.csv');
14
15 $this->assertEquals(302, $client->getResponse()->getStatusCode());
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
24 $client->request('GET', '/export/awesomeness.epub');
25
26 $this->assertEquals(404, $client->getResponse()->getStatusCode());
27 }
28
29 public function testUnknownFormatExport()
30 {
31 $this->logInAs('admin');
32 $client = $this->getClient();
33
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.doc');
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().'.doc');
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');
62
63 $this->assertEquals(404, $client->getResponse()->getStatusCode());
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
75 $this->assertEquals(200, $client->getResponse()->getStatusCode());
76
77 $headers = $client->getResponse()->headers;
78 $this->assertEquals('application/epub+zip', $headers->get('content-type'));
79 $this->assertEquals('attachment; filename="Archive articles.epub"', $headers->get('content-disposition'));
80 $this->assertEquals('binary', $headers->get('content-transfer-encoding'));
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();
94 $crawler = $client->request('GET', '/export/'.$content->getId().'.mobi');
95 ob_end_clean();
96
97 $this->assertEquals(200, $client->getResponse()->getStatusCode());
98
99 $headers = $client->getResponse()->headers;
100 $this->assertEquals('application/x-mobipocket-ebook', $headers->get('content-type'));
101 $this->assertEquals('attachment; filename="'.preg_replace('/[^A-Za-z0-9\-]/', '', $content->getTitle()).'.mobi"', $headers->get('content-disposition'));
102 $this->assertEquals('binary', $headers->get('content-transfer-encoding'));
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
114 $this->assertEquals(200, $client->getResponse()->getStatusCode());
115
116 $headers = $client->getResponse()->headers;
117 $this->assertEquals('application/pdf', $headers->get('content-type'));
118 $this->assertEquals('attachment; filename="All articles.pdf"', $headers->get('content-disposition'));
119 $this->assertEquals('binary', $headers->get('content-transfer-encoding'));
120
121 ob_start();
122 $crawler = $client->request('GET', '/export/tag_entries.pdf?tag=foo-bar');
123 ob_end_clean();
124
125 $this->assertEquals(200, $client->getResponse()->getStatusCode());
126
127 $headers = $client->getResponse()->headers;
128 $this->assertEquals('application/pdf', $headers->get('content-type'));
129 $this->assertEquals('attachment; filename="Tag_entries articles.pdf"', $headers->get('content-disposition'));
130 $this->assertEquals('binary', $headers->get('content-transfer-encoding'));
131 }
132
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
142 $this->assertEquals(200, $client->getResponse()->getStatusCode());
143
144 $headers = $client->getResponse()->headers;
145 $this->assertEquals('text/plain; charset=UTF-8', $headers->get('content-type'));
146 $this->assertEquals('attachment; filename="All articles.txt"', $headers->get('content-disposition'));
147 $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding'));
148 }
149
150 public function testCsvExport()
151 {
152 $this->logInAs('admin');
153 $client = $this->getClient();
154
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')
160 ->select('e, t')
161 ->leftJoin('e.user', 'u')
162 ->leftJoin('e.tags', 't')
163 ->where('u.username = :username')->setParameter('username', 'admin')
164 ->andWhere('e.isArchived = true')
165 ->getQuery()
166 ->getArrayResult();
167
168 ob_start();
169 $crawler = $client->request('GET', '/export/archive.csv');
170 ob_end_clean();
171
172 $this->assertEquals(200, $client->getResponse()->getStatusCode());
173
174 $headers = $client->getResponse()->headers;
175 $this->assertEquals('application/csv', $headers->get('content-type'));
176 $this->assertEquals('attachment; filename="Archive articles.csv"', $headers->get('content-disposition'));
177 $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding'));
178
179 $csv = str_getcsv($client->getResponse()->getContent(), "\n");
180
181 $this->assertGreaterThan(1, $csv);
182 // +1 for title line
183 $this->assertEquals(count($contentInDB) + 1, count($csv));
184 $this->assertEquals('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]);
191
192 $expectedTag = [];
193 foreach ($contentInDB[0]['tags'] as $tag) {
194 $expectedTag[] = $tag['label'];
195 }
196 $this->assertContains(implode(', ', $expectedTag), $csv[1]);
197 }
198
199 public function testJsonExport()
200 {
201 $this->logInAs('admin');
202 $client = $this->getClient();
203
204 $contentInDB = $client->getContainer()
205 ->get('doctrine.orm.entity_manager')
206 ->getRepository('WallabagCoreBundle:Entry')
207 ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
208
209 ob_start();
210 $crawler = $client->request('GET', '/export/'.$contentInDB->getId().'.json');
211 ob_end_clean();
212
213 $this->assertEquals(200, $client->getResponse()->getStatusCode());
214
215 $headers = $client->getResponse()->headers;
216 $this->assertEquals('application/json', $headers->get('content-type'));
217 $this->assertEquals('attachment; filename="'.$contentInDB->getTitle().'.json"', $headers->get('content-disposition'));
218 $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding'));
219
220 $content = json_decode($client->getResponse()->getContent(), true);
221 $this->assertArrayHasKey('id', $content[0]);
222 $this->assertArrayHasKey('title', $content[0]);
223 $this->assertArrayHasKey('url', $content[0]);
224 $this->assertArrayHasKey('is_archived', $content[0]);
225 $this->assertArrayHasKey('is_starred', $content[0]);
226 $this->assertArrayHasKey('content', $content[0]);
227 $this->assertArrayHasKey('mimetype', $content[0]);
228 $this->assertArrayHasKey('language', $content[0]);
229 $this->assertArrayHasKey('reading_time', $content[0]);
230 $this->assertArrayHasKey('domain_name', $content[0]);
231 $this->assertArrayHasKey('tags', $content[0]);
232 $this->assertArrayHasKey('created_at', $content[0]);
233 $this->assertArrayHasKey('updated_at', $content[0]);
234
235 $this->assertEquals($contentInDB->isArchived(), $content[0]['is_archived']);
236 $this->assertEquals($contentInDB->isStarred(), $content[0]['is_starred']);
237 $this->assertEquals($contentInDB->getTitle(), $content[0]['title']);
238 $this->assertEquals($contentInDB->getUrl(), $content[0]['url']);
239 $this->assertEquals([['text' => 'This is my annotation /o/', 'quote' => 'content']], $content[0]['annotations']);
240 $this->assertEquals($contentInDB->getMimetype(), $content[0]['mimetype']);
241 $this->assertEquals($contentInDB->getLanguage(), $content[0]['language']);
242 $this->assertEquals($contentInDB->getReadingtime(), $content[0]['reading_time']);
243 $this->assertEquals($contentInDB->getDomainname(), $content[0]['domain_name']);
244 $this->assertEquals(['foo bar', 'baz'], $content[0]['tags']);
245 }
246
247 public function testXmlExport()
248 {
249 $this->logInAs('admin');
250 $client = $this->getClient();
251
252 // to be sure results are the same
253 $contentInDB = $client->getContainer()
254 ->get('doctrine.orm.entity_manager')
255 ->getRepository('WallabagCoreBundle:Entry')
256 ->createQueryBuilder('e')
257 ->leftJoin('e.user', 'u')
258 ->where('u.username = :username')->setParameter('username', 'admin')
259 ->andWhere('e.isArchived = false')
260 ->getQuery()
261 ->getArrayResult();
262
263 ob_start();
264 $crawler = $client->request('GET', '/export/unread.xml');
265 ob_end_clean();
266
267 $this->assertEquals(200, $client->getResponse()->getStatusCode());
268
269 $headers = $client->getResponse()->headers;
270 $this->assertEquals('application/xml', $headers->get('content-type'));
271 $this->assertEquals('attachment; filename="Unread articles.xml"', $headers->get('content-disposition'));
272 $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding'));
273
274 $content = new \SimpleXMLElement($client->getResponse()->getContent());
275 $this->assertGreaterThan(0, $content->count());
276 $this->assertEquals(count($contentInDB), $content->count());
277 $this->assertNotEmpty('id', (string) $content->entry[0]->id);
278 $this->assertNotEmpty('title', (string) $content->entry[0]->title);
279 $this->assertNotEmpty('url', (string) $content->entry[0]->url);
280 $this->assertNotEmpty('content', (string) $content->entry[0]->content);
281 $this->assertNotEmpty('domain_name', (string) $content->entry[0]->domain_name);
282 $this->assertNotEmpty('created_at', (string) $content->entry[0]->created_at);
283 $this->assertNotEmpty('updated_at', (string) $content->entry[0]->updated_at);
284 }
285 }