3 namespace Tests\Wallabag\ApiBundle\Controller
;
5 use Tests\Wallabag\ApiBundle\WallabagApiTestCase
;
6 use Wallabag\CoreBundle\Entity\Entry
;
7 use Wallabag\CoreBundle\Entity\Tag
;
8 use Wallabag\CoreBundle\Helper\ContentProxy
;
9 use Wallabag\UserBundle\Entity\User
;
11 class EntryRestControllerTest
extends WallabagApiTestCase
13 public function testGetOneEntry()
15 $entry = $this->client
->getContainer()
16 ->get('doctrine.orm.entity_manager')
17 ->getRepository('WallabagCoreBundle:Entry')
18 ->findOneBy(['user' => 1, 'isArchived' => false]);
21 $this->markTestSkipped('No content found in db.');
24 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '.json');
25 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
27 $content = json_decode($this->client
->getResponse()->getContent(), true);
29 $this->assertSame($entry->getTitle(), $content['title']);
30 $this->assertSame($entry->getUrl(), $content['url']);
31 $this->assertCount(\
count($entry->getTags()), $content['tags']);
32 $this->assertSame($entry->getUserName(), $content['user_name']);
33 $this->assertSame($entry->getUserEmail(), $content['user_email']);
34 $this->assertSame($entry->getUserId(), $content['user_id']);
36 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
39 public function testGetOneEntryWithOriginUrl()
41 $entry = $this->client
->getContainer()
42 ->get('doctrine.orm.entity_manager')
43 ->getRepository('WallabagCoreBundle:Entry')
44 ->findOneBy(['user' => 1, 'url' => 'http://0.0.0.0/entry2']);
47 $this->markTestSkipped('No content found in db.');
50 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '.json');
51 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
53 $content = json_decode($this->client
->getResponse()->getContent(), true);
55 $this->assertSame($entry->getOriginUrl(), $content['origin_url']);
58 public function testExportEntry()
60 $entry = $this->client
->getContainer()
61 ->get('doctrine.orm.entity_manager')
62 ->getRepository('WallabagCoreBundle:Entry')
63 ->findOneBy(['user' => 1, 'isArchived' => false]);
66 $this->markTestSkipped('No content found in db.');
69 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '/export.epub');
70 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
72 // epub format got the content type in the content
73 $this->assertContains('application/epub', $this->client
->getResponse()->getContent());
74 $this->assertSame('application/epub+zip', $this->client
->getResponse()->headers
->get('Content-Type'));
76 // re-auth client for mobi
77 $client = $this->createAuthorizedClient();
78 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.mobi');
79 $this->assertSame(200, $client->getResponse()->getStatusCode());
81 $this->assertSame('application/x-mobipocket-ebook', $client->getResponse()->headers
->get('Content-Type'));
83 // re-auth client for pdf
84 $client = $this->createAuthorizedClient();
85 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.pdf');
86 $this->assertSame(200, $client->getResponse()->getStatusCode());
88 $this->assertContains('PDF-', $client->getResponse()->getContent());
89 $this->assertSame('application/pdf', $client->getResponse()->headers
->get('Content-Type'));
91 // re-auth client for pdf
92 $client = $this->createAuthorizedClient();
93 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.txt');
94 $this->assertSame(200, $client->getResponse()->getStatusCode());
96 $this->assertContains('text/plain', $client->getResponse()->headers
->get('Content-Type'));
98 // re-auth client for pdf
99 $client = $this->createAuthorizedClient();
100 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.csv');
101 $this->assertSame(200, $client->getResponse()->getStatusCode());
103 $this->assertContains('application/csv', $client->getResponse()->headers
->get('Content-Type'));
106 public function testGetOneEntryWrongUser()
108 $entry = $this->client
->getContainer()
109 ->get('doctrine.orm.entity_manager')
110 ->getRepository('WallabagCoreBundle:Entry')
111 ->findOneBy(['user' => 2, 'isArchived' => false]);
114 $this->markTestSkipped('No content found in db.');
117 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '.json');
119 $this->assertSame(403, $this->client
->getResponse()->getStatusCode());
122 public function testGetEntries()
124 $this->client
->request('GET', '/api/entries');
126 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
128 $content = json_decode($this->client
->getResponse()->getContent(), true);
130 $this->assertGreaterThanOrEqual(1, \
count($content));
131 $this->assertNotEmpty($content['_embedded']['items']);
132 $this->assertGreaterThanOrEqual(1, $content['total']);
133 $this->assertSame(1, $content['page']);
134 $this->assertGreaterThanOrEqual(1, $content['pages']);
136 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
139 public function testGetEntriesWithFullOptions()
141 $this->client
->request('GET', '/api/entries', [
149 'since' => 1443274283,
153 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
155 $content = json_decode($this->client
->getResponse()->getContent(), true);
157 $this->assertGreaterThanOrEqual(1, \
count($content));
158 $this->assertArrayHasKey('items', $content['_embedded']);
159 $this->assertGreaterThanOrEqual(0, $content['total']);
160 $this->assertSame(1, $content['page']);
161 $this->assertSame(2, $content['limit']);
162 $this->assertGreaterThanOrEqual(1, $content['pages']);
164 $this->assertArrayHasKey('_links', $content);
165 $this->assertArrayHasKey('self', $content['_links']);
166 $this->assertArrayHasKey('first', $content['_links']);
167 $this->assertArrayHasKey('last', $content['_links']);
169 foreach (['self', 'first', 'last'] as $link) {
170 $this->assertArrayHasKey('href', $content['_links'][$link]);
171 $this->assertContains('archive=1', $content['_links'][$link]['href']);
172 $this->assertContains('starred=1', $content['_links'][$link]['href']);
173 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
174 $this->assertContains('order=asc', $content['_links'][$link]['href']);
175 $this->assertContains('tags=foo', $content['_links'][$link]['href']);
176 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
177 $this->assertContains('public=0', $content['_links'][$link]['href']);
180 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
183 public function testGetEntriesPublicOnly()
185 $entry = $this->client
->getContainer()
186 ->get('doctrine.orm.entity_manager')
187 ->getRepository('WallabagCoreBundle:Entry')
191 $this->markTestSkipped('No content found in db.');
194 // generate at least one public entry
195 $entry->generateUid();
197 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
198 $em->persist($entry);
201 $this->client
->request('GET', '/api/entries', [
205 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
207 $content = json_decode($this->client
->getResponse()->getContent(), true);
209 $this->assertGreaterThanOrEqual(1, \
count($content));
210 $this->assertArrayHasKey('items', $content['_embedded']);
211 $this->assertGreaterThanOrEqual(1, $content['total']);
212 $this->assertSame(1, $content['page']);
213 $this->assertSame(30, $content['limit']);
214 $this->assertGreaterThanOrEqual(1, $content['pages']);
216 $this->assertArrayHasKey('_links', $content);
217 $this->assertArrayHasKey('self', $content['_links']);
218 $this->assertArrayHasKey('first', $content['_links']);
219 $this->assertArrayHasKey('last', $content['_links']);
221 foreach (['self', 'first', 'last'] as $link) {
222 $this->assertArrayHasKey('href', $content['_links'][$link]);
223 $this->assertContains('public=1', $content['_links'][$link]['href']);
226 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
229 public function testGetEntriesOnPageTwo()
231 $this->client
->request('GET', '/api/entries', [
236 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
238 $content = json_decode($this->client
->getResponse()->getContent(), true);
240 $this->assertGreaterThanOrEqual(0, $content['total']);
241 $this->assertSame(2, $content['page']);
242 $this->assertSame(2, $content['limit']);
245 public function testGetStarredEntriesWithBadSort()
247 $this->client
->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated', 'order' => 'unknown']);
249 $this->assertSame(400, $this->client
->getResponse()->getStatusCode());
251 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
254 public function testGetStarredEntries()
256 $this->client
->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']);
258 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
260 $content = json_decode($this->client
->getResponse()->getContent(), true);
262 $this->assertGreaterThanOrEqual(1, \
count($content));
263 $this->assertNotEmpty($content['_embedded']['items']);
264 $this->assertGreaterThanOrEqual(1, $content['total']);
265 $this->assertSame(1, $content['page']);
266 $this->assertGreaterThanOrEqual(1, $content['pages']);
268 $this->assertArrayHasKey('_links', $content);
269 $this->assertArrayHasKey('self', $content['_links']);
270 $this->assertArrayHasKey('first', $content['_links']);
271 $this->assertArrayHasKey('last', $content['_links']);
273 foreach (['self', 'first', 'last'] as $link) {
274 $this->assertArrayHasKey('href', $content['_links'][$link]);
275 $this->assertContains('starred=1', $content['_links'][$link]['href']);
276 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
279 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
282 public function testGetArchiveEntries()
284 $this->client
->request('GET', '/api/entries', ['archive' => 1]);
286 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
288 $content = json_decode($this->client
->getResponse()->getContent(), true);
290 $this->assertGreaterThanOrEqual(1, \
count($content));
291 $this->assertNotEmpty($content['_embedded']['items']);
292 $this->assertGreaterThanOrEqual(1, $content['total']);
293 $this->assertSame(1, $content['page']);
294 $this->assertGreaterThanOrEqual(1, $content['pages']);
296 $this->assertArrayHasKey('_links', $content);
297 $this->assertArrayHasKey('self', $content['_links']);
298 $this->assertArrayHasKey('first', $content['_links']);
299 $this->assertArrayHasKey('last', $content['_links']);
301 foreach (['self', 'first', 'last'] as $link) {
302 $this->assertArrayHasKey('href', $content['_links'][$link]);
303 $this->assertContains('archive=1', $content['_links'][$link]['href']);
306 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
309 public function testGetTaggedEntries()
311 $this->client
->request('GET', '/api/entries', ['tags' => 'foo,bar']);
313 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
315 $content = json_decode($this->client
->getResponse()->getContent(), true);
317 $this->assertGreaterThanOrEqual(1, \
count($content));
318 $this->assertNotEmpty($content['_embedded']['items']);
319 $this->assertGreaterThanOrEqual(1, $content['total']);
320 $this->assertSame(1, $content['page']);
321 $this->assertGreaterThanOrEqual(1, $content['pages']);
323 $this->assertContains('foo', array_column($content['_embedded']['items'][0]['tags'], 'label'), 'Entries tags should have "foo" tag');
324 $this->assertContains('bar', array_column($content['_embedded']['items'][0]['tags'], 'label'), 'Entries tags should have "bar" tag');
326 $this->assertArrayHasKey('_links', $content);
327 $this->assertArrayHasKey('self', $content['_links']);
328 $this->assertArrayHasKey('first', $content['_links']);
329 $this->assertArrayHasKey('last', $content['_links']);
331 foreach (['self', 'first', 'last'] as $link) {
332 $this->assertArrayHasKey('href', $content['_links'][$link]);
333 $this->assertContains('tags=' . urlencode('foo,bar'), $content['_links'][$link]['href']);
336 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
339 public function testGetTaggedEntriesWithBadParams()
341 $this->client
->request('GET', '/api/entries', ['tags' => ['foo', 'bar']]);
343 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
346 public function testGetDatedEntries()
348 $this->client
->request('GET', '/api/entries', ['since' => 1443274283]);
350 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
352 $content = json_decode($this->client
->getResponse()->getContent(), true);
354 $this->assertGreaterThanOrEqual(1, \
count($content));
355 $this->assertNotEmpty($content['_embedded']['items']);
356 $this->assertGreaterThanOrEqual(1, $content['total']);
357 $this->assertSame(1, $content['page']);
358 $this->assertGreaterThanOrEqual(1, $content['pages']);
360 $this->assertArrayHasKey('_links', $content);
361 $this->assertArrayHasKey('self', $content['_links']);
362 $this->assertArrayHasKey('first', $content['_links']);
363 $this->assertArrayHasKey('last', $content['_links']);
365 foreach (['self', 'first', 'last'] as $link) {
366 $this->assertArrayHasKey('href', $content['_links'][$link]);
367 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
370 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
373 public function testGetDatedSupEntries()
375 $future = new \
DateTime(date('Y-m-d H:i:s'));
376 $this->client
->request('GET', '/api/entries', ['since' => $future->getTimestamp() +
1000]);
378 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
380 $content = json_decode($this->client
->getResponse()->getContent(), true);
382 $this->assertGreaterThanOrEqual(1, \
count($content));
383 $this->assertEmpty($content['_embedded']['items']);
384 $this->assertSame(0, $content['total']);
385 $this->assertSame(1, $content['page']);
386 $this->assertSame(1, $content['pages']);
388 $this->assertArrayHasKey('_links', $content);
389 $this->assertArrayHasKey('self', $content['_links']);
390 $this->assertArrayHasKey('first', $content['_links']);
391 $this->assertArrayHasKey('last', $content['_links']);
393 foreach (['self', 'first', 'last'] as $link) {
394 $this->assertArrayHasKey('href', $content['_links'][$link]);
395 $this->assertContains('since=' . ($future->getTimestamp() +
1000), $content['_links'][$link]['href']);
398 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
401 public function testDeleteEntry()
403 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
404 $entry = new Entry($em->getReference(User
::class, 1));
405 $entry->setUrl('http://0.0.0.0/test-delete-entry');
406 $entry->setTitle('Test delete entry');
407 $em->persist($entry);
413 'title' => $entry->getTitle(),
414 'url' => $entry->getUrl(),
415 'id' => $entry->getId(),
418 $this->client
->request('DELETE', '/api/entries/' . $e['id'] . '.json');
420 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
422 $content = json_decode($this->client
->getResponse()->getContent(), true);
424 $this->assertSame($e['title'], $content['title']);
425 $this->assertSame($e['url'], $content['url']);
426 $this->assertSame($e['id'], $content['id']);
428 // We'll try to delete this entry again
429 $this->client
->request('DELETE', '/api/entries/' . $e['id'] . '.json');
431 $this->assertSame(404, $this->client
->getResponse()->getStatusCode());
434 public function testDeleteEntryExpectId()
436 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
437 $entry = new Entry($em->getReference(User
::class, 1));
438 $entry->setUrl('http://0.0.0.0/test-delete-entry-id');
439 $em->persist($entry);
444 $id = $entry->getId();
446 $this->client
->request('DELETE', '/api/entries/' . $id . '.json?expect=id');
448 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
450 $content = json_decode($this->client
->getResponse()->getContent(), true);
452 $this->assertSame($id, $content['id']);
453 $this->assertArrayNotHasKey('url', $content);
455 // We'll try to delete this entry again
456 $this->client
->request('DELETE', '/api/entries/' . $id . '.json');
458 $this->assertSame(404, $this->client
->getResponse()->getStatusCode());
461 public function testPostEntry()
463 $this->client
->request('POST', '/api/entries.json', [
464 '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',
466 'title' => 'New title for my article',
467 'content' => 'my content',
469 'published_at' => '2016-09-08T11:55:58+0200',
470 'authors' => 'bob,helen',
474 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
476 $content = json_decode($this->client
->getResponse()->getContent(), true);
478 $this->assertGreaterThan(0, $content['id']);
479 $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']);
480 $this->assertSame(0, $content['is_archived']);
481 $this->assertSame(0, $content['is_starred']);
482 $this->assertNull($content['starred_at']);
483 $this->assertSame('New title for my article', $content['title']);
484 $this->assertSame(1, $content['user_id']);
485 $this->assertCount(2, $content['tags']);
486 $this->assertNull($content['origin_url']);
487 $this->assertSame('my content', $content['content']);
488 $this->assertSame('de', $content['language']);
489 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
490 $this->assertCount(2, $content['published_by']);
491 $this->assertContains('bob', $content['published_by']);
492 $this->assertContains('helen', $content['published_by']);
493 $this->assertTrue($content['is_public'], 'A public link has been generated for that entry');
496 public function testPostSameEntry()
498 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
499 $entry = new Entry($em->getReference(User
::class, 1));
500 $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');
501 $entry->setArchived(true);
502 $entry->addTag((new Tag())->setLabel('google'));
503 $entry->addTag((new Tag())->setLabel('apple'));
504 $em->persist($entry);
508 $this->client
->request('POST', '/api/entries.json', [
509 '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',
511 'tags' => 'google, apple',
514 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
516 $content = json_decode($this->client
->getResponse()->getContent(), true);
518 $this->assertGreaterThan(0, $content['id']);
519 $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']);
520 $this->assertSame(1, $content['is_archived']);
521 $this->assertSame(0, $content['is_starred']);
522 $this->assertCount(3, $content['tags']);
525 public function testPostEntryWhenFetchContentFails()
527 /** @var \Symfony\Component\DependencyInjection\Container $container */
528 $container = $this->client
->getContainer();
529 $contentProxy = $this->getMockBuilder(ContentProxy
::class)
530 ->disableOriginalConstructor()
531 ->setMethods(['updateEntry'])
533 $contentProxy->expects($this->any())
534 ->method('updateEntry')
535 ->willThrowException(new \
Exception('Test Fetch content fails'));
536 $container->set('wallabag_core.content_proxy', $contentProxy);
539 $this->client
->request('POST', '/api/entries.json', [
540 'url' => 'http://www.example.com/',
543 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
544 $content = json_decode($this->client
->getResponse()->getContent(), true);
545 $this->assertGreaterThan(0, $content['id']);
546 $this->assertSame('http://www.example.com/', $content['url']);
547 $this->assertSame('www.example.com', $content['domain_name']);
548 $this->assertSame('www.example.com', $content['title']);
550 // Remove the created entry to avoid side effects on other tests
551 if (isset($content['id'])) {
552 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
553 $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']);
560 public function testPostArchivedAndStarredEntry()
562 $now = new \
DateTime();
563 $this->client
->request('POST', '/api/entries.json', [
564 'url' => 'https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
569 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
571 $content = json_decode($this->client
->getResponse()->getContent(), true);
573 $this->assertGreaterThan(0, $content['id']);
574 $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']);
575 $this->assertSame(1, $content['is_archived']);
576 $this->assertSame(1, $content['is_starred']);
577 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
578 $this->assertSame(1, $content['user_id']);
581 public function testPostArchivedAndStarredEntryWithoutQuotes()
583 $this->client
->request('POST', '/api/entries.json', [
584 'url' => 'https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
589 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
591 $content = json_decode($this->client
->getResponse()->getContent(), true);
593 $this->assertGreaterThan(0, $content['id']);
594 $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']);
595 $this->assertSame(0, $content['is_archived']);
596 $this->assertSame(1, $content['is_starred']);
599 public function testPostEntryWithOriginUrl()
601 $this->client
->request('POST', '/api/entries.json', [
602 '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',
604 'title' => 'New title for my article',
605 'content' => 'my content',
607 'published_at' => '2016-09-08T11:55:58+0200',
608 'authors' => 'bob,helen',
610 'origin_url' => 'http://mysource.tld',
613 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
615 $content = json_decode($this->client
->getResponse()->getContent(), true);
617 $this->assertGreaterThan(0, $content['id']);
618 $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']);
619 $this->assertSame('http://mysource.tld', $content['origin_url']);
622 public function testPatchEntry()
624 $entry = $this->client
->getContainer()
625 ->get('doctrine.orm.entity_manager')
626 ->getRepository('WallabagCoreBundle:Entry')
630 $this->markTestSkipped('No content found in db.');
633 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
634 'title' => 'New awesome title',
635 'tags' => 'new tag ' . uniqid(),
638 'language' => 'de_AT',
639 'preview_picture' => 'http://preview.io/picture.jpg',
640 'authors' => 'bob,sponge',
641 'content' => 'awesome',
643 'published_at' => 1488833381,
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->assertSame('New awesome title', $content['title']);
653 $this->assertGreaterThanOrEqual(1, \
count($content['tags']), 'We force only one tag');
654 $this->assertSame(1, $content['user_id']);
655 $this->assertSame('de_AT', $content['language']);
656 $this->assertSame('http://preview.io/picture.jpg', $content['preview_picture']);
657 $this->assertContains('sponge', $content['published_by']);
658 $this->assertContains('bob', $content['published_by']);
659 $this->assertSame('awesome', $content['content']);
660 $this->assertFalse($content['is_public'], 'Entry is no more shared');
661 $this->assertContains('2017-03-06', $content['published_at']);
664 public function testPatchEntryWithoutQuotes()
666 $entry = $this->client
->getContainer()
667 ->get('doctrine.orm.entity_manager')
668 ->getRepository('WallabagCoreBundle:Entry')
672 $this->markTestSkipped('No content found in db.');
675 $previousContent = $entry->getContent();
676 $previousLanguage = $entry->getLanguage();
678 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
679 'title' => 'New awesome title',
680 'tags' => 'new tag ' . uniqid(),
683 'authors' => ['bob', 'sponge'],
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->assertGreaterThanOrEqual(1, \
count($content['tags']), 'We force only one tag');
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 testPatchEntryWithOriginUrl()
700 $entry = $this->client
->getContainer()
701 ->get('doctrine.orm.entity_manager')
702 ->getRepository('WallabagCoreBundle:Entry')
706 $this->markTestSkipped('No content found in db.');
709 $previousContent = $entry->getContent();
710 $previousLanguage = $entry->getLanguage();
712 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
713 'title' => 'Another awesome title just for profit',
714 'origin_url' => 'https://myawesomesource.example.com',
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->assertSame('https://myawesomesource.example.com', $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');
729 public function testPatchEntryRemoveOriginUrl()
731 $entry = $this->client
->getContainer()
732 ->get('doctrine.orm.entity_manager')
733 ->getRepository('WallabagCoreBundle:Entry')
737 $this->markTestSkipped('No content found in db.');
740 $previousContent = $entry->getContent();
741 $previousLanguage = $entry->getLanguage();
742 $previousTitle = $entry->getTitle();
744 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
748 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
750 $content = json_decode($this->client
->getResponse()->getContent(), true);
752 $this->assertSame($entry->getId(), $content['id']);
753 $this->assertSame($entry->getUrl(), $content['url']);
754 $this->assertEmpty($content['origin_url']);
755 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
756 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
757 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
758 $this->assertSame($previousTitle, $content['title'], 'Ensure title has not moved');
761 public function testPatchEntryNullOriginUrl()
763 $entry = $this->client
->getContainer()
764 ->get('doctrine.orm.entity_manager')
765 ->getRepository('WallabagCoreBundle:Entry')
769 $this->markTestSkipped('No content found in db.');
772 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
773 'origin_url' => null,
776 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
778 $content = json_decode($this->client
->getResponse()->getContent(), true);
780 $this->assertNull($content['origin_url']);
783 public function testGetTagsEntry()
785 $entry = $this->client
->getContainer()
786 ->get('doctrine.orm.entity_manager')
787 ->getRepository('WallabagCoreBundle:Entry')
788 ->findOneWithTags($this->user
->getId());
793 $this->markTestSkipped('No content found in db.');
797 foreach ($entry->getTags() as $tag) {
798 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
801 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '/tags');
803 $this->assertSame(json_encode($tags, JSON_HEX_QUOT
), $this->client
->getResponse()->getContent());
806 public function testPostTagsOnEntry()
808 $entry = $this->client
->getContainer()
809 ->get('doctrine.orm.entity_manager')
810 ->getRepository('WallabagCoreBundle:Entry')
814 $this->markTestSkipped('No content found in db.');
817 $nbTags = \
count($entry->getTags());
819 $newTags = 'tag1,tag2,tag3';
821 $this->client
->request('POST', '/api/entries/' . $entry->getId() . '/tags', ['tags' => $newTags]);
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 +
3, \
count($content['tags']));
830 $entryDB = $this->client
->getContainer()
831 ->get('doctrine.orm.entity_manager')
832 ->getRepository('WallabagCoreBundle:Entry')
833 ->find($entry->getId());
836 foreach ($entryDB->getTags()->toArray() as $tag) {
837 $tagsInDB[$tag->getId()] = $tag->getLabel();
840 foreach (explode(',', $newTags) as $tag) {
841 $this->assertContains($tag, $tagsInDB);
845 public function testDeleteOneTagEntry()
847 $entry = $this->client
->getContainer()
848 ->get('doctrine.orm.entity_manager')
849 ->getRepository('WallabagCoreBundle:Entry')
850 ->findOneWithTags($this->user
->getId());
854 $this->markTestSkipped('No content found in db.');
857 // hydrate the tags relations
858 $nbTags = \
count($entry->getTags());
859 $tag = $entry->getTags()[0];
861 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '/tags/' . $tag->getId() . '.json');
863 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
865 $content = json_decode($this->client
->getResponse()->getContent(), true);
867 $this->assertArrayHasKey('tags', $content);
868 $this->assertSame($nbTags - 1, \
count($content['tags']));
871 public function testSaveIsArchivedAfterPost()
873 $entry = $this->client
->getContainer()
874 ->get('doctrine.orm.entity_manager')
875 ->getRepository('WallabagCoreBundle:Entry')
876 ->findOneBy(['user' => 1, 'isArchived' => true]);
879 $this->markTestSkipped('No content found in db.');
882 $this->client
->request('POST', '/api/entries.json', [
883 'url' => $entry->getUrl(),
886 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
888 $content = json_decode($this->client
->getResponse()->getContent(), true);
890 $this->assertSame(1, $content['is_archived']);
893 public function testSaveIsStarredAfterPost()
895 $entry = $this->client
->getContainer()
896 ->get('doctrine.orm.entity_manager')
897 ->getRepository('WallabagCoreBundle:Entry')
898 ->findOneBy(['user' => 1, 'isStarred' => true]);
901 $this->markTestSkipped('No content found in db.');
904 $this->client
->request('POST', '/api/entries.json', [
905 'url' => $entry->getUrl(),
908 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
910 $content = json_decode($this->client
->getResponse()->getContent(), true);
912 $this->assertSame(1, $content['is_starred']);
915 public function testSaveIsArchivedAfterPatch()
917 $entry = $this->client
->getContainer()
918 ->get('doctrine.orm.entity_manager')
919 ->getRepository('WallabagCoreBundle:Entry')
920 ->findOneBy(['user' => 1, 'isArchived' => true]);
923 $this->markTestSkipped('No content found in db.');
926 $previousTitle = $entry->getTitle();
928 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
929 'title' => $entry->getTitle() . '++',
932 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
934 $content = json_decode($this->client
->getResponse()->getContent(), true);
936 $this->assertSame(1, $content['is_archived']);
937 $this->assertSame($previousTitle . '++', $content['title']);
940 public function testSaveIsStarredAfterPatch()
942 $now = new \
DateTime();
943 $entry = $this->client
->getContainer()
944 ->get('doctrine.orm.entity_manager')
945 ->getRepository('WallabagCoreBundle:Entry')
946 ->findOneBy(['user' => 1, 'isStarred' => true]);
949 $this->markTestSkipped('No content found in db.');
951 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
952 'title' => $entry->getTitle() . '++',
955 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
957 $content = json_decode($this->client
->getResponse()->getContent(), true);
959 $this->assertSame(1, $content['is_starred']);
960 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
963 public function dataForEntriesExistWithUrl()
967 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1',
968 'expectedValue' => 2,
971 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2',
972 'expectedValue' => true,
978 * @dataProvider dataForEntriesExistWithUrl
980 public function testGetEntriesExists($url, $expectedValue)
982 $this->client
->request('GET', $url);
984 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
986 $content = json_decode($this->client
->getResponse()->getContent(), true);
988 $this->assertSame($expectedValue, $content['exists']);
991 public function testGetEntriesExistsWithManyUrls()
993 $url1 = 'http://0.0.0.0/entry2';
994 $url2 = 'http://0.0.0.0/entry10';
995 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1');
997 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
999 $content = json_decode($this->client
->getResponse()->getContent(), true);
1001 $this->assertArrayHasKey($url1, $content);
1002 $this->assertArrayHasKey($url2, $content);
1003 $this->assertSame(2, $content[$url1]);
1004 $this->assertNull($content[$url2]);
1007 public function testGetEntriesExistsWithManyUrlsReturnBool()
1009 $url1 = 'http://0.0.0.0/entry2';
1010 $url2 = 'http://0.0.0.0/entry10';
1011 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2);
1013 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1015 $content = json_decode($this->client
->getResponse()->getContent(), true);
1017 $this->assertArrayHasKey($url1, $content);
1018 $this->assertArrayHasKey($url2, $content);
1019 $this->assertTrue($content[$url1]);
1020 $this->assertFalse($content[$url2]);
1023 public function testGetEntriesExistsWhichDoesNotExists()
1025 $this->client
->request('GET', '/api/entries/exists?url=http://google.com/entry2');
1027 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1029 $content = json_decode($this->client
->getResponse()->getContent(), true);
1031 $this->assertFalse($content['exists']);
1034 public function testGetEntriesExistsWithNoUrl()
1036 $this->client
->request('GET', '/api/entries/exists?url=');
1038 $this->assertSame(403, $this->client
->getResponse()->getStatusCode());
1041 public function testReloadEntryErrorWhileFetching()
1043 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1044 ->getRepository('WallabagCoreBundle:Entry')
1045 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1048 $this->markTestSkipped('No content found in db.');
1051 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json');
1052 $this->assertSame(304, $this->client
->getResponse()->getStatusCode());
1055 public function testReloadEntry()
1057 $this->client
->request('POST', '/api/entries.json', [
1058 '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',
1060 'tags' => 'google, apple',
1063 $json = json_decode($this->client
->getResponse()->getContent(), true);
1067 $this->client
->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json');
1068 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1070 $content = json_decode($this->client
->getResponse()->getContent(), true);
1072 $this->assertNotEmpty($content['title']);
1074 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
1077 public function testPostEntriesTagsListAction()
1079 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1080 ->getRepository('WallabagCoreBundle:Entry')
1081 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1083 $tags = $entry->getTags();
1085 $this->assertCount(2, $tags);
1089 'url' => 'http://0.0.0.0/entry4',
1090 'tags' => 'new tag 1, new tag 2',
1094 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode($list));
1096 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1098 $content = json_decode($this->client
->getResponse()->getContent(), true);
1100 $this->assertInternalType('int', $content[0]['entry']);
1101 $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']);
1103 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1104 ->getRepository('WallabagCoreBundle:Entry')
1105 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1107 $tags = $entry->getTags();
1108 $this->assertCount(4, $tags);
1111 public function testPostEntriesTagsListActionNoList()
1113 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode([]));
1115 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1117 $content = json_decode($this->client
->getResponse()->getContent(), true);
1119 $this->assertEmpty($content);
1122 public function testDeleteEntriesTagsListAction()
1124 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1125 $entry = new Entry($em->getReference(User
::class, 1));
1126 $entry->setUrl('http://0.0.0.0/test-entry');
1127 $entry->addTag((new Tag())->setLabel('foo-tag'));
1128 $entry->addTag((new Tag())->setLabel('bar-tag'));
1129 $em->persist($entry);
1136 'url' => 'http://0.0.0.0/test-entry',
1137 'tags' => 'foo-tag, bar-tag',
1141 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list));
1142 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1144 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
1145 $this->assertCount(0, $entry->getTags());
1148 public function testDeleteEntriesTagsListActionNoList()
1150 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode([]));
1152 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1154 $content = json_decode($this->client
->getResponse()->getContent(), true);
1156 $this->assertEmpty($content);
1159 public function testPostEntriesListAction()
1162 'https://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
1163 'http://0.0.0.0/entry2',
1166 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1168 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1170 $content = json_decode($this->client
->getResponse()->getContent(), true);
1172 $this->assertInternalType('int', $content[0]['entry']);
1173 $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']);
1175 $this->assertInternalType('int', $content[1]['entry']);
1176 $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']);
1179 public function testPostEntriesListActionWithNoUrls()
1181 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode([]));
1183 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1185 $content = json_decode($this->client
->getResponse()->getContent(), true);
1187 $this->assertEmpty($content);
1190 public function testDeleteEntriesListAction()
1192 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1193 $em->persist((new Entry($em->getReference(User
::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
1198 'http://0.0.0.0/test-entry1',
1199 'http://0.0.0.0/test-entry-not-exist',
1202 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode($list));
1204 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1206 $content = json_decode($this->client
->getResponse()->getContent(), true);
1208 $this->assertTrue($content[0]['entry']);
1209 $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']);
1211 $this->assertFalse($content[1]['entry']);
1212 $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
1215 public function testDeleteEntriesListActionWithNoUrls()
1217 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode([]));
1219 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1221 $content = json_decode($this->client
->getResponse()->getContent(), true);
1223 $this->assertEmpty($content);
1226 public function testLimitBulkAction()
1229 'http://0.0.0.0/entry1',
1230 'http://0.0.0.0/entry1',
1231 'http://0.0.0.0/entry1',
1232 'http://0.0.0.0/entry1',
1233 'http://0.0.0.0/entry1',
1234 'http://0.0.0.0/entry1',
1235 'http://0.0.0.0/entry1',
1236 'http://0.0.0.0/entry1',
1237 'http://0.0.0.0/entry1',
1238 'http://0.0.0.0/entry1',
1239 'http://0.0.0.0/entry1',
1242 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1244 $this->assertSame(400, $this->client
->getResponse()->getStatusCode());
1245 $this->assertContains('API limit reached', $this->client
->getResponse()->getContent());
1248 public function testRePostEntryAndReUsePublishedAt()
1250 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1251 $entry = new Entry($em->getReference(User
::class, 1));
1252 $entry->setTitle('Antoine de Caunes : « Je veux avoir le droit de tâtonner »');
1253 $entry->setContent('hihi');
1254 $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');
1255 $entry->setPublishedAt(new \
DateTime('2017-06-26T07:46:02+0200'));
1256 $em->persist($entry);
1260 $this->client
->request('POST', '/api/entries.json', [
1261 'url' => 'https://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html',
1264 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1266 $content = json_decode($this->client
->getResponse()->getContent(), true);
1268 $this->assertGreaterThan(0, $content['id']);
1269 $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']);