]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php
TagRestController: fix test for tag without entries
[github/wallabag/wallabag.git] / tests / Wallabag / ApiBundle / Controller / TagRestControllerTest.php
CommitLineData
900c8448
NL
1<?php
2
3namespace Tests\Wallabag\ApiBundle\Controller;
4
5use Tests\Wallabag\ApiBundle\WallabagApiTestCase;
6use Wallabag\CoreBundle\Entity\Tag;
7
8class TagRestControllerTest extends WallabagApiTestCase
9{
0ee98482
KD
10 private $otherUserTagLabel = 'bob';
11
900c8448
NL
12 public function testGetUserTags()
13 {
14 $this->client->request('GET', '/api/tags.json');
15
f808b016 16 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
17
18 $content = json_decode($this->client->getResponse()->getContent(), true);
19
20 $this->assertGreaterThan(0, $content);
21 $this->assertArrayHasKey('id', $content[0]);
22 $this->assertArrayHasKey('label', $content[0]);
23
0ee98482
KD
24 $tagLabels = array_map(function ($i) {
25 return $i['label'];
26 }, $content);
27
28 $this->assertNotContains($this->otherUserTagLabel, $tagLabels, 'There is a possible tag leak');
29
900c8448
NL
30 return end($content);
31 }
32
7ab5eb95 33 public function testDeleteUserTag()
900c8448 34 {
6c40d7fc
KD
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());
40
41 $entry = $entry[0];
42
7ab5eb95 43 $tagLabel = 'tagtest';
44 $tag = new Tag();
45 $tag->setLabel($tagLabel);
7ab5eb95 46 $em->persist($tag);
6c40d7fc
KD
47
48 $entry->addTag($tag);
49
50 $em->persist($entry);
7ab5eb95 51 $em->flush();
52 $em->clear();
900c8448 53
f808b016 54 $this->client->request('DELETE', '/api/tags/' . $tag->getId() . '.json');
900c8448 55
f808b016 56 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
57
58 $content = json_decode($this->client->getResponse()->getContent(), true);
59
60 $this->assertArrayHasKey('label', $content);
f808b016
JB
61 $this->assertSame($tag->getLabel(), $content['label']);
62 $this->assertSame($tag->getSlug(), $content['slug']);
900c8448 63
7ab5eb95 64 $entries = $em->getRepository('WallabagCoreBundle:Entry')
65 ->findAllByTagId($this->user->getId(), $tag->getId());
900c8448
NL
66
67 $this->assertCount(0, $entries);
68
7ab5eb95 69 $tag = $em->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
900c8448 70
f808b016 71 $this->assertNull($tag, $tagLabel . ' was removed because it begun an orphan tag');
900c8448
NL
72 }
73
0ee98482
KD
74 public function testDeleteOtherUserTag()
75 {
76 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
77 $tag = $em->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($this->otherUserTagLabel);
78
79 $this->client->request('DELETE', '/api/tags/' . $tag->getId() . '.json');
80
81 $this->assertSame(404, $this->client->getResponse()->getStatusCode());
82 }
83
1594a79f
JB
84 public function dataForDeletingTagByLabel()
85 {
86 return [
87 'by_query' => [true],
88 'by_body' => [false],
89 ];
90 }
91
92 /**
93 * @dataProvider dataForDeletingTagByLabel
94 */
95 public function testDeleteTagByLabel($useQueryString)
900c8448
NL
96 {
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());
102
103 $entry = $entry[0];
104
105 $tag = new Tag();
106 $tag->setLabel('Awesome tag for test');
107 $em->persist($tag);
108
109 $entry->addTag($tag);
110
111 $em->persist($entry);
112 $em->flush();
113
1594a79f 114 if ($useQueryString) {
f808b016 115 $this->client->request('DELETE', '/api/tag/label.json?tag=' . $tag->getLabel());
1594a79f
JB
116 } else {
117 $this->client->request('DELETE', '/api/tag/label.json', ['tag' => $tag->getLabel()]);
118 }
900c8448 119
f808b016 120 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
121
122 $content = json_decode($this->client->getResponse()->getContent(), true);
123
124 $this->assertArrayHasKey('label', $content);
f808b016
JB
125 $this->assertSame($tag->getLabel(), $content['label']);
126 $this->assertSame($tag->getSlug(), $content['slug']);
900c8448
NL
127
128 $entries = $this->client->getContainer()
129 ->get('doctrine.orm.entity_manager')
130 ->getRepository('WallabagCoreBundle:Entry')
131 ->findAllByTagId($this->user->getId(), $tag->getId());
132
133 $this->assertCount(0, $entries);
134 }
135
136 public function testDeleteTagByLabelNotFound()
137 {
138 $this->client->request('DELETE', '/api/tag/label.json', ['tag' => 'does not exist']);
139
f808b016 140 $this->assertSame(404, $this->client->getResponse()->getStatusCode());
900c8448
NL
141 }
142
0ee98482
KD
143 public function testDeleteTagByLabelOtherUser()
144 {
145 $this->client->request('DELETE', '/api/tag/label.json', ['tag' => $this->otherUserTagLabel]);
146
147 $this->assertSame(404, $this->client->getResponse()->getStatusCode());
148 }
149
1594a79f
JB
150 /**
151 * @dataProvider dataForDeletingTagByLabel
152 */
153 public function testDeleteTagsByLabel($useQueryString)
900c8448
NL
154 {
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());
160
161 $entry = $entry[0];
162
163 $tag = new Tag();
164 $tag->setLabel('Awesome tag for tagsLabel');
165 $em->persist($tag);
166
167 $tag2 = new Tag();
168 $tag2->setLabel('Awesome tag for tagsLabel 2');
169 $em->persist($tag2);
170
171 $entry->addTag($tag);
172 $entry->addTag($tag2);
173
174 $em->persist($entry);
175 $em->flush();
176
1594a79f 177 if ($useQueryString) {
f808b016 178 $this->client->request('DELETE', '/api/tags/label.json?tags=' . $tag->getLabel() . ',' . $tag2->getLabel());
1594a79f 179 } else {
f808b016 180 $this->client->request('DELETE', '/api/tags/label.json', ['tags' => $tag->getLabel() . ',' . $tag2->getLabel()]);
1594a79f 181 }
900c8448 182
f808b016 183 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
184
185 $content = json_decode($this->client->getResponse()->getContent(), true);
186
187 $this->assertCount(2, $content);
188
189 $this->assertArrayHasKey('label', $content[0]);
f808b016
JB
190 $this->assertSame($tag->getLabel(), $content[0]['label']);
191 $this->assertSame($tag->getSlug(), $content[0]['slug']);
900c8448
NL
192
193 $this->assertArrayHasKey('label', $content[1]);
f808b016
JB
194 $this->assertSame($tag2->getLabel(), $content[1]['label']);
195 $this->assertSame($tag2->getSlug(), $content[1]['slug']);
900c8448
NL
196
197 $entries = $this->client->getContainer()
198 ->get('doctrine.orm.entity_manager')
199 ->getRepository('WallabagCoreBundle:Entry')
200 ->findAllByTagId($this->user->getId(), $tag->getId());
201
202 $this->assertCount(0, $entries);
203
204 $entries = $this->client->getContainer()
205 ->get('doctrine.orm.entity_manager')
206 ->getRepository('WallabagCoreBundle:Entry')
207 ->findAllByTagId($this->user->getId(), $tag2->getId());
208
209 $this->assertCount(0, $entries);
210 }
211
212 public function testDeleteTagsByLabelNotFound()
213 {
214 $this->client->request('DELETE', '/api/tags/label.json', ['tags' => 'does not exist']);
215
f808b016 216 $this->assertSame(404, $this->client->getResponse()->getStatusCode());
900c8448 217 }
0ee98482
KD
218
219 public function testDeleteTagsByLabelOtherUser()
220 {
221 $this->client->request('DELETE', '/api/tags/label.json', ['tags' => $this->otherUserTagLabel]);
222
223 $this->assertSame(404, $this->client->getResponse()->getStatusCode());
224 }
900c8448 225}