]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
Add public filter/field in the API
[github/wallabag/wallabag.git] / tests / Wallabag / ApiBundle / Controller / EntryRestControllerTest.php
CommitLineData
900c8448
NL
1<?php
2
3namespace Tests\Wallabag\ApiBundle\Controller;
4
5use Tests\Wallabag\ApiBundle\WallabagApiTestCase;
7ab5eb95 6use Wallabag\CoreBundle\Entity\Entry;
900c8448 7use Wallabag\CoreBundle\Entity\Tag;
08f29ae7 8use Wallabag\CoreBundle\Helper\ContentProxy;
7ab5eb95 9use Wallabag\UserBundle\Entity\User;
900c8448
NL
10
11class 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->assertEquals(200, $this->client->getResponse()->getStatusCode());
26
27 $content = json_decode($this->client->getResponse()->getContent(), true);
28
29 $this->assertEquals($entry->getTitle(), $content['title']);
30 $this->assertEquals($entry->getUrl(), $content['url']);
31 $this->assertCount(count($entry->getTags()), $content['tags']);
32 $this->assertEquals($entry->getUserName(), $content['user_name']);
33 $this->assertEquals($entry->getUserEmail(), $content['user_email']);
34 $this->assertEquals($entry->getUserId(), $content['user_id']);
35
5a619812
JB
36 $this->assertEquals('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->assertEquals(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->assertEquals('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->assertEquals(200, $client->getResponse()->getStatusCode());
61
62 $this->assertEquals('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->assertEquals(200, $client->getResponse()->getStatusCode());
68
69 $this->assertContains('PDF-', $client->getResponse()->getContent());
70 $this->assertEquals('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->assertEquals(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->assertEquals(200, $client->getResponse()->getStatusCode());
83
84 $this->assertContains('application/csv', $client->getResponse()->headers->get('Content-Type'));
900c8448
NL
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->assertEquals(403, $this->client->getResponse()->getStatusCode());
101 }
102
103 public function testGetEntries()
104 {
105 $this->client->request('GET', '/api/entries');
106
107 $this->assertEquals(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->assertEquals(1, $content['page']);
115 $this->assertGreaterThanOrEqual(1, $content['pages']);
116
5a619812 117 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
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,
1112e547 131 'public' => 0,
900c8448
NL
132 ]);
133
134 $this->assertEquals(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->assertEquals(1, $content['page']);
142 $this->assertEquals(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']);
1112e547
JB
158 $this->assertContains('public=0', $content['_links'][$link]['href']);
159 }
160
161 $this->assertEquals('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->assertEquals(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->assertEquals(1, $content['page']);
194 $this->assertEquals(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']);
900c8448
NL
205 }
206
5a619812 207 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
208 }
209
b60a666d 210 public function testGetEntriesOnPageTwo()
211 {
212 $this->client->request('GET', '/api/entries', [
213 'page' => 2,
214 'perPage' => 2,
215 ]);
216
217 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
218
219 $content = json_decode($this->client->getResponse()->getContent(), true);
220
221 $this->assertGreaterThanOrEqual(0, $content['total']);
222 $this->assertEquals(2, $content['page']);
223 $this->assertEquals(2, $content['limit']);
224 }
225
900c8448
NL
226 public function testGetStarredEntries()
227 {
228 $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']);
229
230 $this->assertEquals(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->assertEquals(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
5a619812 251 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
252 }
253
254 public function testGetArchiveEntries()
255 {
256 $this->client->request('GET', '/api/entries', ['archive' => 1]);
257
258 $this->assertEquals(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->assertEquals(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
5a619812 278 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
279 }
280
281 public function testGetTaggedEntries()
282 {
283 $this->client->request('GET', '/api/entries', ['tags' => 'foo,bar']);
284
285 $this->assertEquals(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->assertEquals(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
5a619812 305 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
306 }
307
308 public function testGetDatedEntries()
309 {
310 $this->client->request('GET', '/api/entries', ['since' => 1443274283]);
311
312 $this->assertEquals(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->assertEquals(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
5a619812 332 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
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->assertEquals(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->assertEquals(0, $content['total']);
347 $this->assertEquals(1, $content['page']);
348 $this->assertEquals(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
5a619812 360 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
361 }
362
363 public function testDeleteEntry()
364 {
365 $entry = $this->client->getContainer()
366 ->get('doctrine.orm.entity_manager')
367 ->getRepository('WallabagCoreBundle:Entry')
dcbebc17 368 ->findOneByUser(1, ['id' => 'asc']);
900c8448
NL
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->assertEquals(200, $this->client->getResponse()->getStatusCode());
377
378 $content = json_decode($this->client->getResponse()->getContent(), true);
379
380 $this->assertEquals($entry->getTitle(), $content['title']);
381 $this->assertEquals($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->assertEquals(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',
e668a812 395 'content' => 'my content',
e9056dd9 396 'language' => 'de',
e668a812 397 'published_at' => '2016-09-08T11:55:58+0200',
fb436e8c 398 'authors' => 'bob,helen',
1112e547 399 'public' => 1,
900c8448
NL
400 ]);
401
402 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
403
404 $content = json_decode($this->client->getResponse()->getContent(), true);
405
406 $this->assertGreaterThan(0, $content['id']);
407 $this->assertEquals('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->assertEquals(false, $content['is_archived']);
409 $this->assertEquals(false, $content['is_starred']);
410 $this->assertEquals('New title for my article', $content['title']);
411 $this->assertEquals(1, $content['user_id']);
fdd725f5 412 $this->assertCount(2, $content['tags']);
e668a812 413 $this->assertSame('my content', $content['content']);
e9056dd9 414 $this->assertSame('de', $content['language']);
e668a812 415 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
fb436e8c
JB
416 $this->assertCount(2, $content['published_by']);
417 $this->assertContains('bob', $content['published_by']);
418 $this->assertContains('helen', $content['published_by']);
1112e547 419 $this->assertTrue($content['is_public'], 'A public link has been generated for that entry');
900c8448
NL
420 }
421
422 public function testPostSameEntry()
423 {
424 $this->client->request('POST', '/api/entries.json', [
425 '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',
426 'archive' => '1',
427 'tags' => 'google, apple',
428 ]);
429
430 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
431
432 $content = json_decode($this->client->getResponse()->getContent(), true);
433
434 $this->assertGreaterThan(0, $content['id']);
435 $this->assertEquals('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']);
436 $this->assertEquals(true, $content['is_archived']);
437 $this->assertEquals(false, $content['is_starred']);
fdd725f5 438 $this->assertCount(3, $content['tags']);
900c8448
NL
439 }
440
08f29ae7 441 public function testPostEntryWhenFetchContentFails()
442 {
443 /** @var \Symfony\Component\DependencyInjection\Container $container */
444 $container = $this->client->getContainer();
445 $contentProxy = $this->getMockBuilder(ContentProxy::class)
446 ->disableOriginalConstructor()
447 ->setMethods(['updateEntry'])
448 ->getMock();
449 $contentProxy->expects($this->any())
450 ->method('updateEntry')
451 ->willThrowException(new \Exception('Test Fetch content fails'));
452 $container->set('wallabag_core.content_proxy', $contentProxy);
453
a9357a83 454 try {
455 $this->client->request('POST', '/api/entries.json', [
456 'url' => 'http://www.example.com/',
457 ]);
458
459 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
460 $content = json_decode($this->client->getResponse()->getContent(), true);
461 $this->assertGreaterThan(0, $content['id']);
462 $this->assertEquals('http://www.example.com/', $content['url']);
463 } finally {
464 // Remove the created entry to avoid side effects on other tests
465 if (isset($content['id'])) {
466 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
467 $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']);
468 $em->remove($entry);
469 $em->flush();
470 }
471 }
08f29ae7 472 }
473
900c8448
NL
474 public function testPostArchivedAndStarredEntry()
475 {
476 $this->client->request('POST', '/api/entries.json', [
477 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
478 'archive' => '1',
479 'starred' => '1',
480 ]);
481
482 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
483
484 $content = json_decode($this->client->getResponse()->getContent(), true);
485
486 $this->assertGreaterThan(0, $content['id']);
487 $this->assertEquals('http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']);
488 $this->assertEquals(true, $content['is_archived']);
489 $this->assertEquals(true, $content['is_starred']);
490 $this->assertEquals(1, $content['user_id']);
491 }
492
493 public function testPostArchivedAndStarredEntryWithoutQuotes()
494 {
495 $this->client->request('POST', '/api/entries.json', [
496 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
497 'archive' => 0,
498 'starred' => 1,
499 ]);
500
501 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
502
503 $content = json_decode($this->client->getResponse()->getContent(), true);
504
505 $this->assertGreaterThan(0, $content['id']);
506 $this->assertEquals('http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']);
507 $this->assertEquals(false, $content['is_archived']);
508 $this->assertEquals(true, $content['is_starred']);
509 }
510
511 public function testPatchEntry()
512 {
513 $entry = $this->client->getContainer()
514 ->get('doctrine.orm.entity_manager')
515 ->getRepository('WallabagCoreBundle:Entry')
516 ->findOneByUser(1);
517
518 if (!$entry) {
519 $this->markTestSkipped('No content found in db.');
520 }
521
522 // hydrate the tags relations
523 $nbTags = count($entry->getTags());
524
525 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
526 'title' => 'New awesome title',
527 'tags' => 'new tag '.uniqid(),
528 'starred' => '1',
529 'archive' => '0',
e9056dd9 530 'language' => 'de_AT',
645291e8
JB
531 'preview_picture' => 'http://preview.io/picture.jpg',
532 'authors' => 'bob,sponge',
533 'content' => 'awesome',
1112e547 534 'public' => 0,
900c8448
NL
535 ]);
536
537 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
538
539 $content = json_decode($this->client->getResponse()->getContent(), true);
540
541 $this->assertEquals($entry->getId(), $content['id']);
542 $this->assertEquals($entry->getUrl(), $content['url']);
543 $this->assertEquals('New awesome title', $content['title']);
544 $this->assertGreaterThan($nbTags, count($content['tags']));
545 $this->assertEquals(1, $content['user_id']);
e9056dd9 546 $this->assertEquals('de_AT', $content['language']);
645291e8
JB
547 $this->assertEquals('http://preview.io/picture.jpg', $content['preview_picture']);
548 $this->assertContains('sponge', $content['published_by']);
549 $this->assertContains('bob', $content['published_by']);
550 $this->assertEquals('awesome', $content['content']);
1112e547 551 $this->assertFalse($content['is_public'], 'Entry is no more shared');
900c8448
NL
552 }
553
554 public function testPatchEntryWithoutQuotes()
555 {
556 $entry = $this->client->getContainer()
557 ->get('doctrine.orm.entity_manager')
558 ->getRepository('WallabagCoreBundle:Entry')
559 ->findOneByUser(1);
560
561 if (!$entry) {
562 $this->markTestSkipped('No content found in db.');
563 }
564
565 // hydrate the tags relations
566 $nbTags = count($entry->getTags());
567
568 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
569 'title' => 'New awesome title',
570 'tags' => 'new tag '.uniqid(),
571 'starred' => 1,
572 'archive' => 0,
645291e8 573 'authors' => ['bob', 'sponge'],
900c8448
NL
574 ]);
575
576 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
577
578 $content = json_decode($this->client->getResponse()->getContent(), true);
579
580 $this->assertEquals($entry->getId(), $content['id']);
581 $this->assertEquals($entry->getUrl(), $content['url']);
582 $this->assertEquals('New awesome title', $content['title']);
583 $this->assertGreaterThan($nbTags, count($content['tags']));
645291e8 584 $this->assertTrue(empty($content['published_by']), 'Authors were not saved because of an array instead of a string');
900c8448
NL
585 }
586
587 public function testGetTagsEntry()
588 {
589 $entry = $this->client->getContainer()
590 ->get('doctrine.orm.entity_manager')
591 ->getRepository('WallabagCoreBundle:Entry')
592 ->findOneWithTags($this->user->getId());
593
594 $entry = $entry[0];
595
596 if (!$entry) {
597 $this->markTestSkipped('No content found in db.');
598 }
599
600 $tags = [];
601 foreach ($entry->getTags() as $tag) {
602 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
603 }
604
605 $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags');
606
607 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent());
608 }
609
610 public function testPostTagsOnEntry()
611 {
612 $entry = $this->client->getContainer()
613 ->get('doctrine.orm.entity_manager')
614 ->getRepository('WallabagCoreBundle:Entry')
615 ->findOneByUser(1);
616
617 if (!$entry) {
618 $this->markTestSkipped('No content found in db.');
619 }
620
621 $nbTags = count($entry->getTags());
622
623 $newTags = 'tag1,tag2,tag3';
624
625 $this->client->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]);
626
627 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
628
629 $content = json_decode($this->client->getResponse()->getContent(), true);
630
631 $this->assertArrayHasKey('tags', $content);
632 $this->assertEquals($nbTags + 3, count($content['tags']));
633
634 $entryDB = $this->client->getContainer()
635 ->get('doctrine.orm.entity_manager')
636 ->getRepository('WallabagCoreBundle:Entry')
637 ->find($entry->getId());
638
639 $tagsInDB = [];
640 foreach ($entryDB->getTags()->toArray() as $tag) {
641 $tagsInDB[$tag->getId()] = $tag->getLabel();
642 }
643
644 foreach (explode(',', $newTags) as $tag) {
645 $this->assertContains($tag, $tagsInDB);
646 }
647 }
648
649 public function testDeleteOneTagEntry()
650 {
651 $entry = $this->client->getContainer()
652 ->get('doctrine.orm.entity_manager')
653 ->getRepository('WallabagCoreBundle:Entry')
654 ->findOneWithTags($this->user->getId());
655 $entry = $entry[0];
656
657 if (!$entry) {
658 $this->markTestSkipped('No content found in db.');
659 }
660
661 // hydrate the tags relations
662 $nbTags = count($entry->getTags());
663 $tag = $entry->getTags()[0];
664
665 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json');
666
667 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
668
669 $content = json_decode($this->client->getResponse()->getContent(), true);
670
671 $this->assertArrayHasKey('tags', $content);
672 $this->assertEquals($nbTags - 1, count($content['tags']));
673 }
674
675 public function testSaveIsArchivedAfterPost()
676 {
677 $entry = $this->client->getContainer()
678 ->get('doctrine.orm.entity_manager')
679 ->getRepository('WallabagCoreBundle:Entry')
680 ->findOneBy(['user' => 1, 'isArchived' => true]);
681
682 if (!$entry) {
683 $this->markTestSkipped('No content found in db.');
684 }
685
686 $this->client->request('POST', '/api/entries.json', [
687 'url' => $entry->getUrl(),
688 ]);
689
690 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
691
692 $content = json_decode($this->client->getResponse()->getContent(), true);
693
694 $this->assertEquals(true, $content['is_archived']);
695 }
696
697 public function testSaveIsStarredAfterPost()
698 {
699 $entry = $this->client->getContainer()
700 ->get('doctrine.orm.entity_manager')
701 ->getRepository('WallabagCoreBundle:Entry')
702 ->findOneBy(['user' => 1, 'isStarred' => true]);
703
704 if (!$entry) {
705 $this->markTestSkipped('No content found in db.');
706 }
707
708 $this->client->request('POST', '/api/entries.json', [
709 'url' => $entry->getUrl(),
710 ]);
711
712 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
713
714 $content = json_decode($this->client->getResponse()->getContent(), true);
715
716 $this->assertEquals(true, $content['is_starred']);
717 }
718
719 public function testSaveIsArchivedAfterPatch()
720 {
721 $entry = $this->client->getContainer()
722 ->get('doctrine.orm.entity_manager')
723 ->getRepository('WallabagCoreBundle:Entry')
724 ->findOneBy(['user' => 1, 'isArchived' => true]);
725
726 if (!$entry) {
727 $this->markTestSkipped('No content found in db.');
728 }
729
730 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
731 'title' => $entry->getTitle().'++',
732 ]);
733
734 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
735
736 $content = json_decode($this->client->getResponse()->getContent(), true);
737
738 $this->assertEquals(true, $content['is_archived']);
739 }
740
741 public function testSaveIsStarredAfterPatch()
742 {
743 $entry = $this->client->getContainer()
744 ->get('doctrine.orm.entity_manager')
745 ->getRepository('WallabagCoreBundle:Entry')
746 ->findOneBy(['user' => 1, 'isStarred' => true]);
747
748 if (!$entry) {
749 $this->markTestSkipped('No content found in db.');
750 }
751 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
752 'title' => $entry->getTitle().'++',
753 ]);
754
755 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
756
757 $content = json_decode($this->client->getResponse()->getContent(), true);
758
759 $this->assertEquals(true, $content['is_starred']);
760 }
761
762 public function testGetEntriesExists()
763 {
764 $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2');
765
766 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
767
768 $content = json_decode($this->client->getResponse()->getContent(), true);
769
ca9a83ee 770 $this->assertEquals(2, $content['exists']);
900c8448
NL
771 }
772
773 public function testGetEntriesExistsWithManyUrls()
774 {
775 $url1 = 'http://0.0.0.0/entry2';
776 $url2 = 'http://0.0.0.0/entry10';
777 $this->client->request('GET', '/api/entries/exists?urls[]='.$url1.'&urls[]='.$url2);
778
779 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
780
781 $content = json_decode($this->client->getResponse()->getContent(), true);
782
783 $this->assertArrayHasKey($url1, $content);
784 $this->assertArrayHasKey($url2, $content);
ca9a83ee 785 $this->assertEquals(2, $content[$url1]);
900c8448
NL
786 $this->assertEquals(false, $content[$url2]);
787 }
788
789 public function testGetEntriesExistsWhichDoesNotExists()
790 {
791 $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2');
792
793 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
794
795 $content = json_decode($this->client->getResponse()->getContent(), true);
796
797 $this->assertEquals(false, $content['exists']);
798 }
799
800 public function testGetEntriesExistsWithNoUrl()
801 {
802 $this->client->request('GET', '/api/entries/exists?url=');
803
804 $this->assertEquals(403, $this->client->getResponse()->getStatusCode());
805 }
0a6f4568
JB
806
807 public function testReloadEntryErrorWhileFetching()
808 {
70584b42 809 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
0a6f4568 810 ->getRepository('WallabagCoreBundle:Entry')
70584b42 811 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
0a6f4568
JB
812
813 if (!$entry) {
814 $this->markTestSkipped('No content found in db.');
815 }
816
817 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'/reload.json');
5cd0857e 818 $this->assertEquals(304, $this->client->getResponse()->getStatusCode());
0a6f4568
JB
819 }
820
821 public function testReloadEntry()
822 {
823 $this->client->request('POST', '/api/entries.json', [
824 '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',
825 'archive' => '1',
826 'tags' => 'google, apple',
827 ]);
828
829 $json = json_decode($this->client->getResponse()->getContent(), true);
830
831 $this->setUp();
832
833 $this->client->request('PATCH', '/api/entries/'.$json['id'].'/reload.json');
834 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
835
836 $content = json_decode($this->client->getResponse()->getContent(), true);
837
838 $this->assertNotEmpty($content['title']);
839
840 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
841 }
d1fc5902
NL
842
843 public function testPostEntriesTagsListAction()
844 {
80299ed2
NL
845 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
846 ->getRepository('WallabagCoreBundle:Entry')
dcbebc17 847 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
80299ed2
NL
848
849 $tags = $entry->getTags();
850
dcbebc17 851 $this->assertCount(2, $tags);
80299ed2 852
d1fc5902
NL
853 $list = [
854 [
dcbebc17 855 'url' => 'http://0.0.0.0/entry4',
80299ed2 856 'tags' => 'new tag 1, new tag 2',
d1fc5902 857 ],
80299ed2
NL
858 ];
859
860 $this->client->request('POST', '/api/entries/tags/lists?list='.json_encode($list));
861
862 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
863
864 $content = json_decode($this->client->getResponse()->getContent(), true);
865
866 $this->assertInternalType('int', $content[0]['entry']);
dcbebc17 867 $this->assertEquals('http://0.0.0.0/entry4', $content[0]['url']);
80299ed2
NL
868
869 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
870 ->getRepository('WallabagCoreBundle:Entry')
dcbebc17 871 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
80299ed2
NL
872
873 $tags = $entry->getTags();
dcbebc17 874 $this->assertCount(4, $tags);
80299ed2
NL
875 }
876
877 public function testDeleteEntriesTagsListAction()
878 {
7ab5eb95 879 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
880 $entry = new Entry($em->getReference(User::class, 1));
881 $entry->setUrl('http://0.0.0.0/test-entry');
882 $entry->addTag((new Tag())->setLabel('foo-tag'));
883 $entry->addTag((new Tag())->setLabel('bar-tag'));
884 $em->persist($entry);
885 $em->flush();
80299ed2 886
7ab5eb95 887 $em->clear();
80299ed2
NL
888
889 $list = [
d1fc5902 890 [
7ab5eb95 891 'url' => 'http://0.0.0.0/test-entry',
892 'tags' => 'foo-tag, bar-tag',
d1fc5902
NL
893 ],
894 ];
895
7d2d1d68 896 $this->client->request('DELETE', '/api/entries/tags/list?list='.json_encode($list));
7ab5eb95 897 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
898
899 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
900 $this->assertCount(0, $entry->getTags());
1eca7831
NL
901 }
902
a7abcc7b 903 public function testPostEntriesListAction()
1eca7831
NL
904 {
905 $list = [
a7abcc7b 906 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
7fa844a3 907 'http://0.0.0.0/entry2',
1eca7831
NL
908 ];
909
a7abcc7b 910 $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list));
d1fc5902
NL
911
912 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
913
914 $content = json_decode($this->client->getResponse()->getContent(), true);
915
80299ed2 916 $this->assertInternalType('int', $content[0]['entry']);
a7abcc7b 917 $this->assertEquals('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']);
d1fc5902 918
1eca7831 919 $this->assertInternalType('int', $content[1]['entry']);
7fa844a3 920 $this->assertEquals('http://0.0.0.0/entry2', $content[1]['url']);
a7abcc7b 921 }
d1fc5902 922
a7abcc7b
NL
923 public function testDeleteEntriesListAction()
924 {
7ab5eb95 925 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
926 $em->persist((new Entry($em->getReference(User::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
927
928 $em->flush();
929 $em->clear();
a7abcc7b 930 $list = [
7ab5eb95 931 'http://0.0.0.0/test-entry1',
932 'http://0.0.0.0/test-entry-not-exist',
a7abcc7b
NL
933 ];
934
935 $this->client->request('DELETE', '/api/entries/list?urls='.json_encode($list));
d1fc5902 936
a7abcc7b
NL
937 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
938
939 $content = json_decode($this->client->getResponse()->getContent(), true);
940
941 $this->assertTrue($content[0]['entry']);
7ab5eb95 942 $this->assertEquals('http://0.0.0.0/test-entry1', $content[0]['url']);
1eca7831 943
a7abcc7b 944 $this->assertFalse($content[1]['entry']);
7ab5eb95 945 $this->assertEquals('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
d1fc5902 946 }
efd351c9 947
efd351c9
NL
948 public function testLimitBulkAction()
949 {
950 $list = [
951 'http://0.0.0.0/entry1',
952 'http://0.0.0.0/entry1',
953 'http://0.0.0.0/entry1',
954 'http://0.0.0.0/entry1',
955 'http://0.0.0.0/entry1',
956 'http://0.0.0.0/entry1',
957 'http://0.0.0.0/entry1',
958 'http://0.0.0.0/entry1',
959 'http://0.0.0.0/entry1',
960 'http://0.0.0.0/entry1',
961 'http://0.0.0.0/entry1',
962 ];
963
964 $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list));
72db15ca
JB
965
966 $this->assertEquals(400, $this->client->getResponse()->getStatusCode());
967 $this->assertContains('API limit reached', $this->client->getResponse()->getContent());
d1fc5902 968 }
900c8448 969}