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