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 testAddUnicodeTagLabel()
309 $this->logInAs('admin');
310 $client = $this->getClient();
312 $entry = new Entry($this->getLoggedInUser());
313 $entry->setUrl('http://0.0.0.0/tag-caché');
314 $this->getEntityManager()->persist($entry);
315 $this->getEntityManager()->flush();
316 $this->getEntityManager()->clear();
318 $crawler = $client->request('GET', '/view/' . $entry->getId());
320 $form = $crawler->filter('form[name=tag]')->form();
323 'tag[label]' => 'cache',
326 $client->submit($form, $data);
328 $crawler = $client->request('GET', '/view/' . $entry->getId());
330 $form = $crawler->filter('form[name=tag]')->form();
333 'tag[label]' => 'caché',
336 $client->submit($form, $data);
338 $newEntry = $client->getContainer()
339 ->get('doctrine.orm.entity_manager')
340 ->getRepository('WallabagCoreBundle:Entry')
341 ->find($entry->getId());
343 $tags = $newEntry->getTags()->toArray();
344 foreach ($tags as $key => $tag) {
345 $tags[$key] = $tag->getLabel();
348 $this->assertGreaterThanOrEqual(2, \
count($tags));
349 $this->assertNotFalse(array_search('cache', $tags, true), 'Tag cache is assigned to the entry');
350 $this->assertNotFalse(array_search('caché', $tags, true), 'Tag caché is assigned to the entry');