diff options
author | Nicolas Lœuillet <nicolas@loeuillet.org> | 2016-01-02 12:46:52 +0100 |
---|---|---|
committer | Nicolas Lœuillet <nicolas@loeuillet.org> | 2016-01-02 12:46:52 +0100 |
commit | dad1c546a521159ca65a5a7649651d37728f0e55 (patch) | |
tree | f1d59eb83b45adb83bb21726374133a399d3c740 | |
parent | 5432f6150939af6e7d2e8bf0faea0576491aaed0 (diff) | |
parent | 6be9750155fa731d75898b4973a126a090345c2d (diff) | |
download | wallabag-dad1c546a521159ca65a5a7649651d37728f0e55.tar.gz wallabag-dad1c546a521159ca65a5a7649651d37728f0e55.tar.zst wallabag-dad1c546a521159ca65a5a7649651d37728f0e55.zip |
Merge pull request #1545 from wallabag/v2-user-tag
v2 – Remove user reference in tag
13 files changed, 159 insertions, 103 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php index 74bfe4dc..459c4172 100644 --- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php | |||
@@ -27,7 +27,7 @@ class WallabagRestController extends FOSRestController | |||
27 | ->findOneByLabel($label); | 27 | ->findOneByLabel($label); |
28 | 28 | ||
29 | if (is_null($tagEntity)) { | 29 | if (is_null($tagEntity)) { |
30 | $tagEntity = new Tag($this->getUser()); | 30 | $tagEntity = new Tag(); |
31 | $tagEntity->setLabel($label); | 31 | $tagEntity->setLabel($label); |
32 | } | 32 | } |
33 | 33 | ||
@@ -74,8 +74,7 @@ class WallabagRestController extends FOSRestController | |||
74 | $perPage = (int) $request->query->get('perPage', 30); | 74 | $perPage = (int) $request->query->get('perPage', 30); |
75 | $tags = $request->query->get('tags', []); | 75 | $tags = $request->query->get('tags', []); |
76 | 76 | ||
77 | $pager = $this | 77 | $pager = $this->getDoctrine() |
78 | ->getDoctrine() | ||
79 | ->getRepository('WallabagCoreBundle:Entry') | 78 | ->getRepository('WallabagCoreBundle:Entry') |
80 | ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order); | 79 | ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order); |
81 | 80 | ||
@@ -175,8 +174,8 @@ class WallabagRestController extends FOSRestController | |||
175 | $this->validateUserAccess($entry->getUser()->getId()); | 174 | $this->validateUserAccess($entry->getUser()->getId()); |
176 | 175 | ||
177 | $title = $request->request->get('title'); | 176 | $title = $request->request->get('title'); |
178 | $isArchived = $request->request->get('is_archived'); | 177 | $isArchived = $request->request->get('archive'); |
179 | $isStarred = $request->request->get('is_starred'); | 178 | $isStarred = $request->request->get('star'); |
180 | 179 | ||
181 | if (!is_null($title)) { | 180 | if (!is_null($title)) { |
182 | $entry->setTitle($title); | 181 | $entry->setTitle($title); |
@@ -311,7 +310,12 @@ class WallabagRestController extends FOSRestController | |||
311 | public function getTagsAction() | 310 | public function getTagsAction() |
312 | { | 311 | { |
313 | $this->validateAuthentication(); | 312 | $this->validateAuthentication(); |
314 | $json = $this->get('serializer')->serialize($this->getUser()->getTags(), 'json'); | 313 | |
314 | $tags = $this->getDoctrine() | ||
315 | ->getRepository('WallabagCoreBundle:Tag') | ||
316 | ->findAllTags($this->getUser()->getId()); | ||
317 | |||
318 | $json = $this->get('serializer')->serialize($tags, 'json'); | ||
315 | 319 | ||
316 | return $this->renderJsonResponse($json); | 320 | return $this->renderJsonResponse($json); |
317 | } | 321 | } |
@@ -328,11 +332,10 @@ class WallabagRestController extends FOSRestController | |||
328 | public function deleteTagAction(Tag $tag) | 332 | public function deleteTagAction(Tag $tag) |
329 | { | 333 | { |
330 | $this->validateAuthentication(); | 334 | $this->validateAuthentication(); |
331 | $this->validateUserAccess($tag->getUser()->getId()); | ||
332 | 335 | ||
333 | $em = $this->getDoctrine()->getManager(); | 336 | $this->getDoctrine() |
334 | $em->remove($tag); | 337 | ->getRepository('WallabagCoreBundle:Entry') |
335 | $em->flush(); | 338 | ->removeTag($this->getUser()->getId(), $tag); |
336 | 339 | ||
337 | $json = $this->get('serializer')->serialize($tag, 'json'); | 340 | $json = $this->get('serializer')->serialize($tag, 'json'); |
338 | 341 | ||
diff --git a/src/Wallabag/ApiBundle/Tests/Controller/WallabagRestControllerTest.php b/src/Wallabag/ApiBundle/Tests/Controller/WallabagRestControllerTest.php index bdd36e0c..22894a77 100644 --- a/src/Wallabag/ApiBundle/Tests/Controller/WallabagRestControllerTest.php +++ b/src/Wallabag/ApiBundle/Tests/Controller/WallabagRestControllerTest.php | |||
@@ -208,7 +208,7 @@ class WallabagRestControllerTest extends WallabagApiTestCase | |||
208 | 208 | ||
209 | $tags = array(); | 209 | $tags = array(); |
210 | foreach ($entry->getTags() as $tag) { | 210 | foreach ($entry->getTags() as $tag) { |
211 | $tags[] = array('id' => $tag->getId(), 'label' => $tag->getLabel()); | 211 | $tags[] = array('id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()); |
212 | } | 212 | } |
213 | 213 | ||
214 | $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags'); | 214 | $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags'); |
@@ -309,5 +309,13 @@ class WallabagRestControllerTest extends WallabagApiTestCase | |||
309 | 309 | ||
310 | $this->assertArrayHasKey('label', $content); | 310 | $this->assertArrayHasKey('label', $content); |
311 | $this->assertEquals($tag['label'], $content['label']); | 311 | $this->assertEquals($tag['label'], $content['label']); |
312 | $this->assertEquals($tag['slug'], $content['slug']); | ||
313 | |||
314 | $entries = $entry = $this->client->getContainer() | ||
315 | ->get('doctrine.orm.entity_manager') | ||
316 | ->getRepository('WallabagCoreBundle:Entry') | ||
317 | ->findAllByTagId($this->user->getId(), $tag['id']); | ||
318 | |||
319 | $this->assertCount(0, $entries); | ||
312 | } | 320 | } |
313 | } | 321 | } |
diff --git a/src/Wallabag/ApiBundle/Tests/WallabagApiTestCase.php b/src/Wallabag/ApiBundle/Tests/WallabagApiTestCase.php index 8a57fea2..a415c749 100644 --- a/src/Wallabag/ApiBundle/Tests/WallabagApiTestCase.php +++ b/src/Wallabag/ApiBundle/Tests/WallabagApiTestCase.php | |||
@@ -12,6 +12,11 @@ abstract class WallabagApiTestCase extends WebTestCase | |||
12 | */ | 12 | */ |
13 | protected $client = null; | 13 | protected $client = null; |
14 | 14 | ||
15 | /** | ||
16 | * @var \FOS\UserBundle\Model\UserInterface | ||
17 | */ | ||
18 | protected $user; | ||
19 | |||
15 | public function setUp() | 20 | public function setUp() |
16 | { | 21 | { |
17 | $this->client = $this->createAuthorizedClient(); | 22 | $this->client = $this->createAuthorizedClient(); |
@@ -31,8 +36,8 @@ abstract class WallabagApiTestCase extends WebTestCase | |||
31 | $loginManager = $container->get('fos_user.security.login_manager'); | 36 | $loginManager = $container->get('fos_user.security.login_manager'); |
32 | $firewallName = $container->getParameter('fos_user.firewall_name'); | 37 | $firewallName = $container->getParameter('fos_user.firewall_name'); |
33 | 38 | ||
34 | $user = $userManager->findUserBy(array('username' => 'admin')); | 39 | $this->user = $userManager->findUserBy(array('username' => 'admin')); |
35 | $loginManager->loginUser($firewallName, $user); | 40 | $loginManager->loginUser($firewallName, $this->user); |
36 | 41 | ||
37 | // save the login token into the session and put it in a cookie | 42 | // save the login token into the session and put it in a cookie |
38 | $container->get('session')->set('_security_'.$firewallName, serialize($container->get('security.token_storage')->getToken())); | 43 | $container->get('session')->set('_security_'.$firewallName, serialize($container->get('security.token_storage')->getToken())); |
diff --git a/src/Wallabag/CoreBundle/Controller/TagController.php b/src/Wallabag/CoreBundle/Controller/TagController.php index fd2069e0..64d53f0c 100644 --- a/src/Wallabag/CoreBundle/Controller/TagController.php +++ b/src/Wallabag/CoreBundle/Controller/TagController.php | |||
@@ -20,25 +20,23 @@ class TagController extends Controller | |||
20 | */ | 20 | */ |
21 | public function addTagFormAction(Request $request, Entry $entry) | 21 | public function addTagFormAction(Request $request, Entry $entry) |
22 | { | 22 | { |
23 | $tag = new Tag($this->getUser()); | 23 | $tag = new Tag(); |
24 | $form = $this->createForm(new NewTagType(), $tag); | 24 | $form = $this->createForm(new NewTagType(), $tag); |
25 | $form->handleRequest($request); | 25 | $form->handleRequest($request); |
26 | 26 | ||
27 | if ($form->isValid()) { | 27 | if ($form->isValid()) { |
28 | $existingTag = $this->getDoctrine() | 28 | $existingTag = $this->getDoctrine() |
29 | ->getRepository('WallabagCoreBundle:Tag') | 29 | ->getRepository('WallabagCoreBundle:Tag') |
30 | ->findOneByLabelAndUserId($tag->getLabel(), $this->getUser()->getId()); | 30 | ->findOneByLabel($tag->getLabel()); |
31 | 31 | ||
32 | $em = $this->getDoctrine()->getManager(); | 32 | $em = $this->getDoctrine()->getManager(); |
33 | 33 | ||
34 | if (is_null($existingTag)) { | 34 | if (is_null($existingTag)) { |
35 | $entry->addTag($tag); | 35 | $entry->addTag($tag); |
36 | $em->persist($tag); | 36 | $em->persist($tag); |
37 | } else { | 37 | } elseif (!$existingTag->hasEntry($entry)) { |
38 | if (!$existingTag->hasEntry($entry)) { | 38 | $entry->addTag($existingTag); |
39 | $entry->addTag($existingTag); | 39 | $em->persist($existingTag); |
40 | $em->persist($existingTag); | ||
41 | } | ||
42 | } | 40 | } |
43 | 41 | ||
44 | $em->flush(); | 42 | $em->flush(); |
diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php index 0513cdb8..6c6a331a 100644 --- a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php +++ b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php | |||
@@ -6,7 +6,6 @@ use Doctrine\Common\DataFixtures\AbstractFixture; | |||
6 | use Doctrine\Common\DataFixtures\OrderedFixtureInterface; | 6 | use Doctrine\Common\DataFixtures\OrderedFixtureInterface; |
7 | use Doctrine\Common\Persistence\ObjectManager; | 7 | use Doctrine\Common\Persistence\ObjectManager; |
8 | use Wallabag\CoreBundle\Entity\Entry; | 8 | use Wallabag\CoreBundle\Entity\Entry; |
9 | use Wallabag\CoreBundle\Entity\Tag; | ||
10 | 9 | ||
11 | class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface | 10 | class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface |
12 | { | 11 | { |
@@ -50,13 +49,8 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface | |||
50 | $entry3->setContent('This is my content /o/'); | 49 | $entry3->setContent('This is my content /o/'); |
51 | $entry3->setLanguage('en'); | 50 | $entry3->setLanguage('en'); |
52 | 51 | ||
53 | $tag1 = new Tag($this->getReference('bob-user')); | 52 | $entry3->addTag($this->getReference('foo-tag')); |
54 | $tag1->setLabel('foo'); | 53 | $entry3->addTag($this->getReference('bar-tag')); |
55 | $tag2 = new Tag($this->getReference('bob-user')); | ||
56 | $tag2->setLabel('bar'); | ||
57 | |||
58 | $entry3->addTag($tag1); | ||
59 | $entry3->addTag($tag2); | ||
60 | 54 | ||
61 | $manager->persist($entry3); | 55 | $manager->persist($entry3); |
62 | 56 | ||
@@ -71,13 +65,8 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface | |||
71 | $entry4->setContent('This is my content /o/'); | 65 | $entry4->setContent('This is my content /o/'); |
72 | $entry4->setLanguage('en'); | 66 | $entry4->setLanguage('en'); |
73 | 67 | ||
74 | $tag1 = new Tag($this->getReference('admin-user')); | 68 | $entry4->addTag($this->getReference('foo-tag')); |
75 | $tag1->setLabel('foo'); | 69 | $entry4->addTag($this->getReference('bar-tag')); |
76 | $tag2 = new Tag($this->getReference('admin-user')); | ||
77 | $tag2->setLabel('bar'); | ||
78 | |||
79 | $entry4->addTag($tag1); | ||
80 | $entry4->addTag($tag2); | ||
81 | 70 | ||
82 | $manager->persist($entry4); | 71 | $manager->persist($entry4); |
83 | 72 | ||
diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTagData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTagData.php new file mode 100644 index 00000000..8553dced --- /dev/null +++ b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTagData.php | |||
@@ -0,0 +1,41 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\DataFixtures\ORM; | ||
4 | |||
5 | use Doctrine\Common\DataFixtures\AbstractFixture; | ||
6 | use Doctrine\Common\DataFixtures\OrderedFixtureInterface; | ||
7 | use Doctrine\Common\Persistence\ObjectManager; | ||
8 | use Wallabag\CoreBundle\Entity\Tag; | ||
9 | |||
10 | class LoadTagData extends AbstractFixture implements OrderedFixtureInterface | ||
11 | { | ||
12 | /** | ||
13 | * {@inheritdoc} | ||
14 | */ | ||
15 | public function load(ObjectManager $manager) | ||
16 | { | ||
17 | $tag1 = new Tag(); | ||
18 | $tag1->setLabel('foo'); | ||
19 | |||
20 | $manager->persist($tag1); | ||
21 | |||
22 | $this->addReference('foo-tag', $tag1); | ||
23 | |||
24 | $tag2 = new Tag(); | ||
25 | $tag2->setLabel('bar'); | ||
26 | |||
27 | $manager->persist($tag2); | ||
28 | |||
29 | $this->addReference('bar-tag', $tag2); | ||
30 | |||
31 | $manager->flush(); | ||
32 | } | ||
33 | |||
34 | /** | ||
35 | * {@inheritdoc} | ||
36 | */ | ||
37 | public function getOrder() | ||
38 | { | ||
39 | return 25; | ||
40 | } | ||
41 | } | ||
diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php index 2813c944..b413c489 100644 --- a/src/Wallabag/CoreBundle/Entity/Entry.php +++ b/src/Wallabag/CoreBundle/Entity/Entry.php | |||
@@ -465,7 +465,7 @@ class Entry | |||
465 | // check if tag already exist but has not yet be persisted | 465 | // check if tag already exist but has not yet be persisted |
466 | // it seems that the previous condition with `contains()` doesn't check that case | 466 | // it seems that the previous condition with `contains()` doesn't check that case |
467 | foreach ($this->tags as $existingTag) { | 467 | foreach ($this->tags as $existingTag) { |
468 | if ($existingTag->getUser() !== $tag->getUser() || $existingTag->getLabel() === $tag->getLabel()) { | 468 | if ($existingTag->getLabel() === $tag->getLabel()) { |
469 | return; | 469 | return; |
470 | } | 470 | } |
471 | } | 471 | } |
diff --git a/src/Wallabag/CoreBundle/Entity/Tag.php b/src/Wallabag/CoreBundle/Entity/Tag.php index 4ed588be..0689c7b3 100644 --- a/src/Wallabag/CoreBundle/Entity/Tag.php +++ b/src/Wallabag/CoreBundle/Entity/Tag.php | |||
@@ -38,6 +38,7 @@ class Tag | |||
38 | private $label; | 38 | private $label; |
39 | 39 | ||
40 | /** | 40 | /** |
41 | * @Expose | ||
41 | * @Gedmo\Slug(fields={"label"}) | 42 | * @Gedmo\Slug(fields={"label"}) |
42 | * @ORM\Column(length=128, unique=true) | 43 | * @ORM\Column(length=128, unique=true) |
43 | */ | 44 | */ |
@@ -48,14 +49,8 @@ class Tag | |||
48 | */ | 49 | */ |
49 | private $entries; | 50 | private $entries; |
50 | 51 | ||
51 | /** | 52 | public function __construct() |
52 | * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="tags") | ||
53 | */ | ||
54 | private $user; | ||
55 | |||
56 | public function __construct(\Wallabag\UserBundle\Entity\User $user) | ||
57 | { | 53 | { |
58 | $this->user = $user; | ||
59 | $this->entries = new ArrayCollection(); | 54 | $this->entries = new ArrayCollection(); |
60 | } | 55 | } |
61 | 56 | ||
@@ -112,12 +107,4 @@ class Tag | |||
112 | { | 107 | { |
113 | return $this->entries->contains($entry); | 108 | return $this->entries->contains($entry); |
114 | } | 109 | } |
115 | |||
116 | /** | ||
117 | * @return User | ||
118 | */ | ||
119 | public function getUser() | ||
120 | { | ||
121 | return $this->user; | ||
122 | } | ||
123 | } | 110 | } |
diff --git a/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php b/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php index 41ef25b8..991c9a56 100644 --- a/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php +++ b/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php | |||
@@ -37,7 +37,7 @@ class RuleBasedTagger | |||
37 | } | 37 | } |
38 | 38 | ||
39 | foreach ($rule->getTags() as $label) { | 39 | foreach ($rule->getTags() as $label) { |
40 | $tag = $this->getTag($entry->getUser(), $label); | 40 | $tag = $this->getTag($label); |
41 | 41 | ||
42 | $entry->addTag($tag); | 42 | $entry->addTag($tag); |
43 | } | 43 | } |
@@ -62,7 +62,7 @@ class RuleBasedTagger | |||
62 | 62 | ||
63 | foreach ($entries as $entry) { | 63 | foreach ($entries as $entry) { |
64 | foreach ($rule->getTags() as $label) { | 64 | foreach ($rule->getTags() as $label) { |
65 | $tag = $this->getTag($user, $label); | 65 | $tag = $this->getTag($label); |
66 | 66 | ||
67 | $entry->addTag($tag); | 67 | $entry->addTag($tag); |
68 | } | 68 | } |
@@ -73,19 +73,18 @@ class RuleBasedTagger | |||
73 | } | 73 | } |
74 | 74 | ||
75 | /** | 75 | /** |
76 | * Fetch a tag for a user. | 76 | * Fetch a tag. |
77 | * | 77 | * |
78 | * @param User $user | ||
79 | * @param string $label The tag's label. | 78 | * @param string $label The tag's label. |
80 | * | 79 | * |
81 | * @return Tag | 80 | * @return Tag |
82 | */ | 81 | */ |
83 | private function getTag(User $user, $label) | 82 | private function getTag($label) |
84 | { | 83 | { |
85 | $tag = $this->tagRepository->findOneByLabelAndUserId($label, $user->getId()); | 84 | $tag = $this->tagRepository->findOneByLabel($label); |
86 | 85 | ||
87 | if (!$tag) { | 86 | if (!$tag) { |
88 | $tag = new Tag($user); | 87 | $tag = new Tag(); |
89 | $tag->setLabel($label); | 88 | $tag->setLabel($label); |
90 | } | 89 | } |
91 | 90 | ||
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 57bf8024..ca71970b 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php | |||
@@ -5,6 +5,7 @@ namespace Wallabag\CoreBundle\Repository; | |||
5 | use Doctrine\ORM\EntityRepository; | 5 | use Doctrine\ORM\EntityRepository; |
6 | use Pagerfanta\Adapter\DoctrineORMAdapter; | 6 | use Pagerfanta\Adapter\DoctrineORMAdapter; |
7 | use Pagerfanta\Pagerfanta; | 7 | use Pagerfanta\Pagerfanta; |
8 | use Wallabag\CoreBundle\Entity\Tag; | ||
8 | 9 | ||
9 | class EntryRepository extends EntityRepository | 10 | class EntryRepository extends EntityRepository |
10 | { | 11 | { |
@@ -179,4 +180,47 @@ class EntryRepository extends EntityRepository | |||
179 | ->getQuery() | 180 | ->getQuery() |
180 | ->getSingleResult(); | 181 | ->getSingleResult(); |
181 | } | 182 | } |
183 | |||
184 | /** | ||
185 | * Remove a tag from all user entries. | ||
186 | * | ||
187 | * We need to loop on each entry attached to the given tag to remove it, since Doctrine doesn't know EntryTag entity because it's a ManyToMany relation. | ||
188 | * It could be faster with one query but I don't know how to retrieve the table name `entry_tag` which can have a prefix: | ||
189 | * | ||
190 | * DELETE et FROM entry_tag et WHERE et.entry_id IN ( SELECT e.id FROM entry e WHERE e.user_id = :userId ) AND et.tag_id = :tagId | ||
191 | * | ||
192 | * @param int $userId | ||
193 | * @param Tag $tag | ||
194 | */ | ||
195 | public function removeTag($userId, Tag $tag) | ||
196 | { | ||
197 | $entries = $this->getBuilderByUser($userId) | ||
198 | ->innerJoin('e.tags', 't') | ||
199 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId()) | ||
200 | ->getQuery() | ||
201 | ->getResult(); | ||
202 | |||
203 | foreach ($entries as $entry) { | ||
204 | $entry->removeTag($tag); | ||
205 | } | ||
206 | |||
207 | $this->getEntityManager()->flush(); | ||
208 | } | ||
209 | |||
210 | /** | ||
211 | * Find all entries that are attached to a give tag id. | ||
212 | * | ||
213 | * @param int $userId | ||
214 | * @param int $tagId | ||
215 | * | ||
216 | * @return array | ||
217 | */ | ||
218 | public function findAllByTagId($userId, $tagId) | ||
219 | { | ||
220 | return $this->getBuilderByUser($userId) | ||
221 | ->innerJoin('e.tags', 't') | ||
222 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId) | ||
223 | ->getQuery() | ||
224 | ->getResult(); | ||
225 | } | ||
182 | } | 226 | } |
diff --git a/src/Wallabag/CoreBundle/Repository/TagRepository.php b/src/Wallabag/CoreBundle/Repository/TagRepository.php index ac3145a1..c4aeb594 100644 --- a/src/Wallabag/CoreBundle/Repository/TagRepository.php +++ b/src/Wallabag/CoreBundle/Repository/TagRepository.php | |||
@@ -9,16 +9,29 @@ use Pagerfanta\Pagerfanta; | |||
9 | class TagRepository extends EntityRepository | 9 | class TagRepository extends EntityRepository |
10 | { | 10 | { |
11 | /** | 11 | /** |
12 | * Find Tags. | 12 | * Return only the QueryBuilder to retrieve all tags for a given user. |
13 | * | 13 | * |
14 | * @param int $userId | 14 | * @param int $userId |
15 | * | 15 | * |
16 | * @return array | 16 | * @return QueryBuilder |
17 | */ | ||
18 | private function getQbForAllTags($userId) | ||
19 | { | ||
20 | return $this->createQueryBuilder('t') | ||
21 | ->leftJoin('t.entries', 'e') | ||
22 | ->where('e.user = :userId')->setParameter('userId', $userId); | ||
23 | } | ||
24 | |||
25 | /** | ||
26 | * Find Tags and return a Pager. | ||
27 | * | ||
28 | * @param int $userId | ||
29 | * | ||
30 | * @return Pagerfanta | ||
17 | */ | 31 | */ |
18 | public function findTags($userId) | 32 | public function findTags($userId) |
19 | { | 33 | { |
20 | $qb = $this->createQueryBuilder('t') | 34 | $qb = $this->getQbForAllTags($userId); |
21 | ->where('t.user =:userId')->setParameter('userId', $userId); | ||
22 | 35 | ||
23 | $pagerAdapter = new DoctrineORMAdapter($qb); | 36 | $pagerAdapter = new DoctrineORMAdapter($qb); |
24 | 37 | ||
@@ -26,19 +39,16 @@ class TagRepository extends EntityRepository | |||
26 | } | 39 | } |
27 | 40 | ||
28 | /** | 41 | /** |
29 | * Find a tag by its label and its owner. | 42 | * Find Tags. |
30 | * | 43 | * |
31 | * @param string $label | 44 | * @param int $userId |
32 | * @param int $userId | ||
33 | * | 45 | * |
34 | * @return Tag|null | 46 | * @return array |
35 | */ | 47 | */ |
36 | public function findOneByLabelAndUserId($label, $userId) | 48 | public function findAllTags($userId) |
37 | { | 49 | { |
38 | return $this->createQueryBuilder('t') | 50 | return $this->getQbForAllTags($userId) |
39 | ->where('t.label = :label')->setParameter('label', $label) | ||
40 | ->andWhere('t.user = :user_id')->setParameter('user_id', $userId) | ||
41 | ->getQuery() | 51 | ->getQuery() |
42 | ->getOneOrNullResult(); | 52 | ->getResult(); |
43 | } | 53 | } |
44 | } | 54 | } |
diff --git a/src/Wallabag/CoreBundle/Tests/Helper/RuleBasedTaggerTest.php b/src/Wallabag/CoreBundle/Tests/Helper/RuleBasedTaggerTest.php index 1de134b8..dee17d65 100644 --- a/src/Wallabag/CoreBundle/Tests/Helper/RuleBasedTaggerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Helper/RuleBasedTaggerTest.php | |||
@@ -69,9 +69,7 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase | |||
69 | 69 | ||
70 | $tags = $entry->getTags(); | 70 | $tags = $entry->getTags(); |
71 | $this->assertSame('foo', $tags[0]->getLabel()); | 71 | $this->assertSame('foo', $tags[0]->getLabel()); |
72 | $this->assertSame($user, $tags[0]->getUser()); | ||
73 | $this->assertSame('bar', $tags[1]->getLabel()); | 72 | $this->assertSame('bar', $tags[1]->getLabel()); |
74 | $this->assertSame($user, $tags[1]->getUser()); | ||
75 | } | 73 | } |
76 | 74 | ||
77 | public function testTagWithAMixOfMatchingRules() | 75 | public function testTagWithAMixOfMatchingRules() |
@@ -92,7 +90,6 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase | |||
92 | 90 | ||
93 | $tags = $entry->getTags(); | 91 | $tags = $entry->getTags(); |
94 | $this->assertSame('foo', $tags[0]->getLabel()); | 92 | $this->assertSame('foo', $tags[0]->getLabel()); |
95 | $this->assertSame($user, $tags[0]->getUser()); | ||
96 | } | 93 | } |
97 | 94 | ||
98 | public function testWhenTheTagExists() | 95 | public function testWhenTheTagExists() |
@@ -100,7 +97,7 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase | |||
100 | $taggingRule = $this->getTaggingRule('rule as string', array('foo')); | 97 | $taggingRule = $this->getTaggingRule('rule as string', array('foo')); |
101 | $user = $this->getUser([$taggingRule]); | 98 | $user = $this->getUser([$taggingRule]); |
102 | $entry = new Entry($user); | 99 | $entry = new Entry($user); |
103 | $tag = new Tag($user); | 100 | $tag = new Tag(); |
104 | 101 | ||
105 | $this->rulerz | 102 | $this->rulerz |
106 | ->expects($this->once()) | 103 | ->expects($this->once()) |
@@ -110,7 +107,9 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase | |||
110 | 107 | ||
111 | $this->tagRepository | 108 | $this->tagRepository |
112 | ->expects($this->once()) | 109 | ->expects($this->once()) |
113 | ->method('findOneByLabelAndUserId') | 110 | // the method `findOneByLabel` doesn't exist, EntityRepository will then call `_call` method |
111 | // to magically call the `findOneBy` with ['label' => 'foo'] | ||
112 | ->method('__call') | ||
114 | ->willReturn($tag); | 113 | ->willReturn($tag); |
115 | 114 | ||
116 | $this->tagger->tag($entry); | 115 | $this->tagger->tag($entry); |
diff --git a/src/Wallabag/UserBundle/Entity/User.php b/src/Wallabag/UserBundle/Entity/User.php index e3b9a519..e6528420 100644 --- a/src/Wallabag/UserBundle/Entity/User.php +++ b/src/Wallabag/UserBundle/Entity/User.php | |||
@@ -13,7 +13,6 @@ use JMS\Serializer\Annotation\Expose; | |||
13 | use FOS\UserBundle\Model\User as BaseUser; | 13 | use FOS\UserBundle\Model\User as BaseUser; |
14 | use Wallabag\CoreBundle\Entity\Config; | 14 | use Wallabag\CoreBundle\Entity\Config; |
15 | use Wallabag\CoreBundle\Entity\Entry; | 15 | use Wallabag\CoreBundle\Entity\Entry; |
16 | use Wallabag\CoreBundle\Entity\Tag; | ||
17 | 16 | ||
18 | /** | 17 | /** |
19 | * User. | 18 | * User. |
@@ -70,11 +69,6 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf | |||
70 | protected $config; | 69 | protected $config; |
71 | 70 | ||
72 | /** | 71 | /** |
73 | * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Tag", mappedBy="user", cascade={"remove"}) | ||
74 | */ | ||
75 | protected $tags; | ||
76 | |||
77 | /** | ||
78 | * @ORM\Column(type="integer", nullable=true) | 72 | * @ORM\Column(type="integer", nullable=true) |
79 | */ | 73 | */ |
80 | private $authCode; | 74 | private $authCode; |
@@ -94,7 +88,6 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf | |||
94 | { | 88 | { |
95 | parent::__construct(); | 89 | parent::__construct(); |
96 | $this->entries = new ArrayCollection(); | 90 | $this->entries = new ArrayCollection(); |
97 | $this->tags = new ArrayCollection(); | ||
98 | $this->roles = array('ROLE_USER'); | 91 | $this->roles = array('ROLE_USER'); |
99 | } | 92 | } |
100 | 93 | ||
@@ -171,26 +164,6 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf | |||
171 | return $this->entries; | 164 | return $this->entries; |
172 | } | 165 | } |
173 | 166 | ||
174 | /** | ||
175 | * @param Entry $entry | ||
176 | * | ||
177 | * @return User | ||
178 | */ | ||
179 | public function addTag(Tag $tag) | ||
180 | { | ||
181 | $this->tags[] = $tag; | ||
182 | |||
183 | return $this; | ||
184 | } | ||
185 | |||
186 | /** | ||
187 | * @return ArrayCollection<Tag> | ||
188 | */ | ||
189 | public function getTags() | ||
190 | { | ||
191 | return $this->tags; | ||
192 | } | ||
193 | |||
194 | public function isEqualTo(UserInterface $user) | 167 | public function isEqualTo(UserInterface $user) |
195 | { | 168 | { |
196 | return $this->username === $user->getUsername(); | 169 | return $this->username === $user->getUsername(); |