]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/Controller/TagControllerTest.php
TagController: ignore ActionMarkAsRead when removing tag from entry
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Controller / TagControllerTest.php
1 <?php
2
3 namespace Tests\Wallabag\CoreBundle\Controller;
4
5 use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
6 use Wallabag\CoreBundle\Entity\Tag;
7
8 class TagControllerTest extends WallabagCoreTestCase
9 {
10 public $tagName = 'opensource';
11
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 }
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')
30 ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
31
32 $crawler = $client->request('GET', '/view/'.$entry->getId());
33
34 $form = $crawler->filter('form[name=tag]')->form();
35
36 $data = [
37 'tag[label]' => $this->tagName,
38 ];
39
40 $client->submit($form, $data);
41 $this->assertEquals(302, $client->getResponse()->getStatusCode());
42
43 // be sure to reload the entry
44 $entry = $client->getContainer()
45 ->get('doctrine.orm.entity_manager')
46 ->getRepository('WallabagCoreBundle:Entry')
47 ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
48
49 $this->assertEquals(4, count($entry->getTags()));
50
51 // tag already exists and already assigned
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')
58 ->find($entry->getId());
59
60 $this->assertEquals(4, count($newEntry->getTags()));
61
62 // tag already exists but still not assigned to this entry
63 $data = [
64 'tag[label]' => 'foo bar',
65 ];
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')
73 ->find($entry->getId());
74
75 $this->assertEquals(4, count($newEntry->getTags()));
76 }
77
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')
86 ->findByUrlAndUserId('http://0.0.0.0/entry2', $this->getLoggedInUserId());
87
88 $crawler = $client->request('GET', '/view/'.$entry->getId());
89
90 $form = $crawler->filter('form[name=tag]')->form();
91
92 $data = [
93 'tag[label]' => 'foo2, bar2',
94 ];
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();
105 foreach ($tags as $key => $tag) {
106 $tags[$key] = $tag->getLabel();
107 }
108
109 $this->assertGreaterThanOrEqual(2, count($tags));
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');
112 }
113
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')
122 ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
123
124 $tag = $client->getContainer()
125 ->get('doctrine.orm.entity_manager')
126 ->getRepository('WallabagCoreBundle:Tag')
127 ->findOneByEntryAndTagLabel($entry, $this->tagName);
128
129 // We make a first request to set an history and test redirection after tag deletion
130 $client->request('GET', '/view/'.$entry->getId());
131 $entryUri = $client->getRequest()->getUri();
132 $client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId());
133
134 $this->assertEquals(302, $client->getResponse()->getStatusCode());
135 $this->assertEquals($entryUri, $client->getResponse()->getTargetUrl());
136
137 $this->assertNotContains($this->tagName, $entry->getTags());
138
139 $client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId());
140
141 $this->assertEquals(404, $client->getResponse()->getStatusCode());
142
143 $tag = $client->getContainer()
144 ->get('doctrine.orm.entity_manager')
145 ->getRepository('WallabagCoreBundle:Tag')
146 ->findOneByLabel($this->tagName);
147
148 $this->assertNull($tag, $this->tagName.' was removed because it begun an orphan tag');
149 }
150
151 public function testShowEntriesForTagAction()
152 {
153 $this->logInAs('admin');
154 $client = $this->getClient();
155 $em = $client->getContainer()
156 ->get('doctrine.orm.entity_manager');
157
158 $tag = new Tag();
159 $tag->setLabel($this->tagName);
160
161 $entry = $client->getContainer()
162 ->get('doctrine.orm.entity_manager')
163 ->getRepository('WallabagCoreBundle:Entry')
164 ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getLoggedInUserId());
165
166 $tag->addEntry($entry);
167
168 $em->persist($entry);
169 $em->persist($tag);
170 $em->flush();
171
172 $tag = $client->getContainer()
173 ->get('doctrine.orm.entity_manager')
174 ->getRepository('WallabagCoreBundle:Tag')
175 ->findOneByEntryAndTagLabel($entry, $this->tagName);
176
177 $crawler = $client->request('GET', '/tag/list/'.$tag->getSlug());
178
179 $this->assertEquals(200, $client->getResponse()->getStatusCode());
180 $this->assertCount(1, $crawler->filter('[id*="entry-"]'));
181
182 $entry->removeTag($tag);
183 $em->remove($tag);
184 $em->flush();
185 }
186 }