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 testGetStarredEntries()
247 $this->client
->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']);
249 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
251 $content = json_decode($this->client
->getResponse()->getContent(), true);
253 $this->assertGreaterThanOrEqual(1, \
count($content));
254 $this->assertNotEmpty($content['_embedded']['items']);
255 $this->assertGreaterThanOrEqual(1, $content['total']);
256 $this->assertSame(1, $content['page']);
257 $this->assertGreaterThanOrEqual(1, $content['pages']);
259 $this->assertArrayHasKey('_links', $content);
260 $this->assertArrayHasKey('self', $content['_links']);
261 $this->assertArrayHasKey('first', $content['_links']);
262 $this->assertArrayHasKey('last', $content['_links']);
264 foreach (['self', 'first', 'last'] as $link) {
265 $this->assertArrayHasKey('href', $content['_links'][$link]);
266 $this->assertContains('starred=1', $content['_links'][$link]['href']);
267 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
270 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
273 public function testGetArchiveEntries()
275 $this->client
->request('GET', '/api/entries', ['archive' => 1]);
277 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
279 $content = json_decode($this->client
->getResponse()->getContent(), true);
281 $this->assertGreaterThanOrEqual(1, \
count($content));
282 $this->assertNotEmpty($content['_embedded']['items']);
283 $this->assertGreaterThanOrEqual(1, $content['total']);
284 $this->assertSame(1, $content['page']);
285 $this->assertGreaterThanOrEqual(1, $content['pages']);
287 $this->assertArrayHasKey('_links', $content);
288 $this->assertArrayHasKey('self', $content['_links']);
289 $this->assertArrayHasKey('first', $content['_links']);
290 $this->assertArrayHasKey('last', $content['_links']);
292 foreach (['self', 'first', 'last'] as $link) {
293 $this->assertArrayHasKey('href', $content['_links'][$link]);
294 $this->assertContains('archive=1', $content['_links'][$link]['href']);
297 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
300 public function testGetTaggedEntries()
302 $this->client
->request('GET', '/api/entries', ['tags' => 'foo,bar']);
304 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
306 $content = json_decode($this->client
->getResponse()->getContent(), true);
308 $this->assertGreaterThanOrEqual(1, \
count($content));
309 $this->assertNotEmpty($content['_embedded']['items']);
310 $this->assertGreaterThanOrEqual(1, $content['total']);
311 $this->assertSame(1, $content['page']);
312 $this->assertGreaterThanOrEqual(1, $content['pages']);
314 $this->assertContains('foo', array_column($content['_embedded']['items'][0]['tags'], 'label'), 'Entries tags should have "foo" tag');
315 $this->assertContains('bar', array_column($content['_embedded']['items'][0]['tags'], 'label'), 'Entries tags should have "bar" tag');
317 $this->assertArrayHasKey('_links', $content);
318 $this->assertArrayHasKey('self', $content['_links']);
319 $this->assertArrayHasKey('first', $content['_links']);
320 $this->assertArrayHasKey('last', $content['_links']);
322 foreach (['self', 'first', 'last'] as $link) {
323 $this->assertArrayHasKey('href', $content['_links'][$link]);
324 $this->assertContains('tags=' . urlencode('foo,bar'), $content['_links'][$link]['href']);
327 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
330 public function testGetTaggedEntriesWithBadParams()
332 $this->client
->request('GET', '/api/entries', ['tags' => ['foo', 'bar']]);
334 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
337 public function testGetDatedEntries()
339 $this->client
->request('GET', '/api/entries', ['since' => 1443274283]);
341 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
343 $content = json_decode($this->client
->getResponse()->getContent(), true);
345 $this->assertGreaterThanOrEqual(1, \
count($content));
346 $this->assertNotEmpty($content['_embedded']['items']);
347 $this->assertGreaterThanOrEqual(1, $content['total']);
348 $this->assertSame(1, $content['page']);
349 $this->assertGreaterThanOrEqual(1, $content['pages']);
351 $this->assertArrayHasKey('_links', $content);
352 $this->assertArrayHasKey('self', $content['_links']);
353 $this->assertArrayHasKey('first', $content['_links']);
354 $this->assertArrayHasKey('last', $content['_links']);
356 foreach (['self', 'first', 'last'] as $link) {
357 $this->assertArrayHasKey('href', $content['_links'][$link]);
358 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
361 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
364 public function testGetDatedSupEntries()
366 $future = new \
DateTime(date('Y-m-d H:i:s'));
367 $this->client
->request('GET', '/api/entries', ['since' => $future->getTimestamp() +
1000]);
369 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
371 $content = json_decode($this->client
->getResponse()->getContent(), true);
373 $this->assertGreaterThanOrEqual(1, \
count($content));
374 $this->assertEmpty($content['_embedded']['items']);
375 $this->assertSame(0, $content['total']);
376 $this->assertSame(1, $content['page']);
377 $this->assertSame(1, $content['pages']);
379 $this->assertArrayHasKey('_links', $content);
380 $this->assertArrayHasKey('self', $content['_links']);
381 $this->assertArrayHasKey('first', $content['_links']);
382 $this->assertArrayHasKey('last', $content['_links']);
384 foreach (['self', 'first', 'last'] as $link) {
385 $this->assertArrayHasKey('href', $content['_links'][$link]);
386 $this->assertContains('since=' . ($future->getTimestamp() +
1000), $content['_links'][$link]['href']);
389 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
392 public function testDeleteEntry()
394 $entry = $this->client
->getContainer()
395 ->get('doctrine.orm.entity_manager')
396 ->getRepository('WallabagCoreBundle:Entry')
397 ->findOneByUser($this->getUserId(), ['id' => 'asc']);
400 $this->markTestSkipped('No content found in db.');
403 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '.json');
405 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
407 $content = json_decode($this->client
->getResponse()->getContent(), true);
409 $this->assertSame($entry->getTitle(), $content['title']);
410 $this->assertSame($entry->getUrl(), $content['url']);
411 $this->assertSame($entry->getId(), $content['id']);
413 // We'll try to delete this entry again
414 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '.json');
416 $this->assertSame(404, $this->client
->getResponse()->getStatusCode());
419 public function testPostEntry()
421 $this->client
->request('POST', '/api/entries.json', [
422 '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',
424 'title' => 'New title for my article',
425 'content' => 'my content',
427 'published_at' => '2016-09-08T11:55:58+0200',
428 'authors' => 'bob,helen',
432 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
434 $content = json_decode($this->client
->getResponse()->getContent(), true);
436 $this->assertGreaterThan(0, $content['id']);
437 $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']);
438 $this->assertSame(0, $content['is_archived']);
439 $this->assertSame(0, $content['is_starred']);
440 $this->assertNull($content['starred_at']);
441 $this->assertNull($content['archived_at']);
442 $this->assertSame('New title for my article', $content['title']);
443 $this->assertSame($this->getUserId(), $content['user_id']);
444 $this->assertCount(2, $content['tags']);
445 $this->assertNull($content['origin_url']);
446 $this->assertSame('my content', $content['content']);
447 $this->assertSame('de', $content['language']);
448 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
449 $this->assertCount(2, $content['published_by']);
450 $this->assertContains('bob', $content['published_by']);
451 $this->assertContains('helen', $content['published_by']);
452 $this->assertTrue($content['is_public'], 'A public link has been generated for that entry');
455 public function testPostSameEntry()
457 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
458 $entry = new Entry($em->getReference(User
::class, $this->getUserId()));
459 $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');
460 $entry->setArchived(true);
461 $entry->addTag((new Tag())->setLabel('google'));
462 $entry->addTag((new Tag())->setLabel('apple'));
463 $em->persist($entry);
467 $this->client
->request('POST', '/api/entries.json', [
468 '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',
470 'tags' => 'google, apple',
473 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
475 $content = json_decode($this->client
->getResponse()->getContent(), true);
477 $this->assertGreaterThan(0, $content['id']);
478 $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']);
479 $this->assertSame(1, $content['is_archived']);
480 $this->assertSame(0, $content['is_starred']);
481 $this->assertCount(3, $content['tags']);
484 public function testPostEntryWhenFetchContentFails()
486 /** @var \Symfony\Component\DependencyInjection\Container $container */
487 $container = $this->client
->getContainer();
488 $contentProxy = $this->getMockBuilder(ContentProxy
::class)
489 ->disableOriginalConstructor()
490 ->setMethods(['updateEntry'])
492 $contentProxy->expects($this->any())
493 ->method('updateEntry')
494 ->willThrowException(new \
Exception('Test Fetch content fails'));
495 $container->set('wallabag_core.content_proxy', $contentProxy);
498 $this->client
->request('POST', '/api/entries.json', [
499 'url' => 'http://www.example.com/',
502 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
503 $content = json_decode($this->client
->getResponse()->getContent(), true);
504 $this->assertGreaterThan(0, $content['id']);
505 $this->assertSame('http://www.example.com/', $content['url']);
506 $this->assertSame('www.example.com', $content['domain_name']);
507 $this->assertSame('www.example.com', $content['title']);
509 // Remove the created entry to avoid side effects on other tests
510 if (isset($content['id'])) {
511 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
512 $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']);
519 public function testPostArchivedAndStarredEntry()
521 $now = new \
DateTime();
522 $this->client
->request('POST', '/api/entries.json', [
523 'url' => 'https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
528 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
530 $content = json_decode($this->client
->getResponse()->getContent(), true);
532 $this->assertGreaterThan(0, $content['id']);
533 $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']);
534 $this->assertSame(1, $content['is_archived']);
535 $this->assertSame(1, $content['is_starred']);
536 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
537 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['archived_at']))->getTimestamp());
538 $this->assertSame($this->getUserId(), $content['user_id']);
541 public function testPostArchivedAndStarredEntryWithoutQuotes()
543 $this->client
->request('POST', '/api/entries.json', [
544 'url' => 'https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
549 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
551 $content = json_decode($this->client
->getResponse()->getContent(), true);
553 $this->assertGreaterThan(0, $content['id']);
554 $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']);
555 $this->assertSame(0, $content['is_archived']);
556 $this->assertSame(1, $content['is_starred']);
559 public function testPostEntryWithOriginUrl()
561 $this->client
->request('POST', '/api/entries.json', [
562 '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',
564 'title' => 'New title for my article',
565 'content' => 'my content',
567 'published_at' => '2016-09-08T11:55:58+0200',
568 'authors' => 'bob,helen',
570 'origin_url' => 'http://mysource.tld',
573 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
575 $content = json_decode($this->client
->getResponse()->getContent(), true);
577 $this->assertGreaterThan(0, $content['id']);
578 $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']);
579 $this->assertSame('http://mysource.tld', $content['origin_url']);
582 public function testPatchEntry()
584 $entry = $this->client
->getContainer()
585 ->get('doctrine.orm.entity_manager')
586 ->getRepository('WallabagCoreBundle:Entry')
587 ->findOneByUser($this->getUserId());
590 $this->markTestSkipped('No content found in db.');
593 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
594 'title' => 'New awesome title',
595 'tags' => 'new tag ' . uniqid(),
598 'language' => 'de_AT',
599 'preview_picture' => 'http://preview.io/picture.jpg',
600 'authors' => 'bob,sponge',
601 'content' => 'awesome',
603 'published_at' => 1488833381,
606 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
608 $content = json_decode($this->client
->getResponse()->getContent(), true);
610 $this->assertSame($entry->getId(), $content['id']);
611 $this->assertSame($entry->getUrl(), $content['url']);
612 $this->assertSame('New awesome title', $content['title']);
613 $this->assertGreaterThanOrEqual(1, \
count($content['tags']), 'We force only one tag');
614 $this->assertSame($this->getUserId(), $content['user_id']);
615 $this->assertSame('de_AT', $content['language']);
616 $this->assertSame('http://preview.io/picture.jpg', $content['preview_picture']);
617 $this->assertContains('sponge', $content['published_by']);
618 $this->assertContains('bob', $content['published_by']);
619 $this->assertSame('awesome', $content['content']);
620 $this->assertFalse($content['is_public'], 'Entry is no more shared');
621 $this->assertContains('2017-03-06', $content['published_at']);
624 public function testPatchEntryWithoutQuotes()
626 $entry = $this->client
->getContainer()
627 ->get('doctrine.orm.entity_manager')
628 ->getRepository('WallabagCoreBundle:Entry')
629 ->findOneByUser($this->getUserId());
632 $this->markTestSkipped('No content found in db.');
635 $previousContent = $entry->getContent();
636 $previousLanguage = $entry->getLanguage();
638 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
639 'title' => 'New awesome title',
640 'tags' => 'new tag ' . uniqid(),
643 'authors' => ['bob', 'sponge'],
646 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
648 $content = json_decode($this->client
->getResponse()->getContent(), true);
650 $this->assertSame($entry->getId(), $content['id']);
651 $this->assertSame($entry->getUrl(), $content['url']);
652 $this->assertGreaterThanOrEqual(1, \
count($content['tags']), 'We force only one tag');
653 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
654 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
655 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
658 public function testPatchEntryWithOriginUrl()
660 $entry = $this->client
->getContainer()
661 ->get('doctrine.orm.entity_manager')
662 ->getRepository('WallabagCoreBundle:Entry')
663 ->findOneByUser($this->getUserId());
666 $this->markTestSkipped('No content found in db.');
669 $previousContent = $entry->getContent();
670 $previousLanguage = $entry->getLanguage();
672 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
673 'title' => 'Another awesome title just for profit',
674 'origin_url' => 'https://myawesomesource.example.com',
677 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
679 $content = json_decode($this->client
->getResponse()->getContent(), true);
681 $this->assertSame($entry->getId(), $content['id']);
682 $this->assertSame($entry->getUrl(), $content['url']);
683 $this->assertSame('https://myawesomesource.example.com', $content['origin_url']);
684 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
685 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
686 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
689 public function testPatchEntryRemoveOriginUrl()
691 $entry = $this->client
->getContainer()
692 ->get('doctrine.orm.entity_manager')
693 ->getRepository('WallabagCoreBundle:Entry')
694 ->findOneByUser($this->getUserId());
697 $this->markTestSkipped('No content found in db.');
700 $previousContent = $entry->getContent();
701 $previousLanguage = $entry->getLanguage();
702 $previousTitle = $entry->getTitle();
704 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
708 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
710 $content = json_decode($this->client
->getResponse()->getContent(), true);
712 $this->assertSame($entry->getId(), $content['id']);
713 $this->assertSame($entry->getUrl(), $content['url']);
714 $this->assertEmpty($content['origin_url']);
715 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
716 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
717 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
718 $this->assertSame($previousTitle, $content['title'], 'Ensure title has not moved');
721 public function testPatchEntryNullOriginUrl()
723 $entry = $this->client
->getContainer()
724 ->get('doctrine.orm.entity_manager')
725 ->getRepository('WallabagCoreBundle:Entry')
726 ->findOneByUser($this->getUserId());
729 $this->markTestSkipped('No content found in db.');
732 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
733 'origin_url' => null,
736 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
738 $content = json_decode($this->client
->getResponse()->getContent(), true);
740 $this->assertNull($content['origin_url']);
743 public function testGetTagsEntry()
745 $entry = $this->client
->getContainer()
746 ->get('doctrine.orm.entity_manager')
747 ->getRepository('WallabagCoreBundle:Entry')
748 ->findOneWithTags($this->user
->getId());
753 $this->markTestSkipped('No content found in db.');
757 foreach ($entry->getTags() as $tag) {
758 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
761 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '/tags');
763 $this->assertSame(json_encode($tags, JSON_HEX_QUOT
), $this->client
->getResponse()->getContent());
766 public function testPostTagsOnEntry()
768 $entry = $this->client
->getContainer()
769 ->get('doctrine.orm.entity_manager')
770 ->getRepository('WallabagCoreBundle:Entry')
771 ->findOneByUser($this->getUserId());
774 $this->markTestSkipped('No content found in db.');
777 $nbTags = \
count($entry->getTags());
779 $newTags = 'tag1,tag2,tag3';
781 $this->client
->request('POST', '/api/entries/' . $entry->getId() . '/tags', ['tags' => $newTags]);
783 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
785 $content = json_decode($this->client
->getResponse()->getContent(), true);
787 $this->assertArrayHasKey('tags', $content);
788 $this->assertSame($nbTags +
3, \
count($content['tags']));
790 $entryDB = $this->client
->getContainer()
791 ->get('doctrine.orm.entity_manager')
792 ->getRepository('WallabagCoreBundle:Entry')
793 ->find($entry->getId());
796 foreach ($entryDB->getTags()->toArray() as $tag) {
797 $tagsInDB[$tag->getId()] = $tag->getLabel();
800 foreach (explode(',', $newTags) as $tag) {
801 $this->assertContains($tag, $tagsInDB);
805 public function testDeleteOneTagEntry()
807 $entry = $this->client
->getContainer()
808 ->get('doctrine.orm.entity_manager')
809 ->getRepository('WallabagCoreBundle:Entry')
810 ->findOneWithTags($this->user
->getId());
814 $this->markTestSkipped('No content found in db.');
817 // hydrate the tags relations
818 $nbTags = \
count($entry->getTags());
819 $tag = $entry->getTags()[0];
821 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '/tags/' . $tag->getId() . '.json');
823 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
825 $content = json_decode($this->client
->getResponse()->getContent(), true);
827 $this->assertArrayHasKey('tags', $content);
828 $this->assertSame($nbTags - 1, \
count($content['tags']));
831 public function testSaveIsArchivedAfterPost()
833 $entry = $this->client
->getContainer()
834 ->get('doctrine.orm.entity_manager')
835 ->getRepository('WallabagCoreBundle:Entry')
836 ->findOneBy(['user' => $this->getUserId(), 'isArchived' => true]);
839 $this->markTestSkipped('No content found in db.');
842 $this->client
->request('POST', '/api/entries.json', [
843 'url' => $entry->getUrl(),
846 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
848 $content = json_decode($this->client
->getResponse()->getContent(), true);
850 $this->assertSame(1, $content['is_archived']);
853 public function testSaveIsStarredAfterPost()
855 $entry = $this->client
->getContainer()
856 ->get('doctrine.orm.entity_manager')
857 ->getRepository('WallabagCoreBundle:Entry')
858 ->findOneBy(['user' => $this->getUserId(), 'isStarred' => true]);
861 $this->markTestSkipped('No content found in db.');
864 $this->client
->request('POST', '/api/entries.json', [
865 'url' => $entry->getUrl(),
868 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
870 $content = json_decode($this->client
->getResponse()->getContent(), true);
872 $this->assertSame(1, $content['is_starred']);
875 public function testSaveIsArchivedAfterPatch()
877 $entry = $this->client
->getContainer()
878 ->get('doctrine.orm.entity_manager')
879 ->getRepository('WallabagCoreBundle:Entry')
880 ->findOneBy(['user' => $this->getUserId(), 'isArchived' => true]);
883 $this->markTestSkipped('No content found in db.');
886 $previousTitle = $entry->getTitle();
888 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
889 'title' => $entry->getTitle() . '++',
892 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
894 $content = json_decode($this->client
->getResponse()->getContent(), true);
896 $this->assertSame(1, $content['is_archived']);
897 $this->assertSame($previousTitle . '++', $content['title']);
900 public function testSaveIsStarredAfterPatch()
902 $now = new \
DateTime();
903 $entry = $this->client
->getContainer()
904 ->get('doctrine.orm.entity_manager')
905 ->getRepository('WallabagCoreBundle:Entry')
906 ->findOneBy(['user' => $this->getUserId(), 'isStarred' => true]);
909 $this->markTestSkipped('No content found in db.');
911 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
912 'title' => $entry->getTitle() . '++',
915 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
917 $content = json_decode($this->client
->getResponse()->getContent(), true);
919 $this->assertSame(1, $content['is_starred']);
920 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
923 public function testGetEntriesExistsWithReturnId()
925 $this->client
->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1');
927 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
929 $content = json_decode($this->client
->getResponse()->getContent(), true);
931 // it returns a database id, we don't know it, so we only check it's greater than the lowest possible value
932 $this->assertGreaterThan(1, $content['exists']);
935 public function testGetEntriesExistsWithoutReturnId()
937 $this->client
->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2');
939 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
941 $content = json_decode($this->client
->getResponse()->getContent(), true);
943 $this->assertTrue($content['exists']);
946 public function testGetEntriesExistsWithManyUrls()
948 $url1 = 'http://0.0.0.0/entry2';
949 $url2 = 'http://0.0.0.0/entry10';
950 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1');
952 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
954 $content = json_decode($this->client
->getResponse()->getContent(), true);
956 $this->assertArrayHasKey($url1, $content);
957 $this->assertArrayHasKey($url2, $content);
958 // it returns a database id, we don't know it, so we only check it's greater than the lowest possible value
959 $this->assertGreaterThan(1, $content[$url1]);
960 $this->assertNull($content[$url2]);
963 public function testGetEntriesExistsWithManyUrlsReturnBool()
965 $url1 = 'http://0.0.0.0/entry2';
966 $url2 = 'http://0.0.0.0/entry10';
967 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2);
969 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
971 $content = json_decode($this->client
->getResponse()->getContent(), true);
973 $this->assertArrayHasKey($url1, $content);
974 $this->assertArrayHasKey($url2, $content);
975 $this->assertTrue($content[$url1]);
976 $this->assertFalse($content[$url2]);
979 public function testGetEntriesExistsWhichDoesNotExists()
981 $this->client
->request('GET', '/api/entries/exists?url=http://google.com/entry2');
983 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
985 $content = json_decode($this->client
->getResponse()->getContent(), true);
987 $this->assertFalse($content['exists']);
990 public function testGetEntriesExistsWithNoUrl()
992 $this->client
->request('GET', '/api/entries/exists?url=');
994 $this->assertSame(403, $this->client
->getResponse()->getStatusCode());
997 public function testReloadEntryErrorWhileFetching()
999 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1000 ->getRepository('WallabagCoreBundle:Entry')
1001 ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getUserId());
1004 $this->markTestSkipped('No content found in db.');
1007 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json');
1008 $this->assertSame(304, $this->client
->getResponse()->getStatusCode());
1011 public function testReloadEntry()
1013 $this->client
->request('POST', '/api/entries.json', [
1014 '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',
1016 'tags' => 'google, apple',
1019 $json = json_decode($this->client
->getResponse()->getContent(), true);
1023 $this->client
->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json');
1024 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1026 $content = json_decode($this->client
->getResponse()->getContent(), true);
1028 $this->assertNotEmpty($content['title']);
1030 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
1033 public function testPostEntriesTagsListAction()
1035 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1036 ->getRepository('WallabagCoreBundle:Entry')
1037 ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getUserId());
1039 $tags = $entry->getTags();
1041 $this->assertCount(2, $tags);
1045 'url' => 'http://0.0.0.0/entry4',
1046 'tags' => 'new tag 1, new tag 2',
1050 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode($list));
1052 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1054 $content = json_decode($this->client
->getResponse()->getContent(), true);
1056 $this->assertInternalType('int', $content[0]['entry']);
1057 $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']);
1059 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1060 ->getRepository('WallabagCoreBundle:Entry')
1061 ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getUserId());
1063 $tags = $entry->getTags();
1064 $this->assertCount(4, $tags);
1067 public function testPostEntriesTagsListActionNoList()
1069 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode([]));
1071 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1073 $content = json_decode($this->client
->getResponse()->getContent(), true);
1075 $this->assertEmpty($content);
1078 public function testDeleteEntriesTagsListAction()
1080 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1081 $entry = new Entry($em->getReference(User
::class, $this->getUserId()));
1082 $entry->setUrl('http://0.0.0.0/test-entry');
1083 $entry->addTag((new Tag())->setLabel('foo-tag'));
1084 $entry->addTag((new Tag())->setLabel('bar-tag'));
1085 $em->persist($entry);
1092 'url' => 'http://0.0.0.0/test-entry',
1093 'tags' => 'foo-tag, bar-tag',
1097 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list));
1098 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1100 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
1101 $this->assertCount(0, $entry->getTags());
1104 public function testDeleteEntriesTagsListActionNoList()
1106 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode([]));
1108 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1110 $content = json_decode($this->client
->getResponse()->getContent(), true);
1112 $this->assertEmpty($content);
1115 public function testPostEntriesListAction()
1118 'https://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
1119 'http://0.0.0.0/entry2',
1122 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1124 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1126 $content = json_decode($this->client
->getResponse()->getContent(), true);
1128 $this->assertInternalType('int', $content[0]['entry']);
1129 $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']);
1131 $this->assertInternalType('int', $content[1]['entry']);
1132 $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']);
1135 public function testPostEntriesListActionWithNoUrls()
1137 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode([]));
1139 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1141 $content = json_decode($this->client
->getResponse()->getContent(), true);
1143 $this->assertEmpty($content);
1146 public function testDeleteEntriesListAction()
1148 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1149 $em->persist((new Entry($em->getReference(User
::class, $this->getUserId())))->setUrl('http://0.0.0.0/test-entry1'));
1154 'http://0.0.0.0/test-entry1',
1155 'http://0.0.0.0/test-entry-not-exist',
1158 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode($list));
1160 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1162 $content = json_decode($this->client
->getResponse()->getContent(), true);
1164 $this->assertTrue($content[0]['entry']);
1165 $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']);
1167 $this->assertFalse($content[1]['entry']);
1168 $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
1171 public function testDeleteEntriesListActionWithNoUrls()
1173 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode([]));
1175 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1177 $content = json_decode($this->client
->getResponse()->getContent(), true);
1179 $this->assertEmpty($content);
1182 public function testLimitBulkAction()
1185 'http://0.0.0.0/entry1',
1186 'http://0.0.0.0/entry1',
1187 'http://0.0.0.0/entry1',
1188 'http://0.0.0.0/entry1',
1189 'http://0.0.0.0/entry1',
1190 'http://0.0.0.0/entry1',
1191 'http://0.0.0.0/entry1',
1192 'http://0.0.0.0/entry1',
1193 'http://0.0.0.0/entry1',
1194 'http://0.0.0.0/entry1',
1195 'http://0.0.0.0/entry1',
1198 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1200 $this->assertSame(400, $this->client
->getResponse()->getStatusCode());
1201 $this->assertContains('API limit reached', $this->client
->getResponse()->getContent());
1204 public function testRePostEntryAndReUsePublishedAt()
1206 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1207 $entry = new Entry($em->getReference(User
::class, $this->getUserId()));
1208 $entry->setTitle('Antoine de Caunes : « Je veux avoir le droit de tâtonner »');
1209 $entry->setContent('hihi');
1210 $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');
1211 $entry->setPublishedAt(new \
DateTime('2017-06-26T07:46:02+0200'));
1212 $em->persist($entry);
1216 $this->client
->request('POST', '/api/entries.json', [
1217 'url' => 'https://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html',
1220 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1222 $content = json_decode($this->client
->getResponse()->getContent(), true);
1224 $this->assertGreaterThan(0, $content['id']);
1225 $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']);