]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/CoreBundle/Controller/TagControllerTest.php
mysql: change collation of tag table
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Controller / TagControllerTest.php
CommitLineData
d0b90fbe
NL
1<?php
2
23634d5d 3namespace Tests\Wallabag\CoreBundle\Controller;
d0b90fbe 4
23634d5d 5use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
7ab5eb95 6use Wallabag\CoreBundle\Entity\Entry;
ac8cf632 7use Wallabag\CoreBundle\Entity\Tag;
d0b90fbe
NL
8
9class TagControllerTest extends WallabagCoreTestCase
10{
567421af 11 public $tagName = 'opensource';
7036d91f 12 public $caseTagName = 'OpenSource';
567421af 13
d0b90fbe
NL
14 public function testList()
15 {
16 $this->logInAs('admin');
17 $client = $this->getClient();
18
19 $client->request('GET', '/tag/list');
20
f808b016 21 $this->assertSame(200, $client->getResponse()->getStatusCode());
d0b90fbe 22 }
7244d6cb
NL
23
24 public function testAddTagToEntry()
25 {
26 $this->logInAs('admin');
27 $client = $this->getClient();
28
7ab5eb95 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();
7244d6cb 34
f808b016 35 $crawler = $client->request('GET', '/view/' . $entry->getId());
7244d6cb 36
0d42217e 37 $form = $crawler->filter('form[name=tag]')->form();
7244d6cb 38
4094ea47 39 $data = [
7036d91f 40 'tag[label]' => $this->caseTagName,
4094ea47 41 ];
7244d6cb
NL
42
43 $client->submit($form, $data);
f808b016 44 $this->assertSame(302, $client->getResponse()->getStatusCode());
7244d6cb 45
fdc90ceb 46 // be sure to reload the entry
7ab5eb95 47 $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
48 $this->assertCount(1, $entry->getTags());
7036d91f 49 $this->assertContains($this->tagName, $entry->getTags());
7244d6cb 50
084fb0d3 51 // tag already exists and already assigned
7244d6cb 52 $client->submit($form, $data);
f808b016 53 $this->assertSame(302, $client->getResponse()->getStatusCode());
7244d6cb 54
7ab5eb95 55 $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
56 $this->assertCount(1, $entry->getTags());
7244d6cb 57
084fb0d3 58 // tag already exists but still not assigned to this entry
4094ea47 59 $data = [
c8de7ab9 60 'tag[label]' => 'foo bar',
4094ea47 61 ];
7244d6cb
NL
62
63 $client->submit($form, $data);
f808b016 64 $this->assertSame(302, $client->getResponse()->getStatusCode());
7244d6cb 65
7ab5eb95 66 $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
67 $this->assertCount(2, $entry->getTags());
7244d6cb 68 }
567421af 69
2baca964
JB
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')
74e1f743 78 ->findByUrlAndUserId('http://0.0.0.0/entry2', $this->getLoggedInUserId());
2baca964 79
f808b016 80 $crawler = $client->request('GET', '/view/' . $entry->getId());
2baca964
JB
81
82 $form = $crawler->filter('form[name=tag]')->form();
83
4094ea47 84 $data = [
7036d91f 85 'tag[label]' => 'foo2, Bar2',
4094ea47 86 ];
2baca964
JB
87
88 $client->submit($form, $data);
f808b016 89 $this->assertSame(302, $client->getResponse()->getStatusCode());
2baca964
JB
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();
74e1f743
JB
97 foreach ($tags as $key => $tag) {
98 $tags[$key] = $tag->getLabel();
99 }
100
2a1ceb67 101 $this->assertGreaterThanOrEqual(2, \count($tags));
f808b016
JB
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');
2baca964
JB
104 }
105
567421af
TC
106 public function testRemoveTagFromEntry()
107 {
108 $this->logInAs('admin');
109 $client = $this->getClient();
110
7ab5eb95 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();
567421af 119
5dbf3f23 120 // We make a first request to set an history and test redirection after tag deletion
f808b016 121 $client->request('GET', '/view/' . $entry->getId());
5dbf3f23 122 $entryUri = $client->getRequest()->getUri();
f808b016 123 $client->request('GET', '/remove-tag/' . $entry->getId() . '/' . $tag->getId());
567421af 124
f808b016
JB
125 $this->assertSame(302, $client->getResponse()->getStatusCode());
126 $this->assertSame($entryUri, $client->getResponse()->getTargetUrl());
567421af 127
6acadf8e 128 // re-retrieve the entry to be sure to get fresh data from database (mostly for tags)
7ab5eb95 129 $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
e686a76d
TC
130 $this->assertNotContains($this->tagName, $entry->getTags());
131
f808b016 132 $client->request('GET', '/remove-tag/' . $entry->getId() . '/' . $tag->getId());
567421af 133
f808b016 134 $this->assertSame(404, $client->getResponse()->getStatusCode());
ac8cf632
JB
135
136 $tag = $client->getContainer()
137 ->get('doctrine.orm.entity_manager')
138 ->getRepository('WallabagCoreBundle:Tag')
139 ->findOneByLabel($this->tagName);
140
f808b016 141 $this->assertNull($tag, $this->tagName . ' was removed because it begun an orphan tag');
567421af 142 }
267e8d63
NL
143
144 public function testShowEntriesForTagAction()
145 {
146 $this->logInAs('admin');
147 $client = $this->getClient();
ac8cf632
JB
148 $em = $client->getContainer()
149 ->get('doctrine.orm.entity_manager');
150
151 $tag = new Tag();
152 $tag->setLabel($this->tagName);
267e8d63
NL
153
154 $entry = $client->getContainer()
155 ->get('doctrine.orm.entity_manager')
156 ->getRepository('WallabagCoreBundle:Entry')
74e1f743 157 ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getLoggedInUserId());
267e8d63 158
ac8cf632 159 $tag->addEntry($entry);
267e8d63 160
ac8cf632
JB
161 $em->persist($entry);
162 $em->persist($tag);
163 $em->flush();
267e8d63
NL
164
165 $tag = $client->getContainer()
166 ->get('doctrine.orm.entity_manager')
167 ->getRepository('WallabagCoreBundle:Tag')
ac8cf632 168 ->findOneByEntryAndTagLabel($entry, $this->tagName);
267e8d63 169
f808b016 170 $crawler = $client->request('GET', '/tag/list/' . $tag->getSlug());
267e8d63 171
f808b016 172 $this->assertSame(200, $client->getResponse()->getStatusCode());
ac8cf632
JB
173 $this->assertCount(1, $crawler->filter('[id*="entry-"]'));
174
175 $entry->removeTag($tag);
176 $em->remove($tag);
177 $em->flush();
267e8d63 178 }
03b2058d
SH
179
180 public function testRenameTagUsingTheFormInsideTagList()
181 {
182 $this->logInAs('admin');
183 $client = $this->getClient();
184
185 $tag = new Tag();
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();
193
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();
197
198 $data = [
199 'tag[label]' => 'specific label',
200 ];
201
202 $client->submit($form, $data);
203 $this->assertSame(302, $client->getResponse()->getStatusCode());
204
205 $freshEntry = $client->getContainer()
206 ->get('doctrine.orm.entity_manager')
207 ->getRepository('WallabagCoreBundle:Entry')
208 ->find($entry->getId());
209
210 $tags = $freshEntry->getTags()->toArray();
211 foreach ($tags as $key => $item) {
212 $tags[$key] = $item->getLabel();
213 }
214
215 $this->assertFalse(array_search($tag->getLabel(), $tags, true), 'Previous tag is not attach to entry anymore.');
216
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.');
223 }
feb239ea
KD
224
225 public function testAddUnicodeTagLabel()
226 {
227 $this->logInAs('admin');
228 $client = $this->getClient();
229
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();
235
236 $crawler = $client->request('GET', '/view/' . $entry->getId());
237
238 $form = $crawler->filter('form[name=tag]')->form();
239
240 $data = [
241 'tag[label]' => 'cache',
242 ];
243
244 $client->submit($form, $data);
245
246 $crawler = $client->request('GET', '/view/' . $entry->getId());
247
248 $form = $crawler->filter('form[name=tag]')->form();
249
250 $data = [
251 'tag[label]' => 'caché',
252 ];
253
254 $client->submit($form, $data);
255
256 $newEntry = $client->getContainer()
257 ->get('doctrine.orm.entity_manager')
258 ->getRepository('WallabagCoreBundle:Entry')
259 ->find($entry->getId());
260
261 $tags = $newEntry->getTags()->toArray();
262 foreach ($tags as $key => $tag) {
263 $tags[$key] = $tag->getLabel();
264 }
265
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');
269 }
d0b90fbe 270}