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->assertSame('my content', $content['content']);
444 $this->assertSame('de', $content['language']);
445 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
446 $this->assertCount(2, $content['published_by']);
447 $this->assertContains('bob', $content['published_by']);
448 $this->assertContains('helen', $content['published_by']);
449 $this->assertTrue($content['is_public'], 'A public link has been generated for that entry');
452 public function testPostSameEntry()
454 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
455 $entry = new Entry($em->getReference(User
::class, 1));
456 $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');
457 $entry->setArchived(true);
458 $entry->addTag((new Tag())->setLabel('google'));
459 $entry->addTag((new Tag())->setLabel('apple'));
460 $em->persist($entry);
464 $this->client
->request('POST', '/api/entries.json', [
465 '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',
467 'tags' => 'google, apple',
470 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
472 $content = json_decode($this->client
->getResponse()->getContent(), true);
474 $this->assertGreaterThan(0, $content['id']);
475 $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']);
476 $this->assertSame(1, $content['is_archived']);
477 $this->assertSame(0, $content['is_starred']);
478 $this->assertCount(3, $content['tags']);
481 public function testPostEntryWhenFetchContentFails()
483 /** @var \Symfony\Component\DependencyInjection\Container $container */
484 $container = $this->client
->getContainer();
485 $contentProxy = $this->getMockBuilder(ContentProxy
::class)
486 ->disableOriginalConstructor()
487 ->setMethods(['updateEntry'])
489 $contentProxy->expects($this->any())
490 ->method('updateEntry')
491 ->willThrowException(new \
Exception('Test Fetch content fails'));
492 $container->set('wallabag_core.content_proxy', $contentProxy);
495 $this->client
->request('POST', '/api/entries.json', [
496 'url' => 'http://www.example.com/',
499 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
500 $content = json_decode($this->client
->getResponse()->getContent(), true);
501 $this->assertGreaterThan(0, $content['id']);
502 $this->assertSame('http://www.example.com/', $content['url']);
504 // Remove the created entry to avoid side effects on other tests
505 if (isset($content['id'])) {
506 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
507 $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']);
514 public function testPostArchivedAndStarredEntry()
516 $now = new \
DateTime();
517 $this->client
->request('POST', '/api/entries.json', [
518 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
523 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
525 $content = json_decode($this->client
->getResponse()->getContent(), true);
527 $this->assertGreaterThan(0, $content['id']);
528 $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']);
529 $this->assertSame(1, $content['is_archived']);
530 $this->assertSame(1, $content['is_starred']);
531 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
532 $this->assertSame(1, $content['user_id']);
535 public function testPostArchivedAndStarredEntryWithoutQuotes()
537 $this->client
->request('POST', '/api/entries.json', [
538 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
543 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
545 $content = json_decode($this->client
->getResponse()->getContent(), true);
547 $this->assertGreaterThan(0, $content['id']);
548 $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']);
549 $this->assertSame(0, $content['is_archived']);
550 $this->assertSame(1, $content['is_starred']);
553 public function testPostEntryWithOriginUrl()
555 $this->client
->request('POST', '/api/entries.json', [
556 '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',
558 'title' => 'New title for my article',
559 'content' => 'my content',
561 'published_at' => '2016-09-08T11:55:58+0200',
562 'authors' => 'bob,helen',
564 'origin_url' => 'http://mysource.tld',
567 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
569 $content = json_decode($this->client
->getResponse()->getContent(), true);
571 $this->assertGreaterThan(0, $content['id']);
572 $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']);
573 $this->assertSame('http://mysource.tld', $content['origin_url']);
576 public function testPatchEntry()
578 $entry = $this->client
->getContainer()
579 ->get('doctrine.orm.entity_manager')
580 ->getRepository('WallabagCoreBundle:Entry')
584 $this->markTestSkipped('No content found in db.');
587 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
588 'title' => 'New awesome title',
589 'tags' => 'new tag ' . uniqid(),
592 'language' => 'de_AT',
593 'preview_picture' => 'http://preview.io/picture.jpg',
594 'authors' => 'bob,sponge',
595 'content' => 'awesome',
597 'published_at' => 1488833381,
600 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
602 $content = json_decode($this->client
->getResponse()->getContent(), true);
604 $this->assertSame($entry->getId(), $content['id']);
605 $this->assertSame($entry->getUrl(), $content['url']);
606 $this->assertSame('New awesome title', $content['title']);
607 $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag');
608 $this->assertSame(1, $content['user_id']);
609 $this->assertSame('de_AT', $content['language']);
610 $this->assertSame('http://preview.io/picture.jpg', $content['preview_picture']);
611 $this->assertContains('sponge', $content['published_by']);
612 $this->assertContains('bob', $content['published_by']);
613 $this->assertSame('awesome', $content['content']);
614 $this->assertFalse($content['is_public'], 'Entry is no more shared');
615 $this->assertContains('2017-03-06', $content['published_at']);
618 public function testPatchEntryWithoutQuotes()
620 $entry = $this->client
->getContainer()
621 ->get('doctrine.orm.entity_manager')
622 ->getRepository('WallabagCoreBundle:Entry')
626 $this->markTestSkipped('No content found in db.');
629 $previousContent = $entry->getContent();
630 $previousLanguage = $entry->getLanguage();
632 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
633 'title' => 'New awesome title',
634 'tags' => 'new tag ' . uniqid(),
637 'authors' => ['bob', 'sponge'],
640 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
642 $content = json_decode($this->client
->getResponse()->getContent(), true);
644 $this->assertSame($entry->getId(), $content['id']);
645 $this->assertSame($entry->getUrl(), $content['url']);
646 $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag');
647 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
648 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
649 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
652 public function testPatchEntryWithOriginUrl()
654 $entry = $this->client
->getContainer()
655 ->get('doctrine.orm.entity_manager')
656 ->getRepository('WallabagCoreBundle:Entry')
660 $this->markTestSkipped('No content found in db.');
663 $previousContent = $entry->getContent();
664 $previousLanguage = $entry->getLanguage();
666 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
667 'title' => 'Another awesome title just for profit',
668 'origin_url' => 'https://myawesomesource.example.com',
671 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
673 $content = json_decode($this->client
->getResponse()->getContent(), true);
675 $this->assertSame($entry->getId(), $content['id']);
676 $this->assertSame($entry->getUrl(), $content['url']);
677 $this->assertSame('https://myawesomesource.example.com', $content['origin_url']);
678 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
679 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
680 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
683 public function testGetTagsEntry()
685 $entry = $this->client
->getContainer()
686 ->get('doctrine.orm.entity_manager')
687 ->getRepository('WallabagCoreBundle:Entry')
688 ->findOneWithTags($this->user
->getId());
693 $this->markTestSkipped('No content found in db.');
697 foreach ($entry->getTags() as $tag) {
698 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
701 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '/tags');
703 $this->assertSame(json_encode($tags, JSON_HEX_QUOT
), $this->client
->getResponse()->getContent());
706 public function testPostTagsOnEntry()
708 $entry = $this->client
->getContainer()
709 ->get('doctrine.orm.entity_manager')
710 ->getRepository('WallabagCoreBundle:Entry')
714 $this->markTestSkipped('No content found in db.');
717 $nbTags = count($entry->getTags());
719 $newTags = 'tag1,tag2,tag3';
721 $this->client
->request('POST', '/api/entries/' . $entry->getId() . '/tags', ['tags' => $newTags]);
723 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
725 $content = json_decode($this->client
->getResponse()->getContent(), true);
727 $this->assertArrayHasKey('tags', $content);
728 $this->assertSame($nbTags +
3, count($content['tags']));
730 $entryDB = $this->client
->getContainer()
731 ->get('doctrine.orm.entity_manager')
732 ->getRepository('WallabagCoreBundle:Entry')
733 ->find($entry->getId());
736 foreach ($entryDB->getTags()->toArray() as $tag) {
737 $tagsInDB[$tag->getId()] = $tag->getLabel();
740 foreach (explode(',', $newTags) as $tag) {
741 $this->assertContains($tag, $tagsInDB);
745 public function testDeleteOneTagEntry()
747 $entry = $this->client
->getContainer()
748 ->get('doctrine.orm.entity_manager')
749 ->getRepository('WallabagCoreBundle:Entry')
750 ->findOneWithTags($this->user
->getId());
754 $this->markTestSkipped('No content found in db.');
757 // hydrate the tags relations
758 $nbTags = count($entry->getTags());
759 $tag = $entry->getTags()[0];
761 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '/tags/' . $tag->getId() . '.json');
763 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
765 $content = json_decode($this->client
->getResponse()->getContent(), true);
767 $this->assertArrayHasKey('tags', $content);
768 $this->assertSame($nbTags - 1, count($content['tags']));
771 public function testSaveIsArchivedAfterPost()
773 $entry = $this->client
->getContainer()
774 ->get('doctrine.orm.entity_manager')
775 ->getRepository('WallabagCoreBundle:Entry')
776 ->findOneBy(['user' => 1, 'isArchived' => true]);
779 $this->markTestSkipped('No content found in db.');
782 $this->client
->request('POST', '/api/entries.json', [
783 'url' => $entry->getUrl(),
786 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
788 $content = json_decode($this->client
->getResponse()->getContent(), true);
790 $this->assertSame(1, $content['is_archived']);
793 public function testSaveIsStarredAfterPost()
795 $entry = $this->client
->getContainer()
796 ->get('doctrine.orm.entity_manager')
797 ->getRepository('WallabagCoreBundle:Entry')
798 ->findOneBy(['user' => 1, 'isStarred' => true]);
801 $this->markTestSkipped('No content found in db.');
804 $this->client
->request('POST', '/api/entries.json', [
805 'url' => $entry->getUrl(),
808 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
810 $content = json_decode($this->client
->getResponse()->getContent(), true);
812 $this->assertSame(1, $content['is_starred']);
815 public function testSaveIsArchivedAfterPatch()
817 $entry = $this->client
->getContainer()
818 ->get('doctrine.orm.entity_manager')
819 ->getRepository('WallabagCoreBundle:Entry')
820 ->findOneBy(['user' => 1, 'isArchived' => true]);
823 $this->markTestSkipped('No content found in db.');
826 $previousTitle = $entry->getTitle();
828 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
829 'title' => $entry->getTitle() . '++',
832 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
834 $content = json_decode($this->client
->getResponse()->getContent(), true);
836 $this->assertSame(1, $content['is_archived']);
837 $this->assertSame($previousTitle . '++', $content['title']);
840 public function testSaveIsStarredAfterPatch()
842 $now = new \
DateTime();
843 $entry = $this->client
->getContainer()
844 ->get('doctrine.orm.entity_manager')
845 ->getRepository('WallabagCoreBundle:Entry')
846 ->findOneBy(['user' => 1, 'isStarred' => true]);
849 $this->markTestSkipped('No content found in db.');
851 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
852 'title' => $entry->getTitle() . '++',
855 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
857 $content = json_decode($this->client
->getResponse()->getContent(), true);
859 $this->assertSame(1, $content['is_starred']);
860 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
863 public function dataForEntriesExistWithUrl()
867 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1',
868 'expectedValue' => 2,
871 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2',
872 'expectedValue' => true,
878 * @dataProvider dataForEntriesExistWithUrl
880 public function testGetEntriesExists($url, $expectedValue)
882 $this->client
->request('GET', $url);
884 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
886 $content = json_decode($this->client
->getResponse()->getContent(), true);
888 $this->assertSame($expectedValue, $content['exists']);
891 public function testGetEntriesExistsWithManyUrls()
893 $url1 = 'http://0.0.0.0/entry2';
894 $url2 = 'http://0.0.0.0/entry10';
895 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1');
897 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
899 $content = json_decode($this->client
->getResponse()->getContent(), true);
901 $this->assertArrayHasKey($url1, $content);
902 $this->assertArrayHasKey($url2, $content);
903 $this->assertSame(2, $content[$url1]);
904 $this->assertNull($content[$url2]);
907 public function testGetEntriesExistsWithManyUrlsReturnBool()
909 $url1 = 'http://0.0.0.0/entry2';
910 $url2 = 'http://0.0.0.0/entry10';
911 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2);
913 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
915 $content = json_decode($this->client
->getResponse()->getContent(), true);
917 $this->assertArrayHasKey($url1, $content);
918 $this->assertArrayHasKey($url2, $content);
919 $this->assertTrue($content[$url1]);
920 $this->assertFalse($content[$url2]);
923 public function testGetEntriesExistsWhichDoesNotExists()
925 $this->client
->request('GET', '/api/entries/exists?url=http://google.com/entry2');
927 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
929 $content = json_decode($this->client
->getResponse()->getContent(), true);
931 $this->assertSame(false, $content['exists']);
934 public function testGetEntriesExistsWithNoUrl()
936 $this->client
->request('GET', '/api/entries/exists?url=');
938 $this->assertSame(403, $this->client
->getResponse()->getStatusCode());
941 public function testReloadEntryErrorWhileFetching()
943 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
944 ->getRepository('WallabagCoreBundle:Entry')
945 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
948 $this->markTestSkipped('No content found in db.');
951 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json');
952 $this->assertSame(304, $this->client
->getResponse()->getStatusCode());
955 public function testReloadEntry()
957 $this->client
->request('POST', '/api/entries.json', [
958 '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',
960 'tags' => 'google, apple',
963 $json = json_decode($this->client
->getResponse()->getContent(), true);
967 $this->client
->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json');
968 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
970 $content = json_decode($this->client
->getResponse()->getContent(), true);
972 $this->assertNotEmpty($content['title']);
974 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
977 public function testPostEntriesTagsListAction()
979 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
980 ->getRepository('WallabagCoreBundle:Entry')
981 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
983 $tags = $entry->getTags();
985 $this->assertCount(2, $tags);
989 'url' => 'http://0.0.0.0/entry4',
990 'tags' => 'new tag 1, new tag 2',
994 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode($list));
996 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
998 $content = json_decode($this->client
->getResponse()->getContent(), true);
1000 $this->assertInternalType('int', $content[0]['entry']);
1001 $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']);
1003 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
1004 ->getRepository('WallabagCoreBundle:Entry')
1005 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1007 $tags = $entry->getTags();
1008 $this->assertCount(4, $tags);
1011 public function testPostEntriesTagsListActionNoList()
1013 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode([]));
1015 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1017 $content = json_decode($this->client
->getResponse()->getContent(), true);
1019 $this->assertEmpty($content);
1022 public function testDeleteEntriesTagsListAction()
1024 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1025 $entry = new Entry($em->getReference(User
::class, 1));
1026 $entry->setUrl('http://0.0.0.0/test-entry');
1027 $entry->addTag((new Tag())->setLabel('foo-tag'));
1028 $entry->addTag((new Tag())->setLabel('bar-tag'));
1029 $em->persist($entry);
1036 'url' => 'http://0.0.0.0/test-entry',
1037 'tags' => 'foo-tag, bar-tag',
1041 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list));
1042 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1044 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
1045 $this->assertCount(0, $entry->getTags());
1048 public function testDeleteEntriesTagsListActionNoList()
1050 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode([]));
1052 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1054 $content = json_decode($this->client
->getResponse()->getContent(), true);
1056 $this->assertEmpty($content);
1059 public function testPostEntriesListAction()
1062 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
1063 'http://0.0.0.0/entry2',
1066 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1068 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1070 $content = json_decode($this->client
->getResponse()->getContent(), true);
1072 $this->assertInternalType('int', $content[0]['entry']);
1073 $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']);
1075 $this->assertInternalType('int', $content[1]['entry']);
1076 $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']);
1079 public function testPostEntriesListActionWithNoUrls()
1081 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode([]));
1083 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1085 $content = json_decode($this->client
->getResponse()->getContent(), true);
1087 $this->assertEmpty($content);
1090 public function testDeleteEntriesListAction()
1092 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1093 $em->persist((new Entry($em->getReference(User
::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
1098 'http://0.0.0.0/test-entry1',
1099 'http://0.0.0.0/test-entry-not-exist',
1102 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode($list));
1104 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1106 $content = json_decode($this->client
->getResponse()->getContent(), true);
1108 $this->assertTrue($content[0]['entry']);
1109 $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']);
1111 $this->assertFalse($content[1]['entry']);
1112 $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
1115 public function testDeleteEntriesListActionWithNoUrls()
1117 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode([]));
1119 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1121 $content = json_decode($this->client
->getResponse()->getContent(), true);
1123 $this->assertEmpty($content);
1126 public function testLimitBulkAction()
1129 'http://0.0.0.0/entry1',
1130 'http://0.0.0.0/entry1',
1131 'http://0.0.0.0/entry1',
1132 'http://0.0.0.0/entry1',
1133 'http://0.0.0.0/entry1',
1134 'http://0.0.0.0/entry1',
1135 'http://0.0.0.0/entry1',
1136 'http://0.0.0.0/entry1',
1137 'http://0.0.0.0/entry1',
1138 'http://0.0.0.0/entry1',
1139 'http://0.0.0.0/entry1',
1142 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1144 $this->assertSame(400, $this->client
->getResponse()->getStatusCode());
1145 $this->assertContains('API limit reached', $this->client
->getResponse()->getContent());
1148 public function testRePostEntryAndReUsePublishedAt()
1150 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1151 $entry = new Entry($em->getReference(User
::class, 1));
1152 $entry->setTitle('Antoine de Caunes : « Je veux avoir le droit de tâtonner »');
1153 $entry->setContent('hihi');
1154 $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');
1155 $entry->setPublishedAt(new \
DateTime('2017-06-26T07:46:02+0200'));
1156 $em->persist($entry);
1160 $this->client
->request('POST', '/api/entries.json', [
1161 'url' => 'http://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html',
1164 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1166 $content = json_decode($this->client
->getResponse()->getContent(), true);
1168 $this->assertGreaterThan(0, $content['id']);
1169 $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']);