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