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 public function testTxtExport()
124 $this->logInAs('admin');
125 $client = $this->getClient();
128 $crawler = $client->request('GET', '/export/all.txt');
131 $this->assertEquals(200, $client->getResponse()->getStatusCode());
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'));
139 public function testCsvExport()
141 $this->logInAs('admin');
142 $client = $this->getClient();
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')
150 ->leftJoin('e.user', 'u')
151 ->leftJoin('e.tags', 't')
152 ->where('u.username = :username')->setParameter('username', 'admin')
153 ->andWhere('e.isArchived = true')
158 $crawler = $client->request('GET', '/export/archive.csv');
161 $this->assertEquals(200, $client->getResponse()->getStatusCode());
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'));
168 $csv = str_getcsv($client->getResponse()->getContent(), "\n");
170 $this->assertGreaterThan(1, $csv);
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]);
182 foreach ($contentInDB[0]['tags'] as $tag) {
183 $expectedTag[] = $tag['label'];
185 $this->assertContains(implode(', ', $expectedTag), $csv[1]);
188 public function testJsonExport()
190 $this->logInAs('admin');
191 $client = $this->getClient();
193 $contentInDB = $client->getContainer()
194 ->get('doctrine.orm.entity_manager')
195 ->getRepository('WallabagCoreBundle:Entry')
196 ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
199 $crawler = $client->request('GET', '/export/'.$contentInDB->getId().'.json');
202 $this->assertEquals(200, $client->getResponse()->getStatusCode());
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'));
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]);
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']);
236 public function testXmlExport()
238 $this->logInAs('admin');
239 $client = $this->getClient();
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')
253 $crawler = $client->request('GET', '/export/unread.xml');
256 $this->assertEquals(200, $client->getResponse()->getStatusCode());
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'));
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
);