3 namespace Tests\Wallabag\CoreBundle\Controller
;
5 use Tests\Wallabag\CoreBundle\WallabagCoreTestCase
;
7 class ExportControllerTest
extends WallabagCoreTestCase
9 public function testLogin()
11 $client = $this->getClient();
13 $client->request('GET', '/export/unread.csv');
15 $this->assertEquals(302, $client->getResponse()->getStatusCode());
16 $this->assertContains('login', $client->getResponse()->headers
->get('location'));
19 public function testUnknownCategoryExport()
21 $this->logInAs('admin');
22 $client = $this->getClient();
24 $client->request('GET', '/export/awesomeness.epub');
26 $this->assertEquals(404, $client->getResponse()->getStatusCode());
29 public function testUnknownFormatExport()
31 $this->logInAs('admin');
32 $client = $this->getClient();
34 $client->request('GET', '/export/unread.xslx');
36 $this->assertEquals(404, $client->getResponse()->getStatusCode());
39 public function testUnsupportedFormatExport()
41 $this->logInAs('admin');
42 $client = $this->getClient();
44 $client->request('GET', '/export/unread.doc');
45 $this->assertEquals(404, $client->getResponse()->getStatusCode());
47 $content = $client->getContainer()
48 ->get('doctrine.orm.entity_manager')
49 ->getRepository('WallabagCoreBundle:Entry')
50 ->findOneByUsernameAndNotArchived('admin');
52 $client->request('GET', '/export/'.$content->getId().'.doc');
53 $this->assertEquals(404, $client->getResponse()->getStatusCode());
56 public function testBadEntryId()
58 $this->logInAs('admin');
59 $client = $this->getClient();
61 $client->request('GET', '/export/0.mobi');
63 $this->assertEquals(404, $client->getResponse()->getStatusCode());
66 public function testEpubExport()
68 $this->logInAs('admin');
69 $client = $this->getClient();
72 $crawler = $client->request('GET', '/export/archive.epub');
75 $this->assertEquals(200, $client->getResponse()->getStatusCode());
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'));
83 public function testMobiExport()
85 $this->logInAs('admin');
86 $client = $this->getClient();
88 $content = $client->getContainer()
89 ->get('doctrine.orm.entity_manager')
90 ->getRepository('WallabagCoreBundle:Entry')
91 ->findOneByUsernameAndNotArchived('admin');
94 $crawler = $client->request('GET', '/export/'.$content->getId().'.mobi');
97 $this->assertEquals(200, $client->getResponse()->getStatusCode());
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'));
105 public function testPdfExport()
107 $this->logInAs('admin');
108 $client = $this->getClient();
111 $crawler = $client->request('GET', '/export/all.pdf');
114 $this->assertEquals(200, $client->getResponse()->getStatusCode());
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'));
122 $crawler = $client->request('GET', '/export/tag_entries.pdf?tag=foo-bar');
125 $this->assertEquals(200, $client->getResponse()->getStatusCode());
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'));
133 public function testTxtExport()
135 $this->logInAs('admin');
136 $client = $this->getClient();
139 $crawler = $client->request('GET', '/export/all.txt');
142 $this->assertEquals(200, $client->getResponse()->getStatusCode());
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'));
150 public function testCsvExport()
152 $this->logInAs('admin');
153 $client = $this->getClient();
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')
161 ->leftJoin('e.user', 'u')
162 ->leftJoin('e.tags', 't')
163 ->where('u.username = :username')->setParameter('username', 'admin')
164 ->andWhere('e.isArchived = true')
169 $crawler = $client->request('GET', '/export/archive.csv');
172 $this->assertEquals(200, $client->getResponse()->getStatusCode());
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'));
179 $csv = str_getcsv($client->getResponse()->getContent(), "\n");
181 $this->assertGreaterThan(1, $csv);
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]);
193 foreach ($contentInDB[0]['tags'] as $tag) {
194 $expectedTag[] = $tag['label'];
196 $this->assertContains(implode(', ', $expectedTag), $csv[1]);
199 public function testJsonExport()
201 $this->logInAs('admin');
202 $client = $this->getClient();
204 $contentInDB = $client->getContainer()
205 ->get('doctrine.orm.entity_manager')
206 ->getRepository('WallabagCoreBundle:Entry')
207 ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
210 $crawler = $client->request('GET', '/export/'.$contentInDB->getId().'.json');
213 $this->assertEquals(200, $client->getResponse()->getStatusCode());
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'));
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]);
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']);
247 public function testXmlExport()
249 $this->logInAs('admin');
250 $client = $this->getClient();
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')
264 $crawler = $client->request('GET', '/export/unread.xml');
267 $this->assertEquals(200, $client->getResponse()->getStatusCode());
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'));
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
);