]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php
Add a real configuration for CS-Fixer
[github/wallabag/wallabag.git] / tests / Wallabag / ApiBundle / Controller / TagRestControllerTest.php
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->assertSame(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
25 public function testDeleteUserTag()
26 {
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();
35
36 $this->client->request('DELETE', '/api/tags/' . $tag->getId() . '.json');
37
38 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
39
40 $content = json_decode($this->client->getResponse()->getContent(), true);
41
42 $this->assertArrayHasKey('label', $content);
43 $this->assertSame($tag->getLabel(), $content['label']);
44 $this->assertSame($tag->getSlug(), $content['slug']);
45
46 $entries = $em->getRepository('WallabagCoreBundle:Entry')
47 ->findAllByTagId($this->user->getId(), $tag->getId());
48
49 $this->assertCount(0, $entries);
50
51 $tag = $em->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
52
53 $this->assertNull($tag, $tagLabel . ' was removed because it begun an orphan tag');
54 }
55
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)
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
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 }
91
92 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
93
94 $content = json_decode($this->client->getResponse()->getContent(), true);
95
96 $this->assertArrayHasKey('label', $content);
97 $this->assertSame($tag->getLabel(), $content['label']);
98 $this->assertSame($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->assertSame(404, $this->client->getResponse()->getStatusCode());
113 }
114
115 /**
116 * @dataProvider dataForDeletingTagByLabel
117 */
118 public function testDeleteTagsByLabel($useQueryString)
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
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 }
147
148 $this->assertSame(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->assertSame($tag->getLabel(), $content[0]['label']);
156 $this->assertSame($tag->getSlug(), $content[0]['slug']);
157
158 $this->assertArrayHasKey('label', $content[1]);
159 $this->assertSame($tag2->getLabel(), $content[1]['label']);
160 $this->assertSame($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->assertSame(404, $this->client->getResponse()->getStatusCode());
182 }
183 }