]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
1489a4722bc67964b7fcbf61469685def1be7280
[github/wallabag/wallabag.git] / tests / Wallabag / ApiBundle / Controller / EntryRestControllerTest.php
1 <?php
2
3 namespace Tests\Wallabag\ApiBundle\Controller;
4
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;
10
11 class EntryRestControllerTest extends WallabagApiTestCase
12 {
13 public function testGetOneEntry()
14 {
15 $entry = $this->client->getContainer()
16 ->get('doctrine.orm.entity_manager')
17 ->getRepository('WallabagCoreBundle:Entry')
18 ->findOneBy(['user' => 1, 'isArchived' => false]);
19
20 if (!$entry) {
21 $this->markTestSkipped('No content found in db.');
22 }
23
24 $this->client->request('GET', '/api/entries/' . $entry->getId() . '.json');
25 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
26
27 $content = json_decode($this->client->getResponse()->getContent(), true);
28
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']);
35
36 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
37 }
38
39 public function testGetOneEntryWithOriginUrl()
40 {
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']);
45
46 if (!$entry) {
47 $this->markTestSkipped('No content found in db.');
48 }
49
50 $this->client->request('GET', '/api/entries/' . $entry->getId() . '.json');
51 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
52
53 $content = json_decode($this->client->getResponse()->getContent(), true);
54
55 $this->assertSame($entry->getOriginUrl(), $content['origin_url']);
56 }
57
58 public function testExportEntry()
59 {
60 $entry = $this->client->getContainer()
61 ->get('doctrine.orm.entity_manager')
62 ->getRepository('WallabagCoreBundle:Entry')
63 ->findOneBy(['user' => 1, 'isArchived' => false]);
64
65 if (!$entry) {
66 $this->markTestSkipped('No content found in db.');
67 }
68
69 $this->client->request('GET', '/api/entries/' . $entry->getId() . '/export.epub');
70 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
71
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'));
75
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());
80
81 $this->assertSame('application/x-mobipocket-ebook', $client->getResponse()->headers->get('Content-Type'));
82
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());
87
88 $this->assertContains('PDF-', $client->getResponse()->getContent());
89 $this->assertSame('application/pdf', $client->getResponse()->headers->get('Content-Type'));
90
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());
95
96 $this->assertContains('text/plain', $client->getResponse()->headers->get('Content-Type'));
97
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());
102
103 $this->assertContains('application/csv', $client->getResponse()->headers->get('Content-Type'));
104 }
105
106 public function testGetOneEntryWrongUser()
107 {
108 $entry = $this->client->getContainer()
109 ->get('doctrine.orm.entity_manager')
110 ->getRepository('WallabagCoreBundle:Entry')
111 ->findOneBy(['user' => 2, 'isArchived' => false]);
112
113 if (!$entry) {
114 $this->markTestSkipped('No content found in db.');
115 }
116
117 $this->client->request('GET', '/api/entries/' . $entry->getId() . '.json');
118
119 $this->assertSame(403, $this->client->getResponse()->getStatusCode());
120 }
121
122 public function testGetEntries()
123 {
124 $this->client->request('GET', '/api/entries');
125
126 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
127
128 $content = json_decode($this->client->getResponse()->getContent(), true);
129
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']);
135
136 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
137 }
138
139 public function testGetEntriesWithFullOptions()
140 {
141 $this->client->request('GET', '/api/entries', [
142 'archive' => 1,
143 'starred' => 1,
144 'sort' => 'updated',
145 'order' => 'asc',
146 'page' => 1,
147 'perPage' => 2,
148 'tags' => 'foo',
149 'since' => 1443274283,
150 'public' => 0,
151 ]);
152
153 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
154
155 $content = json_decode($this->client->getResponse()->getContent(), true);
156
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']);
163
164 $this->assertArrayHasKey('_links', $content);
165 $this->assertArrayHasKey('self', $content['_links']);
166 $this->assertArrayHasKey('first', $content['_links']);
167 $this->assertArrayHasKey('last', $content['_links']);
168
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']);
178 }
179
180 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
181 }
182
183 public function testGetEntriesPublicOnly()
184 {
185 $entry = $this->client->getContainer()
186 ->get('doctrine.orm.entity_manager')
187 ->getRepository('WallabagCoreBundle:Entry')
188 ->findOneByUser(1);
189
190 if (!$entry) {
191 $this->markTestSkipped('No content found in db.');
192 }
193
194 // generate at least one public entry
195 $entry->generateUid();
196
197 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
198 $em->persist($entry);
199 $em->flush();
200
201 $this->client->request('GET', '/api/entries', [
202 'public' => 1,
203 ]);
204
205 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
206
207 $content = json_decode($this->client->getResponse()->getContent(), true);
208
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']);
215
216 $this->assertArrayHasKey('_links', $content);
217 $this->assertArrayHasKey('self', $content['_links']);
218 $this->assertArrayHasKey('first', $content['_links']);
219 $this->assertArrayHasKey('last', $content['_links']);
220
221 foreach (['self', 'first', 'last'] as $link) {
222 $this->assertArrayHasKey('href', $content['_links'][$link]);
223 $this->assertContains('public=1', $content['_links'][$link]['href']);
224 }
225
226 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
227 }
228
229 public function testGetEntriesOnPageTwo()
230 {
231 $this->client->request('GET', '/api/entries', [
232 'page' => 2,
233 'perPage' => 2,
234 ]);
235
236 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
237
238 $content = json_decode($this->client->getResponse()->getContent(), true);
239
240 $this->assertGreaterThanOrEqual(0, $content['total']);
241 $this->assertSame(2, $content['page']);
242 $this->assertSame(2, $content['limit']);
243 }
244
245 public function testGetStarredEntriesWithBadSort()
246 {
247 $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated', 'order' => 'unknown']);
248
249 $this->assertSame(400, $this->client->getResponse()->getStatusCode());
250
251 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
252 }
253
254 public function testGetStarredEntries()
255 {
256 $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']);
257
258 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
259
260 $content = json_decode($this->client->getResponse()->getContent(), true);
261
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']);
267
268 $this->assertArrayHasKey('_links', $content);
269 $this->assertArrayHasKey('self', $content['_links']);
270 $this->assertArrayHasKey('first', $content['_links']);
271 $this->assertArrayHasKey('last', $content['_links']);
272
273 foreach (['self', 'first', 'last'] as $link) {
274 $this->assertArrayHasKey('href', $content['_links'][$link]);
275 $this->assertContains('starred=1', $content['_links'][$link]['href']);
276 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
277 }
278
279 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
280 }
281
282 public function testGetArchiveEntries()
283 {
284 $this->client->request('GET', '/api/entries', ['archive' => 1]);
285
286 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
287
288 $content = json_decode($this->client->getResponse()->getContent(), true);
289
290 $this->assertGreaterThanOrEqual(1, \count($content));
291 $this->assertNotEmpty($content['_embedded']['items']);
292 $this->assertGreaterThanOrEqual(1, $content['total']);
293 $this->assertSame(1, $content['page']);
294 $this->assertGreaterThanOrEqual(1, $content['pages']);
295
296 $this->assertArrayHasKey('_links', $content);
297 $this->assertArrayHasKey('self', $content['_links']);
298 $this->assertArrayHasKey('first', $content['_links']);
299 $this->assertArrayHasKey('last', $content['_links']);
300
301 foreach (['self', 'first', 'last'] as $link) {
302 $this->assertArrayHasKey('href', $content['_links'][$link]);
303 $this->assertContains('archive=1', $content['_links'][$link]['href']);
304 }
305
306 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
307 }
308
309 public function testGetTaggedEntries()
310 {
311 $this->client->request('GET', '/api/entries', ['tags' => 'foo,bar']);
312
313 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
314
315 $content = json_decode($this->client->getResponse()->getContent(), true);
316
317 $this->assertGreaterThanOrEqual(1, \count($content));
318 $this->assertNotEmpty($content['_embedded']['items']);
319 $this->assertGreaterThanOrEqual(1, $content['total']);
320 $this->assertSame(1, $content['page']);
321 $this->assertGreaterThanOrEqual(1, $content['pages']);
322
323 $this->assertContains('foo', array_column($content['_embedded']['items'][0]['tags'], 'label'), 'Entries tags should have "foo" tag');
324 $this->assertContains('bar', array_column($content['_embedded']['items'][0]['tags'], 'label'), 'Entries tags should have "bar" tag');
325
326 $this->assertArrayHasKey('_links', $content);
327 $this->assertArrayHasKey('self', $content['_links']);
328 $this->assertArrayHasKey('first', $content['_links']);
329 $this->assertArrayHasKey('last', $content['_links']);
330
331 foreach (['self', 'first', 'last'] as $link) {
332 $this->assertArrayHasKey('href', $content['_links'][$link]);
333 $this->assertContains('tags=' . urlencode('foo,bar'), $content['_links'][$link]['href']);
334 }
335
336 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
337 }
338
339 public function testGetTaggedEntriesWithBadParams()
340 {
341 $this->client->request('GET', '/api/entries', ['tags' => ['foo', 'bar']]);
342
343 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
344 }
345
346 public function testGetDatedEntries()
347 {
348 $this->client->request('GET', '/api/entries', ['since' => 1443274283]);
349
350 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
351
352 $content = json_decode($this->client->getResponse()->getContent(), true);
353
354 $this->assertGreaterThanOrEqual(1, \count($content));
355 $this->assertNotEmpty($content['_embedded']['items']);
356 $this->assertGreaterThanOrEqual(1, $content['total']);
357 $this->assertSame(1, $content['page']);
358 $this->assertGreaterThanOrEqual(1, $content['pages']);
359
360 $this->assertArrayHasKey('_links', $content);
361 $this->assertArrayHasKey('self', $content['_links']);
362 $this->assertArrayHasKey('first', $content['_links']);
363 $this->assertArrayHasKey('last', $content['_links']);
364
365 foreach (['self', 'first', 'last'] as $link) {
366 $this->assertArrayHasKey('href', $content['_links'][$link]);
367 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
368 }
369
370 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
371 }
372
373 public function testGetDatedSupEntries()
374 {
375 $future = new \DateTime(date('Y-m-d H:i:s'));
376 $this->client->request('GET', '/api/entries', ['since' => $future->getTimestamp() + 1000]);
377
378 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
379
380 $content = json_decode($this->client->getResponse()->getContent(), true);
381
382 $this->assertGreaterThanOrEqual(1, \count($content));
383 $this->assertEmpty($content['_embedded']['items']);
384 $this->assertSame(0, $content['total']);
385 $this->assertSame(1, $content['page']);
386 $this->assertSame(1, $content['pages']);
387
388 $this->assertArrayHasKey('_links', $content);
389 $this->assertArrayHasKey('self', $content['_links']);
390 $this->assertArrayHasKey('first', $content['_links']);
391 $this->assertArrayHasKey('last', $content['_links']);
392
393 foreach (['self', 'first', 'last'] as $link) {
394 $this->assertArrayHasKey('href', $content['_links'][$link]);
395 $this->assertContains('since=' . ($future->getTimestamp() + 1000), $content['_links'][$link]['href']);
396 }
397
398 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
399 }
400
401 public function testDeleteEntry()
402 {
403 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
404 $entry = new Entry($em->getReference(User::class, 1));
405 $entry->setUrl('http://0.0.0.0/test-delete-entry');
406 $entry->setTitle('Test delete entry');
407 $em->persist($entry);
408 $em->flush();
409
410 $em->clear();
411
412 $e = [
413 'title' => $entry->getTitle(),
414 'url' => $entry->getUrl(),
415 'id' => $entry->getId(),
416 ];
417
418 $this->client->request('DELETE', '/api/entries/' . $e['id'] . '.json');
419
420 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
421
422 $content = json_decode($this->client->getResponse()->getContent(), true);
423
424 $this->assertSame($e['title'], $content['title']);
425 $this->assertSame($e['url'], $content['url']);
426 $this->assertSame($e['id'], $content['id']);
427
428 // We'll try to delete this entry again
429 $this->client->request('DELETE', '/api/entries/' . $e['id'] . '.json');
430
431 $this->assertSame(404, $this->client->getResponse()->getStatusCode());
432 }
433
434 public function testDeleteEntryExpectId()
435 {
436 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
437 $entry = new Entry($em->getReference(User::class, 1));
438 $entry->setUrl('http://0.0.0.0/test-delete-entry-id');
439 $em->persist($entry);
440 $em->flush();
441
442 $em->clear();
443
444 $id = $entry->getId();
445
446 $this->client->request('DELETE', '/api/entries/' . $id . '.json?expect=id');
447
448 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
449
450 $content = json_decode($this->client->getResponse()->getContent(), true);
451
452 $this->assertSame($id, $content['id']);
453 $this->assertArrayNotHasKey('url', $content);
454
455 // We'll try to delete this entry again
456 $this->client->request('DELETE', '/api/entries/' . $id . '.json');
457
458 $this->assertSame(404, $this->client->getResponse()->getStatusCode());
459 }
460
461 public function testPostEntry()
462 {
463 $this->client->request('POST', '/api/entries.json', [
464 'url' => 'https://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',
465 'tags' => 'google',
466 'title' => 'New title for my article',
467 'content' => 'my content',
468 'language' => 'de',
469 'published_at' => '2016-09-08T11:55:58+0200',
470 'authors' => 'bob,helen',
471 'public' => 1,
472 ]);
473
474 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
475
476 $content = json_decode($this->client->getResponse()->getContent(), true);
477
478 $this->assertGreaterThan(0, $content['id']);
479 $this->assertSame('https://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']);
480 $this->assertSame(0, $content['is_archived']);
481 $this->assertSame(0, $content['is_starred']);
482 $this->assertNull($content['starred_at']);
483 $this->assertSame('New title for my article', $content['title']);
484 $this->assertSame(1, $content['user_id']);
485 $this->assertCount(2, $content['tags']);
486 $this->assertNull($content['origin_url']);
487 $this->assertSame('my content', $content['content']);
488 $this->assertSame('de', $content['language']);
489 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
490 $this->assertCount(2, $content['published_by']);
491 $this->assertContains('bob', $content['published_by']);
492 $this->assertContains('helen', $content['published_by']);
493 $this->assertTrue($content['is_public'], 'A public link has been generated for that entry');
494 }
495
496 public function testPostSameEntry()
497 {
498 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
499 $entry = new Entry($em->getReference(User::class, 1));
500 $entry->setUrl('https://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');
501 $entry->setArchived(true);
502 $entry->addTag((new Tag())->setLabel('google'));
503 $entry->addTag((new Tag())->setLabel('apple'));
504 $em->persist($entry);
505 $em->flush();
506 $em->clear();
507
508 $this->client->request('POST', '/api/entries.json', [
509 'url' => 'https://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',
510 'archive' => '1',
511 'tags' => 'google, apple',
512 ]);
513
514 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
515
516 $content = json_decode($this->client->getResponse()->getContent(), true);
517
518 $this->assertGreaterThan(0, $content['id']);
519 $this->assertSame('https://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']);
520 $this->assertSame(1, $content['is_archived']);
521 $this->assertSame(0, $content['is_starred']);
522 $this->assertCount(3, $content['tags']);
523 }
524
525 public function testPostEntryWhenFetchContentFails()
526 {
527 /** @var \Symfony\Component\DependencyInjection\Container $container */
528 $container = $this->client->getContainer();
529 $contentProxy = $this->getMockBuilder(ContentProxy::class)
530 ->disableOriginalConstructor()
531 ->setMethods(['updateEntry'])
532 ->getMock();
533 $contentProxy->expects($this->any())
534 ->method('updateEntry')
535 ->willThrowException(new \Exception('Test Fetch content fails'));
536 $container->set('wallabag_core.content_proxy', $contentProxy);
537
538 try {
539 $this->client->request('POST', '/api/entries.json', [
540 'url' => 'http://www.example.com/',
541 ]);
542
543 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
544 $content = json_decode($this->client->getResponse()->getContent(), true);
545 $this->assertGreaterThan(0, $content['id']);
546 $this->assertSame('http://www.example.com/', $content['url']);
547 $this->assertSame('www.example.com', $content['domain_name']);
548 $this->assertSame('www.example.com', $content['title']);
549 } finally {
550 // Remove the created entry to avoid side effects on other tests
551 if (isset($content['id'])) {
552 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
553 $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']);
554 $em->remove($entry);
555 $em->flush();
556 }
557 }
558 }
559
560 public function testPostArchivedAndStarredEntry()
561 {
562 $now = new \DateTime();
563 $this->client->request('POST', '/api/entries.json', [
564 'url' => 'https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
565 'archive' => '1',
566 'starred' => '1',
567 ]);
568
569 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
570
571 $content = json_decode($this->client->getResponse()->getContent(), true);
572
573 $this->assertGreaterThan(0, $content['id']);
574 $this->assertSame('https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']);
575 $this->assertSame(1, $content['is_archived']);
576 $this->assertSame(1, $content['is_starred']);
577 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['starred_at']))->getTimestamp());
578 $this->assertSame(1, $content['user_id']);
579 }
580
581 public function testPostArchivedAndStarredEntryWithoutQuotes()
582 {
583 $this->client->request('POST', '/api/entries.json', [
584 'url' => 'https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
585 'archive' => 0,
586 'starred' => 1,
587 ]);
588
589 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
590
591 $content = json_decode($this->client->getResponse()->getContent(), true);
592
593 $this->assertGreaterThan(0, $content['id']);
594 $this->assertSame('https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']);
595 $this->assertSame(0, $content['is_archived']);
596 $this->assertSame(1, $content['is_starred']);
597 }
598
599 public function testPostEntryWithOriginUrl()
600 {
601 $this->client->request('POST', '/api/entries.json', [
602 'url' => 'https://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',
603 'tags' => 'google',
604 'title' => 'New title for my article',
605 'content' => 'my content',
606 'language' => 'de',
607 'published_at' => '2016-09-08T11:55:58+0200',
608 'authors' => 'bob,helen',
609 'public' => 1,
610 'origin_url' => 'http://mysource.tld',
611 ]);
612
613 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
614
615 $content = json_decode($this->client->getResponse()->getContent(), true);
616
617 $this->assertGreaterThan(0, $content['id']);
618 $this->assertSame('https://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']);
619 $this->assertSame('http://mysource.tld', $content['origin_url']);
620 }
621
622 public function testPatchEntry()
623 {
624 $entry = $this->client->getContainer()
625 ->get('doctrine.orm.entity_manager')
626 ->getRepository('WallabagCoreBundle:Entry')
627 ->findOneByUser(1);
628
629 if (!$entry) {
630 $this->markTestSkipped('No content found in db.');
631 }
632
633 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
634 'title' => 'New awesome title',
635 'tags' => 'new tag ' . uniqid(),
636 'starred' => '1',
637 'archive' => '0',
638 'language' => 'de_AT',
639 'preview_picture' => 'http://preview.io/picture.jpg',
640 'authors' => 'bob,sponge',
641 'content' => 'awesome',
642 'public' => 0,
643 'published_at' => 1488833381,
644 ]);
645
646 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
647
648 $content = json_decode($this->client->getResponse()->getContent(), true);
649
650 $this->assertSame($entry->getId(), $content['id']);
651 $this->assertSame($entry->getUrl(), $content['url']);
652 $this->assertSame('New awesome title', $content['title']);
653 $this->assertGreaterThanOrEqual(1, \count($content['tags']), 'We force only one tag');
654 $this->assertSame(1, $content['user_id']);
655 $this->assertSame('de_AT', $content['language']);
656 $this->assertSame('http://preview.io/picture.jpg', $content['preview_picture']);
657 $this->assertContains('sponge', $content['published_by']);
658 $this->assertContains('bob', $content['published_by']);
659 $this->assertSame('awesome', $content['content']);
660 $this->assertFalse($content['is_public'], 'Entry is no more shared');
661 $this->assertContains('2017-03-06', $content['published_at']);
662 }
663
664 public function testPatchEntryWithoutQuotes()
665 {
666 $entry = $this->client->getContainer()
667 ->get('doctrine.orm.entity_manager')
668 ->getRepository('WallabagCoreBundle:Entry')
669 ->findOneByUser(1);
670
671 if (!$entry) {
672 $this->markTestSkipped('No content found in db.');
673 }
674
675 $previousContent = $entry->getContent();
676 $previousLanguage = $entry->getLanguage();
677
678 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
679 'title' => 'New awesome title',
680 'tags' => 'new tag ' . uniqid(),
681 'starred' => 1,
682 'archive' => 0,
683 'authors' => ['bob', 'sponge'],
684 ]);
685
686 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
687
688 $content = json_decode($this->client->getResponse()->getContent(), true);
689
690 $this->assertSame($entry->getId(), $content['id']);
691 $this->assertSame($entry->getUrl(), $content['url']);
692 $this->assertGreaterThanOrEqual(1, \count($content['tags']), 'We force only one tag');
693 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
694 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
695 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
696 }
697
698 public function testPatchEntryWithOriginUrl()
699 {
700 $entry = $this->client->getContainer()
701 ->get('doctrine.orm.entity_manager')
702 ->getRepository('WallabagCoreBundle:Entry')
703 ->findOneByUser(1);
704
705 if (!$entry) {
706 $this->markTestSkipped('No content found in db.');
707 }
708
709 $previousContent = $entry->getContent();
710 $previousLanguage = $entry->getLanguage();
711
712 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
713 'title' => 'Another awesome title just for profit',
714 'origin_url' => 'https://myawesomesource.example.com',
715 ]);
716
717 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
718
719 $content = json_decode($this->client->getResponse()->getContent(), true);
720
721 $this->assertSame($entry->getId(), $content['id']);
722 $this->assertSame($entry->getUrl(), $content['url']);
723 $this->assertSame('https://myawesomesource.example.com', $content['origin_url']);
724 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
725 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
726 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
727 }
728
729 public function testPatchEntryRemoveOriginUrl()
730 {
731 $entry = $this->client->getContainer()
732 ->get('doctrine.orm.entity_manager')
733 ->getRepository('WallabagCoreBundle:Entry')
734 ->findOneByUser(1);
735
736 if (!$entry) {
737 $this->markTestSkipped('No content found in db.');
738 }
739
740 $previousContent = $entry->getContent();
741 $previousLanguage = $entry->getLanguage();
742 $previousTitle = $entry->getTitle();
743
744 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
745 'origin_url' => '',
746 ]);
747
748 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
749
750 $content = json_decode($this->client->getResponse()->getContent(), true);
751
752 $this->assertSame($entry->getId(), $content['id']);
753 $this->assertSame($entry->getUrl(), $content['url']);
754 $this->assertEmpty($content['origin_url']);
755 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
756 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
757 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
758 $this->assertSame($previousTitle, $content['title'], 'Ensure title has not moved');
759 }
760
761 public function testPatchEntryNullOriginUrl()
762 {
763 $entry = $this->client->getContainer()
764 ->get('doctrine.orm.entity_manager')
765 ->getRepository('WallabagCoreBundle:Entry')
766 ->findOneByUser(1);
767
768 if (!$entry) {
769 $this->markTestSkipped('No content found in db.');
770 }
771
772 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
773 'origin_url' => null,
774 ]);
775
776 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
777
778 $content = json_decode($this->client->getResponse()->getContent(), true);
779
780 $this->assertNull($content['origin_url']);
781 }
782
783 public function testGetTagsEntry()
784 {
785 $entry = $this->client->getContainer()
786 ->get('doctrine.orm.entity_manager')
787 ->getRepository('WallabagCoreBundle:Entry')
788 ->findOneWithTags($this->user->getId());
789
790 $entry = $entry[0];
791
792 if (!$entry) {
793 $this->markTestSkipped('No content found in db.');
794 }
795
796 $tags = [];
797 foreach ($entry->getTags() as $tag) {
798 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
799 }
800
801 $this->client->request('GET', '/api/entries/' . $entry->getId() . '/tags');
802
803 $this->assertSame(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent());
804 }
805
806 public function testPostTagsOnEntry()
807 {
808 $entry = $this->client->getContainer()
809 ->get('doctrine.orm.entity_manager')
810 ->getRepository('WallabagCoreBundle:Entry')
811 ->findOneByUser(1);
812
813 if (!$entry) {
814 $this->markTestSkipped('No content found in db.');
815 }
816
817 $nbTags = \count($entry->getTags());
818
819 $newTags = 'tag1,tag2,tag3';
820
821 $this->client->request('POST', '/api/entries/' . $entry->getId() . '/tags', ['tags' => $newTags]);
822
823 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
824
825 $content = json_decode($this->client->getResponse()->getContent(), true);
826
827 $this->assertArrayHasKey('tags', $content);
828 $this->assertSame($nbTags + 3, \count($content['tags']));
829
830 $entryDB = $this->client->getContainer()
831 ->get('doctrine.orm.entity_manager')
832 ->getRepository('WallabagCoreBundle:Entry')
833 ->find($entry->getId());
834
835 $tagsInDB = [];
836 foreach ($entryDB->getTags()->toArray() as $tag) {
837 $tagsInDB[$tag->getId()] = $tag->getLabel();
838 }
839
840 foreach (explode(',', $newTags) as $tag) {
841 $this->assertContains($tag, $tagsInDB);
842 }
843 }
844
845 public function testDeleteOneTagEntry()
846 {
847 $entry = $this->client->getContainer()
848 ->get('doctrine.orm.entity_manager')
849 ->getRepository('WallabagCoreBundle:Entry')
850 ->findOneWithTags($this->user->getId());
851 $entry = $entry[0];
852
853 if (!$entry) {
854 $this->markTestSkipped('No content found in db.');
855 }
856
857 // hydrate the tags relations
858 $nbTags = \count($entry->getTags());
859 $tag = $entry->getTags()[0];
860
861 $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '/tags/' . $tag->getId() . '.json');
862
863 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
864
865 $content = json_decode($this->client->getResponse()->getContent(), true);
866
867 $this->assertArrayHasKey('tags', $content);
868 $this->assertSame($nbTags - 1, \count($content['tags']));
869 }
870
871 public function testSaveIsArchivedAfterPost()
872 {
873 $entry = $this->client->getContainer()
874 ->get('doctrine.orm.entity_manager')
875 ->getRepository('WallabagCoreBundle:Entry')
876 ->findOneBy(['user' => 1, 'isArchived' => true]);
877
878 if (!$entry) {
879 $this->markTestSkipped('No content found in db.');
880 }
881
882 $this->client->request('POST', '/api/entries.json', [
883 'url' => $entry->getUrl(),
884 ]);
885
886 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
887
888 $content = json_decode($this->client->getResponse()->getContent(), true);
889
890 $this->assertSame(1, $content['is_archived']);
891 }
892
893 public function testSaveIsStarredAfterPost()
894 {
895 $entry = $this->client->getContainer()
896 ->get('doctrine.orm.entity_manager')
897 ->getRepository('WallabagCoreBundle:Entry')
898 ->findOneBy(['user' => 1, 'isStarred' => true]);
899
900 if (!$entry) {
901 $this->markTestSkipped('No content found in db.');
902 }
903
904 $this->client->request('POST', '/api/entries.json', [
905 'url' => $entry->getUrl(),
906 ]);
907
908 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
909
910 $content = json_decode($this->client->getResponse()->getContent(), true);
911
912 $this->assertSame(1, $content['is_starred']);
913 }
914
915 public function testSaveIsArchivedAfterPatch()
916 {
917 $entry = $this->client->getContainer()
918 ->get('doctrine.orm.entity_manager')
919 ->getRepository('WallabagCoreBundle:Entry')
920 ->findOneBy(['user' => 1, 'isArchived' => true]);
921
922 if (!$entry) {
923 $this->markTestSkipped('No content found in db.');
924 }
925
926 $previousTitle = $entry->getTitle();
927
928 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
929 'title' => $entry->getTitle() . '++',
930 ]);
931
932 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
933
934 $content = json_decode($this->client->getResponse()->getContent(), true);
935
936 $this->assertSame(1, $content['is_archived']);
937 $this->assertSame($previousTitle . '++', $content['title']);
938 }
939
940 public function testSaveIsStarredAfterPatch()
941 {
942 $now = new \DateTime();
943 $entry = $this->client->getContainer()
944 ->get('doctrine.orm.entity_manager')
945 ->getRepository('WallabagCoreBundle:Entry')
946 ->findOneBy(['user' => 1, 'isStarred' => true]);
947
948 if (!$entry) {
949 $this->markTestSkipped('No content found in db.');
950 }
951 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
952 'title' => $entry->getTitle() . '++',
953 ]);
954
955 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
956
957 $content = json_decode($this->client->getResponse()->getContent(), true);
958
959 $this->assertSame(1, $content['is_starred']);
960 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['starred_at']))->getTimestamp());
961 }
962
963 public function dataForEntriesExistWithUrl()
964 {
965 return [
966 'with_id' => [
967 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1',
968 'expectedValue' => 2,
969 ],
970 'without_id' => [
971 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2',
972 'expectedValue' => true,
973 ],
974 ];
975 }
976
977 /**
978 * @dataProvider dataForEntriesExistWithUrl
979 */
980 public function testGetEntriesExists($url, $expectedValue)
981 {
982 $this->client->request('GET', $url);
983
984 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
985
986 $content = json_decode($this->client->getResponse()->getContent(), true);
987
988 $this->assertSame($expectedValue, $content['exists']);
989 }
990
991 public function testGetEntriesExistsWithManyUrls()
992 {
993 $url1 = 'http://0.0.0.0/entry2';
994 $url2 = 'http://0.0.0.0/entry10';
995 $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1');
996
997 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
998
999 $content = json_decode($this->client->getResponse()->getContent(), true);
1000
1001 $this->assertArrayHasKey($url1, $content);
1002 $this->assertArrayHasKey($url2, $content);
1003 $this->assertSame(2, $content[$url1]);
1004 $this->assertNull($content[$url2]);
1005 }
1006
1007 public function testGetEntriesExistsWithManyUrlsReturnBool()
1008 {
1009 $url1 = 'http://0.0.0.0/entry2';
1010 $url2 = 'http://0.0.0.0/entry10';
1011 $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2);
1012
1013 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1014
1015 $content = json_decode($this->client->getResponse()->getContent(), true);
1016
1017 $this->assertArrayHasKey($url1, $content);
1018 $this->assertArrayHasKey($url2, $content);
1019 $this->assertTrue($content[$url1]);
1020 $this->assertFalse($content[$url2]);
1021 }
1022
1023 public function testGetEntriesExistsWhichDoesNotExists()
1024 {
1025 $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2');
1026
1027 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1028
1029 $content = json_decode($this->client->getResponse()->getContent(), true);
1030
1031 $this->assertFalse($content['exists']);
1032 }
1033
1034 public function testGetEntriesExistsWithNoUrl()
1035 {
1036 $this->client->request('GET', '/api/entries/exists?url=');
1037
1038 $this->assertSame(403, $this->client->getResponse()->getStatusCode());
1039 }
1040
1041 public function testReloadEntryErrorWhileFetching()
1042 {
1043 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
1044 ->getRepository('WallabagCoreBundle:Entry')
1045 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1046
1047 if (!$entry) {
1048 $this->markTestSkipped('No content found in db.');
1049 }
1050
1051 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json');
1052 $this->assertSame(304, $this->client->getResponse()->getStatusCode());
1053 }
1054
1055 public function testReloadEntry()
1056 {
1057 $this->client->request('POST', '/api/entries.json', [
1058 'url' => 'https://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',
1059 'archive' => '1',
1060 'tags' => 'google, apple',
1061 ]);
1062
1063 $json = json_decode($this->client->getResponse()->getContent(), true);
1064
1065 $this->setUp();
1066
1067 $this->client->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json');
1068 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1069
1070 $content = json_decode($this->client->getResponse()->getContent(), true);
1071
1072 $this->assertNotEmpty($content['title']);
1073
1074 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
1075 }
1076
1077 public function testPostEntriesTagsListAction()
1078 {
1079 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
1080 ->getRepository('WallabagCoreBundle:Entry')
1081 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1082
1083 $tags = $entry->getTags();
1084
1085 $this->assertCount(2, $tags);
1086
1087 $list = [
1088 [
1089 'url' => 'http://0.0.0.0/entry4',
1090 'tags' => 'new tag 1, new tag 2',
1091 ],
1092 ];
1093
1094 $this->client->request('POST', '/api/entries/tags/lists?list=' . json_encode($list));
1095
1096 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1097
1098 $content = json_decode($this->client->getResponse()->getContent(), true);
1099
1100 $this->assertInternalType('int', $content[0]['entry']);
1101 $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']);
1102
1103 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
1104 ->getRepository('WallabagCoreBundle:Entry')
1105 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1106
1107 $tags = $entry->getTags();
1108 $this->assertCount(4, $tags);
1109 }
1110
1111 public function testPostEntriesTagsListActionNoList()
1112 {
1113 $this->client->request('POST', '/api/entries/tags/lists?list=' . json_encode([]));
1114
1115 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1116
1117 $content = json_decode($this->client->getResponse()->getContent(), true);
1118
1119 $this->assertEmpty($content);
1120 }
1121
1122 public function testDeleteEntriesTagsListAction()
1123 {
1124 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
1125 $entry = new Entry($em->getReference(User::class, 1));
1126 $entry->setUrl('http://0.0.0.0/test-entry');
1127 $entry->addTag((new Tag())->setLabel('foo-tag'));
1128 $entry->addTag((new Tag())->setLabel('bar-tag'));
1129 $em->persist($entry);
1130 $em->flush();
1131
1132 $em->clear();
1133
1134 $list = [
1135 [
1136 'url' => 'http://0.0.0.0/test-entry',
1137 'tags' => 'foo-tag, bar-tag',
1138 ],
1139 ];
1140
1141 $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list));
1142 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1143
1144 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
1145 $this->assertCount(0, $entry->getTags());
1146 }
1147
1148 public function testDeleteEntriesTagsListActionNoList()
1149 {
1150 $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode([]));
1151
1152 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1153
1154 $content = json_decode($this->client->getResponse()->getContent(), true);
1155
1156 $this->assertEmpty($content);
1157 }
1158
1159 public function testPostEntriesListAction()
1160 {
1161 $list = [
1162 'https://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
1163 'http://0.0.0.0/entry2',
1164 ];
1165
1166 $this->client->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1167
1168 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1169
1170 $content = json_decode($this->client->getResponse()->getContent(), true);
1171
1172 $this->assertInternalType('int', $content[0]['entry']);
1173 $this->assertSame('https://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']);
1174
1175 $this->assertInternalType('int', $content[1]['entry']);
1176 $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']);
1177 }
1178
1179 public function testPostEntriesListActionWithNoUrls()
1180 {
1181 $this->client->request('POST', '/api/entries/lists?urls=' . json_encode([]));
1182
1183 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1184
1185 $content = json_decode($this->client->getResponse()->getContent(), true);
1186
1187 $this->assertEmpty($content);
1188 }
1189
1190 public function testDeleteEntriesListAction()
1191 {
1192 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
1193 $em->persist((new Entry($em->getReference(User::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
1194
1195 $em->flush();
1196 $em->clear();
1197 $list = [
1198 'http://0.0.0.0/test-entry1',
1199 'http://0.0.0.0/test-entry-not-exist',
1200 ];
1201
1202 $this->client->request('DELETE', '/api/entries/list?urls=' . json_encode($list));
1203
1204 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1205
1206 $content = json_decode($this->client->getResponse()->getContent(), true);
1207
1208 $this->assertTrue($content[0]['entry']);
1209 $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']);
1210
1211 $this->assertFalse($content[1]['entry']);
1212 $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
1213 }
1214
1215 public function testDeleteEntriesListActionWithNoUrls()
1216 {
1217 $this->client->request('DELETE', '/api/entries/list?urls=' . json_encode([]));
1218
1219 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1220
1221 $content = json_decode($this->client->getResponse()->getContent(), true);
1222
1223 $this->assertEmpty($content);
1224 }
1225
1226 public function testLimitBulkAction()
1227 {
1228 $list = [
1229 'http://0.0.0.0/entry1',
1230 'http://0.0.0.0/entry1',
1231 'http://0.0.0.0/entry1',
1232 'http://0.0.0.0/entry1',
1233 'http://0.0.0.0/entry1',
1234 'http://0.0.0.0/entry1',
1235 'http://0.0.0.0/entry1',
1236 'http://0.0.0.0/entry1',
1237 'http://0.0.0.0/entry1',
1238 'http://0.0.0.0/entry1',
1239 'http://0.0.0.0/entry1',
1240 ];
1241
1242 $this->client->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1243
1244 $this->assertSame(400, $this->client->getResponse()->getStatusCode());
1245 $this->assertContains('API limit reached', $this->client->getResponse()->getContent());
1246 }
1247
1248 public function testRePostEntryAndReUsePublishedAt()
1249 {
1250 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
1251 $entry = new Entry($em->getReference(User::class, 1));
1252 $entry->setTitle('Antoine de Caunes : « Je veux avoir le droit de tâtonner »');
1253 $entry->setContent('hihi');
1254 $entry->setUrl('https://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html');
1255 $entry->setPublishedAt(new \DateTime('2017-06-26T07:46:02+0200'));
1256 $em->persist($entry);
1257 $em->flush();
1258 $em->clear();
1259
1260 $this->client->request('POST', '/api/entries.json', [
1261 'url' => 'https://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html',
1262 ]);
1263
1264 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1265
1266 $content = json_decode($this->client->getResponse()->getContent(), true);
1267
1268 $this->assertGreaterThan(0, $content['id']);
1269 $this->assertSame('https://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html', $content['url']);
1270 }
1271 }