]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
api/entries: add parameter detail to exclude or include content in response
[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')
8f2038e5 18 ->findOneBy(['user' => $this->getUserId(), 'isArchived' => false]);
900c8448
NL
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']);
2a1ceb67 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
00f2368f
KD
39 public function testGetOneEntryWithOriginUrl()
40 {
41 $entry = $this->client->getContainer()
42 ->get('doctrine.orm.entity_manager')
43 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 44 ->findOneBy(['user' => $this->getUserId(), 'url' => 'http://0.0.0.0/entry2']);
00f2368f
KD
45
46 if (!$entry) {
47 $this->markTestSkipped('No content found in db.');
48 }
49
50 $this->client->request('GET', '/api/entries/' . $entry->getId() . '.json');
51 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
52
53 $content = json_decode($this->client->getResponse()->getContent(), true);
54
55 $this->assertSame($entry->getOriginUrl(), $content['origin_url']);
56 }
57
5a619812
JB
58 public function testExportEntry()
59 {
60 $entry = $this->client->getContainer()
61 ->get('doctrine.orm.entity_manager')
62 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 63 ->findOneBy(['user' => $this->getUserId(), 'isArchived' => false]);
5a619812
JB
64
65 if (!$entry) {
66 $this->markTestSkipped('No content found in db.');
67 }
68
f808b016
JB
69 $this->client->request('GET', '/api/entries/' . $entry->getId() . '/export.epub');
70 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
5a619812
JB
71
72 // epub format got the content type in the content
73 $this->assertContains('application/epub', $this->client->getResponse()->getContent());
f808b016 74 $this->assertSame('application/epub+zip', $this->client->getResponse()->headers->get('Content-Type'));
5a619812
JB
75
76 // re-auth client for mobi
77 $client = $this->createAuthorizedClient();
f808b016
JB
78 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.mobi');
79 $this->assertSame(200, $client->getResponse()->getStatusCode());
5a619812 80
f808b016 81 $this->assertSame('application/x-mobipocket-ebook', $client->getResponse()->headers->get('Content-Type'));
5a619812
JB
82
83 // re-auth client for pdf
84 $client = $this->createAuthorizedClient();
f808b016
JB
85 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.pdf');
86 $this->assertSame(200, $client->getResponse()->getStatusCode());
5a619812
JB
87
88 $this->assertContains('PDF-', $client->getResponse()->getContent());
f808b016 89 $this->assertSame('application/pdf', $client->getResponse()->headers->get('Content-Type'));
5a619812
JB
90
91 // re-auth client for pdf
92 $client = $this->createAuthorizedClient();
f808b016
JB
93 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.txt');
94 $this->assertSame(200, $client->getResponse()->getStatusCode());
5a619812
JB
95
96 $this->assertContains('text/plain', $client->getResponse()->headers->get('Content-Type'));
97
98 // re-auth client for pdf
99 $client = $this->createAuthorizedClient();
f808b016
JB
100 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.csv');
101 $this->assertSame(200, $client->getResponse()->getStatusCode());
5a619812
JB
102
103 $this->assertContains('application/csv', $client->getResponse()->headers->get('Content-Type'));
900c8448
NL
104 }
105
106 public function testGetOneEntryWrongUser()
107 {
108 $entry = $this->client->getContainer()
109 ->get('doctrine.orm.entity_manager')
110 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 111 ->findOneBy(['user' => $this->getUserId('bob'), 'isArchived' => false]);
900c8448
NL
112
113 if (!$entry) {
114 $this->markTestSkipped('No content found in db.');
115 }
116
f808b016 117 $this->client->request('GET', '/api/entries/' . $entry->getId() . '.json');
900c8448 118
f808b016 119 $this->assertSame(403, $this->client->getResponse()->getStatusCode());
900c8448
NL
120 }
121
122 public function testGetEntries()
123 {
124 $this->client->request('GET', '/api/entries');
125
f808b016 126 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
127
128 $content = json_decode($this->client->getResponse()->getContent(), true);
129
2a1ceb67 130 $this->assertGreaterThanOrEqual(1, \count($content));
900c8448
NL
131 $this->assertNotEmpty($content['_embedded']['items']);
132 $this->assertGreaterThanOrEqual(1, $content['total']);
f808b016 133 $this->assertSame(1, $content['page']);
900c8448
NL
134 $this->assertGreaterThanOrEqual(1, $content['pages']);
135
2c290747
KD
136 $this->assertNotNull($content['_embedded']['items'][0]['content']);
137
138 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
139 }
140
141 public function testGetEntriesDetailMetadata()
142 {
143 $this->client->request('GET', '/api/entries?detail=metadata');
144
145 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
146
147 $content = json_decode($this->client->getResponse()->getContent(), true);
148
149 $this->assertGreaterThanOrEqual(1, \count($content));
150 $this->assertNotEmpty($content['_embedded']['items']);
151 $this->assertGreaterThanOrEqual(1, $content['total']);
152 $this->assertSame(1, $content['page']);
153 $this->assertGreaterThanOrEqual(1, $content['pages']);
154
155 $this->assertNull($content['_embedded']['items'][0]['content']);
156
f808b016 157 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
158 }
159
160 public function testGetEntriesWithFullOptions()
161 {
162 $this->client->request('GET', '/api/entries', [
163 'archive' => 1,
164 'starred' => 1,
165 'sort' => 'updated',
166 'order' => 'asc',
167 'page' => 1,
168 'perPage' => 2,
169 'tags' => 'foo',
170 'since' => 1443274283,
1112e547 171 'public' => 0,
900c8448
NL
172 ]);
173
f808b016 174 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
175
176 $content = json_decode($this->client->getResponse()->getContent(), true);
177
2a1ceb67 178 $this->assertGreaterThanOrEqual(1, \count($content));
900c8448
NL
179 $this->assertArrayHasKey('items', $content['_embedded']);
180 $this->assertGreaterThanOrEqual(0, $content['total']);
f808b016
JB
181 $this->assertSame(1, $content['page']);
182 $this->assertSame(2, $content['limit']);
900c8448
NL
183 $this->assertGreaterThanOrEqual(1, $content['pages']);
184
185 $this->assertArrayHasKey('_links', $content);
186 $this->assertArrayHasKey('self', $content['_links']);
187 $this->assertArrayHasKey('first', $content['_links']);
188 $this->assertArrayHasKey('last', $content['_links']);
189
190 foreach (['self', 'first', 'last'] as $link) {
191 $this->assertArrayHasKey('href', $content['_links'][$link]);
192 $this->assertContains('archive=1', $content['_links'][$link]['href']);
193 $this->assertContains('starred=1', $content['_links'][$link]['href']);
194 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
195 $this->assertContains('order=asc', $content['_links'][$link]['href']);
196 $this->assertContains('tags=foo', $content['_links'][$link]['href']);
197 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
1112e547
JB
198 $this->assertContains('public=0', $content['_links'][$link]['href']);
199 }
200
f808b016 201 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
1112e547
JB
202 }
203
204 public function testGetEntriesPublicOnly()
205 {
206 $entry = $this->client->getContainer()
207 ->get('doctrine.orm.entity_manager')
208 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 209 ->findOneByUser($this->getUserId());
1112e547
JB
210
211 if (!$entry) {
212 $this->markTestSkipped('No content found in db.');
213 }
214
215 // generate at least one public entry
216 $entry->generateUid();
217
218 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
219 $em->persist($entry);
220 $em->flush();
221
222 $this->client->request('GET', '/api/entries', [
223 'public' => 1,
224 ]);
225
f808b016 226 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1112e547
JB
227
228 $content = json_decode($this->client->getResponse()->getContent(), true);
229
2a1ceb67 230 $this->assertGreaterThanOrEqual(1, \count($content));
1112e547
JB
231 $this->assertArrayHasKey('items', $content['_embedded']);
232 $this->assertGreaterThanOrEqual(1, $content['total']);
f808b016
JB
233 $this->assertSame(1, $content['page']);
234 $this->assertSame(30, $content['limit']);
1112e547
JB
235 $this->assertGreaterThanOrEqual(1, $content['pages']);
236
237 $this->assertArrayHasKey('_links', $content);
238 $this->assertArrayHasKey('self', $content['_links']);
239 $this->assertArrayHasKey('first', $content['_links']);
240 $this->assertArrayHasKey('last', $content['_links']);
241
242 foreach (['self', 'first', 'last'] as $link) {
243 $this->assertArrayHasKey('href', $content['_links'][$link]);
244 $this->assertContains('public=1', $content['_links'][$link]['href']);
900c8448
NL
245 }
246
f808b016 247 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
248 }
249
b60a666d 250 public function testGetEntriesOnPageTwo()
251 {
252 $this->client->request('GET', '/api/entries', [
253 'page' => 2,
254 'perPage' => 2,
255 ]);
256
f808b016 257 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
b60a666d 258
259 $content = json_decode($this->client->getResponse()->getContent(), true);
260
261 $this->assertGreaterThanOrEqual(0, $content['total']);
f808b016
JB
262 $this->assertSame(2, $content['page']);
263 $this->assertSame(2, $content['limit']);
b60a666d 264 }
265
78e3fafa
JB
266 public function testGetStarredEntriesWithBadSort()
267 {
268 $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated', 'order' => 'unknown']);
269
270 $this->assertSame(400, $this->client->getResponse()->getStatusCode());
271
272 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
273 }
274
900c8448
NL
275 public function testGetStarredEntries()
276 {
277 $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']);
278
f808b016 279 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
280
281 $content = json_decode($this->client->getResponse()->getContent(), true);
282
2a1ceb67 283 $this->assertGreaterThanOrEqual(1, \count($content));
900c8448
NL
284 $this->assertNotEmpty($content['_embedded']['items']);
285 $this->assertGreaterThanOrEqual(1, $content['total']);
f808b016 286 $this->assertSame(1, $content['page']);
900c8448
NL
287 $this->assertGreaterThanOrEqual(1, $content['pages']);
288
289 $this->assertArrayHasKey('_links', $content);
290 $this->assertArrayHasKey('self', $content['_links']);
291 $this->assertArrayHasKey('first', $content['_links']);
292 $this->assertArrayHasKey('last', $content['_links']);
293
294 foreach (['self', 'first', 'last'] as $link) {
295 $this->assertArrayHasKey('href', $content['_links'][$link]);
296 $this->assertContains('starred=1', $content['_links'][$link]['href']);
297 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
298 }
299
f808b016 300 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
301 }
302
303 public function testGetArchiveEntries()
304 {
305 $this->client->request('GET', '/api/entries', ['archive' => 1]);
306
f808b016 307 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
308
309 $content = json_decode($this->client->getResponse()->getContent(), true);
310
2a1ceb67 311 $this->assertGreaterThanOrEqual(1, \count($content));
900c8448
NL
312 $this->assertNotEmpty($content['_embedded']['items']);
313 $this->assertGreaterThanOrEqual(1, $content['total']);
f808b016 314 $this->assertSame(1, $content['page']);
900c8448
NL
315 $this->assertGreaterThanOrEqual(1, $content['pages']);
316
317 $this->assertArrayHasKey('_links', $content);
318 $this->assertArrayHasKey('self', $content['_links']);
319 $this->assertArrayHasKey('first', $content['_links']);
320 $this->assertArrayHasKey('last', $content['_links']);
321
322 foreach (['self', 'first', 'last'] as $link) {
323 $this->assertArrayHasKey('href', $content['_links'][$link]);
324 $this->assertContains('archive=1', $content['_links'][$link]['href']);
325 }
326
f808b016 327 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
328 }
329
330 public function testGetTaggedEntries()
331 {
332 $this->client->request('GET', '/api/entries', ['tags' => 'foo,bar']);
333
f808b016 334 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
335
336 $content = json_decode($this->client->getResponse()->getContent(), true);
337
2a1ceb67 338 $this->assertGreaterThanOrEqual(1, \count($content));
900c8448
NL
339 $this->assertNotEmpty($content['_embedded']['items']);
340 $this->assertGreaterThanOrEqual(1, $content['total']);
f808b016 341 $this->assertSame(1, $content['page']);
900c8448
NL
342 $this->assertGreaterThanOrEqual(1, $content['pages']);
343
7c04b739
JB
344 $this->assertContains('foo', array_column($content['_embedded']['items'][0]['tags'], 'label'), 'Entries tags should have "foo" tag');
345 $this->assertContains('bar', array_column($content['_embedded']['items'][0]['tags'], 'label'), 'Entries tags should have "bar" tag');
346
900c8448
NL
347 $this->assertArrayHasKey('_links', $content);
348 $this->assertArrayHasKey('self', $content['_links']);
349 $this->assertArrayHasKey('first', $content['_links']);
350 $this->assertArrayHasKey('last', $content['_links']);
351
352 foreach (['self', 'first', 'last'] as $link) {
353 $this->assertArrayHasKey('href', $content['_links'][$link]);
f808b016 354 $this->assertContains('tags=' . urlencode('foo,bar'), $content['_links'][$link]['href']);
900c8448
NL
355 }
356
f808b016 357 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
358 }
359
d03b72f4
SV
360 public function testGetTaggedEntriesWithBadParams()
361 {
bb86dc64 362 $this->client->request('GET', '/api/entries', ['tags' => ['foo', 'bar']]);
d03b72f4
SV
363
364 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
365 }
366
900c8448
NL
367 public function testGetDatedEntries()
368 {
369 $this->client->request('GET', '/api/entries', ['since' => 1443274283]);
370
f808b016 371 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
372
373 $content = json_decode($this->client->getResponse()->getContent(), true);
374
2a1ceb67 375 $this->assertGreaterThanOrEqual(1, \count($content));
900c8448
NL
376 $this->assertNotEmpty($content['_embedded']['items']);
377 $this->assertGreaterThanOrEqual(1, $content['total']);
f808b016 378 $this->assertSame(1, $content['page']);
900c8448
NL
379 $this->assertGreaterThanOrEqual(1, $content['pages']);
380
381 $this->assertArrayHasKey('_links', $content);
382 $this->assertArrayHasKey('self', $content['_links']);
383 $this->assertArrayHasKey('first', $content['_links']);
384 $this->assertArrayHasKey('last', $content['_links']);
385
386 foreach (['self', 'first', 'last'] as $link) {
387 $this->assertArrayHasKey('href', $content['_links'][$link]);
388 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
389 }
390
f808b016 391 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
392 }
393
394 public function testGetDatedSupEntries()
395 {
396 $future = new \DateTime(date('Y-m-d H:i:s'));
397 $this->client->request('GET', '/api/entries', ['since' => $future->getTimestamp() + 1000]);
398
f808b016 399 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
400
401 $content = json_decode($this->client->getResponse()->getContent(), true);
402
2a1ceb67 403 $this->assertGreaterThanOrEqual(1, \count($content));
900c8448 404 $this->assertEmpty($content['_embedded']['items']);
f808b016
JB
405 $this->assertSame(0, $content['total']);
406 $this->assertSame(1, $content['page']);
407 $this->assertSame(1, $content['pages']);
900c8448
NL
408
409 $this->assertArrayHasKey('_links', $content);
410 $this->assertArrayHasKey('self', $content['_links']);
411 $this->assertArrayHasKey('first', $content['_links']);
412 $this->assertArrayHasKey('last', $content['_links']);
413
414 foreach (['self', 'first', 'last'] as $link) {
415 $this->assertArrayHasKey('href', $content['_links'][$link]);
f808b016 416 $this->assertContains('since=' . ($future->getTimestamp() + 1000), $content['_links'][$link]['href']);
900c8448
NL
417 }
418
f808b016 419 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
420 }
421
422 public function testDeleteEntry()
423 {
4e0ed336
KD
424 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
425 $entry = new Entry($em->getReference(User::class, 1));
426 $entry->setUrl('http://0.0.0.0/test-delete-entry');
427 $entry->setTitle('Test delete entry');
428 $em->persist($entry);
429 $em->flush();
900c8448 430
4e0ed336 431 $em->clear();
900c8448 432
4e0ed336
KD
433 $e = [
434 'title' => $entry->getTitle(),
435 'url' => $entry->getUrl(),
436 'id' => $entry->getId(),
437 ];
438
439 $this->client->request('DELETE', '/api/entries/' . $e['id'] . '.json');
900c8448 440
f808b016 441 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
442
443 $content = json_decode($this->client->getResponse()->getContent(), true);
444
4e0ed336
KD
445 $this->assertSame($e['title'], $content['title']);
446 $this->assertSame($e['url'], $content['url']);
447 $this->assertSame($e['id'], $content['id']);
448
449 // We'll try to delete this entry again
12a97c35
KD
450 $client = $this->createAuthorizedClient();
451 $client->request('DELETE', '/api/entries/' . $e['id'] . '.json');
4e0ed336 452
12a97c35 453 $this->assertSame(404, $client->getResponse()->getStatusCode());
4e0ed336 454 }
900c8448 455
50830204
KD
456 public function testDeleteEntryExpectId()
457 {
458 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
459 $entry = new Entry($em->getReference(User::class, 1));
460 $entry->setUrl('http://0.0.0.0/test-delete-entry-id');
461 $em->persist($entry);
462 $em->flush();
463
464 $em->clear();
465
466 $id = $entry->getId();
467
468 $this->client->request('DELETE', '/api/entries/' . $id . '.json?expect=id');
900c8448 469
f808b016 470 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
471
472 $content = json_decode($this->client->getResponse()->getContent(), true);
473
50830204
KD
474 $this->assertSame($id, $content['id']);
475 $this->assertArrayNotHasKey('url', $content);
900c8448
NL
476
477 // We'll try to delete this entry again
12a97c35
KD
478 $client = $this->createAuthorizedClient();
479 $client->request('DELETE', '/api/entries/' . $id . '.json');
900c8448 480
12a97c35
KD
481 $this->assertSame(404, $client->getResponse()->getStatusCode());
482 }
483
484 public function testDeleteEntryExpectBadRequest()
485 {
486 $this->client->request('DELETE', '/api/entries/1.json?expect=badrequest');
900c8448 487
12a97c35 488 $this->assertSame(400, $this->client->getResponse()->getStatusCode());
900c8448
NL
489 }
490
491 public function testPostEntry()
492 {
493 $this->client->request('POST', '/api/entries.json', [
77854331 494 'url' => 'https://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html',
900c8448
NL
495 'tags' => 'google',
496 'title' => 'New title for my article',
e668a812 497 'content' => 'my content',
e9056dd9 498 'language' => 'de',
e668a812 499 'published_at' => '2016-09-08T11:55:58+0200',
fb436e8c 500 'authors' => 'bob,helen',
1112e547 501 'public' => 1,
900c8448
NL
502 ]);
503
f808b016 504 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
505
506 $content = json_decode($this->client->getResponse()->getContent(), true);
507
508 $this->assertGreaterThan(0, $content['id']);
77854331 509 $this->assertSame('https://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', $content['url']);
38520658
JB
510 $this->assertSame(0, $content['is_archived']);
511 $this->assertSame(0, $content['is_starred']);
a991c46e 512 $this->assertNull($content['starred_at']);
7975395d 513 $this->assertNull($content['archived_at']);
f808b016 514 $this->assertSame('New title for my article', $content['title']);
8f2038e5 515 $this->assertSame($this->getUserId(), $content['user_id']);
fdd725f5 516 $this->assertCount(2, $content['tags']);
65152fcb 517 $this->assertNull($content['origin_url']);
e668a812 518 $this->assertSame('my content', $content['content']);
e9056dd9 519 $this->assertSame('de', $content['language']);
e668a812 520 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
fb436e8c
JB
521 $this->assertCount(2, $content['published_by']);
522 $this->assertContains('bob', $content['published_by']);
523 $this->assertContains('helen', $content['published_by']);
1112e547 524 $this->assertTrue($content['is_public'], 'A public link has been generated for that entry');
900c8448
NL
525 }
526
527 public function testPostSameEntry()
528 {
ff9f89fd 529 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
8f2038e5 530 $entry = new Entry($em->getReference(User::class, $this->getUserId()));
77854331 531 $entry->setUrl('https://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html');
ff9f89fd
JB
532 $entry->setArchived(true);
533 $entry->addTag((new Tag())->setLabel('google'));
534 $entry->addTag((new Tag())->setLabel('apple'));
535 $em->persist($entry);
536 $em->flush();
537 $em->clear();
538
900c8448 539 $this->client->request('POST', '/api/entries.json', [
77854331 540 'url' => 'https://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html',
900c8448
NL
541 'archive' => '1',
542 'tags' => 'google, apple',
543 ]);
544
f808b016 545 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
546
547 $content = json_decode($this->client->getResponse()->getContent(), true);
548
549 $this->assertGreaterThan(0, $content['id']);
77854331 550 $this->assertSame('https://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', $content['url']);
38520658
JB
551 $this->assertSame(1, $content['is_archived']);
552 $this->assertSame(0, $content['is_starred']);
fdd725f5 553 $this->assertCount(3, $content['tags']);
900c8448
NL
554 }
555
08f29ae7 556 public function testPostEntryWhenFetchContentFails()
557 {
558 /** @var \Symfony\Component\DependencyInjection\Container $container */
559 $container = $this->client->getContainer();
560 $contentProxy = $this->getMockBuilder(ContentProxy::class)
561 ->disableOriginalConstructor()
562 ->setMethods(['updateEntry'])
563 ->getMock();
564 $contentProxy->expects($this->any())
565 ->method('updateEntry')
566 ->willThrowException(new \Exception('Test Fetch content fails'));
567 $container->set('wallabag_core.content_proxy', $contentProxy);
568
a9357a83 569 try {
570 $this->client->request('POST', '/api/entries.json', [
571 'url' => 'http://www.example.com/',
572 ]);
573
f808b016 574 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
a9357a83 575 $content = json_decode($this->client->getResponse()->getContent(), true);
576 $this->assertGreaterThan(0, $content['id']);
f808b016 577 $this->assertSame('http://www.example.com/', $content['url']);
af29e1bf
KD
578 $this->assertSame('www.example.com', $content['domain_name']);
579 $this->assertSame('www.example.com', $content['title']);
a9357a83 580 } finally {
581 // Remove the created entry to avoid side effects on other tests
582 if (isset($content['id'])) {
583 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
584 $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']);
585 $em->remove($entry);
586 $em->flush();
587 }
588 }
08f29ae7 589 }
590
900c8448
NL
591 public function testPostArchivedAndStarredEntry()
592 {
a991c46e 593 $now = new \DateTime();
900c8448 594 $this->client->request('POST', '/api/entries.json', [
77854331 595 'url' => 'https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
900c8448
NL
596 'archive' => '1',
597 'starred' => '1',
598 ]);
599
f808b016 600 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
601
602 $content = json_decode($this->client->getResponse()->getContent(), true);
603
604 $this->assertGreaterThan(0, $content['id']);
77854331 605 $this->assertSame('https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']);
38520658
JB
606 $this->assertSame(1, $content['is_archived']);
607 $this->assertSame(1, $content['is_starred']);
a991c46e 608 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['starred_at']))->getTimestamp());
7975395d 609 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['archived_at']))->getTimestamp());
8f2038e5 610 $this->assertSame($this->getUserId(), $content['user_id']);
900c8448
NL
611 }
612
613 public function testPostArchivedAndStarredEntryWithoutQuotes()
614 {
615 $this->client->request('POST', '/api/entries.json', [
77854331 616 'url' => 'https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
900c8448
NL
617 'archive' => 0,
618 'starred' => 1,
619 ]);
620
f808b016 621 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
622
623 $content = json_decode($this->client->getResponse()->getContent(), true);
624
625 $this->assertGreaterThan(0, $content['id']);
77854331 626 $this->assertSame('https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']);
38520658
JB
627 $this->assertSame(0, $content['is_archived']);
628 $this->assertSame(1, $content['is_starred']);
900c8448
NL
629 }
630
00f2368f
KD
631 public function testPostEntryWithOriginUrl()
632 {
633 $this->client->request('POST', '/api/entries.json', [
77854331 634 'url' => 'https://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html',
00f2368f
KD
635 'tags' => 'google',
636 'title' => 'New title for my article',
637 'content' => 'my content',
638 'language' => 'de',
639 'published_at' => '2016-09-08T11:55:58+0200',
640 'authors' => 'bob,helen',
641 'public' => 1,
642 'origin_url' => 'http://mysource.tld',
643 ]);
644
645 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
646
647 $content = json_decode($this->client->getResponse()->getContent(), true);
648
649 $this->assertGreaterThan(0, $content['id']);
77854331 650 $this->assertSame('https://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', $content['url']);
00f2368f
KD
651 $this->assertSame('http://mysource.tld', $content['origin_url']);
652 }
653
900c8448
NL
654 public function testPatchEntry()
655 {
656 $entry = $this->client->getContainer()
657 ->get('doctrine.orm.entity_manager')
658 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 659 ->findOneByUser($this->getUserId());
900c8448
NL
660
661 if (!$entry) {
662 $this->markTestSkipped('No content found in db.');
663 }
664
c18a2476 665 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
900c8448 666 'title' => 'New awesome title',
f808b016 667 'tags' => 'new tag ' . uniqid(),
900c8448
NL
668 'starred' => '1',
669 'archive' => '0',
e9056dd9 670 'language' => 'de_AT',
645291e8
JB
671 'preview_picture' => 'http://preview.io/picture.jpg',
672 'authors' => 'bob,sponge',
673 'content' => 'awesome',
1112e547 674 'public' => 0,
a05b6115 675 'published_at' => 1488833381,
900c8448
NL
676 ]);
677
f808b016 678 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
679
680 $content = json_decode($this->client->getResponse()->getContent(), true);
681
f808b016
JB
682 $this->assertSame($entry->getId(), $content['id']);
683 $this->assertSame($entry->getUrl(), $content['url']);
684 $this->assertSame('New awesome title', $content['title']);
2a1ceb67 685 $this->assertGreaterThanOrEqual(1, \count($content['tags']), 'We force only one tag');
8f2038e5 686 $this->assertSame($this->getUserId(), $content['user_id']);
f808b016
JB
687 $this->assertSame('de_AT', $content['language']);
688 $this->assertSame('http://preview.io/picture.jpg', $content['preview_picture']);
645291e8
JB
689 $this->assertContains('sponge', $content['published_by']);
690 $this->assertContains('bob', $content['published_by']);
f808b016 691 $this->assertSame('awesome', $content['content']);
1112e547 692 $this->assertFalse($content['is_public'], 'Entry is no more shared');
a05b6115 693 $this->assertContains('2017-03-06', $content['published_at']);
900c8448
NL
694 }
695
696 public function testPatchEntryWithoutQuotes()
697 {
698 $entry = $this->client->getContainer()
699 ->get('doctrine.orm.entity_manager')
700 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 701 ->findOneByUser($this->getUserId());
900c8448
NL
702
703 if (!$entry) {
704 $this->markTestSkipped('No content found in db.');
705 }
706
a05b6115
JB
707 $previousContent = $entry->getContent();
708 $previousLanguage = $entry->getLanguage();
900c8448 709
f808b016 710 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
900c8448 711 'title' => 'New awesome title',
f808b016 712 'tags' => 'new tag ' . uniqid(),
900c8448
NL
713 'starred' => 1,
714 'archive' => 0,
645291e8 715 'authors' => ['bob', 'sponge'],
900c8448
NL
716 ]);
717
f808b016 718 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
719
720 $content = json_decode($this->client->getResponse()->getContent(), true);
721
f808b016
JB
722 $this->assertSame($entry->getId(), $content['id']);
723 $this->assertSame($entry->getUrl(), $content['url']);
2a1ceb67 724 $this->assertGreaterThanOrEqual(1, \count($content['tags']), 'We force only one tag');
f808b016 725 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
c18a2476
JB
726 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
727 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
900c8448
NL
728 }
729
00f2368f
KD
730 public function testPatchEntryWithOriginUrl()
731 {
732 $entry = $this->client->getContainer()
733 ->get('doctrine.orm.entity_manager')
734 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 735 ->findOneByUser($this->getUserId());
00f2368f
KD
736
737 if (!$entry) {
738 $this->markTestSkipped('No content found in db.');
739 }
740
741 $previousContent = $entry->getContent();
742 $previousLanguage = $entry->getLanguage();
743
744 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
745 'title' => 'Another awesome title just for profit',
746 'origin_url' => 'https://myawesomesource.example.com',
747 ]);
748
749 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
750
751 $content = json_decode($this->client->getResponse()->getContent(), true);
752
753 $this->assertSame($entry->getId(), $content['id']);
754 $this->assertSame($entry->getUrl(), $content['url']);
755 $this->assertSame('https://myawesomesource.example.com', $content['origin_url']);
756 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
757 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
758 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
759 }
760
65152fcb
KD
761 public function testPatchEntryRemoveOriginUrl()
762 {
763 $entry = $this->client->getContainer()
764 ->get('doctrine.orm.entity_manager')
765 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 766 ->findOneByUser($this->getUserId());
65152fcb
KD
767
768 if (!$entry) {
769 $this->markTestSkipped('No content found in db.');
770 }
771
772 $previousContent = $entry->getContent();
773 $previousLanguage = $entry->getLanguage();
774 $previousTitle = $entry->getTitle();
775
776 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
777 'origin_url' => '',
778 ]);
779
780 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
781
782 $content = json_decode($this->client->getResponse()->getContent(), true);
783
784 $this->assertSame($entry->getId(), $content['id']);
785 $this->assertSame($entry->getUrl(), $content['url']);
786 $this->assertEmpty($content['origin_url']);
787 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
788 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
789 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
790 $this->assertSame($previousTitle, $content['title'], 'Ensure title has not moved');
791 }
eae8138b
KD
792
793 public function testPatchEntryNullOriginUrl()
794 {
795 $entry = $this->client->getContainer()
65152fcb
KD
796 ->get('doctrine.orm.entity_manager')
797 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 798 ->findOneByUser($this->getUserId());
eae8138b
KD
799
800 if (!$entry) {
801 $this->markTestSkipped('No content found in db.');
802 }
803
804 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
65152fcb
KD
805 'origin_url' => null,
806 ]);
eae8138b
KD
807
808 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
809
810 $content = json_decode($this->client->getResponse()->getContent(), true);
811
812 $this->assertNull($content['origin_url']);
813 }
65152fcb 814
900c8448
NL
815 public function testGetTagsEntry()
816 {
817 $entry = $this->client->getContainer()
818 ->get('doctrine.orm.entity_manager')
819 ->getRepository('WallabagCoreBundle:Entry')
820 ->findOneWithTags($this->user->getId());
821
822 $entry = $entry[0];
823
824 if (!$entry) {
825 $this->markTestSkipped('No content found in db.');
826 }
827
828 $tags = [];
829 foreach ($entry->getTags() as $tag) {
830 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
831 }
832
f808b016 833 $this->client->request('GET', '/api/entries/' . $entry->getId() . '/tags');
900c8448 834
f808b016 835 $this->assertSame(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent());
900c8448
NL
836 }
837
838 public function testPostTagsOnEntry()
839 {
840 $entry = $this->client->getContainer()
841 ->get('doctrine.orm.entity_manager')
842 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 843 ->findOneByUser($this->getUserId());
900c8448
NL
844
845 if (!$entry) {
846 $this->markTestSkipped('No content found in db.');
847 }
848
2a1ceb67 849 $nbTags = \count($entry->getTags());
900c8448
NL
850
851 $newTags = 'tag1,tag2,tag3';
852
f808b016 853 $this->client->request('POST', '/api/entries/' . $entry->getId() . '/tags', ['tags' => $newTags]);
900c8448 854
f808b016 855 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
856
857 $content = json_decode($this->client->getResponse()->getContent(), true);
858
859 $this->assertArrayHasKey('tags', $content);
1e0d8ad7 860 $this->assertCount($nbTags + 3, $content['tags']);
900c8448
NL
861
862 $entryDB = $this->client->getContainer()
863 ->get('doctrine.orm.entity_manager')
864 ->getRepository('WallabagCoreBundle:Entry')
865 ->find($entry->getId());
866
867 $tagsInDB = [];
868 foreach ($entryDB->getTags()->toArray() as $tag) {
869 $tagsInDB[$tag->getId()] = $tag->getLabel();
870 }
871
872 foreach (explode(',', $newTags) as $tag) {
873 $this->assertContains($tag, $tagsInDB);
874 }
875 }
876
877 public function testDeleteOneTagEntry()
878 {
879 $entry = $this->client->getContainer()
880 ->get('doctrine.orm.entity_manager')
881 ->getRepository('WallabagCoreBundle:Entry')
882 ->findOneWithTags($this->user->getId());
883 $entry = $entry[0];
884
885 if (!$entry) {
886 $this->markTestSkipped('No content found in db.');
887 }
888
889 // hydrate the tags relations
2a1ceb67 890 $nbTags = \count($entry->getTags());
900c8448
NL
891 $tag = $entry->getTags()[0];
892
f808b016 893 $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '/tags/' . $tag->getId() . '.json');
900c8448 894
f808b016 895 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
896
897 $content = json_decode($this->client->getResponse()->getContent(), true);
898
899 $this->assertArrayHasKey('tags', $content);
1e0d8ad7 900 $this->assertCount($nbTags - 1, $content['tags']);
900c8448
NL
901 }
902
903 public function testSaveIsArchivedAfterPost()
904 {
905 $entry = $this->client->getContainer()
906 ->get('doctrine.orm.entity_manager')
907 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 908 ->findOneBy(['user' => $this->getUserId(), 'isArchived' => true]);
900c8448
NL
909
910 if (!$entry) {
911 $this->markTestSkipped('No content found in db.');
912 }
913
914 $this->client->request('POST', '/api/entries.json', [
915 'url' => $entry->getUrl(),
916 ]);
917
f808b016 918 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
919
920 $content = json_decode($this->client->getResponse()->getContent(), true);
921
38520658 922 $this->assertSame(1, $content['is_archived']);
900c8448
NL
923 }
924
925 public function testSaveIsStarredAfterPost()
926 {
927 $entry = $this->client->getContainer()
928 ->get('doctrine.orm.entity_manager')
929 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 930 ->findOneBy(['user' => $this->getUserId(), 'isStarred' => true]);
900c8448
NL
931
932 if (!$entry) {
933 $this->markTestSkipped('No content found in db.');
934 }
935
936 $this->client->request('POST', '/api/entries.json', [
937 'url' => $entry->getUrl(),
938 ]);
939
f808b016 940 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
941
942 $content = json_decode($this->client->getResponse()->getContent(), true);
943
38520658 944 $this->assertSame(1, $content['is_starred']);
900c8448
NL
945 }
946
947 public function testSaveIsArchivedAfterPatch()
948 {
949 $entry = $this->client->getContainer()
950 ->get('doctrine.orm.entity_manager')
951 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 952 ->findOneBy(['user' => $this->getUserId(), 'isArchived' => true]);
900c8448
NL
953
954 if (!$entry) {
955 $this->markTestSkipped('No content found in db.');
956 }
957
a05b6115
JB
958 $previousTitle = $entry->getTitle();
959
c18a2476
JB
960 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
961 'title' => $entry->getTitle() . '++',
900c8448
NL
962 ]);
963
f808b016 964 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
965
966 $content = json_decode($this->client->getResponse()->getContent(), true);
967
38520658 968 $this->assertSame(1, $content['is_archived']);
c18a2476 969 $this->assertSame($previousTitle . '++', $content['title']);
900c8448
NL
970 }
971
972 public function testSaveIsStarredAfterPatch()
973 {
a991c46e 974 $now = new \DateTime();
900c8448
NL
975 $entry = $this->client->getContainer()
976 ->get('doctrine.orm.entity_manager')
977 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 978 ->findOneBy(['user' => $this->getUserId(), 'isStarred' => true]);
900c8448
NL
979
980 if (!$entry) {
981 $this->markTestSkipped('No content found in db.');
982 }
f808b016
JB
983 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
984 'title' => $entry->getTitle() . '++',
900c8448
NL
985 ]);
986
f808b016 987 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
988
989 $content = json_decode($this->client->getResponse()->getContent(), true);
990
38520658 991 $this->assertSame(1, $content['is_starred']);
a991c46e 992 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['starred_at']))->getTimestamp());
900c8448
NL
993 }
994
9c2b2aae 995 public function dataForEntriesExistWithUrl()
900c8448 996 {
8a645662 997 $url = hash('sha1', 'http://0.0.0.0/entry2');
9c2b2aae
JB
998
999 return [
1000 'with_id' => [
1001 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1',
1002 'expectedValue' => 2,
1003 ],
1004 'without_id' => [
1005 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2',
1006 'expectedValue' => true,
1007 ],
1008 'hashed_url_with_id' => [
1009 'url' => '/api/entries/exists?hashed_url=' . $url . '&return_id=1',
1010 'expectedValue' => 2,
1011 ],
1012 'hashed_url_without_id' => [
1013 'url' => '/api/entries/exists?hashed_url=' . $url . '',
1014 'expectedValue' => true,
1015 ],
1016 ];
900c8448
NL
1017 }
1018
9c2b2aae
JB
1019 /**
1020 * @dataProvider dataForEntriesExistWithUrl
1021 */
1022 public function testGetEntriesExists($url, $expectedValue)
bfe02a0b 1023 {
9c2b2aae 1024 $this->client->request('GET', $url);
bfe02a0b
TC
1025
1026 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1027
1028 $content = json_decode($this->client->getResponse()->getContent(), true);
1029
9c2b2aae 1030 $this->assertSame($expectedValue, $content['exists']);
bfe02a0b
TC
1031 }
1032
900c8448 1033 public function testGetEntriesExistsWithManyUrls()
18696f77
JB
1034 {
1035 $url1 = 'http://0.0.0.0/entry2';
1036 $url2 = 'http://0.0.0.0/entry10';
bfe02a0b 1037
f808b016 1038 $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1');
18696f77 1039
f808b016 1040 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
18696f77
JB
1041
1042 $content = json_decode($this->client->getResponse()->getContent(), true);
1043
1044 $this->assertArrayHasKey($url1, $content);
1045 $this->assertArrayHasKey($url2, $content);
8f2038e5
JB
1046 // it returns a database id, we don't know it, so we only check it's greater than the lowest possible value
1047 $this->assertGreaterThan(1, $content[$url1]);
39ffaba3 1048 $this->assertNull($content[$url2]);
18696f77
JB
1049 }
1050
1051 public function testGetEntriesExistsWithManyUrlsReturnBool()
900c8448
NL
1052 {
1053 $url1 = 'http://0.0.0.0/entry2';
1054 $url2 = 'http://0.0.0.0/entry10';
f808b016 1055 $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2);
900c8448 1056
f808b016 1057 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
1058
1059 $content = json_decode($this->client->getResponse()->getContent(), true);
1060
1061 $this->assertArrayHasKey($url1, $content);
1062 $this->assertArrayHasKey($url2, $content);
39ffaba3
JB
1063 $this->assertTrue($content[$url1]);
1064 $this->assertFalse($content[$url2]);
900c8448
NL
1065 }
1066
bfe02a0b
TC
1067 public function testGetEntriesExistsWithManyUrlsHashed()
1068 {
1069 $url1 = 'http://0.0.0.0/entry2';
1070 $url2 = 'http://0.0.0.0/entry10';
8a645662 1071 $this->client->request('GET', '/api/entries/exists?hashed_urls[]=' . hash('sha1', $url1) . '&hashed_urls[]=' . hash('sha1', $url2) . '&return_id=1');
bfe02a0b
TC
1072
1073 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1074
1075 $content = json_decode($this->client->getResponse()->getContent(), true);
1076
8a645662
JB
1077 $this->assertArrayHasKey(hash('sha1', $url1), $content);
1078 $this->assertArrayHasKey(hash('sha1', $url2), $content);
1079 $this->assertSame(2, $content[hash('sha1', $url1)]);
1080 $this->assertNull($content[hash('sha1', $url2)]);
bfe02a0b
TC
1081 }
1082
1083 public function testGetEntriesExistsWithManyUrlsHashedReturnBool()
1084 {
1085 $url1 = 'http://0.0.0.0/entry2';
1086 $url2 = 'http://0.0.0.0/entry10';
8a645662 1087 $this->client->request('GET', '/api/entries/exists?hashed_urls[]=' . hash('sha1', $url1) . '&hashed_urls[]=' . hash('sha1', $url2));
bfe02a0b
TC
1088
1089 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1090
1091 $content = json_decode($this->client->getResponse()->getContent(), true);
1092
8a645662
JB
1093 $this->assertArrayHasKey(hash('sha1', $url1), $content);
1094 $this->assertArrayHasKey(hash('sha1', $url2), $content);
1095 $this->assertTrue($content[hash('sha1', $url1)]);
1096 $this->assertFalse($content[hash('sha1', $url2)]);
bfe02a0b
TC
1097 }
1098
900c8448 1099 public function testGetEntriesExistsWhichDoesNotExists()
c579ce23
JB
1100 {
1101 $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2');
1102
1103 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1104
1105 $content = json_decode($this->client->getResponse()->getContent(), true);
1106
1107 $this->assertFalse($content['exists']);
1108 }
1109
1110 public function testGetEntriesExistsWhichDoesNotExistsWithHashedUrl()
900c8448 1111 {
8a645662 1112 $this->client->request('GET', '/api/entries/exists?hashed_url=' . hash('sha1', 'http://google.com/entry2'));
900c8448 1113
f808b016 1114 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
1115
1116 $content = json_decode($this->client->getResponse()->getContent(), true);
1117
64a5a6cf 1118 $this->assertFalse($content['exists']);
900c8448
NL
1119 }
1120
1121 public function testGetEntriesExistsWithNoUrl()
c579ce23
JB
1122 {
1123 $this->client->request('GET', '/api/entries/exists?url=');
1124
1125 $this->assertSame(403, $this->client->getResponse()->getStatusCode());
1126 }
1127
1128 public function testGetEntriesExistsWithNoHashedUrl()
900c8448 1129 {
9c2b2aae 1130 $this->client->request('GET', '/api/entries/exists?hashed_url=');
900c8448 1131
f808b016 1132 $this->assertSame(403, $this->client->getResponse()->getStatusCode());
900c8448 1133 }
0a6f4568
JB
1134
1135 public function testReloadEntryErrorWhileFetching()
1136 {
70584b42 1137 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
0a6f4568 1138 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 1139 ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getUserId());
0a6f4568
JB
1140
1141 if (!$entry) {
1142 $this->markTestSkipped('No content found in db.');
1143 }
1144
f808b016
JB
1145 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json');
1146 $this->assertSame(304, $this->client->getResponse()->getStatusCode());
0a6f4568
JB
1147 }
1148
1149 public function testReloadEntry()
1150 {
1151 $this->client->request('POST', '/api/entries.json', [
77854331 1152 'url' => 'https://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html',
0a6f4568
JB
1153 'archive' => '1',
1154 'tags' => 'google, apple',
1155 ]);
1156
1157 $json = json_decode($this->client->getResponse()->getContent(), true);
1158
1159 $this->setUp();
1160
f808b016
JB
1161 $this->client->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json');
1162 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
0a6f4568
JB
1163
1164 $content = json_decode($this->client->getResponse()->getContent(), true);
1165
1166 $this->assertNotEmpty($content['title']);
1167
f808b016 1168 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
0a6f4568 1169 }
d1fc5902
NL
1170
1171 public function testPostEntriesTagsListAction()
1172 {
80299ed2
NL
1173 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
1174 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 1175 ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getUserId());
80299ed2
NL
1176
1177 $tags = $entry->getTags();
1178
dcbebc17 1179 $this->assertCount(2, $tags);
80299ed2 1180
d1fc5902
NL
1181 $list = [
1182 [
dcbebc17 1183 'url' => 'http://0.0.0.0/entry4',
80299ed2 1184 'tags' => 'new tag 1, new tag 2',
d1fc5902 1185 ],
80299ed2
NL
1186 ];
1187
f808b016 1188 $this->client->request('POST', '/api/entries/tags/lists?list=' . json_encode($list));
80299ed2 1189
f808b016 1190 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
80299ed2
NL
1191
1192 $content = json_decode($this->client->getResponse()->getContent(), true);
1193
1194 $this->assertInternalType('int', $content[0]['entry']);
f808b016 1195 $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']);
80299ed2
NL
1196
1197 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
1198 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 1199 ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getUserId());
80299ed2
NL
1200
1201 $tags = $entry->getTags();
dcbebc17 1202 $this->assertCount(4, $tags);
80299ed2
NL
1203 }
1204
a05b6115
JB
1205 public function testPostEntriesTagsListActionNoList()
1206 {
c18a2476 1207 $this->client->request('POST', '/api/entries/tags/lists?list=' . json_encode([]));
a05b6115 1208
c18a2476 1209 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
a05b6115
JB
1210
1211 $content = json_decode($this->client->getResponse()->getContent(), true);
1212
1213 $this->assertEmpty($content);
1214 }
1215
80299ed2
NL
1216 public function testDeleteEntriesTagsListAction()
1217 {
7ab5eb95 1218 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
8f2038e5 1219 $entry = new Entry($em->getReference(User::class, $this->getUserId()));
7ab5eb95 1220 $entry->setUrl('http://0.0.0.0/test-entry');
1221 $entry->addTag((new Tag())->setLabel('foo-tag'));
1222 $entry->addTag((new Tag())->setLabel('bar-tag'));
1223 $em->persist($entry);
1224 $em->flush();
80299ed2 1225
7ab5eb95 1226 $em->clear();
80299ed2
NL
1227
1228 $list = [
d1fc5902 1229 [
7ab5eb95 1230 'url' => 'http://0.0.0.0/test-entry',
1231 'tags' => 'foo-tag, bar-tag',
d1fc5902
NL
1232 ],
1233 ];
1234
f808b016
JB
1235 $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list));
1236 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
7ab5eb95 1237
1238 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
1239 $this->assertCount(0, $entry->getTags());
1eca7831
NL
1240 }
1241
a05b6115
JB
1242 public function testDeleteEntriesTagsListActionNoList()
1243 {
c18a2476 1244 $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode([]));
a05b6115 1245
c18a2476 1246 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
a05b6115
JB
1247
1248 $content = json_decode($this->client->getResponse()->getContent(), true);
1249
1250 $this->assertEmpty($content);
1251 }
1252
a7abcc7b 1253 public function testPostEntriesListAction()
1eca7831
NL
1254 {
1255 $list = [
77854331 1256 'https://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
7fa844a3 1257 'http://0.0.0.0/entry2',
1eca7831
NL
1258 ];
1259
f808b016 1260 $this->client->request('POST', '/api/entries/lists?urls=' . json_encode($list));
d1fc5902 1261
f808b016 1262 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
d1fc5902
NL
1263
1264 $content = json_decode($this->client->getResponse()->getContent(), true);
1265
80299ed2 1266 $this->assertInternalType('int', $content[0]['entry']);
77854331 1267 $this->assertSame('https://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', $content[0]['url']);
d1fc5902 1268
1eca7831 1269 $this->assertInternalType('int', $content[1]['entry']);
f808b016 1270 $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']);
a7abcc7b 1271 }
d1fc5902 1272
a05b6115
JB
1273 public function testPostEntriesListActionWithNoUrls()
1274 {
c18a2476 1275 $this->client->request('POST', '/api/entries/lists?urls=' . json_encode([]));
a05b6115 1276
c18a2476 1277 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
a05b6115
JB
1278
1279 $content = json_decode($this->client->getResponse()->getContent(), true);
1280
1281 $this->assertEmpty($content);
1282 }
1283
a7abcc7b
NL
1284 public function testDeleteEntriesListAction()
1285 {
7ab5eb95 1286 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
8f2038e5 1287 $em->persist((new Entry($em->getReference(User::class, $this->getUserId())))->setUrl('http://0.0.0.0/test-entry1'));
7ab5eb95 1288
1289 $em->flush();
1290 $em->clear();
a7abcc7b 1291 $list = [
7ab5eb95 1292 'http://0.0.0.0/test-entry1',
1293 'http://0.0.0.0/test-entry-not-exist',
a7abcc7b
NL
1294 ];
1295
f808b016 1296 $this->client->request('DELETE', '/api/entries/list?urls=' . json_encode($list));
d1fc5902 1297
f808b016 1298 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
a7abcc7b
NL
1299
1300 $content = json_decode($this->client->getResponse()->getContent(), true);
1301
1302 $this->assertTrue($content[0]['entry']);
f808b016 1303 $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']);
1eca7831 1304
a7abcc7b 1305 $this->assertFalse($content[1]['entry']);
f808b016 1306 $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
d1fc5902 1307 }
efd351c9 1308
a05b6115
JB
1309 public function testDeleteEntriesListActionWithNoUrls()
1310 {
c18a2476 1311 $this->client->request('DELETE', '/api/entries/list?urls=' . json_encode([]));
a05b6115 1312
c18a2476 1313 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
a05b6115
JB
1314
1315 $content = json_decode($this->client->getResponse()->getContent(), true);
1316
1317 $this->assertEmpty($content);
1318 }
1319
efd351c9
NL
1320 public function testLimitBulkAction()
1321 {
1322 $list = [
1323 'http://0.0.0.0/entry1',
1324 'http://0.0.0.0/entry1',
1325 'http://0.0.0.0/entry1',
1326 'http://0.0.0.0/entry1',
1327 'http://0.0.0.0/entry1',
1328 'http://0.0.0.0/entry1',
1329 'http://0.0.0.0/entry1',
1330 'http://0.0.0.0/entry1',
1331 'http://0.0.0.0/entry1',
1332 'http://0.0.0.0/entry1',
1333 'http://0.0.0.0/entry1',
1334 ];
1335
f808b016 1336 $this->client->request('POST', '/api/entries/lists?urls=' . json_encode($list));
72db15ca 1337
f808b016 1338 $this->assertSame(400, $this->client->getResponse()->getStatusCode());
72db15ca 1339 $this->assertContains('API limit reached', $this->client->getResponse()->getContent());
d1fc5902 1340 }
ff9f89fd
JB
1341
1342 public function testRePostEntryAndReUsePublishedAt()
1343 {
1344 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
8f2038e5 1345 $entry = new Entry($em->getReference(User::class, $this->getUserId()));
ff9f89fd
JB
1346 $entry->setTitle('Antoine de Caunes : « Je veux avoir le droit de tâtonner »');
1347 $entry->setContent('hihi');
77854331 1348 $entry->setUrl('https://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html');
ff9f89fd
JB
1349 $entry->setPublishedAt(new \DateTime('2017-06-26T07:46:02+0200'));
1350 $em->persist($entry);
1351 $em->flush();
1352 $em->clear();
1353
1354 $this->client->request('POST', '/api/entries.json', [
77854331 1355 'url' => 'https://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html',
ff9f89fd
JB
1356 ]);
1357
1358 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1359
1360 $content = json_decode($this->client->getResponse()->getContent(), true);
1361
1362 $this->assertGreaterThan(0, $content['id']);
77854331 1363 $this->assertSame('https://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html', $content['url']);
ff9f89fd 1364 }
900c8448 1365}