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