]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/Controller/TagControllerTest.php
TagController: support merging labels when renaming one with label of another
[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 $crawler = $client->followRedirect();
215
216 $this->assertContains('flashes.tag.notice.tag_renamed', $crawler->filter('body')->extract(['_text'])[0]);
217
218 $freshEntry = $client->getContainer()
219 ->get('doctrine.orm.entity_manager')
220 ->getRepository('WallabagCoreBundle:Entry')
221 ->find($entry->getId());
222
223 $freshEntry2 = $client->getContainer()
224 ->get('doctrine.orm.entity_manager')
225 ->getRepository('WallabagCoreBundle:Entry')
226 ->find($entry2->getId());
227
228 $tags = [];
229
230 $tagsFromEntry = $freshEntry->getTags()->toArray();
231 foreach ($tagsFromEntry as $key => $item) {
232 $tags[$key] = $item->getLabel();
233 }
234
235 $tagsFromEntry2 = $freshEntry2->getTags()->toArray();
236 foreach ($tagsFromEntry2 as $key => $item) {
237 $tags[$key] = $item->getLabel();
238 }
239
240 $this->assertFalse(array_search($tag->getLabel(), $tags, true), 'Previous tag is not attach to entries anymore.');
241
242 $newTag = $client->getContainer()
243 ->get('doctrine.orm.entity_manager')
244 ->getRepository('WallabagCoreBundle:Tag')
245 ->findByLabel($newTagLabel);
246
247 $this->assertCount(1, $newTag, 'New tag exists.');
248
249 $this->assertTrue($newTag[0]->hasEntry($freshEntry), 'New tag is assigned to the entry.');
250 $this->assertTrue($newTag[0]->hasEntry($freshEntry2), 'New tag is assigned to the entry2.');
251 }
252
253 public function testRenameTagWithSameLabel()
254 {
255 $tagLabel = 'same label';
256 $this->logInAs('admin');
257 $client = $this->getClient();
258
259 $tag = new Tag();
260 $tag->setLabel($tagLabel);
261
262 $entry = new Entry($this->getLoggedInUser());
263 $entry->setUrl('http://0.0.0.0/foobar');
264 $entry->addTag($tag);
265 $this->getEntityManager()->persist($entry);
266
267 $this->getEntityManager()->flush();
268 $this->getEntityManager()->clear();
269
270 // We make a first request to set an history and test redirection after tag deletion
271 $crawler = $client->request('GET', '/tag/list');
272 $form = $crawler->filter('#tag-' . $tag->getId() . ' form')->form();
273
274 $data = [
275 'tag[label]' => $tagLabel,
276 ];
277
278 $client->submit($form, $data);
279 $this->assertSame(302, $client->getResponse()->getStatusCode());
280 $this->assertNotContains('flashes.tag.notice.tag_renamed', $crawler->filter('body')->extract(['_text'])[0]);
281
282 $freshEntry = $client->getContainer()
283 ->get('doctrine.orm.entity_manager')
284 ->getRepository('WallabagCoreBundle:Entry')
285 ->find($entry->getId());
286
287 $tags = [];
288
289 $tagsFromEntry = $freshEntry->getTags()->toArray();
290 foreach ($tagsFromEntry as $key => $item) {
291 $tags[$key] = $item->getLabel();
292 }
293
294 $this->assertNotFalse(array_search($tag->getLabel(), $tags, true), 'Tag is still assigned to the entry.');
295
296 $newTag = $client->getContainer()
297 ->get('doctrine.orm.entity_manager')
298 ->getRepository('WallabagCoreBundle:Tag')
299 ->findByLabel($tagLabel);
300
301 $this->assertCount(1, $newTag);
302 $this->assertSame($tag->getId(), $newTag[0]->getId(), 'Tag is unchanged.');
303
304 $this->assertTrue($newTag[0]->hasEntry($freshEntry), 'Tag is still assigned to the entry.');
305 }
306
307 public function testRenameTagWithSameLabelDifferentCase()
308 {
309 $tagLabel = 'same label';
310 $newTagLabel = 'saMe labEl';
311 $this->logInAs('admin');
312 $client = $this->getClient();
313
314 $tag = new Tag();
315 $tag->setLabel($tagLabel);
316
317 $entry = new Entry($this->getLoggedInUser());
318 $entry->setUrl('http://0.0.0.0/foobar');
319 $entry->addTag($tag);
320 $this->getEntityManager()->persist($entry);
321
322 $this->getEntityManager()->flush();
323 $this->getEntityManager()->clear();
324
325 // We make a first request to set an history and test redirection after tag deletion
326 $crawler = $client->request('GET', '/tag/list');
327 $form = $crawler->filter('#tag-' . $tag->getId() . ' form')->form();
328
329 $data = [
330 'tag[label]' => $newTagLabel,
331 ];
332
333 $client->submit($form, $data);
334 $this->assertSame(302, $client->getResponse()->getStatusCode());
335 $this->assertNotContains('flashes.tag.notice.tag_renamed', $crawler->filter('body')->extract(['_text'])[0]);
336
337 $freshEntry = $client->getContainer()
338 ->get('doctrine.orm.entity_manager')
339 ->getRepository('WallabagCoreBundle:Entry')
340 ->find($entry->getId());
341
342 $tags = [];
343
344 $tagsFromEntry = $freshEntry->getTags()->toArray();
345 foreach ($tagsFromEntry as $key => $item) {
346 $tags[$key] = $item->getLabel();
347 }
348
349 $this->assertFalse(array_search($newTagLabel, $tags, true));
350
351 $tagFromRepo = $client->getContainer()
352 ->get('doctrine.orm.entity_manager')
353 ->getRepository('WallabagCoreBundle:Tag')
354 ->findByLabel($tagLabel);
355
356 $newTagFromRepo = $client->getContainer()
357 ->get('doctrine.orm.entity_manager')
358 ->getRepository('WallabagCoreBundle:Tag')
359 ->findByLabel($newTagLabel);
360
361 $this->assertCount(0, $newTagFromRepo);
362 $this->assertCount(1, $tagFromRepo);
363
364 $this->assertSame($tag->getId(), $tagFromRepo[0]->getId(), 'Tag is unchanged.');
365
366 $this->assertTrue($tagFromRepo[0]->hasEntry($freshEntry), 'Tag is still assigned to the entry.');
367 }
368
369 public function testRenameTagWithExistingLabel()
370 {
371 $tagLabel = 'existing label';
372 $previousTagLabel = 'previous label';
373 $this->logInAs('admin');
374 $client = $this->getClient();
375
376 $tag = new Tag();
377 $tag->setLabel($tagLabel);
378
379 $previousTag = new Tag();
380 $previousTag->setLabel($previousTagLabel);
381
382 $entry1 = new Entry($this->getLoggedInUser());
383 $entry1->setUrl('http://0.0.0.0/foobar');
384 $entry1->addTag($previousTag);
385 $this->getEntityManager()->persist($entry1);
386
387 $entry2 = new Entry($this->getLoggedInUser());
388 $entry2->setUrl('http://0.0.0.0/baz');
389 $entry2->addTag($tag);
390 $this->getEntityManager()->persist($entry2);
391
392 $this->getEntityManager()->flush();
393 $this->getEntityManager()->clear();
394
395 // We make a first request to set an history and test redirection after tag deletion
396 $crawler = $client->request('GET', '/tag/list');
397 $form = $crawler->filter('#tag-' . $previousTag->getId() . ' form')->form();
398
399 $data = [
400 'tag[label]' => $tagLabel,
401 ];
402
403 $client->submit($form, $data);
404 $this->assertSame(302, $client->getResponse()->getStatusCode());
405 $this->assertNotContains('flashes.tag.notice.tag_renamed', $crawler->filter('body')->extract(['_text'])[0]);
406
407 $freshEntry1 = $client->getContainer()
408 ->get('doctrine.orm.entity_manager')
409 ->getRepository('WallabagCoreBundle:Entry')
410 ->find($entry1->getId());
411
412 $freshEntry2 = $client->getContainer()
413 ->get('doctrine.orm.entity_manager')
414 ->getRepository('WallabagCoreBundle:Entry')
415 ->find($entry2->getId());
416
417 $tagFromRepo = $client->getContainer()
418 ->get('doctrine.orm.entity_manager')
419 ->getRepository('WallabagCoreBundle:Tag')
420 ->findByLabel($tagLabel);
421
422 $previousTagFromRepo = $client->getContainer()
423 ->get('doctrine.orm.entity_manager')
424 ->getRepository('WallabagCoreBundle:Tag')
425 ->findByLabel($previousTagLabel);
426
427 $this->assertCount(1, $tagFromRepo);
428
429 $this->assertTrue($tagFromRepo[0]->hasEntry($freshEntry1));
430 $this->assertTrue($tagFromRepo[0]->hasEntry($freshEntry2), 'Tag is assigned to the entry.');
431 $this->assertFalse($previousTagFromRepo[0]->hasEntry($freshEntry1));
432 }
433
434 public function testAddUnicodeTagLabel()
435 {
436 $this->logInAs('admin');
437 $client = $this->getClient();
438
439 $entry = new Entry($this->getLoggedInUser());
440 $entry->setUrl('http://0.0.0.0/tag-caché');
441 $this->getEntityManager()->persist($entry);
442 $this->getEntityManager()->flush();
443 $this->getEntityManager()->clear();
444
445 $crawler = $client->request('GET', '/view/' . $entry->getId());
446
447 $form = $crawler->filter('form[name=tag]')->form();
448
449 $data = [
450 'tag[label]' => 'cache',
451 ];
452
453 $client->submit($form, $data);
454
455 $crawler = $client->request('GET', '/view/' . $entry->getId());
456
457 $form = $crawler->filter('form[name=tag]')->form();
458
459 $data = [
460 'tag[label]' => 'caché',
461 ];
462
463 $client->submit($form, $data);
464
465 $newEntry = $client->getContainer()
466 ->get('doctrine.orm.entity_manager')
467 ->getRepository('WallabagCoreBundle:Entry')
468 ->find($entry->getId());
469
470 $tags = $newEntry->getTags()->toArray();
471 foreach ($tags as $key => $tag) {
472 $tags[$key] = $tag->getLabel();
473 }
474
475 $this->assertGreaterThanOrEqual(2, \count($tags));
476 $this->assertNotFalse(array_search('cache', $tags, true), 'Tag cache is assigned to the entry');
477 $this->assertNotFalse(array_search('caché', $tags, true), 'Tag caché is assigned to the entry');
478 }
479 }