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']);
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' => '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',
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('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']);
438 $this->assertSame(0, $content['is_archived']);
439 $this->assertSame(0, $content['is_starred']);
440 $this->assertNull($content['starred_at']);
441 $this->assertSame('New title for my article', $content['title']);
442 $this->assertSame(1, $content['user_id']);
443 $this->assertCount(2, $content['tags']);
444 $this->assertNull($content['origin_url']);
445 $this->assertSame('my content', $content['content']);
446 $this->assertSame('de', $content['language']);
447 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
448 $this->assertCount(2, $content['published_by']);
449 $this->assertContains('bob', $content['published_by']);
450 $this->assertContains('helen', $content['published_by']);
451 $this->assertTrue($content['is_public'], 'A public link has been generated for that entry');
454 public function testPostSameEntry()
456 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
457 $entry = new Entry($em->getReference(User
::class, 1));
458 $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');
459 $entry->setArchived(true);
460 $entry->addTag((new Tag())->setLabel('google'));
461 $entry->addTag((new Tag())->setLabel('apple'));
462 $em->persist($entry);
466 $this->client
->request('POST', '/api/entries.json', [
467 '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',
469 'tags' => 'google, apple',
472 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
474 $content = json_decode($this->client
->getResponse()->getContent(), true);
476 $this->assertGreaterThan(0, $content['id']);
477 $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']);
478 $this->assertSame(1, $content['is_archived']);
479 $this->assertSame(0, $content['is_starred']);
480 $this->assertCount(3, $content['tags']);
483 public function testPostEntryWhenFetchContentFails()
485 /** @var \Symfony\Component\DependencyInjection\Container $container */
486 $container = $this->client
->getContainer();
487 $contentProxy = $this->getMockBuilder(ContentProxy
::class)
488 ->disableOriginalConstructor()
489 ->setMethods(['updateEntry'])
491 $contentProxy->expects($this->any())
492 ->method('updateEntry')
493 ->willThrowException(new \
Exception('Test Fetch content fails'));
494 $container->set('wallabag_core.content_proxy', $contentProxy);
497 $this->client
->request('POST', '/api/entries.json', [
498 'url' => 'http://www.example.com/',
501 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
502 $content = json_decode($this->client
->getResponse()->getContent(), true);
503 $this->assertGreaterThan(0, $content['id']);
504 $this->assertSame('http://www.example.com/', $content['url']);
505 $this->assertSame('www.example.com', $content['domain_name']);
506 $this->assertSame('www.example.com', $content['title']);
508 // Remove the created entry to avoid side effects on other tests
509 if (isset($content['id'])) {
510 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
511 $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']);
518 public function testPostArchivedAndStarredEntry()
520 $now = new \
DateTime();
521 $this->client
->request('POST', '/api/entries.json', [
522 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
527 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
529 $content = json_decode($this->client
->getResponse()->getContent(), true);
531 $this->assertGreaterThan(0, $content['id']);
532 $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']);
533 $this->assertSame(1, $content['is_archived']);
534 $this->assertSame(1, $content['is_starred']);
535 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
536 $this->assertSame(1, $content['user_id']);
539 public function testPostArchivedAndStarredEntryWithoutQuotes()
541 $this->client
->request('POST', '/api/entries.json', [
542 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
547 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
549 $content = json_decode($this->client
->getResponse()->getContent(), true);
551 $this->assertGreaterThan(0, $content['id']);
552 $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']);
553 $this->assertSame(0, $content['is_archived']);
554 $this->assertSame(1, $content['is_starred']);
557 public function testPostEntryWithOriginUrl()
559 $this->client
->request('POST', '/api/entries.json', [
560 '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',
562 'title' => 'New title for my article',
563 'content' => 'my content',
565 'published_at' => '2016-09-08T11:55:58+0200',
566 'authors' => 'bob,helen',
568 'origin_url' => 'http://mysource.tld',
571 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
573 $content = json_decode($this->client
->getResponse()->getContent(), true);
575 $this->assertGreaterThan(0, $content['id']);
576 $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']);
577 $this->assertSame('http://mysource.tld', $content['origin_url']);
580 public function testPatchEntry()
582 $entry = $this->client
->getContainer()
583 ->get('doctrine.orm.entity_manager')
584 ->getRepository('WallabagCoreBundle:Entry')
588 $this->markTestSkipped('No content found in db.');
591 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
592 'title' => 'New awesome title',
593 'tags' => 'new tag ' . uniqid(),
596 'language' => 'de_AT',
597 'preview_picture' => 'http://preview.io/picture.jpg',
598 'authors' => 'bob,sponge',
599 'content' => 'awesome',
601 'published_at' => 1488833381,
604 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
606 $content = json_decode($this->client
->getResponse()->getContent(), true);
608 $this->assertSame($entry->getId(), $content['id']);
609 $this->assertSame($entry->getUrl(), $content['url']);
610 $this->assertSame('New awesome title', $content['title']);
611 $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag');
612 $this->assertSame(1, $content['user_id']);
613 $this->assertSame('de_AT', $content['language']);
614 $this->assertSame('http://preview.io/picture.jpg', $content['preview_picture']);
615 $this->assertContains('sponge', $content['published_by']);
616 $this->assertContains('bob', $content['published_by']);
617 $this->assertSame('awesome', $content['content']);
618 $this->assertFalse($content['is_public'], 'Entry is no more shared');
619 $this->assertContains('2017-03-06', $content['published_at']);
622 public function testPatchEntryWithoutQuotes()
624 $entry = $this->client
->getContainer()
625 ->get('doctrine.orm.entity_manager')
626 ->getRepository('WallabagCoreBundle:Entry')
630 $this->markTestSkipped('No content found in db.');
633 $previousContent = $entry->getContent();
634 $previousLanguage = $entry->getLanguage();
636 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
637 'title' => 'New awesome title',
638 'tags' => 'new tag ' . uniqid(),
641 'authors' => ['bob', 'sponge'],
644 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
646 $content = json_decode($this->client
->getResponse()->getContent(), true);
648 $this->assertSame($entry->getId(), $content['id']);
649 $this->assertSame($entry->getUrl(), $content['url']);
650 $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag');
651 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
652 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
653 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
656 public function testPatchEntryWithOriginUrl()
658 $entry = $this->client
->getContainer()
659 ->get('doctrine.orm.entity_manager')
660 ->getRepository('WallabagCoreBundle:Entry')
664 $this->markTestSkipped('No content found in db.');
667 $previousContent = $entry->getContent();
668 $previousLanguage = $entry->getLanguage();
670 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
671 'title' => 'Another awesome title just for profit',
672 'origin_url' => 'https://myawesomesource.example.com',
675 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
677 $content = json_decode($this->client
->getResponse()->getContent(), true);
679 $this->assertSame($entry->getId(), $content['id']);
680 $this->assertSame($entry->getUrl(), $content['url']);
681 $this->assertSame('https://myawesomesource.example.com', $content['origin_url']);
682 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
683 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
684 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
687 public function testPatchEntryRemoveOriginUrl()
689 $entry = $this->client
->getContainer()
690 ->get('doctrine.orm.entity_manager')
691 ->getRepository('WallabagCoreBundle:Entry')
695 $this->markTestSkipped('No content found in db.');
698 $previousContent = $entry->getContent();
699 $previousLanguage = $entry->getLanguage();
700 $previousTitle = $entry->getTitle();
702 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
706 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
708 $content = json_decode($this->client
->getResponse()->getContent(), true);
710 $this->assertSame($entry->getId(), $content['id']);
711 $this->assertSame($entry->getUrl(), $content['url']);
712 $this->assertEmpty($content['origin_url']);
713 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
714 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
715 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
716 $this->assertSame($previousTitle, $content['title'], 'Ensure title has not moved');
719 public function testPatchEntryNullOriginUrl()
721 $entry = $this->client
->getContainer()
722 ->get('doctrine.orm.entity_manager')
723 ->getRepository('WallabagCoreBundle:Entry')
727 $this->markTestSkipped('No content found in db.');
730 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
731 'origin_url' => null,
734 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
736 $content = json_decode($this->client
->getResponse()->getContent(), true);
738 $this->assertNull($content['origin_url']);
741 public function testGetTagsEntry()
743 $entry = $this->client
->getContainer()
744 ->get('doctrine.orm.entity_manager')
745 ->getRepository('WallabagCoreBundle:Entry')
746 ->findOneWithTags($this->user
->getId());
751 $this->markTestSkipped('No content found in db.');
755 foreach ($entry->getTags() as $tag) {
756 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
759 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '/tags');
761 $this->assertSame(json_encode($tags, JSON_HEX_QUOT
), $this->client
->getResponse()->getContent());
764 public function testPostTagsOnEntry()
766 $entry = $this->client
->getContainer()
767 ->get('doctrine.orm.entity_manager')
768 ->getRepository('WallabagCoreBundle:Entry')
772 $this->markTestSkipped('No content found in db.');
775 $nbTags = count($entry->getTags());
777 $newTags = 'tag1,tag2,tag3';
779 $this->client
->request('POST', '/api/entries/' . $entry->getId() . '/tags', ['tags' => $newTags]);
781 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
783 $content = json_decode($this->client
->getResponse()->getContent(), true);
785 $this->assertArrayHasKey('tags', $content);
786 $this->assertSame($nbTags +
3, count($content['tags']));
788 $entryDB = $this->client
->getContainer()
789 ->get('doctrine.orm.entity_manager')
790 ->getRepository('WallabagCoreBundle:Entry')
791 ->find($entry->getId());
794 foreach ($entryDB->getTags()->toArray() as $tag) {
795 $tagsInDB[$tag->getId()] = $tag->getLabel();
798 foreach (explode(',', $newTags) as $tag) {
799 $this->assertContains($tag, $tagsInDB);
803 public function testDeleteOneTagEntry()
805 $entry = $this->client
->getContainer()
806 ->get('doctrine.orm.entity_manager')
807 ->getRepository('WallabagCoreBundle:Entry')
808 ->findOneWithTags($this->user
->getId());
812 $this->markTestSkipped('No content found in db.');
815 // hydrate the tags relations
816 $nbTags = count($entry->getTags());
817 $tag = $entry->getTags()[0];
819 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '/tags/' . $tag->getId() . '.json');
821 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
823 $content = json_decode($this->client
->getResponse()->getContent(), true);
825 $this->assertArrayHasKey('tags', $content);
826 $this->assertSame($nbTags - 1, count($content['tags']));
829 public function testSaveIsArchivedAfterPost()
831 $entry = $this->client
->getContainer()
832 ->get('doctrine.orm.entity_manager')
833 ->getRepository('WallabagCoreBundle:Entry')
834 ->findOneBy(['user' => 1, 'isArchived' => true]);
837 $this->markTestSkipped('No content found in db.');
840 $this->client
->request('POST', '/api/entries.json', [
841 'url' => $entry->getUrl(),
844 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
846 $content = json_decode($this->client
->getResponse()->getContent(), true);
848 $this->assertSame(1, $content['is_archived']);
851 public function testSaveIsStarredAfterPost()
853 $entry = $this->client
->getContainer()
854 ->get('doctrine.orm.entity_manager')
855 ->getRepository('WallabagCoreBundle:Entry')
856 ->findOneBy(['user' => 1, 'isStarred' => true]);
859 $this->markTestSkipped('No content found in db.');
862 $this->client
->request('POST', '/api/entries.json', [
863 'url' => $entry->getUrl(),
866 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
868 $content = json_decode($this->client
->getResponse()->getContent(), true);
870 $this->assertSame(1, $content['is_starred']);
873 public function testSaveIsArchivedAfterPatch()
875 $entry = $this->client
->getContainer()
876 ->get('doctrine.orm.entity_manager')
877 ->getRepository('WallabagCoreBundle:Entry')
878 ->findOneBy(['user' => 1, 'isArchived' => true]);
881 $this->markTestSkipped('No content found in db.');
884 $previousTitle = $entry->getTitle();
886 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
887 'title' => $entry->getTitle() . '++',
890 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
892 $content = json_decode($this->client
->getResponse()->getContent(), true);
894 $this->assertSame(1, $content['is_archived']);
895 $this->assertSame($previousTitle . '++', $content['title']);
898 public function testSaveIsStarredAfterPatch()
900 $now = new \
DateTime();
901 $entry = $this->client
->getContainer()
902 ->get('doctrine.orm.entity_manager')
903 ->getRepository('WallabagCoreBundle:Entry')
904 ->findOneBy(['user' => 1, 'isStarred' => true]);
907 $this->markTestSkipped('No content found in db.');
909 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
910 'title' => $entry->getTitle() . '++',
913 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
915 $content = json_decode($this->client
->getResponse()->getContent(), true);
917 $this->assertSame(1, $content['is_starred']);
918 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
921 public function dataForEntriesExistWithUrl()
925 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1',
926 'expectedValue' => 2,
929 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2',
930 'expectedValue' => true,
936 * @dataProvider dataForEntriesExistWithUrl
938 public function testGetEntriesExists($url, $expectedValue)
940 $this->client
->request('GET', $url);
942 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
944 $content = json_decode($this->client
->getResponse()->getContent(), true);
946 $this->assertSame($expectedValue, $content['exists']);
949 public function testGetEntriesExistsWithManyUrls()
951 $url1 = 'http://0.0.0.0/entry2';
952 $url2 = 'http://0.0.0.0/entry10';
953 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1');
955 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
957 $content = json_decode($this->client
->getResponse()->getContent(), true);
959 $this->assertArrayHasKey($url1, $content);
960 $this->assertArrayHasKey($url2, $content);
961 $this->assertSame(2, $content[$url1]);
962 $this->assertNull($content[$url2]);
965 public function testGetEntriesExistsWithManyUrlsReturnBool()
967 $url1 = 'http://0.0.0.0/entry2';
968 $url2 = 'http://0.0.0.0/entry10';
969 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2);
971 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
973 $content = json_decode($this->client
->getResponse()->getContent(), true);
975 $this->assertArrayHasKey($url1, $content);
976 $this->assertArrayHasKey($url2, $content);
977 $this->assertTrue($content[$url1]);
978 $this->assertFalse($content[$url2]);
981 public function testGetEntriesExistsWhichDoesNotExists()
983 $this->client
->request('GET', '/api/entries/exists?url=http://google.com/entry2');
985 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
987 $content = json_decode($this->client
->getResponse()->getContent(), true);
989 $this->assertFalse($content['exists']);
992 public function testGetEntriesExistsWithNoUrl()
994 $this->client
->request('GET', '/api/entries/exists?url=');
996 $this->assertSame(403, $this->client
->getResponse()->getStatusCode());
999 public function testReloadEntryErrorWhileFetching()
1001 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1002 ->getRepository('WallabagCoreBundle:Entry')
1003 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1006 $this->markTestSkipped('No content found in db.');
1009 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json');
1010 $this->assertSame(304, $this->client
->getResponse()->getStatusCode());
1013 public function testReloadEntry()
1015 $this->client
->request('POST', '/api/entries.json', [
1016 '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',
1018 'tags' => 'google, apple',
1021 $json = json_decode($this->client
->getResponse()->getContent(), true);
1025 $this->client
->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json');
1026 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1028 $content = json_decode($this->client
->getResponse()->getContent(), true);
1030 $this->assertNotEmpty($content['title']);
1032 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
1035 public function testPostEntriesTagsListAction()
1037 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1038 ->getRepository('WallabagCoreBundle:Entry')
1039 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1041 $tags = $entry->getTags();
1043 $this->assertCount(2, $tags);
1047 'url' => 'http://0.0.0.0/entry4',
1048 'tags' => 'new tag 1, new tag 2',
1052 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode($list));
1054 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1056 $content = json_decode($this->client
->getResponse()->getContent(), true);
1058 $this->assertInternalType('int', $content[0]['entry']);
1059 $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']);
1061 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1062 ->getRepository('WallabagCoreBundle:Entry')
1063 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1065 $tags = $entry->getTags();
1066 $this->assertCount(4, $tags);
1069 public function testPostEntriesTagsListActionNoList()
1071 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode([]));
1073 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1075 $content = json_decode($this->client
->getResponse()->getContent(), true);
1077 $this->assertEmpty($content);
1080 public function testDeleteEntriesTagsListAction()
1082 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1083 $entry = new Entry($em->getReference(User
::class, 1));
1084 $entry->setUrl('http://0.0.0.0/test-entry');
1085 $entry->addTag((new Tag())->setLabel('foo-tag'));
1086 $entry->addTag((new Tag())->setLabel('bar-tag'));
1087 $em->persist($entry);
1094 'url' => 'http://0.0.0.0/test-entry',
1095 'tags' => 'foo-tag, bar-tag',
1099 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list));
1100 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1102 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
1103 $this->assertCount(0, $entry->getTags());
1106 public function testDeleteEntriesTagsListActionNoList()
1108 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode([]));
1110 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1112 $content = json_decode($this->client
->getResponse()->getContent(), true);
1114 $this->assertEmpty($content);
1117 public function testPostEntriesListAction()
1120 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
1121 'http://0.0.0.0/entry2',
1124 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1126 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1128 $content = json_decode($this->client
->getResponse()->getContent(), true);
1130 $this->assertInternalType('int', $content[0]['entry']);
1131 $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']);
1133 $this->assertInternalType('int', $content[1]['entry']);
1134 $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']);
1137 public function testPostEntriesListActionWithNoUrls()
1139 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode([]));
1141 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1143 $content = json_decode($this->client
->getResponse()->getContent(), true);
1145 $this->assertEmpty($content);
1148 public function testDeleteEntriesListAction()
1150 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1151 $em->persist((new Entry($em->getReference(User
::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
1156 'http://0.0.0.0/test-entry1',
1157 'http://0.0.0.0/test-entry-not-exist',
1160 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode($list));
1162 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1164 $content = json_decode($this->client
->getResponse()->getContent(), true);
1166 $this->assertTrue($content[0]['entry']);
1167 $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']);
1169 $this->assertFalse($content[1]['entry']);
1170 $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
1173 public function testDeleteEntriesListActionWithNoUrls()
1175 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode([]));
1177 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1179 $content = json_decode($this->client
->getResponse()->getContent(), true);
1181 $this->assertEmpty($content);
1184 public function testLimitBulkAction()
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',
1197 'http://0.0.0.0/entry1',
1200 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1202 $this->assertSame(400, $this->client
->getResponse()->getStatusCode());
1203 $this->assertContains('API limit reached', $this->client
->getResponse()->getContent());
1206 public function testRePostEntryAndReUsePublishedAt()
1208 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1209 $entry = new Entry($em->getReference(User
::class, 1));
1210 $entry->setTitle('Antoine de Caunes : « Je veux avoir le droit de tâtonner »');
1211 $entry->setContent('hihi');
1212 $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');
1213 $entry->setPublishedAt(new \
DateTime('2017-06-26T07:46:02+0200'));
1214 $em->persist($entry);
1218 $this->client
->request('POST', '/api/entries.json', [
1219 'url' => 'http://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html',
1222 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1224 $content = json_decode($this->client
->getResponse()->getContent(), true);
1226 $this->assertGreaterThan(0, $content['id']);
1227 $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']);