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