]>
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']); | |
bc44aa57 TC |
30 | $this->assertEquals($entry->getUserName(), $content['user_name']); |
31 | $this->assertEquals($entry->getUserEmail(), $content['user_email']); | |
32 | $this->assertEquals($entry->getUserId(), $content['user_id']); | |
769e19dc J |
33 | |
34 | $this->assertTrue( | |
fcb1fba5 | 35 | $this->client->getResponse()->headers->contains( |
769e19dc J |
36 | 'Content-Type', |
37 | 'application/json' | |
38 | ) | |
39 | ); | |
40 | } | |
41 | ||
42 | public function testGetOneEntryWrongUser() | |
43 | { | |
fcb1fba5 | 44 | $entry = $this->client->getContainer() |
769e19dc J |
45 | ->get('doctrine.orm.entity_manager') |
46 | ->getRepository('WallabagCoreBundle:Entry') | |
47 | ->findOneBy(array('user' => 2, 'isArchived' => false)); | |
48 | ||
49 | if (!$entry) { | |
50 | $this->markTestSkipped('No content found in db.'); | |
51 | } | |
52 | ||
fcb1fba5 | 53 | $this->client->request('GET', '/api/entries/'.$entry->getId().'.json'); |
769e19dc | 54 | |
fcb1fba5 | 55 | $this->assertEquals(403, $this->client->getResponse()->getStatusCode()); |
769e19dc J |
56 | } |
57 | ||
58 | public function testGetEntries() | |
59 | { | |
fcb1fba5 | 60 | $this->client->request('GET', '/api/entries'); |
769e19dc | 61 | |
fcb1fba5 | 62 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 63 | |
fcb1fba5 | 64 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
65 | |
66 | $this->assertGreaterThanOrEqual(1, count($content)); | |
67 | $this->assertNotEmpty($content['_embedded']['items']); | |
68 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
69 | $this->assertEquals(1, $content['page']); | |
70 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
71 | ||
72 | $this->assertTrue( | |
fcb1fba5 | 73 | $this->client->getResponse()->headers->contains( |
769e19dc J |
74 | 'Content-Type', |
75 | 'application/json' | |
76 | ) | |
77 | ); | |
78 | } | |
79 | ||
80 | public function testGetStarredEntries() | |
81 | { | |
fcb1fba5 | 82 | $this->client->request('GET', '/api/entries', array('star' => 1, 'sort' => 'updated')); |
769e19dc | 83 | |
fcb1fba5 | 84 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
e6f55346 | 85 | |
fcb1fba5 | 86 | $content = json_decode($this->client->getResponse()->getContent(), true); |
e6f55346 JB |
87 | |
88 | $this->assertGreaterThanOrEqual(1, count($content)); | |
89 | $this->assertNotEmpty($content['_embedded']['items']); | |
90 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
91 | $this->assertEquals(1, $content['page']); | |
92 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
93 | ||
94 | $this->assertTrue( | |
fcb1fba5 | 95 | $this->client->getResponse()->headers->contains( |
e6f55346 JB |
96 | 'Content-Type', |
97 | 'application/json' | |
98 | ) | |
99 | ); | |
100 | } | |
101 | ||
102 | public function testGetArchiveEntries() | |
103 | { | |
fcb1fba5 | 104 | $this->client->request('GET', '/api/entries', array('archive' => 1)); |
769e19dc | 105 | |
fcb1fba5 | 106 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 107 | |
fcb1fba5 | 108 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
109 | |
110 | $this->assertGreaterThanOrEqual(1, count($content)); | |
9744e971 J |
111 | $this->assertNotEmpty($content['_embedded']['items']); |
112 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
769e19dc | 113 | $this->assertEquals(1, $content['page']); |
9744e971 | 114 | $this->assertGreaterThanOrEqual(1, $content['pages']); |
769e19dc J |
115 | |
116 | $this->assertTrue( | |
fcb1fba5 | 117 | $this->client->getResponse()->headers->contains( |
769e19dc J |
118 | 'Content-Type', |
119 | 'application/json' | |
120 | ) | |
121 | ); | |
122 | } | |
123 | ||
124 | public function testDeleteEntry() | |
125 | { | |
fcb1fba5 | 126 | $entry = $this->client->getContainer() |
769e19dc J |
127 | ->get('doctrine.orm.entity_manager') |
128 | ->getRepository('WallabagCoreBundle:Entry') | |
129 | ->findOneByUser(1); | |
130 | ||
131 | if (!$entry) { | |
132 | $this->markTestSkipped('No content found in db.'); | |
133 | } | |
134 | ||
fcb1fba5 | 135 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json'); |
769e19dc | 136 | |
fcb1fba5 | 137 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 138 | |
fcb1fba5 | 139 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
140 | |
141 | $this->assertEquals($entry->getTitle(), $content['title']); | |
142 | $this->assertEquals($entry->getUrl(), $content['url']); | |
143 | ||
144 | // We'll try to delete this entry again | |
fcb1fba5 | 145 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json'); |
769e19dc | 146 | |
fcb1fba5 | 147 | $this->assertEquals(404, $this->client->getResponse()->getStatusCode()); |
769e19dc J |
148 | } |
149 | ||
150 | public function testPostEntry() | |
151 | { | |
fcb1fba5 | 152 | $this->client->request('POST', '/api/entries.json', array( |
769e19dc J |
153 | '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', |
154 | 'tags' => 'google', | |
fcb1fba5 | 155 | )); |
769e19dc | 156 | |
fcb1fba5 | 157 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 158 | |
fcb1fba5 | 159 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
160 | |
161 | $this->assertGreaterThan(0, $content['id']); | |
162 | $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']); | |
163 | $this->assertEquals(false, $content['is_archived']); | |
164 | $this->assertEquals(false, $content['is_starred']); | |
bc44aa57 | 165 | $this->assertEquals(1, $content['user_id']); |
769e19dc J |
166 | $this->assertCount(1, $content['tags']); |
167 | } | |
168 | ||
09d8bb6f | 169 | public function testPostArchivedAndStarredEntry() |
816ad405 TC |
170 | { |
171 | $this->client->request('POST', '/api/entries.json', array( | |
172 | 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', | |
173 | 'archive' => true, | |
09d8bb6f | 174 | 'starred' => true, |
816ad405 TC |
175 | )); |
176 | ||
177 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
178 | ||
179 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
180 | ||
181 | $this->assertGreaterThan(0, $content['id']); | |
182 | $this->assertEquals('http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']); | |
183 | $this->assertEquals(true, $content['is_archived']); | |
09d8bb6f | 184 | $this->assertEquals(true, $content['is_starred']); |
bc44aa57 | 185 | $this->assertEquals(1, $content['user_id']); |
816ad405 TC |
186 | } |
187 | ||
769e19dc J |
188 | public function testPatchEntry() |
189 | { | |
fcb1fba5 | 190 | $entry = $this->client->getContainer() |
769e19dc J |
191 | ->get('doctrine.orm.entity_manager') |
192 | ->getRepository('WallabagCoreBundle:Entry') | |
193 | ->findOneByUser(1); | |
194 | ||
195 | if (!$entry) { | |
196 | $this->markTestSkipped('No content found in db.'); | |
197 | } | |
198 | ||
199 | // hydrate the tags relations | |
200 | $nbTags = count($entry->getTags()); | |
201 | ||
fcb1fba5 | 202 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', array( |
769e19dc J |
203 | 'title' => 'New awesome title', |
204 | 'tags' => 'new tag '.uniqid(), | |
205 | 'star' => true, | |
206 | 'archive' => false, | |
fcb1fba5 | 207 | )); |
769e19dc | 208 | |
fcb1fba5 | 209 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 210 | |
fcb1fba5 | 211 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
212 | |
213 | $this->assertEquals($entry->getId(), $content['id']); | |
214 | $this->assertEquals($entry->getUrl(), $content['url']); | |
215 | $this->assertEquals('New awesome title', $content['title']); | |
216 | $this->assertGreaterThan($nbTags, count($content['tags'])); | |
bc44aa57 | 217 | $this->assertEquals(1, $content['user_id']); |
769e19dc J |
218 | } |
219 | ||
220 | public function testGetTagsEntry() | |
221 | { | |
fcb1fba5 | 222 | $entry = $this->client->getContainer() |
769e19dc J |
223 | ->get('doctrine.orm.entity_manager') |
224 | ->getRepository('WallabagCoreBundle:Entry') | |
225 | ->findOneWithTags(1); | |
226 | ||
227 | $entry = $entry[0]; | |
228 | ||
229 | if (!$entry) { | |
230 | $this->markTestSkipped('No content found in db.'); | |
231 | } | |
232 | ||
233 | $tags = array(); | |
234 | foreach ($entry->getTags() as $tag) { | |
fc732227 | 235 | $tags[] = array('id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()); |
769e19dc J |
236 | } |
237 | ||
fcb1fba5 | 238 | $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags'); |
769e19dc | 239 | |
fcb1fba5 | 240 | $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent()); |
769e19dc J |
241 | } |
242 | ||
243 | public function testPostTagsOnEntry() | |
244 | { | |
fcb1fba5 | 245 | $entry = $this->client->getContainer() |
769e19dc J |
246 | ->get('doctrine.orm.entity_manager') |
247 | ->getRepository('WallabagCoreBundle:Entry') | |
248 | ->findOneByUser(1); | |
249 | ||
250 | if (!$entry) { | |
251 | $this->markTestSkipped('No content found in db.'); | |
252 | } | |
253 | ||
254 | $nbTags = count($entry->getTags()); | |
255 | ||
256 | $newTags = 'tag1,tag2,tag3'; | |
257 | ||
fcb1fba5 | 258 | $this->client->request('POST', '/api/entries/'.$entry->getId().'/tags', array('tags' => $newTags)); |
769e19dc | 259 | |
fcb1fba5 | 260 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 261 | |
fcb1fba5 | 262 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
263 | |
264 | $this->assertArrayHasKey('tags', $content); | |
4346a860 | 265 | $this->assertEquals($nbTags + 3, count($content['tags'])); |
769e19dc | 266 | |
fcb1fba5 | 267 | $entryDB = $this->client->getContainer() |
769e19dc J |
268 | ->get('doctrine.orm.entity_manager') |
269 | ->getRepository('WallabagCoreBundle:Entry') | |
270 | ->find($entry->getId()); | |
271 | ||
272 | $tagsInDB = array(); | |
273 | foreach ($entryDB->getTags()->toArray() as $tag) { | |
274 | $tagsInDB[$tag->getId()] = $tag->getLabel(); | |
275 | } | |
276 | ||
277 | foreach (explode(',', $newTags) as $tag) { | |
278 | $this->assertContains($tag, $tagsInDB); | |
279 | } | |
280 | } | |
281 | ||
fcb1fba5 | 282 | public function testDeleteOneTagEntry() |
769e19dc | 283 | { |
fcb1fba5 | 284 | $entry = $this->client->getContainer() |
769e19dc J |
285 | ->get('doctrine.orm.entity_manager') |
286 | ->getRepository('WallabagCoreBundle:Entry') | |
fcb1fba5 NL |
287 | ->findOneWithTags(1); |
288 | $entry = $entry[0]; | |
769e19dc J |
289 | |
290 | if (!$entry) { | |
291 | $this->markTestSkipped('No content found in db.'); | |
292 | } | |
293 | ||
294 | // hydrate the tags relations | |
295 | $nbTags = count($entry->getTags()); | |
296 | $tag = $entry->getTags()[0]; | |
297 | ||
fcb1fba5 | 298 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json'); |
769e19dc | 299 | |
fcb1fba5 | 300 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 301 | |
fcb1fba5 | 302 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
303 | |
304 | $this->assertArrayHasKey('tags', $content); | |
4346a860 | 305 | $this->assertEquals($nbTags - 1, count($content['tags'])); |
769e19dc J |
306 | } |
307 | ||
308 | public function testGetUserTags() | |
309 | { | |
fcb1fba5 | 310 | $this->client->request('GET', '/api/tags.json'); |
769e19dc | 311 | |
fcb1fba5 | 312 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 313 | |
fcb1fba5 | 314 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
315 | |
316 | $this->assertGreaterThan(0, $content); | |
317 | $this->assertArrayHasKey('id', $content[0]); | |
318 | $this->assertArrayHasKey('label', $content[0]); | |
319 | ||
320 | return end($content); | |
321 | } | |
322 | ||
323 | /** | |
324 | * @depends testGetUserTags | |
325 | */ | |
326 | public function testDeleteUserTag($tag) | |
327 | { | |
fcb1fba5 | 328 | $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json'); |
769e19dc | 329 | |
fcb1fba5 | 330 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 331 | |
fcb1fba5 | 332 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
333 | |
334 | $this->assertArrayHasKey('label', $content); | |
335 | $this->assertEquals($tag['label'], $content['label']); | |
fc732227 | 336 | $this->assertEquals($tag['slug'], $content['slug']); |
4059a061 JB |
337 | |
338 | $entries = $entry = $this->client->getContainer() | |
339 | ->get('doctrine.orm.entity_manager') | |
340 | ->getRepository('WallabagCoreBundle:Entry') | |
341 | ->findAllByTagId($this->user->getId(), $tag['id']); | |
342 | ||
343 | $this->assertCount(0, $entries); | |
769e19dc | 344 | } |
9761bfa1 | 345 | |
6f8310b4 TC |
346 | public function testGetVersion() |
347 | { | |
348 | $this->client->request('GET', '/api/version'); | |
9761bfa1 V |
349 | |
350 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
351 | ||
352 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
353 | ||
6f8310b4 | 354 | $this->assertEquals($this->client->getContainer()->getParameter('wallabag_core.version'), $content); |
9761bfa1 | 355 | } |
769e19dc | 356 | } |