]>
Commit | Line | Data |
---|---|---|
d0b90fbe NL |
1 | <?php |
2 | ||
23634d5d | 3 | namespace Tests\Wallabag\CoreBundle\Controller; |
d0b90fbe | 4 | |
23634d5d | 5 | use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; |
d0b90fbe NL |
6 | |
7 | class TagControllerTest extends WallabagCoreTestCase | |
8 | { | |
567421af TC |
9 | public $tagName = 'opensource'; |
10 | ||
d0b90fbe NL |
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 | } | |
7244d6cb NL |
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') | |
74e1f743 | 29 | ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId()); |
7244d6cb NL |
30 | |
31 | $crawler = $client->request('GET', '/view/'.$entry->getId()); | |
32 | ||
0d42217e | 33 | $form = $crawler->filter('form[name=tag]')->form(); |
7244d6cb | 34 | |
4094ea47 | 35 | $data = [ |
567421af | 36 | 'tag[label]' => $this->tagName, |
4094ea47 | 37 | ]; |
7244d6cb NL |
38 | |
39 | $client->submit($form, $data); | |
40 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
41 | ||
fdc90ceb JB |
42 | // be sure to reload the entry |
43 | $entry = $client->getContainer() | |
44 | ->get('doctrine.orm.entity_manager') | |
45 | ->getRepository('WallabagCoreBundle:Entry') | |
74e1f743 | 46 | ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId()); |
fdc90ceb | 47 | |
74e1f743 | 48 | $this->assertEquals(3, count($entry->getTags())); |
7244d6cb | 49 | |
084fb0d3 | 50 | // tag already exists and already assigned |
7244d6cb NL |
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') | |
159986c4 | 57 | ->find($entry->getId()); |
7244d6cb | 58 | |
74e1f743 | 59 | $this->assertEquals(3, count($newEntry->getTags())); |
7244d6cb | 60 | |
084fb0d3 | 61 | // tag already exists but still not assigned to this entry |
4094ea47 | 62 | $data = [ |
7244d6cb | 63 | 'tag[label]' => 'foo', |
4094ea47 | 64 | ]; |
7244d6cb NL |
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') | |
159986c4 | 72 | ->find($entry->getId()); |
7244d6cb | 73 | |
74e1f743 | 74 | $this->assertEquals(3, count($newEntry->getTags())); |
7244d6cb | 75 | } |
567421af | 76 | |
2baca964 JB |
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') | |
74e1f743 | 85 | ->findByUrlAndUserId('http://0.0.0.0/entry2', $this->getLoggedInUserId()); |
2baca964 JB |
86 | |
87 | $crawler = $client->request('GET', '/view/'.$entry->getId()); | |
88 | ||
89 | $form = $crawler->filter('form[name=tag]')->form(); | |
90 | ||
4094ea47 | 91 | $data = [ |
2baca964 | 92 | 'tag[label]' => 'foo2, bar2', |
4094ea47 | 93 | ]; |
2baca964 JB |
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(); | |
74e1f743 JB |
104 | foreach ($tags as $key => $tag) { |
105 | $tags[$key] = $tag->getLabel(); | |
106 | } | |
107 | ||
2baca964 | 108 | $this->assertGreaterThanOrEqual(2, count($tags)); |
74e1f743 JB |
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'); | |
2baca964 JB |
111 | } |
112 | ||
567421af TC |
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') | |
74e1f743 | 121 | ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId()); |
567421af TC |
122 | |
123 | $tag = $client->getContainer() | |
124 | ->get('doctrine.orm.entity_manager') | |
125 | ->getRepository('WallabagCoreBundle:Tag') | |
e686a76d | 126 | ->findOneByEntryAndTagLabel($entry, $this->tagName); |
567421af TC |
127 | |
128 | $client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId()); | |
129 | ||
130 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | |
131 | ||
e686a76d TC |
132 | $this->assertNotContains($this->tagName, $entry->getTags()); |
133 | ||
567421af TC |
134 | $client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId()); |
135 | ||
136 | $this->assertEquals(404, $client->getResponse()->getStatusCode()); | |
137 | } | |
267e8d63 NL |
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') | |
74e1f743 | 147 | ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getLoggedInUserId()); |
267e8d63 NL |
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()); | |
74e1f743 | 167 | $this->assertCount(1, $crawler->filter('div[class=entry]')); |
267e8d63 | 168 | } |
d0b90fbe | 169 | } |