]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
Merge pull request #3982 from wallabag/fix/https-test
[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 $client = $this->createAuthorizedClient();
430 $client->request('DELETE', '/api/entries/' . $e['id'] . '.json');
431
432 $this->assertSame(404, $client->getResponse()->getStatusCode());
433 }
434
435 public function testDeleteEntryExpectId()
436 {
437 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
438 $entry = new Entry($em->getReference(User::class, 1));
439 $entry->setUrl('http://0.0.0.0/test-delete-entry-id');
440 $em->persist($entry);
441 $em->flush();
442
443 $em->clear();
444
445 $id = $entry->getId();
446
447 $this->client->request('DELETE', '/api/entries/' . $id . '.json?expect=id');
448
449 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
450
451 $content = json_decode($this->client->getResponse()->getContent(), true);
452
453 $this->assertSame($id, $content['id']);
454 $this->assertArrayNotHasKey('url', $content);
455
456 // We'll try to delete this entry again
457 $client = $this->createAuthorizedClient();
458 $client->request('DELETE', '/api/entries/' . $id . '.json');
459
460 $this->assertSame(404, $client->getResponse()->getStatusCode());
461 }
462
463 public function testDeleteEntryExpectBadRequest()
464 {
465 $this->client->request('DELETE', '/api/entries/1.json?expect=badrequest');
466
467 $this->assertSame(400, $this->client->getResponse()->getStatusCode());
468 }
469
470 public function testPostEntry()
471 {
472 $this->client->request('POST', '/api/entries.json', [
473 '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',
474 'tags' => 'google',
475 'title' => 'New title for my article',
476 'content' => 'my content',
477 'language' => 'de',
478 'published_at' => '2016-09-08T11:55:58+0200',
479 'authors' => 'bob,helen',
480 'public' => 1,
481 ]);
482
483 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
484
485 $content = json_decode($this->client->getResponse()->getContent(), true);
486
487 $this->assertGreaterThan(0, $content['id']);
488 $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']);
489 $this->assertSame(0, $content['is_archived']);
490 $this->assertSame(0, $content['is_starred']);
491 $this->assertNull($content['starred_at']);
492 $this->assertSame('New title for my article', $content['title']);
493 $this->assertSame(1, $content['user_id']);
494 $this->assertCount(2, $content['tags']);
495 $this->assertNull($content['origin_url']);
496 $this->assertSame('my content', $content['content']);
497 $this->assertSame('de', $content['language']);
498 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
499 $this->assertCount(2, $content['published_by']);
500 $this->assertContains('bob', $content['published_by']);
501 $this->assertContains('helen', $content['published_by']);
502 $this->assertTrue($content['is_public'], 'A public link has been generated for that entry');
503 }
504
505 public function testPostSameEntry()
506 {
507 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
508 $entry = new Entry($em->getReference(User::class, 1));
509 $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');
510 $entry->setArchived(true);
511 $entry->addTag((new Tag())->setLabel('google'));
512 $entry->addTag((new Tag())->setLabel('apple'));
513 $em->persist($entry);
514 $em->flush();
515 $em->clear();
516
517 $this->client->request('POST', '/api/entries.json', [
518 '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',
519 'archive' => '1',
520 'tags' => 'google, apple',
521 ]);
522
523 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
524
525 $content = json_decode($this->client->getResponse()->getContent(), true);
526
527 $this->assertGreaterThan(0, $content['id']);
528 $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']);
529 $this->assertSame(1, $content['is_archived']);
530 $this->assertSame(0, $content['is_starred']);
531 $this->assertCount(3, $content['tags']);
532 }
533
534 public function testPostEntryWhenFetchContentFails()
535 {
536 /** @var \Symfony\Component\DependencyInjection\Container $container */
537 $container = $this->client->getContainer();
538 $contentProxy = $this->getMockBuilder(ContentProxy::class)
539 ->disableOriginalConstructor()
540 ->setMethods(['updateEntry'])
541 ->getMock();
542 $contentProxy->expects($this->any())
543 ->method('updateEntry')
544 ->willThrowException(new \Exception('Test Fetch content fails'));
545 $container->set('wallabag_core.content_proxy', $contentProxy);
546
547 try {
548 $this->client->request('POST', '/api/entries.json', [
549 'url' => 'http://www.example.com/',
550 ]);
551
552 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
553 $content = json_decode($this->client->getResponse()->getContent(), true);
554 $this->assertGreaterThan(0, $content['id']);
555 $this->assertSame('http://www.example.com/', $content['url']);
556 $this->assertSame('www.example.com', $content['domain_name']);
557 $this->assertSame('www.example.com', $content['title']);
558 } finally {
559 // Remove the created entry to avoid side effects on other tests
560 if (isset($content['id'])) {
561 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
562 $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']);
563 $em->remove($entry);
564 $em->flush();
565 }
566 }
567 }
568
569 public function testPostArchivedAndStarredEntry()
570 {
571 $now = new \DateTime();
572 $this->client->request('POST', '/api/entries.json', [
573 'url' => 'https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
574 'archive' => '1',
575 'starred' => '1',
576 ]);
577
578 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
579
580 $content = json_decode($this->client->getResponse()->getContent(), true);
581
582 $this->assertGreaterThan(0, $content['id']);
583 $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']);
584 $this->assertSame(1, $content['is_archived']);
585 $this->assertSame(1, $content['is_starred']);
586 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['starred_at']))->getTimestamp());
587 $this->assertSame(1, $content['user_id']);
588 }
589
590 public function testPostArchivedAndStarredEntryWithoutQuotes()
591 {
592 $this->client->request('POST', '/api/entries.json', [
593 'url' => 'https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
594 'archive' => 0,
595 'starred' => 1,
596 ]);
597
598 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
599
600 $content = json_decode($this->client->getResponse()->getContent(), true);
601
602 $this->assertGreaterThan(0, $content['id']);
603 $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']);
604 $this->assertSame(0, $content['is_archived']);
605 $this->assertSame(1, $content['is_starred']);
606 }
607
608 public function testPostEntryWithOriginUrl()
609 {
610 $this->client->request('POST', '/api/entries.json', [
611 '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',
612 'tags' => 'google',
613 'title' => 'New title for my article',
614 'content' => 'my content',
615 'language' => 'de',
616 'published_at' => '2016-09-08T11:55:58+0200',
617 'authors' => 'bob,helen',
618 'public' => 1,
619 'origin_url' => 'http://mysource.tld',
620 ]);
621
622 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
623
624 $content = json_decode($this->client->getResponse()->getContent(), true);
625
626 $this->assertGreaterThan(0, $content['id']);
627 $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']);
628 $this->assertSame('http://mysource.tld', $content['origin_url']);
629 }
630
631 public function testPatchEntry()
632 {
633 $entry = $this->client->getContainer()
634 ->get('doctrine.orm.entity_manager')
635 ->getRepository('WallabagCoreBundle:Entry')
636 ->findOneByUser(1);
637
638 if (!$entry) {
639 $this->markTestSkipped('No content found in db.');
640 }
641
642 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
643 'title' => 'New awesome title',
644 'tags' => 'new tag ' . uniqid(),
645 'starred' => '1',
646 'archive' => '0',
647 'language' => 'de_AT',
648 'preview_picture' => 'http://preview.io/picture.jpg',
649 'authors' => 'bob,sponge',
650 'content' => 'awesome',
651 'public' => 0,
652 'published_at' => 1488833381,
653 ]);
654
655 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
656
657 $content = json_decode($this->client->getResponse()->getContent(), true);
658
659 $this->assertSame($entry->getId(), $content['id']);
660 $this->assertSame($entry->getUrl(), $content['url']);
661 $this->assertSame('New awesome title', $content['title']);
662 $this->assertGreaterThanOrEqual(1, \count($content['tags']), 'We force only one tag');
663 $this->assertSame(1, $content['user_id']);
664 $this->assertSame('de_AT', $content['language']);
665 $this->assertSame('http://preview.io/picture.jpg', $content['preview_picture']);
666 $this->assertContains('sponge', $content['published_by']);
667 $this->assertContains('bob', $content['published_by']);
668 $this->assertSame('awesome', $content['content']);
669 $this->assertFalse($content['is_public'], 'Entry is no more shared');
670 $this->assertContains('2017-03-06', $content['published_at']);
671 }
672
673 public function testPatchEntryWithoutQuotes()
674 {
675 $entry = $this->client->getContainer()
676 ->get('doctrine.orm.entity_manager')
677 ->getRepository('WallabagCoreBundle:Entry')
678 ->findOneByUser(1);
679
680 if (!$entry) {
681 $this->markTestSkipped('No content found in db.');
682 }
683
684 $previousContent = $entry->getContent();
685 $previousLanguage = $entry->getLanguage();
686
687 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
688 'title' => 'New awesome title',
689 'tags' => 'new tag ' . uniqid(),
690 'starred' => 1,
691 'archive' => 0,
692 'authors' => ['bob', 'sponge'],
693 ]);
694
695 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
696
697 $content = json_decode($this->client->getResponse()->getContent(), true);
698
699 $this->assertSame($entry->getId(), $content['id']);
700 $this->assertSame($entry->getUrl(), $content['url']);
701 $this->assertGreaterThanOrEqual(1, \count($content['tags']), 'We force only one tag');
702 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
703 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
704 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
705 }
706
707 public function testPatchEntryWithOriginUrl()
708 {
709 $entry = $this->client->getContainer()
710 ->get('doctrine.orm.entity_manager')
711 ->getRepository('WallabagCoreBundle:Entry')
712 ->findOneByUser(1);
713
714 if (!$entry) {
715 $this->markTestSkipped('No content found in db.');
716 }
717
718 $previousContent = $entry->getContent();
719 $previousLanguage = $entry->getLanguage();
720
721 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
722 'title' => 'Another awesome title just for profit',
723 'origin_url' => 'https://myawesomesource.example.com',
724 ]);
725
726 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
727
728 $content = json_decode($this->client->getResponse()->getContent(), true);
729
730 $this->assertSame($entry->getId(), $content['id']);
731 $this->assertSame($entry->getUrl(), $content['url']);
732 $this->assertSame('https://myawesomesource.example.com', $content['origin_url']);
733 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
734 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
735 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
736 }
737
738 public function testPatchEntryRemoveOriginUrl()
739 {
740 $entry = $this->client->getContainer()
741 ->get('doctrine.orm.entity_manager')
742 ->getRepository('WallabagCoreBundle:Entry')
743 ->findOneByUser(1);
744
745 if (!$entry) {
746 $this->markTestSkipped('No content found in db.');
747 }
748
749 $previousContent = $entry->getContent();
750 $previousLanguage = $entry->getLanguage();
751 $previousTitle = $entry->getTitle();
752
753 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
754 'origin_url' => '',
755 ]);
756
757 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
758
759 $content = json_decode($this->client->getResponse()->getContent(), true);
760
761 $this->assertSame($entry->getId(), $content['id']);
762 $this->assertSame($entry->getUrl(), $content['url']);
763 $this->assertEmpty($content['origin_url']);
764 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
765 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
766 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
767 $this->assertSame($previousTitle, $content['title'], 'Ensure title has not moved');
768 }
769
770 public function testPatchEntryNullOriginUrl()
771 {
772 $entry = $this->client->getContainer()
773 ->get('doctrine.orm.entity_manager')
774 ->getRepository('WallabagCoreBundle:Entry')
775 ->findOneByUser(1);
776
777 if (!$entry) {
778 $this->markTestSkipped('No content found in db.');
779 }
780
781 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
782 'origin_url' => null,
783 ]);
784
785 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
786
787 $content = json_decode($this->client->getResponse()->getContent(), true);
788
789 $this->assertNull($content['origin_url']);
790 }
791
792 public function testGetTagsEntry()
793 {
794 $entry = $this->client->getContainer()
795 ->get('doctrine.orm.entity_manager')
796 ->getRepository('WallabagCoreBundle:Entry')
797 ->findOneWithTags($this->user->getId());
798
799 $entry = $entry[0];
800
801 if (!$entry) {
802 $this->markTestSkipped('No content found in db.');
803 }
804
805 $tags = [];
806 foreach ($entry->getTags() as $tag) {
807 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
808 }
809
810 $this->client->request('GET', '/api/entries/' . $entry->getId() . '/tags');
811
812 $this->assertSame(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent());
813 }
814
815 public function testPostTagsOnEntry()
816 {
817 $entry = $this->client->getContainer()
818 ->get('doctrine.orm.entity_manager')
819 ->getRepository('WallabagCoreBundle:Entry')
820 ->findOneByUser(1);
821
822 if (!$entry) {
823 $this->markTestSkipped('No content found in db.');
824 }
825
826 $nbTags = \count($entry->getTags());
827
828 $newTags = 'tag1,tag2,tag3';
829
830 $this->client->request('POST', '/api/entries/' . $entry->getId() . '/tags', ['tags' => $newTags]);
831
832 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
833
834 $content = json_decode($this->client->getResponse()->getContent(), true);
835
836 $this->assertArrayHasKey('tags', $content);
837 $this->assertSame($nbTags + 3, \count($content['tags']));
838
839 $entryDB = $this->client->getContainer()
840 ->get('doctrine.orm.entity_manager')
841 ->getRepository('WallabagCoreBundle:Entry')
842 ->find($entry->getId());
843
844 $tagsInDB = [];
845 foreach ($entryDB->getTags()->toArray() as $tag) {
846 $tagsInDB[$tag->getId()] = $tag->getLabel();
847 }
848
849 foreach (explode(',', $newTags) as $tag) {
850 $this->assertContains($tag, $tagsInDB);
851 }
852 }
853
854 public function testDeleteOneTagEntry()
855 {
856 $entry = $this->client->getContainer()
857 ->get('doctrine.orm.entity_manager')
858 ->getRepository('WallabagCoreBundle:Entry')
859 ->findOneWithTags($this->user->getId());
860 $entry = $entry[0];
861
862 if (!$entry) {
863 $this->markTestSkipped('No content found in db.');
864 }
865
866 // hydrate the tags relations
867 $nbTags = \count($entry->getTags());
868 $tag = $entry->getTags()[0];
869
870 $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '/tags/' . $tag->getId() . '.json');
871
872 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
873
874 $content = json_decode($this->client->getResponse()->getContent(), true);
875
876 $this->assertArrayHasKey('tags', $content);
877 $this->assertSame($nbTags - 1, \count($content['tags']));
878 }
879
880 public function testSaveIsArchivedAfterPost()
881 {
882 $entry = $this->client->getContainer()
883 ->get('doctrine.orm.entity_manager')
884 ->getRepository('WallabagCoreBundle:Entry')
885 ->findOneBy(['user' => 1, 'isArchived' => true]);
886
887 if (!$entry) {
888 $this->markTestSkipped('No content found in db.');
889 }
890
891 $this->client->request('POST', '/api/entries.json', [
892 'url' => $entry->getUrl(),
893 ]);
894
895 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
896
897 $content = json_decode($this->client->getResponse()->getContent(), true);
898
899 $this->assertSame(1, $content['is_archived']);
900 }
901
902 public function testSaveIsStarredAfterPost()
903 {
904 $entry = $this->client->getContainer()
905 ->get('doctrine.orm.entity_manager')
906 ->getRepository('WallabagCoreBundle:Entry')
907 ->findOneBy(['user' => 1, 'isStarred' => true]);
908
909 if (!$entry) {
910 $this->markTestSkipped('No content found in db.');
911 }
912
913 $this->client->request('POST', '/api/entries.json', [
914 'url' => $entry->getUrl(),
915 ]);
916
917 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
918
919 $content = json_decode($this->client->getResponse()->getContent(), true);
920
921 $this->assertSame(1, $content['is_starred']);
922 }
923
924 public function testSaveIsArchivedAfterPatch()
925 {
926 $entry = $this->client->getContainer()
927 ->get('doctrine.orm.entity_manager')
928 ->getRepository('WallabagCoreBundle:Entry')
929 ->findOneBy(['user' => 1, 'isArchived' => true]);
930
931 if (!$entry) {
932 $this->markTestSkipped('No content found in db.');
933 }
934
935 $previousTitle = $entry->getTitle();
936
937 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
938 'title' => $entry->getTitle() . '++',
939 ]);
940
941 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
942
943 $content = json_decode($this->client->getResponse()->getContent(), true);
944
945 $this->assertSame(1, $content['is_archived']);
946 $this->assertSame($previousTitle . '++', $content['title']);
947 }
948
949 public function testSaveIsStarredAfterPatch()
950 {
951 $now = new \DateTime();
952 $entry = $this->client->getContainer()
953 ->get('doctrine.orm.entity_manager')
954 ->getRepository('WallabagCoreBundle:Entry')
955 ->findOneBy(['user' => 1, 'isStarred' => true]);
956
957 if (!$entry) {
958 $this->markTestSkipped('No content found in db.');
959 }
960 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
961 'title' => $entry->getTitle() . '++',
962 ]);
963
964 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
965
966 $content = json_decode($this->client->getResponse()->getContent(), true);
967
968 $this->assertSame(1, $content['is_starred']);
969 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['starred_at']))->getTimestamp());
970 }
971
972 public function dataForEntriesExistWithUrl()
973 {
974 return [
975 'with_id' => [
976 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1',
977 'expectedValue' => 2,
978 ],
979 'without_id' => [
980 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2',
981 'expectedValue' => true,
982 ],
983 ];
984 }
985
986 /**
987 * @dataProvider dataForEntriesExistWithUrl
988 */
989 public function testGetEntriesExists($url, $expectedValue)
990 {
991 $this->client->request('GET', $url);
992
993 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
994
995 $content = json_decode($this->client->getResponse()->getContent(), true);
996
997 $this->assertSame($expectedValue, $content['exists']);
998 }
999
1000 public function testGetEntriesExistsWithManyUrls()
1001 {
1002 $url1 = 'http://0.0.0.0/entry2';
1003 $url2 = 'http://0.0.0.0/entry10';
1004 $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1');
1005
1006 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1007
1008 $content = json_decode($this->client->getResponse()->getContent(), true);
1009
1010 $this->assertArrayHasKey($url1, $content);
1011 $this->assertArrayHasKey($url2, $content);
1012 $this->assertSame(2, $content[$url1]);
1013 $this->assertNull($content[$url2]);
1014 }
1015
1016 public function testGetEntriesExistsWithManyUrlsReturnBool()
1017 {
1018 $url1 = 'http://0.0.0.0/entry2';
1019 $url2 = 'http://0.0.0.0/entry10';
1020 $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2);
1021
1022 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1023
1024 $content = json_decode($this->client->getResponse()->getContent(), true);
1025
1026 $this->assertArrayHasKey($url1, $content);
1027 $this->assertArrayHasKey($url2, $content);
1028 $this->assertTrue($content[$url1]);
1029 $this->assertFalse($content[$url2]);
1030 }
1031
1032 public function testGetEntriesExistsWhichDoesNotExists()
1033 {
1034 $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2');
1035
1036 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1037
1038 $content = json_decode($this->client->getResponse()->getContent(), true);
1039
1040 $this->assertFalse($content['exists']);
1041 }
1042
1043 public function testGetEntriesExistsWithNoUrl()
1044 {
1045 $this->client->request('GET', '/api/entries/exists?url=');
1046
1047 $this->assertSame(403, $this->client->getResponse()->getStatusCode());
1048 }
1049
1050 public function testReloadEntryErrorWhileFetching()
1051 {
1052 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
1053 ->getRepository('WallabagCoreBundle:Entry')
1054 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1055
1056 if (!$entry) {
1057 $this->markTestSkipped('No content found in db.');
1058 }
1059
1060 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json');
1061 $this->assertSame(304, $this->client->getResponse()->getStatusCode());
1062 }
1063
1064 public function testReloadEntry()
1065 {
1066 $this->client->request('POST', '/api/entries.json', [
1067 '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',
1068 'archive' => '1',
1069 'tags' => 'google, apple',
1070 ]);
1071
1072 $json = json_decode($this->client->getResponse()->getContent(), true);
1073
1074 $this->setUp();
1075
1076 $this->client->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json');
1077 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1078
1079 $content = json_decode($this->client->getResponse()->getContent(), true);
1080
1081 $this->assertNotEmpty($content['title']);
1082
1083 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
1084 }
1085
1086 public function testPostEntriesTagsListAction()
1087 {
1088 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
1089 ->getRepository('WallabagCoreBundle:Entry')
1090 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1091
1092 $tags = $entry->getTags();
1093
1094 $this->assertCount(2, $tags);
1095
1096 $list = [
1097 [
1098 'url' => 'http://0.0.0.0/entry4',
1099 'tags' => 'new tag 1, new tag 2',
1100 ],
1101 ];
1102
1103 $this->client->request('POST', '/api/entries/tags/lists?list=' . json_encode($list));
1104
1105 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1106
1107 $content = json_decode($this->client->getResponse()->getContent(), true);
1108
1109 $this->assertInternalType('int', $content[0]['entry']);
1110 $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']);
1111
1112 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
1113 ->getRepository('WallabagCoreBundle:Entry')
1114 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
1115
1116 $tags = $entry->getTags();
1117 $this->assertCount(4, $tags);
1118 }
1119
1120 public function testPostEntriesTagsListActionNoList()
1121 {
1122 $this->client->request('POST', '/api/entries/tags/lists?list=' . json_encode([]));
1123
1124 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1125
1126 $content = json_decode($this->client->getResponse()->getContent(), true);
1127
1128 $this->assertEmpty($content);
1129 }
1130
1131 public function testDeleteEntriesTagsListAction()
1132 {
1133 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
1134 $entry = new Entry($em->getReference(User::class, 1));
1135 $entry->setUrl('http://0.0.0.0/test-entry');
1136 $entry->addTag((new Tag())->setLabel('foo-tag'));
1137 $entry->addTag((new Tag())->setLabel('bar-tag'));
1138 $em->persist($entry);
1139 $em->flush();
1140
1141 $em->clear();
1142
1143 $list = [
1144 [
1145 'url' => 'http://0.0.0.0/test-entry',
1146 'tags' => 'foo-tag, bar-tag',
1147 ],
1148 ];
1149
1150 $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list));
1151 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1152
1153 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
1154 $this->assertCount(0, $entry->getTags());
1155 }
1156
1157 public function testDeleteEntriesTagsListActionNoList()
1158 {
1159 $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode([]));
1160
1161 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1162
1163 $content = json_decode($this->client->getResponse()->getContent(), true);
1164
1165 $this->assertEmpty($content);
1166 }
1167
1168 public function testPostEntriesListAction()
1169 {
1170 $list = [
1171 'https://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
1172 'http://0.0.0.0/entry2',
1173 ];
1174
1175 $this->client->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1176
1177 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1178
1179 $content = json_decode($this->client->getResponse()->getContent(), true);
1180
1181 $this->assertInternalType('int', $content[0]['entry']);
1182 $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']);
1183
1184 $this->assertInternalType('int', $content[1]['entry']);
1185 $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']);
1186 }
1187
1188 public function testPostEntriesListActionWithNoUrls()
1189 {
1190 $this->client->request('POST', '/api/entries/lists?urls=' . json_encode([]));
1191
1192 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1193
1194 $content = json_decode($this->client->getResponse()->getContent(), true);
1195
1196 $this->assertEmpty($content);
1197 }
1198
1199 public function testDeleteEntriesListAction()
1200 {
1201 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
1202 $em->persist((new Entry($em->getReference(User::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
1203
1204 $em->flush();
1205 $em->clear();
1206 $list = [
1207 'http://0.0.0.0/test-entry1',
1208 'http://0.0.0.0/test-entry-not-exist',
1209 ];
1210
1211 $this->client->request('DELETE', '/api/entries/list?urls=' . json_encode($list));
1212
1213 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1214
1215 $content = json_decode($this->client->getResponse()->getContent(), true);
1216
1217 $this->assertTrue($content[0]['entry']);
1218 $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']);
1219
1220 $this->assertFalse($content[1]['entry']);
1221 $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
1222 }
1223
1224 public function testDeleteEntriesListActionWithNoUrls()
1225 {
1226 $this->client->request('DELETE', '/api/entries/list?urls=' . json_encode([]));
1227
1228 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1229
1230 $content = json_decode($this->client->getResponse()->getContent(), true);
1231
1232 $this->assertEmpty($content);
1233 }
1234
1235 public function testLimitBulkAction()
1236 {
1237 $list = [
1238 'http://0.0.0.0/entry1',
1239 'http://0.0.0.0/entry1',
1240 'http://0.0.0.0/entry1',
1241 'http://0.0.0.0/entry1',
1242 'http://0.0.0.0/entry1',
1243 'http://0.0.0.0/entry1',
1244 'http://0.0.0.0/entry1',
1245 'http://0.0.0.0/entry1',
1246 'http://0.0.0.0/entry1',
1247 'http://0.0.0.0/entry1',
1248 'http://0.0.0.0/entry1',
1249 ];
1250
1251 $this->client->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1252
1253 $this->assertSame(400, $this->client->getResponse()->getStatusCode());
1254 $this->assertContains('API limit reached', $this->client->getResponse()->getContent());
1255 }
1256
1257 public function testRePostEntryAndReUsePublishedAt()
1258 {
1259 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
1260 $entry = new Entry($em->getReference(User::class, 1));
1261 $entry->setTitle('Antoine de Caunes : « Je veux avoir le droit de tâtonner »');
1262 $entry->setContent('hihi');
1263 $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');
1264 $entry->setPublishedAt(new \DateTime('2017-06-26T07:46:02+0200'));
1265 $em->persist($entry);
1266 $em->flush();
1267 $em->clear();
1268
1269 $this->client->request('POST', '/api/entries.json', [
1270 'url' => 'https://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html',
1271 ]);
1272
1273 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1274
1275 $content = json_decode($this->client->getResponse()->getContent(), true);
1276
1277 $this->assertGreaterThan(0, $content['id']);
1278 $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']);
1279 }
1280 }