3 namespace Tests\Wallabag\ApiBundle\Controller
;
5 use Tests\Wallabag\ApiBundle\WallabagApiTestCase
;
6 use Wallabag\CoreBundle\Entity\Tag
;
8 class TagRestControllerTest
extends WallabagApiTestCase
10 private $otherUserTagLabel = 'bob';
12 public function testGetUserTags()
14 $this->client
->request('GET', '/api/tags.json');
16 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
18 $content = json_decode($this->client
->getResponse()->getContent(), true);
20 $this->assertGreaterThan(0, $content);
21 $this->assertArrayHasKey('id', $content[0]);
22 $this->assertArrayHasKey('label', $content[0]);
24 $tagLabels = array_map(function ($i) {
28 $this->assertNotContains($this->otherUserTagLabel
, $tagLabels, 'There is a possible tag leak');
33 public function testDeleteUserTag()
35 $tagLabel = 'tagtest';
37 $tag->setLabel($tagLabel);
39 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
44 $this->client
->request('DELETE', '/api/tags/' . $tag->getId() . '.json');
46 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
48 $content = json_decode($this->client
->getResponse()->getContent(), true);
50 $this->assertArrayHasKey('label', $content);
51 $this->assertSame($tag->getLabel(), $content['label']);
52 $this->assertSame($tag->getSlug(), $content['slug']);
54 $entries = $em->getRepository('WallabagCoreBundle:Entry')
55 ->findAllByTagId($this->user
->getId(), $tag->getId());
57 $this->assertCount(0, $entries);
59 $tag = $em->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
61 $this->assertNull($tag, $tagLabel . ' was removed because it begun an orphan tag');
64 public function testDeleteOtherUserTag()
66 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
67 $tag = $em->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($this->otherUserTagLabel
);
69 $this->client
->request('DELETE', '/api/tags/' . $tag->getId() . '.json');
71 $this->assertSame(404, $this->client
->getResponse()->getStatusCode());
74 public function dataForDeletingTagByLabel()
83 * @dataProvider dataForDeletingTagByLabel
85 public function testDeleteTagByLabel($useQueryString)
87 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
88 $entry = $this->client
->getContainer()
89 ->get('doctrine.orm.entity_manager')
90 ->getRepository('WallabagCoreBundle:Entry')
91 ->findOneWithTags($this->user
->getId());
96 $tag->setLabel('Awesome tag for test');
101 $em->persist($entry);
104 if ($useQueryString) {
105 $this->client
->request('DELETE', '/api/tag/label.json?tag=' . $tag->getLabel());
107 $this->client
->request('DELETE', '/api/tag/label.json', ['tag' => $tag->getLabel()]);
110 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
112 $content = json_decode($this->client
->getResponse()->getContent(), true);
114 $this->assertArrayHasKey('label', $content);
115 $this->assertSame($tag->getLabel(), $content['label']);
116 $this->assertSame($tag->getSlug(), $content['slug']);
118 $entries = $this->client
->getContainer()
119 ->get('doctrine.orm.entity_manager')
120 ->getRepository('WallabagCoreBundle:Entry')
121 ->findAllByTagId($this->user
->getId(), $tag->getId());
123 $this->assertCount(0, $entries);
126 public function testDeleteTagByLabelNotFound()
128 $this->client
->request('DELETE', '/api/tag/label.json', ['tag' => 'does not exist']);
130 $this->assertSame(404, $this->client
->getResponse()->getStatusCode());
133 public function testDeleteTagByLabelOtherUser()
135 $this->client
->request('DELETE', '/api/tag/label.json', ['tag' => $this->otherUserTagLabel
]);
137 $this->assertSame(404, $this->client
->getResponse()->getStatusCode());
141 * @dataProvider dataForDeletingTagByLabel
143 public function testDeleteTagsByLabel($useQueryString)
145 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
146 $entry = $this->client
->getContainer()
147 ->get('doctrine.orm.entity_manager')
148 ->getRepository('WallabagCoreBundle:Entry')
149 ->findOneWithTags($this->user
->getId());
154 $tag->setLabel('Awesome tag for tagsLabel');
158 $tag2->setLabel('Awesome tag for tagsLabel 2');
161 $entry->addTag($tag);
162 $entry->addTag($tag2);
164 $em->persist($entry);
167 if ($useQueryString) {
168 $this->client
->request('DELETE', '/api/tags/label.json?tags=' . $tag->getLabel() . ',' . $tag2->getLabel());
170 $this->client
->request('DELETE', '/api/tags/label.json', ['tags' => $tag->getLabel() . ',' . $tag2->getLabel()]);
173 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
175 $content = json_decode($this->client
->getResponse()->getContent(), true);
177 $this->assertCount(2, $content);
179 $this->assertArrayHasKey('label', $content[0]);
180 $this->assertSame($tag->getLabel(), $content[0]['label']);
181 $this->assertSame($tag->getSlug(), $content[0]['slug']);
183 $this->assertArrayHasKey('label', $content[1]);
184 $this->assertSame($tag2->getLabel(), $content[1]['label']);
185 $this->assertSame($tag2->getSlug(), $content[1]['slug']);
187 $entries = $this->client
->getContainer()
188 ->get('doctrine.orm.entity_manager')
189 ->getRepository('WallabagCoreBundle:Entry')
190 ->findAllByTagId($this->user
->getId(), $tag->getId());
192 $this->assertCount(0, $entries);
194 $entries = $this->client
->getContainer()
195 ->get('doctrine.orm.entity_manager')
196 ->getRepository('WallabagCoreBundle:Entry')
197 ->findAllByTagId($this->user
->getId(), $tag2->getId());
199 $this->assertCount(0, $entries);
202 public function testDeleteTagsByLabelNotFound()
204 $this->client
->request('DELETE', '/api/tags/label.json', ['tags' => 'does not exist']);
206 $this->assertSame(404, $this->client
->getResponse()->getStatusCode());
209 public function testDeleteTagsByLabelOtherUser()
211 $this->client
->request('DELETE', '/api/tags/label.json', ['tags' => $this->otherUserTagLabel
]);
213 $this->assertSame(404, $this->client
->getResponse()->getStatusCode());