]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php
Add a real configuration for CS-Fixer
[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{
10 public function testGetUserTags()
11 {
12 $this->client->request('GET', '/api/tags.json');
13
f808b016 14 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
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
f808b016 36 $this->client->request('DELETE', '/api/tags/' . $tag->getId() . '.json');
900c8448 37
f808b016 38 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
39
40 $content = json_decode($this->client->getResponse()->getContent(), true);
41
42 $this->assertArrayHasKey('label', $content);
f808b016
JB
43 $this->assertSame($tag->getLabel(), $content['label']);
44 $this->assertSame($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
f808b016 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 86 if ($useQueryString) {
f808b016 87 $this->client->request('DELETE', '/api/tag/label.json?tag=' . $tag->getLabel());
1594a79f
JB
88 } else {
89 $this->client->request('DELETE', '/api/tag/label.json', ['tag' => $tag->getLabel()]);
90 }
900c8448 91
f808b016 92 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
93
94 $content = json_decode($this->client->getResponse()->getContent(), true);
95
96 $this->assertArrayHasKey('label', $content);
f808b016
JB
97 $this->assertSame($tag->getLabel(), $content['label']);
98 $this->assertSame($tag->getSlug(), $content['slug']);
900c8448
NL
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
f808b016 112 $this->assertSame(404, $this->client->getResponse()->getStatusCode());
900c8448
NL
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 142 if ($useQueryString) {
f808b016 143 $this->client->request('DELETE', '/api/tags/label.json?tags=' . $tag->getLabel() . ',' . $tag2->getLabel());
1594a79f 144 } else {
f808b016 145 $this->client->request('DELETE', '/api/tags/label.json', ['tags' => $tag->getLabel() . ',' . $tag2->getLabel()]);
1594a79f 146 }
900c8448 147
f808b016 148 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
149
150 $content = json_decode($this->client->getResponse()->getContent(), true);
151
152 $this->assertCount(2, $content);
153
154 $this->assertArrayHasKey('label', $content[0]);
f808b016
JB
155 $this->assertSame($tag->getLabel(), $content[0]['label']);
156 $this->assertSame($tag->getSlug(), $content[0]['slug']);
900c8448
NL
157
158 $this->assertArrayHasKey('label', $content[1]);
f808b016
JB
159 $this->assertSame($tag2->getLabel(), $content[1]['label']);
160 $this->assertSame($tag2->getSlug(), $content[1]['slug']);
900c8448
NL
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
f808b016 181 $this->assertSame(404, $this->client->getResponse()->getStatusCode());
900c8448
NL
182 }
183}