]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ApiBundle/Tests/Controller/WallabagRestControllerTest.php
Convert array + phpDoc
[github/wallabag/wallabag.git] / src / Wallabag / ApiBundle / Tests / Controller / WallabagRestControllerTest.php
CommitLineData
769e19dc
J
1<?php
2
9744e971 3namespace Wallabag\ApiBundle\Tests\Controller;
769e19dc 4
8a493541 5use Wallabag\ApiBundle\Tests\WallabagApiTestCase;
769e19dc 6
8a493541 7class 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')
4094ea47 16 ->findOneBy(['user' => 1, 'isArchived' => false]);
769e19dc
J
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')
4094ea47 47 ->findOneBy(['user' => 2, 'isArchived' => false]);
769e19dc
J
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 {
4094ea47 82 $this->client->request('GET', '/api/entries', ['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 {
4094ea47 104 $this->client->request('GET', '/api/entries', ['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 {
4094ea47 152 $this->client->request('POST', '/api/entries.json', [
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',
4094ea47 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
3107f92a
TC
169 public function testPostSameEntry()
170 {
4094ea47 171 $this->client->request('POST', '/api/entries.json', [
3107f92a
TC
172 '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',
173 'archive' => '1',
2baca964 174 'tags' => 'google, apple',
4094ea47 175 ]);
3107f92a
TC
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/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', $content['url']);
183 $this->assertEquals(true, $content['is_archived']);
184 $this->assertEquals(false, $content['is_starred']);
2baca964 185 $this->assertCount(2, $content['tags']);
3107f92a
TC
186 }
187
09d8bb6f 188 public function testPostArchivedAndStarredEntry()
816ad405 189 {
4094ea47 190 $this->client->request('POST', '/api/entries.json', [
816ad405 191 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
189ef634
TC
192 'archive' => '1',
193 'starred' => '1',
4094ea47 194 ]);
816ad405
TC
195
196 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
197
198 $content = json_decode($this->client->getResponse()->getContent(), true);
199
200 $this->assertGreaterThan(0, $content['id']);
201 $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']);
202 $this->assertEquals(true, $content['is_archived']);
09d8bb6f 203 $this->assertEquals(true, $content['is_starred']);
bc44aa57 204 $this->assertEquals(1, $content['user_id']);
816ad405
TC
205 }
206
2f60e5ea
TC
207 public function testPostArchivedAndStarredEntryWithoutQuotes()
208 {
4094ea47 209 $this->client->request('POST', '/api/entries.json', [
2f60e5ea
TC
210 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
211 'archive' => 0,
212 'starred' => 1,
4094ea47 213 ]);
2f60e5ea
TC
214
215 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
216
217 $content = json_decode($this->client->getResponse()->getContent(), true);
218
219 $this->assertGreaterThan(0, $content['id']);
220 $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']);
221 $this->assertEquals(false, $content['is_archived']);
222 $this->assertEquals(true, $content['is_starred']);
223 }
224
769e19dc
J
225 public function testPatchEntry()
226 {
fcb1fba5 227 $entry = $this->client->getContainer()
769e19dc
J
228 ->get('doctrine.orm.entity_manager')
229 ->getRepository('WallabagCoreBundle:Entry')
230 ->findOneByUser(1);
231
232 if (!$entry) {
233 $this->markTestSkipped('No content found in db.');
234 }
235
236 // hydrate the tags relations
237 $nbTags = count($entry->getTags());
238
4094ea47 239 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
769e19dc
J
240 'title' => 'New awesome title',
241 'tags' => 'new tag '.uniqid(),
189ef634
TC
242 'starred' => '1',
243 'archive' => '0',
4094ea47 244 ]);
769e19dc 245
fcb1fba5 246 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 247
fcb1fba5 248 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
249
250 $this->assertEquals($entry->getId(), $content['id']);
251 $this->assertEquals($entry->getUrl(), $content['url']);
252 $this->assertEquals('New awesome title', $content['title']);
253 $this->assertGreaterThan($nbTags, count($content['tags']));
bc44aa57 254 $this->assertEquals(1, $content['user_id']);
769e19dc
J
255 }
256
2f60e5ea
TC
257 public function testPatchEntryWithoutQuotes()
258 {
259 $entry = $this->client->getContainer()
260 ->get('doctrine.orm.entity_manager')
261 ->getRepository('WallabagCoreBundle:Entry')
262 ->findOneByUser(1);
263
264 if (!$entry) {
265 $this->markTestSkipped('No content found in db.');
266 }
267
268 // hydrate the tags relations
269 $nbTags = count($entry->getTags());
270
4094ea47 271 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
2f60e5ea
TC
272 'title' => 'New awesome title',
273 'tags' => 'new tag '.uniqid(),
274 'starred' => 1,
275 'archive' => 0,
4094ea47 276 ]);
2f60e5ea
TC
277
278 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
279
280 $content = json_decode($this->client->getResponse()->getContent(), true);
281
282 $this->assertEquals($entry->getId(), $content['id']);
283 $this->assertEquals($entry->getUrl(), $content['url']);
284 $this->assertEquals('New awesome title', $content['title']);
285 $this->assertGreaterThan($nbTags, count($content['tags']));
286 }
287
769e19dc
J
288 public function testGetTagsEntry()
289 {
fcb1fba5 290 $entry = $this->client->getContainer()
769e19dc
J
291 ->get('doctrine.orm.entity_manager')
292 ->getRepository('WallabagCoreBundle:Entry')
293 ->findOneWithTags(1);
294
295 $entry = $entry[0];
296
297 if (!$entry) {
298 $this->markTestSkipped('No content found in db.');
299 }
300
4094ea47 301 $tags = [];
769e19dc 302 foreach ($entry->getTags() as $tag) {
4094ea47 303 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
769e19dc
J
304 }
305
fcb1fba5 306 $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags');
769e19dc 307
fcb1fba5 308 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent());
769e19dc
J
309 }
310
311 public function testPostTagsOnEntry()
312 {
fcb1fba5 313 $entry = $this->client->getContainer()
769e19dc
J
314 ->get('doctrine.orm.entity_manager')
315 ->getRepository('WallabagCoreBundle:Entry')
316 ->findOneByUser(1);
317
318 if (!$entry) {
319 $this->markTestSkipped('No content found in db.');
320 }
321
322 $nbTags = count($entry->getTags());
323
324 $newTags = 'tag1,tag2,tag3';
325
4094ea47 326 $this->client->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]);
769e19dc 327
fcb1fba5 328 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 329
fcb1fba5 330 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
331
332 $this->assertArrayHasKey('tags', $content);
4346a860 333 $this->assertEquals($nbTags + 3, count($content['tags']));
769e19dc 334
fcb1fba5 335 $entryDB = $this->client->getContainer()
769e19dc
J
336 ->get('doctrine.orm.entity_manager')
337 ->getRepository('WallabagCoreBundle:Entry')
338 ->find($entry->getId());
339
4094ea47 340 $tagsInDB = [];
769e19dc
J
341 foreach ($entryDB->getTags()->toArray() as $tag) {
342 $tagsInDB[$tag->getId()] = $tag->getLabel();
343 }
344
345 foreach (explode(',', $newTags) as $tag) {
346 $this->assertContains($tag, $tagsInDB);
347 }
348 }
349
fcb1fba5 350 public function testDeleteOneTagEntry()
769e19dc 351 {
fcb1fba5 352 $entry = $this->client->getContainer()
769e19dc
J
353 ->get('doctrine.orm.entity_manager')
354 ->getRepository('WallabagCoreBundle:Entry')
fcb1fba5
NL
355 ->findOneWithTags(1);
356 $entry = $entry[0];
769e19dc
J
357
358 if (!$entry) {
359 $this->markTestSkipped('No content found in db.');
360 }
361
362 // hydrate the tags relations
363 $nbTags = count($entry->getTags());
364 $tag = $entry->getTags()[0];
365
fcb1fba5 366 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json');
769e19dc 367
fcb1fba5 368 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 369
fcb1fba5 370 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
371
372 $this->assertArrayHasKey('tags', $content);
4346a860 373 $this->assertEquals($nbTags - 1, count($content['tags']));
769e19dc
J
374 }
375
376 public function testGetUserTags()
377 {
fcb1fba5 378 $this->client->request('GET', '/api/tags.json');
769e19dc 379
fcb1fba5 380 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 381
fcb1fba5 382 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
383
384 $this->assertGreaterThan(0, $content);
385 $this->assertArrayHasKey('id', $content[0]);
386 $this->assertArrayHasKey('label', $content[0]);
387
388 return end($content);
389 }
390
391 /**
392 * @depends testGetUserTags
393 */
394 public function testDeleteUserTag($tag)
395 {
fcb1fba5 396 $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json');
769e19dc 397
fcb1fba5 398 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 399
fcb1fba5 400 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
401
402 $this->assertArrayHasKey('label', $content);
403 $this->assertEquals($tag['label'], $content['label']);
fc732227 404 $this->assertEquals($tag['slug'], $content['slug']);
4059a061
JB
405
406 $entries = $entry = $this->client->getContainer()
407 ->get('doctrine.orm.entity_manager')
408 ->getRepository('WallabagCoreBundle:Entry')
409 ->findAllByTagId($this->user->getId(), $tag['id']);
410
411 $this->assertCount(0, $entries);
769e19dc 412 }
9761bfa1 413
6f8310b4
TC
414 public function testGetVersion()
415 {
416 $this->client->request('GET', '/api/version');
9761bfa1
V
417
418 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
419
420 $content = json_decode($this->client->getResponse()->getContent(), true);
421
6f8310b4 422 $this->assertEquals($this->client->getContainer()->getParameter('wallabag_core.version'), $content);
9761bfa1 423 }
769e19dc 424}