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