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