]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ApiBundle/Tests/Controller/WallabagRestControllerTest.php
add more properties for entries #1634
[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
31 $this->assertTrue(
32 $this->client->getResponse()->headers->contains(
33 'Content-Type',
34 'application/json'
35 )
36 );
37 }
38
39 public function testGetOneEntryWrongUser()
40 {
41 $entry = $this->client->getContainer()
42 ->get('doctrine.orm.entity_manager')
43 ->getRepository('WallabagCoreBundle:Entry')
44 ->findOneBy(array('user' => 2, 'isArchived' => false));
45
46 if (!$entry) {
47 $this->markTestSkipped('No content found in db.');
48 }
49
50 $this->client->request('GET', '/api/entries/'.$entry->getId().'.json');
51
52 $this->assertEquals(403, $this->client->getResponse()->getStatusCode());
53 }
54
55 public function testGetEntries()
56 {
57 $this->client->request('GET', '/api/entries');
58
59 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
60
61 $content = json_decode($this->client->getResponse()->getContent(), true);
62
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']);
68
69 $this->assertTrue(
70 $this->client->getResponse()->headers->contains(
71 'Content-Type',
72 'application/json'
73 )
74 );
75 }
76
77 public function testGetStarredEntries()
78 {
79 $this->client->request('GET', '/api/entries', array('star' => 1, 'sort' => 'updated'));
80
81 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
82
83 $content = json_decode($this->client->getResponse()->getContent(), true);
84
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']);
90
91 $this->assertTrue(
92 $this->client->getResponse()->headers->contains(
93 'Content-Type',
94 'application/json'
95 )
96 );
97 }
98
99 public function testGetArchiveEntries()
100 {
101 $this->client->request('GET', '/api/entries', array('archive' => 1));
102
103 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
104
105 $content = json_decode($this->client->getResponse()->getContent(), true);
106
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']);
112
113 $this->assertTrue(
114 $this->client->getResponse()->headers->contains(
115 'Content-Type',
116 'application/json'
117 )
118 );
119 }
120
121 public function testDeleteEntry()
122 {
123 $entry = $this->client->getContainer()
124 ->get('doctrine.orm.entity_manager')
125 ->getRepository('WallabagCoreBundle:Entry')
126 ->findOneByUser(1);
127
128 if (!$entry) {
129 $this->markTestSkipped('No content found in db.');
130 }
131
132 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json');
133
134 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
135
136 $content = json_decode($this->client->getResponse()->getContent(), true);
137
138 $this->assertEquals($entry->getTitle(), $content['title']);
139 $this->assertEquals($entry->getUrl(), $content['url']);
140
141 // We'll try to delete this entry again
142 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json');
143
144 $this->assertEquals(404, $this->client->getResponse()->getStatusCode());
145 }
146
147 public function testPostEntry()
148 {
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',
151 'tags' => 'google',
152 ));
153
154 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
155
156 $content = json_decode($this->client->getResponse()->getContent(), true);
157
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']);
163 }
164
165 public function testPostArchivedEntry()
166 {
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',
169 'archive' => true,
170 'starred' => false,
171 ));
172
173 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
174
175 $content = json_decode($this->client->getResponse()->getContent(), true);
176
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']);
181 }
182
183 public function testPostEntryWithContent()
184 {
185 $this->client->request('POST', '/api/entries.json', array(
186 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html',
187 'content' => 'This is a new content for my entry',
188 ));
189
190 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
191
192 $content = json_decode($this->client->getResponse()->getContent(), true);
193
194 $this->assertGreaterThan(0, $content['id']);
195 $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']);
196 $this->assertEquals('This is a new content for my entry', $content['content']);
197 }
198
199 public function testPatchEntry()
200 {
201 $entry = $this->client->getContainer()
202 ->get('doctrine.orm.entity_manager')
203 ->getRepository('WallabagCoreBundle:Entry')
204 ->findOneByUser(1);
205
206 if (!$entry) {
207 $this->markTestSkipped('No content found in db.');
208 }
209
210 // hydrate the tags relations
211 $nbTags = count($entry->getTags());
212
213 $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', array(
214 'title' => 'New awesome title',
215 'tags' => 'new tag '.uniqid(),
216 'star' => true,
217 'archive' => false,
218 ));
219
220 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
221
222 $content = json_decode($this->client->getResponse()->getContent(), true);
223
224 $this->assertEquals($entry->getId(), $content['id']);
225 $this->assertEquals($entry->getUrl(), $content['url']);
226 $this->assertEquals('New awesome title', $content['title']);
227 $this->assertGreaterThan($nbTags, count($content['tags']));
228 }
229
230 public function testGetTagsEntry()
231 {
232 $entry = $this->client->getContainer()
233 ->get('doctrine.orm.entity_manager')
234 ->getRepository('WallabagCoreBundle:Entry')
235 ->findOneWithTags(1);
236
237 $entry = $entry[0];
238
239 if (!$entry) {
240 $this->markTestSkipped('No content found in db.');
241 }
242
243 $tags = array();
244 foreach ($entry->getTags() as $tag) {
245 $tags[] = array('id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug());
246 }
247
248 $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags');
249
250 $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent());
251 }
252
253 public function testPostTagsOnEntry()
254 {
255 $entry = $this->client->getContainer()
256 ->get('doctrine.orm.entity_manager')
257 ->getRepository('WallabagCoreBundle:Entry')
258 ->findOneByUser(1);
259
260 if (!$entry) {
261 $this->markTestSkipped('No content found in db.');
262 }
263
264 $nbTags = count($entry->getTags());
265
266 $newTags = 'tag1,tag2,tag3';
267
268 $this->client->request('POST', '/api/entries/'.$entry->getId().'/tags', array('tags' => $newTags));
269
270 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
271
272 $content = json_decode($this->client->getResponse()->getContent(), true);
273
274 $this->assertArrayHasKey('tags', $content);
275 $this->assertEquals($nbTags + 3, count($content['tags']));
276
277 $entryDB = $this->client->getContainer()
278 ->get('doctrine.orm.entity_manager')
279 ->getRepository('WallabagCoreBundle:Entry')
280 ->find($entry->getId());
281
282 $tagsInDB = array();
283 foreach ($entryDB->getTags()->toArray() as $tag) {
284 $tagsInDB[$tag->getId()] = $tag->getLabel();
285 }
286
287 foreach (explode(',', $newTags) as $tag) {
288 $this->assertContains($tag, $tagsInDB);
289 }
290 }
291
292 public function testDeleteOneTagEntry()
293 {
294 $entry = $this->client->getContainer()
295 ->get('doctrine.orm.entity_manager')
296 ->getRepository('WallabagCoreBundle:Entry')
297 ->findOneWithTags(1);
298 $entry = $entry[0];
299
300 if (!$entry) {
301 $this->markTestSkipped('No content found in db.');
302 }
303
304 // hydrate the tags relations
305 $nbTags = count($entry->getTags());
306 $tag = $entry->getTags()[0];
307
308 $this->client->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json');
309
310 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
311
312 $content = json_decode($this->client->getResponse()->getContent(), true);
313
314 $this->assertArrayHasKey('tags', $content);
315 $this->assertEquals($nbTags - 1, count($content['tags']));
316 }
317
318 public function testGetUserTags()
319 {
320 $this->client->request('GET', '/api/tags.json');
321
322 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
323
324 $content = json_decode($this->client->getResponse()->getContent(), true);
325
326 $this->assertGreaterThan(0, $content);
327 $this->assertArrayHasKey('id', $content[0]);
328 $this->assertArrayHasKey('label', $content[0]);
329
330 return end($content);
331 }
332
333 /**
334 * @depends testGetUserTags
335 */
336 public function testDeleteUserTag($tag)
337 {
338 $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json');
339
340 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
341
342 $content = json_decode($this->client->getResponse()->getContent(), true);
343
344 $this->assertArrayHasKey('label', $content);
345 $this->assertEquals($tag['label'], $content['label']);
346 $this->assertEquals($tag['slug'], $content['slug']);
347
348 $entries = $entry = $this->client->getContainer()
349 ->get('doctrine.orm.entity_manager')
350 ->getRepository('WallabagCoreBundle:Entry')
351 ->findAllByTagId($this->user->getId(), $tag['id']);
352
353 $this->assertCount(0, $entries);
354 }
355 }