aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorKevin Decherf <kevin@kdecherf.com>2020-04-20 18:02:31 +0200
committerGitHub <noreply@github.com>2020-04-20 18:02:31 +0200
commit2ca2ed39fd9bcaa212185ce4434cea44f3d2f4ba (patch)
tree5750b751e51313e95d433a4cc3c8a873e48a1227
parent7443da479f5289c2ceebb96c0cd4273f6445ca37 (diff)
parent48f9a9632d2823be38883628ddfe62344cc282b1 (diff)
downloadwallabag-2ca2ed39fd9bcaa212185ce4434cea44f3d2f4ba.tar.gz
wallabag-2ca2ed39fd9bcaa212185ce4434cea44f3d2f4ba.tar.zst
wallabag-2ca2ed39fd9bcaa212185ce4434cea44f3d2f4ba.zip
Merge pull request #4310 from wallabag/fix/4216
TagController: fix duplicated tags when renaming them
-rw-r--r--src/Wallabag/CoreBundle/Controller/TagController.php33
-rw-r--r--tests/Wallabag/CoreBundle/Controller/TagControllerTest.php223
2 files changed, 239 insertions, 17 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/TagController.php b/src/Wallabag/CoreBundle/Controller/TagController.php
index a6ad131f..16ded948 100644
--- a/src/Wallabag/CoreBundle/Controller/TagController.php
+++ b/src/Wallabag/CoreBundle/Controller/TagController.php
@@ -151,7 +151,22 @@ class TagController extends Controller
151 $form = $this->createForm(RenameTagType::class, new Tag()); 151 $form = $this->createForm(RenameTagType::class, new Tag());
152 $form->handleRequest($request); 152 $form->handleRequest($request);
153 153
154 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'), '', true);
155
154 if ($form->isSubmitted() && $form->isValid()) { 156 if ($form->isSubmitted() && $form->isValid()) {
157 $newTag = new Tag();
158 $newTag->setLabel($form->get('label')->getData());
159
160 if ($newTag->getLabel() === $tag->getLabel()) {
161 return $this->redirect($redirectUrl);
162 }
163
164 $tagFromRepo = $this->get('wallabag_core.tag_repository')->findOneByLabel($newTag->getLabel());
165
166 if (null !== $tagFromRepo) {
167 $newTag = $tagFromRepo;
168 }
169
155 $entries = $this->get('wallabag_core.entry_repository')->findAllByTagId( 170 $entries = $this->get('wallabag_core.entry_repository')->findAllByTagId(
156 $this->getUser()->getId(), 171 $this->getUser()->getId(),
157 $tag->getId() 172 $tag->getId()
@@ -159,21 +174,19 @@ class TagController extends Controller
159 foreach ($entries as $entry) { 174 foreach ($entries as $entry) {
160 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry( 175 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry(
161 $entry, 176 $entry,
162 $form->get('label')->getData() 177 $newTag->getLabel(),
178 [$newTag]
163 ); 179 );
164 $entry->removeTag($tag); 180 $entry->removeTag($tag);
165 } 181 }
166 182
167 $em = $this->getDoctrine()->getManager(); 183 $this->getDoctrine()->getManager()->flush();
168 $em->flush();
169 }
170
171 $this->get('session')->getFlashBag()->add(
172 'notice',
173 'flashes.tag.notice.tag_renamed'
174 );
175 184
176 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'), '', true); 185 $this->get('session')->getFlashBag()->add(
186 'notice',
187 'flashes.tag.notice.tag_renamed'
188 );
189 }
177 190
178 return $this->redirect($redirectUrl); 191 return $this->redirect($redirectUrl);
179 } 192 }
diff --git a/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php b/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php
index 47c83a7b..fb066632 100644
--- a/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php
+++ b/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php
@@ -179,15 +179,91 @@ class TagControllerTest extends WallabagCoreTestCase
179 179
180 public function testRenameTagUsingTheFormInsideTagList() 180 public function testRenameTagUsingTheFormInsideTagList()
181 { 181 {
182 $newTagLabel = 'rename label';
183
182 $this->logInAs('admin'); 184 $this->logInAs('admin');
183 $client = $this->getClient(); 185 $client = $this->getClient();
184 186
185 $tag = new Tag(); 187 $tag = new Tag();
186 $tag->setLabel($this->tagName); 188 $tag->setLabel($this->tagName);
189
187 $entry = new Entry($this->getLoggedInUser()); 190 $entry = new Entry($this->getLoggedInUser());
188 $entry->setUrl('http://0.0.0.0/foo'); 191 $entry->setUrl('http://0.0.0.0/foo');
189 $entry->addTag($tag); 192 $entry->addTag($tag);
190 $this->getEntityManager()->persist($entry); 193 $this->getEntityManager()->persist($entry);
194
195 $entry2 = new Entry($this->getLoggedInUser());
196 $entry2->setUrl('http://0.0.0.0/bar');
197 $entry2->addTag($tag);
198 $this->getEntityManager()->persist($entry);
199
200 $this->getEntityManager()->flush();
201 $this->getEntityManager()->clear();
202
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();
206
207 $data = [
208 'tag[label]' => $newTagLabel,
209 ];
210
211 $client->submit($form, $data);
212 $this->assertSame(302, $client->getResponse()->getStatusCode());
213
214 $crawler = $client->followRedirect();
215
216 $this->assertContains('flashes.tag.notice.tag_renamed', $crawler->filter('body')->extract(['_text'])[0]);
217
218 $freshEntry = $client->getContainer()
219 ->get('doctrine.orm.entity_manager')
220 ->getRepository('WallabagCoreBundle:Entry')
221 ->find($entry->getId());
222
223 $freshEntry2 = $client->getContainer()
224 ->get('doctrine.orm.entity_manager')
225 ->getRepository('WallabagCoreBundle:Entry')
226 ->find($entry2->getId());
227
228 $tags = [];
229
230 $tagsFromEntry = $freshEntry->getTags()->toArray();
231 foreach ($tagsFromEntry as $key => $item) {
232 $tags[$key] = $item->getLabel();
233 }
234
235 $tagsFromEntry2 = $freshEntry2->getTags()->toArray();
236 foreach ($tagsFromEntry2 as $key => $item) {
237 $tags[$key] = $item->getLabel();
238 }
239
240 $this->assertFalse(array_search($tag->getLabel(), $tags, true), 'Previous tag is not attach to entries anymore.');
241
242 $newTag = $client->getContainer()
243 ->get('doctrine.orm.entity_manager')
244 ->getRepository('WallabagCoreBundle:Tag')
245 ->findByLabel($newTagLabel);
246
247 $this->assertCount(1, $newTag, 'New tag exists.');
248
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.');
251 }
252
253 public function testRenameTagWithSameLabel()
254 {
255 $tagLabel = 'same label';
256 $this->logInAs('admin');
257 $client = $this->getClient();
258
259 $tag = new Tag();
260 $tag->setLabel($tagLabel);
261
262 $entry = new Entry($this->getLoggedInUser());
263 $entry->setUrl('http://0.0.0.0/foobar');
264 $entry->addTag($tag);
265 $this->getEntityManager()->persist($entry);
266
191 $this->getEntityManager()->flush(); 267 $this->getEntityManager()->flush();
192 $this->getEntityManager()->clear(); 268 $this->getEntityManager()->clear();
193 269
@@ -196,30 +272,163 @@ class TagControllerTest extends WallabagCoreTestCase
196 $form = $crawler->filter('#tag-' . $tag->getId() . ' form')->form(); 272 $form = $crawler->filter('#tag-' . $tag->getId() . ' form')->form();
197 273
198 $data = [ 274 $data = [
199 'tag[label]' => 'specific label', 275 'tag[label]' => $tagLabel,
200 ]; 276 ];
201 277
202 $client->submit($form, $data); 278 $client->submit($form, $data);
203 $this->assertSame(302, $client->getResponse()->getStatusCode()); 279 $this->assertSame(302, $client->getResponse()->getStatusCode());
280 $this->assertNotContains('flashes.tag.notice.tag_renamed', $crawler->filter('body')->extract(['_text'])[0]);
204 281
205 $freshEntry = $client->getContainer() 282 $freshEntry = $client->getContainer()
206 ->get('doctrine.orm.entity_manager') 283 ->get('doctrine.orm.entity_manager')
207 ->getRepository('WallabagCoreBundle:Entry') 284 ->getRepository('WallabagCoreBundle:Entry')
208 ->find($entry->getId()); 285 ->find($entry->getId());
209 286
210 $tags = $freshEntry->getTags()->toArray(); 287 $tags = [];
211 foreach ($tags as $key => $item) { 288
289 $tagsFromEntry = $freshEntry->getTags()->toArray();
290 foreach ($tagsFromEntry as $key => $item) {
212 $tags[$key] = $item->getLabel(); 291 $tags[$key] = $item->getLabel();
213 } 292 }
214 293
215 $this->assertFalse(array_search($tag->getLabel(), $tags, true), 'Previous tag is not attach to entry anymore.'); 294 $this->assertNotFalse(array_search($tag->getLabel(), $tags, true), 'Tag is still assigned to the entry.');
216 295
217 $newTag = $client->getContainer() 296 $newTag = $client->getContainer()
218 ->get('doctrine.orm.entity_manager') 297 ->get('doctrine.orm.entity_manager')
219 ->getRepository('WallabagCoreBundle:Tag') 298 ->getRepository('WallabagCoreBundle:Tag')
220 ->findOneByLabel('specific label'); 299 ->findByLabel($tagLabel);
221 $this->assertInstanceOf(Tag::class, $newTag, 'Tag "specific label" exists.'); 300
222 $this->assertTrue($newTag->hasEntry($freshEntry), 'Tag "specific label" is assigned to the entry.'); 301 $this->assertCount(1, $newTag);
302 $this->assertSame($tag->getId(), $newTag[0]->getId(), 'Tag is unchanged.');
303
304 $this->assertTrue($newTag[0]->hasEntry($freshEntry), 'Tag is still assigned to the entry.');
305 }
306
307 public function testRenameTagWithSameLabelDifferentCase()
308 {
309 $tagLabel = 'same label';
310 $newTagLabel = 'saMe labEl';
311 $this->logInAs('admin');
312 $client = $this->getClient();
313
314 $tag = new Tag();
315 $tag->setLabel($tagLabel);
316
317 $entry = new Entry($this->getLoggedInUser());
318 $entry->setUrl('http://0.0.0.0/foobar');
319 $entry->addTag($tag);
320 $this->getEntityManager()->persist($entry);
321
322 $this->getEntityManager()->flush();
323 $this->getEntityManager()->clear();
324
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();
328
329 $data = [
330 'tag[label]' => $newTagLabel,
331 ];
332
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]);
336
337 $freshEntry = $client->getContainer()
338 ->get('doctrine.orm.entity_manager')
339 ->getRepository('WallabagCoreBundle:Entry')
340 ->find($entry->getId());
341
342 $tags = [];
343
344 $tagsFromEntry = $freshEntry->getTags()->toArray();
345 foreach ($tagsFromEntry as $key => $item) {
346 $tags[$key] = $item->getLabel();
347 }
348
349 $this->assertFalse(array_search($newTagLabel, $tags, true));
350
351 $tagFromRepo = $client->getContainer()
352 ->get('doctrine.orm.entity_manager')
353 ->getRepository('WallabagCoreBundle:Tag')
354 ->findByLabel($tagLabel);
355
356 $newTagFromRepo = $client->getContainer()
357 ->get('doctrine.orm.entity_manager')
358 ->getRepository('WallabagCoreBundle:Tag')
359 ->findByLabel($newTagLabel);
360
361 $this->assertCount(0, $newTagFromRepo);
362 $this->assertCount(1, $tagFromRepo);
363
364 $this->assertSame($tag->getId(), $tagFromRepo[0]->getId(), 'Tag is unchanged.');
365
366 $this->assertTrue($tagFromRepo[0]->hasEntry($freshEntry), 'Tag is still assigned to the entry.');
367 }
368
369 public function testRenameTagWithExistingLabel()
370 {
371 $tagLabel = 'existing label';
372 $previousTagLabel = 'previous label';
373 $this->logInAs('admin');
374 $client = $this->getClient();
375
376 $tag = new Tag();
377 $tag->setLabel($tagLabel);
378
379 $previousTag = new Tag();
380 $previousTag->setLabel($previousTagLabel);
381
382 $entry1 = new Entry($this->getLoggedInUser());
383 $entry1->setUrl('http://0.0.0.0/foobar');
384 $entry1->addTag($previousTag);
385 $this->getEntityManager()->persist($entry1);
386
387 $entry2 = new Entry($this->getLoggedInUser());
388 $entry2->setUrl('http://0.0.0.0/baz');
389 $entry2->addTag($tag);
390 $this->getEntityManager()->persist($entry2);
391
392 $this->getEntityManager()->flush();
393 $this->getEntityManager()->clear();
394
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();
398
399 $data = [
400 'tag[label]' => $tagLabel,
401 ];
402
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]);
406
407 $freshEntry1 = $client->getContainer()
408 ->get('doctrine.orm.entity_manager')
409 ->getRepository('WallabagCoreBundle:Entry')
410 ->find($entry1->getId());
411
412 $freshEntry2 = $client->getContainer()
413 ->get('doctrine.orm.entity_manager')
414 ->getRepository('WallabagCoreBundle:Entry')
415 ->find($entry2->getId());
416
417 $tagFromRepo = $client->getContainer()
418 ->get('doctrine.orm.entity_manager')
419 ->getRepository('WallabagCoreBundle:Tag')
420 ->findByLabel($tagLabel);
421
422 $previousTagFromRepo = $client->getContainer()
423 ->get('doctrine.orm.entity_manager')
424 ->getRepository('WallabagCoreBundle:Tag')
425 ->findByLabel($previousTagLabel);
426
427 $this->assertCount(1, $tagFromRepo);
428
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));
223 } 432 }
224 433
225 public function testAddUnicodeTagLabel() 434 public function testAddUnicodeTagLabel()