]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
Fix validateAndSetPreviewPicture
[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
f808b016
JB
24 $this->client->request('GET', '/api/entries/' . $entry->getId() . '.json');
25 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
26
27 $content = json_decode($this->client->getResponse()->getContent(), true);
28
f808b016
JB
29 $this->assertSame($entry->getTitle(), $content['title']);
30 $this->assertSame($entry->getUrl(), $content['url']);
900c8448 31 $this->assertCount(count($entry->getTags()), $content['tags']);
f808b016
JB
32 $this->assertSame($entry->getUserName(), $content['user_name']);
33 $this->assertSame($entry->getUserEmail(), $content['user_email']);
34 $this->assertSame($entry->getUserId(), $content['user_id']);
900c8448 35
f808b016 36 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
5a619812
JB
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
f808b016
JB
50 $this->client->request('GET', '/api/entries/' . $entry->getId() . '/export.epub');
51 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
5a619812
JB
52
53 // epub format got the content type in the content
54 $this->assertContains('application/epub', $this->client->getResponse()->getContent());
f808b016 55 $this->assertSame('application/epub+zip', $this->client->getResponse()->headers->get('Content-Type'));
5a619812
JB
56
57 // re-auth client for mobi
58 $client = $this->createAuthorizedClient();
f808b016
JB
59 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.mobi');
60 $this->assertSame(200, $client->getResponse()->getStatusCode());
5a619812 61
f808b016 62 $this->assertSame('application/x-mobipocket-ebook', $client->getResponse()->headers->get('Content-Type'));
5a619812
JB
63
64 // re-auth client for pdf
65 $client = $this->createAuthorizedClient();
f808b016
JB
66 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.pdf');
67 $this->assertSame(200, $client->getResponse()->getStatusCode());
5a619812
JB
68
69 $this->assertContains('PDF-', $client->getResponse()->getContent());
f808b016 70 $this->assertSame('application/pdf', $client->getResponse()->headers->get('Content-Type'));
5a619812
JB
71
72 // re-auth client for pdf
73 $client = $this->createAuthorizedClient();
f808b016
JB
74 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.txt');
75 $this->assertSame(200, $client->getResponse()->getStatusCode());
5a619812
JB
76
77 $this->assertContains('text/plain', $client->getResponse()->headers->get('Content-Type'));
78
79 // re-auth client for pdf
80 $client = $this->createAuthorizedClient();
f808b016
JB
81 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.csv');
82 $this->assertSame(200, $client->getResponse()->getStatusCode());
5a619812
JB
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
f808b016 98 $this->client->request('GET', '/api/entries/' . $entry->getId() . '.json');
900c8448 99
f808b016 100 $this->assertSame(403, $this->client->getResponse()->getStatusCode());
900c8448
NL
101 }
102
103 public function testGetEntries()
104 {
105 $this->client->request('GET', '/api/entries');
106
f808b016 107 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
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']);
f808b016 114 $this->assertSame(1, $content['page']);
900c8448
NL
115 $this->assertGreaterThanOrEqual(1, $content['pages']);
116
f808b016 117 $this->assertSame('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
f808b016 134 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
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']);
f808b016
JB
141 $this->assertSame(1, $content['page']);
142 $this->assertSame(2, $content['limit']);
900c8448
NL
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
f808b016 161 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
1112e547
JB
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
f808b016 186 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1112e547
JB
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']);
f808b016
JB
193 $this->assertSame(1, $content['page']);
194 $this->assertSame(30, $content['limit']);
1112e547
JB
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
f808b016 207 $this->assertSame('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
f808b016 217 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
b60a666d 218
219 $content = json_decode($this->client->getResponse()->getContent(), true);
220
221 $this->assertGreaterThanOrEqual(0, $content['total']);
f808b016
JB
222 $this->assertSame(2, $content['page']);
223 $this->assertSame(2, $content['limit']);
b60a666d 224 }
225
900c8448
NL
226 public function testGetStarredEntries()
227 {
228 $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']);
229
f808b016 230 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
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']);
f808b016 237 $this->assertSame(1, $content['page']);
900c8448
NL
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
f808b016 251 $this->assertSame('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
f808b016 258 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
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']);
f808b016 265 $this->assertSame(1, $content['page']);
900c8448
NL
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
f808b016 278 $this->assertSame('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
f808b016 285 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
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']);
f808b016 292 $this->assertSame(1, $content['page']);
900c8448
NL
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]);
f808b016 302 $this->assertContains('tags=' . urlencode('foo,bar'), $content['_links'][$link]['href']);
900c8448
NL
303 }
304
f808b016 305 $this->assertSame('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
f808b016 312 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
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']);
f808b016 319 $this->assertSame(1, $content['page']);
900c8448
NL
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
f808b016 332 $this->assertSame('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
f808b016 340 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
341
342 $content = json_decode($this->client->getResponse()->getContent(), true);
343
344 $this->assertGreaterThanOrEqual(1, count($content));
345 $this->assertEmpty($content['_embedded']['items']);
f808b016
JB
346 $this->assertSame(0, $content['total']);
347 $this->assertSame(1, $content['page']);
348 $this->assertSame(1, $content['pages']);
900c8448
NL
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]);
f808b016 357 $this->assertContains('since=' . ($future->getTimestamp() + 1000), $content['_links'][$link]['href']);
900c8448
NL
358 }
359
f808b016 360 $this->assertSame('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
f808b016 374 $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '.json');
900c8448 375
f808b016 376 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
377
378 $content = json_decode($this->client->getResponse()->getContent(), true);
379
f808b016
JB
380 $this->assertSame($entry->getTitle(), $content['title']);
381 $this->assertSame($entry->getUrl(), $content['url']);
900c8448
NL
382
383 // We'll try to delete this entry again
f808b016 384 $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '.json');
900c8448 385
f808b016 386 $this->assertSame(404, $this->client->getResponse()->getStatusCode());
900c8448
NL
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
f808b016 402 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
403
404 $content = json_decode($this->client->getResponse()->getContent(), true);
405
406 $this->assertGreaterThan(0, $content['id']);
f808b016 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']);
38520658
JB
408 $this->assertSame(0, $content['is_archived']);
409 $this->assertSame(0, $content['is_starred']);
f808b016
JB
410 $this->assertSame('New title for my article', $content['title']);
411 $this->assertSame(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
f808b016 430 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
431
432 $content = json_decode($this->client->getResponse()->getContent(), true);
433
434 $this->assertGreaterThan(0, $content['id']);
f808b016 435 $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']);
38520658
JB
436 $this->assertSame(1, $content['is_archived']);
437 $this->assertSame(0, $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
f808b016 459 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
a9357a83 460 $content = json_decode($this->client->getResponse()->getContent(), true);
461 $this->assertGreaterThan(0, $content['id']);
f808b016 462 $this->assertSame('http://www.example.com/', $content['url']);
a9357a83 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
f808b016 482 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
483
484 $content = json_decode($this->client->getResponse()->getContent(), true);
485
486 $this->assertGreaterThan(0, $content['id']);
f808b016 487 $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']);
38520658
JB
488 $this->assertSame(1, $content['is_archived']);
489 $this->assertSame(1, $content['is_starred']);
f808b016 490 $this->assertSame(1, $content['user_id']);
900c8448
NL
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
f808b016 501 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
502
503 $content = json_decode($this->client->getResponse()->getContent(), true);
504
505 $this->assertGreaterThan(0, $content['id']);
f808b016 506 $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']);
38520658
JB
507 $this->assertSame(0, $content['is_archived']);
508 $this->assertSame(1, $content['is_starred']);
900c8448
NL
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
a05b6115 522 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
900c8448 523 'title' => 'New awesome title',
f808b016 524 'tags' => 'new tag ' . uniqid(),
900c8448
NL
525 'starred' => '1',
526 'archive' => '0',
e9056dd9 527 'language' => 'de_AT',
645291e8
JB
528 'preview_picture' => 'http://preview.io/picture.jpg',
529 'authors' => 'bob,sponge',
530 'content' => 'awesome',
1112e547 531 'public' => 0,
a05b6115 532 'published_at' => 1488833381,
900c8448
NL
533 ]);
534
f808b016 535 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
536
537 $content = json_decode($this->client->getResponse()->getContent(), true);
538
f808b016
JB
539 $this->assertSame($entry->getId(), $content['id']);
540 $this->assertSame($entry->getUrl(), $content['url']);
541 $this->assertSame('New awesome title', $content['title']);
a05b6115 542 $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag');
f808b016
JB
543 $this->assertSame(1, $content['user_id']);
544 $this->assertSame('de_AT', $content['language']);
545 $this->assertSame('http://preview.io/picture.jpg', $content['preview_picture']);
645291e8
JB
546 $this->assertContains('sponge', $content['published_by']);
547 $this->assertContains('bob', $content['published_by']);
f808b016 548 $this->assertSame('awesome', $content['content']);
1112e547 549 $this->assertFalse($content['is_public'], 'Entry is no more shared');
a05b6115 550 $this->assertContains('2017-03-06', $content['published_at']);
900c8448
NL
551 }
552
553 public function testPatchEntryWithoutQuotes()
554 {
555 $entry = $this->client->getContainer()
556 ->get('doctrine.orm.entity_manager')
557 ->getRepository('WallabagCoreBundle:Entry')
558 ->findOneByUser(1);
559
560 if (!$entry) {
561 $this->markTestSkipped('No content found in db.');
562 }
563
a05b6115
JB
564 $previousContent = $entry->getContent();
565 $previousLanguage = $entry->getLanguage();
900c8448 566
f808b016 567 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
900c8448 568 'title' => 'New awesome title',
f808b016 569 'tags' => 'new tag ' . uniqid(),
900c8448
NL
570 'starred' => 1,
571 'archive' => 0,
645291e8 572 'authors' => ['bob', 'sponge'],
900c8448
NL
573 ]);
574
f808b016 575 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
576
577 $content = json_decode($this->client->getResponse()->getContent(), true);
578
f808b016
JB
579 $this->assertSame($entry->getId(), $content['id']);
580 $this->assertSame($entry->getUrl(), $content['url']);
a05b6115 581 $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag');
900c8448 582 $this->assertGreaterThan($nbTags, count($content['tags']));
f808b016 583 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
a05b6115
JB
584 $this->assertEquals($previousContent, $content['content'], 'Ensure content has not moved');
585 $this->assertEquals($previousLanguage, $content['language'], 'Ensure language has not moved');
900c8448
NL
586 }
587
588 public function testGetTagsEntry()
589 {
590 $entry = $this->client->getContainer()
591 ->get('doctrine.orm.entity_manager')
592 ->getRepository('WallabagCoreBundle:Entry')
593 ->findOneWithTags($this->user->getId());
594
595 $entry = $entry[0];
596
597 if (!$entry) {
598 $this->markTestSkipped('No content found in db.');
599 }
600
601 $tags = [];
602 foreach ($entry->getTags() as $tag) {
603 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
604 }
605
f808b016 606 $this->client->request('GET', '/api/entries/' . $entry->getId() . '/tags');
900c8448 607
f808b016 608 $this->assertSame(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent());
900c8448
NL
609 }
610
611 public function testPostTagsOnEntry()
612 {
613 $entry = $this->client->getContainer()
614 ->get('doctrine.orm.entity_manager')
615 ->getRepository('WallabagCoreBundle:Entry')
616 ->findOneByUser(1);
617
618 if (!$entry) {
619 $this->markTestSkipped('No content found in db.');
620 }
621
622 $nbTags = count($entry->getTags());
623
624 $newTags = 'tag1,tag2,tag3';
625
f808b016 626 $this->client->request('POST', '/api/entries/' . $entry->getId() . '/tags', ['tags' => $newTags]);
900c8448 627
f808b016 628 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
629
630 $content = json_decode($this->client->getResponse()->getContent(), true);
631
632 $this->assertArrayHasKey('tags', $content);
f808b016 633 $this->assertSame($nbTags + 3, count($content['tags']));
900c8448
NL
634
635 $entryDB = $this->client->getContainer()
636 ->get('doctrine.orm.entity_manager')
637 ->getRepository('WallabagCoreBundle:Entry')
638 ->find($entry->getId());
639
640 $tagsInDB = [];
641 foreach ($entryDB->getTags()->toArray() as $tag) {
642 $tagsInDB[$tag->getId()] = $tag->getLabel();
643 }
644
645 foreach (explode(',', $newTags) as $tag) {
646 $this->assertContains($tag, $tagsInDB);
647 }
648 }
649
650 public function testDeleteOneTagEntry()
651 {
652 $entry = $this->client->getContainer()
653 ->get('doctrine.orm.entity_manager')
654 ->getRepository('WallabagCoreBundle:Entry')
655 ->findOneWithTags($this->user->getId());
656 $entry = $entry[0];
657
658 if (!$entry) {
659 $this->markTestSkipped('No content found in db.');
660 }
661
662 // hydrate the tags relations
663 $nbTags = count($entry->getTags());
664 $tag = $entry->getTags()[0];
665
f808b016 666 $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '/tags/' . $tag->getId() . '.json');
900c8448 667
f808b016 668 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
669
670 $content = json_decode($this->client->getResponse()->getContent(), true);
671
672 $this->assertArrayHasKey('tags', $content);
f808b016 673 $this->assertSame($nbTags - 1, count($content['tags']));
900c8448
NL
674 }
675
676 public function testSaveIsArchivedAfterPost()
677 {
678 $entry = $this->client->getContainer()
679 ->get('doctrine.orm.entity_manager')
680 ->getRepository('WallabagCoreBundle:Entry')
681 ->findOneBy(['user' => 1, 'isArchived' => true]);
682
683 if (!$entry) {
684 $this->markTestSkipped('No content found in db.');
685 }
686
687 $this->client->request('POST', '/api/entries.json', [
688 'url' => $entry->getUrl(),
689 ]);
690
f808b016 691 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
692
693 $content = json_decode($this->client->getResponse()->getContent(), true);
694
38520658 695 $this->assertSame(1, $content['is_archived']);
900c8448
NL
696 }
697
698 public function testSaveIsStarredAfterPost()
699 {
700 $entry = $this->client->getContainer()
701 ->get('doctrine.orm.entity_manager')
702 ->getRepository('WallabagCoreBundle:Entry')
703 ->findOneBy(['user' => 1, 'isStarred' => true]);
704
705 if (!$entry) {
706 $this->markTestSkipped('No content found in db.');
707 }
708
709 $this->client->request('POST', '/api/entries.json', [
710 'url' => $entry->getUrl(),
711 ]);
712
f808b016 713 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
714
715 $content = json_decode($this->client->getResponse()->getContent(), true);
716
38520658 717 $this->assertSame(1, $content['is_starred']);
900c8448
NL
718 }
719
720 public function testSaveIsArchivedAfterPatch()
721 {
722 $entry = $this->client->getContainer()
723 ->get('doctrine.orm.entity_manager')
724 ->getRepository('WallabagCoreBundle:Entry')
725 ->findOneBy(['user' => 1, 'isArchived' => true]);
726
727 if (!$entry) {
728 $this->markTestSkipped('No content found in db.');
729 }
730
a05b6115
JB
731 $previousTitle = $entry->getTitle();
732
733 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
734 'title' => $entry->getTitle().'++',
900c8448
NL
735 ]);
736
f808b016 737 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
738
739 $content = json_decode($this->client->getResponse()->getContent(), true);
740
38520658 741 $this->assertSame(1, $content['is_archived']);
a05b6115 742 $this->assertEquals($previousTitle.'++', $content['title']);
900c8448
NL
743 }
744
745 public function testSaveIsStarredAfterPatch()
746 {
747 $entry = $this->client->getContainer()
748 ->get('doctrine.orm.entity_manager')
749 ->getRepository('WallabagCoreBundle:Entry')
750 ->findOneBy(['user' => 1, 'isStarred' => true]);
751
752 if (!$entry) {
753 $this->markTestSkipped('No content found in db.');
754 }
f808b016
JB
755 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
756 'title' => $entry->getTitle() . '++',
900c8448
NL
757 ]);
758
f808b016 759 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
760
761 $content = json_decode($this->client->getResponse()->getContent(), true);
762
38520658 763 $this->assertSame(1, $content['is_starred']);
900c8448
NL
764 }
765
18696f77 766 public function dataForEntriesExistWithUrl()
900c8448 767 {
18696f77
JB
768 return [
769 'with_id' => [
770 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1',
771 'expectedValue' => 2,
772 ],
773 'without_id' => [
774 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2',
775 'expectedValue' => true,
776 ],
777 ];
778 }
779
780 /**
781 * @dataProvider dataForEntriesExistWithUrl
782 */
783 public function testGetEntriesExists($url, $expectedValue)
784 {
785 $this->client->request('GET', $url);
900c8448 786
f808b016 787 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
788
789 $content = json_decode($this->client->getResponse()->getContent(), true);
790
18696f77 791 $this->assertSame($expectedValue, $content['exists']);
900c8448
NL
792 }
793
794 public function testGetEntriesExistsWithManyUrls()
18696f77
JB
795 {
796 $url1 = 'http://0.0.0.0/entry2';
797 $url2 = 'http://0.0.0.0/entry10';
f808b016 798 $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1');
18696f77 799
f808b016 800 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
18696f77
JB
801
802 $content = json_decode($this->client->getResponse()->getContent(), true);
803
804 $this->assertArrayHasKey($url1, $content);
805 $this->assertArrayHasKey($url2, $content);
806 $this->assertSame(2, $content[$url1]);
39ffaba3 807 $this->assertNull($content[$url2]);
18696f77
JB
808 }
809
810 public function testGetEntriesExistsWithManyUrlsReturnBool()
900c8448
NL
811 {
812 $url1 = 'http://0.0.0.0/entry2';
813 $url2 = 'http://0.0.0.0/entry10';
f808b016 814 $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2);
900c8448 815
f808b016 816 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
817
818 $content = json_decode($this->client->getResponse()->getContent(), true);
819
820 $this->assertArrayHasKey($url1, $content);
821 $this->assertArrayHasKey($url2, $content);
39ffaba3
JB
822 $this->assertTrue($content[$url1]);
823 $this->assertFalse($content[$url2]);
900c8448
NL
824 }
825
826 public function testGetEntriesExistsWhichDoesNotExists()
827 {
828 $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2');
829
f808b016 830 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
831
832 $content = json_decode($this->client->getResponse()->getContent(), true);
833
f808b016 834 $this->assertSame(false, $content['exists']);
900c8448
NL
835 }
836
837 public function testGetEntriesExistsWithNoUrl()
838 {
839 $this->client->request('GET', '/api/entries/exists?url=');
840
f808b016 841 $this->assertSame(403, $this->client->getResponse()->getStatusCode());
900c8448 842 }
0a6f4568
JB
843
844 public function testReloadEntryErrorWhileFetching()
845 {
70584b42 846 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
0a6f4568 847 ->getRepository('WallabagCoreBundle:Entry')
70584b42 848 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
0a6f4568
JB
849
850 if (!$entry) {
851 $this->markTestSkipped('No content found in db.');
852 }
853
f808b016
JB
854 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json');
855 $this->assertSame(304, $this->client->getResponse()->getStatusCode());
0a6f4568
JB
856 }
857
858 public function testReloadEntry()
859 {
860 $this->client->request('POST', '/api/entries.json', [
861 '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',
862 'archive' => '1',
863 'tags' => 'google, apple',
864 ]);
865
866 $json = json_decode($this->client->getResponse()->getContent(), true);
867
868 $this->setUp();
869
f808b016
JB
870 $this->client->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json');
871 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
0a6f4568
JB
872
873 $content = json_decode($this->client->getResponse()->getContent(), true);
874
875 $this->assertNotEmpty($content['title']);
876
f808b016 877 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
0a6f4568 878 }
d1fc5902
NL
879
880 public function testPostEntriesTagsListAction()
881 {
80299ed2
NL
882 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
883 ->getRepository('WallabagCoreBundle:Entry')
dcbebc17 884 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
80299ed2
NL
885
886 $tags = $entry->getTags();
887
dcbebc17 888 $this->assertCount(2, $tags);
80299ed2 889
d1fc5902
NL
890 $list = [
891 [
dcbebc17 892 'url' => 'http://0.0.0.0/entry4',
80299ed2 893 'tags' => 'new tag 1, new tag 2',
d1fc5902 894 ],
80299ed2
NL
895 ];
896
f808b016 897 $this->client->request('POST', '/api/entries/tags/lists?list=' . json_encode($list));
80299ed2 898
f808b016 899 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
80299ed2
NL
900
901 $content = json_decode($this->client->getResponse()->getContent(), true);
902
903 $this->assertInternalType('int', $content[0]['entry']);
f808b016 904 $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']);
80299ed2
NL
905
906 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
907 ->getRepository('WallabagCoreBundle:Entry')
dcbebc17 908 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
80299ed2
NL
909
910 $tags = $entry->getTags();
dcbebc17 911 $this->assertCount(4, $tags);
80299ed2
NL
912 }
913
a05b6115
JB
914 public function testPostEntriesTagsListActionNoList()
915 {
916 $this->client->request('POST', '/api/entries/tags/lists?list='.json_encode([]));
917
918 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
919
920 $content = json_decode($this->client->getResponse()->getContent(), true);
921
922 $this->assertEmpty($content);
923 }
924
80299ed2
NL
925 public function testDeleteEntriesTagsListAction()
926 {
7ab5eb95 927 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
928 $entry = new Entry($em->getReference(User::class, 1));
929 $entry->setUrl('http://0.0.0.0/test-entry');
930 $entry->addTag((new Tag())->setLabel('foo-tag'));
931 $entry->addTag((new Tag())->setLabel('bar-tag'));
932 $em->persist($entry);
933 $em->flush();
80299ed2 934
7ab5eb95 935 $em->clear();
80299ed2
NL
936
937 $list = [
d1fc5902 938 [
7ab5eb95 939 'url' => 'http://0.0.0.0/test-entry',
940 'tags' => 'foo-tag, bar-tag',
d1fc5902
NL
941 ],
942 ];
943
f808b016
JB
944 $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list));
945 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
7ab5eb95 946
947 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
948 $this->assertCount(0, $entry->getTags());
1eca7831
NL
949 }
950
a05b6115
JB
951 public function testDeleteEntriesTagsListActionNoList()
952 {
953 $this->client->request('DELETE', '/api/entries/tags/list?list='.json_encode([]));
954
955 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
956
957 $content = json_decode($this->client->getResponse()->getContent(), true);
958
959 $this->assertEmpty($content);
960 }
961
a7abcc7b 962 public function testPostEntriesListAction()
1eca7831
NL
963 {
964 $list = [
a7abcc7b 965 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
7fa844a3 966 'http://0.0.0.0/entry2',
1eca7831
NL
967 ];
968
f808b016 969 $this->client->request('POST', '/api/entries/lists?urls=' . json_encode($list));
d1fc5902 970
f808b016 971 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
d1fc5902
NL
972
973 $content = json_decode($this->client->getResponse()->getContent(), true);
974
80299ed2 975 $this->assertInternalType('int', $content[0]['entry']);
f808b016 976 $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']);
d1fc5902 977
1eca7831 978 $this->assertInternalType('int', $content[1]['entry']);
f808b016 979 $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']);
a7abcc7b 980 }
d1fc5902 981
a05b6115
JB
982 public function testPostEntriesListActionWithNoUrls()
983 {
984 $this->client->request('POST', '/api/entries/lists?urls='.json_encode([]));
985
986 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
987
988 $content = json_decode($this->client->getResponse()->getContent(), true);
989
990 $this->assertEmpty($content);
991 }
992
a7abcc7b
NL
993 public function testDeleteEntriesListAction()
994 {
7ab5eb95 995 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
996 $em->persist((new Entry($em->getReference(User::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
997
998 $em->flush();
999 $em->clear();
a7abcc7b 1000 $list = [
7ab5eb95 1001 'http://0.0.0.0/test-entry1',
1002 'http://0.0.0.0/test-entry-not-exist',
a7abcc7b
NL
1003 ];
1004
f808b016 1005 $this->client->request('DELETE', '/api/entries/list?urls=' . json_encode($list));
d1fc5902 1006
f808b016 1007 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
a7abcc7b
NL
1008
1009 $content = json_decode($this->client->getResponse()->getContent(), true);
1010
1011 $this->assertTrue($content[0]['entry']);
f808b016 1012 $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']);
1eca7831 1013
a7abcc7b 1014 $this->assertFalse($content[1]['entry']);
f808b016 1015 $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
d1fc5902 1016 }
efd351c9 1017
a05b6115
JB
1018 public function testDeleteEntriesListActionWithNoUrls()
1019 {
1020 $this->client->request('DELETE', '/api/entries/list?urls='.json_encode([]));
1021
1022 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
1023
1024 $content = json_decode($this->client->getResponse()->getContent(), true);
1025
1026 $this->assertEmpty($content);
1027 }
1028
efd351c9
NL
1029 public function testLimitBulkAction()
1030 {
1031 $list = [
1032 'http://0.0.0.0/entry1',
1033 'http://0.0.0.0/entry1',
1034 'http://0.0.0.0/entry1',
1035 'http://0.0.0.0/entry1',
1036 'http://0.0.0.0/entry1',
1037 'http://0.0.0.0/entry1',
1038 'http://0.0.0.0/entry1',
1039 'http://0.0.0.0/entry1',
1040 'http://0.0.0.0/entry1',
1041 'http://0.0.0.0/entry1',
1042 'http://0.0.0.0/entry1',
1043 ];
1044
f808b016 1045 $this->client->request('POST', '/api/entries/lists?urls=' . json_encode($list));
72db15ca 1046
f808b016 1047 $this->assertSame(400, $this->client->getResponse()->getStatusCode());
72db15ca 1048 $this->assertContains('API limit reached', $this->client->getResponse()->getContent());
d1fc5902 1049 }
900c8448 1050}