]>
Commit | Line | Data |
---|---|---|
769e19dc J |
1 | <?php |
2 | ||
9744e971 | 3 | namespace Wallabag\ApiBundle\Tests\Controller; |
769e19dc | 4 | |
8a493541 | 5 | use Wallabag\ApiBundle\Tests\WallabagApiTestCase; |
769e19dc | 6 | |
8a493541 | 7 | class WallabagRestControllerTest extends WallabagApiTestCase |
769e19dc J |
8 | { |
9 | protected static $salt; | |
10 | ||
769e19dc J |
11 | public function testGetOneEntry() |
12 | { | |
fcb1fba5 | 13 | $entry = $this->client->getContainer() |
769e19dc J |
14 | ->get('doctrine.orm.entity_manager') |
15 | ->getRepository('WallabagCoreBundle:Entry') | |
16 | ->findOneBy(array('user' => 1, 'isArchived' => false)); | |
17 | ||
18 | if (!$entry) { | |
19 | $this->markTestSkipped('No content found in db.'); | |
20 | } | |
21 | ||
fcb1fba5 NL |
22 | $this->client->request('GET', '/api/entries/'.$entry->getId().'.json'); |
23 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
769e19dc | 24 | |
fcb1fba5 | 25 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
26 | |
27 | $this->assertEquals($entry->getTitle(), $content['title']); | |
28 | $this->assertEquals($entry->getUrl(), $content['url']); | |
29 | $this->assertCount(count($entry->getTags()), $content['tags']); | |
30 | ||
31 | $this->assertTrue( | |
fcb1fba5 | 32 | $this->client->getResponse()->headers->contains( |
769e19dc J |
33 | 'Content-Type', |
34 | 'application/json' | |
35 | ) | |
36 | ); | |
37 | } | |
38 | ||
39 | public function testGetOneEntryWrongUser() | |
40 | { | |
fcb1fba5 | 41 | $entry = $this->client->getContainer() |
769e19dc J |
42 | ->get('doctrine.orm.entity_manager') |
43 | ->getRepository('WallabagCoreBundle:Entry') | |
44 | ->findOneBy(array('user' => 2, 'isArchived' => false)); | |
45 | ||
46 | if (!$entry) { | |
47 | $this->markTestSkipped('No content found in db.'); | |
48 | } | |
49 | ||
fcb1fba5 | 50 | $this->client->request('GET', '/api/entries/'.$entry->getId().'.json'); |
769e19dc | 51 | |
fcb1fba5 | 52 | $this->assertEquals(403, $this->client->getResponse()->getStatusCode()); |
769e19dc J |
53 | } |
54 | ||
55 | public function testGetEntries() | |
56 | { | |
fcb1fba5 | 57 | $this->client->request('GET', '/api/entries'); |
769e19dc | 58 | |
fcb1fba5 | 59 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 60 | |
fcb1fba5 | 61 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
62 | |
63 | $this->assertGreaterThanOrEqual(1, count($content)); | |
64 | $this->assertNotEmpty($content['_embedded']['items']); | |
65 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
66 | $this->assertEquals(1, $content['page']); | |
67 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
68 | ||
69 | $this->assertTrue( | |
fcb1fba5 | 70 | $this->client->getResponse()->headers->contains( |
769e19dc J |
71 | 'Content-Type', |
72 | 'application/json' | |
73 | ) | |
74 | ); | |
75 | } | |
76 | ||
77 | public function testGetStarredEntries() | |
78 | { | |
fcb1fba5 | 79 | $this->client->request('GET', '/api/entries', array('star' => 1, 'sort' => 'updated')); |
769e19dc | 80 | |
fcb1fba5 | 81 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
e6f55346 | 82 | |
fcb1fba5 | 83 | $content = json_decode($this->client->getResponse()->getContent(), true); |
e6f55346 JB |
84 | |
85 | $this->assertGreaterThanOrEqual(1, count($content)); | |
86 | $this->assertNotEmpty($content['_embedded']['items']); | |
87 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
88 | $this->assertEquals(1, $content['page']); | |
89 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
90 | ||
91 | $this->assertTrue( | |
fcb1fba5 | 92 | $this->client->getResponse()->headers->contains( |
e6f55346 JB |
93 | 'Content-Type', |
94 | 'application/json' | |
95 | ) | |
96 | ); | |
97 | } | |
98 | ||
99 | public function testGetArchiveEntries() | |
100 | { | |
fcb1fba5 | 101 | $this->client->request('GET', '/api/entries', array('archive' => 1)); |
769e19dc | 102 | |
fcb1fba5 | 103 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 104 | |
fcb1fba5 | 105 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
106 | |
107 | $this->assertGreaterThanOrEqual(1, count($content)); | |
9744e971 J |
108 | $this->assertNotEmpty($content['_embedded']['items']); |
109 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
769e19dc | 110 | $this->assertEquals(1, $content['page']); |
9744e971 | 111 | $this->assertGreaterThanOrEqual(1, $content['pages']); |
769e19dc J |
112 | |
113 | $this->assertTrue( | |
fcb1fba5 | 114 | $this->client->getResponse()->headers->contains( |
769e19dc J |
115 | 'Content-Type', |
116 | 'application/json' | |
117 | ) | |
118 | ); | |
119 | } | |
120 | ||
121 | public function testDeleteEntry() | |
122 | { | |
fcb1fba5 | 123 | $entry = $this->client->getContainer() |
769e19dc J |
124 | ->get('doctrine.orm.entity_manager') |
125 | ->getRepository('WallabagCoreBundle:Entry') | |
126 | ->findOneByUser(1); | |
127 | ||
128 | if (!$entry) { | |
129 | $this->markTestSkipped('No content found in db.'); | |
130 | } | |
131 | ||
fcb1fba5 | 132 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json'); |
769e19dc | 133 | |
fcb1fba5 | 134 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 135 | |
fcb1fba5 | 136 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
137 | |
138 | $this->assertEquals($entry->getTitle(), $content['title']); | |
139 | $this->assertEquals($entry->getUrl(), $content['url']); | |
140 | ||
141 | // We'll try to delete this entry again | |
fcb1fba5 | 142 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json'); |
769e19dc | 143 | |
fcb1fba5 | 144 | $this->assertEquals(404, $this->client->getResponse()->getStatusCode()); |
769e19dc J |
145 | } |
146 | ||
147 | public function testPostEntry() | |
148 | { | |
fcb1fba5 | 149 | $this->client->request('POST', '/api/entries.json', array( |
769e19dc J |
150 | 'url' => 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', |
151 | 'tags' => 'google', | |
fcb1fba5 | 152 | )); |
769e19dc | 153 | |
fcb1fba5 | 154 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 155 | |
fcb1fba5 | 156 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
157 | |
158 | $this->assertGreaterThan(0, $content['id']); | |
159 | $this->assertEquals('http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', $content['url']); | |
160 | $this->assertEquals(false, $content['is_archived']); | |
161 | $this->assertEquals(false, $content['is_starred']); | |
162 | $this->assertCount(1, $content['tags']); | |
163 | } | |
164 | ||
165 | public function testPatchEntry() | |
166 | { | |
fcb1fba5 | 167 | $entry = $this->client->getContainer() |
769e19dc J |
168 | ->get('doctrine.orm.entity_manager') |
169 | ->getRepository('WallabagCoreBundle:Entry') | |
170 | ->findOneByUser(1); | |
171 | ||
172 | if (!$entry) { | |
173 | $this->markTestSkipped('No content found in db.'); | |
174 | } | |
175 | ||
176 | // hydrate the tags relations | |
177 | $nbTags = count($entry->getTags()); | |
178 | ||
fcb1fba5 | 179 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', array( |
769e19dc J |
180 | 'title' => 'New awesome title', |
181 | 'tags' => 'new tag '.uniqid(), | |
182 | 'star' => true, | |
183 | 'archive' => false, | |
fcb1fba5 | 184 | )); |
769e19dc | 185 | |
fcb1fba5 | 186 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 187 | |
fcb1fba5 | 188 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
189 | |
190 | $this->assertEquals($entry->getId(), $content['id']); | |
191 | $this->assertEquals($entry->getUrl(), $content['url']); | |
192 | $this->assertEquals('New awesome title', $content['title']); | |
193 | $this->assertGreaterThan($nbTags, count($content['tags'])); | |
194 | } | |
195 | ||
196 | public function testGetTagsEntry() | |
197 | { | |
fcb1fba5 | 198 | $entry = $this->client->getContainer() |
769e19dc J |
199 | ->get('doctrine.orm.entity_manager') |
200 | ->getRepository('WallabagCoreBundle:Entry') | |
201 | ->findOneWithTags(1); | |
202 | ||
203 | $entry = $entry[0]; | |
204 | ||
205 | if (!$entry) { | |
206 | $this->markTestSkipped('No content found in db.'); | |
207 | } | |
208 | ||
209 | $tags = array(); | |
210 | foreach ($entry->getTags() as $tag) { | |
211 | $tags[] = array('id' => $tag->getId(), 'label' => $tag->getLabel()); | |
212 | } | |
213 | ||
fcb1fba5 | 214 | $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags'); |
769e19dc | 215 | |
fcb1fba5 | 216 | $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent()); |
769e19dc J |
217 | } |
218 | ||
219 | public function testPostTagsOnEntry() | |
220 | { | |
fcb1fba5 | 221 | $entry = $this->client->getContainer() |
769e19dc J |
222 | ->get('doctrine.orm.entity_manager') |
223 | ->getRepository('WallabagCoreBundle:Entry') | |
224 | ->findOneByUser(1); | |
225 | ||
226 | if (!$entry) { | |
227 | $this->markTestSkipped('No content found in db.'); | |
228 | } | |
229 | ||
230 | $nbTags = count($entry->getTags()); | |
231 | ||
232 | $newTags = 'tag1,tag2,tag3'; | |
233 | ||
fcb1fba5 | 234 | $this->client->request('POST', '/api/entries/'.$entry->getId().'/tags', array('tags' => $newTags)); |
769e19dc | 235 | |
fcb1fba5 | 236 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 237 | |
fcb1fba5 | 238 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
239 | |
240 | $this->assertArrayHasKey('tags', $content); | |
4346a860 | 241 | $this->assertEquals($nbTags + 3, count($content['tags'])); |
769e19dc | 242 | |
fcb1fba5 | 243 | $entryDB = $this->client->getContainer() |
769e19dc J |
244 | ->get('doctrine.orm.entity_manager') |
245 | ->getRepository('WallabagCoreBundle:Entry') | |
246 | ->find($entry->getId()); | |
247 | ||
248 | $tagsInDB = array(); | |
249 | foreach ($entryDB->getTags()->toArray() as $tag) { | |
250 | $tagsInDB[$tag->getId()] = $tag->getLabel(); | |
251 | } | |
252 | ||
253 | foreach (explode(',', $newTags) as $tag) { | |
254 | $this->assertContains($tag, $tagsInDB); | |
255 | } | |
256 | } | |
257 | ||
fcb1fba5 | 258 | public function testDeleteOneTagEntry() |
769e19dc | 259 | { |
fcb1fba5 | 260 | $entry = $this->client->getContainer() |
769e19dc J |
261 | ->get('doctrine.orm.entity_manager') |
262 | ->getRepository('WallabagCoreBundle:Entry') | |
fcb1fba5 NL |
263 | ->findOneWithTags(1); |
264 | $entry = $entry[0]; | |
769e19dc J |
265 | |
266 | if (!$entry) { | |
267 | $this->markTestSkipped('No content found in db.'); | |
268 | } | |
269 | ||
270 | // hydrate the tags relations | |
271 | $nbTags = count($entry->getTags()); | |
272 | $tag = $entry->getTags()[0]; | |
273 | ||
fcb1fba5 | 274 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json'); |
769e19dc | 275 | |
fcb1fba5 | 276 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 277 | |
fcb1fba5 | 278 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
279 | |
280 | $this->assertArrayHasKey('tags', $content); | |
4346a860 | 281 | $this->assertEquals($nbTags - 1, count($content['tags'])); |
769e19dc J |
282 | } |
283 | ||
284 | public function testGetUserTags() | |
285 | { | |
fcb1fba5 | 286 | $this->client->request('GET', '/api/tags.json'); |
769e19dc | 287 | |
fcb1fba5 | 288 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 289 | |
fcb1fba5 | 290 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
291 | |
292 | $this->assertGreaterThan(0, $content); | |
293 | $this->assertArrayHasKey('id', $content[0]); | |
294 | $this->assertArrayHasKey('label', $content[0]); | |
295 | ||
296 | return end($content); | |
297 | } | |
298 | ||
299 | /** | |
300 | * @depends testGetUserTags | |
301 | */ | |
302 | public function testDeleteUserTag($tag) | |
303 | { | |
fcb1fba5 | 304 | $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json'); |
769e19dc | 305 | |
fcb1fba5 | 306 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 307 | |
fcb1fba5 | 308 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
309 | |
310 | $this->assertArrayHasKey('label', $content); | |
311 | $this->assertEquals($tag['label'], $content['label']); | |
312 | } | |
313 | } |