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