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 testDeleteEntry()
126 $entry = $this->client
->getContainer()
127 ->get('doctrine.orm.entity_manager')
128 ->getRepository('WallabagCoreBundle:Entry')
132 $this->markTestSkipped('No content found in db.');
135 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'.json');
137 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
139 $content = json_decode($this->client
->getResponse()->getContent(), true);
141 $this->assertEquals($entry->getTitle(), $content['title']);
142 $this->assertEquals($entry->getUrl(), $content['url']);
144 // We'll try to delete this entry again
145 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'.json');
147 $this->assertEquals(404, $this->client
->getResponse()->getStatusCode());
150 public function testPostEntry()
152 $this->client
->request('POST', '/api/entries.json', [
153 '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',
155 'title' => 'New title for my article',
158 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
160 $content = json_decode($this->client
->getResponse()->getContent(), true);
162 $this->assertGreaterThan(0, $content['id']);
163 $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']);
164 $this->assertEquals(false, $content['is_archived']);
165 $this->assertEquals(false, $content['is_starred']);
166 $this->assertEquals('New title for my article', $content['title']);
167 $this->assertEquals(1, $content['user_id']);
168 $this->assertCount(1, $content['tags']);
171 public function testPostSameEntry()
173 $this->client
->request('POST', '/api/entries.json', [
174 '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',
176 'tags' => 'google, apple',
179 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
181 $content = json_decode($this->client
->getResponse()->getContent(), true);
183 $this->assertGreaterThan(0, $content['id']);
184 $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']);
185 $this->assertEquals(true, $content['is_archived']);
186 $this->assertEquals(false, $content['is_starred']);
187 $this->assertCount(2, $content['tags']);
190 public function testPostArchivedAndStarredEntry()
192 $this->client
->request('POST', '/api/entries.json', [
193 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
198 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
200 $content = json_decode($this->client
->getResponse()->getContent(), true);
202 $this->assertGreaterThan(0, $content['id']);
203 $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']);
204 $this->assertEquals(true, $content['is_archived']);
205 $this->assertEquals(true, $content['is_starred']);
206 $this->assertEquals(1, $content['user_id']);
209 public function testPostArchivedAndStarredEntryWithoutQuotes()
211 $this->client
->request('POST', '/api/entries.json', [
212 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
217 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
219 $content = json_decode($this->client
->getResponse()->getContent(), true);
221 $this->assertGreaterThan(0, $content['id']);
222 $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']);
223 $this->assertEquals(false, $content['is_archived']);
224 $this->assertEquals(true, $content['is_starred']);
227 public function testPatchEntry()
229 $entry = $this->client
->getContainer()
230 ->get('doctrine.orm.entity_manager')
231 ->getRepository('WallabagCoreBundle:Entry')
235 $this->markTestSkipped('No content found in db.');
238 // hydrate the tags relations
239 $nbTags = count($entry->getTags());
241 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
242 'title' => 'New awesome title',
243 'tags' => 'new tag '.uniqid(),
248 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
250 $content = json_decode($this->client
->getResponse()->getContent(), true);
252 $this->assertEquals($entry->getId(), $content['id']);
253 $this->assertEquals($entry->getUrl(), $content['url']);
254 $this->assertEquals('New awesome title', $content['title']);
255 $this->assertGreaterThan($nbTags, count($content['tags']));
256 $this->assertEquals(1, $content['user_id']);
259 public function testPatchEntryWithoutQuotes()
261 $entry = $this->client
->getContainer()
262 ->get('doctrine.orm.entity_manager')
263 ->getRepository('WallabagCoreBundle:Entry')
267 $this->markTestSkipped('No content found in db.');
270 // hydrate the tags relations
271 $nbTags = count($entry->getTags());
273 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
274 'title' => 'New awesome title',
275 'tags' => 'new tag '.uniqid(),
280 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
282 $content = json_decode($this->client
->getResponse()->getContent(), true);
284 $this->assertEquals($entry->getId(), $content['id']);
285 $this->assertEquals($entry->getUrl(), $content['url']);
286 $this->assertEquals('New awesome title', $content['title']);
287 $this->assertGreaterThan($nbTags, count($content['tags']));
290 public function testGetTagsEntry()
292 $entry = $this->client
->getContainer()
293 ->get('doctrine.orm.entity_manager')
294 ->getRepository('WallabagCoreBundle:Entry')
295 ->findOneWithTags(1);
300 $this->markTestSkipped('No content found in db.');
304 foreach ($entry->getTags() as $tag) {
305 $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()];
308 $this->client
->request('GET', '/api/entries/'.$entry->getId().'/tags');
310 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT
), $this->client
->getResponse()->getContent());
313 public function testPostTagsOnEntry()
315 $entry = $this->client
->getContainer()
316 ->get('doctrine.orm.entity_manager')
317 ->getRepository('WallabagCoreBundle:Entry')
321 $this->markTestSkipped('No content found in db.');
324 $nbTags = count($entry->getTags());
326 $newTags = 'tag1,tag2,tag3';
328 $this->client
->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]);
330 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
332 $content = json_decode($this->client
->getResponse()->getContent(), true);
334 $this->assertArrayHasKey('tags', $content);
335 $this->assertEquals($nbTags +
3, count($content['tags']));
337 $entryDB = $this->client
->getContainer()
338 ->get('doctrine.orm.entity_manager')
339 ->getRepository('WallabagCoreBundle:Entry')
340 ->find($entry->getId());
343 foreach ($entryDB->getTags()->toArray() as $tag) {
344 $tagsInDB[$tag->getId()] = $tag->getLabel();
347 foreach (explode(',', $newTags) as $tag) {
348 $this->assertContains($tag, $tagsInDB);
352 public function testDeleteOneTagEntry()
354 $entry = $this->client
->getContainer()
355 ->get('doctrine.orm.entity_manager')
356 ->getRepository('WallabagCoreBundle:Entry')
357 ->findOneWithTags(1);
361 $this->markTestSkipped('No content found in db.');
364 // hydrate the tags relations
365 $nbTags = count($entry->getTags());
366 $tag = $entry->getTags()[0];
368 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json');
370 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
372 $content = json_decode($this->client
->getResponse()->getContent(), true);
374 $this->assertArrayHasKey('tags', $content);
375 $this->assertEquals($nbTags - 1, count($content['tags']));
378 public function testGetUserTags()
380 $this->client
->request('GET', '/api/tags.json');
382 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
384 $content = json_decode($this->client
->getResponse()->getContent(), true);
386 $this->assertGreaterThan(0, $content);
387 $this->assertArrayHasKey('id', $content[0]);
388 $this->assertArrayHasKey('label', $content[0]);
390 return end($content);
394 * @depends testGetUserTags
396 public function testDeleteUserTag($tag)
398 $this->client
->request('DELETE', '/api/tags/'.$tag['id'].'.json');
400 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
402 $content = json_decode($this->client
->getResponse()->getContent(), true);
404 $this->assertArrayHasKey('label', $content);
405 $this->assertEquals($tag['label'], $content['label']);
406 $this->assertEquals($tag['slug'], $content['slug']);
408 $entries = $entry = $this->client
->getContainer()
409 ->get('doctrine.orm.entity_manager')
410 ->getRepository('WallabagCoreBundle:Entry')
411 ->findAllByTagId($this->user
->getId(), $tag['id']);
413 $this->assertCount(0, $entries);
416 public function testGetVersion()
418 $this->client
->request('GET', '/api/version');
420 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
422 $content = json_decode($this->client
->getResponse()->getContent(), true);
424 $this->assertEquals($this->client
->getContainer()->getParameter('wallabag_core.version'), $content);
427 public function testSaveIsArchivedAfterPost()
429 $entry = $this->client
->getContainer()
430 ->get('doctrine.orm.entity_manager')
431 ->getRepository('WallabagCoreBundle:Entry')
432 ->findOneBy(['user' => 1, 'isArchived' => true]);
435 $this->markTestSkipped('No content found in db.');
438 $this->client
->request('POST', '/api/entries.json', [
439 'url' => $entry->getUrl(),
442 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
444 $content = json_decode($this->client
->getResponse()->getContent(), true);
446 $this->assertEquals(true, $content['is_archived']);
449 public function testSaveIsStarredAfterPost()
451 $entry = $this->client
->getContainer()
452 ->get('doctrine.orm.entity_manager')
453 ->getRepository('WallabagCoreBundle:Entry')
454 ->findOneBy(['user' => 1, 'isStarred' => true]);
457 $this->markTestSkipped('No content found in db.');
460 $this->client
->request('POST', '/api/entries.json', [
461 'url' => $entry->getUrl(),
464 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
466 $content = json_decode($this->client
->getResponse()->getContent(), true);
468 $this->assertEquals(true, $content['is_starred']);
471 public function testSaveIsArchivedAfterPatch()
473 $entry = $this->client
->getContainer()
474 ->get('doctrine.orm.entity_manager')
475 ->getRepository('WallabagCoreBundle:Entry')
476 ->findOneBy(['user' => 1, 'isArchived' => true]);
479 $this->markTestSkipped('No content found in db.');
482 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
483 'title' => $entry->getTitle().'++',
486 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
488 $content = json_decode($this->client
->getResponse()->getContent(), true);
490 $this->assertEquals(true, $content['is_archived']);
493 public function testSaveIsStarredAfterPatch()
495 $entry = $this->client
->getContainer()
496 ->get('doctrine.orm.entity_manager')
497 ->getRepository('WallabagCoreBundle:Entry')
498 ->findOneBy(['user' => 1, 'isStarred' => true]);
501 $this->markTestSkipped('No content found in db.');
503 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', [
504 'title' => $entry->getTitle().'++',
507 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
509 $content = json_decode($this->client
->getResponse()->getContent(), true);
511 $this->assertEquals(true, $content['is_starred']);