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