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->assertContains('foo', array_column($content['_embedded']['items'][0]['tags'], 'label'), 'Entries tags should have "foo" tag');
296 $this->assertContains('bar', array_column($content['_embedded']['items'][0]['tags'], 'label'), 'Entries tags should have "bar" tag');
298 $this->assertArrayHasKey('_links', $content);
299 $this->assertArrayHasKey('self', $content['_links']);
300 $this->assertArrayHasKey('first', $content['_links']);
301 $this->assertArrayHasKey('last', $content['_links']);
303 foreach (['self', 'first', 'last'] as $link) {
304 $this->assertArrayHasKey('href', $content['_links'][$link]);
305 $this->assertContains('tags=' . urlencode('foo,bar'), $content['_links'][$link]['href']);
308 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
311 public function testGetDatedEntries()
313 $this->client
->request('GET', '/api/entries', ['since' => 1443274283]);
315 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
317 $content = json_decode($this->client
->getResponse()->getContent(), true);
319 $this->assertGreaterThanOrEqual(1, count($content));
320 $this->assertNotEmpty($content['_embedded']['items']);
321 $this->assertGreaterThanOrEqual(1, $content['total']);
322 $this->assertSame(1, $content['page']);
323 $this->assertGreaterThanOrEqual(1, $content['pages']);
325 $this->assertArrayHasKey('_links', $content);
326 $this->assertArrayHasKey('self', $content['_links']);
327 $this->assertArrayHasKey('first', $content['_links']);
328 $this->assertArrayHasKey('last', $content['_links']);
330 foreach (['self', 'first', 'last'] as $link) {
331 $this->assertArrayHasKey('href', $content['_links'][$link]);
332 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
335 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
338 public function testGetDatedSupEntries()
340 $future = new \
DateTime(date('Y-m-d H:i:s'));
341 $this->client
->request('GET', '/api/entries', ['since' => $future->getTimestamp() +
1000]);
343 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
345 $content = json_decode($this->client
->getResponse()->getContent(), true);
347 $this->assertGreaterThanOrEqual(1, count($content));
348 $this->assertEmpty($content['_embedded']['items']);
349 $this->assertSame(0, $content['total']);
350 $this->assertSame(1, $content['page']);
351 $this->assertSame(1, $content['pages']);
353 $this->assertArrayHasKey('_links', $content);
354 $this->assertArrayHasKey('self', $content['_links']);
355 $this->assertArrayHasKey('first', $content['_links']);
356 $this->assertArrayHasKey('last', $content['_links']);
358 foreach (['self', 'first', 'last'] as $link) {
359 $this->assertArrayHasKey('href', $content['_links'][$link]);
360 $this->assertContains('since=' . ($future->getTimestamp() +
1000), $content['_links'][$link]['href']);
363 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
366 public function testDeleteEntry()
368 $entry = $this->client
->getContainer()
369 ->get('doctrine.orm.entity_manager')
370 ->getRepository('WallabagCoreBundle:Entry')
371 ->findOneByUser(1, ['id' => 'asc']);
374 $this->markTestSkipped('No content found in db.');
377 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '.json');
379 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
381 $content = json_decode($this->client
->getResponse()->getContent(), true);
383 $this->assertSame($entry->getTitle(), $content['title']);
384 $this->assertSame($entry->getUrl(), $content['url']);
386 // We'll try to delete this entry again
387 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '.json');
389 $this->assertSame(404, $this->client
->getResponse()->getStatusCode());
392 public function testPostEntry()
394 $this->client
->request('POST', '/api/entries.json', [
395 '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',
397 'title' => 'New title for my article',
398 'content' => 'my content',
400 'published_at' => '2016-09-08T11:55:58+0200',
401 'authors' => 'bob,helen',
405 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
407 $content = json_decode($this->client
->getResponse()->getContent(), true);
409 $this->assertGreaterThan(0, $content['id']);
410 $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']);
411 $this->assertSame(0, $content['is_archived']);
412 $this->assertSame(0, $content['is_starred']);
413 $this->assertNull($content['starred_at']);
414 $this->assertSame('New title for my article', $content['title']);
415 $this->assertSame(1, $content['user_id']);
416 $this->assertCount(2, $content['tags']);
417 $this->assertSame('my content', $content['content']);
418 $this->assertSame('de', $content['language']);
419 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
420 $this->assertCount(2, $content['published_by']);
421 $this->assertContains('bob', $content['published_by']);
422 $this->assertContains('helen', $content['published_by']);
423 $this->assertTrue($content['is_public'], 'A public link has been generated for that entry');
426 public function testPostSameEntry()
428 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
429 $entry = new Entry($em->getReference(User
::class, 1));
430 $entry->setUrl('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');
431 $entry->setArchived(true);
432 $entry->addTag((new Tag())->setLabel('google'));
433 $entry->addTag((new Tag())->setLabel('apple'));
434 $em->persist($entry);
438 $this->client
->request('POST', '/api/entries.json', [
439 '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',
441 'tags' => 'google, apple',
444 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
446 $content = json_decode($this->client
->getResponse()->getContent(), true);
448 $this->assertGreaterThan(0, $content['id']);
449 $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']);
450 $this->assertSame(1, $content['is_archived']);
451 $this->assertSame(0, $content['is_starred']);
452 $this->assertCount(3, $content['tags']);
455 public function testPostEntryWhenFetchContentFails()
457 /** @var \Symfony\Component\DependencyInjection\Container $container */
458 $container = $this->client
->getContainer();
459 $contentProxy = $this->getMockBuilder(ContentProxy
::class)
460 ->disableOriginalConstructor()
461 ->setMethods(['updateEntry'])
463 $contentProxy->expects($this->any())
464 ->method('updateEntry')
465 ->willThrowException(new \
Exception('Test Fetch content fails'));
466 $container->set('wallabag_core.content_proxy', $contentProxy);
469 $this->client
->request('POST', '/api/entries.json', [
470 'url' => 'http://www.example.com/',
473 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
474 $content = json_decode($this->client
->getResponse()->getContent(), true);
475 $this->assertGreaterThan(0, $content['id']);
476 $this->assertSame('http://www.example.com/', $content['url']);
478 // Remove the created entry to avoid side effects on other tests
479 if (isset($content['id'])) {
480 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
481 $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']);
488 public function testPostArchivedAndStarredEntry()
490 $now = new \
DateTime();
491 $this->client
->request('POST', '/api/entries.json', [
492 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
497 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
499 $content = json_decode($this->client
->getResponse()->getContent(), true);
501 $this->assertGreaterThan(0, $content['id']);
502 $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']);
503 $this->assertSame(1, $content['is_archived']);
504 $this->assertSame(1, $content['is_starred']);
505 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
506 $this->assertSame(1, $content['user_id']);
509 public function testPostArchivedAndStarredEntryWithoutQuotes()
511 $this->client
->request('POST', '/api/entries.json', [
512 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
517 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
519 $content = json_decode($this->client
->getResponse()->getContent(), true);
521 $this->assertGreaterThan(0, $content['id']);
522 $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']);
523 $this->assertSame(0, $content['is_archived']);
524 $this->assertSame(1, $content['is_starred']);
527 public function testPatchEntry()
529 $entry = $this->client
->getContainer()
530 ->get('doctrine.orm.entity_manager')
531 ->getRepository('WallabagCoreBundle:Entry')
535 $this->markTestSkipped('No content found in db.');
538 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
539 'title' => 'New awesome title',
540 'tags' => 'new tag ' . uniqid(),
543 'language' => 'de_AT',
544 'preview_picture' => 'http://preview.io/picture.jpg',
545 'authors' => 'bob,sponge',
546 'content' => 'awesome',
548 'published_at' => 1488833381,
551 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
553 $content = json_decode($this->client
->getResponse()->getContent(), true);
555 $this->assertSame($entry->getId(), $content['id']);
556 $this->assertSame($entry->getUrl(), $content['url']);
557 $this->assertSame('New awesome title', $content['title']);
558 $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag');
559 $this->assertSame(1, $content['user_id']);
560 $this->assertSame('de_AT', $content['language']);
561 $this->assertSame('http://preview.io/picture.jpg', $content['preview_picture']);
562 $this->assertContains('sponge', $content['published_by']);
563 $this->assertContains('bob', $content['published_by']);
564 $this->assertSame('awesome', $content['content']);
565 $this->assertFalse($content['is_public'], 'Entry is no more shared');
566 $this->assertContains('2017-03-06', $content['published_at']);
569 public function testPatchEntryWithoutQuotes()
571 $entry = $this->client
->getContainer()
572 ->get('doctrine.orm.entity_manager')
573 ->getRepository('WallabagCoreBundle:Entry')
577 $this->markTestSkipped('No content found in db.');
580 $previousContent = $entry->getContent();
581 $previousLanguage = $entry->getLanguage();
583 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
584 'title' => 'New awesome title',
585 'tags' => 'new tag ' . uniqid(),
588 'authors' => ['bob', 'sponge'],
591 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
593 $content = json_decode($this->client
->getResponse()->getContent(), true);
595 $this->assertSame($entry->getId(), $content['id']);
596 $this->assertSame($entry->getUrl(), $content['url']);
597 $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag');
598 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
599 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
600 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
603 public function testGetTagsEntry()
605 $entry = $this->client
->getContainer()
606 ->get('doctrine.orm.entity_manager')
607 ->getRepository('WallabagCoreBundle:Entry')
608 ->findOneWithTags($this->user
->getId());
613 $this->markTestSkipped('No content found in db.');
617 foreach ($entry->getTags() as $tag) {
618 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
621 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '/tags');
623 $this->assertSame(json_encode($tags, JSON_HEX_QUOT
), $this->client
->getResponse()->getContent());
626 public function testPostTagsOnEntry()
628 $entry = $this->client
->getContainer()
629 ->get('doctrine.orm.entity_manager')
630 ->getRepository('WallabagCoreBundle:Entry')
634 $this->markTestSkipped('No content found in db.');
637 $nbTags = count($entry->getTags());
639 $newTags = 'tag1,tag2,tag3';
641 $this->client
->request('POST', '/api/entries/' . $entry->getId() . '/tags', ['tags' => $newTags]);
643 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
645 $content = json_decode($this->client
->getResponse()->getContent(), true);
647 $this->assertArrayHasKey('tags', $content);
648 $this->assertSame($nbTags +
3, count($content['tags']));
650 $entryDB = $this->client
->getContainer()
651 ->get('doctrine.orm.entity_manager')
652 ->getRepository('WallabagCoreBundle:Entry')
653 ->find($entry->getId());
656 foreach ($entryDB->getTags()->toArray() as $tag) {
657 $tagsInDB[$tag->getId()] = $tag->getLabel();
660 foreach (explode(',', $newTags) as $tag) {
661 $this->assertContains($tag, $tagsInDB);
665 public function testDeleteOneTagEntry()
667 $entry = $this->client
->getContainer()
668 ->get('doctrine.orm.entity_manager')
669 ->getRepository('WallabagCoreBundle:Entry')
670 ->findOneWithTags($this->user
->getId());
674 $this->markTestSkipped('No content found in db.');
677 // hydrate the tags relations
678 $nbTags = count($entry->getTags());
679 $tag = $entry->getTags()[0];
681 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '/tags/' . $tag->getId() . '.json');
683 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
685 $content = json_decode($this->client
->getResponse()->getContent(), true);
687 $this->assertArrayHasKey('tags', $content);
688 $this->assertSame($nbTags - 1, count($content['tags']));
691 public function testSaveIsArchivedAfterPost()
693 $entry = $this->client
->getContainer()
694 ->get('doctrine.orm.entity_manager')
695 ->getRepository('WallabagCoreBundle:Entry')
696 ->findOneBy(['user' => 1, 'isArchived' => true]);
699 $this->markTestSkipped('No content found in db.');
702 $this->client
->request('POST', '/api/entries.json', [
703 'url' => $entry->getUrl(),
706 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
708 $content = json_decode($this->client
->getResponse()->getContent(), true);
710 $this->assertSame(1, $content['is_archived']);
713 public function testSaveIsStarredAfterPost()
715 $entry = $this->client
->getContainer()
716 ->get('doctrine.orm.entity_manager')
717 ->getRepository('WallabagCoreBundle:Entry')
718 ->findOneBy(['user' => 1, 'isStarred' => true]);
721 $this->markTestSkipped('No content found in db.');
724 $this->client
->request('POST', '/api/entries.json', [
725 'url' => $entry->getUrl(),
728 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
730 $content = json_decode($this->client
->getResponse()->getContent(), true);
732 $this->assertSame(1, $content['is_starred']);
735 public function testSaveIsArchivedAfterPatch()
737 $entry = $this->client
->getContainer()
738 ->get('doctrine.orm.entity_manager')
739 ->getRepository('WallabagCoreBundle:Entry')
740 ->findOneBy(['user' => 1, 'isArchived' => true]);
743 $this->markTestSkipped('No content found in db.');
746 $previousTitle = $entry->getTitle();
748 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
749 'title' => $entry->getTitle() . '++',
752 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
754 $content = json_decode($this->client
->getResponse()->getContent(), true);
756 $this->assertSame(1, $content['is_archived']);
757 $this->assertSame($previousTitle . '++', $content['title']);
760 public function testSaveIsStarredAfterPatch()
762 $now = new \
DateTime();
763 $entry = $this->client
->getContainer()
764 ->get('doctrine.orm.entity_manager')
765 ->getRepository('WallabagCoreBundle:Entry')
766 ->findOneBy(['user' => 1, 'isStarred' => true]);
769 $this->markTestSkipped('No content found in db.');
771 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
772 'title' => $entry->getTitle() . '++',
775 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
777 $content = json_decode($this->client
->getResponse()->getContent(), true);
779 $this->assertSame(1, $content['is_starred']);
780 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
783 public function dataForEntriesExistWithUrl()
787 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1',
788 'expectedValue' => 2,
791 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2',
792 'expectedValue' => true,
798 * @dataProvider dataForEntriesExistWithUrl
800 public function testGetEntriesExists($url, $expectedValue)
802 $this->client
->request('GET', $url);
804 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
806 $content = json_decode($this->client
->getResponse()->getContent(), true);
808 $this->assertSame($expectedValue, $content['exists']);
811 public function testGetEntriesExistsWithManyUrls()
813 $url1 = 'http://0.0.0.0/entry2';
814 $url2 = 'http://0.0.0.0/entry10';
815 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1');
817 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
819 $content = json_decode($this->client
->getResponse()->getContent(), true);
821 $this->assertArrayHasKey($url1, $content);
822 $this->assertArrayHasKey($url2, $content);
823 $this->assertSame(2, $content[$url1]);
824 $this->assertNull($content[$url2]);
827 public function testGetEntriesExistsWithManyUrlsReturnBool()
829 $url1 = 'http://0.0.0.0/entry2';
830 $url2 = 'http://0.0.0.0/entry10';
831 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2);
833 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
835 $content = json_decode($this->client
->getResponse()->getContent(), true);
837 $this->assertArrayHasKey($url1, $content);
838 $this->assertArrayHasKey($url2, $content);
839 $this->assertTrue($content[$url1]);
840 $this->assertFalse($content[$url2]);
843 public function testGetEntriesExistsWhichDoesNotExists()
845 $this->client
->request('GET', '/api/entries/exists?url=http://google.com/entry2');
847 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
849 $content = json_decode($this->client
->getResponse()->getContent(), true);
851 $this->assertSame(false, $content['exists']);
854 public function testGetEntriesExistsWithNoUrl()
856 $this->client
->request('GET', '/api/entries/exists?url=');
858 $this->assertSame(403, $this->client
->getResponse()->getStatusCode());
861 public function testReloadEntryErrorWhileFetching()
863 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
864 ->getRepository('WallabagCoreBundle:Entry')
865 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
868 $this->markTestSkipped('No content found in db.');
871 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json');
872 $this->assertSame(304, $this->client
->getResponse()->getStatusCode());
875 public function testReloadEntry()
877 $this->client
->request('POST', '/api/entries.json', [
878 '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',
880 'tags' => 'google, apple',
883 $json = json_decode($this->client
->getResponse()->getContent(), true);
887 $this->client
->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json');
888 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
890 $content = json_decode($this->client
->getResponse()->getContent(), true);
892 $this->assertNotEmpty($content['title']);
894 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
897 public function testPostEntriesTagsListAction()
899 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
900 ->getRepository('WallabagCoreBundle:Entry')
901 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
903 $tags = $entry->getTags();
905 $this->assertCount(2, $tags);
909 'url' => 'http://0.0.0.0/entry4',
910 'tags' => 'new tag 1, new tag 2',
914 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode($list));
916 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
918 $content = json_decode($this->client
->getResponse()->getContent(), true);
920 $this->assertInternalType('int', $content[0]['entry']);
921 $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']);
923 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
924 ->getRepository('WallabagCoreBundle:Entry')
925 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
927 $tags = $entry->getTags();
928 $this->assertCount(4, $tags);
931 public function testPostEntriesTagsListActionNoList()
933 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode([]));
935 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
937 $content = json_decode($this->client
->getResponse()->getContent(), true);
939 $this->assertEmpty($content);
942 public function testDeleteEntriesTagsListAction()
944 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
945 $entry = new Entry($em->getReference(User
::class, 1));
946 $entry->setUrl('http://0.0.0.0/test-entry');
947 $entry->addTag((new Tag())->setLabel('foo-tag'));
948 $entry->addTag((new Tag())->setLabel('bar-tag'));
949 $em->persist($entry);
956 'url' => 'http://0.0.0.0/test-entry',
957 'tags' => 'foo-tag, bar-tag',
961 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list));
962 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
964 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
965 $this->assertCount(0, $entry->getTags());
968 public function testDeleteEntriesTagsListActionNoList()
970 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode([]));
972 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
974 $content = json_decode($this->client
->getResponse()->getContent(), true);
976 $this->assertEmpty($content);
979 public function testPostEntriesListAction()
982 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
983 'http://0.0.0.0/entry2',
986 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
988 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
990 $content = json_decode($this->client
->getResponse()->getContent(), true);
992 $this->assertInternalType('int', $content[0]['entry']);
993 $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']);
995 $this->assertInternalType('int', $content[1]['entry']);
996 $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']);
999 public function testPostEntriesListActionWithNoUrls()
1001 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode([]));
1003 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1005 $content = json_decode($this->client
->getResponse()->getContent(), true);
1007 $this->assertEmpty($content);
1010 public function testDeleteEntriesListAction()
1012 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1013 $em->persist((new Entry($em->getReference(User
::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
1018 'http://0.0.0.0/test-entry1',
1019 'http://0.0.0.0/test-entry-not-exist',
1022 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode($list));
1024 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1026 $content = json_decode($this->client
->getResponse()->getContent(), true);
1028 $this->assertTrue($content[0]['entry']);
1029 $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']);
1031 $this->assertFalse($content[1]['entry']);
1032 $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
1035 public function testDeleteEntriesListActionWithNoUrls()
1037 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode([]));
1039 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1041 $content = json_decode($this->client
->getResponse()->getContent(), true);
1043 $this->assertEmpty($content);
1046 public function testLimitBulkAction()
1049 'http://0.0.0.0/entry1',
1050 'http://0.0.0.0/entry1',
1051 'http://0.0.0.0/entry1',
1052 'http://0.0.0.0/entry1',
1053 'http://0.0.0.0/entry1',
1054 'http://0.0.0.0/entry1',
1055 'http://0.0.0.0/entry1',
1056 'http://0.0.0.0/entry1',
1057 'http://0.0.0.0/entry1',
1058 'http://0.0.0.0/entry1',
1059 'http://0.0.0.0/entry1',
1062 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1064 $this->assertSame(400, $this->client
->getResponse()->getStatusCode());
1065 $this->assertContains('API limit reached', $this->client
->getResponse()->getContent());
1068 public function testRePostEntryAndReUsePublishedAt()
1070 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1071 $entry = new Entry($em->getReference(User
::class, 1));
1072 $entry->setTitle('Antoine de Caunes : « Je veux avoir le droit de tâtonner »');
1073 $entry->setContent('hihi');
1074 $entry->setUrl('http://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html');
1075 $entry->setPublishedAt(new \
DateTime('2017-06-26T07:46:02+0200'));
1076 $em->persist($entry);
1080 $this->client
->request('POST', '/api/entries.json', [
1081 'url' => 'http://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html',
1084 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1086 $content = json_decode($this->client
->getResponse()->getContent(), true);
1088 $this->assertGreaterThan(0, $content['id']);
1089 $this->assertSame('http://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html', $content['url']);