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 $this->client
->request('DELETE', '/api/entries/' . $e['id'] . '.json');
431 $this->assertSame(404, $this->client
->getResponse()->getStatusCode());
434 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '.json');
436 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
438 $content = json_decode($this->client
->getResponse()->getContent(), true);
440 $this->assertSame($entry->getTitle(), $content['title']);
441 $this->assertSame($entry->getUrl(), $content['url']);
442 $this->assertSame($entry->getId(), $content['id']);
444 // We'll try to delete this entry again
445 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '.json');
447 $this->assertSame(404, $this->client
->getResponse()->getStatusCode());
450 public function testPostEntry()
452 $this->client
->request('POST', '/api/entries.json', [
453 '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',
455 'title' => 'New title for my article',
456 'content' => 'my content',
458 'published_at' => '2016-09-08T11:55:58+0200',
459 'authors' => 'bob,helen',
463 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
465 $content = json_decode($this->client
->getResponse()->getContent(), true);
467 $this->assertGreaterThan(0, $content['id']);
468 $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']);
469 $this->assertSame(0, $content['is_archived']);
470 $this->assertSame(0, $content['is_starred']);
471 $this->assertNull($content['starred_at']);
472 $this->assertSame('New title for my article', $content['title']);
473 $this->assertSame(1, $content['user_id']);
474 $this->assertCount(2, $content['tags']);
475 $this->assertNull($content['origin_url']);
476 $this->assertSame('my content', $content['content']);
477 $this->assertSame('de', $content['language']);
478 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
479 $this->assertCount(2, $content['published_by']);
480 $this->assertContains('bob', $content['published_by']);
481 $this->assertContains('helen', $content['published_by']);
482 $this->assertTrue($content['is_public'], 'A public link has been generated for that entry');
485 public function testPostSameEntry()
487 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
488 $entry = new Entry($em->getReference(User
::class, 1));
489 $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');
490 $entry->setArchived(true);
491 $entry->addTag((new Tag())->setLabel('google'));
492 $entry->addTag((new Tag())->setLabel('apple'));
493 $em->persist($entry);
497 $this->client
->request('POST', '/api/entries.json', [
498 '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',
500 'tags' => 'google, apple',
503 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
505 $content = json_decode($this->client
->getResponse()->getContent(), true);
507 $this->assertGreaterThan(0, $content['id']);
508 $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']);
509 $this->assertSame(1, $content['is_archived']);
510 $this->assertSame(0, $content['is_starred']);
511 $this->assertCount(3, $content['tags']);
514 public function testPostEntryWhenFetchContentFails()
516 /** @var \Symfony\Component\DependencyInjection\Container $container */
517 $container = $this->client
->getContainer();
518 $contentProxy = $this->getMockBuilder(ContentProxy
::class)
519 ->disableOriginalConstructor()
520 ->setMethods(['updateEntry'])
522 $contentProxy->expects($this->any())
523 ->method('updateEntry')
524 ->willThrowException(new \
Exception('Test Fetch content fails'));
525 $container->set('wallabag_core.content_proxy', $contentProxy);
528 $this->client
->request('POST', '/api/entries.json', [
529 'url' => 'http://www.example.com/',
532 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
533 $content = json_decode($this->client
->getResponse()->getContent(), true);
534 $this->assertGreaterThan(0, $content['id']);
535 $this->assertSame('http://www.example.com/', $content['url']);
536 $this->assertSame('www.example.com', $content['domain_name']);
537 $this->assertSame('www.example.com', $content['title']);
539 // Remove the created entry to avoid side effects on other tests
540 if (isset($content['id'])) {
541 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
542 $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']);
549 public function testPostArchivedAndStarredEntry()
551 $now = new \
DateTime();
552 $this->client
->request('POST', '/api/entries.json', [
553 'url' => 'https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
558 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
560 $content = json_decode($this->client
->getResponse()->getContent(), true);
562 $this->assertGreaterThan(0, $content['id']);
563 $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']);
564 $this->assertSame(1, $content['is_archived']);
565 $this->assertSame(1, $content['is_starred']);
566 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
567 $this->assertSame(1, $content['user_id']);
570 public function testPostArchivedAndStarredEntryWithoutQuotes()
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(0, $content['is_archived']);
585 $this->assertSame(1, $content['is_starred']);
588 public function testPostEntryWithOriginUrl()
590 $this->client
->request('POST', '/api/entries.json', [
591 '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',
593 'title' => 'New title for my article',
594 'content' => 'my content',
596 'published_at' => '2016-09-08T11:55:58+0200',
597 'authors' => 'bob,helen',
599 'origin_url' => 'http://mysource.tld',
602 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
604 $content = json_decode($this->client
->getResponse()->getContent(), true);
606 $this->assertGreaterThan(0, $content['id']);
607 $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']);
608 $this->assertSame('http://mysource.tld', $content['origin_url']);
611 public function testPatchEntry()
613 $entry = $this->client
->getContainer()
614 ->get('doctrine.orm.entity_manager')
615 ->getRepository('WallabagCoreBundle:Entry')
619 $this->markTestSkipped('No content found in db.');
622 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
623 'title' => 'New awesome title',
624 'tags' => 'new tag ' . uniqid(),
627 'language' => 'de_AT',
628 'preview_picture' => 'http://preview.io/picture.jpg',
629 'authors' => 'bob,sponge',
630 'content' => 'awesome',
632 'published_at' => 1488833381,
635 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
637 $content = json_decode($this->client
->getResponse()->getContent(), true);
639 $this->assertSame($entry->getId(), $content['id']);
640 $this->assertSame($entry->getUrl(), $content['url']);
641 $this->assertSame('New awesome title', $content['title']);
642 $this->assertGreaterThanOrEqual(1, \
count($content['tags']), 'We force only one tag');
643 $this->assertSame(1, $content['user_id']);
644 $this->assertSame('de_AT', $content['language']);
645 $this->assertSame('http://preview.io/picture.jpg', $content['preview_picture']);
646 $this->assertContains('sponge', $content['published_by']);
647 $this->assertContains('bob', $content['published_by']);
648 $this->assertSame('awesome', $content['content']);
649 $this->assertFalse($content['is_public'], 'Entry is no more shared');
650 $this->assertContains('2017-03-06', $content['published_at']);
653 public function testPatchEntryWithoutQuotes()
655 $entry = $this->client
->getContainer()
656 ->get('doctrine.orm.entity_manager')
657 ->getRepository('WallabagCoreBundle:Entry')
661 $this->markTestSkipped('No content found in db.');
664 $previousContent = $entry->getContent();
665 $previousLanguage = $entry->getLanguage();
667 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
668 'title' => 'New awesome title',
669 'tags' => 'new tag ' . uniqid(),
672 'authors' => ['bob', 'sponge'],
675 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
677 $content = json_decode($this->client
->getResponse()->getContent(), true);
679 $this->assertSame($entry->getId(), $content['id']);
680 $this->assertSame($entry->getUrl(), $content['url']);
681 $this->assertGreaterThanOrEqual(1, \
count($content['tags']), 'We force only one tag');
682 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
683 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
684 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
687 public function testPatchEntryWithOriginUrl()
689 $entry = $this->client
->getContainer()
690 ->get('doctrine.orm.entity_manager')
691 ->getRepository('WallabagCoreBundle:Entry')
695 $this->markTestSkipped('No content found in db.');
698 $previousContent = $entry->getContent();
699 $previousLanguage = $entry->getLanguage();
701 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
702 'title' => 'Another awesome title just for profit',
703 'origin_url' => 'https://myawesomesource.example.com',
706 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
708 $content = json_decode($this->client
->getResponse()->getContent(), true);
710 $this->assertSame($entry->getId(), $content['id']);
711 $this->assertSame($entry->getUrl(), $content['url']);
712 $this->assertSame('https://myawesomesource.example.com', $content['origin_url']);
713 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
714 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
715 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
718 public function testPatchEntryRemoveOriginUrl()
720 $entry = $this->client
->getContainer()
721 ->get('doctrine.orm.entity_manager')
722 ->getRepository('WallabagCoreBundle:Entry')
726 $this->markTestSkipped('No content found in db.');
729 $previousContent = $entry->getContent();
730 $previousLanguage = $entry->getLanguage();
731 $previousTitle = $entry->getTitle();
733 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
737 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
739 $content = json_decode($this->client
->getResponse()->getContent(), true);
741 $this->assertSame($entry->getId(), $content['id']);
742 $this->assertSame($entry->getUrl(), $content['url']);
743 $this->assertEmpty($content['origin_url']);
744 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
745 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
746 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
747 $this->assertSame($previousTitle, $content['title'], 'Ensure title has not moved');
750 public function testPatchEntryNullOriginUrl()
752 $entry = $this->client
->getContainer()
753 ->get('doctrine.orm.entity_manager')
754 ->getRepository('WallabagCoreBundle:Entry')
758 $this->markTestSkipped('No content found in db.');
761 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
762 'origin_url' => null,
765 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
767 $content = json_decode($this->client
->getResponse()->getContent(), true);
769 $this->assertNull($content['origin_url']);
772 public function testGetTagsEntry()
774 $entry = $this->client
->getContainer()
775 ->get('doctrine.orm.entity_manager')
776 ->getRepository('WallabagCoreBundle:Entry')
777 ->findOneWithTags($this->user
->getId());
782 $this->markTestSkipped('No content found in db.');
786 foreach ($entry->getTags() as $tag) {
787 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
790 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '/tags');
792 $this->assertSame(json_encode($tags, JSON_HEX_QUOT
), $this->client
->getResponse()->getContent());
795 public function testPostTagsOnEntry()
797 $entry = $this->client
->getContainer()
798 ->get('doctrine.orm.entity_manager')
799 ->getRepository('WallabagCoreBundle:Entry')
803 $this->markTestSkipped('No content found in db.');
806 $nbTags = \
count($entry->getTags());
808 $newTags = 'tag1,tag2,tag3';
810 $this->client
->request('POST', '/api/entries/' . $entry->getId() . '/tags', ['tags' => $newTags]);
812 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
814 $content = json_decode($this->client
->getResponse()->getContent(), true);
816 $this->assertArrayHasKey('tags', $content);
817 $this->assertSame($nbTags +
3, \
count($content['tags']));
819 $entryDB = $this->client
->getContainer()
820 ->get('doctrine.orm.entity_manager')
821 ->getRepository('WallabagCoreBundle:Entry')
822 ->find($entry->getId());
825 foreach ($entryDB->getTags()->toArray() as $tag) {
826 $tagsInDB[$tag->getId()] = $tag->getLabel();
829 foreach (explode(',', $newTags) as $tag) {
830 $this->assertContains($tag, $tagsInDB);
834 public function testDeleteOneTagEntry()
836 $entry = $this->client
->getContainer()
837 ->get('doctrine.orm.entity_manager')
838 ->getRepository('WallabagCoreBundle:Entry')
839 ->findOneWithTags($this->user
->getId());
843 $this->markTestSkipped('No content found in db.');
846 // hydrate the tags relations
847 $nbTags = \
count($entry->getTags());
848 $tag = $entry->getTags()[0];
850 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '/tags/' . $tag->getId() . '.json');
852 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
854 $content = json_decode($this->client
->getResponse()->getContent(), true);
856 $this->assertArrayHasKey('tags', $content);
857 $this->assertSame($nbTags - 1, \
count($content['tags']));
860 public function testSaveIsArchivedAfterPost()
862 $entry = $this->client
->getContainer()
863 ->get('doctrine.orm.entity_manager')
864 ->getRepository('WallabagCoreBundle:Entry')
865 ->findOneBy(['user' => 1, 'isArchived' => true]);
868 $this->markTestSkipped('No content found in db.');
871 $this->client
->request('POST', '/api/entries.json', [
872 'url' => $entry->getUrl(),
875 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
877 $content = json_decode($this->client
->getResponse()->getContent(), true);
879 $this->assertSame(1, $content['is_archived']);
882 public function testSaveIsStarredAfterPost()
884 $entry = $this->client
->getContainer()
885 ->get('doctrine.orm.entity_manager')
886 ->getRepository('WallabagCoreBundle:Entry')
887 ->findOneBy(['user' => 1, 'isStarred' => true]);
890 $this->markTestSkipped('No content found in db.');
893 $this->client
->request('POST', '/api/entries.json', [
894 'url' => $entry->getUrl(),
897 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
899 $content = json_decode($this->client
->getResponse()->getContent(), true);
901 $this->assertSame(1, $content['is_starred']);
904 public function testSaveIsArchivedAfterPatch()
906 $entry = $this->client
->getContainer()
907 ->get('doctrine.orm.entity_manager')
908 ->getRepository('WallabagCoreBundle:Entry')
909 ->findOneBy(['user' => 1, 'isArchived' => true]);
912 $this->markTestSkipped('No content found in db.');
915 $previousTitle = $entry->getTitle();
917 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
918 'title' => $entry->getTitle() . '++',
921 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
923 $content = json_decode($this->client
->getResponse()->getContent(), true);
925 $this->assertSame(1, $content['is_archived']);
926 $this->assertSame($previousTitle . '++', $content['title']);
929 public function testSaveIsStarredAfterPatch()
931 $now = new \
DateTime();
932 $entry = $this->client
->getContainer()
933 ->get('doctrine.orm.entity_manager')
934 ->getRepository('WallabagCoreBundle:Entry')
935 ->findOneBy(['user' => 1, 'isStarred' => true]);
938 $this->markTestSkipped('No content found in db.');
940 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
941 'title' => $entry->getTitle() . '++',
944 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
946 $content = json_decode($this->client
->getResponse()->getContent(), true);
948 $this->assertSame(1, $content['is_starred']);
949 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
952 public function dataForEntriesExistWithUrl()
956 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1',
957 'expectedValue' => 2,
960 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2',
961 'expectedValue' => true,
967 * @dataProvider dataForEntriesExistWithUrl
969 public function testGetEntriesExists($url, $expectedValue)
971 $this->client
->request('GET', $url);
973 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
975 $content = json_decode($this->client
->getResponse()->getContent(), true);
977 $this->assertSame($expectedValue, $content['exists']);
980 public function testGetEntriesExistsWithManyUrls()
982 $url1 = 'http://0.0.0.0/entry2';
983 $url2 = 'http://0.0.0.0/entry10';
984 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1');
986 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
988 $content = json_decode($this->client
->getResponse()->getContent(), true);
990 $this->assertArrayHasKey($url1, $content);
991 $this->assertArrayHasKey($url2, $content);
992 $this->assertSame(2, $content[$url1]);
993 $this->assertNull($content[$url2]);
996 public function testGetEntriesExistsWithManyUrlsReturnBool()
998 $url1 = 'http://0.0.0.0/entry2';
999 $url2 = 'http://0.0.0.0/entry10';
1000 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2);
1002 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1004 $content = json_decode($this->client
->getResponse()->getContent(), true);
1006 $this->assertArrayHasKey($url1, $content);
1007 $this->assertArrayHasKey($url2, $content);
1008 $this->assertTrue($content[$url1]);
1009 $this->assertFalse($content[$url2]);
1012 public function testGetEntriesExistsWhichDoesNotExists()
1014 $this->client
->request('GET', '/api/entries/exists?url=http://google.com/entry2');
1016 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1018 $content = json_decode($this->client
->getResponse()->getContent(), true);
1020 $this->assertFalse($content['exists']);
1023 public function testGetEntriesExistsWithNoUrl()
1025 $this->client
->request('GET', '/api/entries/exists?url=');
1027 $this->assertSame(403, $this->client
->getResponse()->getStatusCode());
1030 public function testReloadEntryErrorWhileFetching()
1032 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1033 ->getRepository('WallabagCoreBundle:Entry')
1034 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1037 $this->markTestSkipped('No content found in db.');
1040 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json');
1041 $this->assertSame(304, $this->client
->getResponse()->getStatusCode());
1044 public function testReloadEntry()
1046 $this->client
->request('POST', '/api/entries.json', [
1047 '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',
1049 'tags' => 'google, apple',
1052 $json = json_decode($this->client
->getResponse()->getContent(), true);
1056 $this->client
->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json');
1057 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1059 $content = json_decode($this->client
->getResponse()->getContent(), true);
1061 $this->assertNotEmpty($content['title']);
1063 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
1066 public function testPostEntriesTagsListAction()
1068 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1069 ->getRepository('WallabagCoreBundle:Entry')
1070 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1072 $tags = $entry->getTags();
1074 $this->assertCount(2, $tags);
1078 'url' => 'http://0.0.0.0/entry4',
1079 'tags' => 'new tag 1, new tag 2',
1083 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode($list));
1085 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1087 $content = json_decode($this->client
->getResponse()->getContent(), true);
1089 $this->assertInternalType('int', $content[0]['entry']);
1090 $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']);
1092 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1093 ->getRepository('WallabagCoreBundle:Entry')
1094 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1096 $tags = $entry->getTags();
1097 $this->assertCount(4, $tags);
1100 public function testPostEntriesTagsListActionNoList()
1102 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode([]));
1104 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1106 $content = json_decode($this->client
->getResponse()->getContent(), true);
1108 $this->assertEmpty($content);
1111 public function testDeleteEntriesTagsListAction()
1113 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1114 $entry = new Entry($em->getReference(User
::class, 1));
1115 $entry->setUrl('http://0.0.0.0/test-entry');
1116 $entry->addTag((new Tag())->setLabel('foo-tag'));
1117 $entry->addTag((new Tag())->setLabel('bar-tag'));
1118 $em->persist($entry);
1125 'url' => 'http://0.0.0.0/test-entry',
1126 'tags' => 'foo-tag, bar-tag',
1130 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list));
1131 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1133 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
1134 $this->assertCount(0, $entry->getTags());
1137 public function testDeleteEntriesTagsListActionNoList()
1139 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode([]));
1141 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1143 $content = json_decode($this->client
->getResponse()->getContent(), true);
1145 $this->assertEmpty($content);
1148 public function testPostEntriesListAction()
1151 'https://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
1152 'http://0.0.0.0/entry2',
1155 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1157 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1159 $content = json_decode($this->client
->getResponse()->getContent(), true);
1161 $this->assertInternalType('int', $content[0]['entry']);
1162 $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']);
1164 $this->assertInternalType('int', $content[1]['entry']);
1165 $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']);
1168 public function testPostEntriesListActionWithNoUrls()
1170 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode([]));
1172 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1174 $content = json_decode($this->client
->getResponse()->getContent(), true);
1176 $this->assertEmpty($content);
1179 public function testDeleteEntriesListAction()
1181 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1182 $em->persist((new Entry($em->getReference(User
::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
1187 'http://0.0.0.0/test-entry1',
1188 'http://0.0.0.0/test-entry-not-exist',
1191 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode($list));
1193 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1195 $content = json_decode($this->client
->getResponse()->getContent(), true);
1197 $this->assertTrue($content[0]['entry']);
1198 $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']);
1200 $this->assertFalse($content[1]['entry']);
1201 $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
1204 public function testDeleteEntriesListActionWithNoUrls()
1206 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode([]));
1208 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1210 $content = json_decode($this->client
->getResponse()->getContent(), true);
1212 $this->assertEmpty($content);
1215 public function testLimitBulkAction()
1218 'http://0.0.0.0/entry1',
1219 'http://0.0.0.0/entry1',
1220 'http://0.0.0.0/entry1',
1221 'http://0.0.0.0/entry1',
1222 'http://0.0.0.0/entry1',
1223 'http://0.0.0.0/entry1',
1224 'http://0.0.0.0/entry1',
1225 'http://0.0.0.0/entry1',
1226 'http://0.0.0.0/entry1',
1227 'http://0.0.0.0/entry1',
1228 'http://0.0.0.0/entry1',
1231 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1233 $this->assertSame(400, $this->client
->getResponse()->getStatusCode());
1234 $this->assertContains('API limit reached', $this->client
->getResponse()->getContent());
1237 public function testRePostEntryAndReUsePublishedAt()
1239 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1240 $entry = new Entry($em->getReference(User
::class, 1));
1241 $entry->setTitle('Antoine de Caunes : « Je veux avoir le droit de tâtonner »');
1242 $entry->setContent('hihi');
1243 $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');
1244 $entry->setPublishedAt(new \
DateTime('2017-06-26T07:46:02+0200'));
1245 $em->persist($entry);
1249 $this->client
->request('POST', '/api/entries.json', [
1250 'url' => 'https://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html',
1253 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1255 $content = json_decode($this->client
->getResponse()->getContent(), true);
1257 $this->assertGreaterThan(0, $content['id']);
1258 $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']);