]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
Merge pull request #3077 from wallabag/add-tags-list-view
[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;
6use Wallabag\CoreBundle\Entity\Tag;
08f29ae7 7use Wallabag\CoreBundle\Helper\ContentProxy;
900c8448
NL
8
9class EntryRestControllerTest extends WallabagApiTestCase
10{
11 public function testGetOneEntry()
12 {
13 $entry = $this->client->getContainer()
14 ->get('doctrine.orm.entity_manager')
15 ->getRepository('WallabagCoreBundle:Entry')
16 ->findOneBy(['user' => 1, 'isArchived' => false]);
17
18 if (!$entry) {
19 $this->markTestSkipped('No content found in db.');
20 }
21
22 $this->client->request('GET', '/api/entries/'.$entry->getId().'.json');
23 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
24
25 $content = json_decode($this->client->getResponse()->getContent(), true);
26
27 $this->assertEquals($entry->getTitle(), $content['title']);
28 $this->assertEquals($entry->getUrl(), $content['url']);
29 $this->assertCount(count($entry->getTags()), $content['tags']);
30 $this->assertEquals($entry->getUserName(), $content['user_name']);
31 $this->assertEquals($entry->getUserEmail(), $content['user_email']);
32 $this->assertEquals($entry->getUserId(), $content['user_id']);
33
5a619812
JB
34 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
35 }
36
37 public function testExportEntry()
38 {
39 $entry = $this->client->getContainer()
40 ->get('doctrine.orm.entity_manager')
41 ->getRepository('WallabagCoreBundle:Entry')
42 ->findOneBy(['user' => 1, 'isArchived' => false]);
43
44 if (!$entry) {
45 $this->markTestSkipped('No content found in db.');
46 }
47
48 $this->client->request('GET', '/api/entries/'.$entry->getId().'/export.epub');
49 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
50
51 // epub format got the content type in the content
52 $this->assertContains('application/epub', $this->client->getResponse()->getContent());
53 $this->assertEquals('application/epub+zip', $this->client->getResponse()->headers->get('Content-Type'));
54
55 // re-auth client for mobi
56 $client = $this->createAuthorizedClient();
57 $client->request('GET', '/api/entries/'.$entry->getId().'/export.mobi');
58 $this->assertEquals(200, $client->getResponse()->getStatusCode());
59
60 $this->assertEquals('application/x-mobipocket-ebook', $client->getResponse()->headers->get('Content-Type'));
61
62 // re-auth client for pdf
63 $client = $this->createAuthorizedClient();
64 $client->request('GET', '/api/entries/'.$entry->getId().'/export.pdf');
65 $this->assertEquals(200, $client->getResponse()->getStatusCode());
66
67 $this->assertContains('PDF-', $client->getResponse()->getContent());
68 $this->assertEquals('application/pdf', $client->getResponse()->headers->get('Content-Type'));
69
70 // re-auth client for pdf
71 $client = $this->createAuthorizedClient();
72 $client->request('GET', '/api/entries/'.$entry->getId().'/export.txt');
73 $this->assertEquals(200, $client->getResponse()->getStatusCode());
74
75 $this->assertContains('text/plain', $client->getResponse()->headers->get('Content-Type'));
76
77 // re-auth client for pdf
78 $client = $this->createAuthorizedClient();
79 $client->request('GET', '/api/entries/'.$entry->getId().'/export.csv');
80 $this->assertEquals(200, $client->getResponse()->getStatusCode());
81
82 $this->assertContains('application/csv', $client->getResponse()->headers->get('Content-Type'));
900c8448
NL
83 }
84
85 public function testGetOneEntryWrongUser()
86 {
87 $entry = $this->client->getContainer()
88 ->get('doctrine.orm.entity_manager')
89 ->getRepository('WallabagCoreBundle:Entry')
90 ->findOneBy(['user' => 2, 'isArchived' => false]);
91
92 if (!$entry) {
93 $this->markTestSkipped('No content found in db.');
94 }
95
96 $this->client->request('GET', '/api/entries/'.$entry->getId().'.json');
97
98 $this->assertEquals(403, $this->client->getResponse()->getStatusCode());
99 }
100
101 public function testGetEntries()
102 {
103 $this->client->request('GET', '/api/entries');
104
105 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
106
107 $content = json_decode($this->client->getResponse()->getContent(), true);
108
109 $this->assertGreaterThanOrEqual(1, count($content));
110 $this->assertNotEmpty($content['_embedded']['items']);
111 $this->assertGreaterThanOrEqual(1, $content['total']);
112 $this->assertEquals(1, $content['page']);
113 $this->assertGreaterThanOrEqual(1, $content['pages']);
114
5a619812 115 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
116 }
117
118 public function testGetEntriesWithFullOptions()
119 {
120 $this->client->request('GET', '/api/entries', [
121 'archive' => 1,
122 'starred' => 1,
123 'sort' => 'updated',
124 'order' => 'asc',
125 'page' => 1,
126 'perPage' => 2,
127 'tags' => 'foo',
128 'since' => 1443274283,
129 ]);
130
131 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
132
133 $content = json_decode($this->client->getResponse()->getContent(), true);
134
135 $this->assertGreaterThanOrEqual(1, count($content));
136 $this->assertArrayHasKey('items', $content['_embedded']);
137 $this->assertGreaterThanOrEqual(0, $content['total']);
138 $this->assertEquals(1, $content['page']);
139 $this->assertEquals(2, $content['limit']);
140 $this->assertGreaterThanOrEqual(1, $content['pages']);
141
142 $this->assertArrayHasKey('_links', $content);
143 $this->assertArrayHasKey('self', $content['_links']);
144 $this->assertArrayHasKey('first', $content['_links']);
145 $this->assertArrayHasKey('last', $content['_links']);
146
147 foreach (['self', 'first', 'last'] as $link) {
148 $this->assertArrayHasKey('href', $content['_links'][$link]);
149 $this->assertContains('archive=1', $content['_links'][$link]['href']);
150 $this->assertContains('starred=1', $content['_links'][$link]['href']);
151 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
152 $this->assertContains('order=asc', $content['_links'][$link]['href']);
153 $this->assertContains('tags=foo', $content['_links'][$link]['href']);
154 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
155 }
156
5a619812 157 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
158 }
159
b60a666d 160 public function testGetEntriesOnPageTwo()
161 {
162 $this->client->request('GET', '/api/entries', [
163 'page' => 2,
164 'perPage' => 2,
165 ]);
166
167 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
168
169 $content = json_decode($this->client->getResponse()->getContent(), true);
170
171 $this->assertGreaterThanOrEqual(0, $content['total']);
172 $this->assertEquals(2, $content['page']);
173 $this->assertEquals(2, $content['limit']);
174 }
175
900c8448
NL
176 public function testGetStarredEntries()
177 {
178 $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']);
179
180 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
181
182 $content = json_decode($this->client->getResponse()->getContent(), true);
183
184 $this->assertGreaterThanOrEqual(1, count($content));
185 $this->assertNotEmpty($content['_embedded']['items']);
186 $this->assertGreaterThanOrEqual(1, $content['total']);
187 $this->assertEquals(1, $content['page']);
188 $this->assertGreaterThanOrEqual(1, $content['pages']);
189
190 $this->assertArrayHasKey('_links', $content);
191 $this->assertArrayHasKey('self', $content['_links']);
192 $this->assertArrayHasKey('first', $content['_links']);
193 $this->assertArrayHasKey('last', $content['_links']);
194
195 foreach (['self', 'first', 'last'] as $link) {
196 $this->assertArrayHasKey('href', $content['_links'][$link]);
197 $this->assertContains('starred=1', $content['_links'][$link]['href']);
198 $this->assertContains('sort=updated', $content['_links'][$link]['href']);
199 }
200
5a619812 201 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
202 }
203
204 public function testGetArchiveEntries()
205 {
206 $this->client->request('GET', '/api/entries', ['archive' => 1]);
207
208 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
209
210 $content = json_decode($this->client->getResponse()->getContent(), true);
211
212 $this->assertGreaterThanOrEqual(1, count($content));
213 $this->assertNotEmpty($content['_embedded']['items']);
214 $this->assertGreaterThanOrEqual(1, $content['total']);
215 $this->assertEquals(1, $content['page']);
216 $this->assertGreaterThanOrEqual(1, $content['pages']);
217
218 $this->assertArrayHasKey('_links', $content);
219 $this->assertArrayHasKey('self', $content['_links']);
220 $this->assertArrayHasKey('first', $content['_links']);
221 $this->assertArrayHasKey('last', $content['_links']);
222
223 foreach (['self', 'first', 'last'] as $link) {
224 $this->assertArrayHasKey('href', $content['_links'][$link]);
225 $this->assertContains('archive=1', $content['_links'][$link]['href']);
226 }
227
5a619812 228 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
229 }
230
231 public function testGetTaggedEntries()
232 {
233 $this->client->request('GET', '/api/entries', ['tags' => 'foo,bar']);
234
235 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
236
237 $content = json_decode($this->client->getResponse()->getContent(), true);
238
239 $this->assertGreaterThanOrEqual(1, count($content));
240 $this->assertNotEmpty($content['_embedded']['items']);
241 $this->assertGreaterThanOrEqual(1, $content['total']);
242 $this->assertEquals(1, $content['page']);
243 $this->assertGreaterThanOrEqual(1, $content['pages']);
244
245 $this->assertArrayHasKey('_links', $content);
246 $this->assertArrayHasKey('self', $content['_links']);
247 $this->assertArrayHasKey('first', $content['_links']);
248 $this->assertArrayHasKey('last', $content['_links']);
249
250 foreach (['self', 'first', 'last'] as $link) {
251 $this->assertArrayHasKey('href', $content['_links'][$link]);
252 $this->assertContains('tags='.urlencode('foo,bar'), $content['_links'][$link]['href']);
253 }
254
5a619812 255 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
256 }
257
258 public function testGetDatedEntries()
259 {
260 $this->client->request('GET', '/api/entries', ['since' => 1443274283]);
261
262 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
263
264 $content = json_decode($this->client->getResponse()->getContent(), true);
265
266 $this->assertGreaterThanOrEqual(1, count($content));
267 $this->assertNotEmpty($content['_embedded']['items']);
268 $this->assertGreaterThanOrEqual(1, $content['total']);
269 $this->assertEquals(1, $content['page']);
270 $this->assertGreaterThanOrEqual(1, $content['pages']);
271
272 $this->assertArrayHasKey('_links', $content);
273 $this->assertArrayHasKey('self', $content['_links']);
274 $this->assertArrayHasKey('first', $content['_links']);
275 $this->assertArrayHasKey('last', $content['_links']);
276
277 foreach (['self', 'first', 'last'] as $link) {
278 $this->assertArrayHasKey('href', $content['_links'][$link]);
279 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
280 }
281
5a619812 282 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
283 }
284
285 public function testGetDatedSupEntries()
286 {
287 $future = new \DateTime(date('Y-m-d H:i:s'));
288 $this->client->request('GET', '/api/entries', ['since' => $future->getTimestamp() + 1000]);
289
290 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
291
292 $content = json_decode($this->client->getResponse()->getContent(), true);
293
294 $this->assertGreaterThanOrEqual(1, count($content));
295 $this->assertEmpty($content['_embedded']['items']);
296 $this->assertEquals(0, $content['total']);
297 $this->assertEquals(1, $content['page']);
298 $this->assertEquals(1, $content['pages']);
299
300 $this->assertArrayHasKey('_links', $content);
301 $this->assertArrayHasKey('self', $content['_links']);
302 $this->assertArrayHasKey('first', $content['_links']);
303 $this->assertArrayHasKey('last', $content['_links']);
304
305 foreach (['self', 'first', 'last'] as $link) {
306 $this->assertArrayHasKey('href', $content['_links'][$link]);
307 $this->assertContains('since='.($future->getTimestamp() + 1000), $content['_links'][$link]['href']);
308 }
309
5a619812 310 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
900c8448
NL
311 }
312
313 public function testDeleteEntry()
314 {
315 $entry = $this->client->getContainer()
316 ->get('doctrine.orm.entity_manager')
317 ->getRepository('WallabagCoreBundle:Entry')
dcbebc17 318 ->findOneByUser(1, ['id' => 'asc']);
900c8448
NL
319
320 if (!$entry) {
321 $this->markTestSkipped('No content found in db.');
322 }
323
324 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json');
325
326 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
327
328 $content = json_decode($this->client->getResponse()->getContent(), true);
329
330 $this->assertEquals($entry->getTitle(), $content['title']);
331 $this->assertEquals($entry->getUrl(), $content['url']);
332
333 // We'll try to delete this entry again
334 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json');
335
336 $this->assertEquals(404, $this->client->getResponse()->getStatusCode());
337 }
338
339 public function testPostEntry()
340 {
341 $this->client->request('POST', '/api/entries.json', [
342 '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',
343 'tags' => 'google',
344 'title' => 'New title for my article',
e668a812
JB
345 'content' => 'my content',
346 'language' => 'de_DE',
347 'published_at' => '2016-09-08T11:55:58+0200',
fb436e8c 348 'authors' => 'bob,helen',
900c8448
NL
349 ]);
350
351 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
352
353 $content = json_decode($this->client->getResponse()->getContent(), true);
354
355 $this->assertGreaterThan(0, $content['id']);
356 $this->assertEquals('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']);
357 $this->assertEquals(false, $content['is_archived']);
358 $this->assertEquals(false, $content['is_starred']);
359 $this->assertEquals('New title for my article', $content['title']);
360 $this->assertEquals(1, $content['user_id']);
fdd725f5 361 $this->assertCount(2, $content['tags']);
e668a812
JB
362 $this->assertSame('my content', $content['content']);
363 $this->assertSame('de_DE', $content['language']);
364 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
fb436e8c
JB
365 $this->assertCount(2, $content['published_by']);
366 $this->assertContains('bob', $content['published_by']);
367 $this->assertContains('helen', $content['published_by']);
900c8448
NL
368 }
369
370 public function testPostSameEntry()
371 {
372 $this->client->request('POST', '/api/entries.json', [
373 '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',
374 'archive' => '1',
375 'tags' => 'google, apple',
376 ]);
377
378 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
379
380 $content = json_decode($this->client->getResponse()->getContent(), true);
381
382 $this->assertGreaterThan(0, $content['id']);
383 $this->assertEquals('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']);
384 $this->assertEquals(true, $content['is_archived']);
385 $this->assertEquals(false, $content['is_starred']);
fdd725f5 386 $this->assertCount(3, $content['tags']);
900c8448
NL
387 }
388
08f29ae7 389 public function testPostEntryWhenFetchContentFails()
390 {
391 /** @var \Symfony\Component\DependencyInjection\Container $container */
392 $container = $this->client->getContainer();
393 $contentProxy = $this->getMockBuilder(ContentProxy::class)
394 ->disableOriginalConstructor()
395 ->setMethods(['updateEntry'])
396 ->getMock();
397 $contentProxy->expects($this->any())
398 ->method('updateEntry')
399 ->willThrowException(new \Exception('Test Fetch content fails'));
400 $container->set('wallabag_core.content_proxy', $contentProxy);
401
a9357a83 402 try {
403 $this->client->request('POST', '/api/entries.json', [
404 'url' => 'http://www.example.com/',
405 ]);
406
407 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
408 $content = json_decode($this->client->getResponse()->getContent(), true);
409 $this->assertGreaterThan(0, $content['id']);
410 $this->assertEquals('http://www.example.com/', $content['url']);
411 } finally {
412 // Remove the created entry to avoid side effects on other tests
413 if (isset($content['id'])) {
414 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
415 $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']);
416 $em->remove($entry);
417 $em->flush();
418 }
419 }
08f29ae7 420 }
421
900c8448
NL
422 public function testPostArchivedAndStarredEntry()
423 {
424 $this->client->request('POST', '/api/entries.json', [
425 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
426 'archive' => '1',
427 'starred' => '1',
428 ]);
429
430 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
431
432 $content = json_decode($this->client->getResponse()->getContent(), true);
433
434 $this->assertGreaterThan(0, $content['id']);
435 $this->assertEquals('http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']);
436 $this->assertEquals(true, $content['is_archived']);
437 $this->assertEquals(true, $content['is_starred']);
438 $this->assertEquals(1, $content['user_id']);
439 }
440
441 public function testPostArchivedAndStarredEntryWithoutQuotes()
442 {
443 $this->client->request('POST', '/api/entries.json', [
444 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
445 'archive' => 0,
446 'starred' => 1,
447 ]);
448
449 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
450
451 $content = json_decode($this->client->getResponse()->getContent(), true);
452
453 $this->assertGreaterThan(0, $content['id']);
454 $this->assertEquals('http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']);
455 $this->assertEquals(false, $content['is_archived']);
456 $this->assertEquals(true, $content['is_starred']);
457 }
458
459 public function testPatchEntry()
460 {
461 $entry = $this->client->getContainer()
462 ->get('doctrine.orm.entity_manager')
463 ->getRepository('WallabagCoreBundle:Entry')
464 ->findOneByUser(1);
465
466 if (!$entry) {
467 $this->markTestSkipped('No content found in db.');
468 }
469
470 // hydrate the tags relations
471 $nbTags = count($entry->getTags());
472
473 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
474 'title' => 'New awesome title',
475 'tags' => 'new tag '.uniqid(),
476 'starred' => '1',
477 'archive' => '0',
478 ]);
479
480 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
481
482 $content = json_decode($this->client->getResponse()->getContent(), true);
483
484 $this->assertEquals($entry->getId(), $content['id']);
485 $this->assertEquals($entry->getUrl(), $content['url']);
486 $this->assertEquals('New awesome title', $content['title']);
487 $this->assertGreaterThan($nbTags, count($content['tags']));
488 $this->assertEquals(1, $content['user_id']);
489 }
490
491 public function testPatchEntryWithoutQuotes()
492 {
493 $entry = $this->client->getContainer()
494 ->get('doctrine.orm.entity_manager')
495 ->getRepository('WallabagCoreBundle:Entry')
496 ->findOneByUser(1);
497
498 if (!$entry) {
499 $this->markTestSkipped('No content found in db.');
500 }
501
502 // hydrate the tags relations
503 $nbTags = count($entry->getTags());
504
505 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
506 'title' => 'New awesome title',
507 'tags' => 'new tag '.uniqid(),
508 'starred' => 1,
509 'archive' => 0,
510 ]);
511
512 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
513
514 $content = json_decode($this->client->getResponse()->getContent(), true);
515
516 $this->assertEquals($entry->getId(), $content['id']);
517 $this->assertEquals($entry->getUrl(), $content['url']);
518 $this->assertEquals('New awesome title', $content['title']);
519 $this->assertGreaterThan($nbTags, count($content['tags']));
520 }
521
522 public function testGetTagsEntry()
523 {
524 $entry = $this->client->getContainer()
525 ->get('doctrine.orm.entity_manager')
526 ->getRepository('WallabagCoreBundle:Entry')
527 ->findOneWithTags($this->user->getId());
528
529 $entry = $entry[0];
530
531 if (!$entry) {
532 $this->markTestSkipped('No content found in db.');
533 }
534
535 $tags = [];
536 foreach ($entry->getTags() as $tag) {
537 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
538 }
539
540 $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags');
541
542 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent());
543 }
544
545 public function testPostTagsOnEntry()
546 {
547 $entry = $this->client->getContainer()
548 ->get('doctrine.orm.entity_manager')
549 ->getRepository('WallabagCoreBundle:Entry')
550 ->findOneByUser(1);
551
552 if (!$entry) {
553 $this->markTestSkipped('No content found in db.');
554 }
555
556 $nbTags = count($entry->getTags());
557
558 $newTags = 'tag1,tag2,tag3';
559
560 $this->client->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]);
561
562 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
563
564 $content = json_decode($this->client->getResponse()->getContent(), true);
565
566 $this->assertArrayHasKey('tags', $content);
567 $this->assertEquals($nbTags + 3, count($content['tags']));
568
569 $entryDB = $this->client->getContainer()
570 ->get('doctrine.orm.entity_manager')
571 ->getRepository('WallabagCoreBundle:Entry')
572 ->find($entry->getId());
573
574 $tagsInDB = [];
575 foreach ($entryDB->getTags()->toArray() as $tag) {
576 $tagsInDB[$tag->getId()] = $tag->getLabel();
577 }
578
579 foreach (explode(',', $newTags) as $tag) {
580 $this->assertContains($tag, $tagsInDB);
581 }
582 }
583
584 public function testDeleteOneTagEntry()
585 {
586 $entry = $this->client->getContainer()
587 ->get('doctrine.orm.entity_manager')
588 ->getRepository('WallabagCoreBundle:Entry')
589 ->findOneWithTags($this->user->getId());
590 $entry = $entry[0];
591
592 if (!$entry) {
593 $this->markTestSkipped('No content found in db.');
594 }
595
596 // hydrate the tags relations
597 $nbTags = count($entry->getTags());
598 $tag = $entry->getTags()[0];
599
600 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json');
601
602 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
603
604 $content = json_decode($this->client->getResponse()->getContent(), true);
605
606 $this->assertArrayHasKey('tags', $content);
607 $this->assertEquals($nbTags - 1, count($content['tags']));
608 }
609
610 public function testSaveIsArchivedAfterPost()
611 {
612 $entry = $this->client->getContainer()
613 ->get('doctrine.orm.entity_manager')
614 ->getRepository('WallabagCoreBundle:Entry')
615 ->findOneBy(['user' => 1, 'isArchived' => true]);
616
617 if (!$entry) {
618 $this->markTestSkipped('No content found in db.');
619 }
620
621 $this->client->request('POST', '/api/entries.json', [
622 'url' => $entry->getUrl(),
623 ]);
624
625 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
626
627 $content = json_decode($this->client->getResponse()->getContent(), true);
628
629 $this->assertEquals(true, $content['is_archived']);
630 }
631
632 public function testSaveIsStarredAfterPost()
633 {
634 $entry = $this->client->getContainer()
635 ->get('doctrine.orm.entity_manager')
636 ->getRepository('WallabagCoreBundle:Entry')
637 ->findOneBy(['user' => 1, 'isStarred' => true]);
638
639 if (!$entry) {
640 $this->markTestSkipped('No content found in db.');
641 }
642
643 $this->client->request('POST', '/api/entries.json', [
644 'url' => $entry->getUrl(),
645 ]);
646
647 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
648
649 $content = json_decode($this->client->getResponse()->getContent(), true);
650
651 $this->assertEquals(true, $content['is_starred']);
652 }
653
654 public function testSaveIsArchivedAfterPatch()
655 {
656 $entry = $this->client->getContainer()
657 ->get('doctrine.orm.entity_manager')
658 ->getRepository('WallabagCoreBundle:Entry')
659 ->findOneBy(['user' => 1, 'isArchived' => true]);
660
661 if (!$entry) {
662 $this->markTestSkipped('No content found in db.');
663 }
664
665 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
666 'title' => $entry->getTitle().'++',
667 ]);
668
669 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
670
671 $content = json_decode($this->client->getResponse()->getContent(), true);
672
673 $this->assertEquals(true, $content['is_archived']);
674 }
675
676 public function testSaveIsStarredAfterPatch()
677 {
678 $entry = $this->client->getContainer()
679 ->get('doctrine.orm.entity_manager')
680 ->getRepository('WallabagCoreBundle:Entry')
681 ->findOneBy(['user' => 1, 'isStarred' => true]);
682
683 if (!$entry) {
684 $this->markTestSkipped('No content found in db.');
685 }
686 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
687 'title' => $entry->getTitle().'++',
688 ]);
689
690 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
691
692 $content = json_decode($this->client->getResponse()->getContent(), true);
693
694 $this->assertEquals(true, $content['is_starred']);
695 }
696
697 public function testGetEntriesExists()
698 {
699 $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2');
700
701 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
702
703 $content = json_decode($this->client->getResponse()->getContent(), true);
704
ca9a83ee 705 $this->assertEquals(2, $content['exists']);
900c8448
NL
706 }
707
708 public function testGetEntriesExistsWithManyUrls()
709 {
710 $url1 = 'http://0.0.0.0/entry2';
711 $url2 = 'http://0.0.0.0/entry10';
712 $this->client->request('GET', '/api/entries/exists?urls[]='.$url1.'&urls[]='.$url2);
713
714 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
715
716 $content = json_decode($this->client->getResponse()->getContent(), true);
717
718 $this->assertArrayHasKey($url1, $content);
719 $this->assertArrayHasKey($url2, $content);
ca9a83ee 720 $this->assertEquals(2, $content[$url1]);
900c8448
NL
721 $this->assertEquals(false, $content[$url2]);
722 }
723
724 public function testGetEntriesExistsWhichDoesNotExists()
725 {
726 $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2');
727
728 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
729
730 $content = json_decode($this->client->getResponse()->getContent(), true);
731
732 $this->assertEquals(false, $content['exists']);
733 }
734
735 public function testGetEntriesExistsWithNoUrl()
736 {
737 $this->client->request('GET', '/api/entries/exists?url=');
738
739 $this->assertEquals(403, $this->client->getResponse()->getStatusCode());
740 }
0a6f4568
JB
741
742 public function testReloadEntryErrorWhileFetching()
743 {
70584b42 744 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
0a6f4568 745 ->getRepository('WallabagCoreBundle:Entry')
70584b42 746 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
0a6f4568
JB
747
748 if (!$entry) {
749 $this->markTestSkipped('No content found in db.');
750 }
751
752 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'/reload.json');
5cd0857e 753 $this->assertEquals(304, $this->client->getResponse()->getStatusCode());
0a6f4568
JB
754 }
755
756 public function testReloadEntry()
757 {
758 $this->client->request('POST', '/api/entries.json', [
759 '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',
760 'archive' => '1',
761 'tags' => 'google, apple',
762 ]);
763
764 $json = json_decode($this->client->getResponse()->getContent(), true);
765
766 $this->setUp();
767
768 $this->client->request('PATCH', '/api/entries/'.$json['id'].'/reload.json');
769 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
770
771 $content = json_decode($this->client->getResponse()->getContent(), true);
772
773 $this->assertNotEmpty($content['title']);
774
775 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
776 }
d1fc5902
NL
777
778 public function testPostEntriesTagsListAction()
779 {
80299ed2
NL
780 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
781 ->getRepository('WallabagCoreBundle:Entry')
dcbebc17 782 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
80299ed2
NL
783
784 $tags = $entry->getTags();
785
dcbebc17 786 $this->assertCount(2, $tags);
80299ed2 787
d1fc5902
NL
788 $list = [
789 [
dcbebc17 790 'url' => 'http://0.0.0.0/entry4',
80299ed2 791 'tags' => 'new tag 1, new tag 2',
d1fc5902 792 ],
80299ed2
NL
793 ];
794
795 $this->client->request('POST', '/api/entries/tags/lists?list='.json_encode($list));
796
797 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
798
799 $content = json_decode($this->client->getResponse()->getContent(), true);
800
801 $this->assertInternalType('int', $content[0]['entry']);
dcbebc17 802 $this->assertEquals('http://0.0.0.0/entry4', $content[0]['url']);
80299ed2
NL
803
804 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
805 ->getRepository('WallabagCoreBundle:Entry')
dcbebc17 806 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
80299ed2
NL
807
808 $tags = $entry->getTags();
dcbebc17 809 $this->assertCount(4, $tags);
80299ed2
NL
810 }
811
812 public function testDeleteEntriesTagsListAction()
813 {
814 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
815 ->getRepository('WallabagCoreBundle:Entry')
dcbebc17 816 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
80299ed2
NL
817
818 $tags = $entry->getTags();
819
dcbebc17 820 $this->assertCount(4, $tags);
80299ed2
NL
821
822 $list = [
d1fc5902 823 [
dcbebc17 824 'url' => 'http://0.0.0.0/entry4',
d1fc5902 825 'tags' => 'new tag 1, new tag 2',
d1fc5902
NL
826 ],
827 ];
828
7d2d1d68 829 $this->client->request('DELETE', '/api/entries/tags/list?list='.json_encode($list));
1eca7831
NL
830 }
831
a7abcc7b 832 public function testPostEntriesListAction()
1eca7831
NL
833 {
834 $list = [
a7abcc7b 835 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
7fa844a3 836 'http://0.0.0.0/entry2',
1eca7831
NL
837 ];
838
a7abcc7b 839 $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list));
d1fc5902
NL
840
841 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
842
843 $content = json_decode($this->client->getResponse()->getContent(), true);
844
80299ed2 845 $this->assertInternalType('int', $content[0]['entry']);
a7abcc7b 846 $this->assertEquals('http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', $content[0]['url']);
d1fc5902 847
1eca7831 848 $this->assertInternalType('int', $content[1]['entry']);
7fa844a3 849 $this->assertEquals('http://0.0.0.0/entry2', $content[1]['url']);
a7abcc7b 850 }
d1fc5902 851
a7abcc7b
NL
852 public function testDeleteEntriesListAction()
853 {
854 $list = [
719ba257 855 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
a7abcc7b
NL
856 'http://0.0.0.0/entry3',
857 ];
858
859 $this->client->request('DELETE', '/api/entries/list?urls='.json_encode($list));
d1fc5902 860
a7abcc7b
NL
861 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
862
863 $content = json_decode($this->client->getResponse()->getContent(), true);
864
865 $this->assertTrue($content[0]['entry']);
719ba257 866 $this->assertEquals('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']);
1eca7831 867
a7abcc7b
NL
868 $this->assertFalse($content[1]['entry']);
869 $this->assertEquals('http://0.0.0.0/entry3', $content[1]['url']);
d1fc5902 870 }
efd351c9 871
efd351c9
NL
872 public function testLimitBulkAction()
873 {
874 $list = [
875 'http://0.0.0.0/entry1',
876 'http://0.0.0.0/entry1',
877 'http://0.0.0.0/entry1',
878 'http://0.0.0.0/entry1',
879 'http://0.0.0.0/entry1',
880 'http://0.0.0.0/entry1',
881 'http://0.0.0.0/entry1',
882 'http://0.0.0.0/entry1',
883 'http://0.0.0.0/entry1',
884 'http://0.0.0.0/entry1',
885 'http://0.0.0.0/entry1',
886 ];
887
888 $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list));
72db15ca
JB
889
890 $this->assertEquals(400, $this->client->getResponse()->getStatusCode());
891 $this->assertContains('API limit reached', $this->client->getResponse()->getContent());
d1fc5902 892 }
900c8448 893}