]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/Controller/ExportControllerTest.php
9ecd8bc4e62ddd950aa831c0c5cf1ffaa70c3b16
[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
122 public function testTxtExport()
123 {
124 $this->logInAs('admin');
125 $client = $this->getClient();
126
127 ob_start();
128 $crawler = $client->request('GET', '/export/all.txt');
129 ob_end_clean();
130
131 $this->assertEquals(200, $client->getResponse()->getStatusCode());
132
133 $headers = $client->getResponse()->headers;
134 $this->assertEquals('text/plain; charset=UTF-8', $headers->get('content-type'));
135 $this->assertEquals('attachment; filename="All articles.txt"', $headers->get('content-disposition'));
136 $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding'));
137 }
138
139 public function testCsvExport()
140 {
141 $this->logInAs('admin');
142 $client = $this->getClient();
143
144 // to be sure results are the same
145 $contentInDB = $client->getContainer()
146 ->get('doctrine.orm.entity_manager')
147 ->getRepository('WallabagCoreBundle:Entry')
148 ->createQueryBuilder('e')
149 ->select('e, t')
150 ->leftJoin('e.user', 'u')
151 ->leftJoin('e.tags', 't')
152 ->where('u.username = :username')->setParameter('username', 'admin')
153 ->andWhere('e.isArchived = true')
154 ->getQuery()
155 ->getArrayResult();
156
157 ob_start();
158 $crawler = $client->request('GET', '/export/archive.csv');
159 ob_end_clean();
160
161 $this->assertEquals(200, $client->getResponse()->getStatusCode());
162
163 $headers = $client->getResponse()->headers;
164 $this->assertEquals('application/csv', $headers->get('content-type'));
165 $this->assertEquals('attachment; filename="Archive articles.csv"', $headers->get('content-disposition'));
166 $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding'));
167
168 $csv = str_getcsv($client->getResponse()->getContent(), "\n");
169
170 $this->assertGreaterThan(1, $csv);
171 // +1 for title line
172 $this->assertEquals(count($contentInDB) + 1, count($csv));
173 $this->assertEquals('Title;URL;Content;Tags;"MIME Type";Language;"Creation date"', $csv[0]);
174 $this->assertContains($contentInDB[0]['title'], $csv[1]);
175 $this->assertContains($contentInDB[0]['url'], $csv[1]);
176 $this->assertContains($contentInDB[0]['content'], $csv[1]);
177 $this->assertContains($contentInDB[0]['mimetype'], $csv[1]);
178 $this->assertContains($contentInDB[0]['language'], $csv[1]);
179 $this->assertContains($contentInDB[0]['createdAt']->format('d/m/Y h:i:s'), $csv[1]);
180
181 $expectedTag = [];
182 foreach ($contentInDB[0]['tags'] as $tag) {
183 $expectedTag[] = $tag['label'];
184 }
185 $this->assertContains(implode(', ', $expectedTag), $csv[1]);
186 }
187
188 public function testJsonExport()
189 {
190 $this->logInAs('admin');
191 $client = $this->getClient();
192
193 $contentInDB = $client->getContainer()
194 ->get('doctrine.orm.entity_manager')
195 ->getRepository('WallabagCoreBundle:Entry')
196 ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
197
198 ob_start();
199 $crawler = $client->request('GET', '/export/'.$contentInDB->getId().'.json');
200 ob_end_clean();
201
202 $this->assertEquals(200, $client->getResponse()->getStatusCode());
203
204 $headers = $client->getResponse()->headers;
205 $this->assertEquals('application/json', $headers->get('content-type'));
206 $this->assertEquals('attachment; filename="'.$contentInDB->getTitle().'.json"', $headers->get('content-disposition'));
207 $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding'));
208
209 $content = json_decode($client->getResponse()->getContent(), true);
210 $this->assertArrayHasKey('id', $content[0]);
211 $this->assertArrayHasKey('title', $content[0]);
212 $this->assertArrayHasKey('url', $content[0]);
213 $this->assertArrayHasKey('is_archived', $content[0]);
214 $this->assertArrayHasKey('is_starred', $content[0]);
215 $this->assertArrayHasKey('content', $content[0]);
216 $this->assertArrayHasKey('mimetype', $content[0]);
217 $this->assertArrayHasKey('language', $content[0]);
218 $this->assertArrayHasKey('reading_time', $content[0]);
219 $this->assertArrayHasKey('domain_name', $content[0]);
220 $this->assertArrayHasKey('tags', $content[0]);
221 $this->assertArrayHasKey('created_at', $content[0]);
222 $this->assertArrayHasKey('updated_at', $content[0]);
223
224 $this->assertEquals($contentInDB->isArchived(), $content[0]['is_archived']);
225 $this->assertEquals($contentInDB->isStarred(), $content[0]['is_starred']);
226 $this->assertEquals($contentInDB->getTitle(), $content[0]['title']);
227 $this->assertEquals($contentInDB->getUrl(), $content[0]['url']);
228 $this->assertEquals([['text' => 'This is my annotation /o/', 'quote' => 'content']], $content[0]['annotations']);
229 $this->assertEquals($contentInDB->getMimetype(), $content[0]['mimetype']);
230 $this->assertEquals($contentInDB->getLanguage(), $content[0]['language']);
231 $this->assertEquals($contentInDB->getReadingtime(), $content[0]['reading_time']);
232 $this->assertEquals($contentInDB->getDomainname(), $content[0]['domain_name']);
233 $this->assertEquals(['foo', 'baz'], $content[0]['tags']);
234 }
235
236 public function testXmlExport()
237 {
238 $this->logInAs('admin');
239 $client = $this->getClient();
240
241 // to be sure results are the same
242 $contentInDB = $client->getContainer()
243 ->get('doctrine.orm.entity_manager')
244 ->getRepository('WallabagCoreBundle:Entry')
245 ->createQueryBuilder('e')
246 ->leftJoin('e.user', 'u')
247 ->where('u.username = :username')->setParameter('username', 'admin')
248 ->andWhere('e.isArchived = false')
249 ->getQuery()
250 ->getArrayResult();
251
252 ob_start();
253 $crawler = $client->request('GET', '/export/unread.xml');
254 ob_end_clean();
255
256 $this->assertEquals(200, $client->getResponse()->getStatusCode());
257
258 $headers = $client->getResponse()->headers;
259 $this->assertEquals('application/xml', $headers->get('content-type'));
260 $this->assertEquals('attachment; filename="Unread articles.xml"', $headers->get('content-disposition'));
261 $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding'));
262
263 $content = new \SimpleXMLElement($client->getResponse()->getContent());
264 $this->assertGreaterThan(0, $content->count());
265 $this->assertEquals(count($contentInDB), $content->count());
266 $this->assertNotEmpty('id', (string) $content->entry[0]->id);
267 $this->assertNotEmpty('title', (string) $content->entry[0]->title);
268 $this->assertNotEmpty('url', (string) $content->entry[0]->url);
269 $this->assertNotEmpty('content', (string) $content->entry[0]->content);
270 $this->assertNotEmpty('domain_name', (string) $content->entry[0]->domain_name);
271 $this->assertNotEmpty('created_at', (string) $content->entry[0]->created_at);
272 $this->assertNotEmpty('updated_at', (string) $content->entry[0]->updated_at);
273 }
274 }