]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php
90b132ebecbf4600c1d341d6f7027ad76cfbffca
[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->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
25 /**
26 * @depends testGetUserTags
27 */
28 public function testDeleteUserTag($tag)
29 {
30 $tagName = $tag['label'];
31
32 $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json');
33
34 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
35
36 $content = json_decode($this->client->getResponse()->getContent(), true);
37
38 $this->assertArrayHasKey('label', $content);
39 $this->assertEquals($tag['label'], $content['label']);
40 $this->assertEquals($tag['slug'], $content['slug']);
41
42 $entries = $this->client->getContainer()
43 ->get('doctrine.orm.entity_manager')
44 ->getRepository('WallabagCoreBundle:Entry')
45 ->findAllByTagId($this->user->getId(), $tag['id']);
46
47 $this->assertCount(0, $entries);
48
49 $tag = $this->client->getContainer()
50 ->get('doctrine.orm.entity_manager')
51 ->getRepository('WallabagCoreBundle:Tag')
52 ->findOneByLabel($tagName);
53
54 $this->assertNull($tag, $tagName.' was removed because it begun an orphan tag');
55 }
56
57 public function dataForDeletingTagByLabel()
58 {
59 return [
60 'by_query' => [true],
61 'by_body' => [false],
62 ];
63 }
64
65 /**
66 * @dataProvider dataForDeletingTagByLabel
67 */
68 public function testDeleteTagByLabel($useQueryString)
69 {
70 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
71 $entry = $this->client->getContainer()
72 ->get('doctrine.orm.entity_manager')
73 ->getRepository('WallabagCoreBundle:Entry')
74 ->findOneWithTags($this->user->getId());
75
76 $entry = $entry[0];
77
78 $tag = new Tag();
79 $tag->setLabel('Awesome tag for test');
80 $em->persist($tag);
81
82 $entry->addTag($tag);
83
84 $em->persist($entry);
85 $em->flush();
86
87 if ($useQueryString) {
88 $this->client->request('DELETE', '/api/tag/label.json?tag='.$tag->getLabel());
89 } else {
90 $this->client->request('DELETE', '/api/tag/label.json', ['tag' => $tag->getLabel()]);
91 }
92
93 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
94
95 $content = json_decode($this->client->getResponse()->getContent(), true);
96
97 $this->assertArrayHasKey('label', $content);
98 $this->assertEquals($tag->getLabel(), $content['label']);
99 $this->assertEquals($tag->getSlug(), $content['slug']);
100
101 $entries = $this->client->getContainer()
102 ->get('doctrine.orm.entity_manager')
103 ->getRepository('WallabagCoreBundle:Entry')
104 ->findAllByTagId($this->user->getId(), $tag->getId());
105
106 $this->assertCount(0, $entries);
107 }
108
109 public function testDeleteTagByLabelNotFound()
110 {
111 $this->client->request('DELETE', '/api/tag/label.json', ['tag' => 'does not exist']);
112
113 $this->assertEquals(404, $this->client->getResponse()->getStatusCode());
114 }
115
116 /**
117 * @dataProvider dataForDeletingTagByLabel
118 */
119 public function testDeleteTagsByLabel($useQueryString)
120 {
121 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
122 $entry = $this->client->getContainer()
123 ->get('doctrine.orm.entity_manager')
124 ->getRepository('WallabagCoreBundle:Entry')
125 ->findOneWithTags($this->user->getId());
126
127 $entry = $entry[0];
128
129 $tag = new Tag();
130 $tag->setLabel('Awesome tag for tagsLabel');
131 $em->persist($tag);
132
133 $tag2 = new Tag();
134 $tag2->setLabel('Awesome tag for tagsLabel 2');
135 $em->persist($tag2);
136
137 $entry->addTag($tag);
138 $entry->addTag($tag2);
139
140 $em->persist($entry);
141 $em->flush();
142
143 if ($useQueryString) {
144 $this->client->request('DELETE', '/api/tags/label.json?tags='.$tag->getLabel().','.$tag2->getLabel());
145 } else {
146 $this->client->request('DELETE', '/api/tags/label.json', ['tags' => $tag->getLabel().','.$tag2->getLabel()]);
147 }
148
149 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
150
151 $content = json_decode($this->client->getResponse()->getContent(), true);
152
153 $this->assertCount(2, $content);
154
155 $this->assertArrayHasKey('label', $content[0]);
156 $this->assertEquals($tag->getLabel(), $content[0]['label']);
157 $this->assertEquals($tag->getSlug(), $content[0]['slug']);
158
159 $this->assertArrayHasKey('label', $content[1]);
160 $this->assertEquals($tag2->getLabel(), $content[1]['label']);
161 $this->assertEquals($tag2->getSlug(), $content[1]['slug']);
162
163 $entries = $this->client->getContainer()
164 ->get('doctrine.orm.entity_manager')
165 ->getRepository('WallabagCoreBundle:Entry')
166 ->findAllByTagId($this->user->getId(), $tag->getId());
167
168 $this->assertCount(0, $entries);
169
170 $entries = $this->client->getContainer()
171 ->get('doctrine.orm.entity_manager')
172 ->getRepository('WallabagCoreBundle:Entry')
173 ->findAllByTagId($this->user->getId(), $tag2->getId());
174
175 $this->assertCount(0, $entries);
176 }
177
178 public function testDeleteTagsByLabelNotFound()
179 {
180 $this->client->request('DELETE', '/api/tags/label.json', ['tags' => 'does not exist']);
181
182 $this->assertEquals(404, $this->client->getResponse()->getStatusCode());
183 }
184 }