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 testExportEntry()
41 $entry = $this->client
->getContainer()
42 ->get('doctrine.orm.entity_manager')
43 ->getRepository('WallabagCoreBundle:Entry')
44 ->findOneBy(['user' => 1, 'isArchived' => false]);
47 $this->markTestSkipped('No content found in db.');
50 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '/export.epub');
51 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
53 // epub format got the content type in the content
54 $this->assertContains('application/epub', $this->client
->getResponse()->getContent());
55 $this->assertSame('application/epub+zip', $this->client
->getResponse()->headers
->get('Content-Type'));
57 // re-auth client for mobi
58 $client = $this->createAuthorizedClient();
59 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.mobi');
60 $this->assertSame(200, $client->getResponse()->getStatusCode());
62 $this->assertSame('application/x-mobipocket-ebook', $client->getResponse()->headers
->get('Content-Type'));
64 // re-auth client for pdf
65 $client = $this->createAuthorizedClient();
66 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.pdf');
67 $this->assertSame(200, $client->getResponse()->getStatusCode());
69 $this->assertContains('PDF-', $client->getResponse()->getContent());
70 $this->assertSame('application/pdf', $client->getResponse()->headers
->get('Content-Type'));
72 // re-auth client for pdf
73 $client = $this->createAuthorizedClient();
74 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.txt');
75 $this->assertSame(200, $client->getResponse()->getStatusCode());
77 $this->assertContains('text/plain', $client->getResponse()->headers
->get('Content-Type'));
79 // re-auth client for pdf
80 $client = $this->createAuthorizedClient();
81 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.csv');
82 $this->assertSame(200, $client->getResponse()->getStatusCode());
84 $this->assertContains('application/csv', $client->getResponse()->headers
->get('Content-Type'));
87 public function testGetOneEntryWrongUser()
89 $entry = $this->client
->getContainer()
90 ->get('doctrine.orm.entity_manager')
91 ->getRepository('WallabagCoreBundle:Entry')
92 ->findOneBy(['user' => 2, 'isArchived' => false]);
95 $this->markTestSkipped('No content found in db.');
98 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '.json');
100 $this->assertSame(403, $this->client
->getResponse()->getStatusCode());
103 public function testGetEntries()
105 $this->client
->request('GET', '/api/entries');
107 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
109 $content = json_decode($this->client
->getResponse()->getContent(), true);
111 $this->assertGreaterThanOrEqual(1, count($content));
112 $this->assertNotEmpty($content['_embedded']['items']);
113 $this->assertGreaterThanOrEqual(1, $content['total']);
114 $this->assertSame(1, $content['page']);
115 $this->assertGreaterThanOrEqual(1, $content['pages']);
117 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
120 public function testGetEntriesWithFullOptions()
122 $this->client
->request('GET', '/api/entries', [
130 'since' => 1443274283,
134 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
136 $content = json_decode($this->client
->getResponse()->getContent(), true);
138 $this->assertGreaterThanOrEqual(1, count($content));
139 $this->assertArrayHasKey('items', $content['_embedded']);
140 $this->assertGreaterThanOrEqual(0, $content['total']);
141 $this->assertSame(1, $content['page']);
142 $this->assertSame(2, $content['limit']);
143 $this->assertGreaterThanOrEqual(1, $content['pages']);
145 $this->assertArrayHasKey('_links', $content);
146 $this->assertArrayHasKey('self', $content['_links']);
147 $this->assertArrayHasKey('first', $content['_links']);
148 $this->assertArrayHasKey('last', $content['_links']);
150 foreach (['self', 'first', 'last'] as $link) {
151 $this->assertArrayHasKey('href', $content['_links'][$link]);
152 $this->assertContains('archive=1', $content['_links'][$link]['href']);
153 $this->assertContains('starred=1', $content['_links'][$link]['href']);
154 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
155 $this->assertContains('order=asc', $content['_links'][$link]['href']);
156 $this->assertContains('tags=foo', $content['_links'][$link]['href']);
157 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
158 $this->assertContains('public=0', $content['_links'][$link]['href']);
161 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
164 public function testGetEntriesPublicOnly()
166 $entry = $this->client
->getContainer()
167 ->get('doctrine.orm.entity_manager')
168 ->getRepository('WallabagCoreBundle:Entry')
172 $this->markTestSkipped('No content found in db.');
175 // generate at least one public entry
176 $entry->generateUid();
178 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
179 $em->persist($entry);
182 $this->client
->request('GET', '/api/entries', [
186 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
188 $content = json_decode($this->client
->getResponse()->getContent(), true);
190 $this->assertGreaterThanOrEqual(1, count($content));
191 $this->assertArrayHasKey('items', $content['_embedded']);
192 $this->assertGreaterThanOrEqual(1, $content['total']);
193 $this->assertSame(1, $content['page']);
194 $this->assertSame(30, $content['limit']);
195 $this->assertGreaterThanOrEqual(1, $content['pages']);
197 $this->assertArrayHasKey('_links', $content);
198 $this->assertArrayHasKey('self', $content['_links']);
199 $this->assertArrayHasKey('first', $content['_links']);
200 $this->assertArrayHasKey('last', $content['_links']);
202 foreach (['self', 'first', 'last'] as $link) {
203 $this->assertArrayHasKey('href', $content['_links'][$link]);
204 $this->assertContains('public=1', $content['_links'][$link]['href']);
207 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
210 public function testGetEntriesOnPageTwo()
212 $this->client
->request('GET', '/api/entries', [
217 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
219 $content = json_decode($this->client
->getResponse()->getContent(), true);
221 $this->assertGreaterThanOrEqual(0, $content['total']);
222 $this->assertSame(2, $content['page']);
223 $this->assertSame(2, $content['limit']);
226 public function testGetStarredEntries()
228 $this->client
->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']);
230 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
232 $content = json_decode($this->client
->getResponse()->getContent(), true);
234 $this->assertGreaterThanOrEqual(1, count($content));
235 $this->assertNotEmpty($content['_embedded']['items']);
236 $this->assertGreaterThanOrEqual(1, $content['total']);
237 $this->assertSame(1, $content['page']);
238 $this->assertGreaterThanOrEqual(1, $content['pages']);
240 $this->assertArrayHasKey('_links', $content);
241 $this->assertArrayHasKey('self', $content['_links']);
242 $this->assertArrayHasKey('first', $content['_links']);
243 $this->assertArrayHasKey('last', $content['_links']);
245 foreach (['self', 'first', 'last'] as $link) {
246 $this->assertArrayHasKey('href', $content['_links'][$link]);
247 $this->assertContains('starred=1', $content['_links'][$link]['href']);
248 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
251 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
254 public function testGetArchiveEntries()
256 $this->client
->request('GET', '/api/entries', ['archive' => 1]);
258 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
260 $content = json_decode($this->client
->getResponse()->getContent(), true);
262 $this->assertGreaterThanOrEqual(1, count($content));
263 $this->assertNotEmpty($content['_embedded']['items']);
264 $this->assertGreaterThanOrEqual(1, $content['total']);
265 $this->assertSame(1, $content['page']);
266 $this->assertGreaterThanOrEqual(1, $content['pages']);
268 $this->assertArrayHasKey('_links', $content);
269 $this->assertArrayHasKey('self', $content['_links']);
270 $this->assertArrayHasKey('first', $content['_links']);
271 $this->assertArrayHasKey('last', $content['_links']);
273 foreach (['self', 'first', 'last'] as $link) {
274 $this->assertArrayHasKey('href', $content['_links'][$link]);
275 $this->assertContains('archive=1', $content['_links'][$link]['href']);
278 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
281 public function testGetTaggedEntries()
283 $this->client
->request('GET', '/api/entries', ['tags' => 'foo,bar']);
285 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
287 $content = json_decode($this->client
->getResponse()->getContent(), true);
289 $this->assertGreaterThanOrEqual(1, count($content));
290 $this->assertNotEmpty($content['_embedded']['items']);
291 $this->assertGreaterThanOrEqual(1, $content['total']);
292 $this->assertSame(1, $content['page']);
293 $this->assertGreaterThanOrEqual(1, $content['pages']);
295 $this->assertArrayHasKey('_links', $content);
296 $this->assertArrayHasKey('self', $content['_links']);
297 $this->assertArrayHasKey('first', $content['_links']);
298 $this->assertArrayHasKey('last', $content['_links']);
300 foreach (['self', 'first', 'last'] as $link) {
301 $this->assertArrayHasKey('href', $content['_links'][$link]);
302 $this->assertContains('tags=' . urlencode('foo,bar'), $content['_links'][$link]['href']);
305 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
308 public function testGetDatedEntries()
310 $this->client
->request('GET', '/api/entries', ['since' => 1443274283]);
312 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
314 $content = json_decode($this->client
->getResponse()->getContent(), true);
316 $this->assertGreaterThanOrEqual(1, count($content));
317 $this->assertNotEmpty($content['_embedded']['items']);
318 $this->assertGreaterThanOrEqual(1, $content['total']);
319 $this->assertSame(1, $content['page']);
320 $this->assertGreaterThanOrEqual(1, $content['pages']);
322 $this->assertArrayHasKey('_links', $content);
323 $this->assertArrayHasKey('self', $content['_links']);
324 $this->assertArrayHasKey('first', $content['_links']);
325 $this->assertArrayHasKey('last', $content['_links']);
327 foreach (['self', 'first', 'last'] as $link) {
328 $this->assertArrayHasKey('href', $content['_links'][$link]);
329 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
332 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
335 public function testGetDatedSupEntries()
337 $future = new \
DateTime(date('Y-m-d H:i:s'));
338 $this->client
->request('GET', '/api/entries', ['since' => $future->getTimestamp() +
1000]);
340 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
342 $content = json_decode($this->client
->getResponse()->getContent(), true);
344 $this->assertGreaterThanOrEqual(1, count($content));
345 $this->assertEmpty($content['_embedded']['items']);
346 $this->assertSame(0, $content['total']);
347 $this->assertSame(1, $content['page']);
348 $this->assertSame(1, $content['pages']);
350 $this->assertArrayHasKey('_links', $content);
351 $this->assertArrayHasKey('self', $content['_links']);
352 $this->assertArrayHasKey('first', $content['_links']);
353 $this->assertArrayHasKey('last', $content['_links']);
355 foreach (['self', 'first', 'last'] as $link) {
356 $this->assertArrayHasKey('href', $content['_links'][$link]);
357 $this->assertContains('since=' . ($future->getTimestamp() +
1000), $content['_links'][$link]['href']);
360 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
363 public function testDeleteEntry()
365 $entry = $this->client
->getContainer()
366 ->get('doctrine.orm.entity_manager')
367 ->getRepository('WallabagCoreBundle:Entry')
368 ->findOneByUser(1, ['id' => 'asc']);
371 $this->markTestSkipped('No content found in db.');
374 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '.json');
376 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
378 $content = json_decode($this->client
->getResponse()->getContent(), true);
380 $this->assertSame($entry->getTitle(), $content['title']);
381 $this->assertSame($entry->getUrl(), $content['url']);
383 // We'll try to delete this entry again
384 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '.json');
386 $this->assertSame(404, $this->client
->getResponse()->getStatusCode());
389 public function testPostEntry()
391 $this->client
->request('POST', '/api/entries.json', [
392 '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',
394 'title' => 'New title for my article',
395 'content' => 'my content',
397 'published_at' => '2016-09-08T11:55:58+0200',
398 'authors' => 'bob,helen',
402 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
404 $content = json_decode($this->client
->getResponse()->getContent(), true);
406 $this->assertGreaterThan(0, $content['id']);
407 $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']);
408 $this->assertSame(0, $content['is_archived']);
409 $this->assertSame(0, $content['is_starred']);
410 $this->assertNull($content['starred_at']);
411 $this->assertSame('New title for my article', $content['title']);
412 $this->assertSame(1, $content['user_id']);
413 $this->assertCount(2, $content['tags']);
414 $this->assertSame('my content', $content['content']);
415 $this->assertSame('de', $content['language']);
416 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
417 $this->assertCount(2, $content['published_by']);
418 $this->assertContains('bob', $content['published_by']);
419 $this->assertContains('helen', $content['published_by']);
420 $this->assertTrue($content['is_public'], 'A public link has been generated for that entry');
423 public function testPostSameEntry()
425 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
426 $entry = new Entry($em->getReference(User
::class, 1));
427 $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');
428 $entry->setArchived(true);
429 $entry->addTag((new Tag())->setLabel('google'));
430 $entry->addTag((new Tag())->setLabel('apple'));
431 $em->persist($entry);
435 $this->client
->request('POST', '/api/entries.json', [
436 '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',
438 'tags' => 'google, apple',
441 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
443 $content = json_decode($this->client
->getResponse()->getContent(), true);
445 $this->assertGreaterThan(0, $content['id']);
446 $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']);
447 $this->assertSame(1, $content['is_archived']);
448 $this->assertSame(0, $content['is_starred']);
449 $this->assertCount(3, $content['tags']);
452 public function testPostEntryWhenFetchContentFails()
454 /** @var \Symfony\Component\DependencyInjection\Container $container */
455 $container = $this->client
->getContainer();
456 $contentProxy = $this->getMockBuilder(ContentProxy
::class)
457 ->disableOriginalConstructor()
458 ->setMethods(['updateEntry'])
460 $contentProxy->expects($this->any())
461 ->method('updateEntry')
462 ->willThrowException(new \
Exception('Test Fetch content fails'));
463 $container->set('wallabag_core.content_proxy', $contentProxy);
466 $this->client
->request('POST', '/api/entries.json', [
467 'url' => 'http://www.example.com/',
470 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
471 $content = json_decode($this->client
->getResponse()->getContent(), true);
472 $this->assertGreaterThan(0, $content['id']);
473 $this->assertSame('http://www.example.com/', $content['url']);
475 // Remove the created entry to avoid side effects on other tests
476 if (isset($content['id'])) {
477 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
478 $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']);
485 public function testPostArchivedAndStarredEntry()
487 $now = new \
DateTime();
488 $this->client
->request('POST', '/api/entries.json', [
489 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
494 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
496 $content = json_decode($this->client
->getResponse()->getContent(), true);
498 $this->assertGreaterThan(0, $content['id']);
499 $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']);
500 $this->assertSame(1, $content['is_archived']);
501 $this->assertSame(1, $content['is_starred']);
502 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
503 $this->assertSame(1, $content['user_id']);
506 public function testPostArchivedAndStarredEntryWithoutQuotes()
508 $this->client
->request('POST', '/api/entries.json', [
509 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
514 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
516 $content = json_decode($this->client
->getResponse()->getContent(), true);
518 $this->assertGreaterThan(0, $content['id']);
519 $this->assertSame('http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']);
520 $this->assertSame(0, $content['is_archived']);
521 $this->assertSame(1, $content['is_starred']);
524 public function testPatchEntry()
526 $entry = $this->client
->getContainer()
527 ->get('doctrine.orm.entity_manager')
528 ->getRepository('WallabagCoreBundle:Entry')
532 $this->markTestSkipped('No content found in db.');
535 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
536 'title' => 'New awesome title',
537 'tags' => 'new tag ' . uniqid(),
540 'language' => 'de_AT',
541 'preview_picture' => 'http://preview.io/picture.jpg',
542 'authors' => 'bob,sponge',
543 'content' => 'awesome',
545 'published_at' => 1488833381,
548 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
550 $content = json_decode($this->client
->getResponse()->getContent(), true);
552 $this->assertSame($entry->getId(), $content['id']);
553 $this->assertSame($entry->getUrl(), $content['url']);
554 $this->assertSame('New awesome title', $content['title']);
555 $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag');
556 $this->assertSame(1, $content['user_id']);
557 $this->assertSame('de_AT', $content['language']);
558 $this->assertSame('http://preview.io/picture.jpg', $content['preview_picture']);
559 $this->assertContains('sponge', $content['published_by']);
560 $this->assertContains('bob', $content['published_by']);
561 $this->assertSame('awesome', $content['content']);
562 $this->assertFalse($content['is_public'], 'Entry is no more shared');
563 $this->assertContains('2017-03-06', $content['published_at']);
566 public function testPatchEntryWithoutQuotes()
568 $entry = $this->client
->getContainer()
569 ->get('doctrine.orm.entity_manager')
570 ->getRepository('WallabagCoreBundle:Entry')
574 $this->markTestSkipped('No content found in db.');
577 $previousContent = $entry->getContent();
578 $previousLanguage = $entry->getLanguage();
580 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
581 'title' => 'New awesome title',
582 'tags' => 'new tag ' . uniqid(),
585 'authors' => ['bob', 'sponge'],
588 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
590 $content = json_decode($this->client
->getResponse()->getContent(), true);
592 $this->assertSame($entry->getId(), $content['id']);
593 $this->assertSame($entry->getUrl(), $content['url']);
594 $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag');
595 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
596 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
597 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
600 public function testGetTagsEntry()
602 $entry = $this->client
->getContainer()
603 ->get('doctrine.orm.entity_manager')
604 ->getRepository('WallabagCoreBundle:Entry')
605 ->findOneWithTags($this->user
->getId());
610 $this->markTestSkipped('No content found in db.');
614 foreach ($entry->getTags() as $tag) {
615 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
618 $this->client
->request('GET', '/api/entries/' . $entry->getId() . '/tags');
620 $this->assertSame(json_encode($tags, JSON_HEX_QUOT
), $this->client
->getResponse()->getContent());
623 public function testPostTagsOnEntry()
625 $entry = $this->client
->getContainer()
626 ->get('doctrine.orm.entity_manager')
627 ->getRepository('WallabagCoreBundle:Entry')
631 $this->markTestSkipped('No content found in db.');
634 $nbTags = count($entry->getTags());
636 $newTags = 'tag1,tag2,tag3';
638 $this->client
->request('POST', '/api/entries/' . $entry->getId() . '/tags', ['tags' => $newTags]);
640 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
642 $content = json_decode($this->client
->getResponse()->getContent(), true);
644 $this->assertArrayHasKey('tags', $content);
645 $this->assertSame($nbTags +
3, count($content['tags']));
647 $entryDB = $this->client
->getContainer()
648 ->get('doctrine.orm.entity_manager')
649 ->getRepository('WallabagCoreBundle:Entry')
650 ->find($entry->getId());
653 foreach ($entryDB->getTags()->toArray() as $tag) {
654 $tagsInDB[$tag->getId()] = $tag->getLabel();
657 foreach (explode(',', $newTags) as $tag) {
658 $this->assertContains($tag, $tagsInDB);
662 public function testDeleteOneTagEntry()
664 $entry = $this->client
->getContainer()
665 ->get('doctrine.orm.entity_manager')
666 ->getRepository('WallabagCoreBundle:Entry')
667 ->findOneWithTags($this->user
->getId());
671 $this->markTestSkipped('No content found in db.');
674 // hydrate the tags relations
675 $nbTags = count($entry->getTags());
676 $tag = $entry->getTags()[0];
678 $this->client
->request('DELETE', '/api/entries/' . $entry->getId() . '/tags/' . $tag->getId() . '.json');
680 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
682 $content = json_decode($this->client
->getResponse()->getContent(), true);
684 $this->assertArrayHasKey('tags', $content);
685 $this->assertSame($nbTags - 1, count($content['tags']));
688 public function testSaveIsArchivedAfterPost()
690 $entry = $this->client
->getContainer()
691 ->get('doctrine.orm.entity_manager')
692 ->getRepository('WallabagCoreBundle:Entry')
693 ->findOneBy(['user' => 1, 'isArchived' => true]);
696 $this->markTestSkipped('No content found in db.');
699 $this->client
->request('POST', '/api/entries.json', [
700 'url' => $entry->getUrl(),
703 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
705 $content = json_decode($this->client
->getResponse()->getContent(), true);
707 $this->assertSame(1, $content['is_archived']);
710 public function testSaveIsStarredAfterPost()
712 $entry = $this->client
->getContainer()
713 ->get('doctrine.orm.entity_manager')
714 ->getRepository('WallabagCoreBundle:Entry')
715 ->findOneBy(['user' => 1, 'isStarred' => true]);
718 $this->markTestSkipped('No content found in db.');
721 $this->client
->request('POST', '/api/entries.json', [
722 'url' => $entry->getUrl(),
725 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
727 $content = json_decode($this->client
->getResponse()->getContent(), true);
729 $this->assertSame(1, $content['is_starred']);
732 public function testSaveIsArchivedAfterPatch()
734 $entry = $this->client
->getContainer()
735 ->get('doctrine.orm.entity_manager')
736 ->getRepository('WallabagCoreBundle:Entry')
737 ->findOneBy(['user' => 1, 'isArchived' => true]);
740 $this->markTestSkipped('No content found in db.');
743 $previousTitle = $entry->getTitle();
745 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
746 'title' => $entry->getTitle() . '++',
749 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
751 $content = json_decode($this->client
->getResponse()->getContent(), true);
753 $this->assertSame(1, $content['is_archived']);
754 $this->assertSame($previousTitle . '++', $content['title']);
757 public function testSaveIsStarredAfterPatch()
759 $now = new \
DateTime();
760 $entry = $this->client
->getContainer()
761 ->get('doctrine.orm.entity_manager')
762 ->getRepository('WallabagCoreBundle:Entry')
763 ->findOneBy(['user' => 1, 'isStarred' => true]);
766 $this->markTestSkipped('No content found in db.');
768 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
769 'title' => $entry->getTitle() . '++',
772 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
774 $content = json_decode($this->client
->getResponse()->getContent(), true);
776 $this->assertSame(1, $content['is_starred']);
777 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \
DateTime($content['starred_at']))->getTimestamp());
780 public function dataForEntriesExistWithUrl()
784 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1',
785 'expectedValue' => 2,
788 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2',
789 'expectedValue' => true,
795 * @dataProvider dataForEntriesExistWithUrl
797 public function testGetEntriesExists($url, $expectedValue)
799 $this->client
->request('GET', $url);
801 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
803 $content = json_decode($this->client
->getResponse()->getContent(), true);
805 $this->assertSame($expectedValue, $content['exists']);
808 public function testGetEntriesExistsWithManyUrls()
810 $url1 = 'http://0.0.0.0/entry2';
811 $url2 = 'http://0.0.0.0/entry10';
812 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1');
814 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
816 $content = json_decode($this->client
->getResponse()->getContent(), true);
818 $this->assertArrayHasKey($url1, $content);
819 $this->assertArrayHasKey($url2, $content);
820 $this->assertSame(2, $content[$url1]);
821 $this->assertNull($content[$url2]);
824 public function testGetEntriesExistsWithManyUrlsReturnBool()
826 $url1 = 'http://0.0.0.0/entry2';
827 $url2 = 'http://0.0.0.0/entry10';
828 $this->client
->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2);
830 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
832 $content = json_decode($this->client
->getResponse()->getContent(), true);
834 $this->assertArrayHasKey($url1, $content);
835 $this->assertArrayHasKey($url2, $content);
836 $this->assertTrue($content[$url1]);
837 $this->assertFalse($content[$url2]);
840 public function testGetEntriesExistsWhichDoesNotExists()
842 $this->client
->request('GET', '/api/entries/exists?url=http://google.com/entry2');
844 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
846 $content = json_decode($this->client
->getResponse()->getContent(), true);
848 $this->assertSame(false, $content['exists']);
851 public function testGetEntriesExistsWithNoUrl()
853 $this->client
->request('GET', '/api/entries/exists?url=');
855 $this->assertSame(403, $this->client
->getResponse()->getStatusCode());
858 public function testReloadEntryErrorWhileFetching()
860 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
861 ->getRepository('WallabagCoreBundle:Entry')
862 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
865 $this->markTestSkipped('No content found in db.');
868 $this->client
->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json');
869 $this->assertSame(304, $this->client
->getResponse()->getStatusCode());
872 public function testReloadEntry()
874 $this->client
->request('POST', '/api/entries.json', [
875 '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',
877 'tags' => 'google, apple',
880 $json = json_decode($this->client
->getResponse()->getContent(), true);
884 $this->client
->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json');
885 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
887 $content = json_decode($this->client
->getResponse()->getContent(), true);
889 $this->assertNotEmpty($content['title']);
891 $this->assertSame('application/json', $this->client
->getResponse()->headers
->get('Content-Type'));
894 public function testPostEntriesTagsListAction()
896 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
897 ->getRepository('WallabagCoreBundle:Entry')
898 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
900 $tags = $entry->getTags();
902 $this->assertCount(2, $tags);
906 'url' => 'http://0.0.0.0/entry4',
907 'tags' => 'new tag 1, new tag 2',
911 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode($list));
913 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
915 $content = json_decode($this->client
->getResponse()->getContent(), true);
917 $this->assertInternalType('int', $content[0]['entry']);
918 $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']);
920 $entry = $this->client
->getContainer()->get('doctrine.orm.entity_manager')
921 ->getRepository('WallabagCoreBundle:Entry')
922 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
924 $tags = $entry->getTags();
925 $this->assertCount(4, $tags);
928 public function testPostEntriesTagsListActionNoList()
930 $this->client
->request('POST', '/api/entries/tags/lists?list=' . json_encode([]));
932 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
934 $content = json_decode($this->client
->getResponse()->getContent(), true);
936 $this->assertEmpty($content);
939 public function testDeleteEntriesTagsListAction()
941 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
942 $entry = new Entry($em->getReference(User
::class, 1));
943 $entry->setUrl('http://0.0.0.0/test-entry');
944 $entry->addTag((new Tag())->setLabel('foo-tag'));
945 $entry->addTag((new Tag())->setLabel('bar-tag'));
946 $em->persist($entry);
953 'url' => 'http://0.0.0.0/test-entry',
954 'tags' => 'foo-tag, bar-tag',
958 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list));
959 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
961 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
962 $this->assertCount(0, $entry->getTags());
965 public function testDeleteEntriesTagsListActionNoList()
967 $this->client
->request('DELETE', '/api/entries/tags/list?list=' . json_encode([]));
969 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
971 $content = json_decode($this->client
->getResponse()->getContent(), true);
973 $this->assertEmpty($content);
976 public function testPostEntriesListAction()
979 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
980 'http://0.0.0.0/entry2',
983 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
985 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
987 $content = json_decode($this->client
->getResponse()->getContent(), true);
989 $this->assertInternalType('int', $content[0]['entry']);
990 $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']);
992 $this->assertInternalType('int', $content[1]['entry']);
993 $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']);
996 public function testPostEntriesListActionWithNoUrls()
998 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode([]));
1000 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1002 $content = json_decode($this->client
->getResponse()->getContent(), true);
1004 $this->assertEmpty($content);
1007 public function testDeleteEntriesListAction()
1009 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1010 $em->persist((new Entry($em->getReference(User
::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
1015 'http://0.0.0.0/test-entry1',
1016 'http://0.0.0.0/test-entry-not-exist',
1019 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode($list));
1021 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1023 $content = json_decode($this->client
->getResponse()->getContent(), true);
1025 $this->assertTrue($content[0]['entry']);
1026 $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']);
1028 $this->assertFalse($content[1]['entry']);
1029 $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
1032 public function testDeleteEntriesListActionWithNoUrls()
1034 $this->client
->request('DELETE', '/api/entries/list?urls=' . json_encode([]));
1036 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1038 $content = json_decode($this->client
->getResponse()->getContent(), true);
1040 $this->assertEmpty($content);
1043 public function testLimitBulkAction()
1046 'http://0.0.0.0/entry1',
1047 'http://0.0.0.0/entry1',
1048 'http://0.0.0.0/entry1',
1049 'http://0.0.0.0/entry1',
1050 'http://0.0.0.0/entry1',
1051 'http://0.0.0.0/entry1',
1052 'http://0.0.0.0/entry1',
1053 'http://0.0.0.0/entry1',
1054 'http://0.0.0.0/entry1',
1055 'http://0.0.0.0/entry1',
1056 'http://0.0.0.0/entry1',
1059 $this->client
->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1061 $this->assertSame(400, $this->client
->getResponse()->getStatusCode());
1062 $this->assertContains('API limit reached', $this->client
->getResponse()->getContent());
1065 public function testRePostEntryAndReUsePublishedAt()
1067 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
1068 $entry = new Entry($em->getReference(User
::class, 1));
1069 $entry->setTitle('Antoine de Caunes : « Je veux avoir le droit de tâtonner »');
1070 $entry->setContent('hihi');
1071 $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');
1072 $entry->setPublishedAt(new \
DateTime('2017-06-26T07:46:02+0200'));
1073 $em->persist($entry);
1077 $this->client
->request('POST', '/api/entries.json', [
1078 'url' => 'http://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html',
1081 $this->assertSame(200, $this->client
->getResponse()->getStatusCode());
1083 $content = json_decode($this->client
->getResponse()->getContent(), true);
1085 $this->assertGreaterThan(0, $content['id']);
1086 $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']);