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 testGetDatedEntries()
126 $this->client
->request('GET', '/api/entries', ['since' => 1]);
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 testGetDatedSupEntries()
148 $future = new \
DateTime(date('Y-m-d H:i:s'));
149 $this->client
->request('GET', '/api/entries', ['since' => $future->getTimestamp() +
1000]);
151 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
153 $content = json_decode($this->client
->getResponse()->getContent(), true);
155 $this->assertGreaterThanOrEqual(1, count($content));
156 $this->assertEmpty($content['_embedded']['items']);
157 $this->assertEquals(0, $content['total']);
158 $this->assertEquals(1, $content['page']);
159 $this->assertEquals(1, $content['pages']);
162 $this->client
->getResponse()->headers
->contains(
169 public function testDeleteEntry()
171 $entry = $this->client
->getContainer()
172 ->get('doctrine.orm.entity_manager')
173 ->getRepository('WallabagCoreBundle:Entry')
177 $this->markTestSkipped('No content found in db.');
180 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'.json');
182 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
184 $content = json_decode($this->client
->getResponse()->getContent(), true);
186 $this->assertEquals($entry->getTitle(), $content['title']);
187 $this->assertEquals($entry->getUrl(), $content['url']);
189 // We'll try to delete this entry again
190 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'.json');
192 $this->assertEquals(404, $this->client
->getResponse()->getStatusCode());
195 public function testPostEntry()
197 $this->client
->request('POST', '/api/entries.json', [
198 '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',
200 'title' => 'New title for my article',
203 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
205 $content = json_decode($this->client
->getResponse()->getContent(), true);
207 $this->assertGreaterThan(0, $content['id']);
208 $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']);
209 $this->assertEquals(false, $content['is_archived']);
210 $this->assertEquals(false, $content['is_starred']);
211 $this->assertEquals('New title for my article', $content['title']);
212 $this->assertEquals(1, $content['user_id']);
213 $this->assertCount(1, $content['tags']);
216 public function testPostSameEntry()
218 $this->client
->request('POST', '/api/entries.json', [
219 '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',
221 'tags' => 'google, apple',
224 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
226 $content = json_decode($this->client
->getResponse()->getContent(), true);
228 $this->assertGreaterThan(0, $content['id']);
229 $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']);
230 $this->assertEquals(true, $content['is_archived']);
231 $this->assertEquals(false, $content['is_starred']);
232 $this->assertCount(2, $content['tags']);
235 public function testPostArchivedAndStarredEntry()
237 $this->client
->request('POST', '/api/entries.json', [
238 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
243 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
245 $content = json_decode($this->client
->getResponse()->getContent(), true);
247 $this->assertGreaterThan(0, $content['id']);
248 $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']);
249 $this->assertEquals(true, $content['is_archived']);
250 $this->assertEquals(true, $content['is_starred']);
251 $this->assertEquals(1, $content['user_id']);
254 public function testPostArchivedAndStarredEntryWithoutQuotes()
256 $this->client
->request('POST', '/api/entries.json', [
257 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
262 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
264 $content = json_decode($this->client
->getResponse()->getContent(), true);
266 $this->assertGreaterThan(0, $content['id']);
267 $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']);
268 $this->assertEquals(false, $content['is_archived']);
269 $this->assertEquals(true, $content['is_starred']);
272 public function testPatchEntry()
274 $entry = $this->client
->getContainer()
275 ->get('doctrine.orm.entity_manager')
276 ->getRepository('WallabagCoreBundle:Entry')
280 $this->markTestSkipped('No content found in db.');
283 // hydrate the tags relations
284 $nbTags = count($entry->getTags());
286 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
287 'title' => 'New awesome title',
288 'tags' => 'new tag '.uniqid(),
293 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
295 $content = json_decode($this->client
->getResponse()->getContent(), true);
297 $this->assertEquals($entry->getId(), $content['id']);
298 $this->assertEquals($entry->getUrl(), $content['url']);
299 $this->assertEquals('New awesome title', $content['title']);
300 $this->assertGreaterThan($nbTags, count($content['tags']));
301 $this->assertEquals(1, $content['user_id']);
304 public function testPatchEntryWithoutQuotes()
306 $entry = $this->client
->getContainer()
307 ->get('doctrine.orm.entity_manager')
308 ->getRepository('WallabagCoreBundle:Entry')
312 $this->markTestSkipped('No content found in db.');
315 // hydrate the tags relations
316 $nbTags = count($entry->getTags());
318 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
319 'title' => 'New awesome title',
320 'tags' => 'new tag '.uniqid(),
325 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
327 $content = json_decode($this->client
->getResponse()->getContent(), true);
329 $this->assertEquals($entry->getId(), $content['id']);
330 $this->assertEquals($entry->getUrl(), $content['url']);
331 $this->assertEquals('New awesome title', $content['title']);
332 $this->assertGreaterThan($nbTags, count($content['tags']));
335 public function testGetTagsEntry()
337 $entry = $this->client
->getContainer()
338 ->get('doctrine.orm.entity_manager')
339 ->getRepository('WallabagCoreBundle:Entry')
340 ->findOneWithTags(1);
345 $this->markTestSkipped('No content found in db.');
349 foreach ($entry->getTags() as $tag) {
350 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
353 $this->client
->request('GET', '/api/entries/'.$entry->getId().'/tags');
355 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT
), $this->client
->getResponse()->getContent());
358 public function testPostTagsOnEntry()
360 $entry = $this->client
->getContainer()
361 ->get('doctrine.orm.entity_manager')
362 ->getRepository('WallabagCoreBundle:Entry')
366 $this->markTestSkipped('No content found in db.');
369 $nbTags = count($entry->getTags());
371 $newTags = 'tag1,tag2,tag3';
373 $this->client
->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]);
375 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
377 $content = json_decode($this->client
->getResponse()->getContent(), true);
379 $this->assertArrayHasKey('tags', $content);
380 $this->assertEquals($nbTags +
3, count($content['tags']));
382 $entryDB = $this->client
->getContainer()
383 ->get('doctrine.orm.entity_manager')
384 ->getRepository('WallabagCoreBundle:Entry')
385 ->find($entry->getId());
388 foreach ($entryDB->getTags()->toArray() as $tag) {
389 $tagsInDB[$tag->getId()] = $tag->getLabel();
392 foreach (explode(',', $newTags) as $tag) {
393 $this->assertContains($tag, $tagsInDB);
397 public function testDeleteOneTagEntry()
399 $entry = $this->client
->getContainer()
400 ->get('doctrine.orm.entity_manager')
401 ->getRepository('WallabagCoreBundle:Entry')
402 ->findOneWithTags(1);
406 $this->markTestSkipped('No content found in db.');
409 // hydrate the tags relations
410 $nbTags = count($entry->getTags());
411 $tag = $entry->getTags()[0];
413 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json');
415 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
417 $content = json_decode($this->client
->getResponse()->getContent(), true);
419 $this->assertArrayHasKey('tags', $content);
420 $this->assertEquals($nbTags - 1, count($content['tags']));
423 public function testGetUserTags()
425 $this->client
->request('GET', '/api/tags.json');
427 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
429 $content = json_decode($this->client
->getResponse()->getContent(), true);
431 $this->assertGreaterThan(0, $content);
432 $this->assertArrayHasKey('id', $content[0]);
433 $this->assertArrayHasKey('label', $content[0]);
435 return end($content);
439 * @depends testGetUserTags
441 public function testDeleteUserTag($tag)
443 $this->client
->request('DELETE', '/api/tags/'.$tag['id'].'.json');
445 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
447 $content = json_decode($this->client
->getResponse()->getContent(), true);
449 $this->assertArrayHasKey('label', $content);
450 $this->assertEquals($tag['label'], $content['label']);
451 $this->assertEquals($tag['slug'], $content['slug']);
453 $entries = $entry = $this->client
->getContainer()
454 ->get('doctrine.orm.entity_manager')
455 ->getRepository('WallabagCoreBundle:Entry')
456 ->findAllByTagId($this->user
->getId(), $tag['id']);
458 $this->assertCount(0, $entries);
461 public function testGetVersion()
463 $this->client
->request('GET', '/api/version');
465 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
467 $content = json_decode($this->client
->getResponse()->getContent(), true);
469 $this->assertEquals($this->client
->getContainer()->getParameter('wallabag_core.version'), $content);
472 public function testSaveIsArchivedAfterPost()
474 $entry = $this->client
->getContainer()
475 ->get('doctrine.orm.entity_manager')
476 ->getRepository('WallabagCoreBundle:Entry')
477 ->findOneBy(['user' => 1, 'isArchived' => true]);
480 $this->markTestSkipped('No content found in db.');
483 $this->client
->request('POST', '/api/entries.json', [
484 'url' => $entry->getUrl(),
487 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
489 $content = json_decode($this->client
->getResponse()->getContent(), true);
491 $this->assertEquals(true, $content['is_archived']);
494 public function testSaveIsStarredAfterPost()
496 $entry = $this->client
->getContainer()
497 ->get('doctrine.orm.entity_manager')
498 ->getRepository('WallabagCoreBundle:Entry')
499 ->findOneBy(['user' => 1, 'isStarred' => 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_starred']);
516 public function testSaveIsArchivedAfterPatch()
518 $entry = $this->client
->getContainer()
519 ->get('doctrine.orm.entity_manager')
520 ->getRepository('WallabagCoreBundle:Entry')
521 ->findOneBy(['user' => 1, 'isArchived' => true]);
524 $this->markTestSkipped('No content found in db.');
527 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
528 'title' => $entry->getTitle().'++',
531 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
533 $content = json_decode($this->client
->getResponse()->getContent(), true);
535 $this->assertEquals(true, $content['is_archived']);
538 public function testSaveIsStarredAfterPatch()
540 $entry = $this->client
->getContainer()
541 ->get('doctrine.orm.entity_manager')
542 ->getRepository('WallabagCoreBundle:Entry')
543 ->findOneBy(['user' => 1, 'isStarred' => true]);
546 $this->markTestSkipped('No content found in db.');
548 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
549 'title' => $entry->getTitle().'++',
552 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
554 $content = json_decode($this->client
->getResponse()->getContent(), true);
556 $this->assertEquals(true, $content['is_starred']);