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']);
505 // Remove the created entry to avoid side effects on other tests
506 if (isset($content['id'])) {
507 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
508 $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']);
515 public function testPostArchivedAndStarredEntry()
517 $now = new \
DateTime();
518 $this->client
->request('POST', '/api/entries.json', [
519 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
524 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
526 $content = json_decode($this->client
->getResponse()->getContent(), true);
528 $this->assertGreaterThan(0, $content['id']);
529 $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']);
530 $this->assertSame(1, $content['is_archived']);
531 $this->assertSame(1, $content['is_starred']);
532 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
533 $this->assertSame(1, $content['user_id']);
536 public function testPostArchivedAndStarredEntryWithoutQuotes()
538 $this->client
->request('POST', '/api/entries.json', [
539 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
544 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
546 $content = json_decode($this->client
->getResponse()->getContent(), true);
548 $this->assertGreaterThan(0, $content['id']);
549 $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']);
550 $this->assertSame(0, $content['is_archived']);
551 $this->assertSame(1, $content['is_starred']);
554 public function testPostEntryWithOriginUrl()
556 $this->client
->request('POST', '/api/entries.json', [
557 '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',
559 'title' => 'New title for my article',
560 'content' => 'my content',
562 'published_at' => '2016-09-08T11:55:58+0200',
563 'authors' => 'bob,helen',
565 'origin_url' => 'http://mysource.tld',
568 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
570 $content = json_decode($this->client
->getResponse()->getContent(), true);
572 $this->assertGreaterThan(0, $content['id']);
573 $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']);
574 $this->assertSame('http://mysource.tld', $content['origin_url']);
577 public function testPatchEntry()
579 $entry = $this->client
->getContainer()
580 ->get('doctrine.orm.entity_manager')
581 ->getRepository('WallabagCoreBundle:Entry')
585 $this->markTestSkipped('No content found in db.');
588 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
589 'title' => 'New awesome title',
590 'tags' => 'new tag ' . uniqid(),
593 'language' => 'de_AT',
594 'preview_picture' => 'http://preview.io/picture.jpg',
595 'authors' => 'bob,sponge',
596 'content' => 'awesome',
598 'published_at' => 1488833381,
601 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
603 $content = json_decode($this->client
->getResponse()->getContent(), true);
605 $this->assertSame($entry->getId(), $content['id']);
606 $this->assertSame($entry->getUrl(), $content['url']);
607 $this->assertSame('New awesome title', $content['title']);
608 $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag');
609 $this->assertSame(1, $content['user_id']);
610 $this->assertSame('de_AT', $content['language']);
611 $this->assertSame('http://preview.io/picture.jpg', $content['preview_picture']);
612 $this->assertContains('sponge', $content['published_by']);
613 $this->assertContains('bob', $content['published_by']);
614 $this->assertSame('awesome', $content['content']);
615 $this->assertFalse($content['is_public'], 'Entry is no more shared');
616 $this->assertContains('2017-03-06', $content['published_at']);
619 public function testPatchEntryWithoutQuotes()
621 $entry = $this->client
->getContainer()
622 ->get('doctrine.orm.entity_manager')
623 ->getRepository('WallabagCoreBundle:Entry')
627 $this->markTestSkipped('No content found in db.');
630 $previousContent = $entry->getContent();
631 $previousLanguage = $entry->getLanguage();
633 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
634 'title' => 'New awesome title',
635 'tags' => 'new tag ' . uniqid(),
638 'authors' => ['bob', 'sponge'],
641 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
643 $content = json_decode($this->client
->getResponse()->getContent(), true);
645 $this->assertSame($entry->getId(), $content['id']);
646 $this->assertSame($entry->getUrl(), $content['url']);
647 $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag');
648 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
649 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
650 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
653 public function testPatchEntryWithOriginUrl()
655 $entry = $this->client
->getContainer()
656 ->get('doctrine.orm.entity_manager')
657 ->getRepository('WallabagCoreBundle:Entry')
661 $this->markTestSkipped('No content found in db.');
664 $previousContent = $entry->getContent();
665 $previousLanguage = $entry->getLanguage();
667 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
668 'title' => 'Another awesome title just for profit',
669 'origin_url' => 'https://myawesomesource.example.com',
672 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
674 $content = json_decode($this->client
->getResponse()->getContent(), true);
676 $this->assertSame($entry->getId(), $content['id']);
677 $this->assertSame($entry->getUrl(), $content['url']);
678 $this->assertSame('https://myawesomesource.example.com', $content['origin_url']);
679 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
680 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
681 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
684 public function testPatchEntryRemoveOriginUrl()
686 $entry = $this->client
->getContainer()
687 ->get('doctrine.orm.entity_manager')
688 ->getRepository('WallabagCoreBundle:Entry')
692 $this->markTestSkipped('No content found in db.');
695 $previousContent = $entry->getContent();
696 $previousLanguage = $entry->getLanguage();
697 $previousTitle = $entry->getTitle();
699 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
703 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
705 $content = json_decode($this->client
->getResponse()->getContent(), true);
707 $this->assertSame($entry->getId(), $content['id']);
708 $this->assertSame($entry->getUrl(), $content['url']);
709 $this->assertEmpty($content['origin_url']);
710 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
711 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
712 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
713 $this->assertSame($previousTitle, $content['title'], 'Ensure title has not moved');
716 public function testPatchEntryNullOriginUrl()
718 $entry = $this->client
->getContainer()
719 ->get('doctrine.orm.entity_manager')
720 ->getRepository('WallabagCoreBundle:Entry')
724 $this->markTestSkipped('No content found in db.');
727 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
728 'origin_url' => null,
731 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
733 $content = json_decode($this->client
->getResponse()->getContent(), true);
735 $this->assertNull($content['origin_url']);
738 public function testGetTagsEntry()
740 $entry = $this->client
->getContainer()
741 ->get('doctrine.orm.entity_manager')
742 ->getRepository('WallabagCoreBundle:Entry')
743 ->findOneWithTags($this->user
->getId());
748 $this->markTestSkipped('No content found in db.');
752 foreach ($entry->getTags() as $tag) {
753 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
756 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '/tags');
758 $this->assertSame(json_encode($tags, JSON_HEX_QUOT
), $this->client
->getResponse()->getContent());
761 public function testPostTagsOnEntry()
763 $entry = $this->client
->getContainer()
764 ->get('doctrine.orm.entity_manager')
765 ->getRepository('WallabagCoreBundle:Entry')
769 $this->markTestSkipped('No content found in db.');
772 $nbTags = count($entry->getTags());
774 $newTags = 'tag1,tag2,tag3';
776 $this->client
->request('POST', '/api/entries/' . $entry->getId() . '/tags', ['tags' => $newTags]);
778 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
780 $content = json_decode($this->client
->getResponse()->getContent(), true);
782 $this->assertArrayHasKey('tags', $content);
783 $this->assertSame($nbTags +
3, count($content['tags']));
785 $entryDB = $this->client
->getContainer()
786 ->get('doctrine.orm.entity_manager')
787 ->getRepository('WallabagCoreBundle:Entry')
788 ->find($entry->getId());
791 foreach ($entryDB->getTags()->toArray() as $tag) {
792 $tagsInDB[$tag->getId()] = $tag->getLabel();
795 foreach (explode(',', $newTags) as $tag) {
796 $this->assertContains($tag, $tagsInDB);
800 public function testDeleteOneTagEntry()
802 $entry = $this->client
->getContainer()
803 ->get('doctrine.orm.entity_manager')
804 ->getRepository('WallabagCoreBundle:Entry')
805 ->findOneWithTags($this->user
->getId());
809 $this->markTestSkipped('No content found in db.');
812 // hydrate the tags relations
813 $nbTags = count($entry->getTags());
814 $tag = $entry->getTags()[0];
816 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '/tags/' . $tag->getId() . '.json');
818 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
820 $content = json_decode($this->client
->getResponse()->getContent(), true);
822 $this->assertArrayHasKey('tags', $content);
823 $this->assertSame($nbTags - 1, count($content['tags']));
826 public function testSaveIsArchivedAfterPost()
828 $entry = $this->client
->getContainer()
829 ->get('doctrine.orm.entity_manager')
830 ->getRepository('WallabagCoreBundle:Entry')
831 ->findOneBy(['user' => 1, 'isArchived' => true]);
834 $this->markTestSkipped('No content found in db.');
837 $this->client
->request('POST', '/api/entries.json', [
838 'url' => $entry->getUrl(),
841 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
843 $content = json_decode($this->client
->getResponse()->getContent(), true);
845 $this->assertSame(1, $content['is_archived']);
848 public function testSaveIsStarredAfterPost()
850 $entry = $this->client
->getContainer()
851 ->get('doctrine.orm.entity_manager')
852 ->getRepository('WallabagCoreBundle:Entry')
853 ->findOneBy(['user' => 1, 'isStarred' => true]);
856 $this->markTestSkipped('No content found in db.');
859 $this->client
->request('POST', '/api/entries.json', [
860 'url' => $entry->getUrl(),
863 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
865 $content = json_decode($this->client
->getResponse()->getContent(), true);
867 $this->assertSame(1, $content['is_starred']);
870 public function testSaveIsArchivedAfterPatch()
872 $entry = $this->client
->getContainer()
873 ->get('doctrine.orm.entity_manager')
874 ->getRepository('WallabagCoreBundle:Entry')
875 ->findOneBy(['user' => 1, 'isArchived' => true]);
878 $this->markTestSkipped('No content found in db.');
881 $previousTitle = $entry->getTitle();
883 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
884 'title' => $entry->getTitle() . '++',
887 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
889 $content = json_decode($this->client
->getResponse()->getContent(), true);
891 $this->assertSame(1, $content['is_archived']);
892 $this->assertSame($previousTitle . '++', $content['title']);
895 public function testSaveIsStarredAfterPatch()
897 $now = new \
DateTime();
898 $entry = $this->client
->getContainer()
899 ->get('doctrine.orm.entity_manager')
900 ->getRepository('WallabagCoreBundle:Entry')
901 ->findOneBy(['user' => 1, 'isStarred' => true]);
904 $this->markTestSkipped('No content found in db.');
906 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
907 'title' => $entry->getTitle() . '++',
910 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
912 $content = json_decode($this->client
->getResponse()->getContent(), true);
914 $this->assertSame(1, $content['is_starred']);
915 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
918 public function dataForEntriesExistWithUrl()
922 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1',
923 'expectedValue' => 2,
926 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2',
927 'expectedValue' => true,
933 * @dataProvider dataForEntriesExistWithUrl
935 public function testGetEntriesExists($url, $expectedValue)
937 $this->client
->request('GET', $url);
939 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
941 $content = json_decode($this->client
->getResponse()->getContent(), true);
943 $this->assertSame($expectedValue, $content['exists']);
946 public function testGetEntriesExistsWithManyUrls()
948 $url1 = 'http://0.0.0.0/entry2';
949 $url2 = 'http://0.0.0.0/entry10';
950 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1');
952 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
954 $content = json_decode($this->client
->getResponse()->getContent(), true);
956 $this->assertArrayHasKey($url1, $content);
957 $this->assertArrayHasKey($url2, $content);
958 $this->assertSame(2, $content[$url1]);
959 $this->assertNull($content[$url2]);
962 public function testGetEntriesExistsWithManyUrlsReturnBool()
964 $url1 = 'http://0.0.0.0/entry2';
965 $url2 = 'http://0.0.0.0/entry10';
966 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2);
968 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
970 $content = json_decode($this->client
->getResponse()->getContent(), true);
972 $this->assertArrayHasKey($url1, $content);
973 $this->assertArrayHasKey($url2, $content);
974 $this->assertTrue($content[$url1]);
975 $this->assertFalse($content[$url2]);
978 public function testGetEntriesExistsWhichDoesNotExists()
980 $this->client
->request('GET', '/api/entries/exists?url=http://google.com/entry2');
982 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
984 $content = json_decode($this->client
->getResponse()->getContent(), true);
986 $this->assertFalse($content['exists']);
989 public function testGetEntriesExistsWithNoUrl()
991 $this->client
->request('GET', '/api/entries/exists?url=');
993 $this->assertSame(403, $this->client
->getResponse()->getStatusCode());
996 public function testReloadEntryErrorWhileFetching()
998 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
999 ->getRepository('WallabagCoreBundle:Entry')
1000 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1003 $this->markTestSkipped('No content found in db.');
1006 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json');
1007 $this->assertSame(304, $this->client
->getResponse()->getStatusCode());
1010 public function testReloadEntry()
1012 $this->client
->request('POST', '/api/entries.json', [
1013 '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',
1015 'tags' => 'google, apple',
1018 $json = json_decode($this->client
->getResponse()->getContent(), true);
1022 $this->client
->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json');
1023 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1025 $content = json_decode($this->client
->getResponse()->getContent(), true);
1027 $this->assertNotEmpty($content['title']);
1029 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
1032 public function testPostEntriesTagsListAction()
1034 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1035 ->getRepository('WallabagCoreBundle:Entry')
1036 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1038 $tags = $entry->getTags();
1040 $this->assertCount(2, $tags);
1044 'url' => 'http://0.0.0.0/entry4',
1045 'tags' => 'new tag 1, new tag 2',
1049 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode($list));
1051 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1053 $content = json_decode($this->client
->getResponse()->getContent(), true);
1055 $this->assertInternalType('int', $content[0]['entry']);
1056 $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']);
1058 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1059 ->getRepository('WallabagCoreBundle:Entry')
1060 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1062 $tags = $entry->getTags();
1063 $this->assertCount(4, $tags);
1066 public function testPostEntriesTagsListActionNoList()
1068 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode([]));
1070 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1072 $content = json_decode($this->client
->getResponse()->getContent(), true);
1074 $this->assertEmpty($content);
1077 public function testDeleteEntriesTagsListAction()
1079 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1080 $entry = new Entry($em->getReference(User
::class, 1));
1081 $entry->setUrl('http://0.0.0.0/test-entry');
1082 $entry->addTag((new Tag())->setLabel('foo-tag'));
1083 $entry->addTag((new Tag())->setLabel('bar-tag'));
1084 $em->persist($entry);
1091 'url' => 'http://0.0.0.0/test-entry',
1092 'tags' => 'foo-tag, bar-tag',
1096 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list));
1097 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1099 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
1100 $this->assertCount(0, $entry->getTags());
1103 public function testDeleteEntriesTagsListActionNoList()
1105 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode([]));
1107 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1109 $content = json_decode($this->client
->getResponse()->getContent(), true);
1111 $this->assertEmpty($content);
1114 public function testPostEntriesListAction()
1117 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
1118 'http://0.0.0.0/entry2',
1121 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1123 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1125 $content = json_decode($this->client
->getResponse()->getContent(), true);
1127 $this->assertInternalType('int', $content[0]['entry']);
1128 $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']);
1130 $this->assertInternalType('int', $content[1]['entry']);
1131 $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']);
1134 public function testPostEntriesListActionWithNoUrls()
1136 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode([]));
1138 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1140 $content = json_decode($this->client
->getResponse()->getContent(), true);
1142 $this->assertEmpty($content);
1145 public function testDeleteEntriesListAction()
1147 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1148 $em->persist((new Entry($em->getReference(User
::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
1153 'http://0.0.0.0/test-entry1',
1154 'http://0.0.0.0/test-entry-not-exist',
1157 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode($list));
1159 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1161 $content = json_decode($this->client
->getResponse()->getContent(), true);
1163 $this->assertTrue($content[0]['entry']);
1164 $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']);
1166 $this->assertFalse($content[1]['entry']);
1167 $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
1170 public function testDeleteEntriesListActionWithNoUrls()
1172 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode([]));
1174 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1176 $content = json_decode($this->client
->getResponse()->getContent(), true);
1178 $this->assertEmpty($content);
1181 public function testLimitBulkAction()
1184 'http://0.0.0.0/entry1',
1185 'http://0.0.0.0/entry1',
1186 'http://0.0.0.0/entry1',
1187 'http://0.0.0.0/entry1',
1188 'http://0.0.0.0/entry1',
1189 'http://0.0.0.0/entry1',
1190 'http://0.0.0.0/entry1',
1191 'http://0.0.0.0/entry1',
1192 'http://0.0.0.0/entry1',
1193 'http://0.0.0.0/entry1',
1194 'http://0.0.0.0/entry1',
1197 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1199 $this->assertSame(400, $this->client
->getResponse()->getStatusCode());
1200 $this->assertContains('API limit reached', $this->client
->getResponse()->getContent());
1203 public function testRePostEntryAndReUsePublishedAt()
1205 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1206 $entry = new Entry($em->getReference(User
::class, 1));
1207 $entry->setTitle('Antoine de Caunes : « Je veux avoir le droit de tâtonner »');
1208 $entry->setContent('hihi');
1209 $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');
1210 $entry->setPublishedAt(new \
DateTime('2017-06-26T07:46:02+0200'));
1211 $em->persist($entry);
1215 $this->client
->request('POST', '/api/entries.json', [
1216 'url' => 'http://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html',
1219 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1221 $content = json_decode($this->client
->getResponse()->getContent(), true);
1223 $this->assertGreaterThan(0, $content['id']);
1224 $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']);