3 namespace Tests\Wallabag\ApiBundle\Controller
;
5 use Tests\Wallabag\ApiBundle\WallabagApiTestCase
;
6 use Wallabag\CoreBundle\Entity\Tag
;
8 class TagRestControllerTest
extends WallabagApiTestCase
10 public function testGetUserTags()
12 $this->client
->request('GET', '/api/tags.json');
14 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
16 $content = json_decode($this->client
->getResponse()->getContent(), true);
18 $this->assertGreaterThan(0, $content);
19 $this->assertArrayHasKey('id', $content[0]);
20 $this->assertArrayHasKey('label', $content[0]);
25 public function testDeleteUserTag()
27 $tagLabel = 'tagtest';
29 $tag->setLabel($tagLabel);
31 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
36 $this->client
->request('DELETE', '/api/tags/' . $tag->getId() . '.json');
38 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
40 $content = json_decode($this->client
->getResponse()->getContent(), true);
42 $this->assertArrayHasKey('label', $content);
43 $this->assertSame($tag->getLabel(), $content['label']);
44 $this->assertSame($tag->getSlug(), $content['slug']);
46 $entries = $em->getRepository('WallabagCoreBundle:Entry')
47 ->findAllByTagId($this->user
->getId(), $tag->getId());
49 $this->assertCount(0, $entries);
51 $tag = $em->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
53 $this->assertNull($tag, $tagLabel . ' was removed because it begun an orphan tag');
56 public function dataForDeletingTagByLabel()
65 * @dataProvider dataForDeletingTagByLabel
67 public function testDeleteTagByLabel($useQueryString)
69 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
70 $entry = $this->client
->getContainer()
71 ->get('doctrine.orm.entity_manager')
72 ->getRepository('WallabagCoreBundle:Entry')
73 ->findOneWithTags($this->user
->getId());
78 $tag->setLabel('Awesome tag for test');
86 if ($useQueryString) {
87 $this->client
->request('DELETE', '/api/tag/label.json?tag=' . $tag->getLabel());
89 $this->client
->request('DELETE', '/api/tag/label.json', ['tag' => $tag->getLabel()]);
92 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
94 $content = json_decode($this->client
->getResponse()->getContent(), true);
96 $this->assertArrayHasKey('label', $content);
97 $this->assertSame($tag->getLabel(), $content['label']);
98 $this->assertSame($tag->getSlug(), $content['slug']);
100 $entries = $this->client
->getContainer()
101 ->get('doctrine.orm.entity_manager')
102 ->getRepository('WallabagCoreBundle:Entry')
103 ->findAllByTagId($this->user
->getId(), $tag->getId());
105 $this->assertCount(0, $entries);
108 public function testDeleteTagByLabelNotFound()
110 $this->client
->request('DELETE', '/api/tag/label.json', ['tag' => 'does not exist']);
112 $this->assertSame(404, $this->client
->getResponse()->getStatusCode());
116 * @dataProvider dataForDeletingTagByLabel
118 public function testDeleteTagsByLabel($useQueryString)
120 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
121 $entry = $this->client
->getContainer()
122 ->get('doctrine.orm.entity_manager')
123 ->getRepository('WallabagCoreBundle:Entry')
124 ->findOneWithTags($this->user
->getId());
129 $tag->setLabel('Awesome tag for tagsLabel');
133 $tag2->setLabel('Awesome tag for tagsLabel 2');
136 $entry->addTag($tag);
137 $entry->addTag($tag2);
139 $em->persist($entry);
142 if ($useQueryString) {
143 $this->client
->request('DELETE', '/api/tags/label.json?tags=' . $tag->getLabel() . ',' . $tag2->getLabel());
145 $this->client
->request('DELETE', '/api/tags/label.json', ['tags' => $tag->getLabel() . ',' . $tag2->getLabel()]);
148 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
150 $content = json_decode($this->client
->getResponse()->getContent(), true);
152 $this->assertCount(2, $content);
154 $this->assertArrayHasKey('label', $content[0]);
155 $this->assertSame($tag->getLabel(), $content[0]['label']);
156 $this->assertSame($tag->getSlug(), $content[0]['slug']);
158 $this->assertArrayHasKey('label', $content[1]);
159 $this->assertSame($tag2->getLabel(), $content[1]['label']);
160 $this->assertSame($tag2->getSlug(), $content[1]['slug']);
162 $entries = $this->client
->getContainer()
163 ->get('doctrine.orm.entity_manager')
164 ->getRepository('WallabagCoreBundle:Entry')
165 ->findAllByTagId($this->user
->getId(), $tag->getId());
167 $this->assertCount(0, $entries);
169 $entries = $this->client
->getContainer()
170 ->get('doctrine.orm.entity_manager')
171 ->getRepository('WallabagCoreBundle:Entry')
172 ->findAllByTagId($this->user
->getId(), $tag2->getId());
174 $this->assertCount(0, $entries);
177 public function testDeleteTagsByLabelNotFound()
179 $this->client
->request('DELETE', '/api/tags/label.json', ['tags' => 'does not exist']);
181 $this->assertSame(404, $this->client
->getResponse()->getStatusCode());