]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/CoreBundle/Controller/TagControllerTest.php
php-cs-fixer
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Controller / TagControllerTest.php
CommitLineData
d0b90fbe
NL
1<?php
2
23634d5d 3namespace Tests\Wallabag\CoreBundle\Controller;
d0b90fbe 4
23634d5d 5use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
7ab5eb95 6use Wallabag\CoreBundle\Entity\Entry;
ac8cf632 7use Wallabag\CoreBundle\Entity\Tag;
d0b90fbe
NL
8
9class TagControllerTest extends WallabagCoreTestCase
10{
567421af 11 public $tagName = 'opensource';
7036d91f 12 public $caseTagName = 'OpenSource';
567421af 13
d0b90fbe
NL
14 public function testList()
15 {
16 $this->logInAs('admin');
17 $client = $this->getClient();
18
19 $client->request('GET', '/tag/list');
20
f808b016 21 $this->assertSame(200, $client->getResponse()->getStatusCode());
d0b90fbe 22 }
7244d6cb
NL
23
24 public function testAddTagToEntry()
25 {
26 $this->logInAs('admin');
27 $client = $this->getClient();
28
7ab5eb95 29 $entry = new Entry($this->getLoggedInUser());
30 $entry->setUrl('http://0.0.0.0/foo');
31 $this->getEntityManager()->persist($entry);
32 $this->getEntityManager()->flush();
33 $this->getEntityManager()->clear();
7244d6cb 34
f808b016 35 $crawler = $client->request('GET', '/view/' . $entry->getId());
7244d6cb 36
0d42217e 37 $form = $crawler->filter('form[name=tag]')->form();
7244d6cb 38
4094ea47 39 $data = [
7036d91f 40 'tag[label]' => $this->caseTagName,
4094ea47 41 ];
7244d6cb
NL
42
43 $client->submit($form, $data);
f808b016 44 $this->assertSame(302, $client->getResponse()->getStatusCode());
7244d6cb 45
fdc90ceb 46 // be sure to reload the entry
7ab5eb95 47 $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
48 $this->assertCount(1, $entry->getTags());
7036d91f 49 $this->assertContains($this->tagName, $entry->getTags());
7244d6cb 50
084fb0d3 51 // tag already exists and already assigned
7244d6cb 52 $client->submit($form, $data);
f808b016 53 $this->assertSame(302, $client->getResponse()->getStatusCode());
7244d6cb 54
7ab5eb95 55 $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
56 $this->assertCount(1, $entry->getTags());
7244d6cb 57
084fb0d3 58 // tag already exists but still not assigned to this entry
4094ea47 59 $data = [
c8de7ab9 60 'tag[label]' => 'foo bar',
4094ea47 61 ];
7244d6cb
NL
62
63 $client->submit($form, $data);
f808b016 64 $this->assertSame(302, $client->getResponse()->getStatusCode());
7244d6cb 65
7ab5eb95 66 $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
67 $this->assertCount(2, $entry->getTags());
7244d6cb 68 }
567421af 69
2baca964
JB
70 public function testAddMultipleTagToEntry()
71 {
72 $this->logInAs('admin');
73 $client = $this->getClient();
74
75 $entry = $client->getContainer()
76 ->get('doctrine.orm.entity_manager')
77 ->getRepository('WallabagCoreBundle:Entry')
74e1f743 78 ->findByUrlAndUserId('http://0.0.0.0/entry2', $this->getLoggedInUserId());
2baca964 79
f808b016 80 $crawler = $client->request('GET', '/view/' . $entry->getId());
2baca964
JB
81
82 $form = $crawler->filter('form[name=tag]')->form();
83
4094ea47 84 $data = [
7036d91f 85 'tag[label]' => 'foo2, Bar2',
4094ea47 86 ];
2baca964
JB
87
88 $client->submit($form, $data);
f808b016 89 $this->assertSame(302, $client->getResponse()->getStatusCode());
2baca964
JB
90
91 $newEntry = $client->getContainer()
92 ->get('doctrine.orm.entity_manager')
93 ->getRepository('WallabagCoreBundle:Entry')
94 ->find($entry->getId());
95
96 $tags = $newEntry->getTags()->toArray();
74e1f743
JB
97 foreach ($tags as $key => $tag) {
98 $tags[$key] = $tag->getLabel();
99 }
100
2a1ceb67 101 $this->assertGreaterThanOrEqual(2, \count($tags));
f808b016
JB
102 $this->assertNotFalse(array_search('foo2', $tags, true), 'Tag foo2 is assigned to the entry');
103 $this->assertNotFalse(array_search('bar2', $tags, true), 'Tag bar2 is assigned to the entry');
2baca964
JB
104 }
105
567421af
TC
106 public function testRemoveTagFromEntry()
107 {
108 $this->logInAs('admin');
109 $client = $this->getClient();
110
7ab5eb95 111 $tag = new Tag();
112 $tag->setLabel($this->tagName);
113 $entry = new Entry($this->getLoggedInUser());
114 $entry->setUrl('http://0.0.0.0/foo');
115 $entry->addTag($tag);
116 $this->getEntityManager()->persist($entry);
117 $this->getEntityManager()->flush();
118 $this->getEntityManager()->clear();
567421af 119
5dbf3f23 120 // We make a first request to set an history and test redirection after tag deletion
f808b016 121 $client->request('GET', '/view/' . $entry->getId());
5dbf3f23 122 $entryUri = $client->getRequest()->getUri();
f808b016 123 $client->request('GET', '/remove-tag/' . $entry->getId() . '/' . $tag->getId());
567421af 124
f808b016
JB
125 $this->assertSame(302, $client->getResponse()->getStatusCode());
126 $this->assertSame($entryUri, $client->getResponse()->getTargetUrl());
567421af 127
6acadf8e 128 // re-retrieve the entry to be sure to get fresh data from database (mostly for tags)
7ab5eb95 129 $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
e686a76d
TC
130 $this->assertNotContains($this->tagName, $entry->getTags());
131
f808b016 132 $client->request('GET', '/remove-tag/' . $entry->getId() . '/' . $tag->getId());
567421af 133
f808b016 134 $this->assertSame(404, $client->getResponse()->getStatusCode());
ac8cf632
JB
135
136 $tag = $client->getContainer()
137 ->get('doctrine.orm.entity_manager')
138 ->getRepository('WallabagCoreBundle:Tag')
139 ->findOneByLabel($this->tagName);
140
f808b016 141 $this->assertNull($tag, $this->tagName . ' was removed because it begun an orphan tag');
567421af 142 }
267e8d63
NL
143
144 public function testShowEntriesForTagAction()
145 {
146 $this->logInAs('admin');
147 $client = $this->getClient();
ac8cf632
JB
148 $em = $client->getContainer()
149 ->get('doctrine.orm.entity_manager');
150
151 $tag = new Tag();
152 $tag->setLabel($this->tagName);
267e8d63
NL
153
154 $entry = $client->getContainer()
155 ->get('doctrine.orm.entity_manager')
156 ->getRepository('WallabagCoreBundle:Entry')
74e1f743 157 ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getLoggedInUserId());
267e8d63 158
ac8cf632 159 $tag->addEntry($entry);
267e8d63 160
ac8cf632
JB
161 $em->persist($entry);
162 $em->persist($tag);
163 $em->flush();
267e8d63
NL
164
165 $tag = $client->getContainer()
166 ->get('doctrine.orm.entity_manager')
167 ->getRepository('WallabagCoreBundle:Tag')
ac8cf632 168 ->findOneByEntryAndTagLabel($entry, $this->tagName);
267e8d63 169
f808b016 170 $crawler = $client->request('GET', '/tag/list/' . $tag->getSlug());
267e8d63 171
f808b016 172 $this->assertSame(200, $client->getResponse()->getStatusCode());
ac8cf632
JB
173 $this->assertCount(1, $crawler->filter('[id*="entry-"]'));
174
175 $entry->removeTag($tag);
176 $em->remove($tag);
177 $em->flush();
267e8d63 178 }
d0b90fbe 179}