remove tag from entry #1377

This commit is contained in:
Thomas Citharel 2016-02-10 17:41:28 +01:00
parent ae5b37ef2e
commit 567421af50
5 changed files with 74 additions and 2 deletions

View file

@ -55,6 +55,26 @@ class TagController extends Controller
));
}
/**
* Removes tag from entry.
*
* @Route("/remove-tag/{entry}/{tag}", requirements={"entry" = "\d+", "tag" = "\d+"}, name="remove_tag")
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function removeTagFromEntry(Request $request, Entry $entry, Tag $tag)
{
$entry->removeTag($tag);
$em = $this->getDoctrine()->getManager();
$em->flush();
if (count($tag->getEntries()) == 0) {
$em->remove($tag);
}
$em->flush();
return $this->redirect($request->headers->get('referer'));
}
/**
* Shows tags for current user.
*

View file

@ -107,4 +107,14 @@ class Tag
{
return $this->entries->contains($entry);
}
/**
* Get entries for this tag.
*
* @return ArrayCollection<Entry>
*/
public function getEntries()
{
return $this->entries;
}
}

View file

@ -51,4 +51,20 @@ class TagRepository extends EntityRepository
->getQuery()
->getResult();
}
/**
* Used only in test case to get a tag for our entry.
*
* @return Tag
*/
public function findOnebyEntryAndLabel($entry, $label)
{
return $this->createQueryBuilder('t')
->leftJoin('t.entries', 'e')
->where('e.id = :entryId')->setParameter('entryId', $entry->getId())
->andWhere('t.label = :label')->setParameter('label', $label)
->setMaxResults(1)
->getQuery()
->getSingleResult();
}
}

View file

@ -159,7 +159,7 @@ main {
<div id="list">
{% for tag in entry.tags %}
<div class="chip">
{{ tag.label }}
{{ tag.label }} <a href="{{ path('remove_tag', { 'entry': entry.id, 'tag': tag.id }) }}"><i class="chip">✘</i></a>
</div>
{% endfor %}
</div>

View file

@ -6,6 +6,8 @@ use Wallabag\CoreBundle\Tests\WallabagCoreTestCase;
class TagControllerTest extends WallabagCoreTestCase
{
public $tagName = 'opensource';
public function testList()
{
$this->logInAs('admin');
@ -31,7 +33,7 @@ class TagControllerTest extends WallabagCoreTestCase
$form = $crawler->filter('button[id=tag_save]')->form();
$data = array(
'tag[label]' => 'opensource',
'tag[label]' => $this->tagName,
);
$client->submit($form, $data);
@ -65,4 +67,28 @@ class TagControllerTest extends WallabagCoreTestCase
$this->assertEquals(2, count($newEntry->getTags()));
}
public function testRemoveTagFromEntry()
{
$this->logInAs('admin');
$client = $this->getClient();
$entry = $client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry')
->findOneByUsernameAndNotArchived('admin');
$tag = $client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Tag')
->findOnebyEntryAndLabel($entry, $this->tagName);
$client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId());
$this->assertEquals(302, $client->getResponse()->getStatusCode());
$client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId());
$this->assertEquals(404, $client->getResponse()->getStatusCode());
}
}