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