diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Tests/Controller')
-rw-r--r-- | src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php | 234 |
1 files changed, 234 insertions, 0 deletions
diff --git a/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php new file mode 100644 index 00000000..739b2dec --- /dev/null +++ b/src/Wallabag/CoreBundle/Tests/Controller/ExportControllerTest.php | |||
@@ -0,0 +1,234 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Tests\Controller; | ||
4 | |||
5 | use Wallabag\CoreBundle\Tests\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.txt'); | ||
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().'.txt'); | ||
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 testCsvExport() | ||
123 | { | ||
124 | $this->logInAs('admin'); | ||
125 | $client = $this->getClient(); | ||
126 | |||
127 | // to be sure results are the same | ||
128 | $contentInDB = $client->getContainer() | ||
129 | ->get('doctrine.orm.entity_manager') | ||
130 | ->getRepository('WallabagCoreBundle:Entry') | ||
131 | ->createQueryBuilder('e') | ||
132 | ->leftJoin('e.user', 'u') | ||
133 | ->where('u.username = :username')->setParameter('username', 'admin') | ||
134 | ->andWhere('e.isArchived = true') | ||
135 | ->getQuery() | ||
136 | ->getArrayResult(); | ||
137 | |||
138 | ob_start(); | ||
139 | $crawler = $client->request('GET', '/export/archive.csv'); | ||
140 | ob_end_clean(); | ||
141 | |||
142 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | ||
143 | |||
144 | $headers = $client->getResponse()->headers; | ||
145 | $this->assertEquals('application/csv', $headers->get('content-type')); | ||
146 | $this->assertEquals('attachment; filename="Archive articles.csv"', $headers->get('content-disposition')); | ||
147 | $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding')); | ||
148 | |||
149 | $csv = str_getcsv($client->getResponse()->getContent(), "\n"); | ||
150 | |||
151 | $this->assertGreaterThan(1, $csv); | ||
152 | // +1 for title line | ||
153 | $this->assertEquals(count($contentInDB)+1, count($csv)); | ||
154 | $this->assertEquals('Title;URL;Content;Tags;"MIME Type";Language', $csv[0]); | ||
155 | } | ||
156 | |||
157 | public function testJsonExport() | ||
158 | { | ||
159 | $this->logInAs('admin'); | ||
160 | $client = $this->getClient(); | ||
161 | |||
162 | // to be sure results are the same | ||
163 | $contentInDB = $client->getContainer() | ||
164 | ->get('doctrine.orm.entity_manager') | ||
165 | ->getRepository('WallabagCoreBundle:Entry') | ||
166 | ->createQueryBuilder('e') | ||
167 | ->leftJoin('e.user', 'u') | ||
168 | ->where('u.username = :username')->setParameter('username', 'admin') | ||
169 | ->getQuery() | ||
170 | ->getArrayResult(); | ||
171 | |||
172 | ob_start(); | ||
173 | $crawler = $client->request('GET', '/export/all.json'); | ||
174 | ob_end_clean(); | ||
175 | |||
176 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | ||
177 | |||
178 | $headers = $client->getResponse()->headers; | ||
179 | $this->assertEquals('application/json', $headers->get('content-type')); | ||
180 | $this->assertEquals('attachment; filename="All articles.json"', $headers->get('content-disposition')); | ||
181 | $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding')); | ||
182 | |||
183 | $content = json_decode($client->getResponse()->getContent(), true); | ||
184 | $this->assertEquals(count($contentInDB), count($content)); | ||
185 | $this->assertArrayHasKey('id', $content[0]); | ||
186 | $this->assertArrayHasKey('title', $content[0]); | ||
187 | $this->assertArrayHasKey('url', $content[0]); | ||
188 | $this->assertArrayHasKey('is_archived', $content[0]); | ||
189 | $this->assertArrayHasKey('is_starred', $content[0]); | ||
190 | $this->assertArrayHasKey('content', $content[0]); | ||
191 | $this->assertArrayHasKey('mimetype', $content[0]); | ||
192 | $this->assertArrayHasKey('language', $content[0]); | ||
193 | $this->assertArrayHasKey('reading_time', $content[0]); | ||
194 | $this->assertArrayHasKey('domain_name', $content[0]); | ||
195 | $this->assertArrayHasKey('tags', $content[0]); | ||
196 | } | ||
197 | |||
198 | public function testXmlExport() | ||
199 | { | ||
200 | $this->logInAs('admin'); | ||
201 | $client = $this->getClient(); | ||
202 | |||
203 | // to be sure results are the same | ||
204 | $contentInDB = $client->getContainer() | ||
205 | ->get('doctrine.orm.entity_manager') | ||
206 | ->getRepository('WallabagCoreBundle:Entry') | ||
207 | ->createQueryBuilder('e') | ||
208 | ->leftJoin('e.user', 'u') | ||
209 | ->where('u.username = :username')->setParameter('username', 'admin') | ||
210 | ->andWhere('e.isArchived = false') | ||
211 | ->getQuery() | ||
212 | ->getArrayResult(); | ||
213 | |||
214 | ob_start(); | ||
215 | $crawler = $client->request('GET', '/export/unread.xml'); | ||
216 | ob_end_clean(); | ||
217 | |||
218 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | ||
219 | |||
220 | $headers = $client->getResponse()->headers; | ||
221 | $this->assertEquals('application/xml', $headers->get('content-type')); | ||
222 | $this->assertEquals('attachment; filename="Unread articles.xml"', $headers->get('content-disposition')); | ||
223 | $this->assertEquals('UTF-8', $headers->get('content-transfer-encoding')); | ||
224 | |||
225 | $content = new \SimpleXMLElement($client->getResponse()->getContent()); | ||
226 | $this->assertGreaterThan(0, $content->count()); | ||
227 | $this->assertEquals(count($contentInDB), $content->count()); | ||
228 | $this->assertNotEmpty('id', (string) $content->entry[0]->id); | ||
229 | $this->assertNotEmpty('title', (string) $content->entry[0]->title); | ||
230 | $this->assertNotEmpty('url', (string) $content->entry[0]->url); | ||
231 | $this->assertNotEmpty('content', (string) $content->entry[0]->content); | ||
232 | $this->assertNotEmpty('domain_name', (string) $content->entry[0]->domain_name); | ||
233 | } | ||
234 | } | ||