]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
Isolated tests
[github/wallabag/wallabag.git] / tests / Wallabag / ApiBundle / Controller / EntryRestControllerTest.php
1 <?php
2
3 namespace Tests\Wallabag\ApiBundle\Controller;
4
5 use Tests\Wallabag\ApiBundle\WallabagApiTestCase;
6 use Wallabag\CoreBundle\Entity\Entry;
7 use Wallabag\CoreBundle\Entity\Tag;
8 use Wallabag\CoreBundle\Helper\ContentProxy;
9 use Wallabag\UserBundle\Entity\User;
10
11 class EntryRestControllerTest extends WallabagApiTestCase
12 {
13 public function testGetOneEntry()
14 {
15 $entry = $this->client->getContainer()
16 ->get('doctrine.orm.entity_manager')
17 ->getRepository('WallabagCoreBundle:Entry')
18 ->findOneBy(['user' => 1, 'isArchived' => false]);
19
20 if (!$entry) {
21 $this->markTestSkipped('No content found in db.');
22 }
23
24 $this->client->request('GET', '/api/entries/'.$entry->getId().'.json');
25 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
26
27 $content = json_decode($this->client->getResponse()->getContent(), true);
28
29 $this->assertEquals($entry->getTitle(), $content['title']);
30 $this->assertEquals($entry->getUrl(), $content['url']);
31 $this->assertCount(count($entry->getTags()), $content['tags']);
32 $this->assertEquals($entry->getUserName(), $content['user_name']);
33 $this->assertEquals($entry->getUserEmail(), $content['user_email']);
34 $this->assertEquals($entry->getUserId(), $content['user_id']);
35
36 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
37 }
38
39 public function testExportEntry()
40 {
41 $entry = $this->client->getContainer()
42 ->get('doctrine.orm.entity_manager')
43 ->getRepository('WallabagCoreBundle:Entry')
44 ->findOneBy(['user' => 1, 'isArchived' => false]);
45
46 if (!$entry) {
47 $this->markTestSkipped('No content found in db.');
48 }
49
50 $this->client->request('GET', '/api/entries/'.$entry->getId().'/export.epub');
51 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
52
53 // epub format got the content type in the content
54 $this->assertContains('application/epub', $this->client->getResponse()->getContent());
55 $this->assertEquals('application/epub+zip', $this->client->getResponse()->headers->get('Content-Type'));
56
57 // re-auth client for mobi
58 $client = $this->createAuthorizedClient();
59 $client->request('GET', '/api/entries/'.$entry->getId().'/export.mobi');
60 $this->assertEquals(200, $client->getResponse()->getStatusCode());
61
62 $this->assertEquals('application/x-mobipocket-ebook', $client->getResponse()->headers->get('Content-Type'));
63
64 // re-auth client for pdf
65 $client = $this->createAuthorizedClient();
66 $client->request('GET', '/api/entries/'.$entry->getId().'/export.pdf');
67 $this->assertEquals(200, $client->getResponse()->getStatusCode());
68
69 $this->assertContains('PDF-', $client->getResponse()->getContent());
70 $this->assertEquals('application/pdf', $client->getResponse()->headers->get('Content-Type'));
71
72 // re-auth client for pdf
73 $client = $this->createAuthorizedClient();
74 $client->request('GET', '/api/entries/'.$entry->getId().'/export.txt');
75 $this->assertEquals(200, $client->getResponse()->getStatusCode());
76
77 $this->assertContains('text/plain', $client->getResponse()->headers->get('Content-Type'));
78
79 // re-auth client for pdf
80 $client = $this->createAuthorizedClient();
81 $client->request('GET', '/api/entries/'.$entry->getId().'/export.csv');
82 $this->assertEquals(200, $client->getResponse()->getStatusCode());
83
84 $this->assertContains('application/csv', $client->getResponse()->headers->get('Content-Type'));
85 }
86
87 public function testGetOneEntryWrongUser()
88 {
89 $entry = $this->client->getContainer()
90 ->get('doctrine.orm.entity_manager')
91 ->getRepository('WallabagCoreBundle:Entry')
92 ->findOneBy(['user' => 2, 'isArchived' => false]);
93
94 if (!$entry) {
95 $this->markTestSkipped('No content found in db.');
96 }
97
98 $this->client->request('GET', '/api/entries/'.$entry->getId().'.json');
99
100 $this->assertEquals(403, $this->client->getResponse()->getStatusCode());
101 }
102
103 public function testGetEntries()
104 {
105 $this->client->request('GET', '/api/entries');
106
107 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
108
109 $content = json_decode($this->client->getResponse()->getContent(), true);
110
111 $this->assertGreaterThanOrEqual(1, count($content));
112 $this->assertNotEmpty($content['_embedded']['items']);
113 $this->assertGreaterThanOrEqual(1, $content['total']);
114 $this->assertEquals(1, $content['page']);
115 $this->assertGreaterThanOrEqual(1, $content['pages']);
116
117 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
118 }
119
120 public function testGetEntriesWithFullOptions()
121 {
122 $this->client->request('GET', '/api/entries', [
123 'archive' => 1,
124 'starred' => 1,
125 'sort' => 'updated',
126 'order' => 'asc',
127 'page' => 1,
128 'perPage' => 2,
129 'tags' => 'foo',
130 'since' => 1443274283,
131 ]);
132
133 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
134
135 $content = json_decode($this->client->getResponse()->getContent(), true);
136
137 $this->assertGreaterThanOrEqual(1, count($content));
138 $this->assertArrayHasKey('items', $content['_embedded']);
139 $this->assertGreaterThanOrEqual(0, $content['total']);
140 $this->assertEquals(1, $content['page']);
141 $this->assertEquals(2, $content['limit']);
142 $this->assertGreaterThanOrEqual(1, $content['pages']);
143
144 $this->assertArrayHasKey('_links', $content);
145 $this->assertArrayHasKey('self', $content['_links']);
146 $this->assertArrayHasKey('first', $content['_links']);
147 $this->assertArrayHasKey('last', $content['_links']);
148
149 foreach (['self', 'first', 'last'] as $link) {
150 $this->assertArrayHasKey('href', $content['_links'][$link]);
151 $this->assertContains('archive=1', $content['_links'][$link]['href']);
152 $this->assertContains('starred=1', $content['_links'][$link]['href']);
153 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
154 $this->assertContains('order=asc', $content['_links'][$link]['href']);
155 $this->assertContains('tags=foo', $content['_links'][$link]['href']);
156 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
157 }
158
159 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
160 }
161
162 public function testGetEntriesOnPageTwo()
163 {
164 $this->client->request('GET', '/api/entries', [
165 'page' => 2,
166 'perPage' => 2,
167 ]);
168
169 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
170
171 $content = json_decode($this->client->getResponse()->getContent(), true);
172
173 $this->assertGreaterThanOrEqual(0, $content['total']);
174 $this->assertEquals(2, $content['page']);
175 $this->assertEquals(2, $content['limit']);
176 }
177
178 public function testGetStarredEntries()
179 {
180 $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']);
181
182 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
183
184 $content = json_decode($this->client->getResponse()->getContent(), true);
185
186 $this->assertGreaterThanOrEqual(1, count($content));
187 $this->assertNotEmpty($content['_embedded']['items']);
188 $this->assertGreaterThanOrEqual(1, $content['total']);
189 $this->assertEquals(1, $content['page']);
190 $this->assertGreaterThanOrEqual(1, $content['pages']);
191
192 $this->assertArrayHasKey('_links', $content);
193 $this->assertArrayHasKey('self', $content['_links']);
194 $this->assertArrayHasKey('first', $content['_links']);
195 $this->assertArrayHasKey('last', $content['_links']);
196
197 foreach (['self', 'first', 'last'] as $link) {
198 $this->assertArrayHasKey('href', $content['_links'][$link]);
199 $this->assertContains('starred=1', $content['_links'][$link]['href']);
200 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
201 }
202
203 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
204 }
205
206 public function testGetArchiveEntries()
207 {
208 $this->client->request('GET', '/api/entries', ['archive' => 1]);
209
210 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
211
212 $content = json_decode($this->client->getResponse()->getContent(), true);
213
214 $this->assertGreaterThanOrEqual(1, count($content));
215 $this->assertNotEmpty($content['_embedded']['items']);
216 $this->assertGreaterThanOrEqual(1, $content['total']);
217 $this->assertEquals(1, $content['page']);
218 $this->assertGreaterThanOrEqual(1, $content['pages']);
219
220 $this->assertArrayHasKey('_links', $content);
221 $this->assertArrayHasKey('self', $content['_links']);
222 $this->assertArrayHasKey('first', $content['_links']);
223 $this->assertArrayHasKey('last', $content['_links']);
224
225 foreach (['self', 'first', 'last'] as $link) {
226 $this->assertArrayHasKey('href', $content['_links'][$link]);
227 $this->assertContains('archive=1', $content['_links'][$link]['href']);
228 }
229
230 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
231 }
232
233 public function testGetTaggedEntries()
234 {
235 $this->client->request('GET', '/api/entries', ['tags' => 'foo,bar']);
236
237 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
238
239 $content = json_decode($this->client->getResponse()->getContent(), true);
240
241 $this->assertGreaterThanOrEqual(1, count($content));
242 $this->assertNotEmpty($content['_embedded']['items']);
243 $this->assertGreaterThanOrEqual(1, $content['total']);
244 $this->assertEquals(1, $content['page']);
245 $this->assertGreaterThanOrEqual(1, $content['pages']);
246
247 $this->assertArrayHasKey('_links', $content);
248 $this->assertArrayHasKey('self', $content['_links']);
249 $this->assertArrayHasKey('first', $content['_links']);
250 $this->assertArrayHasKey('last', $content['_links']);
251
252 foreach (['self', 'first', 'last'] as $link) {
253 $this->assertArrayHasKey('href', $content['_links'][$link]);
254 $this->assertContains('tags='.urlencode('foo,bar'), $content['_links'][$link]['href']);
255 }
256
257 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
258 }
259
260 public function testGetDatedEntries()
261 {
262 $this->client->request('GET', '/api/entries', ['since' => 1443274283]);
263
264 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
265
266 $content = json_decode($this->client->getResponse()->getContent(), true);
267
268 $this->assertGreaterThanOrEqual(1, count($content));
269 $this->assertNotEmpty($content['_embedded']['items']);
270 $this->assertGreaterThanOrEqual(1, $content['total']);
271 $this->assertEquals(1, $content['page']);
272 $this->assertGreaterThanOrEqual(1, $content['pages']);
273
274 $this->assertArrayHasKey('_links', $content);
275 $this->assertArrayHasKey('self', $content['_links']);
276 $this->assertArrayHasKey('first', $content['_links']);
277 $this->assertArrayHasKey('last', $content['_links']);
278
279 foreach (['self', 'first', 'last'] as $link) {
280 $this->assertArrayHasKey('href', $content['_links'][$link]);
281 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
282 }
283
284 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
285 }
286
287 public function testGetDatedSupEntries()
288 {
289 $future = new \DateTime(date('Y-m-d H:i:s'));
290 $this->client->request('GET', '/api/entries', ['since' => $future->getTimestamp() + 1000]);
291
292 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
293
294 $content = json_decode($this->client->getResponse()->getContent(), true);
295
296 $this->assertGreaterThanOrEqual(1, count($content));
297 $this->assertEmpty($content['_embedded']['items']);
298 $this->assertEquals(0, $content['total']);
299 $this->assertEquals(1, $content['page']);
300 $this->assertEquals(1, $content['pages']);
301
302 $this->assertArrayHasKey('_links', $content);
303 $this->assertArrayHasKey('self', $content['_links']);
304 $this->assertArrayHasKey('first', $content['_links']);
305 $this->assertArrayHasKey('last', $content['_links']);
306
307 foreach (['self', 'first', 'last'] as $link) {
308 $this->assertArrayHasKey('href', $content['_links'][$link]);
309 $this->assertContains('since='.($future->getTimestamp() + 1000), $content['_links'][$link]['href']);
310 }
311
312 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
313 }
314
315 public function testDeleteEntry()
316 {
317 $entry = $this->client->getContainer()
318 ->get('doctrine.orm.entity_manager')
319 ->getRepository('WallabagCoreBundle:Entry')
320 ->findOneByUser(1, ['id' => 'asc']);
321
322 if (!$entry) {
323 $this->markTestSkipped('No content found in db.');
324 }
325
326 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json');
327
328 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
329
330 $content = json_decode($this->client->getResponse()->getContent(), true);
331
332 $this->assertEquals($entry->getTitle(), $content['title']);
333 $this->assertEquals($entry->getUrl(), $content['url']);
334
335 // We'll try to delete this entry again
336 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json');
337
338 $this->assertEquals(404, $this->client->getResponse()->getStatusCode());
339 }
340
341 public function testPostEntry()
342 {
343 $this->client->request('POST', '/api/entries.json', [
344 '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',
345 'tags' => 'google',
346 'title' => 'New title for my article',
347 ]);
348
349 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
350
351 $content = json_decode($this->client->getResponse()->getContent(), true);
352
353 $this->assertGreaterThan(0, $content['id']);
354 $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']);
355 $this->assertEquals(false, $content['is_archived']);
356 $this->assertEquals(false, $content['is_starred']);
357 $this->assertEquals('New title for my article', $content['title']);
358 $this->assertEquals(1, $content['user_id']);
359 $this->assertCount(2, $content['tags']);
360 }
361
362 public function testPostSameEntry()
363 {
364 $this->client->request('POST', '/api/entries.json', [
365 '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',
366 'archive' => '1',
367 'tags' => 'google, apple',
368 ]);
369
370 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
371
372 $content = json_decode($this->client->getResponse()->getContent(), true);
373
374 $this->assertGreaterThan(0, $content['id']);
375 $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']);
376 $this->assertEquals(true, $content['is_archived']);
377 $this->assertEquals(false, $content['is_starred']);
378 $this->assertCount(3, $content['tags']);
379 }
380
381 public function testPostEntryWhenFetchContentFails()
382 {
383 /** @var \Symfony\Component\DependencyInjection\Container $container */
384 $container = $this->client->getContainer();
385 $contentProxy = $this->getMockBuilder(ContentProxy::class)
386 ->disableOriginalConstructor()
387 ->setMethods(['updateEntry'])
388 ->getMock();
389 $contentProxy->expects($this->any())
390 ->method('updateEntry')
391 ->willThrowException(new \Exception('Test Fetch content fails'));
392 $container->set('wallabag_core.content_proxy', $contentProxy);
393
394 try {
395 $this->client->request('POST', '/api/entries.json', [
396 'url' => 'http://www.example.com/',
397 ]);
398
399 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
400 $content = json_decode($this->client->getResponse()->getContent(), true);
401 $this->assertGreaterThan(0, $content['id']);
402 $this->assertEquals('http://www.example.com/', $content['url']);
403 } finally {
404 // Remove the created entry to avoid side effects on other tests
405 if (isset($content['id'])) {
406 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
407 $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']);
408 $em->remove($entry);
409 $em->flush();
410 }
411 }
412 }
413
414 public function testPostArchivedAndStarredEntry()
415 {
416 $this->client->request('POST', '/api/entries.json', [
417 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
418 'archive' => '1',
419 'starred' => '1',
420 ]);
421
422 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
423
424 $content = json_decode($this->client->getResponse()->getContent(), true);
425
426 $this->assertGreaterThan(0, $content['id']);
427 $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']);
428 $this->assertEquals(true, $content['is_archived']);
429 $this->assertEquals(true, $content['is_starred']);
430 $this->assertEquals(1, $content['user_id']);
431 }
432
433 public function testPostArchivedAndStarredEntryWithoutQuotes()
434 {
435 $this->client->request('POST', '/api/entries.json', [
436 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
437 'archive' => 0,
438 'starred' => 1,
439 ]);
440
441 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
442
443 $content = json_decode($this->client->getResponse()->getContent(), true);
444
445 $this->assertGreaterThan(0, $content['id']);
446 $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']);
447 $this->assertEquals(false, $content['is_archived']);
448 $this->assertEquals(true, $content['is_starred']);
449 }
450
451 public function testPatchEntry()
452 {
453 $entry = $this->client->getContainer()
454 ->get('doctrine.orm.entity_manager')
455 ->getRepository('WallabagCoreBundle:Entry')
456 ->findOneByUser(1);
457
458 if (!$entry) {
459 $this->markTestSkipped('No content found in db.');
460 }
461
462 // hydrate the tags relations
463 $nbTags = count($entry->getTags());
464
465 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
466 'title' => 'New awesome title',
467 'tags' => 'new tag '.uniqid(),
468 'starred' => '1',
469 'archive' => '0',
470 ]);
471
472 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
473
474 $content = json_decode($this->client->getResponse()->getContent(), true);
475
476 $this->assertEquals($entry->getId(), $content['id']);
477 $this->assertEquals($entry->getUrl(), $content['url']);
478 $this->assertEquals('New awesome title', $content['title']);
479 $this->assertGreaterThan($nbTags, count($content['tags']));
480 $this->assertEquals(1, $content['user_id']);
481 }
482
483 public function testPatchEntryWithoutQuotes()
484 {
485 $entry = $this->client->getContainer()
486 ->get('doctrine.orm.entity_manager')
487 ->getRepository('WallabagCoreBundle:Entry')
488 ->findOneByUser(1);
489
490 if (!$entry) {
491 $this->markTestSkipped('No content found in db.');
492 }
493
494 // hydrate the tags relations
495 $nbTags = count($entry->getTags());
496
497 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
498 'title' => 'New awesome title',
499 'tags' => 'new tag '.uniqid(),
500 'starred' => 1,
501 'archive' => 0,
502 ]);
503
504 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
505
506 $content = json_decode($this->client->getResponse()->getContent(), true);
507
508 $this->assertEquals($entry->getId(), $content['id']);
509 $this->assertEquals($entry->getUrl(), $content['url']);
510 $this->assertEquals('New awesome title', $content['title']);
511 $this->assertGreaterThan($nbTags, count($content['tags']));
512 }
513
514 public function testGetTagsEntry()
515 {
516 $entry = $this->client->getContainer()
517 ->get('doctrine.orm.entity_manager')
518 ->getRepository('WallabagCoreBundle:Entry')
519 ->findOneWithTags($this->user->getId());
520
521 $entry = $entry[0];
522
523 if (!$entry) {
524 $this->markTestSkipped('No content found in db.');
525 }
526
527 $tags = [];
528 foreach ($entry->getTags() as $tag) {
529 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
530 }
531
532 $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags');
533
534 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent());
535 }
536
537 public function testPostTagsOnEntry()
538 {
539 $entry = $this->client->getContainer()
540 ->get('doctrine.orm.entity_manager')
541 ->getRepository('WallabagCoreBundle:Entry')
542 ->findOneByUser(1);
543
544 if (!$entry) {
545 $this->markTestSkipped('No content found in db.');
546 }
547
548 $nbTags = count($entry->getTags());
549
550 $newTags = 'tag1,tag2,tag3';
551
552 $this->client->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]);
553
554 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
555
556 $content = json_decode($this->client->getResponse()->getContent(), true);
557
558 $this->assertArrayHasKey('tags', $content);
559 $this->assertEquals($nbTags + 3, count($content['tags']));
560
561 $entryDB = $this->client->getContainer()
562 ->get('doctrine.orm.entity_manager')
563 ->getRepository('WallabagCoreBundle:Entry')
564 ->find($entry->getId());
565
566 $tagsInDB = [];
567 foreach ($entryDB->getTags()->toArray() as $tag) {
568 $tagsInDB[$tag->getId()] = $tag->getLabel();
569 }
570
571 foreach (explode(',', $newTags) as $tag) {
572 $this->assertContains($tag, $tagsInDB);
573 }
574 }
575
576 public function testDeleteOneTagEntry()
577 {
578 $entry = $this->client->getContainer()
579 ->get('doctrine.orm.entity_manager')
580 ->getRepository('WallabagCoreBundle:Entry')
581 ->findOneWithTags($this->user->getId());
582 $entry = $entry[0];
583
584 if (!$entry) {
585 $this->markTestSkipped('No content found in db.');
586 }
587
588 // hydrate the tags relations
589 $nbTags = count($entry->getTags());
590 $tag = $entry->getTags()[0];
591
592 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json');
593
594 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
595
596 $content = json_decode($this->client->getResponse()->getContent(), true);
597
598 $this->assertArrayHasKey('tags', $content);
599 $this->assertEquals($nbTags - 1, count($content['tags']));
600 }
601
602 public function testSaveIsArchivedAfterPost()
603 {
604 $entry = $this->client->getContainer()
605 ->get('doctrine.orm.entity_manager')
606 ->getRepository('WallabagCoreBundle:Entry')
607 ->findOneBy(['user' => 1, 'isArchived' => true]);
608
609 if (!$entry) {
610 $this->markTestSkipped('No content found in db.');
611 }
612
613 $this->client->request('POST', '/api/entries.json', [
614 'url' => $entry->getUrl(),
615 ]);
616
617 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
618
619 $content = json_decode($this->client->getResponse()->getContent(), true);
620
621 $this->assertEquals(true, $content['is_archived']);
622 }
623
624 public function testSaveIsStarredAfterPost()
625 {
626 $entry = $this->client->getContainer()
627 ->get('doctrine.orm.entity_manager')
628 ->getRepository('WallabagCoreBundle:Entry')
629 ->findOneBy(['user' => 1, 'isStarred' => true]);
630
631 if (!$entry) {
632 $this->markTestSkipped('No content found in db.');
633 }
634
635 $this->client->request('POST', '/api/entries.json', [
636 'url' => $entry->getUrl(),
637 ]);
638
639 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
640
641 $content = json_decode($this->client->getResponse()->getContent(), true);
642
643 $this->assertEquals(true, $content['is_starred']);
644 }
645
646 public function testSaveIsArchivedAfterPatch()
647 {
648 $entry = $this->client->getContainer()
649 ->get('doctrine.orm.entity_manager')
650 ->getRepository('WallabagCoreBundle:Entry')
651 ->findOneBy(['user' => 1, 'isArchived' => true]);
652
653 if (!$entry) {
654 $this->markTestSkipped('No content found in db.');
655 }
656
657 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
658 'title' => $entry->getTitle().'++',
659 ]);
660
661 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
662
663 $content = json_decode($this->client->getResponse()->getContent(), true);
664
665 $this->assertEquals(true, $content['is_archived']);
666 }
667
668 public function testSaveIsStarredAfterPatch()
669 {
670 $entry = $this->client->getContainer()
671 ->get('doctrine.orm.entity_manager')
672 ->getRepository('WallabagCoreBundle:Entry')
673 ->findOneBy(['user' => 1, 'isStarred' => true]);
674
675 if (!$entry) {
676 $this->markTestSkipped('No content found in db.');
677 }
678 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
679 'title' => $entry->getTitle().'++',
680 ]);
681
682 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
683
684 $content = json_decode($this->client->getResponse()->getContent(), true);
685
686 $this->assertEquals(true, $content['is_starred']);
687 }
688
689 public function testGetEntriesExists()
690 {
691 $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2');
692
693 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
694
695 $content = json_decode($this->client->getResponse()->getContent(), true);
696
697 $this->assertEquals(2, $content['exists']);
698 }
699
700 public function testGetEntriesExistsWithManyUrls()
701 {
702 $url1 = 'http://0.0.0.0/entry2';
703 $url2 = 'http://0.0.0.0/entry10';
704 $this->client->request('GET', '/api/entries/exists?urls[]='.$url1.'&urls[]='.$url2);
705
706 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
707
708 $content = json_decode($this->client->getResponse()->getContent(), true);
709
710 $this->assertArrayHasKey($url1, $content);
711 $this->assertArrayHasKey($url2, $content);
712 $this->assertEquals(2, $content[$url1]);
713 $this->assertEquals(false, $content[$url2]);
714 }
715
716 public function testGetEntriesExistsWhichDoesNotExists()
717 {
718 $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2');
719
720 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
721
722 $content = json_decode($this->client->getResponse()->getContent(), true);
723
724 $this->assertEquals(false, $content['exists']);
725 }
726
727 public function testGetEntriesExistsWithNoUrl()
728 {
729 $this->client->request('GET', '/api/entries/exists?url=');
730
731 $this->assertEquals(403, $this->client->getResponse()->getStatusCode());
732 }
733
734 public function testReloadEntryErrorWhileFetching()
735 {
736 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
737 ->getRepository('WallabagCoreBundle:Entry')
738 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
739
740 if (!$entry) {
741 $this->markTestSkipped('No content found in db.');
742 }
743
744 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'/reload.json');
745 $this->assertEquals(304, $this->client->getResponse()->getStatusCode());
746 }
747
748 public function testReloadEntry()
749 {
750 $this->client->request('POST', '/api/entries.json', [
751 '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',
752 'archive' => '1',
753 'tags' => 'google, apple',
754 ]);
755
756 $json = json_decode($this->client->getResponse()->getContent(), true);
757
758 $this->setUp();
759
760 $this->client->request('PATCH', '/api/entries/'.$json['id'].'/reload.json');
761 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
762
763 $content = json_decode($this->client->getResponse()->getContent(), true);
764
765 $this->assertNotEmpty($content['title']);
766
767 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
768 }
769
770 public function testPostEntriesTagsListAction()
771 {
772 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
773 ->getRepository('WallabagCoreBundle:Entry')
774 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
775
776 $tags = $entry->getTags();
777
778 $this->assertCount(2, $tags);
779
780 $list = [
781 [
782 'url' => 'http://0.0.0.0/entry4',
783 'tags' => 'new tag 1, new tag 2',
784 ],
785 ];
786
787 $this->client->request('POST', '/api/entries/tags/lists?list='.json_encode($list));
788
789 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
790
791 $content = json_decode($this->client->getResponse()->getContent(), true);
792
793 $this->assertInternalType('int', $content[0]['entry']);
794 $this->assertEquals('http://0.0.0.0/entry4', $content[0]['url']);
795
796 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
797 ->getRepository('WallabagCoreBundle:Entry')
798 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
799
800 $tags = $entry->getTags();
801 $this->assertCount(4, $tags);
802 }
803
804 public function testDeleteEntriesTagsListAction()
805 {
806 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
807 $entry = new Entry($em->getReference(User::class, 1));
808 $entry->setUrl('http://0.0.0.0/test-entry');
809 $entry->addTag((new Tag())->setLabel('foo-tag'));
810 $entry->addTag((new Tag())->setLabel('bar-tag'));
811 $em->persist($entry);
812 $em->flush();
813
814 $em->clear();
815
816 $list = [
817 [
818 'url' => 'http://0.0.0.0/test-entry',
819 'tags' => 'foo-tag, bar-tag',
820 ],
821 ];
822
823 $this->client->request('DELETE', '/api/entries/tags/list?list='.json_encode($list));
824 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
825
826 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
827 $this->assertCount(0, $entry->getTags());
828 }
829
830 public function testPostEntriesListAction()
831 {
832 $list = [
833 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
834 'http://0.0.0.0/entry2',
835 ];
836
837 $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list));
838
839 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
840
841 $content = json_decode($this->client->getResponse()->getContent(), true);
842
843 $this->assertInternalType('int', $content[0]['entry']);
844 $this->assertEquals('http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', $content[0]['url']);
845
846 $this->assertInternalType('int', $content[1]['entry']);
847 $this->assertEquals('http://0.0.0.0/entry2', $content[1]['url']);
848 }
849
850 public function testDeleteEntriesListAction()
851 {
852 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
853 $em->persist((new Entry($em->getReference(User::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
854
855 $em->flush();
856 $em->clear();
857 $list = [
858 'http://0.0.0.0/test-entry1',
859 'http://0.0.0.0/test-entry-not-exist',
860 ];
861
862 $this->client->request('DELETE', '/api/entries/list?urls='.json_encode($list));
863
864 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
865
866 $content = json_decode($this->client->getResponse()->getContent(), true);
867
868 $this->assertTrue($content[0]['entry']);
869 $this->assertEquals('http://0.0.0.0/test-entry1', $content[0]['url']);
870
871 $this->assertFalse($content[1]['entry']);
872 $this->assertEquals('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
873 }
874
875 public function testLimitBulkAction()
876 {
877 $list = [
878 'http://0.0.0.0/entry1',
879 'http://0.0.0.0/entry1',
880 'http://0.0.0.0/entry1',
881 'http://0.0.0.0/entry1',
882 'http://0.0.0.0/entry1',
883 'http://0.0.0.0/entry1',
884 'http://0.0.0.0/entry1',
885 'http://0.0.0.0/entry1',
886 'http://0.0.0.0/entry1',
887 'http://0.0.0.0/entry1',
888 'http://0.0.0.0/entry1',
889 ];
890
891 $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list));
892
893 $this->assertEquals(400, $this->client->getResponse()->getStatusCode());
894 $this->assertContains('API limit reached', $this->client->getResponse()->getContent());
895 }
896 }