3 namespace Tests\Wallabag\ApiBundle\Controller
;
5 use Tests\Wallabag\ApiBundle\WallabagApiTestCase
;
6 use Wallabag\CoreBundle\Entity\Entry
;
7 use Wallabag\CoreBundle\Entity\Tag
;
8 use Wallabag\CoreBundle\Helper\ContentProxy
;
9 use Wallabag\UserBundle\Entity\User
;
11 class EntryRestControllerTest
extends WallabagApiTestCase
13 public function testGetOneEntry()
15 $entry = $this->client
->getContainer()
16 ->get('doctrine.orm.entity_manager')
17 ->getRepository('WallabagCoreBundle:Entry')
18 ->findOneBy(['user' => 1, 'isArchived' => false]);
21 $this->markTestSkipped('No content found in db.');
24 $this->client
->request('GET', '/api/entries/'.$entry->getId().'.json');
25 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
27 $content = json_decode($this->client
->getResponse()->getContent(), true);
29 $this->assertEquals($entry->getTitle(), $content['title']);
30 $this->assertEquals($entry->getUrl(), $content['url']);
31 $this->assertCount(count($entry->getTags()), $content['tags']);
32 $this->assertEquals($entry->getUserName(), $content['user_name']);
33 $this->assertEquals($entry->getUserEmail(), $content['user_email']);
34 $this->assertEquals($entry->getUserId(), $content['user_id']);
36 $this->assertEquals('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
39 public function testExportEntry()
41 $entry = $this->client
->getContainer()
42 ->get('doctrine.orm.entity_manager')
43 ->getRepository('WallabagCoreBundle:Entry')
44 ->findOneBy(['user' => 1, 'isArchived' => false]);
47 $this->markTestSkipped('No content found in db.');
50 $this->client
->request('GET', '/api/entries/'.$entry->getId().'/export.epub');
51 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
53 // epub format got the content type in the content
54 $this->assertContains('application/epub', $this->client
->getResponse()->getContent());
55 $this->assertEquals('application/epub+zip', $this->client
->getResponse()->headers
->get('Content-Type'));
57 // re-auth client for mobi
58 $client = $this->createAuthorizedClient();
59 $client->request('GET', '/api/entries/'.$entry->getId().'/export.mobi');
60 $this->assertEquals(200, $client->getResponse()->getStatusCode());
62 $this->assertEquals('application/x-mobipocket-ebook', $client->getResponse()->headers
->get('Content-Type'));
64 // re-auth client for pdf
65 $client = $this->createAuthorizedClient();
66 $client->request('GET', '/api/entries/'.$entry->getId().'/export.pdf');
67 $this->assertEquals(200, $client->getResponse()->getStatusCode());
69 $this->assertContains('PDF-', $client->getResponse()->getContent());
70 $this->assertEquals('application/pdf', $client->getResponse()->headers
->get('Content-Type'));
72 // re-auth client for pdf
73 $client = $this->createAuthorizedClient();
74 $client->request('GET', '/api/entries/'.$entry->getId().'/export.txt');
75 $this->assertEquals(200, $client->getResponse()->getStatusCode());
77 $this->assertContains('text/plain', $client->getResponse()->headers
->get('Content-Type'));
79 // re-auth client for pdf
80 $client = $this->createAuthorizedClient();
81 $client->request('GET', '/api/entries/'.$entry->getId().'/export.csv');
82 $this->assertEquals(200, $client->getResponse()->getStatusCode());
84 $this->assertContains('application/csv', $client->getResponse()->headers
->get('Content-Type'));
87 public function testGetOneEntryWrongUser()
89 $entry = $this->client
->getContainer()
90 ->get('doctrine.orm.entity_manager')
91 ->getRepository('WallabagCoreBundle:Entry')
92 ->findOneBy(['user' => 2, 'isArchived' => false]);
95 $this->markTestSkipped('No content found in db.');
98 $this->client
->request('GET', '/api/entries/'.$entry->getId().'.json');
100 $this->assertEquals(403, $this->client
->getResponse()->getStatusCode());
103 public function testGetEntries()
105 $this->client
->request('GET', '/api/entries');
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']);
117 $this->assertEquals('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
120 public function testGetEntriesWithFullOptions()
122 $this->client
->request('GET', '/api/entries', [
130 'since' => 1443274283,
133 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
135 $content = json_decode($this->client
->getResponse()->getContent(), true);
137 $this->assertGreaterThanOrEqual(1, count($content));
138 $this->assertArrayHasKey('items', $content['_embedded']);
139 $this->assertGreaterThanOrEqual(0, $content['total']);
140 $this->assertEquals(1, $content['page']);
141 $this->assertEquals(2, $content['limit']);
142 $this->assertGreaterThanOrEqual(1, $content['pages']);
144 $this->assertArrayHasKey('_links', $content);
145 $this->assertArrayHasKey('self', $content['_links']);
146 $this->assertArrayHasKey('first', $content['_links']);
147 $this->assertArrayHasKey('last', $content['_links']);
149 foreach (['self', 'first', 'last'] as $link) {
150 $this->assertArrayHasKey('href', $content['_links'][$link]);
151 $this->assertContains('archive=1', $content['_links'][$link]['href']);
152 $this->assertContains('starred=1', $content['_links'][$link]['href']);
153 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
154 $this->assertContains('order=asc', $content['_links'][$link]['href']);
155 $this->assertContains('tags=foo', $content['_links'][$link]['href']);
156 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
159 $this->assertEquals('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
162 public function testGetEntriesOnPageTwo()
164 $this->client
->request('GET', '/api/entries', [
169 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
171 $content = json_decode($this->client
->getResponse()->getContent(), true);
173 $this->assertGreaterThanOrEqual(0, $content['total']);
174 $this->assertEquals(2, $content['page']);
175 $this->assertEquals(2, $content['limit']);
178 public function testGetStarredEntries()
180 $this->client
->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']);
182 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
184 $content = json_decode($this->client
->getResponse()->getContent(), true);
186 $this->assertGreaterThanOrEqual(1, count($content));
187 $this->assertNotEmpty($content['_embedded']['items']);
188 $this->assertGreaterThanOrEqual(1, $content['total']);
189 $this->assertEquals(1, $content['page']);
190 $this->assertGreaterThanOrEqual(1, $content['pages']);
192 $this->assertArrayHasKey('_links', $content);
193 $this->assertArrayHasKey('self', $content['_links']);
194 $this->assertArrayHasKey('first', $content['_links']);
195 $this->assertArrayHasKey('last', $content['_links']);
197 foreach (['self', 'first', 'last'] as $link) {
198 $this->assertArrayHasKey('href', $content['_links'][$link]);
199 $this->assertContains('starred=1', $content['_links'][$link]['href']);
200 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
203 $this->assertEquals('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
206 public function testGetArchiveEntries()
208 $this->client
->request('GET', '/api/entries', ['archive' => 1]);
210 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
212 $content = json_decode($this->client
->getResponse()->getContent(), true);
214 $this->assertGreaterThanOrEqual(1, count($content));
215 $this->assertNotEmpty($content['_embedded']['items']);
216 $this->assertGreaterThanOrEqual(1, $content['total']);
217 $this->assertEquals(1, $content['page']);
218 $this->assertGreaterThanOrEqual(1, $content['pages']);
220 $this->assertArrayHasKey('_links', $content);
221 $this->assertArrayHasKey('self', $content['_links']);
222 $this->assertArrayHasKey('first', $content['_links']);
223 $this->assertArrayHasKey('last', $content['_links']);
225 foreach (['self', 'first', 'last'] as $link) {
226 $this->assertArrayHasKey('href', $content['_links'][$link]);
227 $this->assertContains('archive=1', $content['_links'][$link]['href']);
230 $this->assertEquals('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
233 public function testGetTaggedEntries()
235 $this->client
->request('GET', '/api/entries', ['tags' => 'foo,bar']);
237 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
239 $content = json_decode($this->client
->getResponse()->getContent(), true);
241 $this->assertGreaterThanOrEqual(1, count($content));
242 $this->assertNotEmpty($content['_embedded']['items']);
243 $this->assertGreaterThanOrEqual(1, $content['total']);
244 $this->assertEquals(1, $content['page']);
245 $this->assertGreaterThanOrEqual(1, $content['pages']);
247 $this->assertArrayHasKey('_links', $content);
248 $this->assertArrayHasKey('self', $content['_links']);
249 $this->assertArrayHasKey('first', $content['_links']);
250 $this->assertArrayHasKey('last', $content['_links']);
252 foreach (['self', 'first', 'last'] as $link) {
253 $this->assertArrayHasKey('href', $content['_links'][$link]);
254 $this->assertContains('tags='.urlencode('foo,bar'), $content['_links'][$link]['href']);
257 $this->assertEquals('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
260 public function testGetDatedEntries()
262 $this->client
->request('GET', '/api/entries', ['since' => 1443274283]);
264 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
266 $content = json_decode($this->client
->getResponse()->getContent(), true);
268 $this->assertGreaterThanOrEqual(1, count($content));
269 $this->assertNotEmpty($content['_embedded']['items']);
270 $this->assertGreaterThanOrEqual(1, $content['total']);
271 $this->assertEquals(1, $content['page']);
272 $this->assertGreaterThanOrEqual(1, $content['pages']);
274 $this->assertArrayHasKey('_links', $content);
275 $this->assertArrayHasKey('self', $content['_links']);
276 $this->assertArrayHasKey('first', $content['_links']);
277 $this->assertArrayHasKey('last', $content['_links']);
279 foreach (['self', 'first', 'last'] as $link) {
280 $this->assertArrayHasKey('href', $content['_links'][$link]);
281 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
284 $this->assertEquals('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
287 public function testGetDatedSupEntries()
289 $future = new \
DateTime(date('Y-m-d H:i:s'));
290 $this->client
->request('GET', '/api/entries', ['since' => $future->getTimestamp() +
1000]);
292 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
294 $content = json_decode($this->client
->getResponse()->getContent(), true);
296 $this->assertGreaterThanOrEqual(1, count($content));
297 $this->assertEmpty($content['_embedded']['items']);
298 $this->assertEquals(0, $content['total']);
299 $this->assertEquals(1, $content['page']);
300 $this->assertEquals(1, $content['pages']);
302 $this->assertArrayHasKey('_links', $content);
303 $this->assertArrayHasKey('self', $content['_links']);
304 $this->assertArrayHasKey('first', $content['_links']);
305 $this->assertArrayHasKey('last', $content['_links']);
307 foreach (['self', 'first', 'last'] as $link) {
308 $this->assertArrayHasKey('href', $content['_links'][$link]);
309 $this->assertContains('since='.($future->getTimestamp() +
1000), $content['_links'][$link]['href']);
312 $this->assertEquals('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
315 public function testDeleteEntry()
317 $entry = $this->client
->getContainer()
318 ->get('doctrine.orm.entity_manager')
319 ->getRepository('WallabagCoreBundle:Entry')
320 ->findOneByUser(1, ['id' => 'asc']);
323 $this->markTestSkipped('No content found in db.');
326 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'.json');
328 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
330 $content = json_decode($this->client
->getResponse()->getContent(), true);
332 $this->assertEquals($entry->getTitle(), $content['title']);
333 $this->assertEquals($entry->getUrl(), $content['url']);
335 // We'll try to delete this entry again
336 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'.json');
338 $this->assertEquals(404, $this->client
->getResponse()->getStatusCode());
341 public function testPostEntry()
343 $this->client
->request('POST', '/api/entries.json', [
344 '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',
346 'title' => 'New title for my article',
347 'content' => 'my content',
348 'language' => 'de_DE',
349 'published_at' => '2016-09-08T11:55:58+0200',
350 'authors' => 'bob,helen',
353 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
355 $content = json_decode($this->client
->getResponse()->getContent(), true);
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(false, $content['is_archived']);
360 $this->assertEquals(false, $content['is_starred']);
361 $this->assertEquals('New title for my article', $content['title']);
362 $this->assertEquals(1, $content['user_id']);
363 $this->assertCount(2, $content['tags']);
364 $this->assertSame('my content', $content['content']);
365 $this->assertSame('de_DE', $content['language']);
366 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
367 $this->assertCount(2, $content['published_by']);
368 $this->assertContains('bob', $content['published_by']);
369 $this->assertContains('helen', $content['published_by']);
372 public function testPostSameEntry()
374 $this->client
->request('POST', '/api/entries.json', [
375 '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',
377 'tags' => 'google, apple',
380 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
382 $content = json_decode($this->client
->getResponse()->getContent(), true);
384 $this->assertGreaterThan(0, $content['id']);
385 $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']);
386 $this->assertEquals(true, $content['is_archived']);
387 $this->assertEquals(false, $content['is_starred']);
388 $this->assertCount(3, $content['tags']);
391 public function testPostEntryWhenFetchContentFails()
393 /** @var \Symfony\Component\DependencyInjection\Container $container */
394 $container = $this->client
->getContainer();
395 $contentProxy = $this->getMockBuilder(ContentProxy
::class)
396 ->disableOriginalConstructor()
397 ->setMethods(['updateEntry'])
399 $contentProxy->expects($this->any())
400 ->method('updateEntry')
401 ->willThrowException(new \
Exception('Test Fetch content fails'));
402 $container->set('wallabag_core.content_proxy', $contentProxy);
405 $this->client
->request('POST', '/api/entries.json', [
406 'url' => 'http://www.example.com/',
409 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
410 $content = json_decode($this->client
->getResponse()->getContent(), true);
411 $this->assertGreaterThan(0, $content['id']);
412 $this->assertEquals('http://www.example.com/', $content['url']);
414 // Remove the created entry to avoid side effects on other tests
415 if (isset($content['id'])) {
416 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
417 $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']);
424 public function testPostArchivedAndStarredEntry()
426 $this->client
->request('POST', '/api/entries.json', [
427 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
432 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
434 $content = json_decode($this->client
->getResponse()->getContent(), true);
436 $this->assertGreaterThan(0, $content['id']);
437 $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']);
438 $this->assertEquals(true, $content['is_archived']);
439 $this->assertEquals(true, $content['is_starred']);
440 $this->assertEquals(1, $content['user_id']);
443 public function testPostArchivedAndStarredEntryWithoutQuotes()
445 $this->client
->request('POST', '/api/entries.json', [
446 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
451 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
453 $content = json_decode($this->client
->getResponse()->getContent(), true);
455 $this->assertGreaterThan(0, $content['id']);
456 $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']);
457 $this->assertEquals(false, $content['is_archived']);
458 $this->assertEquals(true, $content['is_starred']);
461 public function testPatchEntry()
463 $entry = $this->client
->getContainer()
464 ->get('doctrine.orm.entity_manager')
465 ->getRepository('WallabagCoreBundle:Entry')
469 $this->markTestSkipped('No content found in db.');
472 // hydrate the tags relations
473 $nbTags = count($entry->getTags());
475 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
476 'title' => 'New awesome title',
477 'tags' => 'new tag '.uniqid(),
482 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
484 $content = json_decode($this->client
->getResponse()->getContent(), true);
486 $this->assertEquals($entry->getId(), $content['id']);
487 $this->assertEquals($entry->getUrl(), $content['url']);
488 $this->assertEquals('New awesome title', $content['title']);
489 $this->assertGreaterThan($nbTags, count($content['tags']));
490 $this->assertEquals(1, $content['user_id']);
493 public function testPatchEntryWithoutQuotes()
495 $entry = $this->client
->getContainer()
496 ->get('doctrine.orm.entity_manager')
497 ->getRepository('WallabagCoreBundle:Entry')
501 $this->markTestSkipped('No content found in db.');
504 // hydrate the tags relations
505 $nbTags = count($entry->getTags());
507 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
508 'title' => 'New awesome title',
509 'tags' => 'new tag '.uniqid(),
514 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
516 $content = json_decode($this->client
->getResponse()->getContent(), true);
518 $this->assertEquals($entry->getId(), $content['id']);
519 $this->assertEquals($entry->getUrl(), $content['url']);
520 $this->assertEquals('New awesome title', $content['title']);
521 $this->assertGreaterThan($nbTags, count($content['tags']));
524 public function testGetTagsEntry()
526 $entry = $this->client
->getContainer()
527 ->get('doctrine.orm.entity_manager')
528 ->getRepository('WallabagCoreBundle:Entry')
529 ->findOneWithTags($this->user
->getId());
534 $this->markTestSkipped('No content found in db.');
538 foreach ($entry->getTags() as $tag) {
539 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
542 $this->client
->request('GET', '/api/entries/'.$entry->getId().'/tags');
544 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT
), $this->client
->getResponse()->getContent());
547 public function testPostTagsOnEntry()
549 $entry = $this->client
->getContainer()
550 ->get('doctrine.orm.entity_manager')
551 ->getRepository('WallabagCoreBundle:Entry')
555 $this->markTestSkipped('No content found in db.');
558 $nbTags = count($entry->getTags());
560 $newTags = 'tag1,tag2,tag3';
562 $this->client
->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]);
564 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
566 $content = json_decode($this->client
->getResponse()->getContent(), true);
568 $this->assertArrayHasKey('tags', $content);
569 $this->assertEquals($nbTags +
3, count($content['tags']));
571 $entryDB = $this->client
->getContainer()
572 ->get('doctrine.orm.entity_manager')
573 ->getRepository('WallabagCoreBundle:Entry')
574 ->find($entry->getId());
577 foreach ($entryDB->getTags()->toArray() as $tag) {
578 $tagsInDB[$tag->getId()] = $tag->getLabel();
581 foreach (explode(',', $newTags) as $tag) {
582 $this->assertContains($tag, $tagsInDB);
586 public function testDeleteOneTagEntry()
588 $entry = $this->client
->getContainer()
589 ->get('doctrine.orm.entity_manager')
590 ->getRepository('WallabagCoreBundle:Entry')
591 ->findOneWithTags($this->user
->getId());
595 $this->markTestSkipped('No content found in db.');
598 // hydrate the tags relations
599 $nbTags = count($entry->getTags());
600 $tag = $entry->getTags()[0];
602 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json');
604 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
606 $content = json_decode($this->client
->getResponse()->getContent(), true);
608 $this->assertArrayHasKey('tags', $content);
609 $this->assertEquals($nbTags - 1, count($content['tags']));
612 public function testSaveIsArchivedAfterPost()
614 $entry = $this->client
->getContainer()
615 ->get('doctrine.orm.entity_manager')
616 ->getRepository('WallabagCoreBundle:Entry')
617 ->findOneBy(['user' => 1, 'isArchived' => true]);
620 $this->markTestSkipped('No content found in db.');
623 $this->client
->request('POST', '/api/entries.json', [
624 'url' => $entry->getUrl(),
627 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
629 $content = json_decode($this->client
->getResponse()->getContent(), true);
631 $this->assertEquals(true, $content['is_archived']);
634 public function testSaveIsStarredAfterPost()
636 $entry = $this->client
->getContainer()
637 ->get('doctrine.orm.entity_manager')
638 ->getRepository('WallabagCoreBundle:Entry')
639 ->findOneBy(['user' => 1, 'isStarred' => true]);
642 $this->markTestSkipped('No content found in db.');
645 $this->client
->request('POST', '/api/entries.json', [
646 'url' => $entry->getUrl(),
649 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
651 $content = json_decode($this->client
->getResponse()->getContent(), true);
653 $this->assertEquals(true, $content['is_starred']);
656 public function testSaveIsArchivedAfterPatch()
658 $entry = $this->client
->getContainer()
659 ->get('doctrine.orm.entity_manager')
660 ->getRepository('WallabagCoreBundle:Entry')
661 ->findOneBy(['user' => 1, 'isArchived' => true]);
664 $this->markTestSkipped('No content found in db.');
667 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
668 'title' => $entry->getTitle().'++',
671 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
673 $content = json_decode($this->client
->getResponse()->getContent(), true);
675 $this->assertEquals(true, $content['is_archived']);
678 public function testSaveIsStarredAfterPatch()
680 $entry = $this->client
->getContainer()
681 ->get('doctrine.orm.entity_manager')
682 ->getRepository('WallabagCoreBundle:Entry')
683 ->findOneBy(['user' => 1, 'isStarred' => true]);
686 $this->markTestSkipped('No content found in db.');
688 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
689 'title' => $entry->getTitle().'++',
692 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
694 $content = json_decode($this->client
->getResponse()->getContent(), true);
696 $this->assertEquals(true, $content['is_starred']);
699 public function testGetEntriesExists()
701 $this->client
->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2');
703 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
705 $content = json_decode($this->client
->getResponse()->getContent(), true);
707 $this->assertEquals(2, $content['exists']);
710 public function testGetEntriesExistsWithManyUrls()
712 $url1 = 'http://0.0.0.0/entry2';
713 $url2 = 'http://0.0.0.0/entry10';
714 $this->client
->request('GET', '/api/entries/exists?urls[]='.$url1.'&urls[]='.$url2);
716 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
718 $content = json_decode($this->client
->getResponse()->getContent(), true);
720 $this->assertArrayHasKey($url1, $content);
721 $this->assertArrayHasKey($url2, $content);
722 $this->assertEquals(2, $content[$url1]);
723 $this->assertEquals(false, $content[$url2]);
726 public function testGetEntriesExistsWhichDoesNotExists()
728 $this->client
->request('GET', '/api/entries/exists?url=http://google.com/entry2');
730 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
732 $content = json_decode($this->client
->getResponse()->getContent(), true);
734 $this->assertEquals(false, $content['exists']);
737 public function testGetEntriesExistsWithNoUrl()
739 $this->client
->request('GET', '/api/entries/exists?url=');
741 $this->assertEquals(403, $this->client
->getResponse()->getStatusCode());
744 public function testReloadEntryErrorWhileFetching()
746 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
747 ->getRepository('WallabagCoreBundle:Entry')
748 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
751 $this->markTestSkipped('No content found in db.');
754 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'/reload.json');
755 $this->assertEquals(304, $this->client
->getResponse()->getStatusCode());
758 public function testReloadEntry()
760 $this->client
->request('POST', '/api/entries.json', [
761 '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',
763 'tags' => 'google, apple',
766 $json = json_decode($this->client
->getResponse()->getContent(), true);
770 $this->client
->request('PATCH', '/api/entries/'.$json['id'].'/reload.json');
771 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
773 $content = json_decode($this->client
->getResponse()->getContent(), true);
775 $this->assertNotEmpty($content['title']);
777 $this->assertEquals('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
780 public function testPostEntriesTagsListAction()
782 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
783 ->getRepository('WallabagCoreBundle:Entry')
784 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
786 $tags = $entry->getTags();
788 $this->assertCount(2, $tags);
792 'url' => 'http://0.0.0.0/entry4',
793 'tags' => 'new tag 1, new tag 2',
797 $this->client
->request('POST', '/api/entries/tags/lists?list='.json_encode($list));
799 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
801 $content = json_decode($this->client
->getResponse()->getContent(), true);
803 $this->assertInternalType('int', $content[0]['entry']);
804 $this->assertEquals('http://0.0.0.0/entry4', $content[0]['url']);
806 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
807 ->getRepository('WallabagCoreBundle:Entry')
808 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
810 $tags = $entry->getTags();
811 $this->assertCount(4, $tags);
814 public function testDeleteEntriesTagsListAction()
816 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
817 $entry = new Entry($em->getReference(User
::class, 1));
818 $entry->setUrl('http://0.0.0.0/test-entry');
819 $entry->addTag((new Tag())->setLabel('foo-tag'));
820 $entry->addTag((new Tag())->setLabel('bar-tag'));
821 $em->persist($entry);
828 'url' => 'http://0.0.0.0/test-entry',
829 'tags' => 'foo-tag, bar-tag',
833 $this->client
->request('DELETE', '/api/entries/tags/list?list='.json_encode($list));
834 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
836 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
837 $this->assertCount(0, $entry->getTags());
840 public function testPostEntriesListAction()
843 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
844 'http://0.0.0.0/entry2',
847 $this->client
->request('POST', '/api/entries/lists?urls='.json_encode($list));
849 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
851 $content = json_decode($this->client
->getResponse()->getContent(), true);
853 $this->assertInternalType('int', $content[0]['entry']);
854 $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']);
856 $this->assertInternalType('int', $content[1]['entry']);
857 $this->assertEquals('http://0.0.0.0/entry2', $content[1]['url']);
860 public function testDeleteEntriesListAction()
862 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
863 $em->persist((new Entry($em->getReference(User
::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
868 'http://0.0.0.0/test-entry1',
869 'http://0.0.0.0/test-entry-not-exist',
872 $this->client
->request('DELETE', '/api/entries/list?urls='.json_encode($list));
874 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
876 $content = json_decode($this->client
->getResponse()->getContent(), true);
878 $this->assertTrue($content[0]['entry']);
879 $this->assertEquals('http://0.0.0.0/test-entry1', $content[0]['url']);
881 $this->assertFalse($content[1]['entry']);
882 $this->assertEquals('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
885 public function testLimitBulkAction()
888 'http://0.0.0.0/entry1',
889 'http://0.0.0.0/entry1',
890 'http://0.0.0.0/entry1',
891 'http://0.0.0.0/entry1',
892 'http://0.0.0.0/entry1',
893 'http://0.0.0.0/entry1',
894 'http://0.0.0.0/entry1',
895 'http://0.0.0.0/entry1',
896 'http://0.0.0.0/entry1',
897 'http://0.0.0.0/entry1',
898 'http://0.0.0.0/entry1',
901 $this->client
->request('POST', '/api/entries/lists?urls='.json_encode($list));
903 $this->assertEquals(400, $this->client
->getResponse()->getStatusCode());
904 $this->assertContains('API limit reached', $this->client
->getResponse()->getContent());