3 namespace Tests\Wallabag\ApiBundle\Controller
;
5 use Tests\Wallabag\ApiBundle\WallabagApiTestCase
;
7 class WallabagRestControllerTest
extends WallabagApiTestCase
9 protected static $salt;
11 public function testGetOneEntry()
13 $entry = $this->client
->getContainer()
14 ->get('doctrine.orm.entity_manager')
15 ->getRepository('WallabagCoreBundle:Entry')
16 ->findOneBy(['user' => 1, 'isArchived' => false]);
19 $this->markTestSkipped('No content found in db.');
22 $this->client
->request('GET', '/api/entries/'.$entry->getId().'.json');
23 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
25 $content = json_decode($this->client
->getResponse()->getContent(), true);
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']);
35 $this->client
->getResponse()->headers
->contains(
42 public function testGetOneEntryWrongUser()
44 $entry = $this->client
->getContainer()
45 ->get('doctrine.orm.entity_manager')
46 ->getRepository('WallabagCoreBundle:Entry')
47 ->findOneBy(['user' => 2, 'isArchived' => false]);
50 $this->markTestSkipped('No content found in db.');
53 $this->client
->request('GET', '/api/entries/'.$entry->getId().'.json');
55 $this->assertEquals(403, $this->client
->getResponse()->getStatusCode());
58 public function testGetEntries()
60 $this->client
->request('GET', '/api/entries');
62 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
64 $content = json_decode($this->client
->getResponse()->getContent(), true);
66 $this->assertGreaterThanOrEqual(1, count($content));
67 $this->assertNotEmpty($content['_embedded']['items']);
68 $this->assertGreaterThanOrEqual(1, $content['total']);
69 $this->assertEquals(1, $content['page']);
70 $this->assertGreaterThanOrEqual(1, $content['pages']);
73 $this->client
->getResponse()->headers
->contains(
80 public function testGetStarredEntries()
82 $this->client
->request('GET', '/api/entries', ['star' => 1, 'sort' => 'updated']);
84 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
86 $content = json_decode($this->client
->getResponse()->getContent(), true);
88 $this->assertGreaterThanOrEqual(1, count($content));
89 $this->assertNotEmpty($content['_embedded']['items']);
90 $this->assertGreaterThanOrEqual(1, $content['total']);
91 $this->assertEquals(1, $content['page']);
92 $this->assertGreaterThanOrEqual(1, $content['pages']);
95 $this->client
->getResponse()->headers
->contains(
102 public function testGetArchiveEntries()
104 $this->client
->request('GET', '/api/entries', ['archive' => 1]);
106 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
108 $content = json_decode($this->client
->getResponse()->getContent(), true);
110 $this->assertGreaterThanOrEqual(1, count($content));
111 $this->assertNotEmpty($content['_embedded']['items']);
112 $this->assertGreaterThanOrEqual(1, $content['total']);
113 $this->assertEquals(1, $content['page']);
114 $this->assertGreaterThanOrEqual(1, $content['pages']);
117 $this->client
->getResponse()->headers
->contains(
124 public function testGetTaggedEntries()
126 $this->client
->request('GET', '/api/entries', ['tags' => 'foo,bar']);
128 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
130 $content = json_decode($this->client
->getResponse()->getContent(), true);
132 $this->assertGreaterThanOrEqual(1, count($content));
133 $this->assertNotEmpty($content['_embedded']['items']);
134 $this->assertGreaterThanOrEqual(1, $content['total']);
135 $this->assertEquals(1, $content['page']);
136 $this->assertGreaterThanOrEqual(1, $content['pages']);
139 $this->client
->getResponse()->headers
->contains(
146 public function testGetDatedEntries()
148 $this->client
->request('GET', '/api/entries', ['since' => 1]);
150 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
152 $content = json_decode($this->client
->getResponse()->getContent(), true);
154 $this->assertGreaterThanOrEqual(1, count($content));
155 $this->assertNotEmpty($content['_embedded']['items']);
156 $this->assertGreaterThanOrEqual(1, $content['total']);
157 $this->assertEquals(1, $content['page']);
158 $this->assertGreaterThanOrEqual(1, $content['pages']);
161 $this->client
->getResponse()->headers
->contains(
168 public function testGetDatedSupEntries()
170 $future = new \
DateTime(date('Y-m-d H:i:s'));
171 $this->client
->request('GET', '/api/entries', ['since' => $future->getTimestamp() +
1000]);
173 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
175 $content = json_decode($this->client
->getResponse()->getContent(), true);
177 $this->assertGreaterThanOrEqual(1, count($content));
178 $this->assertEmpty($content['_embedded']['items']);
179 $this->assertEquals(0, $content['total']);
180 $this->assertEquals(1, $content['page']);
181 $this->assertEquals(1, $content['pages']);
184 $this->client
->getResponse()->headers
->contains(
191 public function testDeleteEntry()
193 $entry = $this->client
->getContainer()
194 ->get('doctrine.orm.entity_manager')
195 ->getRepository('WallabagCoreBundle:Entry')
199 $this->markTestSkipped('No content found in db.');
202 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'.json');
204 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
206 $content = json_decode($this->client
->getResponse()->getContent(), true);
208 $this->assertEquals($entry->getTitle(), $content['title']);
209 $this->assertEquals($entry->getUrl(), $content['url']);
211 // We'll try to delete this entry again
212 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'.json');
214 $this->assertEquals(404, $this->client
->getResponse()->getStatusCode());
217 public function testPostEntry()
219 $this->client
->request('POST', '/api/entries.json', [
220 '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',
222 'title' => 'New title for my article',
225 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
227 $content = json_decode($this->client
->getResponse()->getContent(), true);
229 $this->assertGreaterThan(0, $content['id']);
230 $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']);
231 $this->assertEquals(false, $content['is_archived']);
232 $this->assertEquals(false, $content['is_starred']);
233 $this->assertEquals('New title for my article', $content['title']);
234 $this->assertEquals(1, $content['user_id']);
235 $this->assertCount(1, $content['tags']);
238 public function testPostSameEntry()
240 $this->client
->request('POST', '/api/entries.json', [
241 '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',
243 'tags' => 'google, apple',
246 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
248 $content = json_decode($this->client
->getResponse()->getContent(), true);
250 $this->assertGreaterThan(0, $content['id']);
251 $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']);
252 $this->assertEquals(true, $content['is_archived']);
253 $this->assertEquals(false, $content['is_starred']);
254 $this->assertCount(2, $content['tags']);
257 public function testPostArchivedAndStarredEntry()
259 $this->client
->request('POST', '/api/entries.json', [
260 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
265 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
267 $content = json_decode($this->client
->getResponse()->getContent(), true);
269 $this->assertGreaterThan(0, $content['id']);
270 $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']);
271 $this->assertEquals(true, $content['is_archived']);
272 $this->assertEquals(true, $content['is_starred']);
273 $this->assertEquals(1, $content['user_id']);
276 public function testPostArchivedAndStarredEntryWithoutQuotes()
278 $this->client
->request('POST', '/api/entries.json', [
279 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
284 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
286 $content = json_decode($this->client
->getResponse()->getContent(), true);
288 $this->assertGreaterThan(0, $content['id']);
289 $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']);
290 $this->assertEquals(false, $content['is_archived']);
291 $this->assertEquals(true, $content['is_starred']);
294 public function testPatchEntry()
296 $entry = $this->client
->getContainer()
297 ->get('doctrine.orm.entity_manager')
298 ->getRepository('WallabagCoreBundle:Entry')
302 $this->markTestSkipped('No content found in db.');
305 // hydrate the tags relations
306 $nbTags = count($entry->getTags());
308 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
309 'title' => 'New awesome title',
310 'tags' => 'new tag '.uniqid(),
315 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
317 $content = json_decode($this->client
->getResponse()->getContent(), true);
319 $this->assertEquals($entry->getId(), $content['id']);
320 $this->assertEquals($entry->getUrl(), $content['url']);
321 $this->assertEquals('New awesome title', $content['title']);
322 $this->assertGreaterThan($nbTags, count($content['tags']));
323 $this->assertEquals(1, $content['user_id']);
326 public function testPatchEntryWithoutQuotes()
328 $entry = $this->client
->getContainer()
329 ->get('doctrine.orm.entity_manager')
330 ->getRepository('WallabagCoreBundle:Entry')
334 $this->markTestSkipped('No content found in db.');
337 // hydrate the tags relations
338 $nbTags = count($entry->getTags());
340 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
341 'title' => 'New awesome title',
342 'tags' => 'new tag '.uniqid(),
347 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
349 $content = json_decode($this->client
->getResponse()->getContent(), true);
351 $this->assertEquals($entry->getId(), $content['id']);
352 $this->assertEquals($entry->getUrl(), $content['url']);
353 $this->assertEquals('New awesome title', $content['title']);
354 $this->assertGreaterThan($nbTags, count($content['tags']));
357 public function testGetTagsEntry()
359 $entry = $this->client
->getContainer()
360 ->get('doctrine.orm.entity_manager')
361 ->getRepository('WallabagCoreBundle:Entry')
362 ->findOneWithTags(1);
367 $this->markTestSkipped('No content found in db.');
371 foreach ($entry->getTags() as $tag) {
372 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
375 $this->client
->request('GET', '/api/entries/'.$entry->getId().'/tags');
377 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT
), $this->client
->getResponse()->getContent());
380 public function testPostTagsOnEntry()
382 $entry = $this->client
->getContainer()
383 ->get('doctrine.orm.entity_manager')
384 ->getRepository('WallabagCoreBundle:Entry')
388 $this->markTestSkipped('No content found in db.');
391 $nbTags = count($entry->getTags());
393 $newTags = 'tag1,tag2,tag3';
395 $this->client
->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]);
397 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
399 $content = json_decode($this->client
->getResponse()->getContent(), true);
401 $this->assertArrayHasKey('tags', $content);
402 $this->assertEquals($nbTags +
3, count($content['tags']));
404 $entryDB = $this->client
->getContainer()
405 ->get('doctrine.orm.entity_manager')
406 ->getRepository('WallabagCoreBundle:Entry')
407 ->find($entry->getId());
410 foreach ($entryDB->getTags()->toArray() as $tag) {
411 $tagsInDB[$tag->getId()] = $tag->getLabel();
414 foreach (explode(',', $newTags) as $tag) {
415 $this->assertContains($tag, $tagsInDB);
419 public function testDeleteOneTagEntry()
421 $entry = $this->client
->getContainer()
422 ->get('doctrine.orm.entity_manager')
423 ->getRepository('WallabagCoreBundle:Entry')
424 ->findOneWithTags(1);
428 $this->markTestSkipped('No content found in db.');
431 // hydrate the tags relations
432 $nbTags = count($entry->getTags());
433 $tag = $entry->getTags()[0];
435 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json');
437 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
439 $content = json_decode($this->client
->getResponse()->getContent(), true);
441 $this->assertArrayHasKey('tags', $content);
442 $this->assertEquals($nbTags - 1, count($content['tags']));
445 public function testGetUserTags()
447 $this->client
->request('GET', '/api/tags.json');
449 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
451 $content = json_decode($this->client
->getResponse()->getContent(), true);
453 $this->assertGreaterThan(0, $content);
454 $this->assertArrayHasKey('id', $content[0]);
455 $this->assertArrayHasKey('label', $content[0]);
457 return end($content);
461 * @depends testGetUserTags
463 public function testDeleteUserTag($tag)
465 $this->client
->request('DELETE', '/api/tags/'.$tag['id'].'.json');
467 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
469 $content = json_decode($this->client
->getResponse()->getContent(), true);
471 $this->assertArrayHasKey('label', $content);
472 $this->assertEquals($tag['label'], $content['label']);
473 $this->assertEquals($tag['slug'], $content['slug']);
475 $entries = $entry = $this->client
->getContainer()
476 ->get('doctrine.orm.entity_manager')
477 ->getRepository('WallabagCoreBundle:Entry')
478 ->findAllByTagId($this->user
->getId(), $tag['id']);
480 $this->assertCount(0, $entries);
483 public function testGetVersion()
485 $this->client
->request('GET', '/api/version');
487 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
489 $content = json_decode($this->client
->getResponse()->getContent(), true);
491 $this->assertEquals($this->client
->getContainer()->getParameter('wallabag_core.version'), $content);
494 public function testSaveIsArchivedAfterPost()
496 $entry = $this->client
->getContainer()
497 ->get('doctrine.orm.entity_manager')
498 ->getRepository('WallabagCoreBundle:Entry')
499 ->findOneBy(['user' => 1, 'isArchived' => true]);
502 $this->markTestSkipped('No content found in db.');
505 $this->client
->request('POST', '/api/entries.json', [
506 'url' => $entry->getUrl(),
509 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
511 $content = json_decode($this->client
->getResponse()->getContent(), true);
513 $this->assertEquals(true, $content['is_archived']);
516 public function testSaveIsStarredAfterPost()
518 $entry = $this->client
->getContainer()
519 ->get('doctrine.orm.entity_manager')
520 ->getRepository('WallabagCoreBundle:Entry')
521 ->findOneBy(['user' => 1, 'isStarred' => true]);
524 $this->markTestSkipped('No content found in db.');
527 $this->client
->request('POST', '/api/entries.json', [
528 'url' => $entry->getUrl(),
531 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
533 $content = json_decode($this->client
->getResponse()->getContent(), true);
535 $this->assertEquals(true, $content['is_starred']);
538 public function testSaveIsArchivedAfterPatch()
540 $entry = $this->client
->getContainer()
541 ->get('doctrine.orm.entity_manager')
542 ->getRepository('WallabagCoreBundle:Entry')
543 ->findOneBy(['user' => 1, 'isArchived' => true]);
546 $this->markTestSkipped('No content found in db.');
549 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
550 'title' => $entry->getTitle().'++',
553 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
555 $content = json_decode($this->client
->getResponse()->getContent(), true);
557 $this->assertEquals(true, $content['is_archived']);
560 public function testSaveIsStarredAfterPatch()
562 $entry = $this->client
->getContainer()
563 ->get('doctrine.orm.entity_manager')
564 ->getRepository('WallabagCoreBundle:Entry')
565 ->findOneBy(['user' => 1, 'isStarred' => true]);
568 $this->markTestSkipped('No content found in db.');
570 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
571 'title' => $entry->getTitle().'++',
574 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
576 $content = json_decode($this->client
->getResponse()->getContent(), true);
578 $this->assertEquals(true, $content['is_starred']);