3 namespace Tests\Wallabag\ApiBundle\Controller
;
5 use Tests\Wallabag\ApiBundle\WallabagApiTestCase
;
6 use Wallabag\CoreBundle\Entity\Tag
;
8 class WallabagRestControllerTest
extends WallabagApiTestCase
10 protected static $salt;
12 public function testGetOneEntry()
14 $entry = $this->client
->getContainer()
15 ->get('doctrine.orm.entity_manager')
16 ->getRepository('WallabagCoreBundle:Entry')
17 ->findOneBy(['user' => 1, 'isArchived' => false]);
20 $this->markTestSkipped('No content found in db.');
23 $this->client
->request('GET', '/api/entries/'.$entry->getId().'.json');
24 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
26 $content = json_decode($this->client
->getResponse()->getContent(), true);
28 $this->assertEquals($entry->getTitle(), $content['title']);
29 $this->assertEquals($entry->getUrl(), $content['url']);
30 $this->assertCount(count($entry->getTags()), $content['tags']);
31 $this->assertEquals($entry->getUserName(), $content['user_name']);
32 $this->assertEquals($entry->getUserEmail(), $content['user_email']);
33 $this->assertEquals($entry->getUserId(), $content['user_id']);
36 $this->client
->getResponse()->headers
->contains(
43 public function testGetOneEntryWrongUser()
45 $entry = $this->client
->getContainer()
46 ->get('doctrine.orm.entity_manager')
47 ->getRepository('WallabagCoreBundle:Entry')
48 ->findOneBy(['user' => 2, 'isArchived' => false]);
51 $this->markTestSkipped('No content found in db.');
54 $this->client
->request('GET', '/api/entries/'.$entry->getId().'.json');
56 $this->assertEquals(403, $this->client
->getResponse()->getStatusCode());
59 public function testGetEntries()
61 $this->client
->request('GET', '/api/entries');
63 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
65 $content = json_decode($this->client
->getResponse()->getContent(), true);
67 $this->assertGreaterThanOrEqual(1, count($content));
68 $this->assertNotEmpty($content['_embedded']['items']);
69 $this->assertGreaterThanOrEqual(1, $content['total']);
70 $this->assertEquals(1, $content['page']);
71 $this->assertGreaterThanOrEqual(1, $content['pages']);
74 $this->client
->getResponse()->headers
->contains(
81 public function testGetStarredEntries()
83 $this->client
->request('GET', '/api/entries', ['star' => 1, 'sort' => 'updated']);
85 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
87 $content = json_decode($this->client
->getResponse()->getContent(), true);
89 $this->assertGreaterThanOrEqual(1, count($content));
90 $this->assertNotEmpty($content['_embedded']['items']);
91 $this->assertGreaterThanOrEqual(1, $content['total']);
92 $this->assertEquals(1, $content['page']);
93 $this->assertGreaterThanOrEqual(1, $content['pages']);
96 $this->client
->getResponse()->headers
->contains(
103 public function testGetArchiveEntries()
105 $this->client
->request('GET', '/api/entries', ['archive' => 1]);
107 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
109 $content = json_decode($this->client
->getResponse()->getContent(), true);
111 $this->assertGreaterThanOrEqual(1, count($content));
112 $this->assertNotEmpty($content['_embedded']['items']);
113 $this->assertGreaterThanOrEqual(1, $content['total']);
114 $this->assertEquals(1, $content['page']);
115 $this->assertGreaterThanOrEqual(1, $content['pages']);
118 $this->client
->getResponse()->headers
->contains(
125 public function testGetTaggedEntries()
127 $this->client
->request('GET', '/api/entries', ['tags' => 'foo,bar']);
129 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
131 $content = json_decode($this->client
->getResponse()->getContent(), true);
133 $this->assertGreaterThanOrEqual(1, count($content));
134 $this->assertNotEmpty($content['_embedded']['items']);
135 $this->assertGreaterThanOrEqual(1, $content['total']);
136 $this->assertEquals(1, $content['page']);
137 $this->assertGreaterThanOrEqual(1, $content['pages']);
140 $this->client
->getResponse()->headers
->contains(
147 public function testGetDatedEntries()
149 $this->client
->request('GET', '/api/entries', ['since' => 1]);
151 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
153 $content = json_decode($this->client
->getResponse()->getContent(), true);
155 $this->assertGreaterThanOrEqual(1, count($content));
156 $this->assertNotEmpty($content['_embedded']['items']);
157 $this->assertGreaterThanOrEqual(1, $content['total']);
158 $this->assertEquals(1, $content['page']);
159 $this->assertGreaterThanOrEqual(1, $content['pages']);
162 $this->client
->getResponse()->headers
->contains(
169 public function testGetDatedSupEntries()
171 $future = new \
DateTime(date('Y-m-d H:i:s'));
172 $this->client
->request('GET', '/api/entries', ['since' => $future->getTimestamp() +
1000]);
174 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
176 $content = json_decode($this->client
->getResponse()->getContent(), true);
178 $this->assertGreaterThanOrEqual(1, count($content));
179 $this->assertEmpty($content['_embedded']['items']);
180 $this->assertEquals(0, $content['total']);
181 $this->assertEquals(1, $content['page']);
182 $this->assertEquals(1, $content['pages']);
185 $this->client
->getResponse()->headers
->contains(
192 public function testDeleteEntry()
194 $entry = $this->client
->getContainer()
195 ->get('doctrine.orm.entity_manager')
196 ->getRepository('WallabagCoreBundle:Entry')
200 $this->markTestSkipped('No content found in db.');
203 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'.json');
205 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
207 $content = json_decode($this->client
->getResponse()->getContent(), true);
209 $this->assertEquals($entry->getTitle(), $content['title']);
210 $this->assertEquals($entry->getUrl(), $content['url']);
212 // We'll try to delete this entry again
213 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'.json');
215 $this->assertEquals(404, $this->client
->getResponse()->getStatusCode());
218 public function testPostEntry()
220 $this->client
->request('POST', '/api/entries.json', [
221 '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',
223 'title' => 'New title for my article',
226 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
228 $content = json_decode($this->client
->getResponse()->getContent(), true);
230 $this->assertGreaterThan(0, $content['id']);
231 $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']);
232 $this->assertEquals(false, $content['is_archived']);
233 $this->assertEquals(false, $content['is_starred']);
234 $this->assertEquals('New title for my article', $content['title']);
235 $this->assertEquals(1, $content['user_id']);
236 $this->assertCount(1, $content['tags']);
239 public function testPostSameEntry()
241 $this->client
->request('POST', '/api/entries.json', [
242 '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',
244 'tags' => 'google, apple',
247 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
249 $content = json_decode($this->client
->getResponse()->getContent(), true);
251 $this->assertGreaterThan(0, $content['id']);
252 $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']);
253 $this->assertEquals(true, $content['is_archived']);
254 $this->assertEquals(false, $content['is_starred']);
255 $this->assertCount(2, $content['tags']);
258 public function testPostArchivedAndStarredEntry()
260 $this->client
->request('POST', '/api/entries.json', [
261 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
266 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
268 $content = json_decode($this->client
->getResponse()->getContent(), true);
270 $this->assertGreaterThan(0, $content['id']);
271 $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']);
272 $this->assertEquals(true, $content['is_archived']);
273 $this->assertEquals(true, $content['is_starred']);
274 $this->assertEquals(1, $content['user_id']);
277 public function testPostArchivedAndStarredEntryWithoutQuotes()
279 $this->client
->request('POST', '/api/entries.json', [
280 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
285 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
287 $content = json_decode($this->client
->getResponse()->getContent(), true);
289 $this->assertGreaterThan(0, $content['id']);
290 $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']);
291 $this->assertEquals(false, $content['is_archived']);
292 $this->assertEquals(true, $content['is_starred']);
295 public function testPatchEntry()
297 $entry = $this->client
->getContainer()
298 ->get('doctrine.orm.entity_manager')
299 ->getRepository('WallabagCoreBundle:Entry')
303 $this->markTestSkipped('No content found in db.');
306 // hydrate the tags relations
307 $nbTags = count($entry->getTags());
309 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
310 'title' => 'New awesome title',
311 'tags' => 'new tag '.uniqid(),
316 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
318 $content = json_decode($this->client
->getResponse()->getContent(), true);
320 $this->assertEquals($entry->getId(), $content['id']);
321 $this->assertEquals($entry->getUrl(), $content['url']);
322 $this->assertEquals('New awesome title', $content['title']);
323 $this->assertGreaterThan($nbTags, count($content['tags']));
324 $this->assertEquals(1, $content['user_id']);
327 public function testPatchEntryWithoutQuotes()
329 $entry = $this->client
->getContainer()
330 ->get('doctrine.orm.entity_manager')
331 ->getRepository('WallabagCoreBundle:Entry')
335 $this->markTestSkipped('No content found in db.');
338 // hydrate the tags relations
339 $nbTags = count($entry->getTags());
341 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
342 'title' => 'New awesome title',
343 'tags' => 'new tag '.uniqid(),
348 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
350 $content = json_decode($this->client
->getResponse()->getContent(), true);
352 $this->assertEquals($entry->getId(), $content['id']);
353 $this->assertEquals($entry->getUrl(), $content['url']);
354 $this->assertEquals('New awesome title', $content['title']);
355 $this->assertGreaterThan($nbTags, count($content['tags']));
358 public function testGetTagsEntry()
360 $entry = $this->client
->getContainer()
361 ->get('doctrine.orm.entity_manager')
362 ->getRepository('WallabagCoreBundle:Entry')
363 ->findOneWithTags($this->user
->getId());
368 $this->markTestSkipped('No content found in db.');
372 foreach ($entry->getTags() as $tag) {
373 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
376 $this->client
->request('GET', '/api/entries/'.$entry->getId().'/tags');
378 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT
), $this->client
->getResponse()->getContent());
381 public function testPostTagsOnEntry()
383 $entry = $this->client
->getContainer()
384 ->get('doctrine.orm.entity_manager')
385 ->getRepository('WallabagCoreBundle:Entry')
389 $this->markTestSkipped('No content found in db.');
392 $nbTags = count($entry->getTags());
394 $newTags = 'tag1,tag2,tag3';
396 $this->client
->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]);
398 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
400 $content = json_decode($this->client
->getResponse()->getContent(), true);
402 $this->assertArrayHasKey('tags', $content);
403 $this->assertEquals($nbTags +
3, count($content['tags']));
405 $entryDB = $this->client
->getContainer()
406 ->get('doctrine.orm.entity_manager')
407 ->getRepository('WallabagCoreBundle:Entry')
408 ->find($entry->getId());
411 foreach ($entryDB->getTags()->toArray() as $tag) {
412 $tagsInDB[$tag->getId()] = $tag->getLabel();
415 foreach (explode(',', $newTags) as $tag) {
416 $this->assertContains($tag, $tagsInDB);
420 public function testDeleteOneTagEntry()
422 $entry = $this->client
->getContainer()
423 ->get('doctrine.orm.entity_manager')
424 ->getRepository('WallabagCoreBundle:Entry')
425 ->findOneWithTags($this->user
->getId());
429 $this->markTestSkipped('No content found in db.');
432 // hydrate the tags relations
433 $nbTags = count($entry->getTags());
434 $tag = $entry->getTags()[0];
436 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json');
438 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
440 $content = json_decode($this->client
->getResponse()->getContent(), true);
442 $this->assertArrayHasKey('tags', $content);
443 $this->assertEquals($nbTags - 1, count($content['tags']));
446 public function testGetUserTags()
448 $this->client
->request('GET', '/api/tags.json');
450 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
452 $content = json_decode($this->client
->getResponse()->getContent(), true);
454 $this->assertGreaterThan(0, $content);
455 $this->assertArrayHasKey('id', $content[0]);
456 $this->assertArrayHasKey('label', $content[0]);
458 return end($content);
462 * @depends testGetUserTags
464 public function testDeleteUserTag($tag)
466 $this->client
->request('DELETE', '/api/tags/'.$tag['id'].'.json');
468 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
470 $content = json_decode($this->client
->getResponse()->getContent(), true);
472 $this->assertArrayHasKey('label', $content);
473 $this->assertEquals($tag['label'], $content['label']);
474 $this->assertEquals($tag['slug'], $content['slug']);
476 $entries = $this->client
->getContainer()
477 ->get('doctrine.orm.entity_manager')
478 ->getRepository('WallabagCoreBundle:Entry')
479 ->findAllByTagId($this->user
->getId(), $tag['id']);
481 $this->assertCount(0, $entries);
484 public function testDeleteTagByLabel()
486 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
487 $entry = $this->client
->getContainer()
488 ->get('doctrine.orm.entity_manager')
489 ->getRepository('WallabagCoreBundle:Entry')
490 ->findOneWithTags($this->user
->getId());
495 $tag->setLabel('Awesome tag for test');
498 $entry->addTag($tag);
500 $em->persist($entry);
503 $this->client
->request('DELETE', '/api/tag/label.json', ['tag' => $tag->getLabel()]);
505 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
507 $content = json_decode($this->client
->getResponse()->getContent(), true);
509 $this->assertArrayHasKey('label', $content);
510 $this->assertEquals($tag->getLabel(), $content['label']);
511 $this->assertEquals($tag->getSlug(), $content['slug']);
513 $entries = $this->client
->getContainer()
514 ->get('doctrine.orm.entity_manager')
515 ->getRepository('WallabagCoreBundle:Entry')
516 ->findAllByTagId($this->user
->getId(), $tag->getId());
518 $this->assertCount(0, $entries);
521 public function testDeleteTagByLabelNotFound()
523 $this->client
->request('DELETE', '/api/tag/label.json', ['tag' => 'does not exist']);
525 $this->assertEquals(404, $this->client
->getResponse()->getStatusCode());
528 public function testDeleteTagsByLabel()
530 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
531 $entry = $this->client
->getContainer()
532 ->get('doctrine.orm.entity_manager')
533 ->getRepository('WallabagCoreBundle:Entry')
534 ->findOneWithTags($this->user
->getId());
539 $tag->setLabel('Awesome tag for tagsLabel');
543 $tag2->setLabel('Awesome tag for tagsLabel 2');
546 $entry->addTag($tag);
547 $entry->addTag($tag2);
549 $em->persist($entry);
552 $this->client
->request('DELETE', '/api/tags/label.json', ['tags' => $tag->getLabel().','.$tag2->getLabel()]);
554 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
556 $content = json_decode($this->client
->getResponse()->getContent(), true);
558 $this->assertCount(2, $content);
560 $this->assertArrayHasKey('label', $content[0]);
561 $this->assertEquals($tag->getLabel(), $content[0]['label']);
562 $this->assertEquals($tag->getSlug(), $content[0]['slug']);
564 $this->assertArrayHasKey('label', $content[1]);
565 $this->assertEquals($tag2->getLabel(), $content[1]['label']);
566 $this->assertEquals($tag2->getSlug(), $content[1]['slug']);
568 $entries = $this->client
->getContainer()
569 ->get('doctrine.orm.entity_manager')
570 ->getRepository('WallabagCoreBundle:Entry')
571 ->findAllByTagId($this->user
->getId(), $tag->getId());
573 $this->assertCount(0, $entries);
575 $entries = $this->client
->getContainer()
576 ->get('doctrine.orm.entity_manager')
577 ->getRepository('WallabagCoreBundle:Entry')
578 ->findAllByTagId($this->user
->getId(), $tag2->getId());
580 $this->assertCount(0, $entries);
583 public function testDeleteTagsByLabelNotFound()
585 $this->client
->request('DELETE', '/api/tags/label.json', ['tags' => 'does not exist']);
587 $this->assertEquals(404, $this->client
->getResponse()->getStatusCode());
590 public function testGetVersion()
592 $this->client
->request('GET', '/api/version');
594 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
596 $content = json_decode($this->client
->getResponse()->getContent(), true);
598 $this->assertEquals($this->client
->getContainer()->getParameter('wallabag_core.version'), $content);
601 public function testSaveIsArchivedAfterPost()
603 $entry = $this->client
->getContainer()
604 ->get('doctrine.orm.entity_manager')
605 ->getRepository('WallabagCoreBundle:Entry')
606 ->findOneBy(['user' => 1, 'isArchived' => true]);
609 $this->markTestSkipped('No content found in db.');
612 $this->client
->request('POST', '/api/entries.json', [
613 'url' => $entry->getUrl(),
616 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
618 $content = json_decode($this->client
->getResponse()->getContent(), true);
620 $this->assertEquals(true, $content['is_archived']);
623 public function testSaveIsStarredAfterPost()
625 $entry = $this->client
->getContainer()
626 ->get('doctrine.orm.entity_manager')
627 ->getRepository('WallabagCoreBundle:Entry')
628 ->findOneBy(['user' => 1, 'isStarred' => true]);
631 $this->markTestSkipped('No content found in db.');
634 $this->client
->request('POST', '/api/entries.json', [
635 'url' => $entry->getUrl(),
638 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
640 $content = json_decode($this->client
->getResponse()->getContent(), true);
642 $this->assertEquals(true, $content['is_starred']);
645 public function testSaveIsArchivedAfterPatch()
647 $entry = $this->client
->getContainer()
648 ->get('doctrine.orm.entity_manager')
649 ->getRepository('WallabagCoreBundle:Entry')
650 ->findOneBy(['user' => 1, 'isArchived' => true]);
653 $this->markTestSkipped('No content found in db.');
656 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
657 'title' => $entry->getTitle().'++',
660 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
662 $content = json_decode($this->client
->getResponse()->getContent(), true);
664 $this->assertEquals(true, $content['is_archived']);
667 public function testSaveIsStarredAfterPatch()
669 $entry = $this->client
->getContainer()
670 ->get('doctrine.orm.entity_manager')
671 ->getRepository('WallabagCoreBundle:Entry')
672 ->findOneBy(['user' => 1, 'isStarred' => true]);
675 $this->markTestSkipped('No content found in db.');
677 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
678 'title' => $entry->getTitle().'++',
681 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
683 $content = json_decode($this->client
->getResponse()->getContent(), true);
685 $this->assertEquals(true, $content['is_starred']);