3 namespace Wallabag\ApiBundle\Tests\Controller
;
5 use Wallabag\ApiBundle\Tests\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(array('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(array('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', array('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', array('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', array(
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',
157 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
159 $content = json_decode($this->client
->getResponse()->getContent(), true);
161 $this->assertGreaterThan(0, $content['id']);
162 $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']);
163 $this->assertEquals(false, $content['is_archived']);
164 $this->assertEquals(false, $content['is_starred']);
165 $this->assertEquals(1, $content['user_id']);
166 $this->assertCount(1, $content['tags']);
169 public function testPostSameEntry()
171 $this->client
->request('POST', '/api/entries.json', array(
172 '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',
174 'tags' => 'google, apple',
177 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
179 $content = json_decode($this->client
->getResponse()->getContent(), true);
181 $this->assertGreaterThan(0, $content['id']);
182 $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']);
183 $this->assertEquals(true, $content['is_archived']);
184 $this->assertEquals(false, $content['is_starred']);
185 $this->assertCount(2, $content['tags']);
188 public function testPostArchivedAndStarredEntry()
190 $this->client
->request('POST', '/api/entries.json', array(
191 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
196 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
198 $content = json_decode($this->client
->getResponse()->getContent(), true);
200 $this->assertGreaterThan(0, $content['id']);
201 $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']);
202 $this->assertEquals(true, $content['is_archived']);
203 $this->assertEquals(true, $content['is_starred']);
204 $this->assertEquals(1, $content['user_id']);
207 public function testPostArchivedAndStarredEntryWithoutQuotes()
209 $this->client
->request('POST', '/api/entries.json', array(
210 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
215 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
217 $content = json_decode($this->client
->getResponse()->getContent(), true);
219 $this->assertGreaterThan(0, $content['id']);
220 $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']);
221 $this->assertEquals(false, $content['is_archived']);
222 $this->assertEquals(true, $content['is_starred']);
225 public function testPatchEntry()
227 $entry = $this->client
->getContainer()
228 ->get('doctrine.orm.entity_manager')
229 ->getRepository('WallabagCoreBundle:Entry')
233 $this->markTestSkipped('No content found in db.');
236 // hydrate the tags relations
237 $nbTags = count($entry->getTags());
239 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', array(
240 'title' => 'New awesome title',
241 'tags' => 'new tag '.uniqid(),
246 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
248 $content = json_decode($this->client
->getResponse()->getContent(), true);
250 $this->assertEquals($entry->getId(), $content['id']);
251 $this->assertEquals($entry->getUrl(), $content['url']);
252 $this->assertEquals('New awesome title', $content['title']);
253 $this->assertGreaterThan($nbTags, count($content['tags']));
254 $this->assertEquals(1, $content['user_id']);
257 public function testPatchEntryWithoutQuotes()
259 $entry = $this->client
->getContainer()
260 ->get('doctrine.orm.entity_manager')
261 ->getRepository('WallabagCoreBundle:Entry')
265 $this->markTestSkipped('No content found in db.');
268 // hydrate the tags relations
269 $nbTags = count($entry->getTags());
271 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', array(
272 'title' => 'New awesome title',
273 'tags' => 'new tag '.uniqid(),
278 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
280 $content = json_decode($this->client
->getResponse()->getContent(), true);
282 $this->assertEquals($entry->getId(), $content['id']);
283 $this->assertEquals($entry->getUrl(), $content['url']);
284 $this->assertEquals('New awesome title', $content['title']);
285 $this->assertGreaterThan($nbTags, count($content['tags']));
288 public function testGetTagsEntry()
290 $entry = $this->client
->getContainer()
291 ->get('doctrine.orm.entity_manager')
292 ->getRepository('WallabagCoreBundle:Entry')
293 ->findOneWithTags(1);
298 $this->markTestSkipped('No content found in db.');
302 foreach ($entry->getTags() as $tag) {
303 $tags[] = array('id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug());
306 $this->client
->request('GET', '/api/entries/'.$entry->getId().'/tags');
308 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT
), $this->client
->getResponse()->getContent());
311 public function testPostTagsOnEntry()
313 $entry = $this->client
->getContainer()
314 ->get('doctrine.orm.entity_manager')
315 ->getRepository('WallabagCoreBundle:Entry')
319 $this->markTestSkipped('No content found in db.');
322 $nbTags = count($entry->getTags());
324 $newTags = 'tag1,tag2,tag3';
326 $this->client
->request('POST', '/api/entries/'.$entry->getId().'/tags', array('tags' => $newTags));
328 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
330 $content = json_decode($this->client
->getResponse()->getContent(), true);
332 $this->assertArrayHasKey('tags', $content);
333 $this->assertEquals($nbTags +
3, count($content['tags']));
335 $entryDB = $this->client
->getContainer()
336 ->get('doctrine.orm.entity_manager')
337 ->getRepository('WallabagCoreBundle:Entry')
338 ->find($entry->getId());
341 foreach ($entryDB->getTags()->toArray() as $tag) {
342 $tagsInDB[$tag->getId()] = $tag->getLabel();
345 foreach (explode(',', $newTags) as $tag) {
346 $this->assertContains($tag, $tagsInDB);
350 public function testDeleteOneTagEntry()
352 $entry = $this->client
->getContainer()
353 ->get('doctrine.orm.entity_manager')
354 ->getRepository('WallabagCoreBundle:Entry')
355 ->findOneWithTags(1);
359 $this->markTestSkipped('No content found in db.');
362 // hydrate the tags relations
363 $nbTags = count($entry->getTags());
364 $tag = $entry->getTags()[0];
366 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json');
368 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
370 $content = json_decode($this->client
->getResponse()->getContent(), true);
372 $this->assertArrayHasKey('tags', $content);
373 $this->assertEquals($nbTags - 1, count($content['tags']));
376 public function testGetUserTags()
378 $this->client
->request('GET', '/api/tags.json');
380 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
382 $content = json_decode($this->client
->getResponse()->getContent(), true);
384 $this->assertGreaterThan(0, $content);
385 $this->assertArrayHasKey('id', $content[0]);
386 $this->assertArrayHasKey('label', $content[0]);
388 return end($content);
392 * @depends testGetUserTags
394 public function testDeleteUserTag($tag)
396 $this->client
->request('DELETE', '/api/tags/'.$tag['id'].'.json');
398 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
400 $content = json_decode($this->client
->getResponse()->getContent(), true);
402 $this->assertArrayHasKey('label', $content);
403 $this->assertEquals($tag['label'], $content['label']);
404 $this->assertEquals($tag['slug'], $content['slug']);
406 $entries = $entry = $this->client
->getContainer()
407 ->get('doctrine.orm.entity_manager')
408 ->getRepository('WallabagCoreBundle:Entry')
409 ->findAllByTagId($this->user
->getId(), $tag['id']);
411 $this->assertCount(0, $entries);
414 public function testGetVersion()
416 $this->client
->request('GET', '/api/version');
418 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
420 $content = json_decode($this->client
->getResponse()->getContent(), true);
422 $this->assertEquals($this->client
->getContainer()->getParameter('wallabag_core.version'), $content);