]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ApiBundle/Tests/Controller/WallabagRestControllerTest.php
6bc7afa66f5fc613d2eefc8b1608a7ced53a3698
[github/wallabag/wallabag.git] / src / Wallabag / ApiBundle / Tests / Controller / WallabagRestControllerTest.php
1 <?php
2
3 namespace Wallabag\ApiBundle\Tests\Controller;
4
5 use Wallabag\ApiBundle\Tests\WallabagApiTestCase;
6
7 class WallabagRestControllerTest extends WallabagApiTestCase
8 {
9 protected static $salt;
10
11 public function testGetOneEntry()
12 {
13 $entry = $this->client->getContainer()
14 ->get('doctrine.orm.entity_manager')
15 ->getRepository('WallabagCoreBundle:Entry')
16 ->findOneBy(array('user' => 1, 'isArchived' => false));
17
18 if (!$entry) {
19 $this->markTestSkipped('No content found in db.');
20 }
21
22 $this->client->request('GET', '/api/entries/'.$entry->getId().'.json');
23 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
24
25 $content = json_decode($this->client->getResponse()->getContent(), true);
26
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']);
33
34 $this->assertTrue(
35 $this->client->getResponse()->headers->contains(
36 'Content-Type',
37 'application/json'
38 )
39 );
40 }
41
42 public function testGetOneEntryWrongUser()
43 {
44 $entry = $this->client->getContainer()
45 ->get('doctrine.orm.entity_manager')
46 ->getRepository('WallabagCoreBundle:Entry')
47 ->findOneBy(array('user' => 2, 'isArchived' => false));
48
49 if (!$entry) {
50 $this->markTestSkipped('No content found in db.');
51 }
52
53 $this->client->request('GET', '/api/entries/'.$entry->getId().'.json');
54
55 $this->assertEquals(403, $this->client->getResponse()->getStatusCode());
56 }
57
58 public function testGetEntries()
59 {
60 $this->client->request('GET', '/api/entries');
61
62 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
63
64 $content = json_decode($this->client->getResponse()->getContent(), true);
65
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']);
71
72 $this->assertTrue(
73 $this->client->getResponse()->headers->contains(
74 'Content-Type',
75 'application/json'
76 )
77 );
78 }
79
80 public function testGetStarredEntries()
81 {
82 $this->client->request('GET', '/api/entries', array('star' => 1, 'sort' => 'updated'));
83
84 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
85
86 $content = json_decode($this->client->getResponse()->getContent(), true);
87
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']);
93
94 $this->assertTrue(
95 $this->client->getResponse()->headers->contains(
96 'Content-Type',
97 'application/json'
98 )
99 );
100 }
101
102 public function testGetArchiveEntries()
103 {
104 $this->client->request('GET', '/api/entries', array('archive' => 1));
105
106 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
107
108 $content = json_decode($this->client->getResponse()->getContent(), true);
109
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']);
115
116 $this->assertTrue(
117 $this->client->getResponse()->headers->contains(
118 'Content-Type',
119 'application/json'
120 )
121 );
122 }
123
124 public function testDeleteEntry()
125 {
126 $entry = $this->client->getContainer()
127 ->get('doctrine.orm.entity_manager')
128 ->getRepository('WallabagCoreBundle:Entry')
129 ->findOneByUser(1);
130
131 if (!$entry) {
132 $this->markTestSkipped('No content found in db.');
133 }
134
135 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json');
136
137 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
138
139 $content = json_decode($this->client->getResponse()->getContent(), true);
140
141 $this->assertEquals($entry->getTitle(), $content['title']);
142 $this->assertEquals($entry->getUrl(), $content['url']);
143
144 // We'll try to delete this entry again
145 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json');
146
147 $this->assertEquals(404, $this->client->getResponse()->getStatusCode());
148 }
149
150 public function testPostEntry()
151 {
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',
154 'tags' => 'google',
155 ));
156
157 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
158
159 $content = json_decode($this->client->getResponse()->getContent(), true);
160
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']);
167 }
168
169 public function testPostSameEntry()
170 {
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',
173 'archive' => '1',
174 ));
175
176 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
177
178 $content = json_decode($this->client->getResponse()->getContent(), true);
179
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']);
185 }
186
187 public function testPostArchivedAndStarredEntry()
188 {
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',
191 'archive' => '1',
192 'starred' => '1',
193 ));
194
195 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
196
197 $content = json_decode($this->client->getResponse()->getContent(), true);
198
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']);
204 }
205
206 public function testPatchEntry()
207 {
208 $entry = $this->client->getContainer()
209 ->get('doctrine.orm.entity_manager')
210 ->getRepository('WallabagCoreBundle:Entry')
211 ->findOneByUser(1);
212
213 if (!$entry) {
214 $this->markTestSkipped('No content found in db.');
215 }
216
217 // hydrate the tags relations
218 $nbTags = count($entry->getTags());
219
220 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', array(
221 'title' => 'New awesome title',
222 'tags' => 'new tag '.uniqid(),
223 'starred' => '1',
224 'archive' => '0',
225 ));
226
227 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
228
229 $content = json_decode($this->client->getResponse()->getContent(), true);
230
231 $this->assertEquals($entry->getId(), $content['id']);
232 $this->assertEquals($entry->getUrl(), $content['url']);
233 $this->assertEquals('New awesome title', $content['title']);
234 $this->assertGreaterThan($nbTags, count($content['tags']));
235 $this->assertEquals(1, $content['user_id']);
236 }
237
238 public function testGetTagsEntry()
239 {
240 $entry = $this->client->getContainer()
241 ->get('doctrine.orm.entity_manager')
242 ->getRepository('WallabagCoreBundle:Entry')
243 ->findOneWithTags(1);
244
245 $entry = $entry[0];
246
247 if (!$entry) {
248 $this->markTestSkipped('No content found in db.');
249 }
250
251 $tags = array();
252 foreach ($entry->getTags() as $tag) {
253 $tags[] = array('id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug());
254 }
255
256 $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags');
257
258 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent());
259 }
260
261 public function testPostTagsOnEntry()
262 {
263 $entry = $this->client->getContainer()
264 ->get('doctrine.orm.entity_manager')
265 ->getRepository('WallabagCoreBundle:Entry')
266 ->findOneByUser(1);
267
268 if (!$entry) {
269 $this->markTestSkipped('No content found in db.');
270 }
271
272 $nbTags = count($entry->getTags());
273
274 $newTags = 'tag1,tag2,tag3';
275
276 $this->client->request('POST', '/api/entries/'.$entry->getId().'/tags', array('tags' => $newTags));
277
278 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
279
280 $content = json_decode($this->client->getResponse()->getContent(), true);
281
282 $this->assertArrayHasKey('tags', $content);
283 $this->assertEquals($nbTags + 3, count($content['tags']));
284
285 $entryDB = $this->client->getContainer()
286 ->get('doctrine.orm.entity_manager')
287 ->getRepository('WallabagCoreBundle:Entry')
288 ->find($entry->getId());
289
290 $tagsInDB = array();
291 foreach ($entryDB->getTags()->toArray() as $tag) {
292 $tagsInDB[$tag->getId()] = $tag->getLabel();
293 }
294
295 foreach (explode(',', $newTags) as $tag) {
296 $this->assertContains($tag, $tagsInDB);
297 }
298 }
299
300 public function testDeleteOneTagEntry()
301 {
302 $entry = $this->client->getContainer()
303 ->get('doctrine.orm.entity_manager')
304 ->getRepository('WallabagCoreBundle:Entry')
305 ->findOneWithTags(1);
306 $entry = $entry[0];
307
308 if (!$entry) {
309 $this->markTestSkipped('No content found in db.');
310 }
311
312 // hydrate the tags relations
313 $nbTags = count($entry->getTags());
314 $tag = $entry->getTags()[0];
315
316 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json');
317
318 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
319
320 $content = json_decode($this->client->getResponse()->getContent(), true);
321
322 $this->assertArrayHasKey('tags', $content);
323 $this->assertEquals($nbTags - 1, count($content['tags']));
324 }
325
326 public function testGetUserTags()
327 {
328 $this->client->request('GET', '/api/tags.json');
329
330 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
331
332 $content = json_decode($this->client->getResponse()->getContent(), true);
333
334 $this->assertGreaterThan(0, $content);
335 $this->assertArrayHasKey('id', $content[0]);
336 $this->assertArrayHasKey('label', $content[0]);
337
338 return end($content);
339 }
340
341 /**
342 * @depends testGetUserTags
343 */
344 public function testDeleteUserTag($tag)
345 {
346 $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json');
347
348 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
349
350 $content = json_decode($this->client->getResponse()->getContent(), true);
351
352 $this->assertArrayHasKey('label', $content);
353 $this->assertEquals($tag['label'], $content['label']);
354 $this->assertEquals($tag['slug'], $content['slug']);
355
356 $entries = $entry = $this->client->getContainer()
357 ->get('doctrine.orm.entity_manager')
358 ->getRepository('WallabagCoreBundle:Entry')
359 ->findAllByTagId($this->user->getId(), $tag['id']);
360
361 $this->assertCount(0, $entries);
362 }
363
364 public function testGetVersion()
365 {
366 $this->client->request('GET', '/api/version');
367
368 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
369
370 $content = json_decode($this->client->getResponse()->getContent(), true);
371
372 $this->assertEquals($this->client->getContainer()->getParameter('wallabag_core.version'), $content);
373 }
374 }