]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php
Add since parameter
[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
e5fb89e5
TC
124 public function testGetDatedEntries()
125 {
126 $this->client->request('GET', '/api/entries', ['since' => 1]);
127
128 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
129
130 $content = json_decode($this->client->getResponse()->getContent(), true);
131
132 $this->assertGreaterThanOrEqual(1, count($content));
133 $this->assertNotEmpty($content['_embedded']['items']);
134 $this->assertGreaterThanOrEqual(1, $content['total']);
135 $this->assertEquals(1, $content['page']);
136 $this->assertGreaterThanOrEqual(1, $content['pages']);
137
138 $this->assertTrue(
139 $this->client->getResponse()->headers->contains(
140 'Content-Type',
141 'application/json'
142 )
143 );
144 }
145
146 public function testGetDatedSupEntries()
147 {
148 $future = new \DateTime(date('Y-m-d H:i:s'));
149 $this->client->request('GET', '/api/entries', ['since' => $future->getTimestamp() + 1000]);
150
151 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
152
153 $content = json_decode($this->client->getResponse()->getContent(), true);
154
155 $this->assertGreaterThanOrEqual(1, count($content));
156 $this->assertEmpty($content['_embedded']['items']);
157 $this->assertEquals(0, $content['total']);
158 $this->assertEquals(1, $content['page']);
159 $this->assertEquals(1, $content['pages']);
160
161 $this->assertTrue(
162 $this->client->getResponse()->headers->contains(
163 'Content-Type',
164 'application/json'
165 )
166 );
167 }
168
769e19dc
J
169 public function testDeleteEntry()
170 {
fcb1fba5 171 $entry = $this->client->getContainer()
769e19dc
J
172 ->get('doctrine.orm.entity_manager')
173 ->getRepository('WallabagCoreBundle:Entry')
174 ->findOneByUser(1);
175
176 if (!$entry) {
177 $this->markTestSkipped('No content found in db.');
178 }
179
fcb1fba5 180 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json');
769e19dc 181
fcb1fba5 182 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 183
fcb1fba5 184 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
185
186 $this->assertEquals($entry->getTitle(), $content['title']);
187 $this->assertEquals($entry->getUrl(), $content['url']);
188
189 // We'll try to delete this entry again
fcb1fba5 190 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json');
769e19dc 191
fcb1fba5 192 $this->assertEquals(404, $this->client->getResponse()->getStatusCode());
769e19dc
J
193 }
194
195 public function testPostEntry()
196 {
4094ea47 197 $this->client->request('POST', '/api/entries.json', [
769e19dc
J
198 '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',
199 'tags' => 'google',
51a15609 200 'title' => 'New title for my article',
4094ea47 201 ]);
769e19dc 202
fcb1fba5 203 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 204
fcb1fba5 205 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
206
207 $this->assertGreaterThan(0, $content['id']);
208 $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']);
209 $this->assertEquals(false, $content['is_archived']);
210 $this->assertEquals(false, $content['is_starred']);
51a15609 211 $this->assertEquals('New title for my article', $content['title']);
bc44aa57 212 $this->assertEquals(1, $content['user_id']);
769e19dc
J
213 $this->assertCount(1, $content['tags']);
214 }
215
3107f92a
TC
216 public function testPostSameEntry()
217 {
4094ea47 218 $this->client->request('POST', '/api/entries.json', [
3107f92a
TC
219 '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',
220 'archive' => '1',
2baca964 221 'tags' => 'google, apple',
4094ea47 222 ]);
3107f92a
TC
223
224 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
225
226 $content = json_decode($this->client->getResponse()->getContent(), true);
227
228 $this->assertGreaterThan(0, $content['id']);
229 $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']);
230 $this->assertEquals(true, $content['is_archived']);
231 $this->assertEquals(false, $content['is_starred']);
2baca964 232 $this->assertCount(2, $content['tags']);
3107f92a
TC
233 }
234
09d8bb6f 235 public function testPostArchivedAndStarredEntry()
816ad405 236 {
4094ea47 237 $this->client->request('POST', '/api/entries.json', [
816ad405 238 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
189ef634
TC
239 'archive' => '1',
240 'starred' => '1',
4094ea47 241 ]);
816ad405
TC
242
243 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
244
245 $content = json_decode($this->client->getResponse()->getContent(), true);
246
247 $this->assertGreaterThan(0, $content['id']);
248 $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']);
249 $this->assertEquals(true, $content['is_archived']);
09d8bb6f 250 $this->assertEquals(true, $content['is_starred']);
bc44aa57 251 $this->assertEquals(1, $content['user_id']);
816ad405
TC
252 }
253
2f60e5ea
TC
254 public function testPostArchivedAndStarredEntryWithoutQuotes()
255 {
4094ea47 256 $this->client->request('POST', '/api/entries.json', [
2f60e5ea
TC
257 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
258 'archive' => 0,
259 'starred' => 1,
4094ea47 260 ]);
2f60e5ea
TC
261
262 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
263
264 $content = json_decode($this->client->getResponse()->getContent(), true);
265
266 $this->assertGreaterThan(0, $content['id']);
267 $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']);
268 $this->assertEquals(false, $content['is_archived']);
269 $this->assertEquals(true, $content['is_starred']);
270 }
271
769e19dc
J
272 public function testPatchEntry()
273 {
fcb1fba5 274 $entry = $this->client->getContainer()
769e19dc
J
275 ->get('doctrine.orm.entity_manager')
276 ->getRepository('WallabagCoreBundle:Entry')
277 ->findOneByUser(1);
278
279 if (!$entry) {
280 $this->markTestSkipped('No content found in db.');
281 }
282
283 // hydrate the tags relations
284 $nbTags = count($entry->getTags());
285
4094ea47 286 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
769e19dc
J
287 'title' => 'New awesome title',
288 'tags' => 'new tag '.uniqid(),
189ef634
TC
289 'starred' => '1',
290 'archive' => '0',
4094ea47 291 ]);
769e19dc 292
fcb1fba5 293 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 294
fcb1fba5 295 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
296
297 $this->assertEquals($entry->getId(), $content['id']);
298 $this->assertEquals($entry->getUrl(), $content['url']);
299 $this->assertEquals('New awesome title', $content['title']);
300 $this->assertGreaterThan($nbTags, count($content['tags']));
bc44aa57 301 $this->assertEquals(1, $content['user_id']);
769e19dc
J
302 }
303
2f60e5ea
TC
304 public function testPatchEntryWithoutQuotes()
305 {
306 $entry = $this->client->getContainer()
307 ->get('doctrine.orm.entity_manager')
308 ->getRepository('WallabagCoreBundle:Entry')
309 ->findOneByUser(1);
310
311 if (!$entry) {
312 $this->markTestSkipped('No content found in db.');
313 }
314
315 // hydrate the tags relations
316 $nbTags = count($entry->getTags());
317
4094ea47 318 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
2f60e5ea
TC
319 'title' => 'New awesome title',
320 'tags' => 'new tag '.uniqid(),
321 'starred' => 1,
322 'archive' => 0,
4094ea47 323 ]);
2f60e5ea
TC
324
325 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
326
327 $content = json_decode($this->client->getResponse()->getContent(), true);
328
329 $this->assertEquals($entry->getId(), $content['id']);
330 $this->assertEquals($entry->getUrl(), $content['url']);
331 $this->assertEquals('New awesome title', $content['title']);
332 $this->assertGreaterThan($nbTags, count($content['tags']));
333 }
334
769e19dc
J
335 public function testGetTagsEntry()
336 {
fcb1fba5 337 $entry = $this->client->getContainer()
769e19dc
J
338 ->get('doctrine.orm.entity_manager')
339 ->getRepository('WallabagCoreBundle:Entry')
340 ->findOneWithTags(1);
341
342 $entry = $entry[0];
343
344 if (!$entry) {
345 $this->markTestSkipped('No content found in db.');
346 }
347
4094ea47 348 $tags = [];
769e19dc 349 foreach ($entry->getTags() as $tag) {
4094ea47 350 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
769e19dc
J
351 }
352
fcb1fba5 353 $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags');
769e19dc 354
fcb1fba5 355 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent());
769e19dc
J
356 }
357
358 public function testPostTagsOnEntry()
359 {
fcb1fba5 360 $entry = $this->client->getContainer()
769e19dc
J
361 ->get('doctrine.orm.entity_manager')
362 ->getRepository('WallabagCoreBundle:Entry')
363 ->findOneByUser(1);
364
365 if (!$entry) {
366 $this->markTestSkipped('No content found in db.');
367 }
368
369 $nbTags = count($entry->getTags());
370
371 $newTags = 'tag1,tag2,tag3';
372
4094ea47 373 $this->client->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]);
769e19dc 374
fcb1fba5 375 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 376
fcb1fba5 377 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
378
379 $this->assertArrayHasKey('tags', $content);
4346a860 380 $this->assertEquals($nbTags + 3, count($content['tags']));
769e19dc 381
fcb1fba5 382 $entryDB = $this->client->getContainer()
769e19dc
J
383 ->get('doctrine.orm.entity_manager')
384 ->getRepository('WallabagCoreBundle:Entry')
385 ->find($entry->getId());
386
4094ea47 387 $tagsInDB = [];
769e19dc
J
388 foreach ($entryDB->getTags()->toArray() as $tag) {
389 $tagsInDB[$tag->getId()] = $tag->getLabel();
390 }
391
392 foreach (explode(',', $newTags) as $tag) {
393 $this->assertContains($tag, $tagsInDB);
394 }
395 }
396
fcb1fba5 397 public function testDeleteOneTagEntry()
769e19dc 398 {
fcb1fba5 399 $entry = $this->client->getContainer()
769e19dc
J
400 ->get('doctrine.orm.entity_manager')
401 ->getRepository('WallabagCoreBundle:Entry')
fcb1fba5
NL
402 ->findOneWithTags(1);
403 $entry = $entry[0];
769e19dc
J
404
405 if (!$entry) {
406 $this->markTestSkipped('No content found in db.');
407 }
408
409 // hydrate the tags relations
410 $nbTags = count($entry->getTags());
411 $tag = $entry->getTags()[0];
412
fcb1fba5 413 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json');
769e19dc 414
fcb1fba5 415 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 416
fcb1fba5 417 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
418
419 $this->assertArrayHasKey('tags', $content);
4346a860 420 $this->assertEquals($nbTags - 1, count($content['tags']));
769e19dc
J
421 }
422
423 public function testGetUserTags()
424 {
fcb1fba5 425 $this->client->request('GET', '/api/tags.json');
769e19dc 426
fcb1fba5 427 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 428
fcb1fba5 429 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
430
431 $this->assertGreaterThan(0, $content);
432 $this->assertArrayHasKey('id', $content[0]);
433 $this->assertArrayHasKey('label', $content[0]);
434
435 return end($content);
436 }
437
438 /**
439 * @depends testGetUserTags
440 */
441 public function testDeleteUserTag($tag)
442 {
fcb1fba5 443 $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json');
769e19dc 444
fcb1fba5 445 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 446
fcb1fba5 447 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
448
449 $this->assertArrayHasKey('label', $content);
450 $this->assertEquals($tag['label'], $content['label']);
fc732227 451 $this->assertEquals($tag['slug'], $content['slug']);
4059a061
JB
452
453 $entries = $entry = $this->client->getContainer()
454 ->get('doctrine.orm.entity_manager')
455 ->getRepository('WallabagCoreBundle:Entry')
456 ->findAllByTagId($this->user->getId(), $tag['id']);
457
458 $this->assertCount(0, $entries);
769e19dc 459 }
9761bfa1 460
6f8310b4
TC
461 public function testGetVersion()
462 {
463 $this->client->request('GET', '/api/version');
9761bfa1
V
464
465 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
466
467 $content = json_decode($this->client->getResponse()->getContent(), true);
468
6f8310b4 469 $this->assertEquals($this->client->getContainer()->getParameter('wallabag_core.version'), $content);
9761bfa1 470 }
bba271e6
YE
471
472 public function testSaveIsArchivedAfterPost()
473 {
474 $entry = $this->client->getContainer()
475 ->get('doctrine.orm.entity_manager')
476 ->getRepository('WallabagCoreBundle:Entry')
477 ->findOneBy(['user' => 1, 'isArchived' => true]);
478
479 if (!$entry) {
480 $this->markTestSkipped('No content found in db.');
481 }
482
483 $this->client->request('POST', '/api/entries.json', [
484 'url' => $entry->getUrl(),
485 ]);
486
487 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
488
489 $content = json_decode($this->client->getResponse()->getContent(), true);
490
69221684 491 $this->assertEquals(true, $content['is_archived']);
bba271e6
YE
492 }
493
494 public function testSaveIsStarredAfterPost()
495 {
496 $entry = $this->client->getContainer()
497 ->get('doctrine.orm.entity_manager')
498 ->getRepository('WallabagCoreBundle:Entry')
499 ->findOneBy(['user' => 1, 'isStarred' => true]);
500
501 if (!$entry) {
502 $this->markTestSkipped('No content found in db.');
503 }
504
505 $this->client->request('POST', '/api/entries.json', [
506 'url' => $entry->getUrl(),
507 ]);
508
509 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
510
511 $content = json_decode($this->client->getResponse()->getContent(), true);
512
69221684 513 $this->assertEquals(true, $content['is_starred']);
bba271e6
YE
514 }
515
516 public function testSaveIsArchivedAfterPatch()
517 {
518 $entry = $this->client->getContainer()
519 ->get('doctrine.orm.entity_manager')
520 ->getRepository('WallabagCoreBundle:Entry')
521 ->findOneBy(['user' => 1, 'isArchived' => true]);
522
523 if (!$entry) {
524 $this->markTestSkipped('No content found in db.');
525 }
526
527 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
528 'title' => $entry->getTitle().'++',
529 ]);
530
531 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
532
533 $content = json_decode($this->client->getResponse()->getContent(), true);
534
535 $this->assertEquals(true, $content['is_archived']);
536 }
537
538 public function testSaveIsStarredAfterPatch()
539 {
540 $entry = $this->client->getContainer()
541 ->get('doctrine.orm.entity_manager')
542 ->getRepository('WallabagCoreBundle:Entry')
543 ->findOneBy(['user' => 1, 'isStarred' => true]);
544
545 if (!$entry) {
546 $this->markTestSkipped('No content found in db.');
547 }
548 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
549 'title' => $entry->getTitle().'++',
550 ]);
551
552 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
553
554 $content = json_decode($this->client->getResponse()->getContent(), true);
555
69221684 556 $this->assertEquals(true, $content['is_starred']);
bba271e6 557 }
769e19dc 558}