Merge pull request #1663 from wallabag/v2-remove-tags-from-entry

remove tag from entry #1377
This commit is contained in:
Jeremy Benoist 2016-02-11 16:49:03 +01:00
commit 9b5edf33a0
6 changed files with 77 additions and 3 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 findOneByEntryAndTagLabel($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

@ -9,7 +9,7 @@
<br />
<ul class="row data">
{% for tag in tags %}
<li id="tag-{{ tag.id|e }}" class="col l4 m6 s12">{{tag.label}}</li>
<li id="tag-{{ tag.id|e }}" class="col l4 m6 s12">{{tag.label}} ({{ tag.entries.getValues | length }})</li>
{% endfor %}
</ul>
{% endblock %}

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,30 @@ 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')
->findOneByEntryAndTagLabel($entry, $this->tagName);
$client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId());
$this->assertEquals(302, $client->getResponse()->getStatusCode());
$this->assertNotContains($this->tagName, $entry->getTags());
$client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId());
$this->assertEquals(404, $client->getResponse()->getStatusCode());
}
}