public function testDeleteTagsByLabel() { $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')->getRepository('WallabagCoreBundle:Entry')->findOneWithTags($this->user->getId()); $entry = $entry[0]; $tag = new Tag(); $tag->setLabel('Awesome tag for tagsLabel'); $em->persist($tag); $tag2 = new Tag(); $tag2->setLabel('Awesome tag for tagsLabel 2'); $em->persist($tag2); $entry->addTag($tag); $entry->addTag($tag2); $em->persist($entry); $em->flush(); $this->client->request('DELETE', '/api/tags/label.json', ['tags' => $tag->getLabel() . ',' . $tag2->getLabel()]); $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); $content = json_decode($this->client->getResponse()->getContent(), true); $this->assertCount(2, $content); $this->assertArrayHasKey('label', $content[0]); $this->assertEquals($tag->getLabel(), $content[0]['label']); $this->assertEquals($tag->getSlug(), $content[0]['slug']); $this->assertArrayHasKey('label', $content[1]); $this->assertEquals($tag2->getLabel(), $content[1]['label']); $this->assertEquals($tag2->getSlug(), $content[1]['slug']); $entries = $this->client->getContainer()->get('doctrine.orm.entity_manager')->getRepository('WallabagCoreBundle:Entry')->findAllByTagId($this->user->getId(), $tag->getId()); $this->assertCount(0, $entries); $entries = $this->client->getContainer()->get('doctrine.orm.entity_manager')->getRepository('WallabagCoreBundle:Entry')->findAllByTagId($this->user->getId(), $tag2->getId()); $this->assertCount(0, $entries); }
/** * Fetch a tag. * * @param string $label The tag's label * * @return Tag */ private function getTag($label) { $tag = $this->tagRepository->findOneByLabel($label); if (!$tag) { $tag = new Tag(); $tag->setLabel($label); } return $tag; }
/** * {@inheritdoc} */ public function load(ObjectManager $manager) { $tag1 = new Tag(); $tag1->setLabel('foo'); $manager->persist($tag1); $this->addReference('foo-tag', $tag1); $tag2 = new Tag(); $tag2->setLabel('bar'); $manager->persist($tag2); $this->addReference('bar-tag', $tag2); $manager->flush(); }
/** * @param Tag $tag * @param int $page * * @Route("/tag/list/{slug}/{page}", name="tag_entries", defaults={"page" = "1"}) * @ParamConverter("tag", options={"mapping": {"slug": "slug"}}) * * @return \Symfony\Component\HttpFoundation\Response */ public function showEntriesForTagAction(Tag $tag, $page, Request $request) { $entriesByTag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry')->findAllByTagId($this->getUser()->getId(), $tag->getId()); $pagerAdapter = new ArrayAdapter($entriesByTag); $entries = $this->get('wallabag_core.helper.prepare_pager_for_entries')->prepare($pagerAdapter, $page); try { $entries->setCurrentPage($page); } catch (OutOfRangeCurrentPageException $e) { if ($page > 1) { return $this->redirect($this->generateUrl($request->get('_route'), ['slug' => $tag->getSlug(), 'page' => $entries->getNbPages()]), 302); } } return $this->render('WallabagCoreBundle:Entry:entries.html.twig', ['form' => null, 'entries' => $entries, 'currentPage' => $page]); }
public function testShowEntriesForTagAction() { $this->logInAs('admin'); $client = $this->getClient(); $em = $client->getContainer()->get('doctrine.orm.entity_manager'); $tag = new Tag(); $tag->setLabel($this->tagName); $entry = $client->getContainer()->get('doctrine.orm.entity_manager')->getRepository('WallabagCoreBundle:Entry')->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getLoggedInUserId()); $tag->addEntry($entry); $em->persist($entry); $em->persist($tag); $em->flush(); $tag = $client->getContainer()->get('doctrine.orm.entity_manager')->getRepository('WallabagCoreBundle:Tag')->findOneByEntryAndTagLabel($entry, $this->tagName); $crawler = $client->request('GET', '/tag/list/' . $tag->getSlug()); $this->assertEquals(200, $client->getResponse()->getStatusCode()); $this->assertCount(1, $crawler->filter('[id*="entry-"]')); $entry->removeTag($tag); $em->remove($tag); $em->flush(); }
/** * Assign some tags to an entry. * * @param Entry $entry * @param array|string $tags An array of tag or a string coma separated of tag * @param array $entitiesReady Entities from the EntityManager which are persisted but not yet flushed * It is mostly to fix duplicate tag on import @see http://stackoverflow.com/a/7879164/569101 */ public function assignTagsToEntry(Entry $entry, $tags, array $entitiesReady = []) { if (!is_array($tags)) { $tags = explode(',', $tags); } // keeps only Tag entity from the "not yet flushed entities" $tagsNotYetFlushed = []; foreach ($entitiesReady as $entity) { if ($entity instanceof Tag) { $tagsNotYetFlushed[$entity->getLabel()] = $entity; } } foreach ($tags as $label) { $label = trim($label); // avoid empty tag if (0 === strlen($label)) { continue; } if (isset($tagsNotYetFlushed[$label])) { $tagEntity = $tagsNotYetFlushed[$label]; } else { $tagEntity = $this->tagRepository->findOneByLabel($label); if (is_null($tagEntity)) { $tagEntity = new Tag(); $tagEntity->setLabel($label); } } // only add the tag on the entry if the relation doesn't exist if (false === $entry->getTags()->contains($tagEntity)) { $entry->addTag($tagEntity); } } }
public function testAssignTagsNotFlushed() { $graby = $this->getMockBuilder('Graby\\Graby')->disableOriginalConstructor()->getMock(); $tagRepo = $this->getTagRepositoryMock(); $tagRepo->expects($this->never())->method('__call'); $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger()); $tagEntity = new Tag(); $tagEntity->setLabel('tag1'); $entry = new Entry(new User()); $proxy->assignTagsToEntry($entry, 'tag1', [$tagEntity]); $this->assertCount(1, $entry->getTags()); $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); }
/** * Assign some tags to an entry. * * @param Entry $entry * @param array|string $tags An array of tag or a string coma separated of tag */ public function assignTagsToEntry(Entry $entry, $tags) { if (!is_array($tags)) { $tags = explode(',', $tags); } foreach ($tags as $label) { $label = trim($label); // avoid empty tag if (0 === strlen($label)) { continue; } $tagEntity = $this->tagRepository->findOneByLabel($label); if (is_null($tagEntity)) { $tagEntity = new Tag(); $tagEntity->setLabel($label); } // only add the tag on the entry if the relation doesn't exist if (false === $entry->getTags()->contains($tagEntity)) { $entry->addTag($tagEntity); } } }
/** * Remove a tag from all user entries. * * 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. * 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: * * 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 * * @param int $userId * @param Tag $tag */ public function removeTag($userId, Tag $tag) { $entries = $this->getBuilderByUser($userId)->innerJoin('e.tags', 't')->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())->getQuery()->getResult(); foreach ($entries as $entry) { $entry->removeTag($tag); } $this->getEntityManager()->flush(); }
public function testAssignTagsAlreadyAssigned() { $graby = $this->getMockBuilder('Graby\\Graby')->disableOriginalConstructor()->getMock(); $tagRepo = $this->getTagRepositoryMock(); $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger()); $tagEntity = new Tag(); $tagEntity->setLabel('tag1'); $entry = new Entry(new User()); $entry->addTag($tagEntity); $proxy->assignTagsToEntry($entry, 'tag1, tag2'); $this->assertCount(2, $entry->getTags()); $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); }