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']);
32 $this->client
->getResponse()->headers
->contains(
39 public function testGetOneEntryWrongUser()
41 $entry = $this->client
->getContainer()
42 ->get('doctrine.orm.entity_manager')
43 ->getRepository('WallabagCoreBundle:Entry')
44 ->findOneBy(array('user' => 2, 'isArchived' => false));
47 $this->markTestSkipped('No content found in db.');
50 $this->client
->request('GET', '/api/entries/'.$entry->getId().'.json');
52 $this->assertEquals(403, $this->client
->getResponse()->getStatusCode());
55 public function testGetEntries()
57 $this->client
->request('GET', '/api/entries');
59 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
61 $content = json_decode($this->client
->getResponse()->getContent(), true);
63 $this->assertGreaterThanOrEqual(1, count($content));
64 $this->assertNotEmpty($content['_embedded']['items']);
65 $this->assertGreaterThanOrEqual(1, $content['total']);
66 $this->assertEquals(1, $content['page']);
67 $this->assertGreaterThanOrEqual(1, $content['pages']);
70 $this->client
->getResponse()->headers
->contains(
77 public function testGetStarredEntries()
79 $this->client
->request('GET', '/api/entries', array('star' => 1, 'sort' => 'updated'));
81 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
83 $content = json_decode($this->client
->getResponse()->getContent(), true);
85 $this->assertGreaterThanOrEqual(1, count($content));
86 $this->assertNotEmpty($content['_embedded']['items']);
87 $this->assertGreaterThanOrEqual(1, $content['total']);
88 $this->assertEquals(1, $content['page']);
89 $this->assertGreaterThanOrEqual(1, $content['pages']);
92 $this->client
->getResponse()->headers
->contains(
99 public function testGetArchiveEntries()
101 $this->client
->request('GET', '/api/entries', array('archive' => 1));
103 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
105 $content = json_decode($this->client
->getResponse()->getContent(), true);
107 $this->assertGreaterThanOrEqual(1, count($content));
108 $this->assertNotEmpty($content['_embedded']['items']);
109 $this->assertGreaterThanOrEqual(1, $content['total']);
110 $this->assertEquals(1, $content['page']);
111 $this->assertGreaterThanOrEqual(1, $content['pages']);
114 $this->client
->getResponse()->headers
->contains(
121 public function testDeleteEntry()
123 $entry = $this->client
->getContainer()
124 ->get('doctrine.orm.entity_manager')
125 ->getRepository('WallabagCoreBundle:Entry')
129 $this->markTestSkipped('No content found in db.');
132 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'.json');
134 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
136 $content = json_decode($this->client
->getResponse()->getContent(), true);
138 $this->assertEquals($entry->getTitle(), $content['title']);
139 $this->assertEquals($entry->getUrl(), $content['url']);
141 // We'll try to delete this entry again
142 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'.json');
144 $this->assertEquals(404, $this->client
->getResponse()->getStatusCode());
147 public function testPostEntry()
149 $this->client
->request('POST', '/api/entries.json', array(
150 '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',
154 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
156 $content = json_decode($this->client
->getResponse()->getContent(), true);
158 $this->assertGreaterThan(0, $content['id']);
159 $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']);
160 $this->assertEquals(false, $content['is_archived']);
161 $this->assertEquals(false, $content['is_starred']);
162 $this->assertCount(1, $content['tags']);
165 public function testPostArchivedEntry()
167 $this->client
->request('POST', '/api/entries.json', array(
168 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
173 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
175 $content = json_decode($this->client
->getResponse()->getContent(), true);
177 $this->assertGreaterThan(0, $content['id']);
178 $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']);
179 $this->assertEquals(true, $content['is_archived']);
180 $this->assertEquals(false, $content['is_starred']);
183 public function testPatchEntry()
185 $entry = $this->client
->getContainer()
186 ->get('doctrine.orm.entity_manager')
187 ->getRepository('WallabagCoreBundle:Entry')
191 $this->markTestSkipped('No content found in db.');
194 // hydrate the tags relations
195 $nbTags = count($entry->getTags());
197 $this->client
->request('PATCH', '/api/entries/'.$entry->getId().'.json', array(
198 'title' => 'New awesome title',
199 'tags' => 'new tag '.uniqid(),
204 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
206 $content = json_decode($this->client
->getResponse()->getContent(), true);
208 $this->assertEquals($entry->getId(), $content['id']);
209 $this->assertEquals($entry->getUrl(), $content['url']);
210 $this->assertEquals('New awesome title', $content['title']);
211 $this->assertGreaterThan($nbTags, count($content['tags']));
214 public function testGetTagsEntry()
216 $entry = $this->client
->getContainer()
217 ->get('doctrine.orm.entity_manager')
218 ->getRepository('WallabagCoreBundle:Entry')
219 ->findOneWithTags(1);
224 $this->markTestSkipped('No content found in db.');
228 foreach ($entry->getTags() as $tag) {
229 $tags[] = array('id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug());
232 $this->client
->request('GET', '/api/entries/'.$entry->getId().'/tags');
234 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT
), $this->client
->getResponse()->getContent());
237 public function testPostTagsOnEntry()
239 $entry = $this->client
->getContainer()
240 ->get('doctrine.orm.entity_manager')
241 ->getRepository('WallabagCoreBundle:Entry')
245 $this->markTestSkipped('No content found in db.');
248 $nbTags = count($entry->getTags());
250 $newTags = 'tag1,tag2,tag3';
252 $this->client
->request('POST', '/api/entries/'.$entry->getId().'/tags', array('tags' => $newTags));
254 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
256 $content = json_decode($this->client
->getResponse()->getContent(), true);
258 $this->assertArrayHasKey('tags', $content);
259 $this->assertEquals($nbTags +
3, count($content['tags']));
261 $entryDB = $this->client
->getContainer()
262 ->get('doctrine.orm.entity_manager')
263 ->getRepository('WallabagCoreBundle:Entry')
264 ->find($entry->getId());
267 foreach ($entryDB->getTags()->toArray() as $tag) {
268 $tagsInDB[$tag->getId()] = $tag->getLabel();
271 foreach (explode(',', $newTags) as $tag) {
272 $this->assertContains($tag, $tagsInDB);
276 public function testDeleteOneTagEntry()
278 $entry = $this->client
->getContainer()
279 ->get('doctrine.orm.entity_manager')
280 ->getRepository('WallabagCoreBundle:Entry')
281 ->findOneWithTags(1);
285 $this->markTestSkipped('No content found in db.');
288 // hydrate the tags relations
289 $nbTags = count($entry->getTags());
290 $tag = $entry->getTags()[0];
292 $this->client
->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json');
294 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
296 $content = json_decode($this->client
->getResponse()->getContent(), true);
298 $this->assertArrayHasKey('tags', $content);
299 $this->assertEquals($nbTags - 1, count($content['tags']));
302 public function testGetUserTags()
304 $this->client
->request('GET', '/api/tags.json');
306 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
308 $content = json_decode($this->client
->getResponse()->getContent(), true);
310 $this->assertGreaterThan(0, $content);
311 $this->assertArrayHasKey('id', $content[0]);
312 $this->assertArrayHasKey('label', $content[0]);
314 return end($content);
318 * @depends testGetUserTags
320 public function testDeleteUserTag($tag)
322 $this->client
->request('DELETE', '/api/tags/'.$tag['id'].'.json');
324 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
326 $content = json_decode($this->client
->getResponse()->getContent(), true);
328 $this->assertArrayHasKey('label', $content);
329 $this->assertEquals($tag['label'], $content['label']);
330 $this->assertEquals($tag['slug'], $content['slug']);
332 $entries = $entry = $this->client
->getContainer()
333 ->get('doctrine.orm.entity_manager')
334 ->getRepository('WallabagCoreBundle:Entry')
335 ->findAllByTagId($this->user
->getId(), $tag['id']);
337 $this->assertCount(0, $entries);