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' => $this->getUserId(), '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' => $this->getUserId(), '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' => $this->getUserId(), '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' => $this->getUserId('bob'), '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')
188 ->findOneByUser($this->getUserId());
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($this->getUserId(), ['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->assertNull($content['archived_at']);
451 $this->assertSame('New title for my article', $content['title']);
452 $this->assertSame($this->getUserId(), $content['user_id']);
453 $this->assertCount(2, $content['tags']);
454 $this->assertNull($content['origin_url']);
455 $this->assertSame('my content', $content['content']);
456 $this->assertSame('de', $content['language']);
457 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
458 $this->assertCount(2, $content['published_by']);
459 $this->assertContains('bob', $content['published_by']);
460 $this->assertContains('helen', $content['published_by']);
461 $this->assertTrue($content['is_public'], 'A public link has been generated for that entry');
464 public function testPostSameEntry()
466 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
467 $entry = new Entry($em->getReference(User
::class, $this->getUserId()));
468 $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');
469 $entry->setArchived(true);
470 $entry->addTag((new Tag())->setLabel('google'));
471 $entry->addTag((new Tag())->setLabel('apple'));
472 $em->persist($entry);
476 $this->client
->request('POST', '/api/entries.json', [
477 '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',
479 'tags' => 'google, apple',
482 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
484 $content = json_decode($this->client
->getResponse()->getContent(), true);
486 $this->assertGreaterThan(0, $content['id']);
487 $this->assertSame('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']);
488 $this->assertSame(1, $content['is_archived']);
489 $this->assertSame(0, $content['is_starred']);
490 $this->assertCount(3, $content['tags']);
493 public function testPostEntryWhenFetchContentFails()
495 /** @var \Symfony\Component\DependencyInjection\Container $container */
496 $container = $this->client
->getContainer();
497 $contentProxy = $this->getMockBuilder(ContentProxy
::class)
498 ->disableOriginalConstructor()
499 ->setMethods(['updateEntry'])
501 $contentProxy->expects($this->any())
502 ->method('updateEntry')
503 ->willThrowException(new \
Exception('Test Fetch content fails'));
504 $container->set('wallabag_core.content_proxy', $contentProxy);
507 $this->client
->request('POST', '/api/entries.json', [
508 'url' => 'http://www.example.com/',
511 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
512 $content = json_decode($this->client
->getResponse()->getContent(), true);
513 $this->assertGreaterThan(0, $content['id']);
514 $this->assertSame('http://www.example.com/', $content['url']);
515 $this->assertSame('www.example.com', $content['domain_name']);
516 $this->assertSame('www.example.com', $content['title']);
518 // Remove the created entry to avoid side effects on other tests
519 if (isset($content['id'])) {
520 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
521 $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']);
528 public function testPostArchivedAndStarredEntry()
530 $now = new \
DateTime();
531 $this->client
->request('POST', '/api/entries.json', [
532 'url' => 'https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
537 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
539 $content = json_decode($this->client
->getResponse()->getContent(), true);
541 $this->assertGreaterThan(0, $content['id']);
542 $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']);
543 $this->assertSame(1, $content['is_archived']);
544 $this->assertSame(1, $content['is_starred']);
545 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
546 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['archived_at']))->getTimestamp());
547 $this->assertSame($this->getUserId(), $content['user_id']);
550 public function testPostArchivedAndStarredEntryWithoutQuotes()
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(0, $content['is_archived']);
565 $this->assertSame(1, $content['is_starred']);
568 public function testPostEntryWithOriginUrl()
570 $this->client
->request('POST', '/api/entries.json', [
571 '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',
573 'title' => 'New title for my article',
574 'content' => 'my content',
576 'published_at' => '2016-09-08T11:55:58+0200',
577 'authors' => 'bob,helen',
579 'origin_url' => 'http://mysource.tld',
582 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
584 $content = json_decode($this->client
->getResponse()->getContent(), true);
586 $this->assertGreaterThan(0, $content['id']);
587 $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']);
588 $this->assertSame('http://mysource.tld', $content['origin_url']);
591 public function testPatchEntry()
593 $entry = $this->client
->getContainer()
594 ->get('doctrine.orm.entity_manager')
595 ->getRepository('WallabagCoreBundle:Entry')
596 ->findOneByUser($this->getUserId());
599 $this->markTestSkipped('No content found in db.');
602 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
603 'title' => 'New awesome title',
604 'tags' => 'new tag ' . uniqid(),
607 'language' => 'de_AT',
608 'preview_picture' => 'http://preview.io/picture.jpg',
609 'authors' => 'bob,sponge',
610 'content' => 'awesome',
612 'published_at' => 1488833381,
615 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
617 $content = json_decode($this->client
->getResponse()->getContent(), true);
619 $this->assertSame($entry->getId(), $content['id']);
620 $this->assertSame($entry->getUrl(), $content['url']);
621 $this->assertSame('New awesome title', $content['title']);
622 $this->assertGreaterThanOrEqual(1, \
count($content['tags']), 'We force only one tag');
623 $this->assertSame($this->getUserId(), $content['user_id']);
624 $this->assertSame('de_AT', $content['language']);
625 $this->assertSame('http://preview.io/picture.jpg', $content['preview_picture']);
626 $this->assertContains('sponge', $content['published_by']);
627 $this->assertContains('bob', $content['published_by']);
628 $this->assertSame('awesome', $content['content']);
629 $this->assertFalse($content['is_public'], 'Entry is no more shared');
630 $this->assertContains('2017-03-06', $content['published_at']);
633 public function testPatchEntryWithoutQuotes()
635 $entry = $this->client
->getContainer()
636 ->get('doctrine.orm.entity_manager')
637 ->getRepository('WallabagCoreBundle:Entry')
638 ->findOneByUser($this->getUserId());
641 $this->markTestSkipped('No content found in db.');
644 $previousContent = $entry->getContent();
645 $previousLanguage = $entry->getLanguage();
647 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
648 'title' => 'New awesome title',
649 'tags' => 'new tag ' . uniqid(),
652 'authors' => ['bob', 'sponge'],
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->assertGreaterThanOrEqual(1, \
count($content['tags']), 'We force only one tag');
662 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
663 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
664 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
667 public function testPatchEntryWithOriginUrl()
669 $entry = $this->client
->getContainer()
670 ->get('doctrine.orm.entity_manager')
671 ->getRepository('WallabagCoreBundle:Entry')
672 ->findOneByUser($this->getUserId());
675 $this->markTestSkipped('No content found in db.');
678 $previousContent = $entry->getContent();
679 $previousLanguage = $entry->getLanguage();
681 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
682 'title' => 'Another awesome title just for profit',
683 'origin_url' => 'https://myawesomesource.example.com',
686 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
688 $content = json_decode($this->client
->getResponse()->getContent(), true);
690 $this->assertSame($entry->getId(), $content['id']);
691 $this->assertSame($entry->getUrl(), $content['url']);
692 $this->assertSame('https://myawesomesource.example.com', $content['origin_url']);
693 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
694 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
695 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
698 public function testPatchEntryRemoveOriginUrl()
700 $entry = $this->client
->getContainer()
701 ->get('doctrine.orm.entity_manager')
702 ->getRepository('WallabagCoreBundle:Entry')
703 ->findOneByUser($this->getUserId());
706 $this->markTestSkipped('No content found in db.');
709 $previousContent = $entry->getContent();
710 $previousLanguage = $entry->getLanguage();
711 $previousTitle = $entry->getTitle();
713 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
717 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
719 $content = json_decode($this->client
->getResponse()->getContent(), true);
721 $this->assertSame($entry->getId(), $content['id']);
722 $this->assertSame($entry->getUrl(), $content['url']);
723 $this->assertEmpty($content['origin_url']);
724 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
725 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
726 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
727 $this->assertSame($previousTitle, $content['title'], 'Ensure title has not moved');
730 public function testPatchEntryNullOriginUrl()
732 $entry = $this->client
->getContainer()
733 ->get('doctrine.orm.entity_manager')
734 ->getRepository('WallabagCoreBundle:Entry')
735 ->findOneByUser($this->getUserId());
738 $this->markTestSkipped('No content found in db.');
741 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
742 'origin_url' => null,
745 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
747 $content = json_decode($this->client
->getResponse()->getContent(), true);
749 $this->assertNull($content['origin_url']);
752 public function testGetTagsEntry()
754 $entry = $this->client
->getContainer()
755 ->get('doctrine.orm.entity_manager')
756 ->getRepository('WallabagCoreBundle:Entry')
757 ->findOneWithTags($this->user
->getId());
762 $this->markTestSkipped('No content found in db.');
766 foreach ($entry->getTags() as $tag) {
767 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
770 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '/tags');
772 $this->assertSame(json_encode($tags, JSON_HEX_QUOT
), $this->client
->getResponse()->getContent());
775 public function testPostTagsOnEntry()
777 $entry = $this->client
->getContainer()
778 ->get('doctrine.orm.entity_manager')
779 ->getRepository('WallabagCoreBundle:Entry')
780 ->findOneByUser($this->getUserId());
783 $this->markTestSkipped('No content found in db.');
786 $nbTags = \
count($entry->getTags());
788 $newTags = 'tag1,tag2,tag3';
790 $this->client
->request('POST', '/api/entries/' . $entry->getId() . '/tags', ['tags' => $newTags]);
792 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
794 $content = json_decode($this->client
->getResponse()->getContent(), true);
796 $this->assertArrayHasKey('tags', $content);
797 $this->assertSame($nbTags +
3, \
count($content['tags']));
799 $entryDB = $this->client
->getContainer()
800 ->get('doctrine.orm.entity_manager')
801 ->getRepository('WallabagCoreBundle:Entry')
802 ->find($entry->getId());
805 foreach ($entryDB->getTags()->toArray() as $tag) {
806 $tagsInDB[$tag->getId()] = $tag->getLabel();
809 foreach (explode(',', $newTags) as $tag) {
810 $this->assertContains($tag, $tagsInDB);
814 public function testDeleteOneTagEntry()
816 $entry = $this->client
->getContainer()
817 ->get('doctrine.orm.entity_manager')
818 ->getRepository('WallabagCoreBundle:Entry')
819 ->findOneWithTags($this->user
->getId());
823 $this->markTestSkipped('No content found in db.');
826 // hydrate the tags relations
827 $nbTags = \
count($entry->getTags());
828 $tag = $entry->getTags()[0];
830 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '/tags/' . $tag->getId() . '.json');
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 - 1, \
count($content['tags']));
840 public function testSaveIsArchivedAfterPost()
842 $entry = $this->client
->getContainer()
843 ->get('doctrine.orm.entity_manager')
844 ->getRepository('WallabagCoreBundle:Entry')
845 ->findOneBy(['user' => $this->getUserId(), 'isArchived' => true]);
848 $this->markTestSkipped('No content found in db.');
851 $this->client
->request('POST', '/api/entries.json', [
852 'url' => $entry->getUrl(),
855 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
857 $content = json_decode($this->client
->getResponse()->getContent(), true);
859 $this->assertSame(1, $content['is_archived']);
862 public function testSaveIsStarredAfterPost()
864 $entry = $this->client
->getContainer()
865 ->get('doctrine.orm.entity_manager')
866 ->getRepository('WallabagCoreBundle:Entry')
867 ->findOneBy(['user' => $this->getUserId(), 'isStarred' => true]);
870 $this->markTestSkipped('No content found in db.');
873 $this->client
->request('POST', '/api/entries.json', [
874 'url' => $entry->getUrl(),
877 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
879 $content = json_decode($this->client
->getResponse()->getContent(), true);
881 $this->assertSame(1, $content['is_starred']);
884 public function testSaveIsArchivedAfterPatch()
886 $entry = $this->client
->getContainer()
887 ->get('doctrine.orm.entity_manager')
888 ->getRepository('WallabagCoreBundle:Entry')
889 ->findOneBy(['user' => $this->getUserId(), 'isArchived' => true]);
892 $this->markTestSkipped('No content found in db.');
895 $previousTitle = $entry->getTitle();
897 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
898 'title' => $entry->getTitle() . '++',
901 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
903 $content = json_decode($this->client
->getResponse()->getContent(), true);
905 $this->assertSame(1, $content['is_archived']);
906 $this->assertSame($previousTitle . '++', $content['title']);
909 public function testSaveIsStarredAfterPatch()
911 $now = new \
DateTime();
912 $entry = $this->client
->getContainer()
913 ->get('doctrine.orm.entity_manager')
914 ->getRepository('WallabagCoreBundle:Entry')
915 ->findOneBy(['user' => $this->getUserId(), 'isStarred' => true]);
918 $this->markTestSkipped('No content found in db.');
920 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
921 'title' => $entry->getTitle() . '++',
924 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
926 $content = json_decode($this->client
->getResponse()->getContent(), true);
928 $this->assertSame(1, $content['is_starred']);
929 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
932 public function testGetEntriesExistsWithReturnId()
934 $this->client
->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1');
936 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
938 $content = json_decode($this->client
->getResponse()->getContent(), true);
940 // it returns a database id, we don't know it, so we only check it's greater than the lowest possible value
941 $this->assertGreaterThan(1, $content['exists']);
944 public function testGetEntriesExistsWithoutReturnId()
946 $this->client
->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2');
948 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
950 $content = json_decode($this->client
->getResponse()->getContent(), true);
952 $this->assertTrue($content['exists']);
955 public function testGetEntriesExistsWithManyUrls()
957 $url1 = 'http://0.0.0.0/entry2';
958 $url2 = 'http://0.0.0.0/entry10';
959 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1');
961 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
963 $content = json_decode($this->client
->getResponse()->getContent(), true);
965 $this->assertArrayHasKey($url1, $content);
966 $this->assertArrayHasKey($url2, $content);
967 // it returns a database id, we don't know it, so we only check it's greater than the lowest possible value
968 $this->assertGreaterThan(1, $content[$url1]);
969 $this->assertNull($content[$url2]);
972 public function testGetEntriesExistsWithManyUrlsReturnBool()
974 $url1 = 'http://0.0.0.0/entry2';
975 $url2 = 'http://0.0.0.0/entry10';
976 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2);
978 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
980 $content = json_decode($this->client
->getResponse()->getContent(), true);
982 $this->assertArrayHasKey($url1, $content);
983 $this->assertArrayHasKey($url2, $content);
984 $this->assertTrue($content[$url1]);
985 $this->assertFalse($content[$url2]);
988 public function testGetEntriesExistsWhichDoesNotExists()
990 $this->client
->request('GET', '/api/entries/exists?url=http://google.com/entry2');
992 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
994 $content = json_decode($this->client
->getResponse()->getContent(), true);
996 $this->assertFalse($content['exists']);
999 public function testGetEntriesExistsWithNoUrl()
1001 $this->client
->request('GET', '/api/entries/exists?url=');
1003 $this->assertSame(403, $this->client
->getResponse()->getStatusCode());
1006 public function testReloadEntryErrorWhileFetching()
1008 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1009 ->getRepository('WallabagCoreBundle:Entry')
1010 ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getUserId());
1013 $this->markTestSkipped('No content found in db.');
1016 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json');
1017 $this->assertSame(304, $this->client
->getResponse()->getStatusCode());
1020 public function testReloadEntry()
1022 $this->client
->request('POST', '/api/entries.json', [
1023 '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',
1025 'tags' => 'google, apple',
1028 $json = json_decode($this->client
->getResponse()->getContent(), true);
1032 $this->client
->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json');
1033 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1035 $content = json_decode($this->client
->getResponse()->getContent(), true);
1037 $this->assertNotEmpty($content['title']);
1039 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
1042 public function testPostEntriesTagsListAction()
1044 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1045 ->getRepository('WallabagCoreBundle:Entry')
1046 ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getUserId());
1048 $tags = $entry->getTags();
1050 $this->assertCount(2, $tags);
1054 'url' => 'http://0.0.0.0/entry4',
1055 'tags' => 'new tag 1, new tag 2',
1059 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode($list));
1061 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1063 $content = json_decode($this->client
->getResponse()->getContent(), true);
1065 $this->assertInternalType('int', $content[0]['entry']);
1066 $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']);
1068 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1069 ->getRepository('WallabagCoreBundle:Entry')
1070 ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getUserId());
1072 $tags = $entry->getTags();
1073 $this->assertCount(4, $tags);
1076 public function testPostEntriesTagsListActionNoList()
1078 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode([]));
1080 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1082 $content = json_decode($this->client
->getResponse()->getContent(), true);
1084 $this->assertEmpty($content);
1087 public function testDeleteEntriesTagsListAction()
1089 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1090 $entry = new Entry($em->getReference(User
::class, $this->getUserId()));
1091 $entry->setUrl('http://0.0.0.0/test-entry');
1092 $entry->addTag((new Tag())->setLabel('foo-tag'));
1093 $entry->addTag((new Tag())->setLabel('bar-tag'));
1094 $em->persist($entry);
1101 'url' => 'http://0.0.0.0/test-entry',
1102 'tags' => 'foo-tag, bar-tag',
1106 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list));
1107 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1109 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
1110 $this->assertCount(0, $entry->getTags());
1113 public function testDeleteEntriesTagsListActionNoList()
1115 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode([]));
1117 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1119 $content = json_decode($this->client
->getResponse()->getContent(), true);
1121 $this->assertEmpty($content);
1124 public function testPostEntriesListAction()
1127 'https://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
1128 'http://0.0.0.0/entry2',
1131 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1133 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1135 $content = json_decode($this->client
->getResponse()->getContent(), true);
1137 $this->assertInternalType('int', $content[0]['entry']);
1138 $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']);
1140 $this->assertInternalType('int', $content[1]['entry']);
1141 $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']);
1144 public function testPostEntriesListActionWithNoUrls()
1146 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode([]));
1148 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1150 $content = json_decode($this->client
->getResponse()->getContent(), true);
1152 $this->assertEmpty($content);
1155 public function testDeleteEntriesListAction()
1157 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1158 $em->persist((new Entry($em->getReference(User
::class, $this->getUserId())))->setUrl('http://0.0.0.0/test-entry1'));
1163 'http://0.0.0.0/test-entry1',
1164 'http://0.0.0.0/test-entry-not-exist',
1167 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode($list));
1169 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1171 $content = json_decode($this->client
->getResponse()->getContent(), true);
1173 $this->assertTrue($content[0]['entry']);
1174 $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']);
1176 $this->assertFalse($content[1]['entry']);
1177 $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
1180 public function testDeleteEntriesListActionWithNoUrls()
1182 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode([]));
1184 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1186 $content = json_decode($this->client
->getResponse()->getContent(), true);
1188 $this->assertEmpty($content);
1191 public function testLimitBulkAction()
1194 'http://0.0.0.0/entry1',
1195 'http://0.0.0.0/entry1',
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',
1207 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1209 $this->assertSame(400, $this->client
->getResponse()->getStatusCode());
1210 $this->assertContains('API limit reached', $this->client
->getResponse()->getContent());
1213 public function testRePostEntryAndReUsePublishedAt()
1215 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1216 $entry = new Entry($em->getReference(User
::class, $this->getUserId()));
1217 $entry->setTitle('Antoine de Caunes : « Je veux avoir le droit de tâtonner »');
1218 $entry->setContent('hihi');
1219 $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');
1220 $entry->setPublishedAt(new \
DateTime('2017-06-26T07:46:02+0200'));
1221 $em->persist($entry);
1225 $this->client
->request('POST', '/api/entries.json', [
1226 'url' => 'https://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html',
1229 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1231 $content = json_decode($this->client
->getResponse()->getContent(), true);
1233 $this->assertGreaterThan(0, $content['id']);
1234 $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']);