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 $entry = $this->client
->getContainer()
404 ->get('doctrine.orm.entity_manager')
405 ->getRepository('WallabagCoreBundle:Entry')
406 ->findOneByUser(1, ['id' => 'asc']);
409 $this->markTestSkipped('No content found in db.');
412 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '.json');
414 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
416 $content = json_decode($this->client
->getResponse()->getContent(), true);
418 $this->assertSame($entry->getTitle(), $content['title']);
419 $this->assertSame($entry->getUrl(), $content['url']);
420 $this->assertSame($entry->getId(), $content['id']);
422 // We'll try to delete this entry again
423 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '.json');
425 $this->assertSame(404, $this->client
->getResponse()->getStatusCode());
428 public function testPostEntry()
430 $this->client
->request('POST', '/api/entries.json', [
431 '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',
433 'title' => 'New title for my article',
434 'content' => 'my content',
436 'published_at' => '2016-09-08T11:55:58+0200',
437 'authors' => 'bob,helen',
441 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
443 $content = json_decode($this->client
->getResponse()->getContent(), true);
445 $this->assertGreaterThan(0, $content['id']);
446 $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']);
447 $this->assertSame(0, $content['is_archived']);
448 $this->assertSame(0, $content['is_starred']);
449 $this->assertNull($content['starred_at']);
450 $this->assertSame('New title for my article', $content['title']);
451 $this->assertSame(1, $content['user_id']);
452 $this->assertCount(2, $content['tags']);
453 $this->assertNull($content['origin_url']);
454 $this->assertSame('my content', $content['content']);
455 $this->assertSame('de', $content['language']);
456 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
457 $this->assertCount(2, $content['published_by']);
458 $this->assertContains('bob', $content['published_by']);
459 $this->assertContains('helen', $content['published_by']);
460 $this->assertTrue($content['is_public'], 'A public link has been generated for that entry');
463 public function testPostSameEntry()
465 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
466 $entry = new Entry($em->getReference(User
::class, 1));
467 $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');
468 $entry->setArchived(true);
469 $entry->addTag((new Tag())->setLabel('google'));
470 $entry->addTag((new Tag())->setLabel('apple'));
471 $em->persist($entry);
475 $this->client
->request('POST', '/api/entries.json', [
476 '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',
478 'tags' => 'google, apple',
481 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
483 $content = json_decode($this->client
->getResponse()->getContent(), true);
485 $this->assertGreaterThan(0, $content['id']);
486 $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']);
487 $this->assertSame(1, $content['is_archived']);
488 $this->assertSame(0, $content['is_starred']);
489 $this->assertCount(3, $content['tags']);
492 public function testPostEntryWhenFetchContentFails()
494 /** @var \Symfony\Component\DependencyInjection\Container $container */
495 $container = $this->client
->getContainer();
496 $contentProxy = $this->getMockBuilder(ContentProxy
::class)
497 ->disableOriginalConstructor()
498 ->setMethods(['updateEntry'])
500 $contentProxy->expects($this->any())
501 ->method('updateEntry')
502 ->willThrowException(new \
Exception('Test Fetch content fails'));
503 $container->set('wallabag_core.content_proxy', $contentProxy);
506 $this->client
->request('POST', '/api/entries.json', [
507 'url' => 'http://www.example.com/',
510 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
511 $content = json_decode($this->client
->getResponse()->getContent(), true);
512 $this->assertGreaterThan(0, $content['id']);
513 $this->assertSame('http://www.example.com/', $content['url']);
514 $this->assertSame('www.example.com', $content['domain_name']);
515 $this->assertSame('www.example.com', $content['title']);
517 // Remove the created entry to avoid side effects on other tests
518 if (isset($content['id'])) {
519 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
520 $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']);
527 public function testPostArchivedAndStarredEntry()
529 $now = new \
DateTime();
530 $this->client
->request('POST', '/api/entries.json', [
531 'url' => 'https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
536 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
538 $content = json_decode($this->client
->getResponse()->getContent(), true);
540 $this->assertGreaterThan(0, $content['id']);
541 $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']);
542 $this->assertSame(1, $content['is_archived']);
543 $this->assertSame(1, $content['is_starred']);
544 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
545 $this->assertSame(1, $content['user_id']);
548 public function testPostArchivedAndStarredEntryWithoutQuotes()
550 $this->client
->request('POST', '/api/entries.json', [
551 'url' => 'https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
556 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
558 $content = json_decode($this->client
->getResponse()->getContent(), true);
560 $this->assertGreaterThan(0, $content['id']);
561 $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']);
562 $this->assertSame(0, $content['is_archived']);
563 $this->assertSame(1, $content['is_starred']);
566 public function testPostEntryWithOriginUrl()
568 $this->client
->request('POST', '/api/entries.json', [
569 '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',
571 'title' => 'New title for my article',
572 'content' => 'my content',
574 'published_at' => '2016-09-08T11:55:58+0200',
575 'authors' => 'bob,helen',
577 'origin_url' => 'http://mysource.tld',
580 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
582 $content = json_decode($this->client
->getResponse()->getContent(), true);
584 $this->assertGreaterThan(0, $content['id']);
585 $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']);
586 $this->assertSame('http://mysource.tld', $content['origin_url']);
589 public function testPatchEntry()
591 $entry = $this->client
->getContainer()
592 ->get('doctrine.orm.entity_manager')
593 ->getRepository('WallabagCoreBundle:Entry')
597 $this->markTestSkipped('No content found in db.');
600 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
601 'title' => 'New awesome title',
602 'tags' => 'new tag ' . uniqid(),
605 'language' => 'de_AT',
606 'preview_picture' => 'http://preview.io/picture.jpg',
607 'authors' => 'bob,sponge',
608 'content' => 'awesome',
610 'published_at' => 1488833381,
613 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
615 $content = json_decode($this->client
->getResponse()->getContent(), true);
617 $this->assertSame($entry->getId(), $content['id']);
618 $this->assertSame($entry->getUrl(), $content['url']);
619 $this->assertSame('New awesome title', $content['title']);
620 $this->assertGreaterThanOrEqual(1, \
count($content['tags']), 'We force only one tag');
621 $this->assertSame(1, $content['user_id']);
622 $this->assertSame('de_AT', $content['language']);
623 $this->assertSame('http://preview.io/picture.jpg', $content['preview_picture']);
624 $this->assertContains('sponge', $content['published_by']);
625 $this->assertContains('bob', $content['published_by']);
626 $this->assertSame('awesome', $content['content']);
627 $this->assertFalse($content['is_public'], 'Entry is no more shared');
628 $this->assertContains('2017-03-06', $content['published_at']);
631 public function testPatchEntryWithoutQuotes()
633 $entry = $this->client
->getContainer()
634 ->get('doctrine.orm.entity_manager')
635 ->getRepository('WallabagCoreBundle:Entry')
639 $this->markTestSkipped('No content found in db.');
642 $previousContent = $entry->getContent();
643 $previousLanguage = $entry->getLanguage();
645 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
646 'title' => 'New awesome title',
647 'tags' => 'new tag ' . uniqid(),
650 'authors' => ['bob', 'sponge'],
653 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
655 $content = json_decode($this->client
->getResponse()->getContent(), true);
657 $this->assertSame($entry->getId(), $content['id']);
658 $this->assertSame($entry->getUrl(), $content['url']);
659 $this->assertGreaterThanOrEqual(1, \
count($content['tags']), 'We force only one tag');
660 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
661 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
662 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
665 public function testPatchEntryWithOriginUrl()
667 $entry = $this->client
->getContainer()
668 ->get('doctrine.orm.entity_manager')
669 ->getRepository('WallabagCoreBundle:Entry')
673 $this->markTestSkipped('No content found in db.');
676 $previousContent = $entry->getContent();
677 $previousLanguage = $entry->getLanguage();
679 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
680 'title' => 'Another awesome title just for profit',
681 'origin_url' => 'https://myawesomesource.example.com',
684 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
686 $content = json_decode($this->client
->getResponse()->getContent(), true);
688 $this->assertSame($entry->getId(), $content['id']);
689 $this->assertSame($entry->getUrl(), $content['url']);
690 $this->assertSame('https://myawesomesource.example.com', $content['origin_url']);
691 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
692 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
693 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
696 public function testPatchEntryRemoveOriginUrl()
698 $entry = $this->client
->getContainer()
699 ->get('doctrine.orm.entity_manager')
700 ->getRepository('WallabagCoreBundle:Entry')
704 $this->markTestSkipped('No content found in db.');
707 $previousContent = $entry->getContent();
708 $previousLanguage = $entry->getLanguage();
709 $previousTitle = $entry->getTitle();
711 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
715 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
717 $content = json_decode($this->client
->getResponse()->getContent(), true);
719 $this->assertSame($entry->getId(), $content['id']);
720 $this->assertSame($entry->getUrl(), $content['url']);
721 $this->assertEmpty($content['origin_url']);
722 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
723 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
724 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
725 $this->assertSame($previousTitle, $content['title'], 'Ensure title has not moved');
728 public function testPatchEntryNullOriginUrl()
730 $entry = $this->client
->getContainer()
731 ->get('doctrine.orm.entity_manager')
732 ->getRepository('WallabagCoreBundle:Entry')
736 $this->markTestSkipped('No content found in db.');
739 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
740 'origin_url' => null,
743 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
745 $content = json_decode($this->client
->getResponse()->getContent(), true);
747 $this->assertNull($content['origin_url']);
750 public function testGetTagsEntry()
752 $entry = $this->client
->getContainer()
753 ->get('doctrine.orm.entity_manager')
754 ->getRepository('WallabagCoreBundle:Entry')
755 ->findOneWithTags($this->user
->getId());
760 $this->markTestSkipped('No content found in db.');
764 foreach ($entry->getTags() as $tag) {
765 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
768 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '/tags');
770 $this->assertSame(json_encode($tags, JSON_HEX_QUOT
), $this->client
->getResponse()->getContent());
773 public function testPostTagsOnEntry()
775 $entry = $this->client
->getContainer()
776 ->get('doctrine.orm.entity_manager')
777 ->getRepository('WallabagCoreBundle:Entry')
781 $this->markTestSkipped('No content found in db.');
784 $nbTags = \
count($entry->getTags());
786 $newTags = 'tag1,tag2,tag3';
788 $this->client
->request('POST', '/api/entries/' . $entry->getId() . '/tags', ['tags' => $newTags]);
790 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
792 $content = json_decode($this->client
->getResponse()->getContent(), true);
794 $this->assertArrayHasKey('tags', $content);
795 $this->assertSame($nbTags +
3, \
count($content['tags']));
797 $entryDB = $this->client
->getContainer()
798 ->get('doctrine.orm.entity_manager')
799 ->getRepository('WallabagCoreBundle:Entry')
800 ->find($entry->getId());
803 foreach ($entryDB->getTags()->toArray() as $tag) {
804 $tagsInDB[$tag->getId()] = $tag->getLabel();
807 foreach (explode(',', $newTags) as $tag) {
808 $this->assertContains($tag, $tagsInDB);
812 public function testDeleteOneTagEntry()
814 $entry = $this->client
->getContainer()
815 ->get('doctrine.orm.entity_manager')
816 ->getRepository('WallabagCoreBundle:Entry')
817 ->findOneWithTags($this->user
->getId());
821 $this->markTestSkipped('No content found in db.');
824 // hydrate the tags relations
825 $nbTags = \
count($entry->getTags());
826 $tag = $entry->getTags()[0];
828 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '/tags/' . $tag->getId() . '.json');
830 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
832 $content = json_decode($this->client
->getResponse()->getContent(), true);
834 $this->assertArrayHasKey('tags', $content);
835 $this->assertSame($nbTags - 1, \
count($content['tags']));
838 public function testSaveIsArchivedAfterPost()
840 $entry = $this->client
->getContainer()
841 ->get('doctrine.orm.entity_manager')
842 ->getRepository('WallabagCoreBundle:Entry')
843 ->findOneBy(['user' => 1, 'isArchived' => true]);
846 $this->markTestSkipped('No content found in db.');
849 $this->client
->request('POST', '/api/entries.json', [
850 'url' => $entry->getUrl(),
853 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
855 $content = json_decode($this->client
->getResponse()->getContent(), true);
857 $this->assertSame(1, $content['is_archived']);
860 public function testSaveIsStarredAfterPost()
862 $entry = $this->client
->getContainer()
863 ->get('doctrine.orm.entity_manager')
864 ->getRepository('WallabagCoreBundle:Entry')
865 ->findOneBy(['user' => 1, 'isStarred' => 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_starred']);
882 public function testSaveIsArchivedAfterPatch()
884 $entry = $this->client
->getContainer()
885 ->get('doctrine.orm.entity_manager')
886 ->getRepository('WallabagCoreBundle:Entry')
887 ->findOneBy(['user' => 1, 'isArchived' => true]);
890 $this->markTestSkipped('No content found in db.');
893 $previousTitle = $entry->getTitle();
895 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
896 'title' => $entry->getTitle() . '++',
899 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
901 $content = json_decode($this->client
->getResponse()->getContent(), true);
903 $this->assertSame(1, $content['is_archived']);
904 $this->assertSame($previousTitle . '++', $content['title']);
907 public function testSaveIsStarredAfterPatch()
909 $now = new \
DateTime();
910 $entry = $this->client
->getContainer()
911 ->get('doctrine.orm.entity_manager')
912 ->getRepository('WallabagCoreBundle:Entry')
913 ->findOneBy(['user' => 1, 'isStarred' => true]);
916 $this->markTestSkipped('No content found in db.');
918 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
919 'title' => $entry->getTitle() . '++',
922 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
924 $content = json_decode($this->client
->getResponse()->getContent(), true);
926 $this->assertSame(1, $content['is_starred']);
927 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
930 public function dataForEntriesExistWithUrl()
934 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1',
935 'expectedValue' => 2,
938 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2',
939 'expectedValue' => true,
945 * @dataProvider dataForEntriesExistWithUrl
947 public function testGetEntriesExists($url, $expectedValue)
949 $this->client
->request('GET', $url);
951 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
953 $content = json_decode($this->client
->getResponse()->getContent(), true);
955 $this->assertSame($expectedValue, $content['exists']);
958 public function testGetEntriesExistsWithManyUrls()
960 $url1 = 'http://0.0.0.0/entry2';
961 $url2 = 'http://0.0.0.0/entry10';
962 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1');
964 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
966 $content = json_decode($this->client
->getResponse()->getContent(), true);
968 $this->assertArrayHasKey($url1, $content);
969 $this->assertArrayHasKey($url2, $content);
970 $this->assertSame(2, $content[$url1]);
971 $this->assertNull($content[$url2]);
974 public function testGetEntriesExistsWithManyUrlsReturnBool()
976 $url1 = 'http://0.0.0.0/entry2';
977 $url2 = 'http://0.0.0.0/entry10';
978 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2);
980 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
982 $content = json_decode($this->client
->getResponse()->getContent(), true);
984 $this->assertArrayHasKey($url1, $content);
985 $this->assertArrayHasKey($url2, $content);
986 $this->assertTrue($content[$url1]);
987 $this->assertFalse($content[$url2]);
990 public function testGetEntriesExistsWhichDoesNotExists()
992 $this->client
->request('GET', '/api/entries/exists?url=http://google.com/entry2');
994 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
996 $content = json_decode($this->client
->getResponse()->getContent(), true);
998 $this->assertFalse($content['exists']);
1001 public function testGetEntriesExistsWithNoUrl()
1003 $this->client
->request('GET', '/api/entries/exists?url=');
1005 $this->assertSame(403, $this->client
->getResponse()->getStatusCode());
1008 public function testReloadEntryErrorWhileFetching()
1010 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1011 ->getRepository('WallabagCoreBundle:Entry')
1012 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1015 $this->markTestSkipped('No content found in db.');
1018 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json');
1019 $this->assertSame(304, $this->client
->getResponse()->getStatusCode());
1022 public function testReloadEntry()
1024 $this->client
->request('POST', '/api/entries.json', [
1025 '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',
1027 'tags' => 'google, apple',
1030 $json = json_decode($this->client
->getResponse()->getContent(), true);
1034 $this->client
->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json');
1035 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1037 $content = json_decode($this->client
->getResponse()->getContent(), true);
1039 $this->assertNotEmpty($content['title']);
1041 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
1044 public function testPostEntriesTagsListAction()
1046 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1047 ->getRepository('WallabagCoreBundle:Entry')
1048 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1050 $tags = $entry->getTags();
1052 $this->assertCount(2, $tags);
1056 'url' => 'http://0.0.0.0/entry4',
1057 'tags' => 'new tag 1, new tag 2',
1061 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode($list));
1063 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1065 $content = json_decode($this->client
->getResponse()->getContent(), true);
1067 $this->assertInternalType('int', $content[0]['entry']);
1068 $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']);
1070 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1071 ->getRepository('WallabagCoreBundle:Entry')
1072 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1074 $tags = $entry->getTags();
1075 $this->assertCount(4, $tags);
1078 public function testPostEntriesTagsListActionNoList()
1080 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode([]));
1082 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1084 $content = json_decode($this->client
->getResponse()->getContent(), true);
1086 $this->assertEmpty($content);
1089 public function testDeleteEntriesTagsListAction()
1091 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1092 $entry = new Entry($em->getReference(User
::class, 1));
1093 $entry->setUrl('http://0.0.0.0/test-entry');
1094 $entry->addTag((new Tag())->setLabel('foo-tag'));
1095 $entry->addTag((new Tag())->setLabel('bar-tag'));
1096 $em->persist($entry);
1103 'url' => 'http://0.0.0.0/test-entry',
1104 'tags' => 'foo-tag, bar-tag',
1108 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list));
1109 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1111 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
1112 $this->assertCount(0, $entry->getTags());
1115 public function testDeleteEntriesTagsListActionNoList()
1117 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode([]));
1119 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1121 $content = json_decode($this->client
->getResponse()->getContent(), true);
1123 $this->assertEmpty($content);
1126 public function testPostEntriesListAction()
1129 'https://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
1130 'http://0.0.0.0/entry2',
1133 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1135 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1137 $content = json_decode($this->client
->getResponse()->getContent(), true);
1139 $this->assertInternalType('int', $content[0]['entry']);
1140 $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']);
1142 $this->assertInternalType('int', $content[1]['entry']);
1143 $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']);
1146 public function testPostEntriesListActionWithNoUrls()
1148 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode([]));
1150 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1152 $content = json_decode($this->client
->getResponse()->getContent(), true);
1154 $this->assertEmpty($content);
1157 public function testDeleteEntriesListAction()
1159 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1160 $em->persist((new Entry($em->getReference(User
::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
1165 'http://0.0.0.0/test-entry1',
1166 'http://0.0.0.0/test-entry-not-exist',
1169 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode($list));
1171 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1173 $content = json_decode($this->client
->getResponse()->getContent(), true);
1175 $this->assertTrue($content[0]['entry']);
1176 $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']);
1178 $this->assertFalse($content[1]['entry']);
1179 $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
1182 public function testDeleteEntriesListActionWithNoUrls()
1184 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode([]));
1186 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1188 $content = json_decode($this->client
->getResponse()->getContent(), true);
1190 $this->assertEmpty($content);
1193 public function testLimitBulkAction()
1196 'http://0.0.0.0/entry1',
1197 'http://0.0.0.0/entry1',
1198 'http://0.0.0.0/entry1',
1199 'http://0.0.0.0/entry1',
1200 'http://0.0.0.0/entry1',
1201 'http://0.0.0.0/entry1',
1202 'http://0.0.0.0/entry1',
1203 'http://0.0.0.0/entry1',
1204 'http://0.0.0.0/entry1',
1205 'http://0.0.0.0/entry1',
1206 'http://0.0.0.0/entry1',
1209 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1211 $this->assertSame(400, $this->client
->getResponse()->getStatusCode());
1212 $this->assertContains('API limit reached', $this->client
->getResponse()->getContent());
1215 public function testRePostEntryAndReUsePublishedAt()
1217 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1218 $entry = new Entry($em->getReference(User
::class, 1));
1219 $entry->setTitle('Antoine de Caunes : « Je veux avoir le droit de tâtonner »');
1220 $entry->setContent('hihi');
1221 $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');
1222 $entry->setPublishedAt(new \
DateTime('2017-06-26T07:46:02+0200'));
1223 $em->persist($entry);
1227 $this->client
->request('POST', '/api/entries.json', [
1228 'url' => 'https://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html',
1231 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1233 $content = json_decode($this->client
->getResponse()->getContent(), true);
1235 $this->assertGreaterThan(0, $content['id']);
1236 $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']);