aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php
diff options
context:
space:
mode:
authorPaulino Michelazzo <paulino@michelazzo.com.br>2016-10-18 22:48:23 +0200
committerPaulino Michelazzo <paulino@michelazzo.com.br>2016-10-18 22:48:23 +0200
commit99731f0bb1f6fd2815eeb9af504ce86df927657b (patch)
treeb080efc608d2bbd52b77a4a0067402007f50c5a8 /tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php
parent3a3c6b866b52721431bed22426d9abfcd0d2dfe0 (diff)
parent7180aaed45dce62e40620a9e4b202526ebd6a3bb (diff)
downloadwallabag-99731f0bb1f6fd2815eeb9af504ce86df927657b.tar.gz
wallabag-99731f0bb1f6fd2815eeb9af504ce86df927657b.tar.zst
wallabag-99731f0bb1f6fd2815eeb9af504ce86df927657b.zip
Merge remote-tracking branch 'wallabag/master'
Diffstat (limited to 'tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php')
-rw-r--r--tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php334
1 files changed, 330 insertions, 4 deletions
diff --git a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php
index c39cc357..5dcb3e00 100644
--- a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php
+++ b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php
@@ -3,6 +3,7 @@
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\Tag;
6 7
7class WallabagRestControllerTest extends WallabagApiTestCase 8class WallabagRestControllerTest extends WallabagApiTestCase
8{ 9{
@@ -77,9 +78,56 @@ class WallabagRestControllerTest extends WallabagApiTestCase
77 ); 78 );
78 } 79 }
79 80
81 public function testGetEntriesWithFullOptions()
82 {
83 $this->client->request('GET', '/api/entries', [
84 'archive' => 1,
85 'starred' => 1,
86 'sort' => 'updated',
87 'order' => 'asc',
88 'page' => 1,
89 'perPage' => 2,
90 'tags' => 'foo',
91 'since' => 1443274283,
92 ]);
93
94 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
95
96 $content = json_decode($this->client->getResponse()->getContent(), true);
97
98 $this->assertGreaterThanOrEqual(1, count($content));
99 $this->assertArrayHasKey('items', $content['_embedded']);
100 $this->assertGreaterThanOrEqual(0, $content['total']);
101 $this->assertEquals(1, $content['page']);
102 $this->assertEquals(2, $content['limit']);
103 $this->assertGreaterThanOrEqual(1, $content['pages']);
104
105 $this->assertArrayHasKey('_links', $content);
106 $this->assertArrayHasKey('self', $content['_links']);
107 $this->assertArrayHasKey('first', $content['_links']);
108 $this->assertArrayHasKey('last', $content['_links']);
109
110 foreach (['self', 'first', 'last'] as $link) {
111 $this->assertArrayHasKey('href', $content['_links'][$link]);
112 $this->assertContains('archive=1', $content['_links'][$link]['href']);
113 $this->assertContains('starred=1', $content['_links'][$link]['href']);
114 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
115 $this->assertContains('order=asc', $content['_links'][$link]['href']);
116 $this->assertContains('tags=foo', $content['_links'][$link]['href']);
117 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
118 }
119
120 $this->assertTrue(
121 $this->client->getResponse()->headers->contains(
122 'Content-Type',
123 'application/json'
124 )
125 );
126 }
127
80 public function testGetStarredEntries() 128 public function testGetStarredEntries()
81 { 129 {
82 $this->client->request('GET', '/api/entries', ['star' => 1, 'sort' => 'updated']); 130 $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']);
83 131
84 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 132 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
85 133
@@ -91,6 +139,17 @@ class WallabagRestControllerTest extends WallabagApiTestCase
91 $this->assertEquals(1, $content['page']); 139 $this->assertEquals(1, $content['page']);
92 $this->assertGreaterThanOrEqual(1, $content['pages']); 140 $this->assertGreaterThanOrEqual(1, $content['pages']);
93 141
142 $this->assertArrayHasKey('_links', $content);
143 $this->assertArrayHasKey('self', $content['_links']);
144 $this->assertArrayHasKey('first', $content['_links']);
145 $this->assertArrayHasKey('last', $content['_links']);
146
147 foreach (['self', 'first', 'last'] as $link) {
148 $this->assertArrayHasKey('href', $content['_links'][$link]);
149 $this->assertContains('starred=1', $content['_links'][$link]['href']);
150 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
151 }
152
94 $this->assertTrue( 153 $this->assertTrue(
95 $this->client->getResponse()->headers->contains( 154 $this->client->getResponse()->headers->contains(
96 'Content-Type', 155 'Content-Type',
@@ -113,6 +172,113 @@ class WallabagRestControllerTest extends WallabagApiTestCase
113 $this->assertEquals(1, $content['page']); 172 $this->assertEquals(1, $content['page']);
114 $this->assertGreaterThanOrEqual(1, $content['pages']); 173 $this->assertGreaterThanOrEqual(1, $content['pages']);
115 174
175 $this->assertArrayHasKey('_links', $content);
176 $this->assertArrayHasKey('self', $content['_links']);
177 $this->assertArrayHasKey('first', $content['_links']);
178 $this->assertArrayHasKey('last', $content['_links']);
179
180 foreach (['self', 'first', 'last'] as $link) {
181 $this->assertArrayHasKey('href', $content['_links'][$link]);
182 $this->assertContains('archive=1', $content['_links'][$link]['href']);
183 }
184
185 $this->assertTrue(
186 $this->client->getResponse()->headers->contains(
187 'Content-Type',
188 'application/json'
189 )
190 );
191 }
192
193 public function testGetTaggedEntries()
194 {
195 $this->client->request('GET', '/api/entries', ['tags' => 'foo,bar']);
196
197 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
198
199 $content = json_decode($this->client->getResponse()->getContent(), true);
200
201 $this->assertGreaterThanOrEqual(1, count($content));
202 $this->assertNotEmpty($content['_embedded']['items']);
203 $this->assertGreaterThanOrEqual(1, $content['total']);
204 $this->assertEquals(1, $content['page']);
205 $this->assertGreaterThanOrEqual(1, $content['pages']);
206
207 $this->assertArrayHasKey('_links', $content);
208 $this->assertArrayHasKey('self', $content['_links']);
209 $this->assertArrayHasKey('first', $content['_links']);
210 $this->assertArrayHasKey('last', $content['_links']);
211
212 foreach (['self', 'first', 'last'] as $link) {
213 $this->assertArrayHasKey('href', $content['_links'][$link]);
214 $this->assertContains('tags='.urlencode('foo,bar'), $content['_links'][$link]['href']);
215 }
216
217 $this->assertTrue(
218 $this->client->getResponse()->headers->contains(
219 'Content-Type',
220 'application/json'
221 )
222 );
223 }
224
225 public function testGetDatedEntries()
226 {
227 $this->client->request('GET', '/api/entries', ['since' => 1443274283]);
228
229 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
230
231 $content = json_decode($this->client->getResponse()->getContent(), true);
232
233 $this->assertGreaterThanOrEqual(1, count($content));
234 $this->assertNotEmpty($content['_embedded']['items']);
235 $this->assertGreaterThanOrEqual(1, $content['total']);
236 $this->assertEquals(1, $content['page']);
237 $this->assertGreaterThanOrEqual(1, $content['pages']);
238
239 $this->assertArrayHasKey('_links', $content);
240 $this->assertArrayHasKey('self', $content['_links']);
241 $this->assertArrayHasKey('first', $content['_links']);
242 $this->assertArrayHasKey('last', $content['_links']);
243
244 foreach (['self', 'first', 'last'] as $link) {
245 $this->assertArrayHasKey('href', $content['_links'][$link]);
246 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
247 }
248
249 $this->assertTrue(
250 $this->client->getResponse()->headers->contains(
251 'Content-Type',
252 'application/json'
253 )
254 );
255 }
256
257 public function testGetDatedSupEntries()
258 {
259 $future = new \DateTime(date('Y-m-d H:i:s'));
260 $this->client->request('GET', '/api/entries', ['since' => $future->getTimestamp() + 1000]);
261
262 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
263
264 $content = json_decode($this->client->getResponse()->getContent(), true);
265
266 $this->assertGreaterThanOrEqual(1, count($content));
267 $this->assertEmpty($content['_embedded']['items']);
268 $this->assertEquals(0, $content['total']);
269 $this->assertEquals(1, $content['page']);
270 $this->assertEquals(1, $content['pages']);
271
272 $this->assertArrayHasKey('_links', $content);
273 $this->assertArrayHasKey('self', $content['_links']);
274 $this->assertArrayHasKey('first', $content['_links']);
275 $this->assertArrayHasKey('last', $content['_links']);
276
277 foreach (['self', 'first', 'last'] as $link) {
278 $this->assertArrayHasKey('href', $content['_links'][$link]);
279 $this->assertContains('since='.($future->getTimestamp() + 1000), $content['_links'][$link]['href']);
280 }
281
116 $this->assertTrue( 282 $this->assertTrue(
117 $this->client->getResponse()->headers->contains( 283 $this->client->getResponse()->headers->contains(
118 'Content-Type', 284 'Content-Type',
@@ -292,7 +458,7 @@ class WallabagRestControllerTest extends WallabagApiTestCase
292 $entry = $this->client->getContainer() 458 $entry = $this->client->getContainer()
293 ->get('doctrine.orm.entity_manager') 459 ->get('doctrine.orm.entity_manager')
294 ->getRepository('WallabagCoreBundle:Entry') 460 ->getRepository('WallabagCoreBundle:Entry')
295 ->findOneWithTags(1); 461 ->findOneWithTags($this->user->getId());
296 462
297 $entry = $entry[0]; 463 $entry = $entry[0];
298 464
@@ -354,7 +520,7 @@ class WallabagRestControllerTest extends WallabagApiTestCase
354 $entry = $this->client->getContainer() 520 $entry = $this->client->getContainer()
355 ->get('doctrine.orm.entity_manager') 521 ->get('doctrine.orm.entity_manager')
356 ->getRepository('WallabagCoreBundle:Entry') 522 ->getRepository('WallabagCoreBundle:Entry')
357 ->findOneWithTags(1); 523 ->findOneWithTags($this->user->getId());
358 $entry = $entry[0]; 524 $entry = $entry[0];
359 525
360 if (!$entry) { 526 if (!$entry) {
@@ -395,6 +561,8 @@ class WallabagRestControllerTest extends WallabagApiTestCase
395 */ 561 */
396 public function testDeleteUserTag($tag) 562 public function testDeleteUserTag($tag)
397 { 563 {
564 $tagName = $tag['label'];
565
398 $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json'); 566 $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json');
399 567
400 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 568 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@@ -405,12 +573,125 @@ class WallabagRestControllerTest extends WallabagApiTestCase
405 $this->assertEquals($tag['label'], $content['label']); 573 $this->assertEquals($tag['label'], $content['label']);
406 $this->assertEquals($tag['slug'], $content['slug']); 574 $this->assertEquals($tag['slug'], $content['slug']);
407 575
408 $entries = $entry = $this->client->getContainer() 576 $entries = $this->client->getContainer()
409 ->get('doctrine.orm.entity_manager') 577 ->get('doctrine.orm.entity_manager')
410 ->getRepository('WallabagCoreBundle:Entry') 578 ->getRepository('WallabagCoreBundle:Entry')
411 ->findAllByTagId($this->user->getId(), $tag['id']); 579 ->findAllByTagId($this->user->getId(), $tag['id']);
412 580
413 $this->assertCount(0, $entries); 581 $this->assertCount(0, $entries);
582
583 $tag = $this->client->getContainer()
584 ->get('doctrine.orm.entity_manager')
585 ->getRepository('WallabagCoreBundle:Tag')
586 ->findOneByLabel($tagName);
587
588 $this->assertNull($tag, $tagName.' was removed because it begun an orphan tag');
589 }
590
591 public function testDeleteTagByLabel()
592 {
593 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
594 $entry = $this->client->getContainer()
595 ->get('doctrine.orm.entity_manager')
596 ->getRepository('WallabagCoreBundle:Entry')
597 ->findOneWithTags($this->user->getId());
598
599 $entry = $entry[0];
600
601 $tag = new Tag();
602 $tag->setLabel('Awesome tag for test');
603 $em->persist($tag);
604
605 $entry->addTag($tag);
606
607 $em->persist($entry);
608 $em->flush();
609
610 $this->client->request('DELETE', '/api/tag/label.json', ['tag' => $tag->getLabel()]);
611
612 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
613
614 $content = json_decode($this->client->getResponse()->getContent(), true);
615
616 $this->assertArrayHasKey('label', $content);
617 $this->assertEquals($tag->getLabel(), $content['label']);
618 $this->assertEquals($tag->getSlug(), $content['slug']);
619
620 $entries = $this->client->getContainer()
621 ->get('doctrine.orm.entity_manager')
622 ->getRepository('WallabagCoreBundle:Entry')
623 ->findAllByTagId($this->user->getId(), $tag->getId());
624
625 $this->assertCount(0, $entries);
626 }
627
628 public function testDeleteTagByLabelNotFound()
629 {
630 $this->client->request('DELETE', '/api/tag/label.json', ['tag' => 'does not exist']);
631
632 $this->assertEquals(404, $this->client->getResponse()->getStatusCode());
633 }
634
635 public function testDeleteTagsByLabel()
636 {
637 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
638 $entry = $this->client->getContainer()
639 ->get('doctrine.orm.entity_manager')
640 ->getRepository('WallabagCoreBundle:Entry')
641 ->findOneWithTags($this->user->getId());
642
643 $entry = $entry[0];
644
645 $tag = new Tag();
646 $tag->setLabel('Awesome tag for tagsLabel');
647 $em->persist($tag);
648
649 $tag2 = new Tag();
650 $tag2->setLabel('Awesome tag for tagsLabel 2');
651 $em->persist($tag2);
652
653 $entry->addTag($tag);
654 $entry->addTag($tag2);
655
656 $em->persist($entry);
657 $em->flush();
658
659 $this->client->request('DELETE', '/api/tags/label.json', ['tags' => $tag->getLabel().','.$tag2->getLabel()]);
660
661 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
662
663 $content = json_decode($this->client->getResponse()->getContent(), true);
664
665 $this->assertCount(2, $content);
666
667 $this->assertArrayHasKey('label', $content[0]);
668 $this->assertEquals($tag->getLabel(), $content[0]['label']);
669 $this->assertEquals($tag->getSlug(), $content[0]['slug']);
670
671 $this->assertArrayHasKey('label', $content[1]);
672 $this->assertEquals($tag2->getLabel(), $content[1]['label']);
673 $this->assertEquals($tag2->getSlug(), $content[1]['slug']);
674
675 $entries = $this->client->getContainer()
676 ->get('doctrine.orm.entity_manager')
677 ->getRepository('WallabagCoreBundle:Entry')
678 ->findAllByTagId($this->user->getId(), $tag->getId());
679
680 $this->assertCount(0, $entries);
681
682 $entries = $this->client->getContainer()
683 ->get('doctrine.orm.entity_manager')
684 ->getRepository('WallabagCoreBundle:Entry')
685 ->findAllByTagId($this->user->getId(), $tag2->getId());
686
687 $this->assertCount(0, $entries);
688 }
689
690 public function testDeleteTagsByLabelNotFound()
691 {
692 $this->client->request('DELETE', '/api/tags/label.json', ['tags' => 'does not exist']);
693
694 $this->assertEquals(404, $this->client->getResponse()->getStatusCode());
414 } 695 }
415 696
416 public function testGetVersion() 697 public function testGetVersion()
@@ -510,4 +791,49 @@ class WallabagRestControllerTest extends WallabagApiTestCase
510 791
511 $this->assertEquals(true, $content['is_starred']); 792 $this->assertEquals(true, $content['is_starred']);
512 } 793 }
794
795 public function testGetEntriesExists()
796 {
797 $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2');
798
799 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
800
801 $content = json_decode($this->client->getResponse()->getContent(), true);
802
803 $this->assertEquals(true, $content['exists']);
804 }
805
806 public function testGetEntriesExistsWithManyUrls()
807 {
808 $url1 = 'http://0.0.0.0/entry2';
809 $url2 = 'http://0.0.0.0/entry10';
810 $this->client->request('GET', '/api/entries/exists?urls[]='.$url1.'&urls[]='.$url2);
811
812 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
813
814 $content = json_decode($this->client->getResponse()->getContent(), true);
815
816 $this->assertArrayHasKey($url1, $content);
817 $this->assertArrayHasKey($url2, $content);
818 $this->assertEquals(true, $content[$url1]);
819 $this->assertEquals(false, $content[$url2]);
820 }
821
822 public function testGetEntriesExistsWhichDoesNotExists()
823 {
824 $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2');
825
826 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
827
828 $content = json_decode($this->client->getResponse()->getContent(), true);
829
830 $this->assertEquals(false, $content['exists']);
831 }
832
833 public function testGetEntriesExistsWithNoUrl()
834 {
835 $this->client->request('GET', '/api/entries/exists?url=');
836
837 $this->assertEquals(403, $this->client->getResponse()->getStatusCode());
838 }
513} 839}