]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/Controller/TagControllerTest.php
TagController: fix duplicated tags when renaming them
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Controller / TagControllerTest.php
1 <?php
2
3 namespace Tests\Wallabag\CoreBundle\Controller;
4
5 use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
6 use Wallabag\CoreBundle\Entity\Entry;
7 use Wallabag\CoreBundle\Entity\Tag;
8
9 class TagControllerTest extends WallabagCoreTestCase
10 {
11 public $tagName = 'opensource';
12 public $caseTagName = 'OpenSource';
13
14 public function testList()
15 {
16 $this->logInAs('admin');
17 $client = $this->getClient();
18
19 $client->request('GET', '/tag/list');
20
21 $this->assertSame(200, $client->getResponse()->getStatusCode());
22 }
23
24 public function testAddTagToEntry()
25 {
26 $this->logInAs('admin');
27 $client = $this->getClient();
28
29 $entry = new Entry($this->getLoggedInUser());
30 $entry->setUrl('http://0.0.0.0/foo');
31 $this->getEntityManager()->persist($entry);
32 $this->getEntityManager()->flush();
33 $this->getEntityManager()->clear();
34
35 $crawler = $client->request('GET', '/view/' . $entry->getId());
36
37 $form = $crawler->filter('form[name=tag]')->form();
38
39 $data = [
40 'tag[label]' => $this->caseTagName,
41 ];
42
43 $client->submit($form, $data);
44 $this->assertSame(302, $client->getResponse()->getStatusCode());
45
46 // be sure to reload the entry
47 $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
48 $this->assertCount(1, $entry->getTags());
49 $this->assertContains($this->tagName, $entry->getTags());
50
51 // tag already exists and already assigned
52 $client->submit($form, $data);
53 $this->assertSame(302, $client->getResponse()->getStatusCode());
54
55 $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
56 $this->assertCount(1, $entry->getTags());
57
58 // tag already exists but still not assigned to this entry
59 $data = [
60 'tag[label]' => 'foo bar',
61 ];
62
63 $client->submit($form, $data);
64 $this->assertSame(302, $client->getResponse()->getStatusCode());
65
66 $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
67 $this->assertCount(2, $entry->getTags());
68 }
69
70 public function testAddMultipleTagToEntry()
71 {
72 $this->logInAs('admin');
73 $client = $this->getClient();
74
75 $entry = $client->getContainer()
76 ->get('doctrine.orm.entity_manager')
77 ->getRepository('WallabagCoreBundle:Entry')
78 ->findByUrlAndUserId('http://0.0.0.0/entry2', $this->getLoggedInUserId());
79
80 $crawler = $client->request('GET', '/view/' . $entry->getId());
81
82 $form = $crawler->filter('form[name=tag]')->form();
83
84 $data = [
85 'tag[label]' => 'foo2, Bar2',
86 ];
87
88 $client->submit($form, $data);
89 $this->assertSame(302, $client->getResponse()->getStatusCode());
90
91 $newEntry = $client->getContainer()
92 ->get('doctrine.orm.entity_manager')
93 ->getRepository('WallabagCoreBundle:Entry')
94 ->find($entry->getId());
95
96 $tags = $newEntry->getTags()->toArray();
97 foreach ($tags as $key => $tag) {
98 $tags[$key] = $tag->getLabel();
99 }
100
101 $this->assertGreaterThanOrEqual(2, \count($tags));
102 $this->assertNotFalse(array_search('foo2', $tags, true), 'Tag foo2 is assigned to the entry');
103 $this->assertNotFalse(array_search('bar2', $tags, true), 'Tag bar2 is assigned to the entry');
104 }
105
106 public function testRemoveTagFromEntry()
107 {
108 $this->logInAs('admin');
109 $client = $this->getClient();
110
111 $tag = new Tag();
112 $tag->setLabel($this->tagName);
113 $entry = new Entry($this->getLoggedInUser());
114 $entry->setUrl('http://0.0.0.0/foo');
115 $entry->addTag($tag);
116 $this->getEntityManager()->persist($entry);
117 $this->getEntityManager()->flush();
118 $this->getEntityManager()->clear();
119
120 // We make a first request to set an history and test redirection after tag deletion
121 $client->request('GET', '/view/' . $entry->getId());
122 $entryUri = $client->getRequest()->getUri();
123 $client->request('GET', '/remove-tag/' . $entry->getId() . '/' . $tag->getId());
124
125 $this->assertSame(302, $client->getResponse()->getStatusCode());
126 $this->assertSame($entryUri, $client->getResponse()->getTargetUrl());
127
128 // re-retrieve the entry to be sure to get fresh data from database (mostly for tags)
129 $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
130 $this->assertNotContains($this->tagName, $entry->getTags());
131
132 $client->request('GET', '/remove-tag/' . $entry->getId() . '/' . $tag->getId());
133
134 $this->assertSame(404, $client->getResponse()->getStatusCode());
135
136 $tag = $client->getContainer()
137 ->get('doctrine.orm.entity_manager')
138 ->getRepository('WallabagCoreBundle:Tag')
139 ->findOneByLabel($this->tagName);
140
141 $this->assertNull($tag, $this->tagName . ' was removed because it begun an orphan tag');
142 }
143
144 public function testShowEntriesForTagAction()
145 {
146 $this->logInAs('admin');
147 $client = $this->getClient();
148 $em = $client->getContainer()
149 ->get('doctrine.orm.entity_manager');
150
151 $tag = new Tag();
152 $tag->setLabel($this->tagName);
153
154 $entry = $client->getContainer()
155 ->get('doctrine.orm.entity_manager')
156 ->getRepository('WallabagCoreBundle:Entry')
157 ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getLoggedInUserId());
158
159 $tag->addEntry($entry);
160
161 $em->persist($entry);
162 $em->persist($tag);
163 $em->flush();
164
165 $tag = $client->getContainer()
166 ->get('doctrine.orm.entity_manager')
167 ->getRepository('WallabagCoreBundle:Tag')
168 ->findOneByEntryAndTagLabel($entry, $this->tagName);
169
170 $crawler = $client->request('GET', '/tag/list/' . $tag->getSlug());
171
172 $this->assertSame(200, $client->getResponse()->getStatusCode());
173 $this->assertCount(1, $crawler->filter('[id*="entry-"]'));
174
175 $entry->removeTag($tag);
176 $em->remove($tag);
177 $em->flush();
178 }
179
180 public function testRenameTagUsingTheFormInsideTagList()
181 {
182 $newTagLabel = 'rename label';
183
184 $this->logInAs('admin');
185 $client = $this->getClient();
186
187 $tag = new Tag();
188 $tag->setLabel($this->tagName);
189
190 $entry = new Entry($this->getLoggedInUser());
191 $entry->setUrl('http://0.0.0.0/foo');
192 $entry->addTag($tag);
193 $this->getEntityManager()->persist($entry);
194
195 $entry2 = new Entry($this->getLoggedInUser());
196 $entry2->setUrl('http://0.0.0.0/bar');
197 $entry2->addTag($tag);
198 $this->getEntityManager()->persist($entry);
199
200 $this->getEntityManager()->flush();
201 $this->getEntityManager()->clear();
202
203 // We make a first request to set an history and test redirection after tag deletion
204 $crawler = $client->request('GET', '/tag/list');
205 $form = $crawler->filter('#tag-' . $tag->getId() . ' form')->form();
206
207 $data = [
208 'tag[label]' => $newTagLabel,
209 ];
210
211 $client->submit($form, $data);
212 $this->assertSame(302, $client->getResponse()->getStatusCode());
213
214 $freshEntry = $client->getContainer()
215 ->get('doctrine.orm.entity_manager')
216 ->getRepository('WallabagCoreBundle:Entry')
217 ->find($entry->getId());
218
219 $freshEntry2 = $client->getContainer()
220 ->get('doctrine.orm.entity_manager')
221 ->getRepository('WallabagCoreBundle:Entry')
222 ->find($entry2->getId());
223
224 $tags = [];
225
226 $tagsFromEntry = $freshEntry->getTags()->toArray();
227 foreach ($tagsFromEntry as $key => $item) {
228 $tags[$key] = $item->getLabel();
229 }
230
231 $tagsFromEntry2 = $freshEntry2->getTags()->toArray();
232 foreach ($tagsFromEntry2 as $key => $item) {
233 $tags[$key] = $item->getLabel();
234 }
235
236 $this->assertFalse(array_search($tag->getLabel(), $tags, true), 'Previous tag is not attach to entries anymore.');
237
238 $newTag = $client->getContainer()
239 ->get('doctrine.orm.entity_manager')
240 ->getRepository('WallabagCoreBundle:Tag')
241 ->findByLabel($newTagLabel);
242
243 $this->assertCount(1, $newTag, 'New tag exists.');
244
245 $this->assertTrue($newTag[0]->hasEntry($freshEntry), 'New tag is assigned to the entry.');
246 $this->assertTrue($newTag[0]->hasEntry($freshEntry2), 'New tag is assigned to the entry2.');
247 }
248
249 public function testAddUnicodeTagLabel()
250 {
251 $this->logInAs('admin');
252 $client = $this->getClient();
253
254 $entry = new Entry($this->getLoggedInUser());
255 $entry->setUrl('http://0.0.0.0/tag-caché');
256 $this->getEntityManager()->persist($entry);
257 $this->getEntityManager()->flush();
258 $this->getEntityManager()->clear();
259
260 $crawler = $client->request('GET', '/view/' . $entry->getId());
261
262 $form = $crawler->filter('form[name=tag]')->form();
263
264 $data = [
265 'tag[label]' => 'cache',
266 ];
267
268 $client->submit($form, $data);
269
270 $crawler = $client->request('GET', '/view/' . $entry->getId());
271
272 $form = $crawler->filter('form[name=tag]')->form();
273
274 $data = [
275 'tag[label]' => 'caché',
276 ];
277
278 $client->submit($form, $data);
279
280 $newEntry = $client->getContainer()
281 ->get('doctrine.orm.entity_manager')
282 ->getRepository('WallabagCoreBundle:Entry')
283 ->find($entry->getId());
284
285 $tags = $newEntry->getTags()->toArray();
286 foreach ($tags as $key => $tag) {
287 $tags[$key] = $tag->getLabel();
288 }
289
290 $this->assertGreaterThanOrEqual(2, \count($tags));
291 $this->assertNotFalse(array_search('cache', $tags, true), 'Tag cache is assigned to the entry');
292 $this->assertNotFalse(array_search('caché', $tags, true), 'Tag caché is assigned to the entry');
293 }
294 }