aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/Wallabag/CoreBundle/Controller/TagController.php27
-rw-r--r--tests/Wallabag/CoreBundle/Controller/TagControllerTest.php127
2 files changed, 143 insertions, 11 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/TagController.php b/src/Wallabag/CoreBundle/Controller/TagController.php
index f7b78f5d..16ded948 100644
--- a/src/Wallabag/CoreBundle/Controller/TagController.php
+++ b/src/Wallabag/CoreBundle/Controller/TagController.php
@@ -151,13 +151,21 @@ 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 if ($form->isSubmitted() 154 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'), '', true);
155 && $form->isValid() 155
156 && $form->get('label')->getData() !== $tag->getLabel() 156 if ($form->isSubmitted() && $form->isValid()) {
157 ) {
158 $newTagLabel = $form->get('label')->getData();
159 $newTag = new Tag(); 157 $newTag = new Tag();
160 $newTag->setLabel($newTagLabel); 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 }
161 169
162 $entries = $this->get('wallabag_core.entry_repository')->findAllByTagId( 170 $entries = $this->get('wallabag_core.entry_repository')->findAllByTagId(
163 $this->getUser()->getId(), 171 $this->getUser()->getId(),
@@ -166,14 +174,13 @@ class TagController extends Controller
166 foreach ($entries as $entry) { 174 foreach ($entries as $entry) {
167 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry( 175 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry(
168 $entry, 176 $entry,
169 $newTagLabel, 177 $newTag->getLabel(),
170 [$newTag] 178 [$newTag]
171 ); 179 );
172 $entry->removeTag($tag); 180 $entry->removeTag($tag);
173 } 181 }
174 182
175 $em = $this->getDoctrine()->getManager(); 183 $this->getDoctrine()->getManager()->flush();
176 $em->flush();
177 184
178 $this->get('session')->getFlashBag()->add( 185 $this->get('session')->getFlashBag()->add(
179 'notice', 186 'notice',
@@ -181,8 +188,6 @@ class TagController extends Controller
181 ); 188 );
182 } 189 }
183 190
184 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'), '', true);
185
186 return $this->redirect($redirectUrl); 191 return $this->redirect($redirectUrl);
187 } 192 }
188} 193}
diff --git a/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php b/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php
index 80903b95..fb066632 100644
--- a/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php
+++ b/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php
@@ -304,6 +304,133 @@ class TagControllerTest extends WallabagCoreTestCase
304 $this->assertTrue($newTag[0]->hasEntry($freshEntry), 'Tag is still assigned to the entry.'); 304 $this->assertTrue($newTag[0]->hasEntry($freshEntry), 'Tag is still assigned to the entry.');
305 } 305 }
306 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));
432 }
433
307 public function testAddUnicodeTagLabel() 434 public function testAddUnicodeTagLabel()
308 { 435 {
309 $this->logInAs('admin'); 436 $this->logInAs('admin');