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 $this->logInAs('admin');
183 $client = $this->getClient();
186 $tag->setLabel($this->tagName
);
187 $entry = new Entry($this->getLoggedInUser());
188 $entry->setUrl('http://0.0.0.0/foo');
189 $entry->addTag($tag);
190 $this->getEntityManager()->persist($entry);
191 $this->getEntityManager()->flush();
192 $this->getEntityManager()->clear();
194 // We make a first request to set an history and test redirection after tag deletion
195 $crawler = $client->request('GET', '/tag/list');
196 $form = $crawler->filter('#tag-' . $tag->getId() . ' form')->form();
199 'tag[label]' => 'specific label',
202 $client->submit($form, $data);
203 $this->assertSame(302, $client->getResponse()->getStatusCode());
205 $freshEntry = $client->getContainer()
206 ->get('doctrine.orm.entity_manager')
207 ->getRepository('WallabagCoreBundle:Entry')
208 ->find($entry->getId());
210 $tags = $freshEntry->getTags()->toArray();
211 foreach ($tags as $key => $item) {
212 $tags[$key] = $item->getLabel();
215 $this->assertFalse(array_search($tag->getLabel(), $tags, true), 'Previous tag is not attach to entry anymore.');
217 $newTag = $client->getContainer()
218 ->get('doctrine.orm.entity_manager')
219 ->getRepository('WallabagCoreBundle:Tag')
220 ->findOneByLabel('specific label');
221 $this->assertInstanceOf(Tag
::class, $newTag, 'Tag "specific label" exists.');
222 $this->assertTrue($newTag->hasEntry($freshEntry), 'Tag "specific label" is assigned to the entry.');
225 public function testAddUnicodeTagLabel()
227 $this->logInAs('admin');
228 $client = $this->getClient();
230 $entry = new Entry($this->getLoggedInUser());
231 $entry->setUrl('http://0.0.0.0/tag-caché');
232 $this->getEntityManager()->persist($entry);
233 $this->getEntityManager()->flush();
234 $this->getEntityManager()->clear();
236 $crawler = $client->request('GET', '/view/' . $entry->getId());
238 $form = $crawler->filter('form[name=tag]')->form();
241 'tag[label]' => 'cache',
244 $client->submit($form, $data);
246 $crawler = $client->request('GET', '/view/' . $entry->getId());
248 $form = $crawler->filter('form[name=tag]')->form();
251 'tag[label]' => 'caché',
254 $client->submit($form, $data);
256 $newEntry = $client->getContainer()
257 ->get('doctrine.orm.entity_manager')
258 ->getRepository('WallabagCoreBundle:Entry')
259 ->find($entry->getId());
261 $tags = $newEntry->getTags()->toArray();
262 foreach ($tags as $key => $tag) {
263 $tags[$key] = $tag->getLabel();
266 $this->assertGreaterThanOrEqual(2, \
count($tags));
267 $this->assertNotFalse(array_search('cache', $tags, true), 'Tag cache is assigned to the entry');
268 $this->assertNotFalse(array_search('caché', $tags, true), 'Tag caché is assigned to the entry');