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',
176 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
178 $content = json_decode($this->client
->getResponse()->getContent(), true);
180 $this->assertGreaterThan(0, $content['id']);
181 $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']);
182 $this->assertEquals(true, $content['is_archived']);
183 $this->assertEquals(false, $content['is_starred']);
184 $this->assertCount(1, $content['tags']);
187 public function testPostArchivedAndStarredEntry()
189 $this->client
->request('POST', '/api/entries.json', array(
190 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
195 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
197 $content = json_decode($this->client
->getResponse()->getContent(), true);
199 $this->assertGreaterThan(0, $content['id']);
200 $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']);
201 $this->assertEquals(true, $content['is_archived']);
202 $this->assertEquals(true, $content['is_starred']);
203 $this->assertEquals(1, $content['user_id']);
206 public function testPostArchivedAndStarredEntryWithoutQuotes()
208 $this->client
->request('POST', '/api/entries.json', array(
209 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
214 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
216 $content = json_decode($this->client
->getResponse()->getContent(), true);
218 $this->assertGreaterThan(0, $content['id']);
219 $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']);
220 $this->assertEquals(false, $content['is_archived']);
221 $this->assertEquals(true, $content['is_starred']);
224 public function testPatchEntry()
226 $entry = $this->client
->getContainer()
227 ->get('doctrine.orm.entity_manager')
228 ->getRepository('WallabagCoreBundle:Entry')
232 $this->markTestSkipped('No content found in db.');
235 // hydrate the tags relations
236 $nbTags = count($entry->getTags());
238 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', array(
239 'title' => 'New awesome title',
240 'tags' => 'new tag '.uniqid(),
245 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
247 $content = json_decode($this->client
->getResponse()->getContent(), true);
249 $this->assertEquals($entry->getId(), $content['id']);
250 $this->assertEquals($entry->getUrl(), $content['url']);
251 $this->assertEquals('New awesome title', $content['title']);
252 $this->assertGreaterThan($nbTags, count($content['tags']));
253 $this->assertEquals(1, $content['user_id']);
256 public function testPatchEntryWithoutQuotes()
258 $entry = $this->client
->getContainer()
259 ->get('doctrine.orm.entity_manager')
260 ->getRepository('WallabagCoreBundle:Entry')
264 $this->markTestSkipped('No content found in db.');
267 // hydrate the tags relations
268 $nbTags = count($entry->getTags());
270 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', array(
271 'title' => 'New awesome title',
272 'tags' => 'new tag '.uniqid(),
277 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
279 $content = json_decode($this->client
->getResponse()->getContent(), true);
281 $this->assertEquals($entry->getId(), $content['id']);
282 $this->assertEquals($entry->getUrl(), $content['url']);
283 $this->assertEquals('New awesome title', $content['title']);
284 $this->assertGreaterThan($nbTags, count($content['tags']));
287 public function testGetTagsEntry()
289 $entry = $this->client
->getContainer()
290 ->get('doctrine.orm.entity_manager')
291 ->getRepository('WallabagCoreBundle:Entry')
292 ->findOneWithTags(1);
297 $this->markTestSkipped('No content found in db.');
301 foreach ($entry->getTags() as $tag) {
302 $tags[] = array('id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug());
305 $this->client
->request('GET', '/api/entries/'.$entry->getId().'/tags');
307 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT
), $this->client
->getResponse()->getContent());
310 public function testPostTagsOnEntry()
312 $entry = $this->client
->getContainer()
313 ->get('doctrine.orm.entity_manager')
314 ->getRepository('WallabagCoreBundle:Entry')
318 $this->markTestSkipped('No content found in db.');
321 $nbTags = count($entry->getTags());
323 $newTags = 'tag1,tag2,tag3';
325 $this->client
->request('POST', '/api/entries/'.$entry->getId().'/tags', array('tags' => $newTags));
327 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
329 $content = json_decode($this->client
->getResponse()->getContent(), true);
331 $this->assertArrayHasKey('tags', $content);
332 $this->assertEquals($nbTags +
3, count($content['tags']));
334 $entryDB = $this->client
->getContainer()
335 ->get('doctrine.orm.entity_manager')
336 ->getRepository('WallabagCoreBundle:Entry')
337 ->find($entry->getId());
340 foreach ($entryDB->getTags()->toArray() as $tag) {
341 $tagsInDB[$tag->getId()] = $tag->getLabel();
344 foreach (explode(',', $newTags) as $tag) {
345 $this->assertContains($tag, $tagsInDB);
349 public function testDeleteOneTagEntry()
351 $entry = $this->client
->getContainer()
352 ->get('doctrine.orm.entity_manager')
353 ->getRepository('WallabagCoreBundle:Entry')
354 ->findOneWithTags(1);
358 $this->markTestSkipped('No content found in db.');
361 // hydrate the tags relations
362 $nbTags = count($entry->getTags());
363 $tag = $entry->getTags()[0];
365 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json');
367 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
369 $content = json_decode($this->client
->getResponse()->getContent(), true);
371 $this->assertArrayHasKey('tags', $content);
372 $this->assertEquals($nbTags - 1, count($content['tags']));
375 public function testGetUserTags()
377 $this->client
->request('GET', '/api/tags.json');
379 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
381 $content = json_decode($this->client
->getResponse()->getContent(), true);
383 $this->assertGreaterThan(0, $content);
384 $this->assertArrayHasKey('id', $content[0]);
385 $this->assertArrayHasKey('label', $content[0]);
387 return end($content);
391 * @depends testGetUserTags
393 public function testDeleteUserTag($tag)
395 $this->client
->request('DELETE', '/api/tags/'.$tag['id'].'.json');
397 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
399 $content = json_decode($this->client
->getResponse()->getContent(), true);
401 $this->assertArrayHasKey('label', $content);
402 $this->assertEquals($tag['label'], $content['label']);
403 $this->assertEquals($tag['slug'], $content['slug']);
405 $entries = $entry = $this->client
->getContainer()
406 ->get('doctrine.orm.entity_manager')
407 ->getRepository('WallabagCoreBundle:Entry')
408 ->findAllByTagId($this->user
->getId(), $tag['id']);
410 $this->assertCount(0, $entries);
413 public function testGetVersion()
415 $this->client
->request('GET', '/api/version');
417 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
419 $content = json_decode($this->client
->getResponse()->getContent(), true);
421 $this->assertEquals($this->client
->getContainer()->getParameter('wallabag_core.version'), $content);