]>
Commit | Line | Data |
---|---|---|
769e19dc J |
1 | <?php |
2 | ||
23634d5d | 3 | namespace Tests\Wallabag\ApiBundle\Controller; |
769e19dc | 4 | |
23634d5d | 5 | use Tests\Wallabag\ApiBundle\WallabagApiTestCase; |
a0e1eafc | 6 | use Wallabag\CoreBundle\Entity\Tag; |
769e19dc | 7 | |
8a493541 | 8 | class WallabagRestControllerTest extends WallabagApiTestCase |
769e19dc J |
9 | { |
10 | protected static $salt; | |
11 | ||
769e19dc J |
12 | public function testGetOneEntry() |
13 | { | |
fcb1fba5 | 14 | $entry = $this->client->getContainer() |
769e19dc J |
15 | ->get('doctrine.orm.entity_manager') |
16 | ->getRepository('WallabagCoreBundle:Entry') | |
4094ea47 | 17 | ->findOneBy(['user' => 1, 'isArchived' => false]); |
769e19dc J |
18 | |
19 | if (!$entry) { | |
20 | $this->markTestSkipped('No content found in db.'); | |
21 | } | |
22 | ||
fcb1fba5 NL |
23 | $this->client->request('GET', '/api/entries/'.$entry->getId().'.json'); |
24 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
769e19dc | 25 | |
fcb1fba5 | 26 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
27 | |
28 | $this->assertEquals($entry->getTitle(), $content['title']); | |
29 | $this->assertEquals($entry->getUrl(), $content['url']); | |
30 | $this->assertCount(count($entry->getTags()), $content['tags']); | |
bc44aa57 TC |
31 | $this->assertEquals($entry->getUserName(), $content['user_name']); |
32 | $this->assertEquals($entry->getUserEmail(), $content['user_email']); | |
33 | $this->assertEquals($entry->getUserId(), $content['user_id']); | |
769e19dc | 34 | |
3f3a6087 JB |
35 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
36 | } | |
37 | ||
38 | public function testExportEntry() | |
39 | { | |
40 | $entry = $this->client->getContainer() | |
41 | ->get('doctrine.orm.entity_manager') | |
42 | ->getRepository('WallabagCoreBundle:Entry') | |
43 | ->findOneBy(['user' => 1, 'isArchived' => false]); | |
44 | ||
45 | if (!$entry) { | |
46 | $this->markTestSkipped('No content found in db.'); | |
47 | } | |
48 | ||
49 | $this->client->request('GET', '/api/entries/'.$entry->getId().'/export.epub'); | |
50 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
51 | ||
52 | // epub format got the content type in the content | |
53 | $this->assertContains('application/epub', $this->client->getResponse()->getContent()); | |
54 | $this->assertEquals('application/epub+zip', $this->client->getResponse()->headers->get('Content-Type')); | |
55 | ||
56 | // re-auth client for mobi | |
57 | $client = $this->createAuthorizedClient(); | |
58 | $client->request('GET', '/api/entries/'.$entry->getId().'/export.mobi'); | |
59 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
60 | ||
61 | $this->assertEquals('application/x-mobipocket-ebook', $client->getResponse()->headers->get('Content-Type')); | |
62 | ||
63 | // re-auth client for pdf | |
64 | $client = $this->createAuthorizedClient(); | |
65 | $client->request('GET', '/api/entries/'.$entry->getId().'/export.pdf'); | |
66 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
67 | ||
68 | $this->assertContains('PDF-', $client->getResponse()->getContent()); | |
69 | $this->assertEquals('application/pdf', $client->getResponse()->headers->get('Content-Type')); | |
70 | ||
71 | // re-auth client for pdf | |
72 | $client = $this->createAuthorizedClient(); | |
73 | $client->request('GET', '/api/entries/'.$entry->getId().'/export.txt'); | |
74 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
75 | ||
76 | $this->assertContains('text/plain', $client->getResponse()->headers->get('Content-Type')); | |
77 | ||
78 | // re-auth client for pdf | |
79 | $client = $this->createAuthorizedClient(); | |
80 | $client->request('GET', '/api/entries/'.$entry->getId().'/export.csv'); | |
81 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
82 | ||
83 | $this->assertContains('application/csv', $client->getResponse()->headers->get('Content-Type')); | |
769e19dc J |
84 | } |
85 | ||
86 | public function testGetOneEntryWrongUser() | |
87 | { | |
fcb1fba5 | 88 | $entry = $this->client->getContainer() |
769e19dc J |
89 | ->get('doctrine.orm.entity_manager') |
90 | ->getRepository('WallabagCoreBundle:Entry') | |
4094ea47 | 91 | ->findOneBy(['user' => 2, 'isArchived' => false]); |
769e19dc J |
92 | |
93 | if (!$entry) { | |
94 | $this->markTestSkipped('No content found in db.'); | |
95 | } | |
96 | ||
fcb1fba5 | 97 | $this->client->request('GET', '/api/entries/'.$entry->getId().'.json'); |
769e19dc | 98 | |
fcb1fba5 | 99 | $this->assertEquals(403, $this->client->getResponse()->getStatusCode()); |
769e19dc J |
100 | } |
101 | ||
102 | public function testGetEntries() | |
103 | { | |
fcb1fba5 | 104 | $this->client->request('GET', '/api/entries'); |
769e19dc | 105 | |
fcb1fba5 | 106 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 107 | |
fcb1fba5 | 108 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
109 | |
110 | $this->assertGreaterThanOrEqual(1, count($content)); | |
111 | $this->assertNotEmpty($content['_embedded']['items']); | |
112 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
113 | $this->assertEquals(1, $content['page']); | |
114 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
115 | ||
3f3a6087 | 116 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
769e19dc J |
117 | } |
118 | ||
c3f8b428 JB |
119 | public function testGetEntriesWithFullOptions() |
120 | { | |
121 | $this->client->request('GET', '/api/entries', [ | |
122 | 'archive' => 1, | |
123 | 'starred' => 1, | |
124 | 'sort' => 'updated', | |
125 | 'order' => 'asc', | |
126 | 'page' => 1, | |
127 | 'perPage' => 2, | |
128 | 'tags' => 'foo', | |
129 | 'since' => 1443274283, | |
130 | ]); | |
131 | ||
132 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
133 | ||
134 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
135 | ||
136 | $this->assertGreaterThanOrEqual(1, count($content)); | |
137 | $this->assertArrayHasKey('items', $content['_embedded']); | |
138 | $this->assertGreaterThanOrEqual(0, $content['total']); | |
139 | $this->assertEquals(1, $content['page']); | |
140 | $this->assertEquals(2, $content['limit']); | |
141 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
142 | ||
143 | $this->assertArrayHasKey('_links', $content); | |
144 | $this->assertArrayHasKey('self', $content['_links']); | |
145 | $this->assertArrayHasKey('first', $content['_links']); | |
146 | $this->assertArrayHasKey('last', $content['_links']); | |
147 | ||
148 | foreach (['self', 'first', 'last'] as $link) { | |
149 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
150 | $this->assertContains('archive=1', $content['_links'][$link]['href']); | |
151 | $this->assertContains('starred=1', $content['_links'][$link]['href']); | |
152 | $this->assertContains('sort=updated', $content['_links'][$link]['href']); | |
153 | $this->assertContains('order=asc', $content['_links'][$link]['href']); | |
154 | $this->assertContains('tags=foo', $content['_links'][$link]['href']); | |
155 | $this->assertContains('since=1443274283', $content['_links'][$link]['href']); | |
156 | } | |
157 | ||
3f3a6087 | 158 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
c3f8b428 JB |
159 | } |
160 | ||
769e19dc J |
161 | public function testGetStarredEntries() |
162 | { | |
f0fd82d0 | 163 | $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']); |
769e19dc | 164 | |
fcb1fba5 | 165 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
e6f55346 | 166 | |
fcb1fba5 | 167 | $content = json_decode($this->client->getResponse()->getContent(), true); |
e6f55346 JB |
168 | |
169 | $this->assertGreaterThanOrEqual(1, count($content)); | |
170 | $this->assertNotEmpty($content['_embedded']['items']); | |
171 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
172 | $this->assertEquals(1, $content['page']); | |
173 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
174 | ||
c3f8b428 JB |
175 | $this->assertArrayHasKey('_links', $content); |
176 | $this->assertArrayHasKey('self', $content['_links']); | |
177 | $this->assertArrayHasKey('first', $content['_links']); | |
178 | $this->assertArrayHasKey('last', $content['_links']); | |
179 | ||
180 | foreach (['self', 'first', 'last'] as $link) { | |
181 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
182 | $this->assertContains('starred=1', $content['_links'][$link]['href']); | |
183 | $this->assertContains('sort=updated', $content['_links'][$link]['href']); | |
184 | } | |
185 | ||
3f3a6087 | 186 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
e6f55346 JB |
187 | } |
188 | ||
189 | public function testGetArchiveEntries() | |
190 | { | |
4094ea47 | 191 | $this->client->request('GET', '/api/entries', ['archive' => 1]); |
769e19dc | 192 | |
fcb1fba5 | 193 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 194 | |
fcb1fba5 | 195 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
196 | |
197 | $this->assertGreaterThanOrEqual(1, count($content)); | |
9744e971 | 198 | $this->assertNotEmpty($content['_embedded']['items']); |
28803f10 TC |
199 | $this->assertGreaterThanOrEqual(1, $content['total']); |
200 | $this->assertEquals(1, $content['page']); | |
201 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
202 | ||
c3f8b428 JB |
203 | $this->assertArrayHasKey('_links', $content); |
204 | $this->assertArrayHasKey('self', $content['_links']); | |
205 | $this->assertArrayHasKey('first', $content['_links']); | |
206 | $this->assertArrayHasKey('last', $content['_links']); | |
207 | ||
208 | foreach (['self', 'first', 'last'] as $link) { | |
209 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
210 | $this->assertContains('archive=1', $content['_links'][$link]['href']); | |
211 | } | |
212 | ||
3f3a6087 | 213 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
28803f10 TC |
214 | } |
215 | ||
216 | public function testGetTaggedEntries() | |
217 | { | |
218 | $this->client->request('GET', '/api/entries', ['tags' => 'foo,bar']); | |
219 | ||
220 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
221 | ||
222 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
223 | ||
224 | $this->assertGreaterThanOrEqual(1, count($content)); | |
225 | $this->assertNotEmpty($content['_embedded']['items']); | |
9744e971 | 226 | $this->assertGreaterThanOrEqual(1, $content['total']); |
769e19dc | 227 | $this->assertEquals(1, $content['page']); |
9744e971 | 228 | $this->assertGreaterThanOrEqual(1, $content['pages']); |
769e19dc | 229 | |
c3f8b428 JB |
230 | $this->assertArrayHasKey('_links', $content); |
231 | $this->assertArrayHasKey('self', $content['_links']); | |
232 | $this->assertArrayHasKey('first', $content['_links']); | |
233 | $this->assertArrayHasKey('last', $content['_links']); | |
234 | ||
235 | foreach (['self', 'first', 'last'] as $link) { | |
236 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
237 | $this->assertContains('tags='.urlencode('foo,bar'), $content['_links'][$link]['href']); | |
238 | } | |
239 | ||
3f3a6087 | 240 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
769e19dc J |
241 | } |
242 | ||
e5fb89e5 TC |
243 | public function testGetDatedEntries() |
244 | { | |
c3f8b428 | 245 | $this->client->request('GET', '/api/entries', ['since' => 1443274283]); |
e5fb89e5 TC |
246 | |
247 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
248 | ||
249 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
250 | ||
251 | $this->assertGreaterThanOrEqual(1, count($content)); | |
252 | $this->assertNotEmpty($content['_embedded']['items']); | |
253 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
254 | $this->assertEquals(1, $content['page']); | |
255 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
256 | ||
c3f8b428 JB |
257 | $this->assertArrayHasKey('_links', $content); |
258 | $this->assertArrayHasKey('self', $content['_links']); | |
259 | $this->assertArrayHasKey('first', $content['_links']); | |
260 | $this->assertArrayHasKey('last', $content['_links']); | |
261 | ||
262 | foreach (['self', 'first', 'last'] as $link) { | |
263 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
264 | $this->assertContains('since=1443274283', $content['_links'][$link]['href']); | |
265 | } | |
266 | ||
3f3a6087 | 267 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
e5fb89e5 TC |
268 | } |
269 | ||
270 | public function testGetDatedSupEntries() | |
271 | { | |
272 | $future = new \DateTime(date('Y-m-d H:i:s')); | |
273 | $this->client->request('GET', '/api/entries', ['since' => $future->getTimestamp() + 1000]); | |
274 | ||
275 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
276 | ||
277 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
278 | ||
279 | $this->assertGreaterThanOrEqual(1, count($content)); | |
280 | $this->assertEmpty($content['_embedded']['items']); | |
281 | $this->assertEquals(0, $content['total']); | |
282 | $this->assertEquals(1, $content['page']); | |
283 | $this->assertEquals(1, $content['pages']); | |
284 | ||
c3f8b428 JB |
285 | $this->assertArrayHasKey('_links', $content); |
286 | $this->assertArrayHasKey('self', $content['_links']); | |
287 | $this->assertArrayHasKey('first', $content['_links']); | |
288 | $this->assertArrayHasKey('last', $content['_links']); | |
289 | ||
290 | foreach (['self', 'first', 'last'] as $link) { | |
291 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
292 | $this->assertContains('since='.($future->getTimestamp() + 1000), $content['_links'][$link]['href']); | |
293 | } | |
294 | ||
3f3a6087 | 295 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
e5fb89e5 TC |
296 | } |
297 | ||
769e19dc J |
298 | public function testDeleteEntry() |
299 | { | |
fcb1fba5 | 300 | $entry = $this->client->getContainer() |
769e19dc J |
301 | ->get('doctrine.orm.entity_manager') |
302 | ->getRepository('WallabagCoreBundle:Entry') | |
303 | ->findOneByUser(1); | |
304 | ||
305 | if (!$entry) { | |
306 | $this->markTestSkipped('No content found in db.'); | |
307 | } | |
308 | ||
fcb1fba5 | 309 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json'); |
769e19dc | 310 | |
fcb1fba5 | 311 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 312 | |
fcb1fba5 | 313 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
314 | |
315 | $this->assertEquals($entry->getTitle(), $content['title']); | |
316 | $this->assertEquals($entry->getUrl(), $content['url']); | |
317 | ||
318 | // We'll try to delete this entry again | |
fcb1fba5 | 319 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json'); |
769e19dc | 320 | |
fcb1fba5 | 321 | $this->assertEquals(404, $this->client->getResponse()->getStatusCode()); |
769e19dc J |
322 | } |
323 | ||
324 | public function testPostEntry() | |
325 | { | |
4094ea47 | 326 | $this->client->request('POST', '/api/entries.json', [ |
769e19dc J |
327 | 'url' => 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', |
328 | 'tags' => 'google', | |
51a15609 | 329 | 'title' => 'New title for my article', |
4094ea47 | 330 | ]); |
769e19dc | 331 | |
fcb1fba5 | 332 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 333 | |
fcb1fba5 | 334 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
335 | |
336 | $this->assertGreaterThan(0, $content['id']); | |
337 | $this->assertEquals('http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', $content['url']); | |
338 | $this->assertEquals(false, $content['is_archived']); | |
339 | $this->assertEquals(false, $content['is_starred']); | |
51a15609 | 340 | $this->assertEquals('New title for my article', $content['title']); |
bc44aa57 | 341 | $this->assertEquals(1, $content['user_id']); |
769e19dc J |
342 | $this->assertCount(1, $content['tags']); |
343 | } | |
344 | ||
3107f92a TC |
345 | public function testPostSameEntry() |
346 | { | |
4094ea47 | 347 | $this->client->request('POST', '/api/entries.json', [ |
3107f92a TC |
348 | 'url' => 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', |
349 | 'archive' => '1', | |
2baca964 | 350 | 'tags' => 'google, apple', |
4094ea47 | 351 | ]); |
3107f92a TC |
352 | |
353 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
354 | ||
355 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
356 | ||
357 | $this->assertGreaterThan(0, $content['id']); | |
358 | $this->assertEquals('http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', $content['url']); | |
359 | $this->assertEquals(true, $content['is_archived']); | |
360 | $this->assertEquals(false, $content['is_starred']); | |
2baca964 | 361 | $this->assertCount(2, $content['tags']); |
3107f92a TC |
362 | } |
363 | ||
09d8bb6f | 364 | public function testPostArchivedAndStarredEntry() |
816ad405 | 365 | { |
4094ea47 | 366 | $this->client->request('POST', '/api/entries.json', [ |
816ad405 | 367 | 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', |
189ef634 TC |
368 | 'archive' => '1', |
369 | 'starred' => '1', | |
4094ea47 | 370 | ]); |
816ad405 TC |
371 | |
372 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
373 | ||
374 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
375 | ||
376 | $this->assertGreaterThan(0, $content['id']); | |
377 | $this->assertEquals('http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']); | |
378 | $this->assertEquals(true, $content['is_archived']); | |
09d8bb6f | 379 | $this->assertEquals(true, $content['is_starred']); |
bc44aa57 | 380 | $this->assertEquals(1, $content['user_id']); |
816ad405 TC |
381 | } |
382 | ||
2f60e5ea TC |
383 | public function testPostArchivedAndStarredEntryWithoutQuotes() |
384 | { | |
4094ea47 | 385 | $this->client->request('POST', '/api/entries.json', [ |
2f60e5ea TC |
386 | 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', |
387 | 'archive' => 0, | |
388 | 'starred' => 1, | |
4094ea47 | 389 | ]); |
2f60e5ea TC |
390 | |
391 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
392 | ||
393 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
394 | ||
395 | $this->assertGreaterThan(0, $content['id']); | |
396 | $this->assertEquals('http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']); | |
397 | $this->assertEquals(false, $content['is_archived']); | |
398 | $this->assertEquals(true, $content['is_starred']); | |
399 | } | |
400 | ||
769e19dc J |
401 | public function testPatchEntry() |
402 | { | |
fcb1fba5 | 403 | $entry = $this->client->getContainer() |
769e19dc J |
404 | ->get('doctrine.orm.entity_manager') |
405 | ->getRepository('WallabagCoreBundle:Entry') | |
406 | ->findOneByUser(1); | |
407 | ||
408 | if (!$entry) { | |
409 | $this->markTestSkipped('No content found in db.'); | |
410 | } | |
411 | ||
412 | // hydrate the tags relations | |
413 | $nbTags = count($entry->getTags()); | |
414 | ||
4094ea47 | 415 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ |
769e19dc J |
416 | 'title' => 'New awesome title', |
417 | 'tags' => 'new tag '.uniqid(), | |
189ef634 TC |
418 | 'starred' => '1', |
419 | 'archive' => '0', | |
4094ea47 | 420 | ]); |
769e19dc | 421 | |
fcb1fba5 | 422 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 423 | |
fcb1fba5 | 424 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
425 | |
426 | $this->assertEquals($entry->getId(), $content['id']); | |
427 | $this->assertEquals($entry->getUrl(), $content['url']); | |
428 | $this->assertEquals('New awesome title', $content['title']); | |
429 | $this->assertGreaterThan($nbTags, count($content['tags'])); | |
bc44aa57 | 430 | $this->assertEquals(1, $content['user_id']); |
769e19dc J |
431 | } |
432 | ||
2f60e5ea TC |
433 | public function testPatchEntryWithoutQuotes() |
434 | { | |
435 | $entry = $this->client->getContainer() | |
436 | ->get('doctrine.orm.entity_manager') | |
437 | ->getRepository('WallabagCoreBundle:Entry') | |
438 | ->findOneByUser(1); | |
439 | ||
440 | if (!$entry) { | |
441 | $this->markTestSkipped('No content found in db.'); | |
442 | } | |
443 | ||
444 | // hydrate the tags relations | |
445 | $nbTags = count($entry->getTags()); | |
446 | ||
4094ea47 | 447 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ |
2f60e5ea TC |
448 | 'title' => 'New awesome title', |
449 | 'tags' => 'new tag '.uniqid(), | |
450 | 'starred' => 1, | |
451 | 'archive' => 0, | |
4094ea47 | 452 | ]); |
2f60e5ea TC |
453 | |
454 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
455 | ||
456 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
457 | ||
458 | $this->assertEquals($entry->getId(), $content['id']); | |
459 | $this->assertEquals($entry->getUrl(), $content['url']); | |
460 | $this->assertEquals('New awesome title', $content['title']); | |
461 | $this->assertGreaterThan($nbTags, count($content['tags'])); | |
462 | } | |
463 | ||
769e19dc J |
464 | public function testGetTagsEntry() |
465 | { | |
fcb1fba5 | 466 | $entry = $this->client->getContainer() |
769e19dc J |
467 | ->get('doctrine.orm.entity_manager') |
468 | ->getRepository('WallabagCoreBundle:Entry') | |
a0e1eafc | 469 | ->findOneWithTags($this->user->getId()); |
769e19dc J |
470 | |
471 | $entry = $entry[0]; | |
472 | ||
473 | if (!$entry) { | |
474 | $this->markTestSkipped('No content found in db.'); | |
475 | } | |
476 | ||
4094ea47 | 477 | $tags = []; |
769e19dc | 478 | foreach ($entry->getTags() as $tag) { |
4094ea47 | 479 | $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()]; |
769e19dc J |
480 | } |
481 | ||
fcb1fba5 | 482 | $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags'); |
769e19dc | 483 | |
fcb1fba5 | 484 | $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent()); |
769e19dc J |
485 | } |
486 | ||
487 | public function testPostTagsOnEntry() | |
488 | { | |
fcb1fba5 | 489 | $entry = $this->client->getContainer() |
769e19dc J |
490 | ->get('doctrine.orm.entity_manager') |
491 | ->getRepository('WallabagCoreBundle:Entry') | |
492 | ->findOneByUser(1); | |
493 | ||
494 | if (!$entry) { | |
495 | $this->markTestSkipped('No content found in db.'); | |
496 | } | |
497 | ||
498 | $nbTags = count($entry->getTags()); | |
499 | ||
500 | $newTags = 'tag1,tag2,tag3'; | |
501 | ||
4094ea47 | 502 | $this->client->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]); |
769e19dc | 503 | |
fcb1fba5 | 504 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 505 | |
fcb1fba5 | 506 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
507 | |
508 | $this->assertArrayHasKey('tags', $content); | |
4346a860 | 509 | $this->assertEquals($nbTags + 3, count($content['tags'])); |
769e19dc | 510 | |
fcb1fba5 | 511 | $entryDB = $this->client->getContainer() |
769e19dc J |
512 | ->get('doctrine.orm.entity_manager') |
513 | ->getRepository('WallabagCoreBundle:Entry') | |
514 | ->find($entry->getId()); | |
515 | ||
4094ea47 | 516 | $tagsInDB = []; |
769e19dc J |
517 | foreach ($entryDB->getTags()->toArray() as $tag) { |
518 | $tagsInDB[$tag->getId()] = $tag->getLabel(); | |
519 | } | |
520 | ||
521 | foreach (explode(',', $newTags) as $tag) { | |
522 | $this->assertContains($tag, $tagsInDB); | |
523 | } | |
524 | } | |
525 | ||
fcb1fba5 | 526 | public function testDeleteOneTagEntry() |
769e19dc | 527 | { |
fcb1fba5 | 528 | $entry = $this->client->getContainer() |
769e19dc J |
529 | ->get('doctrine.orm.entity_manager') |
530 | ->getRepository('WallabagCoreBundle:Entry') | |
a0e1eafc | 531 | ->findOneWithTags($this->user->getId()); |
fcb1fba5 | 532 | $entry = $entry[0]; |
769e19dc J |
533 | |
534 | if (!$entry) { | |
535 | $this->markTestSkipped('No content found in db.'); | |
536 | } | |
537 | ||
538 | // hydrate the tags relations | |
539 | $nbTags = count($entry->getTags()); | |
540 | $tag = $entry->getTags()[0]; | |
541 | ||
fcb1fba5 | 542 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json'); |
769e19dc | 543 | |
fcb1fba5 | 544 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 545 | |
fcb1fba5 | 546 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
547 | |
548 | $this->assertArrayHasKey('tags', $content); | |
4346a860 | 549 | $this->assertEquals($nbTags - 1, count($content['tags'])); |
769e19dc J |
550 | } |
551 | ||
552 | public function testGetUserTags() | |
553 | { | |
fcb1fba5 | 554 | $this->client->request('GET', '/api/tags.json'); |
769e19dc | 555 | |
fcb1fba5 | 556 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 557 | |
fcb1fba5 | 558 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
559 | |
560 | $this->assertGreaterThan(0, $content); | |
561 | $this->assertArrayHasKey('id', $content[0]); | |
562 | $this->assertArrayHasKey('label', $content[0]); | |
563 | ||
564 | return end($content); | |
565 | } | |
566 | ||
567 | /** | |
568 | * @depends testGetUserTags | |
569 | */ | |
570 | public function testDeleteUserTag($tag) | |
571 | { | |
fcb1fba5 | 572 | $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json'); |
769e19dc | 573 | |
fcb1fba5 | 574 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 575 | |
fcb1fba5 | 576 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
577 | |
578 | $this->assertArrayHasKey('label', $content); | |
579 | $this->assertEquals($tag['label'], $content['label']); | |
fc732227 | 580 | $this->assertEquals($tag['slug'], $content['slug']); |
4059a061 | 581 | |
a0e1eafc | 582 | $entries = $this->client->getContainer() |
4059a061 JB |
583 | ->get('doctrine.orm.entity_manager') |
584 | ->getRepository('WallabagCoreBundle:Entry') | |
585 | ->findAllByTagId($this->user->getId(), $tag['id']); | |
586 | ||
587 | $this->assertCount(0, $entries); | |
769e19dc | 588 | } |
9761bfa1 | 589 | |
a0e1eafc JB |
590 | public function testDeleteTagByLabel() |
591 | { | |
592 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
593 | $entry = $this->client->getContainer() | |
594 | ->get('doctrine.orm.entity_manager') | |
595 | ->getRepository('WallabagCoreBundle:Entry') | |
596 | ->findOneWithTags($this->user->getId()); | |
597 | ||
598 | $entry = $entry[0]; | |
599 | ||
600 | $tag = new Tag(); | |
601 | $tag->setLabel('Awesome tag for test'); | |
602 | $em->persist($tag); | |
603 | ||
604 | $entry->addTag($tag); | |
605 | ||
606 | $em->persist($entry); | |
607 | $em->flush(); | |
608 | ||
609 | $this->client->request('DELETE', '/api/tag/label.json', ['tag' => $tag->getLabel()]); | |
610 | ||
611 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
612 | ||
613 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
614 | ||
615 | $this->assertArrayHasKey('label', $content); | |
616 | $this->assertEquals($tag->getLabel(), $content['label']); | |
617 | $this->assertEquals($tag->getSlug(), $content['slug']); | |
618 | ||
619 | $entries = $this->client->getContainer() | |
620 | ->get('doctrine.orm.entity_manager') | |
621 | ->getRepository('WallabagCoreBundle:Entry') | |
622 | ->findAllByTagId($this->user->getId(), $tag->getId()); | |
623 | ||
624 | $this->assertCount(0, $entries); | |
625 | } | |
626 | ||
627 | public function testDeleteTagByLabelNotFound() | |
628 | { | |
629 | $this->client->request('DELETE', '/api/tag/label.json', ['tag' => 'does not exist']); | |
630 | ||
631 | $this->assertEquals(404, $this->client->getResponse()->getStatusCode()); | |
632 | } | |
633 | ||
634 | public function testDeleteTagsByLabel() | |
635 | { | |
636 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
637 | $entry = $this->client->getContainer() | |
638 | ->get('doctrine.orm.entity_manager') | |
639 | ->getRepository('WallabagCoreBundle:Entry') | |
640 | ->findOneWithTags($this->user->getId()); | |
641 | ||
642 | $entry = $entry[0]; | |
643 | ||
644 | $tag = new Tag(); | |
645 | $tag->setLabel('Awesome tag for tagsLabel'); | |
646 | $em->persist($tag); | |
647 | ||
648 | $tag2 = new Tag(); | |
649 | $tag2->setLabel('Awesome tag for tagsLabel 2'); | |
650 | $em->persist($tag2); | |
651 | ||
652 | $entry->addTag($tag); | |
653 | $entry->addTag($tag2); | |
654 | ||
655 | $em->persist($entry); | |
656 | $em->flush(); | |
657 | ||
658 | $this->client->request('DELETE', '/api/tags/label.json', ['tags' => $tag->getLabel().','.$tag2->getLabel()]); | |
659 | ||
660 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
661 | ||
662 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
663 | ||
664 | $this->assertCount(2, $content); | |
665 | ||
666 | $this->assertArrayHasKey('label', $content[0]); | |
667 | $this->assertEquals($tag->getLabel(), $content[0]['label']); | |
668 | $this->assertEquals($tag->getSlug(), $content[0]['slug']); | |
669 | ||
670 | $this->assertArrayHasKey('label', $content[1]); | |
671 | $this->assertEquals($tag2->getLabel(), $content[1]['label']); | |
672 | $this->assertEquals($tag2->getSlug(), $content[1]['slug']); | |
673 | ||
674 | $entries = $this->client->getContainer() | |
675 | ->get('doctrine.orm.entity_manager') | |
676 | ->getRepository('WallabagCoreBundle:Entry') | |
677 | ->findAllByTagId($this->user->getId(), $tag->getId()); | |
678 | ||
679 | $this->assertCount(0, $entries); | |
680 | ||
681 | $entries = $this->client->getContainer() | |
682 | ->get('doctrine.orm.entity_manager') | |
683 | ->getRepository('WallabagCoreBundle:Entry') | |
684 | ->findAllByTagId($this->user->getId(), $tag2->getId()); | |
685 | ||
686 | $this->assertCount(0, $entries); | |
687 | } | |
688 | ||
689 | public function testDeleteTagsByLabelNotFound() | |
690 | { | |
691 | $this->client->request('DELETE', '/api/tags/label.json', ['tags' => 'does not exist']); | |
692 | ||
693 | $this->assertEquals(404, $this->client->getResponse()->getStatusCode()); | |
694 | } | |
695 | ||
6f8310b4 TC |
696 | public function testGetVersion() |
697 | { | |
698 | $this->client->request('GET', '/api/version'); | |
9761bfa1 V |
699 | |
700 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
701 | ||
702 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
703 | ||
6f8310b4 | 704 | $this->assertEquals($this->client->getContainer()->getParameter('wallabag_core.version'), $content); |
9761bfa1 | 705 | } |
bba271e6 YE |
706 | |
707 | public function testSaveIsArchivedAfterPost() | |
708 | { | |
709 | $entry = $this->client->getContainer() | |
710 | ->get('doctrine.orm.entity_manager') | |
711 | ->getRepository('WallabagCoreBundle:Entry') | |
712 | ->findOneBy(['user' => 1, 'isArchived' => true]); | |
713 | ||
714 | if (!$entry) { | |
715 | $this->markTestSkipped('No content found in db.'); | |
716 | } | |
717 | ||
718 | $this->client->request('POST', '/api/entries.json', [ | |
719 | 'url' => $entry->getUrl(), | |
720 | ]); | |
721 | ||
722 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
723 | ||
724 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
725 | ||
69221684 | 726 | $this->assertEquals(true, $content['is_archived']); |
bba271e6 YE |
727 | } |
728 | ||
729 | public function testSaveIsStarredAfterPost() | |
730 | { | |
731 | $entry = $this->client->getContainer() | |
732 | ->get('doctrine.orm.entity_manager') | |
733 | ->getRepository('WallabagCoreBundle:Entry') | |
734 | ->findOneBy(['user' => 1, 'isStarred' => true]); | |
735 | ||
736 | if (!$entry) { | |
737 | $this->markTestSkipped('No content found in db.'); | |
738 | } | |
739 | ||
740 | $this->client->request('POST', '/api/entries.json', [ | |
741 | 'url' => $entry->getUrl(), | |
742 | ]); | |
743 | ||
744 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
745 | ||
746 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
747 | ||
69221684 | 748 | $this->assertEquals(true, $content['is_starred']); |
bba271e6 YE |
749 | } |
750 | ||
751 | public function testSaveIsArchivedAfterPatch() | |
752 | { | |
753 | $entry = $this->client->getContainer() | |
754 | ->get('doctrine.orm.entity_manager') | |
755 | ->getRepository('WallabagCoreBundle:Entry') | |
756 | ->findOneBy(['user' => 1, 'isArchived' => true]); | |
757 | ||
758 | if (!$entry) { | |
759 | $this->markTestSkipped('No content found in db.'); | |
760 | } | |
761 | ||
762 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | |
763 | 'title' => $entry->getTitle().'++', | |
764 | ]); | |
765 | ||
766 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
767 | ||
768 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
769 | ||
770 | $this->assertEquals(true, $content['is_archived']); | |
771 | } | |
772 | ||
773 | public function testSaveIsStarredAfterPatch() | |
774 | { | |
775 | $entry = $this->client->getContainer() | |
776 | ->get('doctrine.orm.entity_manager') | |
777 | ->getRepository('WallabagCoreBundle:Entry') | |
778 | ->findOneBy(['user' => 1, 'isStarred' => true]); | |
779 | ||
780 | if (!$entry) { | |
781 | $this->markTestSkipped('No content found in db.'); | |
782 | } | |
783 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | |
784 | 'title' => $entry->getTitle().'++', | |
785 | ]); | |
786 | ||
787 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
788 | ||
789 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
790 | ||
69221684 | 791 | $this->assertEquals(true, $content['is_starred']); |
bba271e6 | 792 | } |
6273fefd JB |
793 | |
794 | public function testGetEntriesExists() | |
795 | { | |
796 | $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2'); | |
797 | ||
798 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
799 | ||
800 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
801 | ||
802 | $this->assertEquals(true, $content['exists']); | |
803 | } | |
55551e33 JB |
804 | |
805 | public function testGetEntriesExistsWhichDoesNotExists() | |
806 | { | |
807 | $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2'); | |
808 | ||
809 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
810 | ||
811 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
812 | ||
813 | $this->assertEquals(false, $content['exists']); | |
814 | } | |
0b174d69 JB |
815 | |
816 | public function testGetEntriesExistsWithNoUrl() | |
817 | { | |
818 | $this->client->request('GET', '/api/entries/exists?url='); | |
819 | ||
820 | $this->assertEquals(403, $this->client->getResponse()->getStatusCode()); | |
821 | } | |
769e19dc | 822 | } |