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 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(1, ['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']);
412 // We'll try to delete this entry again
413 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '.json');
415 $this->assertSame(404, $this->client
->getResponse()->getStatusCode());
418 public function testPostEntry()
420 $this->client
->request('POST', '/api/entries.json', [
421 'url' => 'http://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',
423 'title' => 'New title for my article',
424 'content' => 'my content',
426 'published_at' => '2016-09-08T11:55:58+0200',
427 'authors' => 'bob,helen',
431 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
433 $content = json_decode($this->client
->getResponse()->getContent(), true);
435 $this->assertGreaterThan(0, $content['id']);
436 $this->assertSame('http://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']);
437 $this->assertSame(0, $content['is_archived']);
438 $this->assertSame(0, $content['is_starred']);
439 $this->assertNull($content['starred_at']);
440 $this->assertSame('New title for my article', $content['title']);
441 $this->assertSame(1, $content['user_id']);
442 $this->assertCount(2, $content['tags']);
443 $this->assertNull($content['origin_url']);
444 $this->assertSame('my content', $content['content']);
445 $this->assertSame('de', $content['language']);
446 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
447 $this->assertCount(2, $content['published_by']);
448 $this->assertContains('bob', $content['published_by']);
449 $this->assertContains('helen', $content['published_by']);
450 $this->assertTrue($content['is_public'], 'A public link has been generated for that entry');
453 public function testPostSameEntry()
455 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
456 $entry = new Entry($em->getReference(User
::class, 1));
457 $entry->setUrl('http://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');
458 $entry->setArchived(true);
459 $entry->addTag((new Tag())->setLabel('google'));
460 $entry->addTag((new Tag())->setLabel('apple'));
461 $em->persist($entry);
465 $this->client
->request('POST', '/api/entries.json', [
466 'url' => 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html',
468 'tags' => 'google, apple',
471 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
473 $content = json_decode($this->client
->getResponse()->getContent(), true);
475 $this->assertGreaterThan(0, $content['id']);
476 $this->assertSame('http://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']);
477 $this->assertSame(1, $content['is_archived']);
478 $this->assertSame(0, $content['is_starred']);
479 $this->assertCount(3, $content['tags']);
482 public function testPostEntryWhenFetchContentFails()
484 /** @var \Symfony\Component\DependencyInjection\Container $container */
485 $container = $this->client
->getContainer();
486 $contentProxy = $this->getMockBuilder(ContentProxy
::class)
487 ->disableOriginalConstructor()
488 ->setMethods(['updateEntry'])
490 $contentProxy->expects($this->any())
491 ->method('updateEntry')
492 ->willThrowException(new \
Exception('Test Fetch content fails'));
493 $container->set('wallabag_core.content_proxy', $contentProxy);
496 $this->client
->request('POST', '/api/entries.json', [
497 'url' => 'http://www.example.com/',
500 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
501 $content = json_decode($this->client
->getResponse()->getContent(), true);
502 $this->assertGreaterThan(0, $content['id']);
503 $this->assertSame('http://www.example.com/', $content['url']);
504 $this->assertSame('www.example.com', $content['domain_name']);
505 $this->assertSame('www.example.com', $content['title']);
507 // Remove the created entry to avoid side effects on other tests
508 if (isset($content['id'])) {
509 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
510 $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']);
517 public function testPostArchivedAndStarredEntry()
519 $now = new \
DateTime();
520 $this->client
->request('POST', '/api/entries.json', [
521 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
526 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
528 $content = json_decode($this->client
->getResponse()->getContent(), true);
530 $this->assertGreaterThan(0, $content['id']);
531 $this->assertSame('http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']);
532 $this->assertSame(1, $content['is_archived']);
533 $this->assertSame(1, $content['is_starred']);
534 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
535 $this->assertSame(1, $content['user_id']);
538 public function testPostArchivedAndStarredEntryWithoutQuotes()
540 $this->client
->request('POST', '/api/entries.json', [
541 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
546 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
548 $content = json_decode($this->client
->getResponse()->getContent(), true);
550 $this->assertGreaterThan(0, $content['id']);
551 $this->assertSame('http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']);
552 $this->assertSame(0, $content['is_archived']);
553 $this->assertSame(1, $content['is_starred']);
556 public function testPostEntryWithOriginUrl()
558 $this->client
->request('POST', '/api/entries.json', [
559 'url' => 'http://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',
561 'title' => 'New title for my article',
562 'content' => 'my content',
564 'published_at' => '2016-09-08T11:55:58+0200',
565 'authors' => 'bob,helen',
567 'origin_url' => 'http://mysource.tld',
570 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
572 $content = json_decode($this->client
->getResponse()->getContent(), true);
574 $this->assertGreaterThan(0, $content['id']);
575 $this->assertSame('http://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']);
576 $this->assertSame('http://mysource.tld', $content['origin_url']);
579 public function testPatchEntry()
581 $entry = $this->client
->getContainer()
582 ->get('doctrine.orm.entity_manager')
583 ->getRepository('WallabagCoreBundle:Entry')
587 $this->markTestSkipped('No content found in db.');
590 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
591 'title' => 'New awesome title',
592 'tags' => 'new tag ' . uniqid(),
595 'language' => 'de_AT',
596 'preview_picture' => 'http://preview.io/picture.jpg',
597 'authors' => 'bob,sponge',
598 'content' => 'awesome',
600 'published_at' => 1488833381,
603 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
605 $content = json_decode($this->client
->getResponse()->getContent(), true);
607 $this->assertSame($entry->getId(), $content['id']);
608 $this->assertSame($entry->getUrl(), $content['url']);
609 $this->assertSame('New awesome title', $content['title']);
610 $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag');
611 $this->assertSame(1, $content['user_id']);
612 $this->assertSame('de_AT', $content['language']);
613 $this->assertSame('http://preview.io/picture.jpg', $content['preview_picture']);
614 $this->assertContains('sponge', $content['published_by']);
615 $this->assertContains('bob', $content['published_by']);
616 $this->assertSame('awesome', $content['content']);
617 $this->assertFalse($content['is_public'], 'Entry is no more shared');
618 $this->assertContains('2017-03-06', $content['published_at']);
621 public function testPatchEntryWithoutQuotes()
623 $entry = $this->client
->getContainer()
624 ->get('doctrine.orm.entity_manager')
625 ->getRepository('WallabagCoreBundle:Entry')
629 $this->markTestSkipped('No content found in db.');
632 $previousContent = $entry->getContent();
633 $previousLanguage = $entry->getLanguage();
635 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
636 'title' => 'New awesome title',
637 'tags' => 'new tag ' . uniqid(),
640 'authors' => ['bob', 'sponge'],
643 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
645 $content = json_decode($this->client
->getResponse()->getContent(), true);
647 $this->assertSame($entry->getId(), $content['id']);
648 $this->assertSame($entry->getUrl(), $content['url']);
649 $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag');
650 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
651 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
652 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
655 public function testPatchEntryWithOriginUrl()
657 $entry = $this->client
->getContainer()
658 ->get('doctrine.orm.entity_manager')
659 ->getRepository('WallabagCoreBundle:Entry')
663 $this->markTestSkipped('No content found in db.');
666 $previousContent = $entry->getContent();
667 $previousLanguage = $entry->getLanguage();
669 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
670 'title' => 'Another awesome title just for profit',
671 'origin_url' => 'https://myawesomesource.example.com',
674 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
676 $content = json_decode($this->client
->getResponse()->getContent(), true);
678 $this->assertSame($entry->getId(), $content['id']);
679 $this->assertSame($entry->getUrl(), $content['url']);
680 $this->assertSame('https://myawesomesource.example.com', $content['origin_url']);
681 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
682 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
683 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
686 public function testPatchEntryRemoveOriginUrl()
688 $entry = $this->client
->getContainer()
689 ->get('doctrine.orm.entity_manager')
690 ->getRepository('WallabagCoreBundle:Entry')
694 $this->markTestSkipped('No content found in db.');
697 $previousContent = $entry->getContent();
698 $previousLanguage = $entry->getLanguage();
699 $previousTitle = $entry->getTitle();
701 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
705 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
707 $content = json_decode($this->client
->getResponse()->getContent(), true);
709 $this->assertSame($entry->getId(), $content['id']);
710 $this->assertSame($entry->getUrl(), $content['url']);
711 $this->assertEmpty($content['origin_url']);
712 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
713 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
714 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
715 $this->assertSame($previousTitle, $content['title'], 'Ensure title has not moved');
718 public function testPatchEntryNullOriginUrl()
720 $entry = $this->client
->getContainer()
721 ->get('doctrine.orm.entity_manager')
722 ->getRepository('WallabagCoreBundle:Entry')
726 $this->markTestSkipped('No content found in db.');
729 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
730 'origin_url' => null,
733 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
735 $content = json_decode($this->client
->getResponse()->getContent(), true);
737 $this->assertNull($content['origin_url']);
740 public function testGetTagsEntry()
742 $entry = $this->client
->getContainer()
743 ->get('doctrine.orm.entity_manager')
744 ->getRepository('WallabagCoreBundle:Entry')
745 ->findOneWithTags($this->user
->getId());
750 $this->markTestSkipped('No content found in db.');
754 foreach ($entry->getTags() as $tag) {
755 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
758 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '/tags');
760 $this->assertSame(json_encode($tags, JSON_HEX_QUOT
), $this->client
->getResponse()->getContent());
763 public function testPostTagsOnEntry()
765 $entry = $this->client
->getContainer()
766 ->get('doctrine.orm.entity_manager')
767 ->getRepository('WallabagCoreBundle:Entry')
771 $this->markTestSkipped('No content found in db.');
774 $nbTags = count($entry->getTags());
776 $newTags = 'tag1,tag2,tag3';
778 $this->client
->request('POST', '/api/entries/' . $entry->getId() . '/tags', ['tags' => $newTags]);
780 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
782 $content = json_decode($this->client
->getResponse()->getContent(), true);
784 $this->assertArrayHasKey('tags', $content);
785 $this->assertSame($nbTags +
3, count($content['tags']));
787 $entryDB = $this->client
->getContainer()
788 ->get('doctrine.orm.entity_manager')
789 ->getRepository('WallabagCoreBundle:Entry')
790 ->find($entry->getId());
793 foreach ($entryDB->getTags()->toArray() as $tag) {
794 $tagsInDB[$tag->getId()] = $tag->getLabel();
797 foreach (explode(',', $newTags) as $tag) {
798 $this->assertContains($tag, $tagsInDB);
802 public function testDeleteOneTagEntry()
804 $entry = $this->client
->getContainer()
805 ->get('doctrine.orm.entity_manager')
806 ->getRepository('WallabagCoreBundle:Entry')
807 ->findOneWithTags($this->user
->getId());
811 $this->markTestSkipped('No content found in db.');
814 // hydrate the tags relations
815 $nbTags = count($entry->getTags());
816 $tag = $entry->getTags()[0];
818 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '/tags/' . $tag->getId() . '.json');
820 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
822 $content = json_decode($this->client
->getResponse()->getContent(), true);
824 $this->assertArrayHasKey('tags', $content);
825 $this->assertSame($nbTags - 1, count($content['tags']));
828 public function testSaveIsArchivedAfterPost()
830 $entry = $this->client
->getContainer()
831 ->get('doctrine.orm.entity_manager')
832 ->getRepository('WallabagCoreBundle:Entry')
833 ->findOneBy(['user' => 1, 'isArchived' => true]);
836 $this->markTestSkipped('No content found in db.');
839 $this->client
->request('POST', '/api/entries.json', [
840 'url' => $entry->getUrl(),
843 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
845 $content = json_decode($this->client
->getResponse()->getContent(), true);
847 $this->assertSame(1, $content['is_archived']);
850 public function testSaveIsStarredAfterPost()
852 $entry = $this->client
->getContainer()
853 ->get('doctrine.orm.entity_manager')
854 ->getRepository('WallabagCoreBundle:Entry')
855 ->findOneBy(['user' => 1, 'isStarred' => true]);
858 $this->markTestSkipped('No content found in db.');
861 $this->client
->request('POST', '/api/entries.json', [
862 'url' => $entry->getUrl(),
865 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
867 $content = json_decode($this->client
->getResponse()->getContent(), true);
869 $this->assertSame(1, $content['is_starred']);
872 public function testSaveIsArchivedAfterPatch()
874 $entry = $this->client
->getContainer()
875 ->get('doctrine.orm.entity_manager')
876 ->getRepository('WallabagCoreBundle:Entry')
877 ->findOneBy(['user' => 1, 'isArchived' => true]);
880 $this->markTestSkipped('No content found in db.');
883 $previousTitle = $entry->getTitle();
885 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
886 'title' => $entry->getTitle() . '++',
889 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
891 $content = json_decode($this->client
->getResponse()->getContent(), true);
893 $this->assertSame(1, $content['is_archived']);
894 $this->assertSame($previousTitle . '++', $content['title']);
897 public function testSaveIsStarredAfterPatch()
899 $now = new \
DateTime();
900 $entry = $this->client
->getContainer()
901 ->get('doctrine.orm.entity_manager')
902 ->getRepository('WallabagCoreBundle:Entry')
903 ->findOneBy(['user' => 1, 'isStarred' => true]);
906 $this->markTestSkipped('No content found in db.');
908 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
909 'title' => $entry->getTitle() . '++',
912 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
914 $content = json_decode($this->client
->getResponse()->getContent(), true);
916 $this->assertSame(1, $content['is_starred']);
917 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
920 public function dataForEntriesExistWithUrl()
924 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1',
925 'expectedValue' => 2,
928 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2',
929 'expectedValue' => true,
935 * @dataProvider dataForEntriesExistWithUrl
937 public function testGetEntriesExists($url, $expectedValue)
939 $this->client
->request('GET', $url);
941 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
943 $content = json_decode($this->client
->getResponse()->getContent(), true);
945 $this->assertSame($expectedValue, $content['exists']);
948 public function testGetEntriesExistsWithManyUrls()
950 $url1 = 'http://0.0.0.0/entry2';
951 $url2 = 'http://0.0.0.0/entry10';
952 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1');
954 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
956 $content = json_decode($this->client
->getResponse()->getContent(), true);
958 $this->assertArrayHasKey($url1, $content);
959 $this->assertArrayHasKey($url2, $content);
960 $this->assertSame(2, $content[$url1]);
961 $this->assertNull($content[$url2]);
964 public function testGetEntriesExistsWithManyUrlsReturnBool()
966 $url1 = 'http://0.0.0.0/entry2';
967 $url2 = 'http://0.0.0.0/entry10';
968 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2);
970 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
972 $content = json_decode($this->client
->getResponse()->getContent(), true);
974 $this->assertArrayHasKey($url1, $content);
975 $this->assertArrayHasKey($url2, $content);
976 $this->assertTrue($content[$url1]);
977 $this->assertFalse($content[$url2]);
980 public function testGetEntriesExistsWhichDoesNotExists()
982 $this->client
->request('GET', '/api/entries/exists?url=http://google.com/entry2');
984 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
986 $content = json_decode($this->client
->getResponse()->getContent(), true);
988 $this->assertFalse($content['exists']);
991 public function testGetEntriesExistsWithNoUrl()
993 $this->client
->request('GET', '/api/entries/exists?url=');
995 $this->assertSame(403, $this->client
->getResponse()->getStatusCode());
998 public function testReloadEntryErrorWhileFetching()
1000 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1001 ->getRepository('WallabagCoreBundle:Entry')
1002 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1005 $this->markTestSkipped('No content found in db.');
1008 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json');
1009 $this->assertSame(304, $this->client
->getResponse()->getStatusCode());
1012 public function testReloadEntry()
1014 $this->client
->request('POST', '/api/entries.json', [
1015 'url' => 'http://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',
1017 'tags' => 'google, apple',
1020 $json = json_decode($this->client
->getResponse()->getContent(), true);
1024 $this->client
->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json');
1025 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1027 $content = json_decode($this->client
->getResponse()->getContent(), true);
1029 $this->assertNotEmpty($content['title']);
1031 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
1034 public function testPostEntriesTagsListAction()
1036 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1037 ->getRepository('WallabagCoreBundle:Entry')
1038 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1040 $tags = $entry->getTags();
1042 $this->assertCount(2, $tags);
1046 'url' => 'http://0.0.0.0/entry4',
1047 'tags' => 'new tag 1, new tag 2',
1051 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode($list));
1053 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1055 $content = json_decode($this->client
->getResponse()->getContent(), true);
1057 $this->assertInternalType('int', $content[0]['entry']);
1058 $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']);
1060 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1061 ->getRepository('WallabagCoreBundle:Entry')
1062 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1064 $tags = $entry->getTags();
1065 $this->assertCount(4, $tags);
1068 public function testPostEntriesTagsListActionNoList()
1070 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode([]));
1072 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1074 $content = json_decode($this->client
->getResponse()->getContent(), true);
1076 $this->assertEmpty($content);
1079 public function testDeleteEntriesTagsListAction()
1081 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1082 $entry = new Entry($em->getReference(User
::class, 1));
1083 $entry->setUrl('http://0.0.0.0/test-entry');
1084 $entry->addTag((new Tag())->setLabel('foo-tag'));
1085 $entry->addTag((new Tag())->setLabel('bar-tag'));
1086 $em->persist($entry);
1093 'url' => 'http://0.0.0.0/test-entry',
1094 'tags' => 'foo-tag, bar-tag',
1098 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list));
1099 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1101 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
1102 $this->assertCount(0, $entry->getTags());
1105 public function testDeleteEntriesTagsListActionNoList()
1107 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode([]));
1109 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1111 $content = json_decode($this->client
->getResponse()->getContent(), true);
1113 $this->assertEmpty($content);
1116 public function testPostEntriesListAction()
1119 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
1120 'http://0.0.0.0/entry2',
1123 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1125 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1127 $content = json_decode($this->client
->getResponse()->getContent(), true);
1129 $this->assertInternalType('int', $content[0]['entry']);
1130 $this->assertSame('http://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']);
1132 $this->assertInternalType('int', $content[1]['entry']);
1133 $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']);
1136 public function testPostEntriesListActionWithNoUrls()
1138 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode([]));
1140 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1142 $content = json_decode($this->client
->getResponse()->getContent(), true);
1144 $this->assertEmpty($content);
1147 public function testDeleteEntriesListAction()
1149 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1150 $em->persist((new Entry($em->getReference(User
::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
1155 'http://0.0.0.0/test-entry1',
1156 'http://0.0.0.0/test-entry-not-exist',
1159 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode($list));
1161 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1163 $content = json_decode($this->client
->getResponse()->getContent(), true);
1165 $this->assertTrue($content[0]['entry']);
1166 $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']);
1168 $this->assertFalse($content[1]['entry']);
1169 $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
1172 public function testDeleteEntriesListActionWithNoUrls()
1174 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode([]));
1176 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1178 $content = json_decode($this->client
->getResponse()->getContent(), true);
1180 $this->assertEmpty($content);
1183 public function testLimitBulkAction()
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',
1196 'http://0.0.0.0/entry1',
1199 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1201 $this->assertSame(400, $this->client
->getResponse()->getStatusCode());
1202 $this->assertContains('API limit reached', $this->client
->getResponse()->getContent());
1205 public function testRePostEntryAndReUsePublishedAt()
1207 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1208 $entry = new Entry($em->getReference(User
::class, 1));
1209 $entry->setTitle('Antoine de Caunes : « Je veux avoir le droit de tâtonner »');
1210 $entry->setContent('hihi');
1211 $entry->setUrl('http://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html');
1212 $entry->setPublishedAt(new \
DateTime('2017-06-26T07:46:02+0200'));
1213 $em->persist($entry);
1217 $this->client
->request('POST', '/api/entries.json', [
1218 'url' => 'http://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html',
1221 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1223 $content = json_decode($this->client
->getResponse()->getContent(), true);
1225 $this->assertGreaterThan(0, $content['id']);
1226 $this->assertSame('http://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html', $content['url']);