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 testGetOneEntryWithOriginUrl()
41 $entry = $this->client
->getContainer()
42 ->get('doctrine.orm.entity_manager')
43 ->getRepository('WallabagCoreBundle:Entry')
44 ->findOneBy(['user' => 1, 'url' => 'http://0.0.0.0/entry2']);
47 $this->markTestSkipped('No content found in db.');
50 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '.json');
51 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
53 $content = json_decode($this->client
->getResponse()->getContent(), true);
55 $this->assertSame($entry->getOriginUrl(), $content['origin_url']);
58 public function testExportEntry()
60 $entry = $this->client
->getContainer()
61 ->get('doctrine.orm.entity_manager')
62 ->getRepository('WallabagCoreBundle:Entry')
63 ->findOneBy(['user' => 1, 'isArchived' => false]);
66 $this->markTestSkipped('No content found in db.');
69 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '/export.epub');
70 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
72 // epub format got the content type in the content
73 $this->assertContains('application/epub', $this->client
->getResponse()->getContent());
74 $this->assertSame('application/epub+zip', $this->client
->getResponse()->headers
->get('Content-Type'));
76 // re-auth client for mobi
77 $client = $this->createAuthorizedClient();
78 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.mobi');
79 $this->assertSame(200, $client->getResponse()->getStatusCode());
81 $this->assertSame('application/x-mobipocket-ebook', $client->getResponse()->headers
->get('Content-Type'));
83 // re-auth client for pdf
84 $client = $this->createAuthorizedClient();
85 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.pdf');
86 $this->assertSame(200, $client->getResponse()->getStatusCode());
88 $this->assertContains('PDF-', $client->getResponse()->getContent());
89 $this->assertSame('application/pdf', $client->getResponse()->headers
->get('Content-Type'));
91 // re-auth client for pdf
92 $client = $this->createAuthorizedClient();
93 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.txt');
94 $this->assertSame(200, $client->getResponse()->getStatusCode());
96 $this->assertContains('text/plain', $client->getResponse()->headers
->get('Content-Type'));
98 // re-auth client for pdf
99 $client = $this->createAuthorizedClient();
100 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.csv');
101 $this->assertSame(200, $client->getResponse()->getStatusCode());
103 $this->assertContains('application/csv', $client->getResponse()->headers
->get('Content-Type'));
106 public function testGetOneEntryWrongUser()
108 $entry = $this->client
->getContainer()
109 ->get('doctrine.orm.entity_manager')
110 ->getRepository('WallabagCoreBundle:Entry')
111 ->findOneBy(['user' => 2, 'isArchived' => false]);
114 $this->markTestSkipped('No content found in db.');
117 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '.json');
119 $this->assertSame(403, $this->client
->getResponse()->getStatusCode());
122 public function testGetEntries()
124 $this->client
->request('GET', '/api/entries');
126 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
128 $content = json_decode($this->client
->getResponse()->getContent(), true);
130 $this->assertGreaterThanOrEqual(1, \
count($content));
131 $this->assertNotEmpty($content['_embedded']['items']);
132 $this->assertGreaterThanOrEqual(1, $content['total']);
133 $this->assertSame(1, $content['page']);
134 $this->assertGreaterThanOrEqual(1, $content['pages']);
136 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
139 public function testGetEntriesWithFullOptions()
141 $this->client
->request('GET', '/api/entries', [
149 'since' => 1443274283,
153 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
155 $content = json_decode($this->client
->getResponse()->getContent(), true);
157 $this->assertGreaterThanOrEqual(1, \
count($content));
158 $this->assertArrayHasKey('items', $content['_embedded']);
159 $this->assertGreaterThanOrEqual(0, $content['total']);
160 $this->assertSame(1, $content['page']);
161 $this->assertSame(2, $content['limit']);
162 $this->assertGreaterThanOrEqual(1, $content['pages']);
164 $this->assertArrayHasKey('_links', $content);
165 $this->assertArrayHasKey('self', $content['_links']);
166 $this->assertArrayHasKey('first', $content['_links']);
167 $this->assertArrayHasKey('last', $content['_links']);
169 foreach (['self', 'first', 'last'] as $link) {
170 $this->assertArrayHasKey('href', $content['_links'][$link]);
171 $this->assertContains('archive=1', $content['_links'][$link]['href']);
172 $this->assertContains('starred=1', $content['_links'][$link]['href']);
173 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
174 $this->assertContains('order=asc', $content['_links'][$link]['href']);
175 $this->assertContains('tags=foo', $content['_links'][$link]['href']);
176 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
177 $this->assertContains('public=0', $content['_links'][$link]['href']);
180 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
183 public function testGetEntriesPublicOnly()
185 $entry = $this->client
->getContainer()
186 ->get('doctrine.orm.entity_manager')
187 ->getRepository('WallabagCoreBundle:Entry')
191 $this->markTestSkipped('No content found in db.');
194 // generate at least one public entry
195 $entry->generateUid();
197 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
198 $em->persist($entry);
201 $this->client
->request('GET', '/api/entries', [
205 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
207 $content = json_decode($this->client
->getResponse()->getContent(), true);
209 $this->assertGreaterThanOrEqual(1, \
count($content));
210 $this->assertArrayHasKey('items', $content['_embedded']);
211 $this->assertGreaterThanOrEqual(1, $content['total']);
212 $this->assertSame(1, $content['page']);
213 $this->assertSame(30, $content['limit']);
214 $this->assertGreaterThanOrEqual(1, $content['pages']);
216 $this->assertArrayHasKey('_links', $content);
217 $this->assertArrayHasKey('self', $content['_links']);
218 $this->assertArrayHasKey('first', $content['_links']);
219 $this->assertArrayHasKey('last', $content['_links']);
221 foreach (['self', 'first', 'last'] as $link) {
222 $this->assertArrayHasKey('href', $content['_links'][$link]);
223 $this->assertContains('public=1', $content['_links'][$link]['href']);
226 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
229 public function testGetEntriesOnPageTwo()
231 $this->client
->request('GET', '/api/entries', [
236 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
238 $content = json_decode($this->client
->getResponse()->getContent(), true);
240 $this->assertGreaterThanOrEqual(0, $content['total']);
241 $this->assertSame(2, $content['page']);
242 $this->assertSame(2, $content['limit']);
245 public function testGetStarredEntriesWithBadSort()
247 $this->client
->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated', 'order' => 'unknown']);
249 $this->assertSame(400, $this->client
->getResponse()->getStatusCode());
251 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
254 public function testGetStarredEntries()
256 $this->client
->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']);
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('starred=1', $content['_links'][$link]['href']);
276 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
279 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
282 public function testGetArchiveEntries()
284 $this->client
->request('GET', '/api/entries', ['archive' => 1]);
286 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
288 $content = json_decode($this->client
->getResponse()->getContent(), true);
290 $this->assertGreaterThanOrEqual(1, \
count($content));
291 $this->assertNotEmpty($content['_embedded']['items']);
292 $this->assertGreaterThanOrEqual(1, $content['total']);
293 $this->assertSame(1, $content['page']);
294 $this->assertGreaterThanOrEqual(1, $content['pages']);
296 $this->assertArrayHasKey('_links', $content);
297 $this->assertArrayHasKey('self', $content['_links']);
298 $this->assertArrayHasKey('first', $content['_links']);
299 $this->assertArrayHasKey('last', $content['_links']);
301 foreach (['self', 'first', 'last'] as $link) {
302 $this->assertArrayHasKey('href', $content['_links'][$link]);
303 $this->assertContains('archive=1', $content['_links'][$link]['href']);
306 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
309 public function testGetTaggedEntries()
311 $this->client
->request('GET', '/api/entries', ['tags' => 'foo,bar']);
313 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
315 $content = json_decode($this->client
->getResponse()->getContent(), true);
317 $this->assertGreaterThanOrEqual(1, \
count($content));
318 $this->assertNotEmpty($content['_embedded']['items']);
319 $this->assertGreaterThanOrEqual(1, $content['total']);
320 $this->assertSame(1, $content['page']);
321 $this->assertGreaterThanOrEqual(1, $content['pages']);
323 $this->assertContains('foo', array_column($content['_embedded']['items'][0]['tags'], 'label'), 'Entries tags should have "foo" tag');
324 $this->assertContains('bar', array_column($content['_embedded']['items'][0]['tags'], 'label'), 'Entries tags should have "bar" tag');
326 $this->assertArrayHasKey('_links', $content);
327 $this->assertArrayHasKey('self', $content['_links']);
328 $this->assertArrayHasKey('first', $content['_links']);
329 $this->assertArrayHasKey('last', $content['_links']);
331 foreach (['self', 'first', 'last'] as $link) {
332 $this->assertArrayHasKey('href', $content['_links'][$link]);
333 $this->assertContains('tags=' . urlencode('foo,bar'), $content['_links'][$link]['href']);
336 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
339 public function testGetTaggedEntriesWithBadParams()
341 $this->client
->request('GET', '/api/entries', ['tags' => ['foo', 'bar']]);
343 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
346 public function testGetDatedEntries()
348 $this->client
->request('GET', '/api/entries', ['since' => 1443274283]);
350 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
352 $content = json_decode($this->client
->getResponse()->getContent(), true);
354 $this->assertGreaterThanOrEqual(1, \
count($content));
355 $this->assertNotEmpty($content['_embedded']['items']);
356 $this->assertGreaterThanOrEqual(1, $content['total']);
357 $this->assertSame(1, $content['page']);
358 $this->assertGreaterThanOrEqual(1, $content['pages']);
360 $this->assertArrayHasKey('_links', $content);
361 $this->assertArrayHasKey('self', $content['_links']);
362 $this->assertArrayHasKey('first', $content['_links']);
363 $this->assertArrayHasKey('last', $content['_links']);
365 foreach (['self', 'first', 'last'] as $link) {
366 $this->assertArrayHasKey('href', $content['_links'][$link]);
367 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
370 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
373 public function testGetDatedSupEntries()
375 $future = new \
DateTime(date('Y-m-d H:i:s'));
376 $this->client
->request('GET', '/api/entries', ['since' => $future->getTimestamp() +
1000]);
378 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
380 $content = json_decode($this->client
->getResponse()->getContent(), true);
382 $this->assertGreaterThanOrEqual(1, \
count($content));
383 $this->assertEmpty($content['_embedded']['items']);
384 $this->assertSame(0, $content['total']);
385 $this->assertSame(1, $content['page']);
386 $this->assertSame(1, $content['pages']);
388 $this->assertArrayHasKey('_links', $content);
389 $this->assertArrayHasKey('self', $content['_links']);
390 $this->assertArrayHasKey('first', $content['_links']);
391 $this->assertArrayHasKey('last', $content['_links']);
393 foreach (['self', 'first', 'last'] as $link) {
394 $this->assertArrayHasKey('href', $content['_links'][$link]);
395 $this->assertContains('since=' . ($future->getTimestamp() +
1000), $content['_links'][$link]['href']);
398 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
401 public function testDeleteEntry()
403 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
404 $entry = new Entry($em->getReference(User
::class, 1));
405 $entry->setUrl('http://0.0.0.0/test-delete-entry');
406 $entry->setTitle('Test delete entry');
407 $em->persist($entry);
413 'title' => $entry->getTitle(),
414 'url' => $entry->getUrl(),
415 'id' => $entry->getId(),
418 $this->client
->request('DELETE', '/api/entries/' . $e['id'] . '.json');
420 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
422 $content = json_decode($this->client
->getResponse()->getContent(), true);
424 $this->assertSame($e['title'], $content['title']);
425 $this->assertSame($e['url'], $content['url']);
426 $this->assertSame($e['id'], $content['id']);
428 // We'll try to delete this entry again
429 $client = $this->createAuthorizedClient();
430 $client->request('DELETE', '/api/entries/' . $e['id'] . '.json');
432 $this->assertSame(404, $client->getResponse()->getStatusCode());
435 public function testDeleteEntryExpectId()
437 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
438 $entry = new Entry($em->getReference(User
::class, 1));
439 $entry->setUrl('http://0.0.0.0/test-delete-entry-id');
440 $em->persist($entry);
445 $id = $entry->getId();
447 $this->client
->request('DELETE', '/api/entries/' . $id . '.json?expect=id');
449 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
451 $content = json_decode($this->client
->getResponse()->getContent(), true);
453 $this->assertSame($id, $content['id']);
454 $this->assertArrayNotHasKey('url', $content);
456 // We'll try to delete this entry again
457 $client = $this->createAuthorizedClient();
458 $client->request('DELETE', '/api/entries/' . $id . '.json');
460 $this->assertSame(404, $client->getResponse()->getStatusCode());
463 public function testDeleteEntryExpectBadRequest()
465 $this->client
->request('DELETE', '/api/entries/1.json?expect=badrequest');
467 $this->assertSame(400, $this->client
->getResponse()->getStatusCode());
470 public function testPostEntry()
472 $this->client
->request('POST', '/api/entries.json', [
473 'url' => 'https://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',
475 'title' => 'New title for my article',
476 'content' => 'my content',
478 'published_at' => '2016-09-08T11:55:58+0200',
479 'authors' => 'bob,helen',
483 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
485 $content = json_decode($this->client
->getResponse()->getContent(), true);
487 $this->assertGreaterThan(0, $content['id']);
488 $this->assertSame('https://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']);
489 $this->assertSame(0, $content['is_archived']);
490 $this->assertSame(0, $content['is_starred']);
491 $this->assertNull($content['starred_at']);
492 $this->assertSame('New title for my article', $content['title']);
493 $this->assertSame(1, $content['user_id']);
494 $this->assertCount(2, $content['tags']);
495 $this->assertNull($content['origin_url']);
496 $this->assertSame('my content', $content['content']);
497 $this->assertSame('de', $content['language']);
498 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
499 $this->assertCount(2, $content['published_by']);
500 $this->assertContains('bob', $content['published_by']);
501 $this->assertContains('helen', $content['published_by']);
502 $this->assertTrue($content['is_public'], 'A public link has been generated for that entry');
505 public function testPostSameEntry()
507 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
508 $entry = new Entry($em->getReference(User
::class, 1));
509 $entry->setUrl('https://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');
510 $entry->setArchived(true);
511 $entry->addTag((new Tag())->setLabel('google'));
512 $entry->addTag((new Tag())->setLabel('apple'));
513 $em->persist($entry);
517 $this->client
->request('POST', '/api/entries.json', [
518 'url' => 'https://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',
520 'tags' => 'google, apple',
523 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
525 $content = json_decode($this->client
->getResponse()->getContent(), true);
527 $this->assertGreaterThan(0, $content['id']);
528 $this->assertSame('https://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']);
529 $this->assertSame(1, $content['is_archived']);
530 $this->assertSame(0, $content['is_starred']);
531 $this->assertCount(3, $content['tags']);
534 public function testPostEntryWhenFetchContentFails()
536 /** @var \Symfony\Component\DependencyInjection\Container $container */
537 $container = $this->client
->getContainer();
538 $contentProxy = $this->getMockBuilder(ContentProxy
::class)
539 ->disableOriginalConstructor()
540 ->setMethods(['updateEntry'])
542 $contentProxy->expects($this->any())
543 ->method('updateEntry')
544 ->willThrowException(new \
Exception('Test Fetch content fails'));
545 $container->set('wallabag_core.content_proxy', $contentProxy);
548 $this->client
->request('POST', '/api/entries.json', [
549 'url' => 'http://www.example.com/',
552 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
553 $content = json_decode($this->client
->getResponse()->getContent(), true);
554 $this->assertGreaterThan(0, $content['id']);
555 $this->assertSame('http://www.example.com/', $content['url']);
556 $this->assertSame('www.example.com', $content['domain_name']);
557 $this->assertSame('www.example.com', $content['title']);
559 // Remove the created entry to avoid side effects on other tests
560 if (isset($content['id'])) {
561 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
562 $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']);
569 public function testPostArchivedAndStarredEntry()
571 $now = new \
DateTime();
572 $this->client
->request('POST', '/api/entries.json', [
573 'url' => 'https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
578 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
580 $content = json_decode($this->client
->getResponse()->getContent(), true);
582 $this->assertGreaterThan(0, $content['id']);
583 $this->assertSame('https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']);
584 $this->assertSame(1, $content['is_archived']);
585 $this->assertSame(1, $content['is_starred']);
586 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
587 $this->assertSame(1, $content['user_id']);
590 public function testPostArchivedAndStarredEntryWithoutQuotes()
592 $this->client
->request('POST', '/api/entries.json', [
593 'url' => 'https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
598 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
600 $content = json_decode($this->client
->getResponse()->getContent(), true);
602 $this->assertGreaterThan(0, $content['id']);
603 $this->assertSame('https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']);
604 $this->assertSame(0, $content['is_archived']);
605 $this->assertSame(1, $content['is_starred']);
608 public function testPostEntryWithOriginUrl()
610 $this->client
->request('POST', '/api/entries.json', [
611 'url' => 'https://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',
613 'title' => 'New title for my article',
614 'content' => 'my content',
616 'published_at' => '2016-09-08T11:55:58+0200',
617 'authors' => 'bob,helen',
619 'origin_url' => 'http://mysource.tld',
622 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
624 $content = json_decode($this->client
->getResponse()->getContent(), true);
626 $this->assertGreaterThan(0, $content['id']);
627 $this->assertSame('https://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']);
628 $this->assertSame('http://mysource.tld', $content['origin_url']);
631 public function testPatchEntry()
633 $entry = $this->client
->getContainer()
634 ->get('doctrine.orm.entity_manager')
635 ->getRepository('WallabagCoreBundle:Entry')
639 $this->markTestSkipped('No content found in db.');
642 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
643 'title' => 'New awesome title',
644 'tags' => 'new tag ' . uniqid(),
647 'language' => 'de_AT',
648 'preview_picture' => 'http://preview.io/picture.jpg',
649 'authors' => 'bob,sponge',
650 'content' => 'awesome',
652 'published_at' => 1488833381,
655 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
657 $content = json_decode($this->client
->getResponse()->getContent(), true);
659 $this->assertSame($entry->getId(), $content['id']);
660 $this->assertSame($entry->getUrl(), $content['url']);
661 $this->assertSame('New awesome title', $content['title']);
662 $this->assertGreaterThanOrEqual(1, \
count($content['tags']), 'We force only one tag');
663 $this->assertSame(1, $content['user_id']);
664 $this->assertSame('de_AT', $content['language']);
665 $this->assertSame('http://preview.io/picture.jpg', $content['preview_picture']);
666 $this->assertContains('sponge', $content['published_by']);
667 $this->assertContains('bob', $content['published_by']);
668 $this->assertSame('awesome', $content['content']);
669 $this->assertFalse($content['is_public'], 'Entry is no more shared');
670 $this->assertContains('2017-03-06', $content['published_at']);
673 public function testPatchEntryWithoutQuotes()
675 $entry = $this->client
->getContainer()
676 ->get('doctrine.orm.entity_manager')
677 ->getRepository('WallabagCoreBundle:Entry')
681 $this->markTestSkipped('No content found in db.');
684 $previousContent = $entry->getContent();
685 $previousLanguage = $entry->getLanguage();
687 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
688 'title' => 'New awesome title',
689 'tags' => 'new tag ' . uniqid(),
692 'authors' => ['bob', 'sponge'],
695 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
697 $content = json_decode($this->client
->getResponse()->getContent(), true);
699 $this->assertSame($entry->getId(), $content['id']);
700 $this->assertSame($entry->getUrl(), $content['url']);
701 $this->assertGreaterThanOrEqual(1, \
count($content['tags']), 'We force only one tag');
702 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
703 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
704 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
707 public function testPatchEntryWithOriginUrl()
709 $entry = $this->client
->getContainer()
710 ->get('doctrine.orm.entity_manager')
711 ->getRepository('WallabagCoreBundle:Entry')
715 $this->markTestSkipped('No content found in db.');
718 $previousContent = $entry->getContent();
719 $previousLanguage = $entry->getLanguage();
721 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
722 'title' => 'Another awesome title just for profit',
723 'origin_url' => 'https://myawesomesource.example.com',
726 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
728 $content = json_decode($this->client
->getResponse()->getContent(), true);
730 $this->assertSame($entry->getId(), $content['id']);
731 $this->assertSame($entry->getUrl(), $content['url']);
732 $this->assertSame('https://myawesomesource.example.com', $content['origin_url']);
733 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
734 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
735 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
738 public function testPatchEntryRemoveOriginUrl()
740 $entry = $this->client
->getContainer()
741 ->get('doctrine.orm.entity_manager')
742 ->getRepository('WallabagCoreBundle:Entry')
746 $this->markTestSkipped('No content found in db.');
749 $previousContent = $entry->getContent();
750 $previousLanguage = $entry->getLanguage();
751 $previousTitle = $entry->getTitle();
753 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
757 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
759 $content = json_decode($this->client
->getResponse()->getContent(), true);
761 $this->assertSame($entry->getId(), $content['id']);
762 $this->assertSame($entry->getUrl(), $content['url']);
763 $this->assertEmpty($content['origin_url']);
764 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
765 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
766 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
767 $this->assertSame($previousTitle, $content['title'], 'Ensure title has not moved');
770 public function testPatchEntryNullOriginUrl()
772 $entry = $this->client
->getContainer()
773 ->get('doctrine.orm.entity_manager')
774 ->getRepository('WallabagCoreBundle:Entry')
778 $this->markTestSkipped('No content found in db.');
781 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
782 'origin_url' => null,
785 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
787 $content = json_decode($this->client
->getResponse()->getContent(), true);
789 $this->assertNull($content['origin_url']);
792 public function testGetTagsEntry()
794 $entry = $this->client
->getContainer()
795 ->get('doctrine.orm.entity_manager')
796 ->getRepository('WallabagCoreBundle:Entry')
797 ->findOneWithTags($this->user
->getId());
802 $this->markTestSkipped('No content found in db.');
806 foreach ($entry->getTags() as $tag) {
807 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
810 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '/tags');
812 $this->assertSame(json_encode($tags, JSON_HEX_QUOT
), $this->client
->getResponse()->getContent());
815 public function testPostTagsOnEntry()
817 $entry = $this->client
->getContainer()
818 ->get('doctrine.orm.entity_manager')
819 ->getRepository('WallabagCoreBundle:Entry')
823 $this->markTestSkipped('No content found in db.');
826 $nbTags = \
count($entry->getTags());
828 $newTags = 'tag1,tag2,tag3';
830 $this->client
->request('POST', '/api/entries/' . $entry->getId() . '/tags', ['tags' => $newTags]);
832 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
834 $content = json_decode($this->client
->getResponse()->getContent(), true);
836 $this->assertArrayHasKey('tags', $content);
837 $this->assertSame($nbTags +
3, \
count($content['tags']));
839 $entryDB = $this->client
->getContainer()
840 ->get('doctrine.orm.entity_manager')
841 ->getRepository('WallabagCoreBundle:Entry')
842 ->find($entry->getId());
845 foreach ($entryDB->getTags()->toArray() as $tag) {
846 $tagsInDB[$tag->getId()] = $tag->getLabel();
849 foreach (explode(',', $newTags) as $tag) {
850 $this->assertContains($tag, $tagsInDB);
854 public function testDeleteOneTagEntry()
856 $entry = $this->client
->getContainer()
857 ->get('doctrine.orm.entity_manager')
858 ->getRepository('WallabagCoreBundle:Entry')
859 ->findOneWithTags($this->user
->getId());
863 $this->markTestSkipped('No content found in db.');
866 // hydrate the tags relations
867 $nbTags = \
count($entry->getTags());
868 $tag = $entry->getTags()[0];
870 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '/tags/' . $tag->getId() . '.json');
872 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
874 $content = json_decode($this->client
->getResponse()->getContent(), true);
876 $this->assertArrayHasKey('tags', $content);
877 $this->assertSame($nbTags - 1, \
count($content['tags']));
880 public function testSaveIsArchivedAfterPost()
882 $entry = $this->client
->getContainer()
883 ->get('doctrine.orm.entity_manager')
884 ->getRepository('WallabagCoreBundle:Entry')
885 ->findOneBy(['user' => 1, 'isArchived' => true]);
888 $this->markTestSkipped('No content found in db.');
891 $this->client
->request('POST', '/api/entries.json', [
892 'url' => $entry->getUrl(),
895 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
897 $content = json_decode($this->client
->getResponse()->getContent(), true);
899 $this->assertSame(1, $content['is_archived']);
902 public function testSaveIsStarredAfterPost()
904 $entry = $this->client
->getContainer()
905 ->get('doctrine.orm.entity_manager')
906 ->getRepository('WallabagCoreBundle:Entry')
907 ->findOneBy(['user' => 1, 'isStarred' => true]);
910 $this->markTestSkipped('No content found in db.');
913 $this->client
->request('POST', '/api/entries.json', [
914 'url' => $entry->getUrl(),
917 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
919 $content = json_decode($this->client
->getResponse()->getContent(), true);
921 $this->assertSame(1, $content['is_starred']);
924 public function testSaveIsArchivedAfterPatch()
926 $entry = $this->client
->getContainer()
927 ->get('doctrine.orm.entity_manager')
928 ->getRepository('WallabagCoreBundle:Entry')
929 ->findOneBy(['user' => 1, 'isArchived' => true]);
932 $this->markTestSkipped('No content found in db.');
935 $previousTitle = $entry->getTitle();
937 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
938 'title' => $entry->getTitle() . '++',
941 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
943 $content = json_decode($this->client
->getResponse()->getContent(), true);
945 $this->assertSame(1, $content['is_archived']);
946 $this->assertSame($previousTitle . '++', $content['title']);
949 public function testSaveIsStarredAfterPatch()
951 $now = new \
DateTime();
952 $entry = $this->client
->getContainer()
953 ->get('doctrine.orm.entity_manager')
954 ->getRepository('WallabagCoreBundle:Entry')
955 ->findOneBy(['user' => 1, 'isStarred' => true]);
958 $this->markTestSkipped('No content found in db.');
960 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
961 'title' => $entry->getTitle() . '++',
964 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
966 $content = json_decode($this->client
->getResponse()->getContent(), true);
968 $this->assertSame(1, $content['is_starred']);
969 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
972 public function dataForEntriesExistWithUrl()
976 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1',
977 'expectedValue' => 2,
980 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2',
981 'expectedValue' => true,
987 * @dataProvider dataForEntriesExistWithUrl
989 public function testGetEntriesExists($url, $expectedValue)
991 $this->client
->request('GET', $url);
993 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
995 $content = json_decode($this->client
->getResponse()->getContent(), true);
997 $this->assertSame($expectedValue, $content['exists']);
1000 public function testGetEntriesExistsWithManyUrls()
1002 $url1 = 'http://0.0.0.0/entry2';
1003 $url2 = 'http://0.0.0.0/entry10';
1004 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1');
1006 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1008 $content = json_decode($this->client
->getResponse()->getContent(), true);
1010 $this->assertArrayHasKey($url1, $content);
1011 $this->assertArrayHasKey($url2, $content);
1012 $this->assertSame(2, $content[$url1]);
1013 $this->assertNull($content[$url2]);
1016 public function testGetEntriesExistsWithManyUrlsReturnBool()
1018 $url1 = 'http://0.0.0.0/entry2';
1019 $url2 = 'http://0.0.0.0/entry10';
1020 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2);
1022 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1024 $content = json_decode($this->client
->getResponse()->getContent(), true);
1026 $this->assertArrayHasKey($url1, $content);
1027 $this->assertArrayHasKey($url2, $content);
1028 $this->assertTrue($content[$url1]);
1029 $this->assertFalse($content[$url2]);
1032 public function testGetEntriesExistsWhichDoesNotExists()
1034 $this->client
->request('GET', '/api/entries/exists?url=http://google.com/entry2');
1036 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1038 $content = json_decode($this->client
->getResponse()->getContent(), true);
1040 $this->assertFalse($content['exists']);
1043 public function testGetEntriesExistsWithNoUrl()
1045 $this->client
->request('GET', '/api/entries/exists?url=');
1047 $this->assertSame(403, $this->client
->getResponse()->getStatusCode());
1050 public function testReloadEntryErrorWhileFetching()
1052 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1053 ->getRepository('WallabagCoreBundle:Entry')
1054 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1057 $this->markTestSkipped('No content found in db.');
1060 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json');
1061 $this->assertSame(304, $this->client
->getResponse()->getStatusCode());
1064 public function testReloadEntry()
1066 $this->client
->request('POST', '/api/entries.json', [
1067 'url' => 'https://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',
1069 'tags' => 'google, apple',
1072 $json = json_decode($this->client
->getResponse()->getContent(), true);
1076 $this->client
->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json');
1077 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1079 $content = json_decode($this->client
->getResponse()->getContent(), true);
1081 $this->assertNotEmpty($content['title']);
1083 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
1086 public function testPostEntriesTagsListAction()
1088 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1089 ->getRepository('WallabagCoreBundle:Entry')
1090 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1092 $tags = $entry->getTags();
1094 $this->assertCount(2, $tags);
1098 'url' => 'http://0.0.0.0/entry4',
1099 'tags' => 'new tag 1, new tag 2',
1103 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode($list));
1105 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1107 $content = json_decode($this->client
->getResponse()->getContent(), true);
1109 $this->assertInternalType('int', $content[0]['entry']);
1110 $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']);
1112 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1113 ->getRepository('WallabagCoreBundle:Entry')
1114 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1116 $tags = $entry->getTags();
1117 $this->assertCount(4, $tags);
1120 public function testPostEntriesTagsListActionNoList()
1122 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode([]));
1124 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1126 $content = json_decode($this->client
->getResponse()->getContent(), true);
1128 $this->assertEmpty($content);
1131 public function testDeleteEntriesTagsListAction()
1133 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1134 $entry = new Entry($em->getReference(User
::class, 1));
1135 $entry->setUrl('http://0.0.0.0/test-entry');
1136 $entry->addTag((new Tag())->setLabel('foo-tag'));
1137 $entry->addTag((new Tag())->setLabel('bar-tag'));
1138 $em->persist($entry);
1145 'url' => 'http://0.0.0.0/test-entry',
1146 'tags' => 'foo-tag, bar-tag',
1150 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list));
1151 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1153 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
1154 $this->assertCount(0, $entry->getTags());
1157 public function testDeleteEntriesTagsListActionNoList()
1159 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode([]));
1161 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1163 $content = json_decode($this->client
->getResponse()->getContent(), true);
1165 $this->assertEmpty($content);
1168 public function testPostEntriesListAction()
1171 'https://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
1172 'http://0.0.0.0/entry2',
1175 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1177 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1179 $content = json_decode($this->client
->getResponse()->getContent(), true);
1181 $this->assertInternalType('int', $content[0]['entry']);
1182 $this->assertSame('https://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']);
1184 $this->assertInternalType('int', $content[1]['entry']);
1185 $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']);
1188 public function testPostEntriesListActionWithNoUrls()
1190 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode([]));
1192 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1194 $content = json_decode($this->client
->getResponse()->getContent(), true);
1196 $this->assertEmpty($content);
1199 public function testDeleteEntriesListAction()
1201 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1202 $em->persist((new Entry($em->getReference(User
::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
1207 'http://0.0.0.0/test-entry1',
1208 'http://0.0.0.0/test-entry-not-exist',
1211 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode($list));
1213 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1215 $content = json_decode($this->client
->getResponse()->getContent(), true);
1217 $this->assertTrue($content[0]['entry']);
1218 $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']);
1220 $this->assertFalse($content[1]['entry']);
1221 $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
1224 public function testDeleteEntriesListActionWithNoUrls()
1226 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode([]));
1228 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1230 $content = json_decode($this->client
->getResponse()->getContent(), true);
1232 $this->assertEmpty($content);
1235 public function testLimitBulkAction()
1238 'http://0.0.0.0/entry1',
1239 'http://0.0.0.0/entry1',
1240 'http://0.0.0.0/entry1',
1241 'http://0.0.0.0/entry1',
1242 'http://0.0.0.0/entry1',
1243 'http://0.0.0.0/entry1',
1244 'http://0.0.0.0/entry1',
1245 'http://0.0.0.0/entry1',
1246 'http://0.0.0.0/entry1',
1247 'http://0.0.0.0/entry1',
1248 'http://0.0.0.0/entry1',
1251 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1253 $this->assertSame(400, $this->client
->getResponse()->getStatusCode());
1254 $this->assertContains('API limit reached', $this->client
->getResponse()->getContent());
1257 public function testRePostEntryAndReUsePublishedAt()
1259 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1260 $entry = new Entry($em->getReference(User
::class, 1));
1261 $entry->setTitle('Antoine de Caunes : « Je veux avoir le droit de tâtonner »');
1262 $entry->setContent('hihi');
1263 $entry->setUrl('https://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html');
1264 $entry->setPublishedAt(new \
DateTime('2017-06-26T07:46:02+0200'));
1265 $em->persist($entry);
1269 $this->client
->request('POST', '/api/entries.json', [
1270 'url' => 'https://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html',
1273 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1275 $content = json_decode($this->client
->getResponse()->getContent(), true);
1277 $this->assertGreaterThan(0, $content['id']);
1278 $this->assertSame('https://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html', $content['url']);