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 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
36 $entry = $this->client
->getContainer()
37 ->get('doctrine.orm.entity_manager')
38 ->getRepository('WallabagCoreBundle:Entry')
39 ->findOneWithTags($this->user
->getId());
43 $tagLabel = 'tagtest';
45 $tag->setLabel($tagLabel);
54 $this->client
->request('DELETE', '/api/tags/' . $tag->getId() . '.json');
56 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
58 $content = json_decode($this->client
->getResponse()->getContent(), true);
60 $this->assertArrayHasKey('label', $content);
61 $this->assertSame($tag->getLabel(), $content['label']);
62 $this->assertSame($tag->getSlug(), $content['slug']);
64 $entries = $em->getRepository('WallabagCoreBundle:Entry')
65 ->findAllByTagId($this->user
->getId(), $tag->getId());
67 $this->assertCount(0, $entries);
69 $tag = $em->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
71 $this->assertNull($tag, $tagLabel . ' was removed because it begun an orphan tag');
74 public function testDeleteOtherUserTag()
76 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
77 $tag = $em->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($this->otherUserTagLabel
);
79 $this->client
->request('DELETE', '/api/tags/' . $tag->getId() . '.json');
81 $this->assertSame(404, $this->client
->getResponse()->getStatusCode());
84 public function dataForDeletingTagByLabel()
93 * @dataProvider dataForDeletingTagByLabel
95 public function testDeleteTagByLabel($useQueryString)
97 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
98 $entry = $this->client
->getContainer()
99 ->get('doctrine.orm.entity_manager')
100 ->getRepository('WallabagCoreBundle:Entry')
101 ->findOneWithTags($this->user
->getId());
106 $tag->setLabel('Awesome tag for test');
109 $entry->addTag($tag);
111 $em->persist($entry);
114 if ($useQueryString) {
115 $this->client
->request('DELETE', '/api/tag/label.json?tag=' . $tag->getLabel());
117 $this->client
->request('DELETE', '/api/tag/label.json', ['tag' => $tag->getLabel()]);
120 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
122 $content = json_decode($this->client
->getResponse()->getContent(), true);
124 $this->assertArrayHasKey('label', $content);
125 $this->assertSame($tag->getLabel(), $content['label']);
126 $this->assertSame($tag->getSlug(), $content['slug']);
128 $entries = $this->client
->getContainer()
129 ->get('doctrine.orm.entity_manager')
130 ->getRepository('WallabagCoreBundle:Entry')
131 ->findAllByTagId($this->user
->getId(), $tag->getId());
133 $this->assertCount(0, $entries);
136 public function testDeleteTagByLabelNotFound()
138 $this->client
->request('DELETE', '/api/tag/label.json', ['tag' => 'does not exist']);
140 $this->assertSame(404, $this->client
->getResponse()->getStatusCode());
143 public function testDeleteTagByLabelOtherUser()
145 $this->client
->request('DELETE', '/api/tag/label.json', ['tag' => $this->otherUserTagLabel
]);
147 $this->assertSame(404, $this->client
->getResponse()->getStatusCode());
151 * @dataProvider dataForDeletingTagByLabel
153 public function testDeleteTagsByLabel($useQueryString)
155 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
156 $entry = $this->client
->getContainer()
157 ->get('doctrine.orm.entity_manager')
158 ->getRepository('WallabagCoreBundle:Entry')
159 ->findOneWithTags($this->user
->getId());
164 $tag->setLabel('Awesome tag for tagsLabel');
168 $tag2->setLabel('Awesome tag for tagsLabel 2');
171 $entry->addTag($tag);
172 $entry->addTag($tag2);
174 $em->persist($entry);
177 if ($useQueryString) {
178 $this->client
->request('DELETE', '/api/tags/label.json?tags=' . $tag->getLabel() . ',' . $tag2->getLabel());
180 $this->client
->request('DELETE', '/api/tags/label.json', ['tags' => $tag->getLabel() . ',' . $tag2->getLabel()]);
183 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
185 $content = json_decode($this->client
->getResponse()->getContent(), true);
187 $this->assertCount(2, $content);
189 $this->assertArrayHasKey('label', $content[0]);
190 $this->assertSame($tag->getLabel(), $content[0]['label']);
191 $this->assertSame($tag->getSlug(), $content[0]['slug']);
193 $this->assertArrayHasKey('label', $content[1]);
194 $this->assertSame($tag2->getLabel(), $content[1]['label']);
195 $this->assertSame($tag2->getSlug(), $content[1]['slug']);
197 $entries = $this->client
->getContainer()
198 ->get('doctrine.orm.entity_manager')
199 ->getRepository('WallabagCoreBundle:Entry')
200 ->findAllByTagId($this->user
->getId(), $tag->getId());
202 $this->assertCount(0, $entries);
204 $entries = $this->client
->getContainer()
205 ->get('doctrine.orm.entity_manager')
206 ->getRepository('WallabagCoreBundle:Entry')
207 ->findAllByTagId($this->user
->getId(), $tag2->getId());
209 $this->assertCount(0, $entries);
212 public function testDeleteTagsByLabelNotFound()
214 $this->client
->request('DELETE', '/api/tags/label.json', ['tags' => 'does not exist']);
216 $this->assertSame(404, $this->client
->getResponse()->getStatusCode());
219 public function testDeleteTagsByLabelOtherUser()
221 $this->client
->request('DELETE', '/api/tags/label.json', ['tags' => $this->otherUserTagLabel
]);
223 $this->assertSame(404, $this->client
->getResponse()->getStatusCode());