]>
Commit | Line | Data |
---|---|---|
769e19dc J |
1 | <?php |
2 | ||
23634d5d | 3 | namespace Tests\Wallabag\ApiBundle\Controller; |
769e19dc | 4 | |
23634d5d | 5 | use Tests\Wallabag\ApiBundle\WallabagApiTestCase; |
a0e1eafc | 6 | use Wallabag\CoreBundle\Entity\Tag; |
769e19dc | 7 | |
8a493541 | 8 | class WallabagRestControllerTest extends WallabagApiTestCase |
769e19dc J |
9 | { |
10 | protected static $salt; | |
11 | ||
769e19dc J |
12 | public function testGetOneEntry() |
13 | { | |
fcb1fba5 | 14 | $entry = $this->client->getContainer() |
769e19dc J |
15 | ->get('doctrine.orm.entity_manager') |
16 | ->getRepository('WallabagCoreBundle:Entry') | |
4094ea47 | 17 | ->findOneBy(['user' => 1, 'isArchived' => false]); |
769e19dc J |
18 | |
19 | if (!$entry) { | |
20 | $this->markTestSkipped('No content found in db.'); | |
21 | } | |
22 | ||
fcb1fba5 NL |
23 | $this->client->request('GET', '/api/entries/'.$entry->getId().'.json'); |
24 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
769e19dc | 25 | |
fcb1fba5 | 26 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
27 | |
28 | $this->assertEquals($entry->getTitle(), $content['title']); | |
29 | $this->assertEquals($entry->getUrl(), $content['url']); | |
30 | $this->assertCount(count($entry->getTags()), $content['tags']); | |
bc44aa57 TC |
31 | $this->assertEquals($entry->getUserName(), $content['user_name']); |
32 | $this->assertEquals($entry->getUserEmail(), $content['user_email']); | |
33 | $this->assertEquals($entry->getUserId(), $content['user_id']); | |
769e19dc J |
34 | |
35 | $this->assertTrue( | |
fcb1fba5 | 36 | $this->client->getResponse()->headers->contains( |
769e19dc J |
37 | 'Content-Type', |
38 | 'application/json' | |
39 | ) | |
40 | ); | |
41 | } | |
42 | ||
43 | public function testGetOneEntryWrongUser() | |
44 | { | |
fcb1fba5 | 45 | $entry = $this->client->getContainer() |
769e19dc J |
46 | ->get('doctrine.orm.entity_manager') |
47 | ->getRepository('WallabagCoreBundle:Entry') | |
4094ea47 | 48 | ->findOneBy(['user' => 2, 'isArchived' => false]); |
769e19dc J |
49 | |
50 | if (!$entry) { | |
51 | $this->markTestSkipped('No content found in db.'); | |
52 | } | |
53 | ||
fcb1fba5 | 54 | $this->client->request('GET', '/api/entries/'.$entry->getId().'.json'); |
769e19dc | 55 | |
fcb1fba5 | 56 | $this->assertEquals(403, $this->client->getResponse()->getStatusCode()); |
769e19dc J |
57 | } |
58 | ||
59 | public function testGetEntries() | |
60 | { | |
fcb1fba5 | 61 | $this->client->request('GET', '/api/entries'); |
769e19dc | 62 | |
fcb1fba5 | 63 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 64 | |
fcb1fba5 | 65 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
66 | |
67 | $this->assertGreaterThanOrEqual(1, count($content)); | |
68 | $this->assertNotEmpty($content['_embedded']['items']); | |
69 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
70 | $this->assertEquals(1, $content['page']); | |
71 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
72 | ||
73 | $this->assertTrue( | |
fcb1fba5 | 74 | $this->client->getResponse()->headers->contains( |
769e19dc J |
75 | 'Content-Type', |
76 | 'application/json' | |
77 | ) | |
78 | ); | |
79 | } | |
80 | ||
c3f8b428 JB |
81 | public function testGetEntriesWithFullOptions() |
82 | { | |
83 | $this->client->request('GET', '/api/entries', [ | |
84 | 'archive' => 1, | |
85 | 'starred' => 1, | |
86 | 'sort' => 'updated', | |
87 | 'order' => 'asc', | |
88 | 'page' => 1, | |
89 | 'perPage' => 2, | |
90 | 'tags' => 'foo', | |
91 | 'since' => 1443274283, | |
92 | ]); | |
93 | ||
94 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
95 | ||
96 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
97 | ||
98 | $this->assertGreaterThanOrEqual(1, count($content)); | |
99 | $this->assertArrayHasKey('items', $content['_embedded']); | |
100 | $this->assertGreaterThanOrEqual(0, $content['total']); | |
101 | $this->assertEquals(1, $content['page']); | |
102 | $this->assertEquals(2, $content['limit']); | |
103 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
104 | ||
105 | $this->assertArrayHasKey('_links', $content); | |
106 | $this->assertArrayHasKey('self', $content['_links']); | |
107 | $this->assertArrayHasKey('first', $content['_links']); | |
108 | $this->assertArrayHasKey('last', $content['_links']); | |
109 | ||
110 | foreach (['self', 'first', 'last'] as $link) { | |
111 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
112 | $this->assertContains('archive=1', $content['_links'][$link]['href']); | |
113 | $this->assertContains('starred=1', $content['_links'][$link]['href']); | |
114 | $this->assertContains('sort=updated', $content['_links'][$link]['href']); | |
115 | $this->assertContains('order=asc', $content['_links'][$link]['href']); | |
116 | $this->assertContains('tags=foo', $content['_links'][$link]['href']); | |
117 | $this->assertContains('since=1443274283', $content['_links'][$link]['href']); | |
118 | } | |
119 | ||
120 | $this->assertTrue( | |
121 | $this->client->getResponse()->headers->contains( | |
122 | 'Content-Type', | |
123 | 'application/json' | |
124 | ) | |
125 | ); | |
126 | } | |
127 | ||
769e19dc J |
128 | public function testGetStarredEntries() |
129 | { | |
f0fd82d0 | 130 | $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']); |
769e19dc | 131 | |
fcb1fba5 | 132 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
e6f55346 | 133 | |
fcb1fba5 | 134 | $content = json_decode($this->client->getResponse()->getContent(), true); |
e6f55346 JB |
135 | |
136 | $this->assertGreaterThanOrEqual(1, count($content)); | |
137 | $this->assertNotEmpty($content['_embedded']['items']); | |
138 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
139 | $this->assertEquals(1, $content['page']); | |
140 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
141 | ||
c3f8b428 JB |
142 | $this->assertArrayHasKey('_links', $content); |
143 | $this->assertArrayHasKey('self', $content['_links']); | |
144 | $this->assertArrayHasKey('first', $content['_links']); | |
145 | $this->assertArrayHasKey('last', $content['_links']); | |
146 | ||
147 | foreach (['self', 'first', 'last'] as $link) { | |
148 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
149 | $this->assertContains('starred=1', $content['_links'][$link]['href']); | |
150 | $this->assertContains('sort=updated', $content['_links'][$link]['href']); | |
151 | } | |
152 | ||
e6f55346 | 153 | $this->assertTrue( |
fcb1fba5 | 154 | $this->client->getResponse()->headers->contains( |
e6f55346 JB |
155 | 'Content-Type', |
156 | 'application/json' | |
157 | ) | |
158 | ); | |
159 | } | |
160 | ||
161 | public function testGetArchiveEntries() | |
162 | { | |
4094ea47 | 163 | $this->client->request('GET', '/api/entries', ['archive' => 1]); |
769e19dc | 164 | |
fcb1fba5 | 165 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 166 | |
fcb1fba5 | 167 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
168 | |
169 | $this->assertGreaterThanOrEqual(1, count($content)); | |
9744e971 | 170 | $this->assertNotEmpty($content['_embedded']['items']); |
28803f10 TC |
171 | $this->assertGreaterThanOrEqual(1, $content['total']); |
172 | $this->assertEquals(1, $content['page']); | |
173 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
174 | ||
c3f8b428 JB |
175 | $this->assertArrayHasKey('_links', $content); |
176 | $this->assertArrayHasKey('self', $content['_links']); | |
177 | $this->assertArrayHasKey('first', $content['_links']); | |
178 | $this->assertArrayHasKey('last', $content['_links']); | |
179 | ||
180 | foreach (['self', 'first', 'last'] as $link) { | |
181 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
182 | $this->assertContains('archive=1', $content['_links'][$link]['href']); | |
183 | } | |
184 | ||
28803f10 TC |
185 | $this->assertTrue( |
186 | $this->client->getResponse()->headers->contains( | |
187 | 'Content-Type', | |
188 | 'application/json' | |
189 | ) | |
190 | ); | |
191 | } | |
192 | ||
193 | public function testGetTaggedEntries() | |
194 | { | |
195 | $this->client->request('GET', '/api/entries', ['tags' => 'foo,bar']); | |
196 | ||
197 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
198 | ||
199 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
200 | ||
201 | $this->assertGreaterThanOrEqual(1, count($content)); | |
202 | $this->assertNotEmpty($content['_embedded']['items']); | |
9744e971 | 203 | $this->assertGreaterThanOrEqual(1, $content['total']); |
769e19dc | 204 | $this->assertEquals(1, $content['page']); |
9744e971 | 205 | $this->assertGreaterThanOrEqual(1, $content['pages']); |
769e19dc | 206 | |
c3f8b428 JB |
207 | $this->assertArrayHasKey('_links', $content); |
208 | $this->assertArrayHasKey('self', $content['_links']); | |
209 | $this->assertArrayHasKey('first', $content['_links']); | |
210 | $this->assertArrayHasKey('last', $content['_links']); | |
211 | ||
212 | foreach (['self', 'first', 'last'] as $link) { | |
213 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
214 | $this->assertContains('tags='.urlencode('foo,bar'), $content['_links'][$link]['href']); | |
215 | } | |
216 | ||
769e19dc | 217 | $this->assertTrue( |
fcb1fba5 | 218 | $this->client->getResponse()->headers->contains( |
769e19dc J |
219 | 'Content-Type', |
220 | 'application/json' | |
221 | ) | |
222 | ); | |
223 | } | |
224 | ||
e5fb89e5 TC |
225 | public function testGetDatedEntries() |
226 | { | |
c3f8b428 | 227 | $this->client->request('GET', '/api/entries', ['since' => 1443274283]); |
e5fb89e5 TC |
228 | |
229 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
230 | ||
231 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
232 | ||
233 | $this->assertGreaterThanOrEqual(1, count($content)); | |
234 | $this->assertNotEmpty($content['_embedded']['items']); | |
235 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
236 | $this->assertEquals(1, $content['page']); | |
237 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
238 | ||
c3f8b428 JB |
239 | $this->assertArrayHasKey('_links', $content); |
240 | $this->assertArrayHasKey('self', $content['_links']); | |
241 | $this->assertArrayHasKey('first', $content['_links']); | |
242 | $this->assertArrayHasKey('last', $content['_links']); | |
243 | ||
244 | foreach (['self', 'first', 'last'] as $link) { | |
245 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
246 | $this->assertContains('since=1443274283', $content['_links'][$link]['href']); | |
247 | } | |
248 | ||
e5fb89e5 TC |
249 | $this->assertTrue( |
250 | $this->client->getResponse()->headers->contains( | |
251 | 'Content-Type', | |
252 | 'application/json' | |
253 | ) | |
254 | ); | |
255 | } | |
256 | ||
257 | public function testGetDatedSupEntries() | |
258 | { | |
259 | $future = new \DateTime(date('Y-m-d H:i:s')); | |
260 | $this->client->request('GET', '/api/entries', ['since' => $future->getTimestamp() + 1000]); | |
261 | ||
262 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
263 | ||
264 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
265 | ||
266 | $this->assertGreaterThanOrEqual(1, count($content)); | |
267 | $this->assertEmpty($content['_embedded']['items']); | |
268 | $this->assertEquals(0, $content['total']); | |
269 | $this->assertEquals(1, $content['page']); | |
270 | $this->assertEquals(1, $content['pages']); | |
271 | ||
c3f8b428 JB |
272 | $this->assertArrayHasKey('_links', $content); |
273 | $this->assertArrayHasKey('self', $content['_links']); | |
274 | $this->assertArrayHasKey('first', $content['_links']); | |
275 | $this->assertArrayHasKey('last', $content['_links']); | |
276 | ||
277 | foreach (['self', 'first', 'last'] as $link) { | |
278 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
279 | $this->assertContains('since='.($future->getTimestamp() + 1000), $content['_links'][$link]['href']); | |
280 | } | |
281 | ||
e5fb89e5 TC |
282 | $this->assertTrue( |
283 | $this->client->getResponse()->headers->contains( | |
284 | 'Content-Type', | |
285 | 'application/json' | |
286 | ) | |
287 | ); | |
288 | } | |
289 | ||
769e19dc J |
290 | public function testDeleteEntry() |
291 | { | |
fcb1fba5 | 292 | $entry = $this->client->getContainer() |
769e19dc J |
293 | ->get('doctrine.orm.entity_manager') |
294 | ->getRepository('WallabagCoreBundle:Entry') | |
295 | ->findOneByUser(1); | |
296 | ||
297 | if (!$entry) { | |
298 | $this->markTestSkipped('No content found in db.'); | |
299 | } | |
300 | ||
fcb1fba5 | 301 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json'); |
769e19dc | 302 | |
fcb1fba5 | 303 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 304 | |
fcb1fba5 | 305 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
306 | |
307 | $this->assertEquals($entry->getTitle(), $content['title']); | |
308 | $this->assertEquals($entry->getUrl(), $content['url']); | |
309 | ||
310 | // We'll try to delete this entry again | |
fcb1fba5 | 311 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json'); |
769e19dc | 312 | |
fcb1fba5 | 313 | $this->assertEquals(404, $this->client->getResponse()->getStatusCode()); |
769e19dc J |
314 | } |
315 | ||
316 | public function testPostEntry() | |
317 | { | |
4094ea47 | 318 | $this->client->request('POST', '/api/entries.json', [ |
769e19dc J |
319 | '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', |
320 | 'tags' => 'google', | |
51a15609 | 321 | 'title' => 'New title for my article', |
4094ea47 | 322 | ]); |
769e19dc | 323 | |
fcb1fba5 | 324 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 325 | |
fcb1fba5 | 326 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
327 | |
328 | $this->assertGreaterThan(0, $content['id']); | |
329 | $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']); | |
330 | $this->assertEquals(false, $content['is_archived']); | |
331 | $this->assertEquals(false, $content['is_starred']); | |
51a15609 | 332 | $this->assertEquals('New title for my article', $content['title']); |
bc44aa57 | 333 | $this->assertEquals(1, $content['user_id']); |
769e19dc J |
334 | $this->assertCount(1, $content['tags']); |
335 | } | |
336 | ||
3107f92a TC |
337 | public function testPostSameEntry() |
338 | { | |
4094ea47 | 339 | $this->client->request('POST', '/api/entries.json', [ |
3107f92a TC |
340 | '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', |
341 | 'archive' => '1', | |
2baca964 | 342 | 'tags' => 'google, apple', |
4094ea47 | 343 | ]); |
3107f92a TC |
344 | |
345 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
346 | ||
347 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
348 | ||
349 | $this->assertGreaterThan(0, $content['id']); | |
350 | $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']); | |
351 | $this->assertEquals(true, $content['is_archived']); | |
352 | $this->assertEquals(false, $content['is_starred']); | |
2baca964 | 353 | $this->assertCount(2, $content['tags']); |
3107f92a TC |
354 | } |
355 | ||
09d8bb6f | 356 | public function testPostArchivedAndStarredEntry() |
816ad405 | 357 | { |
4094ea47 | 358 | $this->client->request('POST', '/api/entries.json', [ |
816ad405 | 359 | 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', |
189ef634 TC |
360 | 'archive' => '1', |
361 | 'starred' => '1', | |
4094ea47 | 362 | ]); |
816ad405 TC |
363 | |
364 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
365 | ||
366 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
367 | ||
368 | $this->assertGreaterThan(0, $content['id']); | |
369 | $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']); | |
370 | $this->assertEquals(true, $content['is_archived']); | |
09d8bb6f | 371 | $this->assertEquals(true, $content['is_starred']); |
bc44aa57 | 372 | $this->assertEquals(1, $content['user_id']); |
816ad405 TC |
373 | } |
374 | ||
2f60e5ea TC |
375 | public function testPostArchivedAndStarredEntryWithoutQuotes() |
376 | { | |
4094ea47 | 377 | $this->client->request('POST', '/api/entries.json', [ |
2f60e5ea TC |
378 | 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', |
379 | 'archive' => 0, | |
380 | 'starred' => 1, | |
4094ea47 | 381 | ]); |
2f60e5ea TC |
382 | |
383 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
384 | ||
385 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
386 | ||
387 | $this->assertGreaterThan(0, $content['id']); | |
388 | $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']); | |
389 | $this->assertEquals(false, $content['is_archived']); | |
390 | $this->assertEquals(true, $content['is_starred']); | |
391 | } | |
392 | ||
769e19dc J |
393 | public function testPatchEntry() |
394 | { | |
fcb1fba5 | 395 | $entry = $this->client->getContainer() |
769e19dc J |
396 | ->get('doctrine.orm.entity_manager') |
397 | ->getRepository('WallabagCoreBundle:Entry') | |
398 | ->findOneByUser(1); | |
399 | ||
400 | if (!$entry) { | |
401 | $this->markTestSkipped('No content found in db.'); | |
402 | } | |
403 | ||
404 | // hydrate the tags relations | |
405 | $nbTags = count($entry->getTags()); | |
406 | ||
4094ea47 | 407 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ |
769e19dc J |
408 | 'title' => 'New awesome title', |
409 | 'tags' => 'new tag '.uniqid(), | |
189ef634 TC |
410 | 'starred' => '1', |
411 | 'archive' => '0', | |
4094ea47 | 412 | ]); |
769e19dc | 413 | |
fcb1fba5 | 414 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 415 | |
fcb1fba5 | 416 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
417 | |
418 | $this->assertEquals($entry->getId(), $content['id']); | |
419 | $this->assertEquals($entry->getUrl(), $content['url']); | |
420 | $this->assertEquals('New awesome title', $content['title']); | |
421 | $this->assertGreaterThan($nbTags, count($content['tags'])); | |
bc44aa57 | 422 | $this->assertEquals(1, $content['user_id']); |
769e19dc J |
423 | } |
424 | ||
2f60e5ea TC |
425 | public function testPatchEntryWithoutQuotes() |
426 | { | |
427 | $entry = $this->client->getContainer() | |
428 | ->get('doctrine.orm.entity_manager') | |
429 | ->getRepository('WallabagCoreBundle:Entry') | |
430 | ->findOneByUser(1); | |
431 | ||
432 | if (!$entry) { | |
433 | $this->markTestSkipped('No content found in db.'); | |
434 | } | |
435 | ||
436 | // hydrate the tags relations | |
437 | $nbTags = count($entry->getTags()); | |
438 | ||
4094ea47 | 439 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ |
2f60e5ea TC |
440 | 'title' => 'New awesome title', |
441 | 'tags' => 'new tag '.uniqid(), | |
442 | 'starred' => 1, | |
443 | 'archive' => 0, | |
4094ea47 | 444 | ]); |
2f60e5ea TC |
445 | |
446 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
447 | ||
448 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
449 | ||
450 | $this->assertEquals($entry->getId(), $content['id']); | |
451 | $this->assertEquals($entry->getUrl(), $content['url']); | |
452 | $this->assertEquals('New awesome title', $content['title']); | |
453 | $this->assertGreaterThan($nbTags, count($content['tags'])); | |
454 | } | |
455 | ||
769e19dc J |
456 | public function testGetTagsEntry() |
457 | { | |
fcb1fba5 | 458 | $entry = $this->client->getContainer() |
769e19dc J |
459 | ->get('doctrine.orm.entity_manager') |
460 | ->getRepository('WallabagCoreBundle:Entry') | |
a0e1eafc | 461 | ->findOneWithTags($this->user->getId()); |
769e19dc J |
462 | |
463 | $entry = $entry[0]; | |
464 | ||
465 | if (!$entry) { | |
466 | $this->markTestSkipped('No content found in db.'); | |
467 | } | |
468 | ||
4094ea47 | 469 | $tags = []; |
769e19dc | 470 | foreach ($entry->getTags() as $tag) { |
4094ea47 | 471 | $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()]; |
769e19dc J |
472 | } |
473 | ||
fcb1fba5 | 474 | $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags'); |
769e19dc | 475 | |
fcb1fba5 | 476 | $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent()); |
769e19dc J |
477 | } |
478 | ||
479 | public function testPostTagsOnEntry() | |
480 | { | |
fcb1fba5 | 481 | $entry = $this->client->getContainer() |
769e19dc J |
482 | ->get('doctrine.orm.entity_manager') |
483 | ->getRepository('WallabagCoreBundle:Entry') | |
484 | ->findOneByUser(1); | |
485 | ||
486 | if (!$entry) { | |
487 | $this->markTestSkipped('No content found in db.'); | |
488 | } | |
489 | ||
490 | $nbTags = count($entry->getTags()); | |
491 | ||
492 | $newTags = 'tag1,tag2,tag3'; | |
493 | ||
4094ea47 | 494 | $this->client->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]); |
769e19dc | 495 | |
fcb1fba5 | 496 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 497 | |
fcb1fba5 | 498 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
499 | |
500 | $this->assertArrayHasKey('tags', $content); | |
4346a860 | 501 | $this->assertEquals($nbTags + 3, count($content['tags'])); |
769e19dc | 502 | |
fcb1fba5 | 503 | $entryDB = $this->client->getContainer() |
769e19dc J |
504 | ->get('doctrine.orm.entity_manager') |
505 | ->getRepository('WallabagCoreBundle:Entry') | |
506 | ->find($entry->getId()); | |
507 | ||
4094ea47 | 508 | $tagsInDB = []; |
769e19dc J |
509 | foreach ($entryDB->getTags()->toArray() as $tag) { |
510 | $tagsInDB[$tag->getId()] = $tag->getLabel(); | |
511 | } | |
512 | ||
513 | foreach (explode(',', $newTags) as $tag) { | |
514 | $this->assertContains($tag, $tagsInDB); | |
515 | } | |
516 | } | |
517 | ||
fcb1fba5 | 518 | public function testDeleteOneTagEntry() |
769e19dc | 519 | { |
fcb1fba5 | 520 | $entry = $this->client->getContainer() |
769e19dc J |
521 | ->get('doctrine.orm.entity_manager') |
522 | ->getRepository('WallabagCoreBundle:Entry') | |
a0e1eafc | 523 | ->findOneWithTags($this->user->getId()); |
fcb1fba5 | 524 | $entry = $entry[0]; |
769e19dc J |
525 | |
526 | if (!$entry) { | |
527 | $this->markTestSkipped('No content found in db.'); | |
528 | } | |
529 | ||
530 | // hydrate the tags relations | |
531 | $nbTags = count($entry->getTags()); | |
532 | $tag = $entry->getTags()[0]; | |
533 | ||
fcb1fba5 | 534 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json'); |
769e19dc | 535 | |
fcb1fba5 | 536 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 537 | |
fcb1fba5 | 538 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
539 | |
540 | $this->assertArrayHasKey('tags', $content); | |
4346a860 | 541 | $this->assertEquals($nbTags - 1, count($content['tags'])); |
769e19dc J |
542 | } |
543 | ||
544 | public function testGetUserTags() | |
545 | { | |
fcb1fba5 | 546 | $this->client->request('GET', '/api/tags.json'); |
769e19dc | 547 | |
fcb1fba5 | 548 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 549 | |
fcb1fba5 | 550 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
551 | |
552 | $this->assertGreaterThan(0, $content); | |
553 | $this->assertArrayHasKey('id', $content[0]); | |
554 | $this->assertArrayHasKey('label', $content[0]); | |
555 | ||
556 | return end($content); | |
557 | } | |
558 | ||
559 | /** | |
560 | * @depends testGetUserTags | |
561 | */ | |
562 | public function testDeleteUserTag($tag) | |
563 | { | |
ac8cf632 JB |
564 | $tagName = $tag['label']; |
565 | ||
fcb1fba5 | 566 | $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json'); |
769e19dc | 567 | |
fcb1fba5 | 568 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
769e19dc | 569 | |
fcb1fba5 | 570 | $content = json_decode($this->client->getResponse()->getContent(), true); |
769e19dc J |
571 | |
572 | $this->assertArrayHasKey('label', $content); | |
573 | $this->assertEquals($tag['label'], $content['label']); | |
fc732227 | 574 | $this->assertEquals($tag['slug'], $content['slug']); |
4059a061 | 575 | |
a0e1eafc | 576 | $entries = $this->client->getContainer() |
4059a061 JB |
577 | ->get('doctrine.orm.entity_manager') |
578 | ->getRepository('WallabagCoreBundle:Entry') | |
579 | ->findAllByTagId($this->user->getId(), $tag['id']); | |
580 | ||
581 | $this->assertCount(0, $entries); | |
ac8cf632 JB |
582 | |
583 | $tag = $this->client->getContainer() | |
584 | ->get('doctrine.orm.entity_manager') | |
585 | ->getRepository('WallabagCoreBundle:Tag') | |
586 | ->findOneByLabel($tagName); | |
587 | ||
588 | $this->assertNull($tag, $tagName.' was removed because it begun an orphan tag'); | |
769e19dc | 589 | } |
9761bfa1 | 590 | |
a0e1eafc JB |
591 | public function testDeleteTagByLabel() |
592 | { | |
593 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
594 | $entry = $this->client->getContainer() | |
595 | ->get('doctrine.orm.entity_manager') | |
596 | ->getRepository('WallabagCoreBundle:Entry') | |
597 | ->findOneWithTags($this->user->getId()); | |
598 | ||
599 | $entry = $entry[0]; | |
600 | ||
601 | $tag = new Tag(); | |
602 | $tag->setLabel('Awesome tag for test'); | |
603 | $em->persist($tag); | |
604 | ||
605 | $entry->addTag($tag); | |
606 | ||
607 | $em->persist($entry); | |
608 | $em->flush(); | |
609 | ||
610 | $this->client->request('DELETE', '/api/tag/label.json', ['tag' => $tag->getLabel()]); | |
611 | ||
612 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
613 | ||
614 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
615 | ||
616 | $this->assertArrayHasKey('label', $content); | |
617 | $this->assertEquals($tag->getLabel(), $content['label']); | |
618 | $this->assertEquals($tag->getSlug(), $content['slug']); | |
619 | ||
620 | $entries = $this->client->getContainer() | |
621 | ->get('doctrine.orm.entity_manager') | |
622 | ->getRepository('WallabagCoreBundle:Entry') | |
623 | ->findAllByTagId($this->user->getId(), $tag->getId()); | |
624 | ||
625 | $this->assertCount(0, $entries); | |
626 | } | |
627 | ||
628 | public function testDeleteTagByLabelNotFound() | |
629 | { | |
630 | $this->client->request('DELETE', '/api/tag/label.json', ['tag' => 'does not exist']); | |
631 | ||
632 | $this->assertEquals(404, $this->client->getResponse()->getStatusCode()); | |
633 | } | |
634 | ||
635 | public function testDeleteTagsByLabel() | |
636 | { | |
637 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
638 | $entry = $this->client->getContainer() | |
639 | ->get('doctrine.orm.entity_manager') | |
640 | ->getRepository('WallabagCoreBundle:Entry') | |
641 | ->findOneWithTags($this->user->getId()); | |
642 | ||
643 | $entry = $entry[0]; | |
644 | ||
645 | $tag = new Tag(); | |
646 | $tag->setLabel('Awesome tag for tagsLabel'); | |
647 | $em->persist($tag); | |
648 | ||
649 | $tag2 = new Tag(); | |
650 | $tag2->setLabel('Awesome tag for tagsLabel 2'); | |
651 | $em->persist($tag2); | |
652 | ||
653 | $entry->addTag($tag); | |
654 | $entry->addTag($tag2); | |
655 | ||
656 | $em->persist($entry); | |
657 | $em->flush(); | |
658 | ||
659 | $this->client->request('DELETE', '/api/tags/label.json', ['tags' => $tag->getLabel().','.$tag2->getLabel()]); | |
660 | ||
661 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
662 | ||
663 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
664 | ||
665 | $this->assertCount(2, $content); | |
666 | ||
667 | $this->assertArrayHasKey('label', $content[0]); | |
668 | $this->assertEquals($tag->getLabel(), $content[0]['label']); | |
669 | $this->assertEquals($tag->getSlug(), $content[0]['slug']); | |
670 | ||
671 | $this->assertArrayHasKey('label', $content[1]); | |
672 | $this->assertEquals($tag2->getLabel(), $content[1]['label']); | |
673 | $this->assertEquals($tag2->getSlug(), $content[1]['slug']); | |
674 | ||
675 | $entries = $this->client->getContainer() | |
676 | ->get('doctrine.orm.entity_manager') | |
677 | ->getRepository('WallabagCoreBundle:Entry') | |
678 | ->findAllByTagId($this->user->getId(), $tag->getId()); | |
679 | ||
680 | $this->assertCount(0, $entries); | |
681 | ||
682 | $entries = $this->client->getContainer() | |
683 | ->get('doctrine.orm.entity_manager') | |
684 | ->getRepository('WallabagCoreBundle:Entry') | |
685 | ->findAllByTagId($this->user->getId(), $tag2->getId()); | |
686 | ||
687 | $this->assertCount(0, $entries); | |
688 | } | |
689 | ||
690 | public function testDeleteTagsByLabelNotFound() | |
691 | { | |
692 | $this->client->request('DELETE', '/api/tags/label.json', ['tags' => 'does not exist']); | |
693 | ||
694 | $this->assertEquals(404, $this->client->getResponse()->getStatusCode()); | |
695 | } | |
696 | ||
6f8310b4 TC |
697 | public function testGetVersion() |
698 | { | |
699 | $this->client->request('GET', '/api/version'); | |
9761bfa1 V |
700 | |
701 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
702 | ||
703 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
704 | ||
6f8310b4 | 705 | $this->assertEquals($this->client->getContainer()->getParameter('wallabag_core.version'), $content); |
9761bfa1 | 706 | } |
bba271e6 YE |
707 | |
708 | public function testSaveIsArchivedAfterPost() | |
709 | { | |
710 | $entry = $this->client->getContainer() | |
711 | ->get('doctrine.orm.entity_manager') | |
712 | ->getRepository('WallabagCoreBundle:Entry') | |
713 | ->findOneBy(['user' => 1, 'isArchived' => true]); | |
714 | ||
715 | if (!$entry) { | |
716 | $this->markTestSkipped('No content found in db.'); | |
717 | } | |
718 | ||
719 | $this->client->request('POST', '/api/entries.json', [ | |
720 | 'url' => $entry->getUrl(), | |
721 | ]); | |
722 | ||
723 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
724 | ||
725 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
726 | ||
69221684 | 727 | $this->assertEquals(true, $content['is_archived']); |
bba271e6 YE |
728 | } |
729 | ||
730 | public function testSaveIsStarredAfterPost() | |
731 | { | |
732 | $entry = $this->client->getContainer() | |
733 | ->get('doctrine.orm.entity_manager') | |
734 | ->getRepository('WallabagCoreBundle:Entry') | |
735 | ->findOneBy(['user' => 1, 'isStarred' => true]); | |
736 | ||
737 | if (!$entry) { | |
738 | $this->markTestSkipped('No content found in db.'); | |
739 | } | |
740 | ||
741 | $this->client->request('POST', '/api/entries.json', [ | |
742 | 'url' => $entry->getUrl(), | |
743 | ]); | |
744 | ||
745 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
746 | ||
747 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
748 | ||
69221684 | 749 | $this->assertEquals(true, $content['is_starred']); |
bba271e6 YE |
750 | } |
751 | ||
752 | public function testSaveIsArchivedAfterPatch() | |
753 | { | |
754 | $entry = $this->client->getContainer() | |
755 | ->get('doctrine.orm.entity_manager') | |
756 | ->getRepository('WallabagCoreBundle:Entry') | |
757 | ->findOneBy(['user' => 1, 'isArchived' => true]); | |
758 | ||
759 | if (!$entry) { | |
760 | $this->markTestSkipped('No content found in db.'); | |
761 | } | |
762 | ||
763 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | |
764 | 'title' => $entry->getTitle().'++', | |
765 | ]); | |
766 | ||
767 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
768 | ||
769 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
770 | ||
771 | $this->assertEquals(true, $content['is_archived']); | |
772 | } | |
773 | ||
774 | public function testSaveIsStarredAfterPatch() | |
775 | { | |
776 | $entry = $this->client->getContainer() | |
777 | ->get('doctrine.orm.entity_manager') | |
778 | ->getRepository('WallabagCoreBundle:Entry') | |
779 | ->findOneBy(['user' => 1, 'isStarred' => true]); | |
780 | ||
781 | if (!$entry) { | |
782 | $this->markTestSkipped('No content found in db.'); | |
783 | } | |
784 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | |
785 | 'title' => $entry->getTitle().'++', | |
786 | ]); | |
787 | ||
788 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
789 | ||
790 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
791 | ||
69221684 | 792 | $this->assertEquals(true, $content['is_starred']); |
bba271e6 | 793 | } |
6273fefd JB |
794 | |
795 | public function testGetEntriesExists() | |
796 | { | |
797 | $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2'); | |
798 | ||
799 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
800 | ||
801 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
802 | ||
803 | $this->assertEquals(true, $content['exists']); | |
804 | } | |
55551e33 | 805 | |
f0abc22d JB |
806 | public function testGetEntriesExistsWithManyUrls() |
807 | { | |
808 | $url1 = 'http://0.0.0.0/entry2'; | |
809 | $url2 = 'http://0.0.0.0/entry10'; | |
810 | $this->client->request('GET', '/api/entries/exists?urls[]='.$url1.'&urls[]='.$url2); | |
811 | ||
812 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
813 | ||
814 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
815 | ||
816 | $this->assertArrayHasKey($url1, $content); | |
817 | $this->assertArrayHasKey($url2, $content); | |
818 | $this->assertEquals(true, $content[$url1]); | |
819 | $this->assertEquals(false, $content[$url2]); | |
820 | } | |
821 | ||
55551e33 JB |
822 | public function testGetEntriesExistsWhichDoesNotExists() |
823 | { | |
824 | $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2'); | |
825 | ||
826 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
827 | ||
828 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
829 | ||
830 | $this->assertEquals(false, $content['exists']); | |
831 | } | |
0b174d69 JB |
832 | |
833 | public function testGetEntriesExistsWithNoUrl() | |
834 | { | |
835 | $this->client->request('GET', '/api/entries/exists?url='); | |
836 | ||
837 | $this->assertEquals(403, $this->client->getResponse()->getStatusCode()); | |
838 | } | |
769e19dc | 839 | } |