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 $freshEntry = $client->getContainer()
215 ->get('doctrine.orm.entity_manager')
216 ->getRepository('WallabagCoreBundle:Entry')
217 ->find($entry->getId());
219 $freshEntry2 = $client->getContainer()
220 ->get('doctrine.orm.entity_manager')
221 ->getRepository('WallabagCoreBundle:Entry')
222 ->find($entry2->getId());
226 $tagsFromEntry = $freshEntry->getTags()->toArray();
227 foreach ($tagsFromEntry as $key => $item) {
228 $tags[$key] = $item->getLabel();
231 $tagsFromEntry2 = $freshEntry2->getTags()->toArray();
232 foreach ($tagsFromEntry2 as $key => $item) {
233 $tags[$key] = $item->getLabel();
236 $this->assertFalse(array_search($tag->getLabel(), $tags, true), 'Previous tag is not attach to entries anymore.');
238 $newTag = $client->getContainer()
239 ->get('doctrine.orm.entity_manager')
240 ->getRepository('WallabagCoreBundle:Tag')
241 ->findByLabel($newTagLabel);
243 $this->assertCount(1, $newTag, 'New tag exists.');
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.');
249 public function testAddUnicodeTagLabel()
251 $this->logInAs('admin');
252 $client = $this->getClient();
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();
260 $crawler = $client->request('GET', '/view/' . $entry->getId());
262 $form = $crawler->filter('form[name=tag]')->form();
265 'tag[label]' => 'cache',
268 $client->submit($form, $data);
270 $crawler = $client->request('GET', '/view/' . $entry->getId());
272 $form = $crawler->filter('form[name=tag]')->form();
275 'tag[label]' => 'caché',
278 $client->submit($form, $data);
280 $newEntry = $client->getContainer()
281 ->get('doctrine.orm.entity_manager')
282 ->getRepository('WallabagCoreBundle:Entry')
283 ->find($entry->getId());
285 $tags = $newEntry->getTags()->toArray();
286 foreach ($tags as $key => $tag) {
287 $tags[$key] = $tag->getLabel();
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');