3 namespace Tests\Wallabag\CoreBundle\Controller
;
5 use Tests\Wallabag\CoreBundle\WallabagCoreTestCase
;
6 use Wallabag\CoreBundle\Entity\Entry
;
7 use Wallabag\CoreBundle\Entity\Tag
;
9 class TagControllerTest
extends WallabagCoreTestCase
11 public $tagName = 'opensource';
12 public $caseTagName = 'OpenSource';
14 public function testList()
16 $this->logInAs('admin');
17 $client = $this->getClient();
19 $client->request('GET', '/tag/list');
21 $this->assertSame(200, $client->getResponse()->getStatusCode());
24 public function testAddTagToEntry()
26 $this->logInAs('admin');
27 $client = $this->getClient();
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();
35 $crawler = $client->request('GET', '/view/' . $entry->getId());
37 $form = $crawler->filter('form[name=tag]')->form();
40 'tag[label]' => $this->caseTagName
,
43 $client->submit($form, $data);
44 $this->assertSame(302, $client->getResponse()->getStatusCode());
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());
51 // tag already exists and already assigned
52 $client->submit($form, $data);
53 $this->assertSame(302, $client->getResponse()->getStatusCode());
55 $entry = $this->getEntityManager()->getRepository(Entry
::class)->find($entry->getId());
56 $this->assertCount(1, $entry->getTags());
58 // tag already exists but still not assigned to this entry
60 'tag[label]' => 'foo bar',
63 $client->submit($form, $data);
64 $this->assertSame(302, $client->getResponse()->getStatusCode());
66 $entry = $this->getEntityManager()->getRepository(Entry
::class)->find($entry->getId());
67 $this->assertCount(2, $entry->getTags());
70 public function testAddMultipleTagToEntry()
72 $this->logInAs('admin');
73 $client = $this->getClient();
75 $entry = $client->getContainer()
76 ->get('doctrine.orm.entity_manager')
77 ->getRepository('WallabagCoreBundle:Entry')
78 ->findByUrlAndUserId('http://0.0.0.0/entry2', $this->getLoggedInUserId());
80 $crawler = $client->request('GET', '/view/' . $entry->getId());
82 $form = $crawler->filter('form[name=tag]')->form();
85 'tag[label]' => 'foo2, Bar2',
88 $client->submit($form, $data);
89 $this->assertSame(302, $client->getResponse()->getStatusCode());
91 $newEntry = $client->getContainer()
92 ->get('doctrine.orm.entity_manager')
93 ->getRepository('WallabagCoreBundle:Entry')
94 ->find($entry->getId());
96 $tags = $newEntry->getTags()->toArray();
97 foreach ($tags as $key => $tag) {
98 $tags[$key] = $tag->getLabel();
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');
106 public function testRemoveTagFromEntry()
108 $this->logInAs('admin');
109 $client = $this->getClient();
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();
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());
125 $this->assertSame(302, $client->getResponse()->getStatusCode());
126 $this->assertSame($entryUri, $client->getResponse()->getTargetUrl());
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());
132 $client->request('GET', '/remove-tag/' . $entry->getId() . '/' . $tag->getId());
134 $this->assertSame(404, $client->getResponse()->getStatusCode());
136 $tag = $client->getContainer()
137 ->get('doctrine.orm.entity_manager')
138 ->getRepository('WallabagCoreBundle:Tag')
139 ->findOneByLabel($this->tagName
);
141 $this->assertNull($tag, $this->tagName
. ' was removed because it begun an orphan tag');
144 public function testShowEntriesForTagAction()
146 $this->logInAs('admin');
147 $client = $this->getClient();
148 $em = $client->getContainer()
149 ->get('doctrine.orm.entity_manager');
152 $tag->setLabel($this->tagName
);
154 $entry = $client->getContainer()
155 ->get('doctrine.orm.entity_manager')
156 ->getRepository('WallabagCoreBundle:Entry')
157 ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getLoggedInUserId());
159 $tag->addEntry($entry);
161 $em->persist($entry);
165 $tag = $client->getContainer()
166 ->get('doctrine.orm.entity_manager')
167 ->getRepository('WallabagCoreBundle:Tag')
168 ->findOneByEntryAndTagLabel($entry, $this->tagName
);
170 $crawler = $client->request('GET', '/tag/list/' . $tag->getSlug());
172 $this->assertSame(200, $client->getResponse()->getStatusCode());
173 $this->assertCount(1, $crawler->filter('[id*="entry-"]'));
175 $entry->removeTag($tag);
180 public function testRenameTagUsingTheFormInsideTagList()
182 $newTagLabel = 'rename label';
184 $this->logInAs('admin');
185 $client = $this->getClient();
188 $tag->setLabel($this->tagName
);
190 $entry = new Entry($this->getLoggedInUser());
191 $entry->setUrl('http://0.0.0.0/foo');
192 $entry->addTag($tag);
193 $this->getEntityManager()->persist($entry);
195 $entry2 = new Entry($this->getLoggedInUser());
196 $entry2->setUrl('http://0.0.0.0/bar');
197 $entry2->addTag($tag);
198 $this->getEntityManager()->persist($entry);
200 $this->getEntityManager()->flush();
201 $this->getEntityManager()->clear();
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();
208 'tag[label]' => $newTagLabel,
211 $client->submit($form, $data);
212 $this->assertSame(302, $client->getResponse()->getStatusCode());
214 $crawler = $client->followRedirect();
216 $this->assertContains('flashes.tag.notice.tag_renamed', $crawler->filter('body')->extract(['_text'])[0]);
218 $freshEntry = $client->getContainer()
219 ->get('doctrine.orm.entity_manager')
220 ->getRepository('WallabagCoreBundle:Entry')
221 ->find($entry->getId());
223 $freshEntry2 = $client->getContainer()
224 ->get('doctrine.orm.entity_manager')
225 ->getRepository('WallabagCoreBundle:Entry')
226 ->find($entry2->getId());
230 $tagsFromEntry = $freshEntry->getTags()->toArray();
231 foreach ($tagsFromEntry as $key => $item) {
232 $tags[$key] = $item->getLabel();
235 $tagsFromEntry2 = $freshEntry2->getTags()->toArray();
236 foreach ($tagsFromEntry2 as $key => $item) {
237 $tags[$key] = $item->getLabel();
240 $this->assertFalse(array_search($tag->getLabel(), $tags, true), 'Previous tag is not attach to entries anymore.');
242 $newTag = $client->getContainer()
243 ->get('doctrine.orm.entity_manager')
244 ->getRepository('WallabagCoreBundle:Tag')
245 ->findByLabel($newTagLabel);
247 $this->assertCount(1, $newTag, 'New tag exists.');
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.');
253 public function testRenameTagWithSameLabel()
255 $tagLabel = 'same label';
256 $this->logInAs('admin');
257 $client = $this->getClient();
260 $tag->setLabel($tagLabel);
262 $entry = new Entry($this->getLoggedInUser());
263 $entry->setUrl('http://0.0.0.0/foobar');
264 $entry->addTag($tag);
265 $this->getEntityManager()->persist($entry);
267 $this->getEntityManager()->flush();
268 $this->getEntityManager()->clear();
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();
275 'tag[label]' => $tagLabel,
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]);
282 $freshEntry = $client->getContainer()
283 ->get('doctrine.orm.entity_manager')
284 ->getRepository('WallabagCoreBundle:Entry')
285 ->find($entry->getId());
289 $tagsFromEntry = $freshEntry->getTags()->toArray();
290 foreach ($tagsFromEntry as $key => $item) {
291 $tags[$key] = $item->getLabel();
294 $this->assertNotFalse(array_search($tag->getLabel(), $tags, true), 'Tag is still assigned to the entry.');
296 $newTag = $client->getContainer()
297 ->get('doctrine.orm.entity_manager')
298 ->getRepository('WallabagCoreBundle:Tag')
299 ->findByLabel($tagLabel);
301 $this->assertCount(1, $newTag);
302 $this->assertSame($tag->getId(), $newTag[0]->getId(), 'Tag is unchanged.');
304 $this->assertTrue($newTag[0]->hasEntry($freshEntry), 'Tag is still assigned to the entry.');
307 public function testRenameTagWithSameLabelDifferentCase()
309 $tagLabel = 'same label';
310 $newTagLabel = 'saMe labEl';
311 $this->logInAs('admin');
312 $client = $this->getClient();
315 $tag->setLabel($tagLabel);
317 $entry = new Entry($this->getLoggedInUser());
318 $entry->setUrl('http://0.0.0.0/foobar');
319 $entry->addTag($tag);
320 $this->getEntityManager()->persist($entry);
322 $this->getEntityManager()->flush();
323 $this->getEntityManager()->clear();
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();
330 'tag[label]' => $newTagLabel,
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]);
337 $freshEntry = $client->getContainer()
338 ->get('doctrine.orm.entity_manager')
339 ->getRepository('WallabagCoreBundle:Entry')
340 ->find($entry->getId());
344 $tagsFromEntry = $freshEntry->getTags()->toArray();
345 foreach ($tagsFromEntry as $key => $item) {
346 $tags[$key] = $item->getLabel();
349 $this->assertFalse(array_search($newTagLabel, $tags, true));
351 $tagFromRepo = $client->getContainer()
352 ->get('doctrine.orm.entity_manager')
353 ->getRepository('WallabagCoreBundle:Tag')
354 ->findByLabel($tagLabel);
356 $newTagFromRepo = $client->getContainer()
357 ->get('doctrine.orm.entity_manager')
358 ->getRepository('WallabagCoreBundle:Tag')
359 ->findByLabel($newTagLabel);
361 $this->assertCount(0, $newTagFromRepo);
362 $this->assertCount(1, $tagFromRepo);
364 $this->assertSame($tag->getId(), $tagFromRepo[0]->getId(), 'Tag is unchanged.');
366 $this->assertTrue($tagFromRepo[0]->hasEntry($freshEntry), 'Tag is still assigned to the entry.');
369 public function testRenameTagWithExistingLabel()
371 $tagLabel = 'existing label';
372 $previousTagLabel = 'previous label';
373 $this->logInAs('admin');
374 $client = $this->getClient();
377 $tag->setLabel($tagLabel);
379 $previousTag = new Tag();
380 $previousTag->setLabel($previousTagLabel);
382 $entry1 = new Entry($this->getLoggedInUser());
383 $entry1->setUrl('http://0.0.0.0/foobar');
384 $entry1->addTag($previousTag);
385 $this->getEntityManager()->persist($entry1);
387 $entry2 = new Entry($this->getLoggedInUser());
388 $entry2->setUrl('http://0.0.0.0/baz');
389 $entry2->addTag($tag);
390 $this->getEntityManager()->persist($entry2);
392 $this->getEntityManager()->flush();
393 $this->getEntityManager()->clear();
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();
400 'tag[label]' => $tagLabel,
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]);
407 $freshEntry1 = $client->getContainer()
408 ->get('doctrine.orm.entity_manager')
409 ->getRepository('WallabagCoreBundle:Entry')
410 ->find($entry1->getId());
412 $freshEntry2 = $client->getContainer()
413 ->get('doctrine.orm.entity_manager')
414 ->getRepository('WallabagCoreBundle:Entry')
415 ->find($entry2->getId());
417 $tagFromRepo = $client->getContainer()
418 ->get('doctrine.orm.entity_manager')
419 ->getRepository('WallabagCoreBundle:Tag')
420 ->findByLabel($tagLabel);
422 $previousTagFromRepo = $client->getContainer()
423 ->get('doctrine.orm.entity_manager')
424 ->getRepository('WallabagCoreBundle:Tag')
425 ->findByLabel($previousTagLabel);
427 $this->assertCount(1, $tagFromRepo);
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));
434 public function testAddUnicodeTagLabel()
436 $this->logInAs('admin');
437 $client = $this->getClient();
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();
445 $crawler = $client->request('GET', '/view/' . $entry->getId());
447 $form = $crawler->filter('form[name=tag]')->form();
450 'tag[label]' => 'cache',
453 $client->submit($form, $data);
455 $crawler = $client->request('GET', '/view/' . $entry->getId());
457 $form = $crawler->filter('form[name=tag]')->form();
460 'tag[label]' => 'caché',
463 $client->submit($form, $data);
465 $newEntry = $client->getContainer()
466 ->get('doctrine.orm.entity_manager')
467 ->getRepository('WallabagCoreBundle:Entry')
468 ->find($entry->getId());
470 $tags = $newEntry->getTags()->toArray();
471 foreach ($tags as $key => $tag) {
472 $tags[$key] = $tag->getLabel();
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');