]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/Controller/TagControllerTest.php
Fix tag test
[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
7 class TagControllerTest extends WallabagCoreTestCase
8 {
9 public $tagName = 'opensource';
10
11 public function testList()
12 {
13 $this->logInAs('admin');
14 $client = $this->getClient();
15
16 $client->request('GET', '/tag/list');
17
18 $this->assertEquals(200, $client->getResponse()->getStatusCode());
19 }
20
21 public function testAddTagToEntry()
22 {
23 $this->logInAs('admin');
24 $client = $this->getClient();
25
26 $entry = $client->getContainer()
27 ->get('doctrine.orm.entity_manager')
28 ->getRepository('WallabagCoreBundle:Entry')
29 ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
30
31 $crawler = $client->request('GET', '/view/'.$entry->getId());
32
33 $form = $crawler->filter('form[name=tag]')->form();
34
35 $data = [
36 'tag[label]' => $this->tagName,
37 ];
38
39 $client->submit($form, $data);
40 $this->assertEquals(302, $client->getResponse()->getStatusCode());
41
42 // be sure to reload the entry
43 $entry = $client->getContainer()
44 ->get('doctrine.orm.entity_manager')
45 ->getRepository('WallabagCoreBundle:Entry')
46 ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
47
48 $this->assertEquals(3, count($entry->getTags()));
49
50 // tag already exists and already assigned
51 $client->submit($form, $data);
52 $this->assertEquals(302, $client->getResponse()->getStatusCode());
53
54 $newEntry = $client->getContainer()
55 ->get('doctrine.orm.entity_manager')
56 ->getRepository('WallabagCoreBundle:Entry')
57 ->find($entry->getId());
58
59 $this->assertEquals(3, count($newEntry->getTags()));
60
61 // tag already exists but still not assigned to this entry
62 $data = [
63 'tag[label]' => 'foo',
64 ];
65
66 $client->submit($form, $data);
67 $this->assertEquals(302, $client->getResponse()->getStatusCode());
68
69 $newEntry = $client->getContainer()
70 ->get('doctrine.orm.entity_manager')
71 ->getRepository('WallabagCoreBundle:Entry')
72 ->find($entry->getId());
73
74 $this->assertEquals(3, count($newEntry->getTags()));
75 }
76
77 public function testAddMultipleTagToEntry()
78 {
79 $this->logInAs('admin');
80 $client = $this->getClient();
81
82 $entry = $client->getContainer()
83 ->get('doctrine.orm.entity_manager')
84 ->getRepository('WallabagCoreBundle:Entry')
85 ->findByUrlAndUserId('http://0.0.0.0/entry2', $this->getLoggedInUserId());
86
87 $crawler = $client->request('GET', '/view/'.$entry->getId());
88
89 $form = $crawler->filter('form[name=tag]')->form();
90
91 $data = [
92 'tag[label]' => 'foo2, bar2',
93 ];
94
95 $client->submit($form, $data);
96 $this->assertEquals(302, $client->getResponse()->getStatusCode());
97
98 $newEntry = $client->getContainer()
99 ->get('doctrine.orm.entity_manager')
100 ->getRepository('WallabagCoreBundle:Entry')
101 ->find($entry->getId());
102
103 $tags = $newEntry->getTags()->toArray();
104 foreach ($tags as $key => $tag) {
105 $tags[$key] = $tag->getLabel();
106 }
107
108 $this->assertGreaterThanOrEqual(2, count($tags));
109 $this->assertNotFalse(array_search('foo2', $tags), 'Tag foo2 is assigned to the entry');
110 $this->assertNotFalse(array_search('bar2', $tags), 'Tag bar2 is assigned to the entry');
111 }
112
113 public function testRemoveTagFromEntry()
114 {
115 $this->logInAs('admin');
116 $client = $this->getClient();
117
118 $entry = $client->getContainer()
119 ->get('doctrine.orm.entity_manager')
120 ->getRepository('WallabagCoreBundle:Entry')
121 ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
122
123 $tag = $client->getContainer()
124 ->get('doctrine.orm.entity_manager')
125 ->getRepository('WallabagCoreBundle:Tag')
126 ->findOneByEntryAndTagLabel($entry, $this->tagName);
127
128 $client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId());
129
130 $this->assertEquals(302, $client->getResponse()->getStatusCode());
131
132 $this->assertNotContains($this->tagName, $entry->getTags());
133
134 $client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId());
135
136 $this->assertEquals(404, $client->getResponse()->getStatusCode());
137 }
138
139 public function testShowEntriesForTagAction()
140 {
141 $this->logInAs('admin');
142 $client = $this->getClient();
143
144 $entry = $client->getContainer()
145 ->get('doctrine.orm.entity_manager')
146 ->getRepository('WallabagCoreBundle:Entry')
147 ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getLoggedInUserId());
148
149 $tag = $client->getContainer()
150 ->get('doctrine.orm.entity_manager')
151 ->getRepository('WallabagCoreBundle:Tag')
152 ->findOneByEntryAndTagLabel($entry, 'foo');
153
154 $crawler = $client->request('GET', '/tag/list/'.$tag->getSlug());
155
156 $this->assertEquals(200, $client->getResponse()->getStatusCode());
157 $this->assertCount(2, $crawler->filter('div[class=entry]'));
158
159 $tag = $client->getContainer()
160 ->get('doctrine.orm.entity_manager')
161 ->getRepository('WallabagCoreBundle:Tag')
162 ->findOneByLabel('baz');
163
164 $crawler = $client->request('GET', '/tag/list/'.$tag->getSlug());
165
166 $this->assertEquals(200, $client->getResponse()->getStatusCode());
167 $this->assertCount(1, $crawler->filter('div[class=entry]'));
168 }
169 }