aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php')
-rw-r--r--tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php212
1 files changed, 207 insertions, 5 deletions
diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
index 0a65f9ce..067aed2c 100644
--- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
+++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
@@ -3,8 +3,10 @@
3namespace Tests\Wallabag\ApiBundle\Controller; 3namespace Tests\Wallabag\ApiBundle\Controller;
4 4
5use Tests\Wallabag\ApiBundle\WallabagApiTestCase; 5use Tests\Wallabag\ApiBundle\WallabagApiTestCase;
6use Wallabag\CoreBundle\Entity\Entry;
6use Wallabag\CoreBundle\Entity\Tag; 7use Wallabag\CoreBundle\Entity\Tag;
7use Wallabag\CoreBundle\Helper\ContentProxy; 8use Wallabag\CoreBundle\Helper\ContentProxy;
9use Wallabag\UserBundle\Entity\User;
8 10
9class EntryRestControllerTest extends WallabagApiTestCase 11class EntryRestControllerTest extends WallabagApiTestCase
10{ 12{
@@ -126,6 +128,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
126 'perPage' => 2, 128 'perPage' => 2,
127 'tags' => 'foo', 129 'tags' => 'foo',
128 'since' => 1443274283, 130 'since' => 1443274283,
131 'public' => 0,
129 ]); 132 ]);
130 133
131 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 134 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@@ -152,6 +155,53 @@ class EntryRestControllerTest extends WallabagApiTestCase
152 $this->assertContains('order=asc', $content['_links'][$link]['href']); 155 $this->assertContains('order=asc', $content['_links'][$link]['href']);
153 $this->assertContains('tags=foo', $content['_links'][$link]['href']); 156 $this->assertContains('tags=foo', $content['_links'][$link]['href']);
154 $this->assertContains('since=1443274283', $content['_links'][$link]['href']); 157 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
158 $this->assertContains('public=0', $content['_links'][$link]['href']);
159 }
160
161 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
162 }
163
164 public function testGetEntriesPublicOnly()
165 {
166 $entry = $this->client->getContainer()
167 ->get('doctrine.orm.entity_manager')
168 ->getRepository('WallabagCoreBundle:Entry')
169 ->findOneByUser(1);
170
171 if (!$entry) {
172 $this->markTestSkipped('No content found in db.');
173 }
174
175 // generate at least one public entry
176 $entry->generateUid();
177
178 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
179 $em->persist($entry);
180 $em->flush();
181
182 $this->client->request('GET', '/api/entries', [
183 'public' => 1,
184 ]);
185
186 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
187
188 $content = json_decode($this->client->getResponse()->getContent(), true);
189
190 $this->assertGreaterThanOrEqual(1, count($content));
191 $this->assertArrayHasKey('items', $content['_embedded']);
192 $this->assertGreaterThanOrEqual(1, $content['total']);
193 $this->assertEquals(1, $content['page']);
194 $this->assertEquals(30, $content['limit']);
195 $this->assertGreaterThanOrEqual(1, $content['pages']);
196
197 $this->assertArrayHasKey('_links', $content);
198 $this->assertArrayHasKey('self', $content['_links']);
199 $this->assertArrayHasKey('first', $content['_links']);
200 $this->assertArrayHasKey('last', $content['_links']);
201
202 foreach (['self', 'first', 'last'] as $link) {
203 $this->assertArrayHasKey('href', $content['_links'][$link]);
204 $this->assertContains('public=1', $content['_links'][$link]['href']);
155 } 205 }
156 206
157 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); 207 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
@@ -315,7 +365,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
315 $entry = $this->client->getContainer() 365 $entry = $this->client->getContainer()
316 ->get('doctrine.orm.entity_manager') 366 ->get('doctrine.orm.entity_manager')
317 ->getRepository('WallabagCoreBundle:Entry') 367 ->getRepository('WallabagCoreBundle:Entry')
318 ->findOneByUser(1); 368 ->findOneByUser(1, ['id' => 'asc']);
319 369
320 if (!$entry) { 370 if (!$entry) {
321 $this->markTestSkipped('No content found in db.'); 371 $this->markTestSkipped('No content found in db.');
@@ -342,6 +392,11 @@ class EntryRestControllerTest extends WallabagApiTestCase
342 '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', 392 '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',
343 'tags' => 'google', 393 'tags' => 'google',
344 'title' => 'New title for my article', 394 'title' => 'New title for my article',
395 'content' => 'my content',
396 'language' => 'de',
397 'published_at' => '2016-09-08T11:55:58+0200',
398 'authors' => 'bob,helen',
399 'public' => 1,
345 ]); 400 ]);
346 401
347 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 402 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@@ -354,7 +409,14 @@ class EntryRestControllerTest extends WallabagApiTestCase
354 $this->assertEquals(false, $content['is_starred']); 409 $this->assertEquals(false, $content['is_starred']);
355 $this->assertEquals('New title for my article', $content['title']); 410 $this->assertEquals('New title for my article', $content['title']);
356 $this->assertEquals(1, $content['user_id']); 411 $this->assertEquals(1, $content['user_id']);
357 $this->assertCount(1, $content['tags']); 412 $this->assertCount(2, $content['tags']);
413 $this->assertSame('my content', $content['content']);
414 $this->assertSame('de', $content['language']);
415 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
416 $this->assertCount(2, $content['published_by']);
417 $this->assertContains('bob', $content['published_by']);
418 $this->assertContains('helen', $content['published_by']);
419 $this->assertTrue($content['is_public'], 'A public link has been generated for that entry');
358 } 420 }
359 421
360 public function testPostSameEntry() 422 public function testPostSameEntry()
@@ -373,7 +435,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
373 $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']); 435 $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']);
374 $this->assertEquals(true, $content['is_archived']); 436 $this->assertEquals(true, $content['is_archived']);
375 $this->assertEquals(false, $content['is_starred']); 437 $this->assertEquals(false, $content['is_starred']);
376 $this->assertCount(2, $content['tags']); 438 $this->assertCount(3, $content['tags']);
377 } 439 }
378 440
379 public function testPostEntryWhenFetchContentFails() 441 public function testPostEntryWhenFetchContentFails()
@@ -465,6 +527,11 @@ class EntryRestControllerTest extends WallabagApiTestCase
465 'tags' => 'new tag '.uniqid(), 527 'tags' => 'new tag '.uniqid(),
466 'starred' => '1', 528 'starred' => '1',
467 'archive' => '0', 529 'archive' => '0',
530 'language' => 'de_AT',
531 'preview_picture' => 'http://preview.io/picture.jpg',
532 'authors' => 'bob,sponge',
533 'content' => 'awesome',
534 'public' => 0,
468 ]); 535 ]);
469 536
470 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 537 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@@ -476,6 +543,12 @@ class EntryRestControllerTest extends WallabagApiTestCase
476 $this->assertEquals('New awesome title', $content['title']); 543 $this->assertEquals('New awesome title', $content['title']);
477 $this->assertGreaterThan($nbTags, count($content['tags'])); 544 $this->assertGreaterThan($nbTags, count($content['tags']));
478 $this->assertEquals(1, $content['user_id']); 545 $this->assertEquals(1, $content['user_id']);
546 $this->assertEquals('de_AT', $content['language']);
547 $this->assertEquals('http://preview.io/picture.jpg', $content['preview_picture']);
548 $this->assertContains('sponge', $content['published_by']);
549 $this->assertContains('bob', $content['published_by']);
550 $this->assertEquals('awesome', $content['content']);
551 $this->assertFalse($content['is_public'], 'Entry is no more shared');
479 } 552 }
480 553
481 public function testPatchEntryWithoutQuotes() 554 public function testPatchEntryWithoutQuotes()
@@ -497,6 +570,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
497 'tags' => 'new tag '.uniqid(), 570 'tags' => 'new tag '.uniqid(),
498 'starred' => 1, 571 'starred' => 1,
499 'archive' => 0, 572 'archive' => 0,
573 'authors' => ['bob', 'sponge'],
500 ]); 574 ]);
501 575
502 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 576 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@@ -507,6 +581,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
507 $this->assertEquals($entry->getUrl(), $content['url']); 581 $this->assertEquals($entry->getUrl(), $content['url']);
508 $this->assertEquals('New awesome title', $content['title']); 582 $this->assertEquals('New awesome title', $content['title']);
509 $this->assertGreaterThan($nbTags, count($content['tags'])); 583 $this->assertGreaterThan($nbTags, count($content['tags']));
584 $this->assertTrue(empty($content['published_by']), 'Authors were not saved because of an array instead of a string');
510 } 585 }
511 586
512 public function testGetTagsEntry() 587 public function testGetTagsEntry()
@@ -692,7 +767,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
692 767
693 $content = json_decode($this->client->getResponse()->getContent(), true); 768 $content = json_decode($this->client->getResponse()->getContent(), true);
694 769
695 $this->assertEquals(true, $content['exists']); 770 $this->assertEquals(2, $content['exists']);
696 } 771 }
697 772
698 public function testGetEntriesExistsWithManyUrls() 773 public function testGetEntriesExistsWithManyUrls()
@@ -707,7 +782,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
707 782
708 $this->assertArrayHasKey($url1, $content); 783 $this->assertArrayHasKey($url1, $content);
709 $this->assertArrayHasKey($url2, $content); 784 $this->assertArrayHasKey($url2, $content);
710 $this->assertEquals(true, $content[$url1]); 785 $this->assertEquals(2, $content[$url1]);
711 $this->assertEquals(false, $content[$url2]); 786 $this->assertEquals(false, $content[$url2]);
712 } 787 }
713 788
@@ -764,4 +839,131 @@ class EntryRestControllerTest extends WallabagApiTestCase
764 839
765 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); 840 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
766 } 841 }
842
843 public function testPostEntriesTagsListAction()
844 {
845 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
846 ->getRepository('WallabagCoreBundle:Entry')
847 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
848
849 $tags = $entry->getTags();
850
851 $this->assertCount(2, $tags);
852
853 $list = [
854 [
855 'url' => 'http://0.0.0.0/entry4',
856 'tags' => 'new tag 1, new tag 2',
857 ],
858 ];
859
860 $this->client->request('POST', '/api/entries/tags/lists?list='.json_encode($list));
861
862 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
863
864 $content = json_decode($this->client->getResponse()->getContent(), true);
865
866 $this->assertInternalType('int', $content[0]['entry']);
867 $this->assertEquals('http://0.0.0.0/entry4', $content[0]['url']);
868
869 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
870 ->getRepository('WallabagCoreBundle:Entry')
871 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
872
873 $tags = $entry->getTags();
874 $this->assertCount(4, $tags);
875 }
876
877 public function testDeleteEntriesTagsListAction()
878 {
879 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
880 $entry = new Entry($em->getReference(User::class, 1));
881 $entry->setUrl('http://0.0.0.0/test-entry');
882 $entry->addTag((new Tag())->setLabel('foo-tag'));
883 $entry->addTag((new Tag())->setLabel('bar-tag'));
884 $em->persist($entry);
885 $em->flush();
886
887 $em->clear();
888
889 $list = [
890 [
891 'url' => 'http://0.0.0.0/test-entry',
892 'tags' => 'foo-tag, bar-tag',
893 ],
894 ];
895
896 $this->client->request('DELETE', '/api/entries/tags/list?list='.json_encode($list));
897 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
898
899 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
900 $this->assertCount(0, $entry->getTags());
901 }
902
903 public function testPostEntriesListAction()
904 {
905 $list = [
906 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
907 'http://0.0.0.0/entry2',
908 ];
909
910 $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list));
911
912 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
913
914 $content = json_decode($this->client->getResponse()->getContent(), true);
915
916 $this->assertInternalType('int', $content[0]['entry']);
917 $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']);
918
919 $this->assertInternalType('int', $content[1]['entry']);
920 $this->assertEquals('http://0.0.0.0/entry2', $content[1]['url']);
921 }
922
923 public function testDeleteEntriesListAction()
924 {
925 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
926 $em->persist((new Entry($em->getReference(User::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
927
928 $em->flush();
929 $em->clear();
930 $list = [
931 'http://0.0.0.0/test-entry1',
932 'http://0.0.0.0/test-entry-not-exist',
933 ];
934
935 $this->client->request('DELETE', '/api/entries/list?urls='.json_encode($list));
936
937 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
938
939 $content = json_decode($this->client->getResponse()->getContent(), true);
940
941 $this->assertTrue($content[0]['entry']);
942 $this->assertEquals('http://0.0.0.0/test-entry1', $content[0]['url']);
943
944 $this->assertFalse($content[1]['entry']);
945 $this->assertEquals('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
946 }
947
948 public function testLimitBulkAction()
949 {
950 $list = [
951 'http://0.0.0.0/entry1',
952 'http://0.0.0.0/entry1',
953 'http://0.0.0.0/entry1',
954 'http://0.0.0.0/entry1',
955 'http://0.0.0.0/entry1',
956 'http://0.0.0.0/entry1',
957 'http://0.0.0.0/entry1',
958 'http://0.0.0.0/entry1',
959 'http://0.0.0.0/entry1',
960 'http://0.0.0.0/entry1',
961 'http://0.0.0.0/entry1',
962 ];
963
964 $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list));
965
966 $this->assertEquals(400, $this->client->getResponse()->getStatusCode());
967 $this->assertContains('API limit reached', $this->client->getResponse()->getContent());
968 }
767} 969}