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