aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2016-10-07 23:31:53 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2016-10-07 23:31:53 +0200
commitac8cf632bb3a225c1b69d16e714ff60a2e988c89 (patch)
tree6789145b0518564d943399fd1b37830089cc4376
parent3049afe190d125e4861059b6bbad7c6fbea6f1bb (diff)
downloadwallabag-ac8cf632bb3a225c1b69d16e714ff60a2e988c89.tar.gz
wallabag-ac8cf632bb3a225c1b69d16e714ff60a2e988c89.tar.zst
wallabag-ac8cf632bb3a225c1b69d16e714ff60a2e988c89.zip
Ensure orphan tag are remove in API
When the association between a tag and an entry is removed, if the tag doesn’t have other entries, we can remove it. Also add more tests for that part and ensure TagControllerTest is isolated from the rest of the test suite (finally!)
-rw-r--r--src/Wallabag/ApiBundle/Controller/WallabagRestController.php28
-rw-r--r--src/Wallabag/CoreBundle/Controller/TagController.php6
-rw-r--r--src/Wallabag/CoreBundle/Resources/config/services.yml2
-rw-r--r--tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php9
-rw-r--r--tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php27
-rw-r--r--tests/Wallabag/CoreBundle/Controller/TagControllerTest.php33
6 files changed, 78 insertions, 27 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
index a0d9d4f3..cc6923a0 100644
--- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
@@ -400,6 +400,8 @@ class WallabagRestController extends FOSRestController
400 ->getRepository('WallabagCoreBundle:Entry') 400 ->getRepository('WallabagCoreBundle:Entry')
401 ->removeTag($this->getUser()->getId(), $tag); 401 ->removeTag($this->getUser()->getId(), $tag);
402 402
403 $this->cleanOrphanTag($tag);
404
403 $json = $this->get('serializer')->serialize($tag, 'json'); 405 $json = $this->get('serializer')->serialize($tag, 'json');
404 406
405 return (new JsonResponse())->setJson($json); 407 return (new JsonResponse())->setJson($json);
@@ -440,6 +442,8 @@ class WallabagRestController extends FOSRestController
440 ->getRepository('WallabagCoreBundle:Entry') 442 ->getRepository('WallabagCoreBundle:Entry')
441 ->removeTags($this->getUser()->getId(), $tags); 443 ->removeTags($this->getUser()->getId(), $tags);
442 444
445 $this->cleanOrphanTag($tags);
446
443 $json = $this->get('serializer')->serialize($tags, 'json'); 447 $json = $this->get('serializer')->serialize($tags, 'json');
444 448
445 return (new JsonResponse())->setJson($json); 449 return (new JsonResponse())->setJson($json);
@@ -464,6 +468,8 @@ class WallabagRestController extends FOSRestController
464 ->getRepository('WallabagCoreBundle:Entry') 468 ->getRepository('WallabagCoreBundle:Entry')
465 ->removeTag($this->getUser()->getId(), $tag); 469 ->removeTag($this->getUser()->getId(), $tag);
466 470
471 $this->cleanOrphanTag($tag);
472
467 $json = $this->get('serializer')->serialize($tag, 'json'); 473 $json = $this->get('serializer')->serialize($tag, 'json');
468 474
469 return (new JsonResponse())->setJson($json); 475 return (new JsonResponse())->setJson($json);
@@ -486,6 +492,28 @@ class WallabagRestController extends FOSRestController
486 } 492 }
487 493
488 /** 494 /**
495 * Remove orphan tag in case no entries are associated to it.
496 *
497 * @param Tag|array $tags
498 */
499 private function cleanOrphanTag($tags)
500 {
501 if (!is_array($tags)) {
502 $tags = [$tags];
503 }
504
505 $em = $this->getDoctrine()->getManager();
506
507 foreach ($tags as $tag) {
508 if (count($tag->getEntries()) === 0) {
509 $em->remove($tag);
510 }
511 }
512
513 $em->flush();
514 }
515
516 /**
489 * Validate that the first id is equal to the second one. 517 * Validate that the first id is equal to the second one.
490 * If not, throw exception. It means a user try to access information from an other user. 518 * If not, throw exception. It means a user try to access information from an other user.
491 * 519 *
diff --git a/src/Wallabag/CoreBundle/Controller/TagController.php b/src/Wallabag/CoreBundle/Controller/TagController.php
index 623a6146..c5746734 100644
--- a/src/Wallabag/CoreBundle/Controller/TagController.php
+++ b/src/Wallabag/CoreBundle/Controller/TagController.php
@@ -63,10 +63,12 @@ class TagController extends Controller
63 $entry->removeTag($tag); 63 $entry->removeTag($tag);
64 $em = $this->getDoctrine()->getManager(); 64 $em = $this->getDoctrine()->getManager();
65 $em->flush(); 65 $em->flush();
66 if (count($tag->getEntries()) == 0) { 66
67 // remove orphan tag in case no entries are associated to it
68 if (count($tag->getEntries()) === 0) {
67 $em->remove($tag); 69 $em->remove($tag);
70 $em->flush();
68 } 71 }
69 $em->flush();
70 72
71 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); 73 $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer'));
72 74
diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml
index fb97454e..a4b727f4 100644
--- a/src/Wallabag/CoreBundle/Resources/config/services.yml
+++ b/src/Wallabag/CoreBundle/Resources/config/services.yml
@@ -29,7 +29,7 @@ services:
29 arguments: 29 arguments:
30 - "@doctrine" 30 - "@doctrine"
31 31
32 wallabag_core.table_prefix_subscriber: 32 wallabag_core.subscriber.table_prefix:
33 class: Wallabag\CoreBundle\Subscriber\TablePrefixSubscriber 33 class: Wallabag\CoreBundle\Subscriber\TablePrefixSubscriber
34 arguments: 34 arguments:
35 - "%database_table_prefix%" 35 - "%database_table_prefix%"
diff --git a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php
index 65b65290..c797daf7 100644
--- a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php
+++ b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php
@@ -561,6 +561,8 @@ class WallabagRestControllerTest extends WallabagApiTestCase
561 */ 561 */
562 public function testDeleteUserTag($tag) 562 public function testDeleteUserTag($tag)
563 { 563 {
564 $tagName = $tag['label'];
565
564 $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json'); 566 $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json');
565 567
566 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 568 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@@ -577,6 +579,13 @@ class WallabagRestControllerTest extends WallabagApiTestCase
577 ->findAllByTagId($this->user->getId(), $tag['id']); 579 ->findAllByTagId($this->user->getId(), $tag['id']);
578 580
579 $this->assertCount(0, $entries); 581 $this->assertCount(0, $entries);
582
583 $tag = $this->client->getContainer()
584 ->get('doctrine.orm.entity_manager')
585 ->getRepository('WallabagCoreBundle:Tag')
586 ->findOneByLabel($tagName);
587
588 $this->assertNull($tag, $tagName.' was removed because it begun an orphan tag');
580 } 589 }
581 590
582 public function testDeleteTagByLabel() 591 public function testDeleteTagByLabel()
diff --git a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php
index d7bf03ba..9b03a519 100644
--- a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php
+++ b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php
@@ -341,22 +341,23 @@ class EntryControllerTest extends WallabagCoreTestCase
341 $this->logInAs('admin'); 341 $this->logInAs('admin');
342 $client = $this->getClient(); 342 $client = $this->getClient();
343 343
344 $content = $client->getContainer() 344 $em = $client->getContainer()
345 ->get('doctrine.orm.entity_manager') 345 ->get('doctrine.orm.entity_manager');
346
347 $content = $em
346 ->getRepository('WallabagCoreBundle:Entry') 348 ->getRepository('WallabagCoreBundle:Entry')
347 ->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); 349 ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
348 350
349 // empty content 351 // empty content
350 $content->setContent(''); 352 $content->setContent('');
351 $client->getContainer()->get('doctrine.orm.entity_manager')->persist($content); 353 $em->persist($content);
352 $client->getContainer()->get('doctrine.orm.entity_manager')->flush(); 354 $em->flush();
353 355
354 $client->request('GET', '/reload/'.$content->getId()); 356 $client->request('GET', '/reload/'.$content->getId());
355 357
356 $this->assertEquals(302, $client->getResponse()->getStatusCode()); 358 $this->assertEquals(302, $client->getResponse()->getStatusCode());
357 359
358 $content = $client->getContainer() 360 $content = $em
359 ->get('doctrine.orm.entity_manager')
360 ->getRepository('WallabagCoreBundle:Entry') 361 ->getRepository('WallabagCoreBundle:Entry')
361 ->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); 362 ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
362 363
@@ -486,9 +487,11 @@ class EntryControllerTest extends WallabagCoreTestCase
486 $this->logInAs('admin'); 487 $this->logInAs('admin');
487 $client = $this->getClient(); 488 $client = $this->getClient();
488 489
490 $em = $client->getContainer()
491 ->get('doctrine.orm.entity_manager');
492
489 // add a new content to be removed later 493 // add a new content to be removed later
490 $user = $client->getContainer() 494 $user = $em
491 ->get('doctrine.orm.entity_manager')
492 ->getRepository('WallabagUserBundle:User') 495 ->getRepository('WallabagUserBundle:User')
493 ->findOneByUserName('admin'); 496 ->findOneByUserName('admin');
494 497
@@ -502,12 +505,8 @@ class EntryControllerTest extends WallabagCoreTestCase
502 $content->setArchived(true); 505 $content->setArchived(true);
503 $content->setLanguage('fr'); 506 $content->setLanguage('fr');
504 507
505 $client->getContainer() 508 $em->persist($content);
506 ->get('doctrine.orm.entity_manager') 509 $em->flush();
507 ->persist($content);
508 $client->getContainer()
509 ->get('doctrine.orm.entity_manager')
510 ->flush();
511 510
512 $client->request('GET', '/view/'.$content->getId()); 511 $client->request('GET', '/view/'.$content->getId());
513 $this->assertEquals(200, $client->getResponse()->getStatusCode()); 512 $this->assertEquals(200, $client->getResponse()->getStatusCode());
diff --git a/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php b/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php
index 86a6cca2..769ce66e 100644
--- a/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php
+++ b/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php
@@ -3,6 +3,7 @@
3namespace Tests\Wallabag\CoreBundle\Controller; 3namespace Tests\Wallabag\CoreBundle\Controller;
4 4
5use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; 5use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
6use Wallabag\CoreBundle\Entity\Tag;
6 7
7class TagControllerTest extends WallabagCoreTestCase 8class TagControllerTest extends WallabagCoreTestCase
8{ 9{
@@ -134,36 +135,48 @@ class TagControllerTest extends WallabagCoreTestCase
134 $client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId()); 135 $client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId());
135 136
136 $this->assertEquals(404, $client->getResponse()->getStatusCode()); 137 $this->assertEquals(404, $client->getResponse()->getStatusCode());
138
139 $tag = $client->getContainer()
140 ->get('doctrine.orm.entity_manager')
141 ->getRepository('WallabagCoreBundle:Tag')
142 ->findOneByLabel($this->tagName);
143
144 $this->assertNull($tag, $this->tagName.' was removed because it begun an orphan tag');
137 } 145 }
138 146
139 public function testShowEntriesForTagAction() 147 public function testShowEntriesForTagAction()
140 { 148 {
141 $this->logInAs('admin'); 149 $this->logInAs('admin');
142 $client = $this->getClient(); 150 $client = $this->getClient();
151 $em = $client->getContainer()
152 ->get('doctrine.orm.entity_manager');
153
154 $tag = new Tag();
155 $tag->setLabel($this->tagName);
143 156
144 $entry = $client->getContainer() 157 $entry = $client->getContainer()
145 ->get('doctrine.orm.entity_manager') 158 ->get('doctrine.orm.entity_manager')
146 ->getRepository('WallabagCoreBundle:Entry') 159 ->getRepository('WallabagCoreBundle:Entry')
147 ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getLoggedInUserId()); 160 ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getLoggedInUserId());
148 161
149 $tag = $client->getContainer() 162 $tag->addEntry($entry);
150 ->get('doctrine.orm.entity_manager')
151 ->getRepository('WallabagCoreBundle:Tag')
152 ->findOneByEntryAndTagLabel($entry, 'foo');
153
154 $crawler = $client->request('GET', '/tag/list/'.$tag->getSlug());
155 163
156 $this->assertEquals(200, $client->getResponse()->getStatusCode()); 164 $em->persist($entry);
157 $this->assertCount(2, $crawler->filter('div[class=entry]')); 165 $em->persist($tag);
166 $em->flush();
158 167
159 $tag = $client->getContainer() 168 $tag = $client->getContainer()
160 ->get('doctrine.orm.entity_manager') 169 ->get('doctrine.orm.entity_manager')
161 ->getRepository('WallabagCoreBundle:Tag') 170 ->getRepository('WallabagCoreBundle:Tag')
162 ->findOneByLabel('baz'); 171 ->findOneByEntryAndTagLabel($entry, $this->tagName);
163 172
164 $crawler = $client->request('GET', '/tag/list/'.$tag->getSlug()); 173 $crawler = $client->request('GET', '/tag/list/'.$tag->getSlug());
165 174
166 $this->assertEquals(200, $client->getResponse()->getStatusCode()); 175 $this->assertEquals(200, $client->getResponse()->getStatusCode());
167 $this->assertCount(1, $crawler->filter('div[class=entry]')); 176 $this->assertCount(1, $crawler->filter('[id*="entry-"]'));
177
178 $entry->removeTag($tag);
179 $em->remove($tag);
180 $em->flush();
168 } 181 }
169} 182}