3 namespace Tests\Wallabag\ApiBundle\Controller
;
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
;
11 class EntryRestControllerTest
extends WallabagApiTestCase
13 public function testGetOneEntry()
15 $entry = $this->client
->getContainer()
16 ->get('doctrine.orm.entity_manager')
17 ->getRepository('WallabagCoreBundle:Entry')
18 ->findOneBy(['user' => 1, 'isArchived' => false]);
21 $this->markTestSkipped('No content found in db.');
24 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '.json');
25 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
27 $content = json_decode($this->client
->getResponse()->getContent(), true);
29 $this->assertSame($entry->getTitle(), $content['title']);
30 $this->assertSame($entry->getUrl(), $content['url']);
31 $this->assertCount(count($entry->getTags()), $content['tags']);
32 $this->assertSame($entry->getUserName(), $content['user_name']);
33 $this->assertSame($entry->getUserEmail(), $content['user_email']);
34 $this->assertSame($entry->getUserId(), $content['user_id']);
36 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
39 public function testExportEntry()
41 $entry = $this->client
->getContainer()
42 ->get('doctrine.orm.entity_manager')
43 ->getRepository('WallabagCoreBundle:Entry')
44 ->findOneBy(['user' => 1, 'isArchived' => false]);
47 $this->markTestSkipped('No content found in db.');
50 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '/export.epub');
51 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
53 // epub format got the content type in the content
54 $this->assertContains('application/epub', $this->client
->getResponse()->getContent());
55 $this->assertSame('application/epub+zip', $this->client
->getResponse()->headers
->get('Content-Type'));
57 // re-auth client for mobi
58 $client = $this->createAuthorizedClient();
59 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.mobi');
60 $this->assertSame(200, $client->getResponse()->getStatusCode());
62 $this->assertSame('application/x-mobipocket-ebook', $client->getResponse()->headers
->get('Content-Type'));
64 // re-auth client for pdf
65 $client = $this->createAuthorizedClient();
66 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.pdf');
67 $this->assertSame(200, $client->getResponse()->getStatusCode());
69 $this->assertContains('PDF-', $client->getResponse()->getContent());
70 $this->assertSame('application/pdf', $client->getResponse()->headers
->get('Content-Type'));
72 // re-auth client for pdf
73 $client = $this->createAuthorizedClient();
74 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.txt');
75 $this->assertSame(200, $client->getResponse()->getStatusCode());
77 $this->assertContains('text/plain', $client->getResponse()->headers
->get('Content-Type'));
79 // re-auth client for pdf
80 $client = $this->createAuthorizedClient();
81 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.csv');
82 $this->assertSame(200, $client->getResponse()->getStatusCode());
84 $this->assertContains('application/csv', $client->getResponse()->headers
->get('Content-Type'));
87 public function testGetOneEntryWrongUser()
89 $entry = $this->client
->getContainer()
90 ->get('doctrine.orm.entity_manager')
91 ->getRepository('WallabagCoreBundle:Entry')
92 ->findOneBy(['user' => 2, 'isArchived' => false]);
95 $this->markTestSkipped('No content found in db.');
98 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '.json');
100 $this->assertSame(403, $this->client
->getResponse()->getStatusCode());
103 public function testGetEntries()
105 $this->client
->request('GET', '/api/entries');
107 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
109 $content = json_decode($this->client
->getResponse()->getContent(), true);
111 $this->assertGreaterThanOrEqual(1, count($content));
112 $this->assertNotEmpty($content['_embedded']['items']);
113 $this->assertGreaterThanOrEqual(1, $content['total']);
114 $this->assertSame(1, $content['page']);
115 $this->assertGreaterThanOrEqual(1, $content['pages']);
117 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
120 public function testGetEntriesWithFullOptions()
122 $this->client
->request('GET', '/api/entries', [
130 'since' => 1443274283,
134 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
136 $content = json_decode($this->client
->getResponse()->getContent(), true);
138 $this->assertGreaterThanOrEqual(1, count($content));
139 $this->assertArrayHasKey('items', $content['_embedded']);
140 $this->assertGreaterThanOrEqual(0, $content['total']);
141 $this->assertSame(1, $content['page']);
142 $this->assertSame(2, $content['limit']);
143 $this->assertGreaterThanOrEqual(1, $content['pages']);
145 $this->assertArrayHasKey('_links', $content);
146 $this->assertArrayHasKey('self', $content['_links']);
147 $this->assertArrayHasKey('first', $content['_links']);
148 $this->assertArrayHasKey('last', $content['_links']);
150 foreach (['self', 'first', 'last'] as $link) {
151 $this->assertArrayHasKey('href', $content['_links'][$link]);
152 $this->assertContains('archive=1', $content['_links'][$link]['href']);
153 $this->assertContains('starred=1', $content['_links'][$link]['href']);
154 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
155 $this->assertContains('order=asc', $content['_links'][$link]['href']);
156 $this->assertContains('tags=foo', $content['_links'][$link]['href']);
157 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
158 $this->assertContains('public=0', $content['_links'][$link]['href']);
161 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
164 public function testGetEntriesPublicOnly()
166 $entry = $this->client
->getContainer()
167 ->get('doctrine.orm.entity_manager')
168 ->getRepository('WallabagCoreBundle:Entry')
172 $this->markTestSkipped('No content found in db.');
175 // generate at least one public entry
176 $entry->generateUid();
178 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
179 $em->persist($entry);
182 $this->client
->request('GET', '/api/entries', [
186 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
188 $content = json_decode($this->client
->getResponse()->getContent(), true);
190 $this->assertGreaterThanOrEqual(1, count($content));
191 $this->assertArrayHasKey('items', $content['_embedded']);
192 $this->assertGreaterThanOrEqual(1, $content['total']);
193 $this->assertSame(1, $content['page']);
194 $this->assertSame(30, $content['limit']);
195 $this->assertGreaterThanOrEqual(1, $content['pages']);
197 $this->assertArrayHasKey('_links', $content);
198 $this->assertArrayHasKey('self', $content['_links']);
199 $this->assertArrayHasKey('first', $content['_links']);
200 $this->assertArrayHasKey('last', $content['_links']);
202 foreach (['self', 'first', 'last'] as $link) {
203 $this->assertArrayHasKey('href', $content['_links'][$link]);
204 $this->assertContains('public=1', $content['_links'][$link]['href']);
207 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
210 public function testGetEntriesOnPageTwo()
212 $this->client
->request('GET', '/api/entries', [
217 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
219 $content = json_decode($this->client
->getResponse()->getContent(), true);
221 $this->assertGreaterThanOrEqual(0, $content['total']);
222 $this->assertSame(2, $content['page']);
223 $this->assertSame(2, $content['limit']);
226 public function testGetStarredEntries()
228 $this->client
->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']);
230 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
232 $content = json_decode($this->client
->getResponse()->getContent(), true);
234 $this->assertGreaterThanOrEqual(1, count($content));
235 $this->assertNotEmpty($content['_embedded']['items']);
236 $this->assertGreaterThanOrEqual(1, $content['total']);
237 $this->assertSame(1, $content['page']);
238 $this->assertGreaterThanOrEqual(1, $content['pages']);
240 $this->assertArrayHasKey('_links', $content);
241 $this->assertArrayHasKey('self', $content['_links']);
242 $this->assertArrayHasKey('first', $content['_links']);
243 $this->assertArrayHasKey('last', $content['_links']);
245 foreach (['self', 'first', 'last'] as $link) {
246 $this->assertArrayHasKey('href', $content['_links'][$link]);
247 $this->assertContains('starred=1', $content['_links'][$link]['href']);
248 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
251 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
254 public function testGetArchiveEntries()
256 $this->client
->request('GET', '/api/entries', ['archive' => 1]);
258 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
260 $content = json_decode($this->client
->getResponse()->getContent(), true);
262 $this->assertGreaterThanOrEqual(1, count($content));
263 $this->assertNotEmpty($content['_embedded']['items']);
264 $this->assertGreaterThanOrEqual(1, $content['total']);
265 $this->assertSame(1, $content['page']);
266 $this->assertGreaterThanOrEqual(1, $content['pages']);
268 $this->assertArrayHasKey('_links', $content);
269 $this->assertArrayHasKey('self', $content['_links']);
270 $this->assertArrayHasKey('first', $content['_links']);
271 $this->assertArrayHasKey('last', $content['_links']);
273 foreach (['self', 'first', 'last'] as $link) {
274 $this->assertArrayHasKey('href', $content['_links'][$link]);
275 $this->assertContains('archive=1', $content['_links'][$link]['href']);
278 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
281 public function testGetTaggedEntries()
283 $this->client
->request('GET', '/api/entries', ['tags' => 'foo,bar']);
285 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
287 $content = json_decode($this->client
->getResponse()->getContent(), true);
289 $this->assertGreaterThanOrEqual(1, count($content));
290 $this->assertNotEmpty($content['_embedded']['items']);
291 $this->assertGreaterThanOrEqual(1, $content['total']);
292 $this->assertSame(1, $content['page']);
293 $this->assertGreaterThanOrEqual(1, $content['pages']);
295 $this->assertArrayHasKey('_links', $content);
296 $this->assertArrayHasKey('self', $content['_links']);
297 $this->assertArrayHasKey('first', $content['_links']);
298 $this->assertArrayHasKey('last', $content['_links']);
300 foreach (['self', 'first', 'last'] as $link) {
301 $this->assertArrayHasKey('href', $content['_links'][$link]);
302 $this->assertContains('tags=' . urlencode('foo,bar'), $content['_links'][$link]['href']);
305 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
308 public function testGetDatedEntries()
310 $this->client
->request('GET', '/api/entries', ['since' => 1443274283]);
312 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
314 $content = json_decode($this->client
->getResponse()->getContent(), true);
316 $this->assertGreaterThanOrEqual(1, count($content));
317 $this->assertNotEmpty($content['_embedded']['items']);
318 $this->assertGreaterThanOrEqual(1, $content['total']);
319 $this->assertSame(1, $content['page']);
320 $this->assertGreaterThanOrEqual(1, $content['pages']);
322 $this->assertArrayHasKey('_links', $content);
323 $this->assertArrayHasKey('self', $content['_links']);
324 $this->assertArrayHasKey('first', $content['_links']);
325 $this->assertArrayHasKey('last', $content['_links']);
327 foreach (['self', 'first', 'last'] as $link) {
328 $this->assertArrayHasKey('href', $content['_links'][$link]);
329 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
332 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
335 public function testGetDatedSupEntries()
337 $future = new \
DateTime(date('Y-m-d H:i:s'));
338 $this->client
->request('GET', '/api/entries', ['since' => $future->getTimestamp() +
1000]);
340 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
342 $content = json_decode($this->client
->getResponse()->getContent(), true);
344 $this->assertGreaterThanOrEqual(1, count($content));
345 $this->assertEmpty($content['_embedded']['items']);
346 $this->assertSame(0, $content['total']);
347 $this->assertSame(1, $content['page']);
348 $this->assertSame(1, $content['pages']);
350 $this->assertArrayHasKey('_links', $content);
351 $this->assertArrayHasKey('self', $content['_links']);
352 $this->assertArrayHasKey('first', $content['_links']);
353 $this->assertArrayHasKey('last', $content['_links']);
355 foreach (['self', 'first', 'last'] as $link) {
356 $this->assertArrayHasKey('href', $content['_links'][$link]);
357 $this->assertContains('since=' . ($future->getTimestamp() +
1000), $content['_links'][$link]['href']);
360 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
363 public function testDeleteEntry()
365 $entry = $this->client
->getContainer()
366 ->get('doctrine.orm.entity_manager')
367 ->getRepository('WallabagCoreBundle:Entry')
368 ->findOneByUser(1, ['id' => 'asc']);
371 $this->markTestSkipped('No content found in db.');
374 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '.json');
376 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
378 $content = json_decode($this->client
->getResponse()->getContent(), true);
380 $this->assertSame($entry->getTitle(), $content['title']);
381 $this->assertSame($entry->getUrl(), $content['url']);
383 // We'll try to delete this entry again
384 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '.json');
386 $this->assertSame(404, $this->client
->getResponse()->getStatusCode());
389 public function testPostEntry()
391 $this->client
->request('POST', '/api/entries.json', [
392 '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',
394 'title' => 'New title for my article',
395 'content' => 'my content',
397 'published_at' => '2016-09-08T11:55:58+0200',
398 'authors' => 'bob,helen',
402 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
404 $content = json_decode($this->client
->getResponse()->getContent(), true);
406 $this->assertGreaterThan(0, $content['id']);
407 $this->assertSame('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']);
408 $this->assertSame(0, $content['is_archived']);
409 $this->assertSame(0, $content['is_starred']);
410 $this->assertSame('New title for my article', $content['title']);
411 $this->assertSame(1, $content['user_id']);
412 $this->assertCount(2, $content['tags']);
413 $this->assertSame('my content', $content['content']);
414 $this->assertSame('de', $content['language']);
415 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
416 $this->assertCount(2, $content['published_by']);
417 $this->assertContains('bob', $content['published_by']);
418 $this->assertContains('helen', $content['published_by']);
419 $this->assertTrue($content['is_public'], 'A public link has been generated for that entry');
422 public function testPostSameEntry()
424 $this->client
->request('POST', '/api/entries.json', [
425 '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',
427 'tags' => 'google, apple',
430 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
432 $content = json_decode($this->client
->getResponse()->getContent(), true);
434 $this->assertGreaterThan(0, $content['id']);
435 $this->assertSame('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']);
436 $this->assertSame(1, $content['is_archived']);
437 $this->assertSame(0, $content['is_starred']);
438 $this->assertCount(3, $content['tags']);
441 public function testPostEntryWhenFetchContentFails()
443 /** @var \Symfony\Component\DependencyInjection\Container $container */
444 $container = $this->client
->getContainer();
445 $contentProxy = $this->getMockBuilder(ContentProxy
::class)
446 ->disableOriginalConstructor()
447 ->setMethods(['updateEntry'])
449 $contentProxy->expects($this->any())
450 ->method('updateEntry')
451 ->willThrowException(new \
Exception('Test Fetch content fails'));
452 $container->set('wallabag_core.content_proxy', $contentProxy);
455 $this->client
->request('POST', '/api/entries.json', [
456 'url' => 'http://www.example.com/',
459 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
460 $content = json_decode($this->client
->getResponse()->getContent(), true);
461 $this->assertGreaterThan(0, $content['id']);
462 $this->assertSame('http://www.example.com/', $content['url']);
464 // Remove the created entry to avoid side effects on other tests
465 if (isset($content['id'])) {
466 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
467 $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']);
474 public function testPostArchivedAndStarredEntry()
476 $this->client
->request('POST', '/api/entries.json', [
477 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
482 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
484 $content = json_decode($this->client
->getResponse()->getContent(), true);
486 $this->assertGreaterThan(0, $content['id']);
487 $this->assertSame('http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']);
488 $this->assertSame(1, $content['is_archived']);
489 $this->assertSame(1, $content['is_starred']);
490 $this->assertSame(1, $content['user_id']);
493 public function testPostArchivedAndStarredEntryWithoutQuotes()
495 $this->client
->request('POST', '/api/entries.json', [
496 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
501 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
503 $content = json_decode($this->client
->getResponse()->getContent(), true);
505 $this->assertGreaterThan(0, $content['id']);
506 $this->assertSame('http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']);
507 $this->assertSame(0, $content['is_archived']);
508 $this->assertSame(1, $content['is_starred']);
511 public function testPatchEntry()
513 $entry = $this->client
->getContainer()
514 ->get('doctrine.orm.entity_manager')
515 ->getRepository('WallabagCoreBundle:Entry')
519 $this->markTestSkipped('No content found in db.');
522 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
523 'title' => 'New awesome title',
524 'tags' => 'new tag ' . uniqid(),
527 'language' => 'de_AT',
528 'preview_picture' => 'http://preview.io/picture.jpg',
529 'authors' => 'bob,sponge',
530 'content' => 'awesome',
532 'published_at' => 1488833381,
535 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
537 $content = json_decode($this->client
->getResponse()->getContent(), true);
539 $this->assertSame($entry->getId(), $content['id']);
540 $this->assertSame($entry->getUrl(), $content['url']);
541 $this->assertSame('New awesome title', $content['title']);
542 $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag');
543 $this->assertSame(1, $content['user_id']);
544 $this->assertSame('de_AT', $content['language']);
545 $this->assertSame('http://preview.io/picture.jpg', $content['preview_picture']);
546 $this->assertContains('sponge', $content['published_by']);
547 $this->assertContains('bob', $content['published_by']);
548 $this->assertSame('awesome', $content['content']);
549 $this->assertFalse($content['is_public'], 'Entry is no more shared');
550 $this->assertContains('2017-03-06', $content['published_at']);
553 public function testPatchEntryWithoutQuotes()
555 $entry = $this->client
->getContainer()
556 ->get('doctrine.orm.entity_manager')
557 ->getRepository('WallabagCoreBundle:Entry')
561 $this->markTestSkipped('No content found in db.');
564 $previousContent = $entry->getContent();
565 $previousLanguage = $entry->getLanguage();
567 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
568 'title' => 'New awesome title',
569 'tags' => 'new tag ' . uniqid(),
572 'authors' => ['bob', 'sponge'],
575 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
577 $content = json_decode($this->client
->getResponse()->getContent(), true);
579 $this->assertSame($entry->getId(), $content['id']);
580 $this->assertSame($entry->getUrl(), $content['url']);
581 $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag');
582 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
583 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
584 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
587 public function testGetTagsEntry()
589 $entry = $this->client
->getContainer()
590 ->get('doctrine.orm.entity_manager')
591 ->getRepository('WallabagCoreBundle:Entry')
592 ->findOneWithTags($this->user
->getId());
597 $this->markTestSkipped('No content found in db.');
601 foreach ($entry->getTags() as $tag) {
602 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
605 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '/tags');
607 $this->assertSame(json_encode($tags, JSON_HEX_QUOT
), $this->client
->getResponse()->getContent());
610 public function testPostTagsOnEntry()
612 $entry = $this->client
->getContainer()
613 ->get('doctrine.orm.entity_manager')
614 ->getRepository('WallabagCoreBundle:Entry')
618 $this->markTestSkipped('No content found in db.');
621 $nbTags = count($entry->getTags());
623 $newTags = 'tag1,tag2,tag3';
625 $this->client
->request('POST', '/api/entries/' . $entry->getId() . '/tags', ['tags' => $newTags]);
627 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
629 $content = json_decode($this->client
->getResponse()->getContent(), true);
631 $this->assertArrayHasKey('tags', $content);
632 $this->assertSame($nbTags +
3, count($content['tags']));
634 $entryDB = $this->client
->getContainer()
635 ->get('doctrine.orm.entity_manager')
636 ->getRepository('WallabagCoreBundle:Entry')
637 ->find($entry->getId());
640 foreach ($entryDB->getTags()->toArray() as $tag) {
641 $tagsInDB[$tag->getId()] = $tag->getLabel();
644 foreach (explode(',', $newTags) as $tag) {
645 $this->assertContains($tag, $tagsInDB);
649 public function testDeleteOneTagEntry()
651 $entry = $this->client
->getContainer()
652 ->get('doctrine.orm.entity_manager')
653 ->getRepository('WallabagCoreBundle:Entry')
654 ->findOneWithTags($this->user
->getId());
658 $this->markTestSkipped('No content found in db.');
661 // hydrate the tags relations
662 $nbTags = count($entry->getTags());
663 $tag = $entry->getTags()[0];
665 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '/tags/' . $tag->getId() . '.json');
667 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
669 $content = json_decode($this->client
->getResponse()->getContent(), true);
671 $this->assertArrayHasKey('tags', $content);
672 $this->assertSame($nbTags - 1, count($content['tags']));
675 public function testSaveIsArchivedAfterPost()
677 $entry = $this->client
->getContainer()
678 ->get('doctrine.orm.entity_manager')
679 ->getRepository('WallabagCoreBundle:Entry')
680 ->findOneBy(['user' => 1, 'isArchived' => true]);
683 $this->markTestSkipped('No content found in db.');
686 $this->client
->request('POST', '/api/entries.json', [
687 'url' => $entry->getUrl(),
690 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
692 $content = json_decode($this->client
->getResponse()->getContent(), true);
694 $this->assertSame(1, $content['is_archived']);
697 public function testSaveIsStarredAfterPost()
699 $entry = $this->client
->getContainer()
700 ->get('doctrine.orm.entity_manager')
701 ->getRepository('WallabagCoreBundle:Entry')
702 ->findOneBy(['user' => 1, 'isStarred' => true]);
705 $this->markTestSkipped('No content found in db.');
708 $this->client
->request('POST', '/api/entries.json', [
709 'url' => $entry->getUrl(),
712 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
714 $content = json_decode($this->client
->getResponse()->getContent(), true);
716 $this->assertSame(1, $content['is_starred']);
719 public function testSaveIsArchivedAfterPatch()
721 $entry = $this->client
->getContainer()
722 ->get('doctrine.orm.entity_manager')
723 ->getRepository('WallabagCoreBundle:Entry')
724 ->findOneBy(['user' => 1, 'isArchived' => true]);
727 $this->markTestSkipped('No content found in db.');
730 $previousTitle = $entry->getTitle();
732 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
733 'title' => $entry->getTitle() . '++',
736 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
738 $content = json_decode($this->client
->getResponse()->getContent(), true);
740 $this->assertSame(1, $content['is_archived']);
741 $this->assertSame($previousTitle . '++', $content['title']);
744 public function testSaveIsStarredAfterPatch()
746 $entry = $this->client
->getContainer()
747 ->get('doctrine.orm.entity_manager')
748 ->getRepository('WallabagCoreBundle:Entry')
749 ->findOneBy(['user' => 1, 'isStarred' => true]);
752 $this->markTestSkipped('No content found in db.');
754 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
755 'title' => $entry->getTitle() . '++',
758 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
760 $content = json_decode($this->client
->getResponse()->getContent(), true);
762 $this->assertSame(1, $content['is_starred']);
765 public function dataForEntriesExistWithUrl()
769 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1',
770 'expectedValue' => 2,
773 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2',
774 'expectedValue' => true,
780 * @dataProvider dataForEntriesExistWithUrl
782 public function testGetEntriesExists($url, $expectedValue)
784 $this->client
->request('GET', $url);
786 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
788 $content = json_decode($this->client
->getResponse()->getContent(), true);
790 $this->assertSame($expectedValue, $content['exists']);
793 public function testGetEntriesExistsWithManyUrls()
795 $url1 = 'http://0.0.0.0/entry2';
796 $url2 = 'http://0.0.0.0/entry10';
797 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1');
799 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
801 $content = json_decode($this->client
->getResponse()->getContent(), true);
803 $this->assertArrayHasKey($url1, $content);
804 $this->assertArrayHasKey($url2, $content);
805 $this->assertSame(2, $content[$url1]);
806 $this->assertNull($content[$url2]);
809 public function testGetEntriesExistsWithManyUrlsReturnBool()
811 $url1 = 'http://0.0.0.0/entry2';
812 $url2 = 'http://0.0.0.0/entry10';
813 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2);
815 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
817 $content = json_decode($this->client
->getResponse()->getContent(), true);
819 $this->assertArrayHasKey($url1, $content);
820 $this->assertArrayHasKey($url2, $content);
821 $this->assertTrue($content[$url1]);
822 $this->assertFalse($content[$url2]);
825 public function testGetEntriesExistsWhichDoesNotExists()
827 $this->client
->request('GET', '/api/entries/exists?url=http://google.com/entry2');
829 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
831 $content = json_decode($this->client
->getResponse()->getContent(), true);
833 $this->assertSame(false, $content['exists']);
836 public function testGetEntriesExistsWithNoUrl()
838 $this->client
->request('GET', '/api/entries/exists?url=');
840 $this->assertSame(403, $this->client
->getResponse()->getStatusCode());
843 public function testReloadEntryErrorWhileFetching()
845 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
846 ->getRepository('WallabagCoreBundle:Entry')
847 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
850 $this->markTestSkipped('No content found in db.');
853 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json');
854 $this->assertSame(304, $this->client
->getResponse()->getStatusCode());
857 public function testReloadEntry()
859 $this->client
->request('POST', '/api/entries.json', [
860 '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',
862 'tags' => 'google, apple',
865 $json = json_decode($this->client
->getResponse()->getContent(), true);
869 $this->client
->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json');
870 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
872 $content = json_decode($this->client
->getResponse()->getContent(), true);
874 $this->assertNotEmpty($content['title']);
876 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
879 public function testPostEntriesTagsListAction()
881 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
882 ->getRepository('WallabagCoreBundle:Entry')
883 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
885 $tags = $entry->getTags();
887 $this->assertCount(2, $tags);
891 'url' => 'http://0.0.0.0/entry4',
892 'tags' => 'new tag 1, new tag 2',
896 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode($list));
898 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
900 $content = json_decode($this->client
->getResponse()->getContent(), true);
902 $this->assertInternalType('int', $content[0]['entry']);
903 $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']);
905 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
906 ->getRepository('WallabagCoreBundle:Entry')
907 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
909 $tags = $entry->getTags();
910 $this->assertCount(4, $tags);
913 public function testPostEntriesTagsListActionNoList()
915 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode([]));
917 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
919 $content = json_decode($this->client
->getResponse()->getContent(), true);
921 $this->assertEmpty($content);
924 public function testDeleteEntriesTagsListAction()
926 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
927 $entry = new Entry($em->getReference(User
::class, 1));
928 $entry->setUrl('http://0.0.0.0/test-entry');
929 $entry->addTag((new Tag())->setLabel('foo-tag'));
930 $entry->addTag((new Tag())->setLabel('bar-tag'));
931 $em->persist($entry);
938 'url' => 'http://0.0.0.0/test-entry',
939 'tags' => 'foo-tag, bar-tag',
943 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list));
944 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
946 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
947 $this->assertCount(0, $entry->getTags());
950 public function testDeleteEntriesTagsListActionNoList()
952 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode([]));
954 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
956 $content = json_decode($this->client
->getResponse()->getContent(), true);
958 $this->assertEmpty($content);
961 public function testPostEntriesListAction()
964 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
965 'http://0.0.0.0/entry2',
968 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
970 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
972 $content = json_decode($this->client
->getResponse()->getContent(), true);
974 $this->assertInternalType('int', $content[0]['entry']);
975 $this->assertSame('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']);
977 $this->assertInternalType('int', $content[1]['entry']);
978 $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']);
981 public function testPostEntriesListActionWithNoUrls()
983 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode([]));
985 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
987 $content = json_decode($this->client
->getResponse()->getContent(), true);
989 $this->assertEmpty($content);
992 public function testDeleteEntriesListAction()
994 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
995 $em->persist((new Entry($em->getReference(User
::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
1000 'http://0.0.0.0/test-entry1',
1001 'http://0.0.0.0/test-entry-not-exist',
1004 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode($list));
1006 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1008 $content = json_decode($this->client
->getResponse()->getContent(), true);
1010 $this->assertTrue($content[0]['entry']);
1011 $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']);
1013 $this->assertFalse($content[1]['entry']);
1014 $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
1017 public function testDeleteEntriesListActionWithNoUrls()
1019 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode([]));
1021 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1023 $content = json_decode($this->client
->getResponse()->getContent(), true);
1025 $this->assertEmpty($content);
1028 public function testLimitBulkAction()
1031 'http://0.0.0.0/entry1',
1032 'http://0.0.0.0/entry1',
1033 'http://0.0.0.0/entry1',
1034 'http://0.0.0.0/entry1',
1035 'http://0.0.0.0/entry1',
1036 'http://0.0.0.0/entry1',
1037 'http://0.0.0.0/entry1',
1038 'http://0.0.0.0/entry1',
1039 'http://0.0.0.0/entry1',
1040 'http://0.0.0.0/entry1',
1041 'http://0.0.0.0/entry1',
1044 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1046 $this->assertSame(400, $this->client
->getResponse()->getStatusCode());
1047 $this->assertContains('API limit reached', $this->client
->getResponse()->getContent());