]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php
Jump to Symfony 3.1
[github/wallabag/wallabag.git] / tests / Wallabag / ApiBundle / Controller / WallabagRestControllerTest.php
CommitLineData
769e19dc
J
1<?php
2
23634d5d 3namespace Tests\Wallabag\ApiBundle\Controller;
769e19dc 4
23634d5d 5use Tests\Wallabag\ApiBundle\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',
51a15609 155 'title' => 'New title for my article',
4094ea47 156 ]);
769e19dc 157
fcb1fba5 158 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 159
fcb1fba5 160 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
161
162 $this->assertGreaterThan(0, $content['id']);
163 $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']);
164 $this->assertEquals(false, $content['is_archived']);
165 $this->assertEquals(false, $content['is_starred']);
51a15609 166 $this->assertEquals('New title for my article', $content['title']);
bc44aa57 167 $this->assertEquals(1, $content['user_id']);
769e19dc
J
168 $this->assertCount(1, $content['tags']);
169 }
170
3107f92a
TC
171 public function testPostSameEntry()
172 {
4094ea47 173 $this->client->request('POST', '/api/entries.json', [
3107f92a
TC
174 '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',
175 'archive' => '1',
2baca964 176 'tags' => 'google, apple',
4094ea47 177 ]);
3107f92a
TC
178
179 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
180
181 $content = json_decode($this->client->getResponse()->getContent(), true);
182
183 $this->assertGreaterThan(0, $content['id']);
184 $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']);
185 $this->assertEquals(true, $content['is_archived']);
186 $this->assertEquals(false, $content['is_starred']);
2baca964 187 $this->assertCount(2, $content['tags']);
3107f92a
TC
188 }
189
09d8bb6f 190 public function testPostArchivedAndStarredEntry()
816ad405 191 {
4094ea47 192 $this->client->request('POST', '/api/entries.json', [
816ad405 193 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
189ef634
TC
194 'archive' => '1',
195 'starred' => '1',
4094ea47 196 ]);
816ad405
TC
197
198 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
199
200 $content = json_decode($this->client->getResponse()->getContent(), true);
201
202 $this->assertGreaterThan(0, $content['id']);
203 $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']);
204 $this->assertEquals(true, $content['is_archived']);
09d8bb6f 205 $this->assertEquals(true, $content['is_starred']);
bc44aa57 206 $this->assertEquals(1, $content['user_id']);
816ad405
TC
207 }
208
2f60e5ea
TC
209 public function testPostArchivedAndStarredEntryWithoutQuotes()
210 {
4094ea47 211 $this->client->request('POST', '/api/entries.json', [
2f60e5ea
TC
212 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
213 'archive' => 0,
214 'starred' => 1,
4094ea47 215 ]);
2f60e5ea
TC
216
217 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
218
219 $content = json_decode($this->client->getResponse()->getContent(), true);
220
221 $this->assertGreaterThan(0, $content['id']);
222 $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']);
223 $this->assertEquals(false, $content['is_archived']);
224 $this->assertEquals(true, $content['is_starred']);
225 }
226
769e19dc
J
227 public function testPatchEntry()
228 {
fcb1fba5 229 $entry = $this->client->getContainer()
769e19dc
J
230 ->get('doctrine.orm.entity_manager')
231 ->getRepository('WallabagCoreBundle:Entry')
232 ->findOneByUser(1);
233
234 if (!$entry) {
235 $this->markTestSkipped('No content found in db.');
236 }
237
238 // hydrate the tags relations
239 $nbTags = count($entry->getTags());
240
4094ea47 241 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
769e19dc
J
242 'title' => 'New awesome title',
243 'tags' => 'new tag '.uniqid(),
189ef634
TC
244 'starred' => '1',
245 'archive' => '0',
4094ea47 246 ]);
769e19dc 247
fcb1fba5 248 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 249
fcb1fba5 250 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
251
252 $this->assertEquals($entry->getId(), $content['id']);
253 $this->assertEquals($entry->getUrl(), $content['url']);
254 $this->assertEquals('New awesome title', $content['title']);
255 $this->assertGreaterThan($nbTags, count($content['tags']));
bc44aa57 256 $this->assertEquals(1, $content['user_id']);
769e19dc
J
257 }
258
2f60e5ea
TC
259 public function testPatchEntryWithoutQuotes()
260 {
261 $entry = $this->client->getContainer()
262 ->get('doctrine.orm.entity_manager')
263 ->getRepository('WallabagCoreBundle:Entry')
264 ->findOneByUser(1);
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
4094ea47 273 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
2f60e5ea
TC
274 'title' => 'New awesome title',
275 'tags' => 'new tag '.uniqid(),
276 'starred' => 1,
277 'archive' => 0,
4094ea47 278 ]);
2f60e5ea
TC
279
280 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
281
282 $content = json_decode($this->client->getResponse()->getContent(), true);
283
284 $this->assertEquals($entry->getId(), $content['id']);
285 $this->assertEquals($entry->getUrl(), $content['url']);
286 $this->assertEquals('New awesome title', $content['title']);
287 $this->assertGreaterThan($nbTags, count($content['tags']));
288 }
289
769e19dc
J
290 public function testGetTagsEntry()
291 {
fcb1fba5 292 $entry = $this->client->getContainer()
769e19dc
J
293 ->get('doctrine.orm.entity_manager')
294 ->getRepository('WallabagCoreBundle:Entry')
295 ->findOneWithTags(1);
296
297 $entry = $entry[0];
298
299 if (!$entry) {
300 $this->markTestSkipped('No content found in db.');
301 }
302
4094ea47 303 $tags = [];
769e19dc 304 foreach ($entry->getTags() as $tag) {
4094ea47 305 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
769e19dc
J
306 }
307
fcb1fba5 308 $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags');
769e19dc 309
fcb1fba5 310 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent());
769e19dc
J
311 }
312
313 public function testPostTagsOnEntry()
314 {
fcb1fba5 315 $entry = $this->client->getContainer()
769e19dc
J
316 ->get('doctrine.orm.entity_manager')
317 ->getRepository('WallabagCoreBundle:Entry')
318 ->findOneByUser(1);
319
320 if (!$entry) {
321 $this->markTestSkipped('No content found in db.');
322 }
323
324 $nbTags = count($entry->getTags());
325
326 $newTags = 'tag1,tag2,tag3';
327
4094ea47 328 $this->client->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]);
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('tags', $content);
4346a860 335 $this->assertEquals($nbTags + 3, count($content['tags']));
769e19dc 336
fcb1fba5 337 $entryDB = $this->client->getContainer()
769e19dc
J
338 ->get('doctrine.orm.entity_manager')
339 ->getRepository('WallabagCoreBundle:Entry')
340 ->find($entry->getId());
341
4094ea47 342 $tagsInDB = [];
769e19dc
J
343 foreach ($entryDB->getTags()->toArray() as $tag) {
344 $tagsInDB[$tag->getId()] = $tag->getLabel();
345 }
346
347 foreach (explode(',', $newTags) as $tag) {
348 $this->assertContains($tag, $tagsInDB);
349 }
350 }
351
fcb1fba5 352 public function testDeleteOneTagEntry()
769e19dc 353 {
fcb1fba5 354 $entry = $this->client->getContainer()
769e19dc
J
355 ->get('doctrine.orm.entity_manager')
356 ->getRepository('WallabagCoreBundle:Entry')
fcb1fba5
NL
357 ->findOneWithTags(1);
358 $entry = $entry[0];
769e19dc
J
359
360 if (!$entry) {
361 $this->markTestSkipped('No content found in db.');
362 }
363
364 // hydrate the tags relations
365 $nbTags = count($entry->getTags());
366 $tag = $entry->getTags()[0];
367
fcb1fba5 368 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json');
769e19dc 369
fcb1fba5 370 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 371
fcb1fba5 372 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
373
374 $this->assertArrayHasKey('tags', $content);
4346a860 375 $this->assertEquals($nbTags - 1, count($content['tags']));
769e19dc
J
376 }
377
378 public function testGetUserTags()
379 {
fcb1fba5 380 $this->client->request('GET', '/api/tags.json');
769e19dc 381
fcb1fba5 382 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 383
fcb1fba5 384 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
385
386 $this->assertGreaterThan(0, $content);
387 $this->assertArrayHasKey('id', $content[0]);
388 $this->assertArrayHasKey('label', $content[0]);
389
390 return end($content);
391 }
392
393 /**
394 * @depends testGetUserTags
395 */
396 public function testDeleteUserTag($tag)
397 {
fcb1fba5 398 $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json');
769e19dc 399
fcb1fba5 400 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 401
fcb1fba5 402 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
403
404 $this->assertArrayHasKey('label', $content);
405 $this->assertEquals($tag['label'], $content['label']);
fc732227 406 $this->assertEquals($tag['slug'], $content['slug']);
4059a061
JB
407
408 $entries = $entry = $this->client->getContainer()
409 ->get('doctrine.orm.entity_manager')
410 ->getRepository('WallabagCoreBundle:Entry')
411 ->findAllByTagId($this->user->getId(), $tag['id']);
412
413 $this->assertCount(0, $entries);
769e19dc 414 }
9761bfa1 415
6f8310b4
TC
416 public function testGetVersion()
417 {
418 $this->client->request('GET', '/api/version');
9761bfa1
V
419
420 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
421
422 $content = json_decode($this->client->getResponse()->getContent(), true);
423
6f8310b4 424 $this->assertEquals($this->client->getContainer()->getParameter('wallabag_core.version'), $content);
9761bfa1 425 }
bba271e6
YE
426
427 public function testSaveIsArchivedAfterPost()
428 {
429 $entry = $this->client->getContainer()
430 ->get('doctrine.orm.entity_manager')
431 ->getRepository('WallabagCoreBundle:Entry')
432 ->findOneBy(['user' => 1, 'isArchived' => true]);
433
434 if (!$entry) {
435 $this->markTestSkipped('No content found in db.');
436 }
437
438 $this->client->request('POST', '/api/entries.json', [
439 'url' => $entry->getUrl(),
440 ]);
441
442 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
443
444 $content = json_decode($this->client->getResponse()->getContent(), true);
445
69221684 446 $this->assertEquals(true, $content['is_archived']);
bba271e6
YE
447 }
448
449 public function testSaveIsStarredAfterPost()
450 {
451 $entry = $this->client->getContainer()
452 ->get('doctrine.orm.entity_manager')
453 ->getRepository('WallabagCoreBundle:Entry')
454 ->findOneBy(['user' => 1, 'isStarred' => true]);
455
456 if (!$entry) {
457 $this->markTestSkipped('No content found in db.');
458 }
459
460 $this->client->request('POST', '/api/entries.json', [
461 'url' => $entry->getUrl(),
462 ]);
463
464 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
465
466 $content = json_decode($this->client->getResponse()->getContent(), true);
467
69221684 468 $this->assertEquals(true, $content['is_starred']);
bba271e6
YE
469 }
470
471 public function testSaveIsArchivedAfterPatch()
472 {
473 $entry = $this->client->getContainer()
474 ->get('doctrine.orm.entity_manager')
475 ->getRepository('WallabagCoreBundle:Entry')
476 ->findOneBy(['user' => 1, 'isArchived' => true]);
477
478 if (!$entry) {
479 $this->markTestSkipped('No content found in db.');
480 }
481
482 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
483 'title' => $entry->getTitle().'++',
484 ]);
485
486 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
487
488 $content = json_decode($this->client->getResponse()->getContent(), true);
489
490 $this->assertEquals(true, $content['is_archived']);
491 }
492
493 public function testSaveIsStarredAfterPatch()
494 {
495 $entry = $this->client->getContainer()
496 ->get('doctrine.orm.entity_manager')
497 ->getRepository('WallabagCoreBundle:Entry')
498 ->findOneBy(['user' => 1, 'isStarred' => true]);
499
500 if (!$entry) {
501 $this->markTestSkipped('No content found in db.');
502 }
503 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
504 'title' => $entry->getTitle().'++',
505 ]);
506
507 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
508
509 $content = json_decode($this->client->getResponse()->getContent(), true);
510
69221684 511 $this->assertEquals(true, $content['is_starred']);
bba271e6 512 }
769e19dc 513}