]>
Commit | Line | Data |
---|---|---|
900c8448 NL |
1 | <?php |
2 | ||
3 | namespace Tests\Wallabag\ApiBundle\Controller; | |
4 | ||
5 | use Tests\Wallabag\ApiBundle\WallabagApiTestCase; | |
6 | use Wallabag\CoreBundle\Entity\Tag; | |
7 | ||
8 | class TagRestControllerTest extends WallabagApiTestCase | |
9 | { | |
10 | public function testGetUserTags() | |
11 | { | |
12 | $this->client->request('GET', '/api/tags.json'); | |
13 | ||
14 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
15 | ||
16 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
17 | ||
18 | $this->assertGreaterThan(0, $content); | |
19 | $this->assertArrayHasKey('id', $content[0]); | |
20 | $this->assertArrayHasKey('label', $content[0]); | |
21 | ||
22 | return end($content); | |
23 | } | |
24 | ||
7ab5eb95 | 25 | public function testDeleteUserTag() |
900c8448 | 26 | { |
7ab5eb95 | 27 | $tagLabel = 'tagtest'; |
28 | $tag = new Tag(); | |
29 | $tag->setLabel($tagLabel); | |
30 | ||
31 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
32 | $em->persist($tag); | |
33 | $em->flush(); | |
34 | $em->clear(); | |
900c8448 | 35 | |
7ab5eb95 | 36 | $this->client->request('DELETE', '/api/tags/'.$tag->getId().'.json'); |
900c8448 NL |
37 | |
38 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
39 | ||
40 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
41 | ||
42 | $this->assertArrayHasKey('label', $content); | |
7ab5eb95 | 43 | $this->assertEquals($tag->getLabel(), $content['label']); |
44 | $this->assertEquals($tag->getSlug(), $content['slug']); | |
900c8448 | 45 | |
7ab5eb95 | 46 | $entries = $em->getRepository('WallabagCoreBundle:Entry') |
47 | ->findAllByTagId($this->user->getId(), $tag->getId()); | |
900c8448 NL |
48 | |
49 | $this->assertCount(0, $entries); | |
50 | ||
7ab5eb95 | 51 | $tag = $em->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel); |
900c8448 | 52 | |
7ab5eb95 | 53 | $this->assertNull($tag, $tagLabel.' was removed because it begun an orphan tag'); |
900c8448 NL |
54 | } |
55 | ||
1594a79f JB |
56 | public function dataForDeletingTagByLabel() |
57 | { | |
58 | return [ | |
59 | 'by_query' => [true], | |
60 | 'by_body' => [false], | |
61 | ]; | |
62 | } | |
63 | ||
64 | /** | |
65 | * @dataProvider dataForDeletingTagByLabel | |
66 | */ | |
67 | public function testDeleteTagByLabel($useQueryString) | |
900c8448 NL |
68 | { |
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()); | |
74 | ||
75 | $entry = $entry[0]; | |
76 | ||
77 | $tag = new Tag(); | |
78 | $tag->setLabel('Awesome tag for test'); | |
79 | $em->persist($tag); | |
80 | ||
81 | $entry->addTag($tag); | |
82 | ||
83 | $em->persist($entry); | |
84 | $em->flush(); | |
85 | ||
1594a79f JB |
86 | if ($useQueryString) { |
87 | $this->client->request('DELETE', '/api/tag/label.json?tag='.$tag->getLabel()); | |
88 | } else { | |
89 | $this->client->request('DELETE', '/api/tag/label.json', ['tag' => $tag->getLabel()]); | |
90 | } | |
900c8448 NL |
91 | |
92 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
93 | ||
94 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
95 | ||
96 | $this->assertArrayHasKey('label', $content); | |
97 | $this->assertEquals($tag->getLabel(), $content['label']); | |
98 | $this->assertEquals($tag->getSlug(), $content['slug']); | |
99 | ||
100 | $entries = $this->client->getContainer() | |
101 | ->get('doctrine.orm.entity_manager') | |
102 | ->getRepository('WallabagCoreBundle:Entry') | |
103 | ->findAllByTagId($this->user->getId(), $tag->getId()); | |
104 | ||
105 | $this->assertCount(0, $entries); | |
106 | } | |
107 | ||
108 | public function testDeleteTagByLabelNotFound() | |
109 | { | |
110 | $this->client->request('DELETE', '/api/tag/label.json', ['tag' => 'does not exist']); | |
111 | ||
112 | $this->assertEquals(404, $this->client->getResponse()->getStatusCode()); | |
113 | } | |
114 | ||
1594a79f JB |
115 | /** |
116 | * @dataProvider dataForDeletingTagByLabel | |
117 | */ | |
118 | public function testDeleteTagsByLabel($useQueryString) | |
900c8448 NL |
119 | { |
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()); | |
125 | ||
126 | $entry = $entry[0]; | |
127 | ||
128 | $tag = new Tag(); | |
129 | $tag->setLabel('Awesome tag for tagsLabel'); | |
130 | $em->persist($tag); | |
131 | ||
132 | $tag2 = new Tag(); | |
133 | $tag2->setLabel('Awesome tag for tagsLabel 2'); | |
134 | $em->persist($tag2); | |
135 | ||
136 | $entry->addTag($tag); | |
137 | $entry->addTag($tag2); | |
138 | ||
139 | $em->persist($entry); | |
140 | $em->flush(); | |
141 | ||
1594a79f JB |
142 | if ($useQueryString) { |
143 | $this->client->request('DELETE', '/api/tags/label.json?tags='.$tag->getLabel().','.$tag2->getLabel()); | |
144 | } else { | |
145 | $this->client->request('DELETE', '/api/tags/label.json', ['tags' => $tag->getLabel().','.$tag2->getLabel()]); | |
146 | } | |
900c8448 NL |
147 | |
148 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
149 | ||
150 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
151 | ||
152 | $this->assertCount(2, $content); | |
153 | ||
154 | $this->assertArrayHasKey('label', $content[0]); | |
155 | $this->assertEquals($tag->getLabel(), $content[0]['label']); | |
156 | $this->assertEquals($tag->getSlug(), $content[0]['slug']); | |
157 | ||
158 | $this->assertArrayHasKey('label', $content[1]); | |
159 | $this->assertEquals($tag2->getLabel(), $content[1]['label']); | |
160 | $this->assertEquals($tag2->getSlug(), $content[1]['slug']); | |
161 | ||
162 | $entries = $this->client->getContainer() | |
163 | ->get('doctrine.orm.entity_manager') | |
164 | ->getRepository('WallabagCoreBundle:Entry') | |
165 | ->findAllByTagId($this->user->getId(), $tag->getId()); | |
166 | ||
167 | $this->assertCount(0, $entries); | |
168 | ||
169 | $entries = $this->client->getContainer() | |
170 | ->get('doctrine.orm.entity_manager') | |
171 | ->getRepository('WallabagCoreBundle:Entry') | |
172 | ->findAllByTagId($this->user->getId(), $tag2->getId()); | |
173 | ||
174 | $this->assertCount(0, $entries); | |
175 | } | |
176 | ||
177 | public function testDeleteTagsByLabelNotFound() | |
178 | { | |
179 | $this->client->request('DELETE', '/api/tags/label.json', ['tags' => 'does not exist']); | |
180 | ||
181 | $this->assertEquals(404, $this->client->getResponse()->getStatusCode()); | |
182 | } | |
183 | } |