]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
Fix tests
[github/wallabag/wallabag.git] / tests / Wallabag / ApiBundle / Controller / EntryRestControllerTest.php
CommitLineData
900c8448
NL
1<?php
2
3namespace Tests\Wallabag\ApiBundle\Controller;
4
5use Tests\Wallabag\ApiBundle\WallabagApiTestCase;
7ab5eb95 6use Wallabag\CoreBundle\Entity\Entry;
900c8448 7use Wallabag\CoreBundle\Entity\Tag;
08f29ae7 8use Wallabag\CoreBundle\Helper\ContentProxy;
7ab5eb95 9use Wallabag\UserBundle\Entity\User;
900c8448
NL
10
11class EntryRestControllerTest extends WallabagApiTestCase
12{
13 public function testGetOneEntry()
14 {
15 $entry = $this->client->getContainer()
16 ->get('doctrine.orm.entity_manager')
17 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 18 ->findOneBy(['user' => $this->getUserId(), 'isArchived' => false]);
900c8448
NL
19
20 if (!$entry) {
21 $this->markTestSkipped('No content found in db.');
22 }
23
f808b016
JB
24 $this->client->request('GET', '/api/entries/' . $entry->getId() . '.json');
25 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
26
27 $content = json_decode($this->client->getResponse()->getContent(), true);
28
f808b016
JB
29 $this->assertSame($entry->getTitle(), $content['title']);
30 $this->assertSame($entry->getUrl(), $content['url']);
2a1ceb67 31 $this->assertCount(\count($entry->getTags()), $content['tags']);
f808b016
JB
32 $this->assertSame($entry->getUserName(), $content['user_name']);
33 $this->assertSame($entry->getUserEmail(), $content['user_email']);
34 $this->assertSame($entry->getUserId(), $content['user_id']);
900c8448 35
f808b016 36 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
5a619812
JB
37 }
38
00f2368f
KD
39 public function testGetOneEntryWithOriginUrl()
40 {
41 $entry = $this->client->getContainer()
42 ->get('doctrine.orm.entity_manager')
43 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 44 ->findOneBy(['user' => $this->getUserId(), 'url' => 'http://0.0.0.0/entry2']);
00f2368f
KD
45
46 if (!$entry) {
47 $this->markTestSkipped('No content found in db.');
48 }
49
50 $this->client->request('GET', '/api/entries/' . $entry->getId() . '.json');
51 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
52
53 $content = json_decode($this->client->getResponse()->getContent(), true);
54
55 $this->assertSame($entry->getOriginUrl(), $content['origin_url']);
56 }
57
5a619812
JB
58 public function testExportEntry()
59 {
60 $entry = $this->client->getContainer()
61 ->get('doctrine.orm.entity_manager')
62 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 63 ->findOneBy(['user' => $this->getUserId(), 'isArchived' => false]);
5a619812
JB
64
65 if (!$entry) {
66 $this->markTestSkipped('No content found in db.');
67 }
68
f808b016
JB
69 $this->client->request('GET', '/api/entries/' . $entry->getId() . '/export.epub');
70 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
5a619812
JB
71
72 // epub format got the content type in the content
73 $this->assertContains('application/epub', $this->client->getResponse()->getContent());
f808b016 74 $this->assertSame('application/epub+zip', $this->client->getResponse()->headers->get('Content-Type'));
5a619812
JB
75
76 // re-auth client for mobi
77 $client = $this->createAuthorizedClient();
f808b016
JB
78 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.mobi');
79 $this->assertSame(200, $client->getResponse()->getStatusCode());
5a619812 80
f808b016 81 $this->assertSame('application/x-mobipocket-ebook', $client->getResponse()->headers->get('Content-Type'));
5a619812
JB
82
83 // re-auth client for pdf
84 $client = $this->createAuthorizedClient();
f808b016
JB
85 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.pdf');
86 $this->assertSame(200, $client->getResponse()->getStatusCode());
5a619812
JB
87
88 $this->assertContains('PDF-', $client->getResponse()->getContent());
f808b016 89 $this->assertSame('application/pdf', $client->getResponse()->headers->get('Content-Type'));
5a619812
JB
90
91 // re-auth client for pdf
92 $client = $this->createAuthorizedClient();
f808b016
JB
93 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.txt');
94 $this->assertSame(200, $client->getResponse()->getStatusCode());
5a619812
JB
95
96 $this->assertContains('text/plain', $client->getResponse()->headers->get('Content-Type'));
97
98 // re-auth client for pdf
99 $client = $this->createAuthorizedClient();
f808b016
JB
100 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.csv');
101 $this->assertSame(200, $client->getResponse()->getStatusCode());
5a619812
JB
102
103 $this->assertContains('application/csv', $client->getResponse()->headers->get('Content-Type'));
900c8448
NL
104 }
105
106 public function testGetOneEntryWrongUser()
107 {
108 $entry = $this->client->getContainer()
109 ->get('doctrine.orm.entity_manager')
110 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 111 ->findOneBy(['user' => $this->getUserId('bob'), 'isArchived' => false]);
900c8448
NL
112
113 if (!$entry) {
114 $this->markTestSkipped('No content found in db.');
115 }
116
f808b016 117 $this->client->request('GET', '/api/entries/' . $entry->getId() . '.json');
900c8448 118
f808b016 119 $this->assertSame(403, $this->client->getResponse()->getStatusCode());
900c8448
NL
120 }
121
122 public function testGetEntries()
123 {
124 $this->client->request('GET', '/api/entries');
125
f808b016 126 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
127
128 $content = json_decode($this->client->getResponse()->getContent(), true);
129
2a1ceb67 130 $this->assertGreaterThanOrEqual(1, \count($content));
900c8448
NL
131 $this->assertNotEmpty($content['_embedded']['items']);
132 $this->assertGreaterThanOrEqual(1, $content['total']);
f808b016 133 $this->assertSame(1, $content['page']);
900c8448
NL
134 $this->assertGreaterThanOrEqual(1, $content['pages']);
135
f808b016 136 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
137 }
138
139 public function testGetEntriesWithFullOptions()
140 {
141 $this->client->request('GET', '/api/entries', [
142 'archive' => 1,
143 'starred' => 1,
144 'sort' => 'updated',
145 'order' => 'asc',
146 'page' => 1,
147 'perPage' => 2,
148 'tags' => 'foo',
149 'since' => 1443274283,
1112e547 150 'public' => 0,
900c8448
NL
151 ]);
152
f808b016 153 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
154
155 $content = json_decode($this->client->getResponse()->getContent(), true);
156
2a1ceb67 157 $this->assertGreaterThanOrEqual(1, \count($content));
900c8448
NL
158 $this->assertArrayHasKey('items', $content['_embedded']);
159 $this->assertGreaterThanOrEqual(0, $content['total']);
f808b016
JB
160 $this->assertSame(1, $content['page']);
161 $this->assertSame(2, $content['limit']);
900c8448
NL
162 $this->assertGreaterThanOrEqual(1, $content['pages']);
163
164 $this->assertArrayHasKey('_links', $content);
165 $this->assertArrayHasKey('self', $content['_links']);
166 $this->assertArrayHasKey('first', $content['_links']);
167 $this->assertArrayHasKey('last', $content['_links']);
168
169 foreach (['self', 'first', 'last'] as $link) {
170 $this->assertArrayHasKey('href', $content['_links'][$link]);
171 $this->assertContains('archive=1', $content['_links'][$link]['href']);
172 $this->assertContains('starred=1', $content['_links'][$link]['href']);
173 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
174 $this->assertContains('order=asc', $content['_links'][$link]['href']);
175 $this->assertContains('tags=foo', $content['_links'][$link]['href']);
176 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
1112e547
JB
177 $this->assertContains('public=0', $content['_links'][$link]['href']);
178 }
179
f808b016 180 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
1112e547
JB
181 }
182
183 public function testGetEntriesPublicOnly()
184 {
185 $entry = $this->client->getContainer()
186 ->get('doctrine.orm.entity_manager')
187 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 188 ->findOneByUser($this->getUserId());
1112e547
JB
189
190 if (!$entry) {
191 $this->markTestSkipped('No content found in db.');
192 }
193
194 // generate at least one public entry
195 $entry->generateUid();
196
197 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
198 $em->persist($entry);
199 $em->flush();
200
201 $this->client->request('GET', '/api/entries', [
202 'public' => 1,
203 ]);
204
f808b016 205 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1112e547
JB
206
207 $content = json_decode($this->client->getResponse()->getContent(), true);
208
2a1ceb67 209 $this->assertGreaterThanOrEqual(1, \count($content));
1112e547
JB
210 $this->assertArrayHasKey('items', $content['_embedded']);
211 $this->assertGreaterThanOrEqual(1, $content['total']);
f808b016
JB
212 $this->assertSame(1, $content['page']);
213 $this->assertSame(30, $content['limit']);
1112e547
JB
214 $this->assertGreaterThanOrEqual(1, $content['pages']);
215
216 $this->assertArrayHasKey('_links', $content);
217 $this->assertArrayHasKey('self', $content['_links']);
218 $this->assertArrayHasKey('first', $content['_links']);
219 $this->assertArrayHasKey('last', $content['_links']);
220
221 foreach (['self', 'first', 'last'] as $link) {
222 $this->assertArrayHasKey('href', $content['_links'][$link]);
223 $this->assertContains('public=1', $content['_links'][$link]['href']);
900c8448
NL
224 }
225
f808b016 226 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
227 }
228
b60a666d 229 public function testGetEntriesOnPageTwo()
230 {
231 $this->client->request('GET', '/api/entries', [
232 'page' => 2,
233 'perPage' => 2,
234 ]);
235
f808b016 236 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
b60a666d 237
238 $content = json_decode($this->client->getResponse()->getContent(), true);
239
240 $this->assertGreaterThanOrEqual(0, $content['total']);
f808b016
JB
241 $this->assertSame(2, $content['page']);
242 $this->assertSame(2, $content['limit']);
b60a666d 243 }
244
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')
8f2038e5 397 ->findOneByUser($this->getUserId(), ['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 442 $this->assertSame('New title for my article', $content['title']);
8f2038e5 443 $this->assertSame($this->getUserId(), $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 457 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
8f2038e5 458 $entry = new Entry($em->getReference(User::class, $this->getUserId()));
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());
8f2038e5 538 $this->assertSame($this->getUserId(), $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')
8f2038e5 587 ->findOneByUser($this->getUserId());
900c8448
NL
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');
8f2038e5 614 $this->assertSame($this->getUserId(), $content['user_id']);
f808b016
JB
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')
8f2038e5 629 ->findOneByUser($this->getUserId());
900c8448
NL
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')
8f2038e5 663 ->findOneByUser($this->getUserId());
00f2368f
KD
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')
8f2038e5 694 ->findOneByUser($this->getUserId());
65152fcb
KD
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')
8f2038e5 726 ->findOneByUser($this->getUserId());
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')
8f2038e5 771 ->findOneByUser($this->getUserId());
900c8448
NL
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')
8f2038e5 836 ->findOneBy(['user' => $this->getUserId(), 'isArchived' => true]);
900c8448
NL
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')
8f2038e5 858 ->findOneBy(['user' => $this->getUserId(), 'isStarred' => true]);
900c8448
NL
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')
8f2038e5 880 ->findOneBy(['user' => $this->getUserId(), 'isArchived' => true]);
900c8448
NL
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')
8f2038e5 906 ->findOneBy(['user' => $this->getUserId(), 'isStarred' => true]);
900c8448
NL
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
8f2038e5 923 public function testGetEntriesExistsWithReturnId()
900c8448 924 {
8f2038e5
JB
925 $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1');
926
927 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
928
929 $content = json_decode($this->client->getResponse()->getContent(), true);
930
931 // it returns a database id, we don't know it, so we only check it's greater than the lowest possible value
932 $this->assertGreaterThan(1, $content['exists']);
18696f77
JB
933 }
934
8f2038e5 935 public function testGetEntriesExistsWithoutReturnId()
18696f77 936 {
8f2038e5 937 $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2');
900c8448 938
f808b016 939 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
940
941 $content = json_decode($this->client->getResponse()->getContent(), true);
942
8f2038e5 943 $this->assertSame(true, $content['exists']);
900c8448
NL
944 }
945
946 public function testGetEntriesExistsWithManyUrls()
18696f77
JB
947 {
948 $url1 = 'http://0.0.0.0/entry2';
949 $url2 = 'http://0.0.0.0/entry10';
f808b016 950 $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1');
18696f77 951
f808b016 952 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
18696f77
JB
953
954 $content = json_decode($this->client->getResponse()->getContent(), true);
955
956 $this->assertArrayHasKey($url1, $content);
957 $this->assertArrayHasKey($url2, $content);
8f2038e5
JB
958 // it returns a database id, we don't know it, so we only check it's greater than the lowest possible value
959 $this->assertGreaterThan(1, $content[$url1]);
39ffaba3 960 $this->assertNull($content[$url2]);
18696f77
JB
961 }
962
963 public function testGetEntriesExistsWithManyUrlsReturnBool()
900c8448
NL
964 {
965 $url1 = 'http://0.0.0.0/entry2';
966 $url2 = 'http://0.0.0.0/entry10';
f808b016 967 $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2);
900c8448 968
f808b016 969 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
970
971 $content = json_decode($this->client->getResponse()->getContent(), true);
972
973 $this->assertArrayHasKey($url1, $content);
974 $this->assertArrayHasKey($url2, $content);
39ffaba3
JB
975 $this->assertTrue($content[$url1]);
976 $this->assertFalse($content[$url2]);
900c8448
NL
977 }
978
979 public function testGetEntriesExistsWhichDoesNotExists()
980 {
981 $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2');
982
f808b016 983 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
900c8448
NL
984
985 $content = json_decode($this->client->getResponse()->getContent(), true);
986
64a5a6cf 987 $this->assertFalse($content['exists']);
900c8448
NL
988 }
989
990 public function testGetEntriesExistsWithNoUrl()
991 {
992 $this->client->request('GET', '/api/entries/exists?url=');
993
f808b016 994 $this->assertSame(403, $this->client->getResponse()->getStatusCode());
900c8448 995 }
0a6f4568
JB
996
997 public function testReloadEntryErrorWhileFetching()
998 {
70584b42 999 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
0a6f4568 1000 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 1001 ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getUserId());
0a6f4568
JB
1002
1003 if (!$entry) {
1004 $this->markTestSkipped('No content found in db.');
1005 }
1006
f808b016
JB
1007 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json');
1008 $this->assertSame(304, $this->client->getResponse()->getStatusCode());
0a6f4568
JB
1009 }
1010
1011 public function testReloadEntry()
1012 {
1013 $this->client->request('POST', '/api/entries.json', [
77854331 1014 '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
1015 'archive' => '1',
1016 'tags' => 'google, apple',
1017 ]);
1018
1019 $json = json_decode($this->client->getResponse()->getContent(), true);
1020
1021 $this->setUp();
1022
f808b016
JB
1023 $this->client->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json');
1024 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
0a6f4568
JB
1025
1026 $content = json_decode($this->client->getResponse()->getContent(), true);
1027
1028 $this->assertNotEmpty($content['title']);
1029
f808b016 1030 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
0a6f4568 1031 }
d1fc5902
NL
1032
1033 public function testPostEntriesTagsListAction()
1034 {
80299ed2
NL
1035 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
1036 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 1037 ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getUserId());
80299ed2
NL
1038
1039 $tags = $entry->getTags();
1040
dcbebc17 1041 $this->assertCount(2, $tags);
80299ed2 1042
d1fc5902
NL
1043 $list = [
1044 [
dcbebc17 1045 'url' => 'http://0.0.0.0/entry4',
80299ed2 1046 'tags' => 'new tag 1, new tag 2',
d1fc5902 1047 ],
80299ed2
NL
1048 ];
1049
f808b016 1050 $this->client->request('POST', '/api/entries/tags/lists?list=' . json_encode($list));
80299ed2 1051
f808b016 1052 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
80299ed2
NL
1053
1054 $content = json_decode($this->client->getResponse()->getContent(), true);
1055
1056 $this->assertInternalType('int', $content[0]['entry']);
f808b016 1057 $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']);
80299ed2
NL
1058
1059 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
1060 ->getRepository('WallabagCoreBundle:Entry')
8f2038e5 1061 ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getUserId());
80299ed2
NL
1062
1063 $tags = $entry->getTags();
dcbebc17 1064 $this->assertCount(4, $tags);
80299ed2
NL
1065 }
1066
a05b6115
JB
1067 public function testPostEntriesTagsListActionNoList()
1068 {
c18a2476 1069 $this->client->request('POST', '/api/entries/tags/lists?list=' . json_encode([]));
a05b6115 1070
c18a2476 1071 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
a05b6115
JB
1072
1073 $content = json_decode($this->client->getResponse()->getContent(), true);
1074
1075 $this->assertEmpty($content);
1076 }
1077
80299ed2
NL
1078 public function testDeleteEntriesTagsListAction()
1079 {
7ab5eb95 1080 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
8f2038e5 1081 $entry = new Entry($em->getReference(User::class, $this->getUserId()));
7ab5eb95 1082 $entry->setUrl('http://0.0.0.0/test-entry');
1083 $entry->addTag((new Tag())->setLabel('foo-tag'));
1084 $entry->addTag((new Tag())->setLabel('bar-tag'));
1085 $em->persist($entry);
1086 $em->flush();
80299ed2 1087
7ab5eb95 1088 $em->clear();
80299ed2
NL
1089
1090 $list = [
d1fc5902 1091 [
7ab5eb95 1092 'url' => 'http://0.0.0.0/test-entry',
1093 'tags' => 'foo-tag, bar-tag',
d1fc5902
NL
1094 ],
1095 ];
1096
f808b016
JB
1097 $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list));
1098 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
7ab5eb95 1099
1100 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
1101 $this->assertCount(0, $entry->getTags());
1eca7831
NL
1102 }
1103
a05b6115
JB
1104 public function testDeleteEntriesTagsListActionNoList()
1105 {
c18a2476 1106 $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode([]));
a05b6115 1107
c18a2476 1108 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
a05b6115
JB
1109
1110 $content = json_decode($this->client->getResponse()->getContent(), true);
1111
1112 $this->assertEmpty($content);
1113 }
1114
a7abcc7b 1115 public function testPostEntriesListAction()
1eca7831
NL
1116 {
1117 $list = [
77854331 1118 'https://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
7fa844a3 1119 'http://0.0.0.0/entry2',
1eca7831
NL
1120 ];
1121
f808b016 1122 $this->client->request('POST', '/api/entries/lists?urls=' . json_encode($list));
d1fc5902 1123
f808b016 1124 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
d1fc5902
NL
1125
1126 $content = json_decode($this->client->getResponse()->getContent(), true);
1127
80299ed2 1128 $this->assertInternalType('int', $content[0]['entry']);
77854331 1129 $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 1130
1eca7831 1131 $this->assertInternalType('int', $content[1]['entry']);
f808b016 1132 $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']);
a7abcc7b 1133 }
d1fc5902 1134
a05b6115
JB
1135 public function testPostEntriesListActionWithNoUrls()
1136 {
c18a2476 1137 $this->client->request('POST', '/api/entries/lists?urls=' . json_encode([]));
a05b6115 1138
c18a2476 1139 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
a05b6115
JB
1140
1141 $content = json_decode($this->client->getResponse()->getContent(), true);
1142
1143 $this->assertEmpty($content);
1144 }
1145
a7abcc7b
NL
1146 public function testDeleteEntriesListAction()
1147 {
7ab5eb95 1148 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
8f2038e5 1149 $em->persist((new Entry($em->getReference(User::class, $this->getUserId())))->setUrl('http://0.0.0.0/test-entry1'));
7ab5eb95 1150
1151 $em->flush();
1152 $em->clear();
a7abcc7b 1153 $list = [
7ab5eb95 1154 'http://0.0.0.0/test-entry1',
1155 'http://0.0.0.0/test-entry-not-exist',
a7abcc7b
NL
1156 ];
1157
f808b016 1158 $this->client->request('DELETE', '/api/entries/list?urls=' . json_encode($list));
d1fc5902 1159
f808b016 1160 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
a7abcc7b
NL
1161
1162 $content = json_decode($this->client->getResponse()->getContent(), true);
1163
1164 $this->assertTrue($content[0]['entry']);
f808b016 1165 $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']);
1eca7831 1166
a7abcc7b 1167 $this->assertFalse($content[1]['entry']);
f808b016 1168 $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
d1fc5902 1169 }
efd351c9 1170
a05b6115
JB
1171 public function testDeleteEntriesListActionWithNoUrls()
1172 {
c18a2476 1173 $this->client->request('DELETE', '/api/entries/list?urls=' . json_encode([]));
a05b6115 1174
c18a2476 1175 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
a05b6115
JB
1176
1177 $content = json_decode($this->client->getResponse()->getContent(), true);
1178
1179 $this->assertEmpty($content);
1180 }
1181
efd351c9
NL
1182 public function testLimitBulkAction()
1183 {
1184 $list = [
1185 'http://0.0.0.0/entry1',
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 ];
1197
f808b016 1198 $this->client->request('POST', '/api/entries/lists?urls=' . json_encode($list));
72db15ca 1199
f808b016 1200 $this->assertSame(400, $this->client->getResponse()->getStatusCode());
72db15ca 1201 $this->assertContains('API limit reached', $this->client->getResponse()->getContent());
d1fc5902 1202 }
ff9f89fd
JB
1203
1204 public function testRePostEntryAndReUsePublishedAt()
1205 {
1206 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
8f2038e5 1207 $entry = new Entry($em->getReference(User::class, $this->getUserId()));
ff9f89fd
JB
1208 $entry->setTitle('Antoine de Caunes : « Je veux avoir le droit de tâtonner »');
1209 $entry->setContent('hihi');
77854331 1210 $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
1211 $entry->setPublishedAt(new \DateTime('2017-06-26T07:46:02+0200'));
1212 $em->persist($entry);
1213 $em->flush();
1214 $em->clear();
1215
1216 $this->client->request('POST', '/api/entries.json', [
77854331 1217 '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
1218 ]);
1219
1220 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1221
1222 $content = json_decode($this->client->getResponse()->getContent(), true);
1223
1224 $this->assertGreaterThan(0, $content['id']);
77854331 1225 $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 1226 }
900c8448 1227}