]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
Merge pull request #3309 from wallabag/fix-multiple-tag-search
[github/wallabag/wallabag.git] / tests / Wallabag / ApiBundle / Controller / EntryRestControllerTest.php
1 <?php
2
3 namespace Tests\Wallabag\ApiBundle\Controller;
4
5 use Tests\Wallabag\ApiBundle\WallabagApiTestCase;
6 use Wallabag\CoreBundle\Entity\Entry;
7 use Wallabag\CoreBundle\Entity\Tag;
8 use Wallabag\CoreBundle\Helper\ContentProxy;
9 use Wallabag\UserBundle\Entity\User;
10
11 class 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
24 $this->client->request('GET', '/api/entries/' . $entry->getId() . '.json');
25 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
26
27 $content = json_decode($this->client->getResponse()->getContent(), true);
28
29 $this->assertSame($entry->getTitle(), $content['title']);
30 $this->assertSame($entry->getUrl(), $content['url']);
31 $this->assertCount(count($entry->getTags()), $content['tags']);
32 $this->assertSame($entry->getUserName(), $content['user_name']);
33 $this->assertSame($entry->getUserEmail(), $content['user_email']);
34 $this->assertSame($entry->getUserId(), $content['user_id']);
35
36 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
37 }
38
39 public function testExportEntry()
40 {
41 $entry = $this->client->getContainer()
42 ->get('doctrine.orm.entity_manager')
43 ->getRepository('WallabagCoreBundle:Entry')
44 ->findOneBy(['user' => 1, 'isArchived' => false]);
45
46 if (!$entry) {
47 $this->markTestSkipped('No content found in db.');
48 }
49
50 $this->client->request('GET', '/api/entries/' . $entry->getId() . '/export.epub');
51 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
52
53 // epub format got the content type in the content
54 $this->assertContains('application/epub', $this->client->getResponse()->getContent());
55 $this->assertSame('application/epub+zip', $this->client->getResponse()->headers->get('Content-Type'));
56
57 // re-auth client for mobi
58 $client = $this->createAuthorizedClient();
59 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.mobi');
60 $this->assertSame(200, $client->getResponse()->getStatusCode());
61
62 $this->assertSame('application/x-mobipocket-ebook', $client->getResponse()->headers->get('Content-Type'));
63
64 // re-auth client for pdf
65 $client = $this->createAuthorizedClient();
66 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.pdf');
67 $this->assertSame(200, $client->getResponse()->getStatusCode());
68
69 $this->assertContains('PDF-', $client->getResponse()->getContent());
70 $this->assertSame('application/pdf', $client->getResponse()->headers->get('Content-Type'));
71
72 // re-auth client for pdf
73 $client = $this->createAuthorizedClient();
74 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.txt');
75 $this->assertSame(200, $client->getResponse()->getStatusCode());
76
77 $this->assertContains('text/plain', $client->getResponse()->headers->get('Content-Type'));
78
79 // re-auth client for pdf
80 $client = $this->createAuthorizedClient();
81 $client->request('GET', '/api/entries/' . $entry->getId() . '/export.csv');
82 $this->assertSame(200, $client->getResponse()->getStatusCode());
83
84 $this->assertContains('application/csv', $client->getResponse()->headers->get('Content-Type'));
85 }
86
87 public function testGetOneEntryWrongUser()
88 {
89 $entry = $this->client->getContainer()
90 ->get('doctrine.orm.entity_manager')
91 ->getRepository('WallabagCoreBundle:Entry')
92 ->findOneBy(['user' => 2, 'isArchived' => false]);
93
94 if (!$entry) {
95 $this->markTestSkipped('No content found in db.');
96 }
97
98 $this->client->request('GET', '/api/entries/' . $entry->getId() . '.json');
99
100 $this->assertSame(403, $this->client->getResponse()->getStatusCode());
101 }
102
103 public function testGetEntries()
104 {
105 $this->client->request('GET', '/api/entries');
106
107 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
108
109 $content = json_decode($this->client->getResponse()->getContent(), true);
110
111 $this->assertGreaterThanOrEqual(1, count($content));
112 $this->assertNotEmpty($content['_embedded']['items']);
113 $this->assertGreaterThanOrEqual(1, $content['total']);
114 $this->assertSame(1, $content['page']);
115 $this->assertGreaterThanOrEqual(1, $content['pages']);
116
117 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
118 }
119
120 public function testGetEntriesWithFullOptions()
121 {
122 $this->client->request('GET', '/api/entries', [
123 'archive' => 1,
124 'starred' => 1,
125 'sort' => 'updated',
126 'order' => 'asc',
127 'page' => 1,
128 'perPage' => 2,
129 'tags' => 'foo',
130 'since' => 1443274283,
131 'public' => 0,
132 ]);
133
134 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
135
136 $content = json_decode($this->client->getResponse()->getContent(), true);
137
138 $this->assertGreaterThanOrEqual(1, count($content));
139 $this->assertArrayHasKey('items', $content['_embedded']);
140 $this->assertGreaterThanOrEqual(0, $content['total']);
141 $this->assertSame(1, $content['page']);
142 $this->assertSame(2, $content['limit']);
143 $this->assertGreaterThanOrEqual(1, $content['pages']);
144
145 $this->assertArrayHasKey('_links', $content);
146 $this->assertArrayHasKey('self', $content['_links']);
147 $this->assertArrayHasKey('first', $content['_links']);
148 $this->assertArrayHasKey('last', $content['_links']);
149
150 foreach (['self', 'first', 'last'] as $link) {
151 $this->assertArrayHasKey('href', $content['_links'][$link]);
152 $this->assertContains('archive=1', $content['_links'][$link]['href']);
153 $this->assertContains('starred=1', $content['_links'][$link]['href']);
154 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
155 $this->assertContains('order=asc', $content['_links'][$link]['href']);
156 $this->assertContains('tags=foo', $content['_links'][$link]['href']);
157 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
158 $this->assertContains('public=0', $content['_links'][$link]['href']);
159 }
160
161 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
162 }
163
164 public function testGetEntriesPublicOnly()
165 {
166 $entry = $this->client->getContainer()
167 ->get('doctrine.orm.entity_manager')
168 ->getRepository('WallabagCoreBundle:Entry')
169 ->findOneByUser(1);
170
171 if (!$entry) {
172 $this->markTestSkipped('No content found in db.');
173 }
174
175 // generate at least one public entry
176 $entry->generateUid();
177
178 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
179 $em->persist($entry);
180 $em->flush();
181
182 $this->client->request('GET', '/api/entries', [
183 'public' => 1,
184 ]);
185
186 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
187
188 $content = json_decode($this->client->getResponse()->getContent(), true);
189
190 $this->assertGreaterThanOrEqual(1, count($content));
191 $this->assertArrayHasKey('items', $content['_embedded']);
192 $this->assertGreaterThanOrEqual(1, $content['total']);
193 $this->assertSame(1, $content['page']);
194 $this->assertSame(30, $content['limit']);
195 $this->assertGreaterThanOrEqual(1, $content['pages']);
196
197 $this->assertArrayHasKey('_links', $content);
198 $this->assertArrayHasKey('self', $content['_links']);
199 $this->assertArrayHasKey('first', $content['_links']);
200 $this->assertArrayHasKey('last', $content['_links']);
201
202 foreach (['self', 'first', 'last'] as $link) {
203 $this->assertArrayHasKey('href', $content['_links'][$link]);
204 $this->assertContains('public=1', $content['_links'][$link]['href']);
205 }
206
207 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
208 }
209
210 public function testGetEntriesOnPageTwo()
211 {
212 $this->client->request('GET', '/api/entries', [
213 'page' => 2,
214 'perPage' => 2,
215 ]);
216
217 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
218
219 $content = json_decode($this->client->getResponse()->getContent(), true);
220
221 $this->assertGreaterThanOrEqual(0, $content['total']);
222 $this->assertSame(2, $content['page']);
223 $this->assertSame(2, $content['limit']);
224 }
225
226 public function testGetStarredEntries()
227 {
228 $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']);
229
230 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
231
232 $content = json_decode($this->client->getResponse()->getContent(), true);
233
234 $this->assertGreaterThanOrEqual(1, count($content));
235 $this->assertNotEmpty($content['_embedded']['items']);
236 $this->assertGreaterThanOrEqual(1, $content['total']);
237 $this->assertSame(1, $content['page']);
238 $this->assertGreaterThanOrEqual(1, $content['pages']);
239
240 $this->assertArrayHasKey('_links', $content);
241 $this->assertArrayHasKey('self', $content['_links']);
242 $this->assertArrayHasKey('first', $content['_links']);
243 $this->assertArrayHasKey('last', $content['_links']);
244
245 foreach (['self', 'first', 'last'] as $link) {
246 $this->assertArrayHasKey('href', $content['_links'][$link]);
247 $this->assertContains('starred=1', $content['_links'][$link]['href']);
248 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
249 }
250
251 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
252 }
253
254 public function testGetArchiveEntries()
255 {
256 $this->client->request('GET', '/api/entries', ['archive' => 1]);
257
258 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
259
260 $content = json_decode($this->client->getResponse()->getContent(), true);
261
262 $this->assertGreaterThanOrEqual(1, count($content));
263 $this->assertNotEmpty($content['_embedded']['items']);
264 $this->assertGreaterThanOrEqual(1, $content['total']);
265 $this->assertSame(1, $content['page']);
266 $this->assertGreaterThanOrEqual(1, $content['pages']);
267
268 $this->assertArrayHasKey('_links', $content);
269 $this->assertArrayHasKey('self', $content['_links']);
270 $this->assertArrayHasKey('first', $content['_links']);
271 $this->assertArrayHasKey('last', $content['_links']);
272
273 foreach (['self', 'first', 'last'] as $link) {
274 $this->assertArrayHasKey('href', $content['_links'][$link]);
275 $this->assertContains('archive=1', $content['_links'][$link]['href']);
276 }
277
278 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
279 }
280
281 public function testGetTaggedEntries()
282 {
283 $this->client->request('GET', '/api/entries', ['tags' => 'foo,bar']);
284
285 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
286
287 $content = json_decode($this->client->getResponse()->getContent(), true);
288
289 $this->assertGreaterThanOrEqual(1, count($content));
290 $this->assertNotEmpty($content['_embedded']['items']);
291 $this->assertGreaterThanOrEqual(1, $content['total']);
292 $this->assertSame(1, $content['page']);
293 $this->assertGreaterThanOrEqual(1, $content['pages']);
294
295 $this->assertContains('foo', array_column($content['_embedded']['items'][0]['tags'], 'label'), 'Entries tags should have "foo" tag');
296 $this->assertContains('bar', array_column($content['_embedded']['items'][0]['tags'], 'label'), 'Entries tags should have "bar" tag');
297
298 $this->assertArrayHasKey('_links', $content);
299 $this->assertArrayHasKey('self', $content['_links']);
300 $this->assertArrayHasKey('first', $content['_links']);
301 $this->assertArrayHasKey('last', $content['_links']);
302
303 foreach (['self', 'first', 'last'] as $link) {
304 $this->assertArrayHasKey('href', $content['_links'][$link]);
305 $this->assertContains('tags=' . urlencode('foo,bar'), $content['_links'][$link]['href']);
306 }
307
308 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
309 }
310
311 public function testGetDatedEntries()
312 {
313 $this->client->request('GET', '/api/entries', ['since' => 1443274283]);
314
315 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
316
317 $content = json_decode($this->client->getResponse()->getContent(), true);
318
319 $this->assertGreaterThanOrEqual(1, count($content));
320 $this->assertNotEmpty($content['_embedded']['items']);
321 $this->assertGreaterThanOrEqual(1, $content['total']);
322 $this->assertSame(1, $content['page']);
323 $this->assertGreaterThanOrEqual(1, $content['pages']);
324
325 $this->assertArrayHasKey('_links', $content);
326 $this->assertArrayHasKey('self', $content['_links']);
327 $this->assertArrayHasKey('first', $content['_links']);
328 $this->assertArrayHasKey('last', $content['_links']);
329
330 foreach (['self', 'first', 'last'] as $link) {
331 $this->assertArrayHasKey('href', $content['_links'][$link]);
332 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
333 }
334
335 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
336 }
337
338 public function testGetDatedSupEntries()
339 {
340 $future = new \DateTime(date('Y-m-d H:i:s'));
341 $this->client->request('GET', '/api/entries', ['since' => $future->getTimestamp() + 1000]);
342
343 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
344
345 $content = json_decode($this->client->getResponse()->getContent(), true);
346
347 $this->assertGreaterThanOrEqual(1, count($content));
348 $this->assertEmpty($content['_embedded']['items']);
349 $this->assertSame(0, $content['total']);
350 $this->assertSame(1, $content['page']);
351 $this->assertSame(1, $content['pages']);
352
353 $this->assertArrayHasKey('_links', $content);
354 $this->assertArrayHasKey('self', $content['_links']);
355 $this->assertArrayHasKey('first', $content['_links']);
356 $this->assertArrayHasKey('last', $content['_links']);
357
358 foreach (['self', 'first', 'last'] as $link) {
359 $this->assertArrayHasKey('href', $content['_links'][$link]);
360 $this->assertContains('since=' . ($future->getTimestamp() + 1000), $content['_links'][$link]['href']);
361 }
362
363 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
364 }
365
366 public function testDeleteEntry()
367 {
368 $entry = $this->client->getContainer()
369 ->get('doctrine.orm.entity_manager')
370 ->getRepository('WallabagCoreBundle:Entry')
371 ->findOneByUser(1, ['id' => 'asc']);
372
373 if (!$entry) {
374 $this->markTestSkipped('No content found in db.');
375 }
376
377 $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '.json');
378
379 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
380
381 $content = json_decode($this->client->getResponse()->getContent(), true);
382
383 $this->assertSame($entry->getTitle(), $content['title']);
384 $this->assertSame($entry->getUrl(), $content['url']);
385
386 // We'll try to delete this entry again
387 $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '.json');
388
389 $this->assertSame(404, $this->client->getResponse()->getStatusCode());
390 }
391
392 public function testPostEntry()
393 {
394 $this->client->request('POST', '/api/entries.json', [
395 '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',
396 'tags' => 'google',
397 'title' => 'New title for my article',
398 'content' => 'my content',
399 'language' => 'de',
400 'published_at' => '2016-09-08T11:55:58+0200',
401 'authors' => 'bob,helen',
402 'public' => 1,
403 ]);
404
405 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
406
407 $content = json_decode($this->client->getResponse()->getContent(), true);
408
409 $this->assertGreaterThan(0, $content['id']);
410 $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']);
411 $this->assertSame(0, $content['is_archived']);
412 $this->assertSame(0, $content['is_starred']);
413 $this->assertNull($content['starred_at']);
414 $this->assertSame('New title for my article', $content['title']);
415 $this->assertSame(1, $content['user_id']);
416 $this->assertCount(2, $content['tags']);
417 $this->assertSame('my content', $content['content']);
418 $this->assertSame('de', $content['language']);
419 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
420 $this->assertCount(2, $content['published_by']);
421 $this->assertContains('bob', $content['published_by']);
422 $this->assertContains('helen', $content['published_by']);
423 $this->assertTrue($content['is_public'], 'A public link has been generated for that entry');
424 }
425
426 public function testPostSameEntry()
427 {
428 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
429 $entry = new Entry($em->getReference(User::class, 1));
430 $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');
431 $entry->setArchived(true);
432 $entry->addTag((new Tag())->setLabel('google'));
433 $entry->addTag((new Tag())->setLabel('apple'));
434 $em->persist($entry);
435 $em->flush();
436 $em->clear();
437
438 $this->client->request('POST', '/api/entries.json', [
439 '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',
440 'archive' => '1',
441 'tags' => 'google, apple',
442 ]);
443
444 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
445
446 $content = json_decode($this->client->getResponse()->getContent(), true);
447
448 $this->assertGreaterThan(0, $content['id']);
449 $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']);
450 $this->assertSame(1, $content['is_archived']);
451 $this->assertSame(0, $content['is_starred']);
452 $this->assertCount(3, $content['tags']);
453 }
454
455 public function testPostEntryWhenFetchContentFails()
456 {
457 /** @var \Symfony\Component\DependencyInjection\Container $container */
458 $container = $this->client->getContainer();
459 $contentProxy = $this->getMockBuilder(ContentProxy::class)
460 ->disableOriginalConstructor()
461 ->setMethods(['updateEntry'])
462 ->getMock();
463 $contentProxy->expects($this->any())
464 ->method('updateEntry')
465 ->willThrowException(new \Exception('Test Fetch content fails'));
466 $container->set('wallabag_core.content_proxy', $contentProxy);
467
468 try {
469 $this->client->request('POST', '/api/entries.json', [
470 'url' => 'http://www.example.com/',
471 ]);
472
473 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
474 $content = json_decode($this->client->getResponse()->getContent(), true);
475 $this->assertGreaterThan(0, $content['id']);
476 $this->assertSame('http://www.example.com/', $content['url']);
477 } finally {
478 // Remove the created entry to avoid side effects on other tests
479 if (isset($content['id'])) {
480 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
481 $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']);
482 $em->remove($entry);
483 $em->flush();
484 }
485 }
486 }
487
488 public function testPostArchivedAndStarredEntry()
489 {
490 $now = new \DateTime();
491 $this->client->request('POST', '/api/entries.json', [
492 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
493 'archive' => '1',
494 'starred' => '1',
495 ]);
496
497 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
498
499 $content = json_decode($this->client->getResponse()->getContent(), true);
500
501 $this->assertGreaterThan(0, $content['id']);
502 $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']);
503 $this->assertSame(1, $content['is_archived']);
504 $this->assertSame(1, $content['is_starred']);
505 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['starred_at']))->getTimestamp());
506 $this->assertSame(1, $content['user_id']);
507 }
508
509 public function testPostArchivedAndStarredEntryWithoutQuotes()
510 {
511 $this->client->request('POST', '/api/entries.json', [
512 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
513 'archive' => 0,
514 'starred' => 1,
515 ]);
516
517 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
518
519 $content = json_decode($this->client->getResponse()->getContent(), true);
520
521 $this->assertGreaterThan(0, $content['id']);
522 $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']);
523 $this->assertSame(0, $content['is_archived']);
524 $this->assertSame(1, $content['is_starred']);
525 }
526
527 public function testPatchEntry()
528 {
529 $entry = $this->client->getContainer()
530 ->get('doctrine.orm.entity_manager')
531 ->getRepository('WallabagCoreBundle:Entry')
532 ->findOneByUser(1);
533
534 if (!$entry) {
535 $this->markTestSkipped('No content found in db.');
536 }
537
538 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
539 'title' => 'New awesome title',
540 'tags' => 'new tag ' . uniqid(),
541 'starred' => '1',
542 'archive' => '0',
543 'language' => 'de_AT',
544 'preview_picture' => 'http://preview.io/picture.jpg',
545 'authors' => 'bob,sponge',
546 'content' => 'awesome',
547 'public' => 0,
548 'published_at' => 1488833381,
549 ]);
550
551 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
552
553 $content = json_decode($this->client->getResponse()->getContent(), true);
554
555 $this->assertSame($entry->getId(), $content['id']);
556 $this->assertSame($entry->getUrl(), $content['url']);
557 $this->assertSame('New awesome title', $content['title']);
558 $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag');
559 $this->assertSame(1, $content['user_id']);
560 $this->assertSame('de_AT', $content['language']);
561 $this->assertSame('http://preview.io/picture.jpg', $content['preview_picture']);
562 $this->assertContains('sponge', $content['published_by']);
563 $this->assertContains('bob', $content['published_by']);
564 $this->assertSame('awesome', $content['content']);
565 $this->assertFalse($content['is_public'], 'Entry is no more shared');
566 $this->assertContains('2017-03-06', $content['published_at']);
567 }
568
569 public function testPatchEntryWithoutQuotes()
570 {
571 $entry = $this->client->getContainer()
572 ->get('doctrine.orm.entity_manager')
573 ->getRepository('WallabagCoreBundle:Entry')
574 ->findOneByUser(1);
575
576 if (!$entry) {
577 $this->markTestSkipped('No content found in db.');
578 }
579
580 $previousContent = $entry->getContent();
581 $previousLanguage = $entry->getLanguage();
582
583 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
584 'title' => 'New awesome title',
585 'tags' => 'new tag ' . uniqid(),
586 'starred' => 1,
587 'archive' => 0,
588 'authors' => ['bob', 'sponge'],
589 ]);
590
591 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
592
593 $content = json_decode($this->client->getResponse()->getContent(), true);
594
595 $this->assertSame($entry->getId(), $content['id']);
596 $this->assertSame($entry->getUrl(), $content['url']);
597 $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag');
598 $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string');
599 $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved');
600 $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved');
601 }
602
603 public function testGetTagsEntry()
604 {
605 $entry = $this->client->getContainer()
606 ->get('doctrine.orm.entity_manager')
607 ->getRepository('WallabagCoreBundle:Entry')
608 ->findOneWithTags($this->user->getId());
609
610 $entry = $entry[0];
611
612 if (!$entry) {
613 $this->markTestSkipped('No content found in db.');
614 }
615
616 $tags = [];
617 foreach ($entry->getTags() as $tag) {
618 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
619 }
620
621 $this->client->request('GET', '/api/entries/' . $entry->getId() . '/tags');
622
623 $this->assertSame(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent());
624 }
625
626 public function testPostTagsOnEntry()
627 {
628 $entry = $this->client->getContainer()
629 ->get('doctrine.orm.entity_manager')
630 ->getRepository('WallabagCoreBundle:Entry')
631 ->findOneByUser(1);
632
633 if (!$entry) {
634 $this->markTestSkipped('No content found in db.');
635 }
636
637 $nbTags = count($entry->getTags());
638
639 $newTags = 'tag1,tag2,tag3';
640
641 $this->client->request('POST', '/api/entries/' . $entry->getId() . '/tags', ['tags' => $newTags]);
642
643 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
644
645 $content = json_decode($this->client->getResponse()->getContent(), true);
646
647 $this->assertArrayHasKey('tags', $content);
648 $this->assertSame($nbTags + 3, count($content['tags']));
649
650 $entryDB = $this->client->getContainer()
651 ->get('doctrine.orm.entity_manager')
652 ->getRepository('WallabagCoreBundle:Entry')
653 ->find($entry->getId());
654
655 $tagsInDB = [];
656 foreach ($entryDB->getTags()->toArray() as $tag) {
657 $tagsInDB[$tag->getId()] = $tag->getLabel();
658 }
659
660 foreach (explode(',', $newTags) as $tag) {
661 $this->assertContains($tag, $tagsInDB);
662 }
663 }
664
665 public function testDeleteOneTagEntry()
666 {
667 $entry = $this->client->getContainer()
668 ->get('doctrine.orm.entity_manager')
669 ->getRepository('WallabagCoreBundle:Entry')
670 ->findOneWithTags($this->user->getId());
671 $entry = $entry[0];
672
673 if (!$entry) {
674 $this->markTestSkipped('No content found in db.');
675 }
676
677 // hydrate the tags relations
678 $nbTags = count($entry->getTags());
679 $tag = $entry->getTags()[0];
680
681 $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '/tags/' . $tag->getId() . '.json');
682
683 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
684
685 $content = json_decode($this->client->getResponse()->getContent(), true);
686
687 $this->assertArrayHasKey('tags', $content);
688 $this->assertSame($nbTags - 1, count($content['tags']));
689 }
690
691 public function testSaveIsArchivedAfterPost()
692 {
693 $entry = $this->client->getContainer()
694 ->get('doctrine.orm.entity_manager')
695 ->getRepository('WallabagCoreBundle:Entry')
696 ->findOneBy(['user' => 1, 'isArchived' => true]);
697
698 if (!$entry) {
699 $this->markTestSkipped('No content found in db.');
700 }
701
702 $this->client->request('POST', '/api/entries.json', [
703 'url' => $entry->getUrl(),
704 ]);
705
706 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
707
708 $content = json_decode($this->client->getResponse()->getContent(), true);
709
710 $this->assertSame(1, $content['is_archived']);
711 }
712
713 public function testSaveIsStarredAfterPost()
714 {
715 $entry = $this->client->getContainer()
716 ->get('doctrine.orm.entity_manager')
717 ->getRepository('WallabagCoreBundle:Entry')
718 ->findOneBy(['user' => 1, 'isStarred' => true]);
719
720 if (!$entry) {
721 $this->markTestSkipped('No content found in db.');
722 }
723
724 $this->client->request('POST', '/api/entries.json', [
725 'url' => $entry->getUrl(),
726 ]);
727
728 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
729
730 $content = json_decode($this->client->getResponse()->getContent(), true);
731
732 $this->assertSame(1, $content['is_starred']);
733 }
734
735 public function testSaveIsArchivedAfterPatch()
736 {
737 $entry = $this->client->getContainer()
738 ->get('doctrine.orm.entity_manager')
739 ->getRepository('WallabagCoreBundle:Entry')
740 ->findOneBy(['user' => 1, 'isArchived' => true]);
741
742 if (!$entry) {
743 $this->markTestSkipped('No content found in db.');
744 }
745
746 $previousTitle = $entry->getTitle();
747
748 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
749 'title' => $entry->getTitle() . '++',
750 ]);
751
752 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
753
754 $content = json_decode($this->client->getResponse()->getContent(), true);
755
756 $this->assertSame(1, $content['is_archived']);
757 $this->assertSame($previousTitle . '++', $content['title']);
758 }
759
760 public function testSaveIsStarredAfterPatch()
761 {
762 $now = new \DateTime();
763 $entry = $this->client->getContainer()
764 ->get('doctrine.orm.entity_manager')
765 ->getRepository('WallabagCoreBundle:Entry')
766 ->findOneBy(['user' => 1, 'isStarred' => true]);
767
768 if (!$entry) {
769 $this->markTestSkipped('No content found in db.');
770 }
771 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [
772 'title' => $entry->getTitle() . '++',
773 ]);
774
775 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
776
777 $content = json_decode($this->client->getResponse()->getContent(), true);
778
779 $this->assertSame(1, $content['is_starred']);
780 $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['starred_at']))->getTimestamp());
781 }
782
783 public function dataForEntriesExistWithUrl()
784 {
785 return [
786 'with_id' => [
787 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1',
788 'expectedValue' => 2,
789 ],
790 'without_id' => [
791 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2',
792 'expectedValue' => true,
793 ],
794 ];
795 }
796
797 /**
798 * @dataProvider dataForEntriesExistWithUrl
799 */
800 public function testGetEntriesExists($url, $expectedValue)
801 {
802 $this->client->request('GET', $url);
803
804 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
805
806 $content = json_decode($this->client->getResponse()->getContent(), true);
807
808 $this->assertSame($expectedValue, $content['exists']);
809 }
810
811 public function testGetEntriesExistsWithManyUrls()
812 {
813 $url1 = 'http://0.0.0.0/entry2';
814 $url2 = 'http://0.0.0.0/entry10';
815 $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1');
816
817 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
818
819 $content = json_decode($this->client->getResponse()->getContent(), true);
820
821 $this->assertArrayHasKey($url1, $content);
822 $this->assertArrayHasKey($url2, $content);
823 $this->assertSame(2, $content[$url1]);
824 $this->assertNull($content[$url2]);
825 }
826
827 public function testGetEntriesExistsWithManyUrlsReturnBool()
828 {
829 $url1 = 'http://0.0.0.0/entry2';
830 $url2 = 'http://0.0.0.0/entry10';
831 $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2);
832
833 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
834
835 $content = json_decode($this->client->getResponse()->getContent(), true);
836
837 $this->assertArrayHasKey($url1, $content);
838 $this->assertArrayHasKey($url2, $content);
839 $this->assertTrue($content[$url1]);
840 $this->assertFalse($content[$url2]);
841 }
842
843 public function testGetEntriesExistsWhichDoesNotExists()
844 {
845 $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2');
846
847 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
848
849 $content = json_decode($this->client->getResponse()->getContent(), true);
850
851 $this->assertSame(false, $content['exists']);
852 }
853
854 public function testGetEntriesExistsWithNoUrl()
855 {
856 $this->client->request('GET', '/api/entries/exists?url=');
857
858 $this->assertSame(403, $this->client->getResponse()->getStatusCode());
859 }
860
861 public function testReloadEntryErrorWhileFetching()
862 {
863 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
864 ->getRepository('WallabagCoreBundle:Entry')
865 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
866
867 if (!$entry) {
868 $this->markTestSkipped('No content found in db.');
869 }
870
871 $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json');
872 $this->assertSame(304, $this->client->getResponse()->getStatusCode());
873 }
874
875 public function testReloadEntry()
876 {
877 $this->client->request('POST', '/api/entries.json', [
878 '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',
879 'archive' => '1',
880 'tags' => 'google, apple',
881 ]);
882
883 $json = json_decode($this->client->getResponse()->getContent(), true);
884
885 $this->setUp();
886
887 $this->client->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json');
888 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
889
890 $content = json_decode($this->client->getResponse()->getContent(), true);
891
892 $this->assertNotEmpty($content['title']);
893
894 $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
895 }
896
897 public function testPostEntriesTagsListAction()
898 {
899 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
900 ->getRepository('WallabagCoreBundle:Entry')
901 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
902
903 $tags = $entry->getTags();
904
905 $this->assertCount(2, $tags);
906
907 $list = [
908 [
909 'url' => 'http://0.0.0.0/entry4',
910 'tags' => 'new tag 1, new tag 2',
911 ],
912 ];
913
914 $this->client->request('POST', '/api/entries/tags/lists?list=' . json_encode($list));
915
916 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
917
918 $content = json_decode($this->client->getResponse()->getContent(), true);
919
920 $this->assertInternalType('int', $content[0]['entry']);
921 $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']);
922
923 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
924 ->getRepository('WallabagCoreBundle:Entry')
925 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
926
927 $tags = $entry->getTags();
928 $this->assertCount(4, $tags);
929 }
930
931 public function testPostEntriesTagsListActionNoList()
932 {
933 $this->client->request('POST', '/api/entries/tags/lists?list=' . json_encode([]));
934
935 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
936
937 $content = json_decode($this->client->getResponse()->getContent(), true);
938
939 $this->assertEmpty($content);
940 }
941
942 public function testDeleteEntriesTagsListAction()
943 {
944 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
945 $entry = new Entry($em->getReference(User::class, 1));
946 $entry->setUrl('http://0.0.0.0/test-entry');
947 $entry->addTag((new Tag())->setLabel('foo-tag'));
948 $entry->addTag((new Tag())->setLabel('bar-tag'));
949 $em->persist($entry);
950 $em->flush();
951
952 $em->clear();
953
954 $list = [
955 [
956 'url' => 'http://0.0.0.0/test-entry',
957 'tags' => 'foo-tag, bar-tag',
958 ],
959 ];
960
961 $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list));
962 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
963
964 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
965 $this->assertCount(0, $entry->getTags());
966 }
967
968 public function testDeleteEntriesTagsListActionNoList()
969 {
970 $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode([]));
971
972 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
973
974 $content = json_decode($this->client->getResponse()->getContent(), true);
975
976 $this->assertEmpty($content);
977 }
978
979 public function testPostEntriesListAction()
980 {
981 $list = [
982 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
983 'http://0.0.0.0/entry2',
984 ];
985
986 $this->client->request('POST', '/api/entries/lists?urls=' . json_encode($list));
987
988 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
989
990 $content = json_decode($this->client->getResponse()->getContent(), true);
991
992 $this->assertInternalType('int', $content[0]['entry']);
993 $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']);
994
995 $this->assertInternalType('int', $content[1]['entry']);
996 $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']);
997 }
998
999 public function testPostEntriesListActionWithNoUrls()
1000 {
1001 $this->client->request('POST', '/api/entries/lists?urls=' . json_encode([]));
1002
1003 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1004
1005 $content = json_decode($this->client->getResponse()->getContent(), true);
1006
1007 $this->assertEmpty($content);
1008 }
1009
1010 public function testDeleteEntriesListAction()
1011 {
1012 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
1013 $em->persist((new Entry($em->getReference(User::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
1014
1015 $em->flush();
1016 $em->clear();
1017 $list = [
1018 'http://0.0.0.0/test-entry1',
1019 'http://0.0.0.0/test-entry-not-exist',
1020 ];
1021
1022 $this->client->request('DELETE', '/api/entries/list?urls=' . json_encode($list));
1023
1024 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1025
1026 $content = json_decode($this->client->getResponse()->getContent(), true);
1027
1028 $this->assertTrue($content[0]['entry']);
1029 $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']);
1030
1031 $this->assertFalse($content[1]['entry']);
1032 $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
1033 }
1034
1035 public function testDeleteEntriesListActionWithNoUrls()
1036 {
1037 $this->client->request('DELETE', '/api/entries/list?urls=' . json_encode([]));
1038
1039 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1040
1041 $content = json_decode($this->client->getResponse()->getContent(), true);
1042
1043 $this->assertEmpty($content);
1044 }
1045
1046 public function testLimitBulkAction()
1047 {
1048 $list = [
1049 'http://0.0.0.0/entry1',
1050 'http://0.0.0.0/entry1',
1051 'http://0.0.0.0/entry1',
1052 'http://0.0.0.0/entry1',
1053 'http://0.0.0.0/entry1',
1054 'http://0.0.0.0/entry1',
1055 'http://0.0.0.0/entry1',
1056 'http://0.0.0.0/entry1',
1057 'http://0.0.0.0/entry1',
1058 'http://0.0.0.0/entry1',
1059 'http://0.0.0.0/entry1',
1060 ];
1061
1062 $this->client->request('POST', '/api/entries/lists?urls=' . json_encode($list));
1063
1064 $this->assertSame(400, $this->client->getResponse()->getStatusCode());
1065 $this->assertContains('API limit reached', $this->client->getResponse()->getContent());
1066 }
1067
1068 public function testRePostEntryAndReUsePublishedAt()
1069 {
1070 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
1071 $entry = new Entry($em->getReference(User::class, 1));
1072 $entry->setTitle('Antoine de Caunes : « Je veux avoir le droit de tâtonner »');
1073 $entry->setContent('hihi');
1074 $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');
1075 $entry->setPublishedAt(new \DateTime('2017-06-26T07:46:02+0200'));
1076 $em->persist($entry);
1077 $em->flush();
1078 $em->clear();
1079
1080 $this->client->request('POST', '/api/entries.json', [
1081 'url' => 'http://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html',
1082 ]);
1083
1084 $this->assertSame(200, $this->client->getResponse()->getStatusCode());
1085
1086 $content = json_decode($this->client->getResponse()->getContent(), true);
1087
1088 $this->assertGreaterThan(0, $content['id']);
1089 $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']);
1090 }
1091 }