]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php
Add since parameter
[github/wallabag/wallabag.git] / tests / Wallabag / ApiBundle / Controller / WallabagRestControllerTest.php
1 <?php
2
3 namespace Tests\Wallabag\ApiBundle\Controller;
4
5 use Tests\Wallabag\ApiBundle\WallabagApiTestCase;
6
7 class WallabagRestControllerTest extends WallabagApiTestCase
8 {
9 protected static $salt;
10
11 public function testGetOneEntry()
12 {
13 $entry = $this->client->getContainer()
14 ->get('doctrine.orm.entity_manager')
15 ->getRepository('WallabagCoreBundle:Entry')
16 ->findOneBy(['user' => 1, 'isArchived' => false]);
17
18 if (!$entry) {
19 $this->markTestSkipped('No content found in db.');
20 }
21
22 $this->client->request('GET', '/api/entries/'.$entry->getId().'.json');
23 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
24
25 $content = json_decode($this->client->getResponse()->getContent(), true);
26
27 $this->assertEquals($entry->getTitle(), $content['title']);
28 $this->assertEquals($entry->getUrl(), $content['url']);
29 $this->assertCount(count($entry->getTags()), $content['tags']);
30 $this->assertEquals($entry->getUserName(), $content['user_name']);
31 $this->assertEquals($entry->getUserEmail(), $content['user_email']);
32 $this->assertEquals($entry->getUserId(), $content['user_id']);
33
34 $this->assertTrue(
35 $this->client->getResponse()->headers->contains(
36 'Content-Type',
37 'application/json'
38 )
39 );
40 }
41
42 public function testGetOneEntryWrongUser()
43 {
44 $entry = $this->client->getContainer()
45 ->get('doctrine.orm.entity_manager')
46 ->getRepository('WallabagCoreBundle:Entry')
47 ->findOneBy(['user' => 2, 'isArchived' => false]);
48
49 if (!$entry) {
50 $this->markTestSkipped('No content found in db.');
51 }
52
53 $this->client->request('GET', '/api/entries/'.$entry->getId().'.json');
54
55 $this->assertEquals(403, $this->client->getResponse()->getStatusCode());
56 }
57
58 public function testGetEntries()
59 {
60 $this->client->request('GET', '/api/entries');
61
62 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
63
64 $content = json_decode($this->client->getResponse()->getContent(), true);
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(
73 $this->client->getResponse()->headers->contains(
74 'Content-Type',
75 'application/json'
76 )
77 );
78 }
79
80 public function testGetStarredEntries()
81 {
82 $this->client->request('GET', '/api/entries', ['star' => 1, 'sort' => 'updated']);
83
84 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
85
86 $content = json_decode($this->client->getResponse()->getContent(), true);
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(
95 $this->client->getResponse()->headers->contains(
96 'Content-Type',
97 'application/json'
98 )
99 );
100 }
101
102 public function testGetArchiveEntries()
103 {
104 $this->client->request('GET', '/api/entries', ['archive' => 1]);
105
106 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
107
108 $content = json_decode($this->client->getResponse()->getContent(), true);
109
110 $this->assertGreaterThanOrEqual(1, count($content));
111 $this->assertNotEmpty($content['_embedded']['items']);
112 $this->assertGreaterThanOrEqual(1, $content['total']);
113 $this->assertEquals(1, $content['page']);
114 $this->assertGreaterThanOrEqual(1, $content['pages']);
115
116 $this->assertTrue(
117 $this->client->getResponse()->headers->contains(
118 'Content-Type',
119 'application/json'
120 )
121 );
122 }
123
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
169 public function testDeleteEntry()
170 {
171 $entry = $this->client->getContainer()
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
180 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json');
181
182 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
183
184 $content = json_decode($this->client->getResponse()->getContent(), true);
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
190 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json');
191
192 $this->assertEquals(404, $this->client->getResponse()->getStatusCode());
193 }
194
195 public function testPostEntry()
196 {
197 $this->client->request('POST', '/api/entries.json', [
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',
200 'title' => 'New title for my article',
201 ]);
202
203 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
204
205 $content = json_decode($this->client->getResponse()->getContent(), true);
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']);
211 $this->assertEquals('New title for my article', $content['title']);
212 $this->assertEquals(1, $content['user_id']);
213 $this->assertCount(1, $content['tags']);
214 }
215
216 public function testPostSameEntry()
217 {
218 $this->client->request('POST', '/api/entries.json', [
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',
221 'tags' => 'google, apple',
222 ]);
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']);
232 $this->assertCount(2, $content['tags']);
233 }
234
235 public function testPostArchivedAndStarredEntry()
236 {
237 $this->client->request('POST', '/api/entries.json', [
238 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
239 'archive' => '1',
240 'starred' => '1',
241 ]);
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']);
250 $this->assertEquals(true, $content['is_starred']);
251 $this->assertEquals(1, $content['user_id']);
252 }
253
254 public function testPostArchivedAndStarredEntryWithoutQuotes()
255 {
256 $this->client->request('POST', '/api/entries.json', [
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,
260 ]);
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
272 public function testPatchEntry()
273 {
274 $entry = $this->client->getContainer()
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
286 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
287 'title' => 'New awesome title',
288 'tags' => 'new tag '.uniqid(),
289 'starred' => '1',
290 'archive' => '0',
291 ]);
292
293 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
294
295 $content = json_decode($this->client->getResponse()->getContent(), true);
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']));
301 $this->assertEquals(1, $content['user_id']);
302 }
303
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
318 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
319 'title' => 'New awesome title',
320 'tags' => 'new tag '.uniqid(),
321 'starred' => 1,
322 'archive' => 0,
323 ]);
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
335 public function testGetTagsEntry()
336 {
337 $entry = $this->client->getContainer()
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
348 $tags = [];
349 foreach ($entry->getTags() as $tag) {
350 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
351 }
352
353 $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags');
354
355 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent());
356 }
357
358 public function testPostTagsOnEntry()
359 {
360 $entry = $this->client->getContainer()
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
373 $this->client->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]);
374
375 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
376
377 $content = json_decode($this->client->getResponse()->getContent(), true);
378
379 $this->assertArrayHasKey('tags', $content);
380 $this->assertEquals($nbTags + 3, count($content['tags']));
381
382 $entryDB = $this->client->getContainer()
383 ->get('doctrine.orm.entity_manager')
384 ->getRepository('WallabagCoreBundle:Entry')
385 ->find($entry->getId());
386
387 $tagsInDB = [];
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
397 public function testDeleteOneTagEntry()
398 {
399 $entry = $this->client->getContainer()
400 ->get('doctrine.orm.entity_manager')
401 ->getRepository('WallabagCoreBundle:Entry')
402 ->findOneWithTags(1);
403 $entry = $entry[0];
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
413 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json');
414
415 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
416
417 $content = json_decode($this->client->getResponse()->getContent(), true);
418
419 $this->assertArrayHasKey('tags', $content);
420 $this->assertEquals($nbTags - 1, count($content['tags']));
421 }
422
423 public function testGetUserTags()
424 {
425 $this->client->request('GET', '/api/tags.json');
426
427 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
428
429 $content = json_decode($this->client->getResponse()->getContent(), true);
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 {
443 $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json');
444
445 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
446
447 $content = json_decode($this->client->getResponse()->getContent(), true);
448
449 $this->assertArrayHasKey('label', $content);
450 $this->assertEquals($tag['label'], $content['label']);
451 $this->assertEquals($tag['slug'], $content['slug']);
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);
459 }
460
461 public function testGetVersion()
462 {
463 $this->client->request('GET', '/api/version');
464
465 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
466
467 $content = json_decode($this->client->getResponse()->getContent(), true);
468
469 $this->assertEquals($this->client->getContainer()->getParameter('wallabag_core.version'), $content);
470 }
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
491 $this->assertEquals(true, $content['is_archived']);
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
513 $this->assertEquals(true, $content['is_starred']);
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
556 $this->assertEquals(true, $content['is_starred']);
557 }
558 }