]>
Commit | Line | Data |
---|---|---|
900c8448 NL |
1 | <?php |
2 | ||
3 | namespace Tests\Wallabag\ApiBundle\Controller; | |
4 | ||
5 | use Tests\Wallabag\ApiBundle\WallabagApiTestCase; | |
6 | use Wallabag\CoreBundle\Entity\Tag; | |
08f29ae7 | 7 | use Wallabag\CoreBundle\Helper\ContentProxy; |
900c8448 NL |
8 | |
9 | class EntryRestControllerTest extends WallabagApiTestCase | |
10 | { | |
11 | public function testGetOneEntry() | |
12 | { | |
13 | $entry = $this->client->getContainer() | |
14 | ->get('doctrine.orm.entity_manager') | |
15 | ->getRepository('WallabagCoreBundle:Entry') | |
16 | ->findOneBy(['user' => 1, 'isArchived' => false]); | |
17 | ||
18 | if (!$entry) { | |
19 | $this->markTestSkipped('No content found in db.'); | |
20 | } | |
21 | ||
22 | $this->client->request('GET', '/api/entries/'.$entry->getId().'.json'); | |
23 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
24 | ||
25 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
26 | ||
27 | $this->assertEquals($entry->getTitle(), $content['title']); | |
28 | $this->assertEquals($entry->getUrl(), $content['url']); | |
29 | $this->assertCount(count($entry->getTags()), $content['tags']); | |
30 | $this->assertEquals($entry->getUserName(), $content['user_name']); | |
31 | $this->assertEquals($entry->getUserEmail(), $content['user_email']); | |
32 | $this->assertEquals($entry->getUserId(), $content['user_id']); | |
33 | ||
5a619812 JB |
34 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
35 | } | |
36 | ||
37 | public function testExportEntry() | |
38 | { | |
39 | $entry = $this->client->getContainer() | |
40 | ->get('doctrine.orm.entity_manager') | |
41 | ->getRepository('WallabagCoreBundle:Entry') | |
42 | ->findOneBy(['user' => 1, 'isArchived' => false]); | |
43 | ||
44 | if (!$entry) { | |
45 | $this->markTestSkipped('No content found in db.'); | |
46 | } | |
47 | ||
48 | $this->client->request('GET', '/api/entries/'.$entry->getId().'/export.epub'); | |
49 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
50 | ||
51 | // epub format got the content type in the content | |
52 | $this->assertContains('application/epub', $this->client->getResponse()->getContent()); | |
53 | $this->assertEquals('application/epub+zip', $this->client->getResponse()->headers->get('Content-Type')); | |
54 | ||
55 | // re-auth client for mobi | |
56 | $client = $this->createAuthorizedClient(); | |
57 | $client->request('GET', '/api/entries/'.$entry->getId().'/export.mobi'); | |
58 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
59 | ||
60 | $this->assertEquals('application/x-mobipocket-ebook', $client->getResponse()->headers->get('Content-Type')); | |
61 | ||
62 | // re-auth client for pdf | |
63 | $client = $this->createAuthorizedClient(); | |
64 | $client->request('GET', '/api/entries/'.$entry->getId().'/export.pdf'); | |
65 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
66 | ||
67 | $this->assertContains('PDF-', $client->getResponse()->getContent()); | |
68 | $this->assertEquals('application/pdf', $client->getResponse()->headers->get('Content-Type')); | |
69 | ||
70 | // re-auth client for pdf | |
71 | $client = $this->createAuthorizedClient(); | |
72 | $client->request('GET', '/api/entries/'.$entry->getId().'/export.txt'); | |
73 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
74 | ||
75 | $this->assertContains('text/plain', $client->getResponse()->headers->get('Content-Type')); | |
76 | ||
77 | // re-auth client for pdf | |
78 | $client = $this->createAuthorizedClient(); | |
79 | $client->request('GET', '/api/entries/'.$entry->getId().'/export.csv'); | |
80 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
81 | ||
82 | $this->assertContains('application/csv', $client->getResponse()->headers->get('Content-Type')); | |
900c8448 NL |
83 | } |
84 | ||
85 | public function testGetOneEntryWrongUser() | |
86 | { | |
87 | $entry = $this->client->getContainer() | |
88 | ->get('doctrine.orm.entity_manager') | |
89 | ->getRepository('WallabagCoreBundle:Entry') | |
90 | ->findOneBy(['user' => 2, 'isArchived' => false]); | |
91 | ||
92 | if (!$entry) { | |
93 | $this->markTestSkipped('No content found in db.'); | |
94 | } | |
95 | ||
96 | $this->client->request('GET', '/api/entries/'.$entry->getId().'.json'); | |
97 | ||
98 | $this->assertEquals(403, $this->client->getResponse()->getStatusCode()); | |
99 | } | |
100 | ||
101 | public function testGetEntries() | |
102 | { | |
103 | $this->client->request('GET', '/api/entries'); | |
104 | ||
105 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
106 | ||
107 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
108 | ||
109 | $this->assertGreaterThanOrEqual(1, count($content)); | |
110 | $this->assertNotEmpty($content['_embedded']['items']); | |
111 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
112 | $this->assertEquals(1, $content['page']); | |
113 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
114 | ||
5a619812 | 115 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
900c8448 NL |
116 | } |
117 | ||
118 | public function testGetEntriesWithFullOptions() | |
119 | { | |
120 | $this->client->request('GET', '/api/entries', [ | |
121 | 'archive' => 1, | |
122 | 'starred' => 1, | |
123 | 'sort' => 'updated', | |
124 | 'order' => 'asc', | |
125 | 'page' => 1, | |
126 | 'perPage' => 2, | |
127 | 'tags' => 'foo', | |
128 | 'since' => 1443274283, | |
129 | ]); | |
130 | ||
131 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
132 | ||
133 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
134 | ||
135 | $this->assertGreaterThanOrEqual(1, count($content)); | |
136 | $this->assertArrayHasKey('items', $content['_embedded']); | |
137 | $this->assertGreaterThanOrEqual(0, $content['total']); | |
138 | $this->assertEquals(1, $content['page']); | |
139 | $this->assertEquals(2, $content['limit']); | |
140 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
141 | ||
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('archive=1', $content['_links'][$link]['href']); | |
150 | $this->assertContains('starred=1', $content['_links'][$link]['href']); | |
151 | $this->assertContains('sort=updated', $content['_links'][$link]['href']); | |
152 | $this->assertContains('order=asc', $content['_links'][$link]['href']); | |
153 | $this->assertContains('tags=foo', $content['_links'][$link]['href']); | |
154 | $this->assertContains('since=1443274283', $content['_links'][$link]['href']); | |
155 | } | |
156 | ||
5a619812 | 157 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
900c8448 NL |
158 | } |
159 | ||
160 | public function testGetStarredEntries() | |
161 | { | |
162 | $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']); | |
163 | ||
164 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
165 | ||
166 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
167 | ||
168 | $this->assertGreaterThanOrEqual(1, count($content)); | |
169 | $this->assertNotEmpty($content['_embedded']['items']); | |
170 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
171 | $this->assertEquals(1, $content['page']); | |
172 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
173 | ||
174 | $this->assertArrayHasKey('_links', $content); | |
175 | $this->assertArrayHasKey('self', $content['_links']); | |
176 | $this->assertArrayHasKey('first', $content['_links']); | |
177 | $this->assertArrayHasKey('last', $content['_links']); | |
178 | ||
179 | foreach (['self', 'first', 'last'] as $link) { | |
180 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
181 | $this->assertContains('starred=1', $content['_links'][$link]['href']); | |
182 | $this->assertContains('sort=updated', $content['_links'][$link]['href']); | |
183 | } | |
184 | ||
5a619812 | 185 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
900c8448 NL |
186 | } |
187 | ||
188 | public function testGetArchiveEntries() | |
189 | { | |
190 | $this->client->request('GET', '/api/entries', ['archive' => 1]); | |
191 | ||
192 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
193 | ||
194 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
195 | ||
196 | $this->assertGreaterThanOrEqual(1, count($content)); | |
197 | $this->assertNotEmpty($content['_embedded']['items']); | |
198 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
199 | $this->assertEquals(1, $content['page']); | |
200 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
201 | ||
202 | $this->assertArrayHasKey('_links', $content); | |
203 | $this->assertArrayHasKey('self', $content['_links']); | |
204 | $this->assertArrayHasKey('first', $content['_links']); | |
205 | $this->assertArrayHasKey('last', $content['_links']); | |
206 | ||
207 | foreach (['self', 'first', 'last'] as $link) { | |
208 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
209 | $this->assertContains('archive=1', $content['_links'][$link]['href']); | |
210 | } | |
211 | ||
5a619812 | 212 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
900c8448 NL |
213 | } |
214 | ||
215 | public function testGetTaggedEntries() | |
216 | { | |
217 | $this->client->request('GET', '/api/entries', ['tags' => 'foo,bar']); | |
218 | ||
219 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
220 | ||
221 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
222 | ||
223 | $this->assertGreaterThanOrEqual(1, count($content)); | |
224 | $this->assertNotEmpty($content['_embedded']['items']); | |
225 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
226 | $this->assertEquals(1, $content['page']); | |
227 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
228 | ||
229 | $this->assertArrayHasKey('_links', $content); | |
230 | $this->assertArrayHasKey('self', $content['_links']); | |
231 | $this->assertArrayHasKey('first', $content['_links']); | |
232 | $this->assertArrayHasKey('last', $content['_links']); | |
233 | ||
234 | foreach (['self', 'first', 'last'] as $link) { | |
235 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
236 | $this->assertContains('tags='.urlencode('foo,bar'), $content['_links'][$link]['href']); | |
237 | } | |
238 | ||
5a619812 | 239 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
900c8448 NL |
240 | } |
241 | ||
242 | public function testGetDatedEntries() | |
243 | { | |
244 | $this->client->request('GET', '/api/entries', ['since' => 1443274283]); | |
245 | ||
246 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
247 | ||
248 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
249 | ||
250 | $this->assertGreaterThanOrEqual(1, count($content)); | |
251 | $this->assertNotEmpty($content['_embedded']['items']); | |
252 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
253 | $this->assertEquals(1, $content['page']); | |
254 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
255 | ||
256 | $this->assertArrayHasKey('_links', $content); | |
257 | $this->assertArrayHasKey('self', $content['_links']); | |
258 | $this->assertArrayHasKey('first', $content['_links']); | |
259 | $this->assertArrayHasKey('last', $content['_links']); | |
260 | ||
261 | foreach (['self', 'first', 'last'] as $link) { | |
262 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
263 | $this->assertContains('since=1443274283', $content['_links'][$link]['href']); | |
264 | } | |
265 | ||
5a619812 | 266 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
900c8448 NL |
267 | } |
268 | ||
269 | public function testGetDatedSupEntries() | |
270 | { | |
271 | $future = new \DateTime(date('Y-m-d H:i:s')); | |
272 | $this->client->request('GET', '/api/entries', ['since' => $future->getTimestamp() + 1000]); | |
273 | ||
274 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
275 | ||
276 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
277 | ||
278 | $this->assertGreaterThanOrEqual(1, count($content)); | |
279 | $this->assertEmpty($content['_embedded']['items']); | |
280 | $this->assertEquals(0, $content['total']); | |
281 | $this->assertEquals(1, $content['page']); | |
282 | $this->assertEquals(1, $content['pages']); | |
283 | ||
284 | $this->assertArrayHasKey('_links', $content); | |
285 | $this->assertArrayHasKey('self', $content['_links']); | |
286 | $this->assertArrayHasKey('first', $content['_links']); | |
287 | $this->assertArrayHasKey('last', $content['_links']); | |
288 | ||
289 | foreach (['self', 'first', 'last'] as $link) { | |
290 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
291 | $this->assertContains('since='.($future->getTimestamp() + 1000), $content['_links'][$link]['href']); | |
292 | } | |
293 | ||
5a619812 | 294 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
900c8448 NL |
295 | } |
296 | ||
297 | public function testDeleteEntry() | |
298 | { | |
299 | $entry = $this->client->getContainer() | |
300 | ->get('doctrine.orm.entity_manager') | |
301 | ->getRepository('WallabagCoreBundle:Entry') | |
302 | ->findOneByUser(1); | |
303 | ||
304 | if (!$entry) { | |
305 | $this->markTestSkipped('No content found in db.'); | |
306 | } | |
307 | ||
308 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json'); | |
309 | ||
310 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
311 | ||
312 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
313 | ||
314 | $this->assertEquals($entry->getTitle(), $content['title']); | |
315 | $this->assertEquals($entry->getUrl(), $content['url']); | |
316 | ||
317 | // We'll try to delete this entry again | |
318 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json'); | |
319 | ||
320 | $this->assertEquals(404, $this->client->getResponse()->getStatusCode()); | |
321 | } | |
322 | ||
323 | public function testPostEntry() | |
324 | { | |
325 | $this->client->request('POST', '/api/entries.json', [ | |
326 | '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', | |
327 | 'tags' => 'google', | |
328 | 'title' => 'New title for my article', | |
329 | ]); | |
330 | ||
331 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
332 | ||
333 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
334 | ||
335 | $this->assertGreaterThan(0, $content['id']); | |
336 | $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']); | |
337 | $this->assertEquals(false, $content['is_archived']); | |
338 | $this->assertEquals(false, $content['is_starred']); | |
339 | $this->assertEquals('New title for my article', $content['title']); | |
340 | $this->assertEquals(1, $content['user_id']); | |
341 | $this->assertCount(1, $content['tags']); | |
342 | } | |
343 | ||
344 | public function testPostSameEntry() | |
345 | { | |
346 | $this->client->request('POST', '/api/entries.json', [ | |
347 | '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', | |
348 | 'archive' => '1', | |
349 | 'tags' => 'google, apple', | |
350 | ]); | |
351 | ||
352 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
353 | ||
354 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
355 | ||
356 | $this->assertGreaterThan(0, $content['id']); | |
357 | $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']); | |
358 | $this->assertEquals(true, $content['is_archived']); | |
359 | $this->assertEquals(false, $content['is_starred']); | |
360 | $this->assertCount(2, $content['tags']); | |
361 | } | |
362 | ||
08f29ae7 | 363 | public function testPostEntryWhenFetchContentFails() |
364 | { | |
365 | /** @var \Symfony\Component\DependencyInjection\Container $container */ | |
366 | $container = $this->client->getContainer(); | |
367 | $contentProxy = $this->getMockBuilder(ContentProxy::class) | |
368 | ->disableOriginalConstructor() | |
369 | ->setMethods(['updateEntry']) | |
370 | ->getMock(); | |
371 | $contentProxy->expects($this->any()) | |
372 | ->method('updateEntry') | |
373 | ->willThrowException(new \Exception('Test Fetch content fails')); | |
374 | $container->set('wallabag_core.content_proxy', $contentProxy); | |
375 | ||
376 | $this->client->request('POST', '/api/entries.json', [ | |
377 | 'url' => 'http://www.example.com/', | |
378 | ]); | |
379 | ||
380 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
381 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
382 | $this->assertGreaterThan(0, $content['id']); | |
383 | $this->assertEquals('http://www.example.com/', $content['url']); | |
384 | } | |
385 | ||
900c8448 NL |
386 | public function testPostArchivedAndStarredEntry() |
387 | { | |
388 | $this->client->request('POST', '/api/entries.json', [ | |
389 | 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', | |
390 | 'archive' => '1', | |
391 | 'starred' => '1', | |
392 | ]); | |
393 | ||
394 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
395 | ||
396 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
397 | ||
398 | $this->assertGreaterThan(0, $content['id']); | |
399 | $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']); | |
400 | $this->assertEquals(true, $content['is_archived']); | |
401 | $this->assertEquals(true, $content['is_starred']); | |
402 | $this->assertEquals(1, $content['user_id']); | |
403 | } | |
404 | ||
405 | public function testPostArchivedAndStarredEntryWithoutQuotes() | |
406 | { | |
407 | $this->client->request('POST', '/api/entries.json', [ | |
408 | 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', | |
409 | 'archive' => 0, | |
410 | 'starred' => 1, | |
411 | ]); | |
412 | ||
413 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
414 | ||
415 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
416 | ||
417 | $this->assertGreaterThan(0, $content['id']); | |
418 | $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']); | |
419 | $this->assertEquals(false, $content['is_archived']); | |
420 | $this->assertEquals(true, $content['is_starred']); | |
421 | } | |
422 | ||
423 | public function testPatchEntry() | |
424 | { | |
425 | $entry = $this->client->getContainer() | |
426 | ->get('doctrine.orm.entity_manager') | |
427 | ->getRepository('WallabagCoreBundle:Entry') | |
428 | ->findOneByUser(1); | |
429 | ||
430 | if (!$entry) { | |
431 | $this->markTestSkipped('No content found in db.'); | |
432 | } | |
433 | ||
434 | // hydrate the tags relations | |
435 | $nbTags = count($entry->getTags()); | |
436 | ||
437 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | |
438 | 'title' => 'New awesome title', | |
439 | 'tags' => 'new tag '.uniqid(), | |
440 | 'starred' => '1', | |
441 | 'archive' => '0', | |
442 | ]); | |
443 | ||
444 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
445 | ||
446 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
447 | ||
448 | $this->assertEquals($entry->getId(), $content['id']); | |
449 | $this->assertEquals($entry->getUrl(), $content['url']); | |
450 | $this->assertEquals('New awesome title', $content['title']); | |
451 | $this->assertGreaterThan($nbTags, count($content['tags'])); | |
452 | $this->assertEquals(1, $content['user_id']); | |
453 | } | |
454 | ||
455 | public function testPatchEntryWithoutQuotes() | |
456 | { | |
457 | $entry = $this->client->getContainer() | |
458 | ->get('doctrine.orm.entity_manager') | |
459 | ->getRepository('WallabagCoreBundle:Entry') | |
460 | ->findOneByUser(1); | |
461 | ||
462 | if (!$entry) { | |
463 | $this->markTestSkipped('No content found in db.'); | |
464 | } | |
465 | ||
466 | // hydrate the tags relations | |
467 | $nbTags = count($entry->getTags()); | |
468 | ||
469 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | |
470 | 'title' => 'New awesome title', | |
471 | 'tags' => 'new tag '.uniqid(), | |
472 | 'starred' => 1, | |
473 | 'archive' => 0, | |
474 | ]); | |
475 | ||
476 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
477 | ||
478 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
479 | ||
480 | $this->assertEquals($entry->getId(), $content['id']); | |
481 | $this->assertEquals($entry->getUrl(), $content['url']); | |
482 | $this->assertEquals('New awesome title', $content['title']); | |
483 | $this->assertGreaterThan($nbTags, count($content['tags'])); | |
484 | } | |
485 | ||
486 | public function testGetTagsEntry() | |
487 | { | |
488 | $entry = $this->client->getContainer() | |
489 | ->get('doctrine.orm.entity_manager') | |
490 | ->getRepository('WallabagCoreBundle:Entry') | |
491 | ->findOneWithTags($this->user->getId()); | |
492 | ||
493 | $entry = $entry[0]; | |
494 | ||
495 | if (!$entry) { | |
496 | $this->markTestSkipped('No content found in db.'); | |
497 | } | |
498 | ||
499 | $tags = []; | |
500 | foreach ($entry->getTags() as $tag) { | |
501 | $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()]; | |
502 | } | |
503 | ||
504 | $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags'); | |
505 | ||
506 | $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent()); | |
507 | } | |
508 | ||
509 | public function testPostTagsOnEntry() | |
510 | { | |
511 | $entry = $this->client->getContainer() | |
512 | ->get('doctrine.orm.entity_manager') | |
513 | ->getRepository('WallabagCoreBundle:Entry') | |
514 | ->findOneByUser(1); | |
515 | ||
516 | if (!$entry) { | |
517 | $this->markTestSkipped('No content found in db.'); | |
518 | } | |
519 | ||
520 | $nbTags = count($entry->getTags()); | |
521 | ||
522 | $newTags = 'tag1,tag2,tag3'; | |
523 | ||
524 | $this->client->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]); | |
525 | ||
526 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
527 | ||
528 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
529 | ||
530 | $this->assertArrayHasKey('tags', $content); | |
531 | $this->assertEquals($nbTags + 3, count($content['tags'])); | |
532 | ||
533 | $entryDB = $this->client->getContainer() | |
534 | ->get('doctrine.orm.entity_manager') | |
535 | ->getRepository('WallabagCoreBundle:Entry') | |
536 | ->find($entry->getId()); | |
537 | ||
538 | $tagsInDB = []; | |
539 | foreach ($entryDB->getTags()->toArray() as $tag) { | |
540 | $tagsInDB[$tag->getId()] = $tag->getLabel(); | |
541 | } | |
542 | ||
543 | foreach (explode(',', $newTags) as $tag) { | |
544 | $this->assertContains($tag, $tagsInDB); | |
545 | } | |
546 | } | |
547 | ||
548 | public function testDeleteOneTagEntry() | |
549 | { | |
550 | $entry = $this->client->getContainer() | |
551 | ->get('doctrine.orm.entity_manager') | |
552 | ->getRepository('WallabagCoreBundle:Entry') | |
553 | ->findOneWithTags($this->user->getId()); | |
554 | $entry = $entry[0]; | |
555 | ||
556 | if (!$entry) { | |
557 | $this->markTestSkipped('No content found in db.'); | |
558 | } | |
559 | ||
560 | // hydrate the tags relations | |
561 | $nbTags = count($entry->getTags()); | |
562 | $tag = $entry->getTags()[0]; | |
563 | ||
564 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json'); | |
565 | ||
566 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
567 | ||
568 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
569 | ||
570 | $this->assertArrayHasKey('tags', $content); | |
571 | $this->assertEquals($nbTags - 1, count($content['tags'])); | |
572 | } | |
573 | ||
574 | public function testSaveIsArchivedAfterPost() | |
575 | { | |
576 | $entry = $this->client->getContainer() | |
577 | ->get('doctrine.orm.entity_manager') | |
578 | ->getRepository('WallabagCoreBundle:Entry') | |
579 | ->findOneBy(['user' => 1, 'isArchived' => true]); | |
580 | ||
581 | if (!$entry) { | |
582 | $this->markTestSkipped('No content found in db.'); | |
583 | } | |
584 | ||
585 | $this->client->request('POST', '/api/entries.json', [ | |
586 | 'url' => $entry->getUrl(), | |
587 | ]); | |
588 | ||
589 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
590 | ||
591 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
592 | ||
593 | $this->assertEquals(true, $content['is_archived']); | |
594 | } | |
595 | ||
596 | public function testSaveIsStarredAfterPost() | |
597 | { | |
598 | $entry = $this->client->getContainer() | |
599 | ->get('doctrine.orm.entity_manager') | |
600 | ->getRepository('WallabagCoreBundle:Entry') | |
601 | ->findOneBy(['user' => 1, 'isStarred' => true]); | |
602 | ||
603 | if (!$entry) { | |
604 | $this->markTestSkipped('No content found in db.'); | |
605 | } | |
606 | ||
607 | $this->client->request('POST', '/api/entries.json', [ | |
608 | 'url' => $entry->getUrl(), | |
609 | ]); | |
610 | ||
611 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
612 | ||
613 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
614 | ||
615 | $this->assertEquals(true, $content['is_starred']); | |
616 | } | |
617 | ||
618 | public function testSaveIsArchivedAfterPatch() | |
619 | { | |
620 | $entry = $this->client->getContainer() | |
621 | ->get('doctrine.orm.entity_manager') | |
622 | ->getRepository('WallabagCoreBundle:Entry') | |
623 | ->findOneBy(['user' => 1, 'isArchived' => true]); | |
624 | ||
625 | if (!$entry) { | |
626 | $this->markTestSkipped('No content found in db.'); | |
627 | } | |
628 | ||
629 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | |
630 | 'title' => $entry->getTitle().'++', | |
631 | ]); | |
632 | ||
633 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
634 | ||
635 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
636 | ||
637 | $this->assertEquals(true, $content['is_archived']); | |
638 | } | |
639 | ||
640 | public function testSaveIsStarredAfterPatch() | |
641 | { | |
642 | $entry = $this->client->getContainer() | |
643 | ->get('doctrine.orm.entity_manager') | |
644 | ->getRepository('WallabagCoreBundle:Entry') | |
645 | ->findOneBy(['user' => 1, 'isStarred' => true]); | |
646 | ||
647 | if (!$entry) { | |
648 | $this->markTestSkipped('No content found in db.'); | |
649 | } | |
650 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | |
651 | 'title' => $entry->getTitle().'++', | |
652 | ]); | |
653 | ||
654 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
655 | ||
656 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
657 | ||
658 | $this->assertEquals(true, $content['is_starred']); | |
659 | } | |
660 | ||
661 | public function testGetEntriesExists() | |
662 | { | |
663 | $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2'); | |
664 | ||
665 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
666 | ||
667 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
668 | ||
669 | $this->assertEquals(true, $content['exists']); | |
670 | } | |
671 | ||
672 | public function testGetEntriesExistsWithManyUrls() | |
673 | { | |
674 | $url1 = 'http://0.0.0.0/entry2'; | |
675 | $url2 = 'http://0.0.0.0/entry10'; | |
676 | $this->client->request('GET', '/api/entries/exists?urls[]='.$url1.'&urls[]='.$url2); | |
677 | ||
678 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
679 | ||
680 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
681 | ||
682 | $this->assertArrayHasKey($url1, $content); | |
683 | $this->assertArrayHasKey($url2, $content); | |
684 | $this->assertEquals(true, $content[$url1]); | |
685 | $this->assertEquals(false, $content[$url2]); | |
686 | } | |
687 | ||
688 | public function testGetEntriesExistsWhichDoesNotExists() | |
689 | { | |
690 | $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2'); | |
691 | ||
692 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
693 | ||
694 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
695 | ||
696 | $this->assertEquals(false, $content['exists']); | |
697 | } | |
698 | ||
699 | public function testGetEntriesExistsWithNoUrl() | |
700 | { | |
701 | $this->client->request('GET', '/api/entries/exists?url='); | |
702 | ||
703 | $this->assertEquals(403, $this->client->getResponse()->getStatusCode()); | |
704 | } | |
0a6f4568 JB |
705 | |
706 | public function testReloadEntryErrorWhileFetching() | |
707 | { | |
70584b42 | 708 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') |
0a6f4568 | 709 | ->getRepository('WallabagCoreBundle:Entry') |
70584b42 | 710 | ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); |
0a6f4568 JB |
711 | |
712 | if (!$entry) { | |
713 | $this->markTestSkipped('No content found in db.'); | |
714 | } | |
715 | ||
716 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'/reload.json'); | |
5cd0857e | 717 | $this->assertEquals(304, $this->client->getResponse()->getStatusCode()); |
0a6f4568 JB |
718 | } |
719 | ||
720 | public function testReloadEntry() | |
721 | { | |
722 | $this->client->request('POST', '/api/entries.json', [ | |
723 | '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', | |
724 | 'archive' => '1', | |
725 | 'tags' => 'google, apple', | |
726 | ]); | |
727 | ||
728 | $json = json_decode($this->client->getResponse()->getContent(), true); | |
729 | ||
730 | $this->setUp(); | |
731 | ||
732 | $this->client->request('PATCH', '/api/entries/'.$json['id'].'/reload.json'); | |
733 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
734 | ||
735 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
736 | ||
737 | $this->assertNotEmpty($content['title']); | |
738 | ||
739 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
740 | } | |
900c8448 | 741 | } |