3 namespace Tests\Wallabag\ApiBundle\Controller
;
5 use Tests\Wallabag\ApiBundle\WallabagApiTestCase
;
6 use Wallabag\CoreBundle\Entity\Tag
;
8 class EntryRestControllerTest
extends WallabagApiTestCase
10 public function testGetOneEntry()
12 $entry = $this->client
->getContainer()
13 ->get('doctrine.orm.entity_manager')
14 ->getRepository('WallabagCoreBundle:Entry')
15 ->findOneBy(['user' => 1, 'isArchived' => false]);
18 $this->markTestSkipped('No content found in db.');
21 $this->client
->request('GET', '/api/entries/'.$entry->getId().'.json');
22 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
24 $content = json_decode($this->client
->getResponse()->getContent(), true);
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']);
34 $this->client
->getResponse()->headers
->contains(
41 public function testGetOneEntryWrongUser()
43 $entry = $this->client
->getContainer()
44 ->get('doctrine.orm.entity_manager')
45 ->getRepository('WallabagCoreBundle:Entry')
46 ->findOneBy(['user' => 2, 'isArchived' => false]);
49 $this->markTestSkipped('No content found in db.');
52 $this->client
->request('GET', '/api/entries/'.$entry->getId().'.json');
54 $this->assertEquals(403, $this->client
->getResponse()->getStatusCode());
57 public function testGetEntries()
59 $this->client
->request('GET', '/api/entries');
61 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
63 $content = json_decode($this->client
->getResponse()->getContent(), true);
65 $this->assertGreaterThanOrEqual(1, count($content));
66 $this->assertNotEmpty($content['_embedded']['items']);
67 $this->assertGreaterThanOrEqual(1, $content['total']);
68 $this->assertEquals(1, $content['page']);
69 $this->assertGreaterThanOrEqual(1, $content['pages']);
72 $this->client
->getResponse()->headers
->contains(
79 public function testGetEntriesWithFullOptions()
81 $this->client
->request('GET', '/api/entries', [
89 'since' => 1443274283,
92 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
94 $content = json_decode($this->client
->getResponse()->getContent(), true);
96 $this->assertGreaterThanOrEqual(1, count($content));
97 $this->assertArrayHasKey('items', $content['_embedded']);
98 $this->assertGreaterThanOrEqual(0, $content['total']);
99 $this->assertEquals(1, $content['page']);
100 $this->assertEquals(2, $content['limit']);
101 $this->assertGreaterThanOrEqual(1, $content['pages']);
103 $this->assertArrayHasKey('_links', $content);
104 $this->assertArrayHasKey('self', $content['_links']);
105 $this->assertArrayHasKey('first', $content['_links']);
106 $this->assertArrayHasKey('last', $content['_links']);
108 foreach (['self', 'first', 'last'] as $link) {
109 $this->assertArrayHasKey('href', $content['_links'][$link]);
110 $this->assertContains('archive=1', $content['_links'][$link]['href']);
111 $this->assertContains('starred=1', $content['_links'][$link]['href']);
112 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
113 $this->assertContains('order=asc', $content['_links'][$link]['href']);
114 $this->assertContains('tags=foo', $content['_links'][$link]['href']);
115 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
119 $this->client
->getResponse()->headers
->contains(
126 public function testGetStarredEntries()
128 $this->client
->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']);
130 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
132 $content = json_decode($this->client
->getResponse()->getContent(), true);
134 $this->assertGreaterThanOrEqual(1, count($content));
135 $this->assertNotEmpty($content['_embedded']['items']);
136 $this->assertGreaterThanOrEqual(1, $content['total']);
137 $this->assertEquals(1, $content['page']);
138 $this->assertGreaterThanOrEqual(1, $content['pages']);
140 $this->assertArrayHasKey('_links', $content);
141 $this->assertArrayHasKey('self', $content['_links']);
142 $this->assertArrayHasKey('first', $content['_links']);
143 $this->assertArrayHasKey('last', $content['_links']);
145 foreach (['self', 'first', 'last'] as $link) {
146 $this->assertArrayHasKey('href', $content['_links'][$link]);
147 $this->assertContains('starred=1', $content['_links'][$link]['href']);
148 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
152 $this->client
->getResponse()->headers
->contains(
159 public function testGetArchiveEntries()
161 $this->client
->request('GET', '/api/entries', ['archive' => 1]);
163 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
165 $content = json_decode($this->client
->getResponse()->getContent(), true);
167 $this->assertGreaterThanOrEqual(1, count($content));
168 $this->assertNotEmpty($content['_embedded']['items']);
169 $this->assertGreaterThanOrEqual(1, $content['total']);
170 $this->assertEquals(1, $content['page']);
171 $this->assertGreaterThanOrEqual(1, $content['pages']);
173 $this->assertArrayHasKey('_links', $content);
174 $this->assertArrayHasKey('self', $content['_links']);
175 $this->assertArrayHasKey('first', $content['_links']);
176 $this->assertArrayHasKey('last', $content['_links']);
178 foreach (['self', 'first', 'last'] as $link) {
179 $this->assertArrayHasKey('href', $content['_links'][$link]);
180 $this->assertContains('archive=1', $content['_links'][$link]['href']);
184 $this->client
->getResponse()->headers
->contains(
191 public function testGetTaggedEntries()
193 $this->client
->request('GET', '/api/entries', ['tags' => 'foo,bar']);
195 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
197 $content = json_decode($this->client
->getResponse()->getContent(), true);
199 $this->assertGreaterThanOrEqual(1, count($content));
200 $this->assertNotEmpty($content['_embedded']['items']);
201 $this->assertGreaterThanOrEqual(1, $content['total']);
202 $this->assertEquals(1, $content['page']);
203 $this->assertGreaterThanOrEqual(1, $content['pages']);
205 $this->assertArrayHasKey('_links', $content);
206 $this->assertArrayHasKey('self', $content['_links']);
207 $this->assertArrayHasKey('first', $content['_links']);
208 $this->assertArrayHasKey('last', $content['_links']);
210 foreach (['self', 'first', 'last'] as $link) {
211 $this->assertArrayHasKey('href', $content['_links'][$link]);
212 $this->assertContains('tags='.urlencode('foo,bar'), $content['_links'][$link]['href']);
216 $this->client
->getResponse()->headers
->contains(
223 public function testGetDatedEntries()
225 $this->client
->request('GET', '/api/entries', ['since' => 1443274283]);
227 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
229 $content = json_decode($this->client
->getResponse()->getContent(), true);
231 $this->assertGreaterThanOrEqual(1, count($content));
232 $this->assertNotEmpty($content['_embedded']['items']);
233 $this->assertGreaterThanOrEqual(1, $content['total']);
234 $this->assertEquals(1, $content['page']);
235 $this->assertGreaterThanOrEqual(1, $content['pages']);
237 $this->assertArrayHasKey('_links', $content);
238 $this->assertArrayHasKey('self', $content['_links']);
239 $this->assertArrayHasKey('first', $content['_links']);
240 $this->assertArrayHasKey('last', $content['_links']);
242 foreach (['self', 'first', 'last'] as $link) {
243 $this->assertArrayHasKey('href', $content['_links'][$link]);
244 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
248 $this->client
->getResponse()->headers
->contains(
255 public function testGetDatedSupEntries()
257 $future = new \
DateTime(date('Y-m-d H:i:s'));
258 $this->client
->request('GET', '/api/entries', ['since' => $future->getTimestamp() +
1000]);
260 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
262 $content = json_decode($this->client
->getResponse()->getContent(), true);
264 $this->assertGreaterThanOrEqual(1, count($content));
265 $this->assertEmpty($content['_embedded']['items']);
266 $this->assertEquals(0, $content['total']);
267 $this->assertEquals(1, $content['page']);
268 $this->assertEquals(1, $content['pages']);
270 $this->assertArrayHasKey('_links', $content);
271 $this->assertArrayHasKey('self', $content['_links']);
272 $this->assertArrayHasKey('first', $content['_links']);
273 $this->assertArrayHasKey('last', $content['_links']);
275 foreach (['self', 'first', 'last'] as $link) {
276 $this->assertArrayHasKey('href', $content['_links'][$link]);
277 $this->assertContains('since='.($future->getTimestamp() +
1000), $content['_links'][$link]['href']);
281 $this->client
->getResponse()->headers
->contains(
288 public function testDeleteEntry()
290 $entry = $this->client
->getContainer()
291 ->get('doctrine.orm.entity_manager')
292 ->getRepository('WallabagCoreBundle:Entry')
296 $this->markTestSkipped('No content found in db.');
299 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'.json');
301 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
303 $content = json_decode($this->client
->getResponse()->getContent(), true);
305 $this->assertEquals($entry->getTitle(), $content['title']);
306 $this->assertEquals($entry->getUrl(), $content['url']);
308 // We'll try to delete this entry again
309 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'.json');
311 $this->assertEquals(404, $this->client
->getResponse()->getStatusCode());
314 public function testPostEntry()
316 $this->client
->request('POST', '/api/entries.json', [
317 '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',
319 'title' => 'New title for my article',
322 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
324 $content = json_decode($this->client
->getResponse()->getContent(), true);
326 $this->assertGreaterThan(0, $content['id']);
327 $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']);
328 $this->assertEquals(false, $content['is_archived']);
329 $this->assertEquals(false, $content['is_starred']);
330 $this->assertEquals('New title for my article', $content['title']);
331 $this->assertEquals(1, $content['user_id']);
332 $this->assertCount(1, $content['tags']);
335 public function testPostSameEntry()
337 $this->client
->request('POST', '/api/entries.json', [
338 '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',
340 'tags' => 'google, apple',
343 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
345 $content = json_decode($this->client
->getResponse()->getContent(), true);
347 $this->assertGreaterThan(0, $content['id']);
348 $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']);
349 $this->assertEquals(true, $content['is_archived']);
350 $this->assertEquals(false, $content['is_starred']);
351 $this->assertCount(2, $content['tags']);
354 public function testPostArchivedAndStarredEntry()
356 $this->client
->request('POST', '/api/entries.json', [
357 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
362 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
364 $content = json_decode($this->client
->getResponse()->getContent(), true);
366 $this->assertGreaterThan(0, $content['id']);
367 $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']);
368 $this->assertEquals(true, $content['is_archived']);
369 $this->assertEquals(true, $content['is_starred']);
370 $this->assertEquals(1, $content['user_id']);
373 public function testPostArchivedAndStarredEntryWithoutQuotes()
375 $this->client
->request('POST', '/api/entries.json', [
376 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
381 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
383 $content = json_decode($this->client
->getResponse()->getContent(), true);
385 $this->assertGreaterThan(0, $content['id']);
386 $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']);
387 $this->assertEquals(false, $content['is_archived']);
388 $this->assertEquals(true, $content['is_starred']);
391 public function testPatchEntry()
393 $entry = $this->client
->getContainer()
394 ->get('doctrine.orm.entity_manager')
395 ->getRepository('WallabagCoreBundle:Entry')
399 $this->markTestSkipped('No content found in db.');
402 // hydrate the tags relations
403 $nbTags = count($entry->getTags());
405 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
406 'title' => 'New awesome title',
407 'tags' => 'new tag '.uniqid(),
412 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
414 $content = json_decode($this->client
->getResponse()->getContent(), true);
416 $this->assertEquals($entry->getId(), $content['id']);
417 $this->assertEquals($entry->getUrl(), $content['url']);
418 $this->assertEquals('New awesome title', $content['title']);
419 $this->assertGreaterThan($nbTags, count($content['tags']));
420 $this->assertEquals(1, $content['user_id']);
423 public function testPatchEntryWithoutQuotes()
425 $entry = $this->client
->getContainer()
426 ->get('doctrine.orm.entity_manager')
427 ->getRepository('WallabagCoreBundle:Entry')
431 $this->markTestSkipped('No content found in db.');
434 // hydrate the tags relations
435 $nbTags = count($entry->getTags());
437 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
438 'title' => 'New awesome title',
439 'tags' => 'new tag '.uniqid(),
444 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
446 $content = json_decode($this->client
->getResponse()->getContent(), true);
448 $this->assertEquals($entry->getId(), $content['id']);
449 $this->assertEquals($entry->getUrl(), $content['url']);
450 $this->assertEquals('New awesome title', $content['title']);
451 $this->assertGreaterThan($nbTags, count($content['tags']));
454 public function testGetTagsEntry()
456 $entry = $this->client
->getContainer()
457 ->get('doctrine.orm.entity_manager')
458 ->getRepository('WallabagCoreBundle:Entry')
459 ->findOneWithTags($this->user
->getId());
464 $this->markTestSkipped('No content found in db.');
468 foreach ($entry->getTags() as $tag) {
469 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
472 $this->client
->request('GET', '/api/entries/'.$entry->getId().'/tags');
474 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT
), $this->client
->getResponse()->getContent());
477 public function testPostTagsOnEntry()
479 $entry = $this->client
->getContainer()
480 ->get('doctrine.orm.entity_manager')
481 ->getRepository('WallabagCoreBundle:Entry')
485 $this->markTestSkipped('No content found in db.');
488 $nbTags = count($entry->getTags());
490 $newTags = 'tag1,tag2,tag3';
492 $this->client
->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]);
494 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
496 $content = json_decode($this->client
->getResponse()->getContent(), true);
498 $this->assertArrayHasKey('tags', $content);
499 $this->assertEquals($nbTags +
3, count($content['tags']));
501 $entryDB = $this->client
->getContainer()
502 ->get('doctrine.orm.entity_manager')
503 ->getRepository('WallabagCoreBundle:Entry')
504 ->find($entry->getId());
507 foreach ($entryDB->getTags()->toArray() as $tag) {
508 $tagsInDB[$tag->getId()] = $tag->getLabel();
511 foreach (explode(',', $newTags) as $tag) {
512 $this->assertContains($tag, $tagsInDB);
516 public function testDeleteOneTagEntry()
518 $entry = $this->client
->getContainer()
519 ->get('doctrine.orm.entity_manager')
520 ->getRepository('WallabagCoreBundle:Entry')
521 ->findOneWithTags($this->user
->getId());
525 $this->markTestSkipped('No content found in db.');
528 // hydrate the tags relations
529 $nbTags = count($entry->getTags());
530 $tag = $entry->getTags()[0];
532 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json');
534 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
536 $content = json_decode($this->client
->getResponse()->getContent(), true);
538 $this->assertArrayHasKey('tags', $content);
539 $this->assertEquals($nbTags - 1, count($content['tags']));
542 public function testSaveIsArchivedAfterPost()
544 $entry = $this->client
->getContainer()
545 ->get('doctrine.orm.entity_manager')
546 ->getRepository('WallabagCoreBundle:Entry')
547 ->findOneBy(['user' => 1, 'isArchived' => true]);
550 $this->markTestSkipped('No content found in db.');
553 $this->client
->request('POST', '/api/entries.json', [
554 'url' => $entry->getUrl(),
557 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
559 $content = json_decode($this->client
->getResponse()->getContent(), true);
561 $this->assertEquals(true, $content['is_archived']);
564 public function testSaveIsStarredAfterPost()
566 $entry = $this->client
->getContainer()
567 ->get('doctrine.orm.entity_manager')
568 ->getRepository('WallabagCoreBundle:Entry')
569 ->findOneBy(['user' => 1, 'isStarred' => true]);
572 $this->markTestSkipped('No content found in db.');
575 $this->client
->request('POST', '/api/entries.json', [
576 'url' => $entry->getUrl(),
579 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
581 $content = json_decode($this->client
->getResponse()->getContent(), true);
583 $this->assertEquals(true, $content['is_starred']);
586 public function testSaveIsArchivedAfterPatch()
588 $entry = $this->client
->getContainer()
589 ->get('doctrine.orm.entity_manager')
590 ->getRepository('WallabagCoreBundle:Entry')
591 ->findOneBy(['user' => 1, 'isArchived' => true]);
594 $this->markTestSkipped('No content found in db.');
597 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
598 'title' => $entry->getTitle().'++',
601 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
603 $content = json_decode($this->client
->getResponse()->getContent(), true);
605 $this->assertEquals(true, $content['is_archived']);
608 public function testSaveIsStarredAfterPatch()
610 $entry = $this->client
->getContainer()
611 ->get('doctrine.orm.entity_manager')
612 ->getRepository('WallabagCoreBundle:Entry')
613 ->findOneBy(['user' => 1, 'isStarred' => true]);
616 $this->markTestSkipped('No content found in db.');
618 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
619 'title' => $entry->getTitle().'++',
622 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
624 $content = json_decode($this->client
->getResponse()->getContent(), true);
626 $this->assertEquals(true, $content['is_starred']);
629 public function testGetEntriesExists()
631 $this->client
->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2');
633 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
635 $content = json_decode($this->client
->getResponse()->getContent(), true);
637 $this->assertEquals(true, $content['exists']);
640 public function testGetEntriesExistsWithManyUrls()
642 $url1 = 'http://0.0.0.0/entry2';
643 $url2 = 'http://0.0.0.0/entry10';
644 $this->client
->request('GET', '/api/entries/exists?urls[]='.$url1.'&urls[]='.$url2);
646 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
648 $content = json_decode($this->client
->getResponse()->getContent(), true);
650 $this->assertArrayHasKey($url1, $content);
651 $this->assertArrayHasKey($url2, $content);
652 $this->assertEquals(true, $content[$url1]);
653 $this->assertEquals(false, $content[$url2]);
656 public function testGetEntriesExistsWhichDoesNotExists()
658 $this->client
->request('GET', '/api/entries/exists?url=http://google.com/entry2');
660 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
662 $content = json_decode($this->client
->getResponse()->getContent(), true);
664 $this->assertEquals(false, $content['exists']);
667 public function testGetEntriesExistsWithNoUrl()
669 $this->client
->request('GET', '/api/entries/exists?url=');
671 $this->assertEquals(403, $this->client
->getResponse()->getStatusCode());