]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ApiBundle/Tests/Controller/WallabagRestControllerTest.php
check if archive/star parameters without quotes work
[github/wallabag/wallabag.git] / src / Wallabag / ApiBundle / Tests / Controller / WallabagRestControllerTest.php
CommitLineData
769e19dc
J
1<?php
2
9744e971 3namespace Wallabag\ApiBundle\Tests\Controller;
769e19dc 4
8a493541 5use Wallabag\ApiBundle\Tests\WallabagApiTestCase;
769e19dc 6
8a493541 7class WallabagRestControllerTest extends WallabagApiTestCase
769e19dc
J
8{
9 protected static $salt;
10
769e19dc
J
11 public function testGetOneEntry()
12 {
fcb1fba5 13 $entry = $this->client->getContainer()
769e19dc
J
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
fcb1fba5
NL
22 $this->client->request('GET', '/api/entries/'.$entry->getId().'.json');
23 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 24
fcb1fba5 25 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
26
27 $this->assertEquals($entry->getTitle(), $content['title']);
28 $this->assertEquals($entry->getUrl(), $content['url']);
29 $this->assertCount(count($entry->getTags()), $content['tags']);
bc44aa57
TC
30 $this->assertEquals($entry->getUserName(), $content['user_name']);
31 $this->assertEquals($entry->getUserEmail(), $content['user_email']);
32 $this->assertEquals($entry->getUserId(), $content['user_id']);
769e19dc
J
33
34 $this->assertTrue(
fcb1fba5 35 $this->client->getResponse()->headers->contains(
769e19dc
J
36 'Content-Type',
37 'application/json'
38 )
39 );
40 }
41
42 public function testGetOneEntryWrongUser()
43 {
fcb1fba5 44 $entry = $this->client->getContainer()
769e19dc
J
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
fcb1fba5 53 $this->client->request('GET', '/api/entries/'.$entry->getId().'.json');
769e19dc 54
fcb1fba5 55 $this->assertEquals(403, $this->client->getResponse()->getStatusCode());
769e19dc
J
56 }
57
58 public function testGetEntries()
59 {
fcb1fba5 60 $this->client->request('GET', '/api/entries');
769e19dc 61
fcb1fba5 62 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 63
fcb1fba5 64 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
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(
fcb1fba5 73 $this->client->getResponse()->headers->contains(
769e19dc
J
74 'Content-Type',
75 'application/json'
76 )
77 );
78 }
79
80 public function testGetStarredEntries()
81 {
fcb1fba5 82 $this->client->request('GET', '/api/entries', array('star' => 1, 'sort' => 'updated'));
769e19dc 83
fcb1fba5 84 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
e6f55346 85
fcb1fba5 86 $content = json_decode($this->client->getResponse()->getContent(), true);
e6f55346
JB
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(
fcb1fba5 95 $this->client->getResponse()->headers->contains(
e6f55346
JB
96 'Content-Type',
97 'application/json'
98 )
99 );
100 }
101
102 public function testGetArchiveEntries()
103 {
fcb1fba5 104 $this->client->request('GET', '/api/entries', array('archive' => 1));
769e19dc 105
fcb1fba5 106 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 107
fcb1fba5 108 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
109
110 $this->assertGreaterThanOrEqual(1, count($content));
9744e971
J
111 $this->assertNotEmpty($content['_embedded']['items']);
112 $this->assertGreaterThanOrEqual(1, $content['total']);
769e19dc 113 $this->assertEquals(1, $content['page']);
9744e971 114 $this->assertGreaterThanOrEqual(1, $content['pages']);
769e19dc
J
115
116 $this->assertTrue(
fcb1fba5 117 $this->client->getResponse()->headers->contains(
769e19dc
J
118 'Content-Type',
119 'application/json'
120 )
121 );
122 }
123
124 public function testDeleteEntry()
125 {
fcb1fba5 126 $entry = $this->client->getContainer()
769e19dc
J
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
fcb1fba5 135 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json');
769e19dc 136
fcb1fba5 137 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 138
fcb1fba5 139 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
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
fcb1fba5 145 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json');
769e19dc 146
fcb1fba5 147 $this->assertEquals(404, $this->client->getResponse()->getStatusCode());
769e19dc
J
148 }
149
150 public function testPostEntry()
151 {
fcb1fba5 152 $this->client->request('POST', '/api/entries.json', array(
769e19dc
J
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',
fcb1fba5 155 ));
769e19dc 156
fcb1fba5 157 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 158
fcb1fba5 159 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
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']);
bc44aa57 165 $this->assertEquals(1, $content['user_id']);
769e19dc
J
166 $this->assertCount(1, $content['tags']);
167 }
168
3107f92a
TC
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
09d8bb6f 187 public function testPostArchivedAndStarredEntry()
816ad405
TC
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',
189ef634
TC
191 'archive' => '1',
192 'starred' => '1',
816ad405
TC
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']);
09d8bb6f 202 $this->assertEquals(true, $content['is_starred']);
bc44aa57 203 $this->assertEquals(1, $content['user_id']);
816ad405
TC
204 }
205
2f60e5ea
TC
206 public function testPostArchivedAndStarredEntryWithoutQuotes()
207 {
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',
210 'archive' => 0,
211 'starred' => 1,
212 ));
213
214 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
215
216 $content = json_decode($this->client->getResponse()->getContent(), true);
217
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']);
222 }
223
769e19dc
J
224 public function testPatchEntry()
225 {
fcb1fba5 226 $entry = $this->client->getContainer()
769e19dc
J
227 ->get('doctrine.orm.entity_manager')
228 ->getRepository('WallabagCoreBundle:Entry')
229 ->findOneByUser(1);
230
231 if (!$entry) {
232 $this->markTestSkipped('No content found in db.');
233 }
234
235 // hydrate the tags relations
236 $nbTags = count($entry->getTags());
237
fcb1fba5 238 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', array(
769e19dc
J
239 'title' => 'New awesome title',
240 'tags' => 'new tag '.uniqid(),
189ef634
TC
241 'starred' => '1',
242 'archive' => '0',
fcb1fba5 243 ));
769e19dc 244
fcb1fba5 245 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 246
fcb1fba5 247 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
248
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']));
bc44aa57 253 $this->assertEquals(1, $content['user_id']);
769e19dc
J
254 }
255
2f60e5ea
TC
256 public function testPatchEntryWithoutQuotes()
257 {
258 $entry = $this->client->getContainer()
259 ->get('doctrine.orm.entity_manager')
260 ->getRepository('WallabagCoreBundle:Entry')
261 ->findOneByUser(1);
262
263 if (!$entry) {
264 $this->markTestSkipped('No content found in db.');
265 }
266
267 // hydrate the tags relations
268 $nbTags = count($entry->getTags());
269
270 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', array(
271 'title' => 'New awesome title',
272 'tags' => 'new tag '.uniqid(),
273 'starred' => 1,
274 'archive' => 0,
275 ));
276
277 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
278
279 $content = json_decode($this->client->getResponse()->getContent(), true);
280
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']));
285 }
286
769e19dc
J
287 public function testGetTagsEntry()
288 {
fcb1fba5 289 $entry = $this->client->getContainer()
769e19dc
J
290 ->get('doctrine.orm.entity_manager')
291 ->getRepository('WallabagCoreBundle:Entry')
292 ->findOneWithTags(1);
293
294 $entry = $entry[0];
295
296 if (!$entry) {
297 $this->markTestSkipped('No content found in db.');
298 }
299
300 $tags = array();
301 foreach ($entry->getTags() as $tag) {
fc732227 302 $tags[] = array('id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug());
769e19dc
J
303 }
304
fcb1fba5 305 $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags');
769e19dc 306
fcb1fba5 307 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent());
769e19dc
J
308 }
309
310 public function testPostTagsOnEntry()
311 {
fcb1fba5 312 $entry = $this->client->getContainer()
769e19dc
J
313 ->get('doctrine.orm.entity_manager')
314 ->getRepository('WallabagCoreBundle:Entry')
315 ->findOneByUser(1);
316
317 if (!$entry) {
318 $this->markTestSkipped('No content found in db.');
319 }
320
321 $nbTags = count($entry->getTags());
322
323 $newTags = 'tag1,tag2,tag3';
324
fcb1fba5 325 $this->client->request('POST', '/api/entries/'.$entry->getId().'/tags', array('tags' => $newTags));
769e19dc 326
fcb1fba5 327 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 328
fcb1fba5 329 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
330
331 $this->assertArrayHasKey('tags', $content);
4346a860 332 $this->assertEquals($nbTags + 3, count($content['tags']));
769e19dc 333
fcb1fba5 334 $entryDB = $this->client->getContainer()
769e19dc
J
335 ->get('doctrine.orm.entity_manager')
336 ->getRepository('WallabagCoreBundle:Entry')
337 ->find($entry->getId());
338
339 $tagsInDB = array();
340 foreach ($entryDB->getTags()->toArray() as $tag) {
341 $tagsInDB[$tag->getId()] = $tag->getLabel();
342 }
343
344 foreach (explode(',', $newTags) as $tag) {
345 $this->assertContains($tag, $tagsInDB);
346 }
347 }
348
fcb1fba5 349 public function testDeleteOneTagEntry()
769e19dc 350 {
fcb1fba5 351 $entry = $this->client->getContainer()
769e19dc
J
352 ->get('doctrine.orm.entity_manager')
353 ->getRepository('WallabagCoreBundle:Entry')
fcb1fba5
NL
354 ->findOneWithTags(1);
355 $entry = $entry[0];
769e19dc
J
356
357 if (!$entry) {
358 $this->markTestSkipped('No content found in db.');
359 }
360
361 // hydrate the tags relations
362 $nbTags = count($entry->getTags());
363 $tag = $entry->getTags()[0];
364
fcb1fba5 365 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json');
769e19dc 366
fcb1fba5 367 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 368
fcb1fba5 369 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
370
371 $this->assertArrayHasKey('tags', $content);
4346a860 372 $this->assertEquals($nbTags - 1, count($content['tags']));
769e19dc
J
373 }
374
375 public function testGetUserTags()
376 {
fcb1fba5 377 $this->client->request('GET', '/api/tags.json');
769e19dc 378
fcb1fba5 379 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 380
fcb1fba5 381 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
382
383 $this->assertGreaterThan(0, $content);
384 $this->assertArrayHasKey('id', $content[0]);
385 $this->assertArrayHasKey('label', $content[0]);
386
387 return end($content);
388 }
389
390 /**
391 * @depends testGetUserTags
392 */
393 public function testDeleteUserTag($tag)
394 {
fcb1fba5 395 $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json');
769e19dc 396
fcb1fba5 397 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
769e19dc 398
fcb1fba5 399 $content = json_decode($this->client->getResponse()->getContent(), true);
769e19dc
J
400
401 $this->assertArrayHasKey('label', $content);
402 $this->assertEquals($tag['label'], $content['label']);
fc732227 403 $this->assertEquals($tag['slug'], $content['slug']);
4059a061
JB
404
405 $entries = $entry = $this->client->getContainer()
406 ->get('doctrine.orm.entity_manager')
407 ->getRepository('WallabagCoreBundle:Entry')
408 ->findAllByTagId($this->user->getId(), $tag['id']);
409
410 $this->assertCount(0, $entries);
769e19dc 411 }
9761bfa1 412
6f8310b4
TC
413 public function testGetVersion()
414 {
415 $this->client->request('GET', '/api/version');
9761bfa1
V
416
417 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
418
419 $content = json_decode($this->client->getResponse()->getContent(), true);
420
6f8310b4 421 $this->assertEquals($this->client->getContainer()->getParameter('wallabag_core.version'), $content);
9761bfa1 422 }
769e19dc 423}