]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Tests\Wallabag\ApiBundle\Controller; | |
4 | ||
5 | use Tests\Wallabag\ApiBundle\WallabagApiTestCase; | |
6 | use Wallabag\CoreBundle\Entity\Tag; | |
7 | use Wallabag\CoreBundle\Helper\ContentProxy; | |
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 | ||
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')); | |
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 | ||
115 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
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 | ||
157 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
158 | } | |
159 | ||
160 | public function testGetEntriesOnPageTwo() | |
161 | { | |
162 | $this->client->request('GET', '/api/entries', [ | |
163 | 'page' => 2, | |
164 | 'perPage' => 2, | |
165 | ]); | |
166 | ||
167 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
168 | ||
169 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
170 | ||
171 | $this->assertGreaterThanOrEqual(0, $content['total']); | |
172 | $this->assertEquals(2, $content['page']); | |
173 | $this->assertEquals(2, $content['limit']); | |
174 | } | |
175 | ||
176 | public function testGetStarredEntries() | |
177 | { | |
178 | $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']); | |
179 | ||
180 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
181 | ||
182 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
183 | ||
184 | $this->assertGreaterThanOrEqual(1, count($content)); | |
185 | $this->assertNotEmpty($content['_embedded']['items']); | |
186 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
187 | $this->assertEquals(1, $content['page']); | |
188 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
189 | ||
190 | $this->assertArrayHasKey('_links', $content); | |
191 | $this->assertArrayHasKey('self', $content['_links']); | |
192 | $this->assertArrayHasKey('first', $content['_links']); | |
193 | $this->assertArrayHasKey('last', $content['_links']); | |
194 | ||
195 | foreach (['self', 'first', 'last'] as $link) { | |
196 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
197 | $this->assertContains('starred=1', $content['_links'][$link]['href']); | |
198 | $this->assertContains('sort=updated', $content['_links'][$link]['href']); | |
199 | } | |
200 | ||
201 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
202 | } | |
203 | ||
204 | public function testGetArchiveEntries() | |
205 | { | |
206 | $this->client->request('GET', '/api/entries', ['archive' => 1]); | |
207 | ||
208 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
209 | ||
210 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
211 | ||
212 | $this->assertGreaterThanOrEqual(1, count($content)); | |
213 | $this->assertNotEmpty($content['_embedded']['items']); | |
214 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
215 | $this->assertEquals(1, $content['page']); | |
216 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
217 | ||
218 | $this->assertArrayHasKey('_links', $content); | |
219 | $this->assertArrayHasKey('self', $content['_links']); | |
220 | $this->assertArrayHasKey('first', $content['_links']); | |
221 | $this->assertArrayHasKey('last', $content['_links']); | |
222 | ||
223 | foreach (['self', 'first', 'last'] as $link) { | |
224 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
225 | $this->assertContains('archive=1', $content['_links'][$link]['href']); | |
226 | } | |
227 | ||
228 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
229 | } | |
230 | ||
231 | public function testGetTaggedEntries() | |
232 | { | |
233 | $this->client->request('GET', '/api/entries', ['tags' => 'foo,bar']); | |
234 | ||
235 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
236 | ||
237 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
238 | ||
239 | $this->assertGreaterThanOrEqual(1, count($content)); | |
240 | $this->assertNotEmpty($content['_embedded']['items']); | |
241 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
242 | $this->assertEquals(1, $content['page']); | |
243 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
244 | ||
245 | $this->assertArrayHasKey('_links', $content); | |
246 | $this->assertArrayHasKey('self', $content['_links']); | |
247 | $this->assertArrayHasKey('first', $content['_links']); | |
248 | $this->assertArrayHasKey('last', $content['_links']); | |
249 | ||
250 | foreach (['self', 'first', 'last'] as $link) { | |
251 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
252 | $this->assertContains('tags='.urlencode('foo,bar'), $content['_links'][$link]['href']); | |
253 | } | |
254 | ||
255 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
256 | } | |
257 | ||
258 | public function testGetDatedEntries() | |
259 | { | |
260 | $this->client->request('GET', '/api/entries', ['since' => 1443274283]); | |
261 | ||
262 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
263 | ||
264 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
265 | ||
266 | $this->assertGreaterThanOrEqual(1, count($content)); | |
267 | $this->assertNotEmpty($content['_embedded']['items']); | |
268 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
269 | $this->assertEquals(1, $content['page']); | |
270 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
271 | ||
272 | $this->assertArrayHasKey('_links', $content); | |
273 | $this->assertArrayHasKey('self', $content['_links']); | |
274 | $this->assertArrayHasKey('first', $content['_links']); | |
275 | $this->assertArrayHasKey('last', $content['_links']); | |
276 | ||
277 | foreach (['self', 'first', 'last'] as $link) { | |
278 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
279 | $this->assertContains('since=1443274283', $content['_links'][$link]['href']); | |
280 | } | |
281 | ||
282 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
283 | } | |
284 | ||
285 | public function testGetDatedSupEntries() | |
286 | { | |
287 | $future = new \DateTime(date('Y-m-d H:i:s')); | |
288 | $this->client->request('GET', '/api/entries', ['since' => $future->getTimestamp() + 1000]); | |
289 | ||
290 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
291 | ||
292 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
293 | ||
294 | $this->assertGreaterThanOrEqual(1, count($content)); | |
295 | $this->assertEmpty($content['_embedded']['items']); | |
296 | $this->assertEquals(0, $content['total']); | |
297 | $this->assertEquals(1, $content['page']); | |
298 | $this->assertEquals(1, $content['pages']); | |
299 | ||
300 | $this->assertArrayHasKey('_links', $content); | |
301 | $this->assertArrayHasKey('self', $content['_links']); | |
302 | $this->assertArrayHasKey('first', $content['_links']); | |
303 | $this->assertArrayHasKey('last', $content['_links']); | |
304 | ||
305 | foreach (['self', 'first', 'last'] as $link) { | |
306 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
307 | $this->assertContains('since='.($future->getTimestamp() + 1000), $content['_links'][$link]['href']); | |
308 | } | |
309 | ||
310 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
311 | } | |
312 | ||
313 | public function testDeleteEntry() | |
314 | { | |
315 | $entry = $this->client->getContainer() | |
316 | ->get('doctrine.orm.entity_manager') | |
317 | ->getRepository('WallabagCoreBundle:Entry') | |
318 | ->findOneByUser(1, ['id' => 'asc']); | |
319 | ||
320 | if (!$entry) { | |
321 | $this->markTestSkipped('No content found in db.'); | |
322 | } | |
323 | ||
324 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json'); | |
325 | ||
326 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
327 | ||
328 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
329 | ||
330 | $this->assertEquals($entry->getTitle(), $content['title']); | |
331 | $this->assertEquals($entry->getUrl(), $content['url']); | |
332 | ||
333 | // We'll try to delete this entry again | |
334 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json'); | |
335 | ||
336 | $this->assertEquals(404, $this->client->getResponse()->getStatusCode()); | |
337 | } | |
338 | ||
339 | public function testPostEntry() | |
340 | { | |
341 | $this->client->request('POST', '/api/entries.json', [ | |
342 | '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', | |
343 | 'tags' => 'google', | |
344 | 'title' => 'New title for my article', | |
345 | ]); | |
346 | ||
347 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
348 | ||
349 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
350 | ||
351 | $this->assertGreaterThan(0, $content['id']); | |
352 | $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']); | |
353 | $this->assertEquals(false, $content['is_archived']); | |
354 | $this->assertEquals(false, $content['is_starred']); | |
355 | $this->assertEquals('New title for my article', $content['title']); | |
356 | $this->assertEquals(1, $content['user_id']); | |
357 | $this->assertCount(2, $content['tags']); | |
358 | } | |
359 | ||
360 | public function testPostSameEntry() | |
361 | { | |
362 | $this->client->request('POST', '/api/entries.json', [ | |
363 | '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', | |
364 | 'archive' => '1', | |
365 | 'tags' => 'google, apple', | |
366 | ]); | |
367 | ||
368 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
369 | ||
370 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
371 | ||
372 | $this->assertGreaterThan(0, $content['id']); | |
373 | $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']); | |
374 | $this->assertEquals(true, $content['is_archived']); | |
375 | $this->assertEquals(false, $content['is_starred']); | |
376 | $this->assertCount(3, $content['tags']); | |
377 | } | |
378 | ||
379 | public function testPostEntryWhenFetchContentFails() | |
380 | { | |
381 | /** @var \Symfony\Component\DependencyInjection\Container $container */ | |
382 | $container = $this->client->getContainer(); | |
383 | $contentProxy = $this->getMockBuilder(ContentProxy::class) | |
384 | ->disableOriginalConstructor() | |
385 | ->setMethods(['updateEntry']) | |
386 | ->getMock(); | |
387 | $contentProxy->expects($this->any()) | |
388 | ->method('updateEntry') | |
389 | ->willThrowException(new \Exception('Test Fetch content fails')); | |
390 | $container->set('wallabag_core.content_proxy', $contentProxy); | |
391 | ||
392 | try { | |
393 | $this->client->request('POST', '/api/entries.json', [ | |
394 | 'url' => 'http://www.example.com/', | |
395 | ]); | |
396 | ||
397 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
398 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
399 | $this->assertGreaterThan(0, $content['id']); | |
400 | $this->assertEquals('http://www.example.com/', $content['url']); | |
401 | } finally { | |
402 | // Remove the created entry to avoid side effects on other tests | |
403 | if (isset($content['id'])) { | |
404 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
405 | $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']); | |
406 | $em->remove($entry); | |
407 | $em->flush(); | |
408 | } | |
409 | } | |
410 | } | |
411 | ||
412 | public function testPostArchivedAndStarredEntry() | |
413 | { | |
414 | $this->client->request('POST', '/api/entries.json', [ | |
415 | 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', | |
416 | 'archive' => '1', | |
417 | 'starred' => '1', | |
418 | ]); | |
419 | ||
420 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
421 | ||
422 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
423 | ||
424 | $this->assertGreaterThan(0, $content['id']); | |
425 | $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']); | |
426 | $this->assertEquals(true, $content['is_archived']); | |
427 | $this->assertEquals(true, $content['is_starred']); | |
428 | $this->assertEquals(1, $content['user_id']); | |
429 | } | |
430 | ||
431 | public function testPostArchivedAndStarredEntryWithoutQuotes() | |
432 | { | |
433 | $this->client->request('POST', '/api/entries.json', [ | |
434 | 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', | |
435 | 'archive' => 0, | |
436 | 'starred' => 1, | |
437 | ]); | |
438 | ||
439 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
440 | ||
441 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
442 | ||
443 | $this->assertGreaterThan(0, $content['id']); | |
444 | $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']); | |
445 | $this->assertEquals(false, $content['is_archived']); | |
446 | $this->assertEquals(true, $content['is_starred']); | |
447 | } | |
448 | ||
449 | public function testPatchEntry() | |
450 | { | |
451 | $entry = $this->client->getContainer() | |
452 | ->get('doctrine.orm.entity_manager') | |
453 | ->getRepository('WallabagCoreBundle:Entry') | |
454 | ->findOneByUser(1); | |
455 | ||
456 | if (!$entry) { | |
457 | $this->markTestSkipped('No content found in db.'); | |
458 | } | |
459 | ||
460 | // hydrate the tags relations | |
461 | $nbTags = count($entry->getTags()); | |
462 | ||
463 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | |
464 | 'title' => 'New awesome title', | |
465 | 'tags' => 'new tag '.uniqid(), | |
466 | 'starred' => '1', | |
467 | 'archive' => '0', | |
468 | ]); | |
469 | ||
470 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
471 | ||
472 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
473 | ||
474 | $this->assertEquals($entry->getId(), $content['id']); | |
475 | $this->assertEquals($entry->getUrl(), $content['url']); | |
476 | $this->assertEquals('New awesome title', $content['title']); | |
477 | $this->assertGreaterThan($nbTags, count($content['tags'])); | |
478 | $this->assertEquals(1, $content['user_id']); | |
479 | } | |
480 | ||
481 | public function testPatchEntryWithoutQuotes() | |
482 | { | |
483 | $entry = $this->client->getContainer() | |
484 | ->get('doctrine.orm.entity_manager') | |
485 | ->getRepository('WallabagCoreBundle:Entry') | |
486 | ->findOneByUser(1); | |
487 | ||
488 | if (!$entry) { | |
489 | $this->markTestSkipped('No content found in db.'); | |
490 | } | |
491 | ||
492 | // hydrate the tags relations | |
493 | $nbTags = count($entry->getTags()); | |
494 | ||
495 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | |
496 | 'title' => 'New awesome title', | |
497 | 'tags' => 'new tag '.uniqid(), | |
498 | 'starred' => 1, | |
499 | 'archive' => 0, | |
500 | ]); | |
501 | ||
502 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
503 | ||
504 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
505 | ||
506 | $this->assertEquals($entry->getId(), $content['id']); | |
507 | $this->assertEquals($entry->getUrl(), $content['url']); | |
508 | $this->assertEquals('New awesome title', $content['title']); | |
509 | $this->assertGreaterThan($nbTags, count($content['tags'])); | |
510 | } | |
511 | ||
512 | public function testGetTagsEntry() | |
513 | { | |
514 | $entry = $this->client->getContainer() | |
515 | ->get('doctrine.orm.entity_manager') | |
516 | ->getRepository('WallabagCoreBundle:Entry') | |
517 | ->findOneWithTags($this->user->getId()); | |
518 | ||
519 | $entry = $entry[0]; | |
520 | ||
521 | if (!$entry) { | |
522 | $this->markTestSkipped('No content found in db.'); | |
523 | } | |
524 | ||
525 | $tags = []; | |
526 | foreach ($entry->getTags() as $tag) { | |
527 | $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()]; | |
528 | } | |
529 | ||
530 | $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags'); | |
531 | ||
532 | $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent()); | |
533 | } | |
534 | ||
535 | public function testPostTagsOnEntry() | |
536 | { | |
537 | $entry = $this->client->getContainer() | |
538 | ->get('doctrine.orm.entity_manager') | |
539 | ->getRepository('WallabagCoreBundle:Entry') | |
540 | ->findOneByUser(1); | |
541 | ||
542 | if (!$entry) { | |
543 | $this->markTestSkipped('No content found in db.'); | |
544 | } | |
545 | ||
546 | $nbTags = count($entry->getTags()); | |
547 | ||
548 | $newTags = 'tag1,tag2,tag3'; | |
549 | ||
550 | $this->client->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]); | |
551 | ||
552 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
553 | ||
554 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
555 | ||
556 | $this->assertArrayHasKey('tags', $content); | |
557 | $this->assertEquals($nbTags + 3, count($content['tags'])); | |
558 | ||
559 | $entryDB = $this->client->getContainer() | |
560 | ->get('doctrine.orm.entity_manager') | |
561 | ->getRepository('WallabagCoreBundle:Entry') | |
562 | ->find($entry->getId()); | |
563 | ||
564 | $tagsInDB = []; | |
565 | foreach ($entryDB->getTags()->toArray() as $tag) { | |
566 | $tagsInDB[$tag->getId()] = $tag->getLabel(); | |
567 | } | |
568 | ||
569 | foreach (explode(',', $newTags) as $tag) { | |
570 | $this->assertContains($tag, $tagsInDB); | |
571 | } | |
572 | } | |
573 | ||
574 | public function testDeleteOneTagEntry() | |
575 | { | |
576 | $entry = $this->client->getContainer() | |
577 | ->get('doctrine.orm.entity_manager') | |
578 | ->getRepository('WallabagCoreBundle:Entry') | |
579 | ->findOneWithTags($this->user->getId()); | |
580 | $entry = $entry[0]; | |
581 | ||
582 | if (!$entry) { | |
583 | $this->markTestSkipped('No content found in db.'); | |
584 | } | |
585 | ||
586 | // hydrate the tags relations | |
587 | $nbTags = count($entry->getTags()); | |
588 | $tag = $entry->getTags()[0]; | |
589 | ||
590 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json'); | |
591 | ||
592 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
593 | ||
594 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
595 | ||
596 | $this->assertArrayHasKey('tags', $content); | |
597 | $this->assertEquals($nbTags - 1, count($content['tags'])); | |
598 | } | |
599 | ||
600 | public function testSaveIsArchivedAfterPost() | |
601 | { | |
602 | $entry = $this->client->getContainer() | |
603 | ->get('doctrine.orm.entity_manager') | |
604 | ->getRepository('WallabagCoreBundle:Entry') | |
605 | ->findOneBy(['user' => 1, 'isArchived' => true]); | |
606 | ||
607 | if (!$entry) { | |
608 | $this->markTestSkipped('No content found in db.'); | |
609 | } | |
610 | ||
611 | $this->client->request('POST', '/api/entries.json', [ | |
612 | 'url' => $entry->getUrl(), | |
613 | ]); | |
614 | ||
615 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
616 | ||
617 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
618 | ||
619 | $this->assertEquals(true, $content['is_archived']); | |
620 | } | |
621 | ||
622 | public function testSaveIsStarredAfterPost() | |
623 | { | |
624 | $entry = $this->client->getContainer() | |
625 | ->get('doctrine.orm.entity_manager') | |
626 | ->getRepository('WallabagCoreBundle:Entry') | |
627 | ->findOneBy(['user' => 1, 'isStarred' => true]); | |
628 | ||
629 | if (!$entry) { | |
630 | $this->markTestSkipped('No content found in db.'); | |
631 | } | |
632 | ||
633 | $this->client->request('POST', '/api/entries.json', [ | |
634 | 'url' => $entry->getUrl(), | |
635 | ]); | |
636 | ||
637 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
638 | ||
639 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
640 | ||
641 | $this->assertEquals(true, $content['is_starred']); | |
642 | } | |
643 | ||
644 | public function testSaveIsArchivedAfterPatch() | |
645 | { | |
646 | $entry = $this->client->getContainer() | |
647 | ->get('doctrine.orm.entity_manager') | |
648 | ->getRepository('WallabagCoreBundle:Entry') | |
649 | ->findOneBy(['user' => 1, 'isArchived' => true]); | |
650 | ||
651 | if (!$entry) { | |
652 | $this->markTestSkipped('No content found in db.'); | |
653 | } | |
654 | ||
655 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | |
656 | 'title' => $entry->getTitle().'++', | |
657 | ]); | |
658 | ||
659 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
660 | ||
661 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
662 | ||
663 | $this->assertEquals(true, $content['is_archived']); | |
664 | } | |
665 | ||
666 | public function testSaveIsStarredAfterPatch() | |
667 | { | |
668 | $entry = $this->client->getContainer() | |
669 | ->get('doctrine.orm.entity_manager') | |
670 | ->getRepository('WallabagCoreBundle:Entry') | |
671 | ->findOneBy(['user' => 1, 'isStarred' => true]); | |
672 | ||
673 | if (!$entry) { | |
674 | $this->markTestSkipped('No content found in db.'); | |
675 | } | |
676 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | |
677 | 'title' => $entry->getTitle().'++', | |
678 | ]); | |
679 | ||
680 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
681 | ||
682 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
683 | ||
684 | $this->assertEquals(true, $content['is_starred']); | |
685 | } | |
686 | ||
687 | public function testGetEntriesExists() | |
688 | { | |
689 | $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2'); | |
690 | ||
691 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
692 | ||
693 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
694 | ||
695 | $this->assertEquals(2, $content['exists']); | |
696 | } | |
697 | ||
698 | public function testGetEntriesExistsWithManyUrls() | |
699 | { | |
700 | $url1 = 'http://0.0.0.0/entry2'; | |
701 | $url2 = 'http://0.0.0.0/entry10'; | |
702 | $this->client->request('GET', '/api/entries/exists?urls[]='.$url1.'&urls[]='.$url2); | |
703 | ||
704 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
705 | ||
706 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
707 | ||
708 | $this->assertArrayHasKey($url1, $content); | |
709 | $this->assertArrayHasKey($url2, $content); | |
710 | $this->assertEquals(2, $content[$url1]); | |
711 | $this->assertEquals(false, $content[$url2]); | |
712 | } | |
713 | ||
714 | public function testGetEntriesExistsWhichDoesNotExists() | |
715 | { | |
716 | $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2'); | |
717 | ||
718 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
719 | ||
720 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
721 | ||
722 | $this->assertEquals(false, $content['exists']); | |
723 | } | |
724 | ||
725 | public function testGetEntriesExistsWithNoUrl() | |
726 | { | |
727 | $this->client->request('GET', '/api/entries/exists?url='); | |
728 | ||
729 | $this->assertEquals(403, $this->client->getResponse()->getStatusCode()); | |
730 | } | |
731 | ||
732 | public function testReloadEntryErrorWhileFetching() | |
733 | { | |
734 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') | |
735 | ->getRepository('WallabagCoreBundle:Entry') | |
736 | ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); | |
737 | ||
738 | if (!$entry) { | |
739 | $this->markTestSkipped('No content found in db.'); | |
740 | } | |
741 | ||
742 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'/reload.json'); | |
743 | $this->assertEquals(304, $this->client->getResponse()->getStatusCode()); | |
744 | } | |
745 | ||
746 | public function testReloadEntry() | |
747 | { | |
748 | $this->client->request('POST', '/api/entries.json', [ | |
749 | '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', | |
750 | 'archive' => '1', | |
751 | 'tags' => 'google, apple', | |
752 | ]); | |
753 | ||
754 | $json = json_decode($this->client->getResponse()->getContent(), true); | |
755 | ||
756 | $this->setUp(); | |
757 | ||
758 | $this->client->request('PATCH', '/api/entries/'.$json['id'].'/reload.json'); | |
759 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
760 | ||
761 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
762 | ||
763 | $this->assertNotEmpty($content['title']); | |
764 | ||
765 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
766 | } | |
767 | ||
768 | public function testPostEntriesTagsListAction() | |
769 | { | |
770 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') | |
771 | ->getRepository('WallabagCoreBundle:Entry') | |
772 | ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); | |
773 | ||
774 | $tags = $entry->getTags(); | |
775 | ||
776 | $this->assertCount(2, $tags); | |
777 | ||
778 | $list = [ | |
779 | [ | |
780 | 'url' => 'http://0.0.0.0/entry4', | |
781 | 'tags' => 'new tag 1, new tag 2', | |
782 | ], | |
783 | ]; | |
784 | ||
785 | $this->client->request('POST', '/api/entries/tags/lists?list='.json_encode($list)); | |
786 | ||
787 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
788 | ||
789 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
790 | ||
791 | $this->assertInternalType('int', $content[0]['entry']); | |
792 | $this->assertEquals('http://0.0.0.0/entry4', $content[0]['url']); | |
793 | ||
794 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') | |
795 | ->getRepository('WallabagCoreBundle:Entry') | |
796 | ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); | |
797 | ||
798 | $tags = $entry->getTags(); | |
799 | $this->assertCount(4, $tags); | |
800 | } | |
801 | ||
802 | public function testDeleteEntriesTagsListAction() | |
803 | { | |
804 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') | |
805 | ->getRepository('WallabagCoreBundle:Entry') | |
806 | ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); | |
807 | ||
808 | $tags = $entry->getTags(); | |
809 | ||
810 | $this->assertCount(4, $tags); | |
811 | ||
812 | $list = [ | |
813 | [ | |
814 | 'url' => 'http://0.0.0.0/entry4', | |
815 | 'tags' => 'new tag 1, new tag 2', | |
816 | ], | |
817 | ]; | |
818 | ||
819 | $this->client->request('DELETE', '/api/entries/tags/list?list='.json_encode($list)); | |
820 | } | |
821 | ||
822 | public function testPostEntriesListAction() | |
823 | { | |
824 | $list = [ | |
825 | 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', | |
826 | 'http://0.0.0.0/entry2', | |
827 | ]; | |
828 | ||
829 | $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list)); | |
830 | ||
831 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
832 | ||
833 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
834 | ||
835 | $this->assertInternalType('int', $content[0]['entry']); | |
836 | $this->assertEquals('http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', $content[0]['url']); | |
837 | ||
838 | $this->assertInternalType('int', $content[1]['entry']); | |
839 | $this->assertEquals('http://0.0.0.0/entry2', $content[1]['url']); | |
840 | } | |
841 | ||
842 | public function testDeleteEntriesListAction() | |
843 | { | |
844 | $list = [ | |
845 | 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', | |
846 | 'http://0.0.0.0/entry3', | |
847 | ]; | |
848 | ||
849 | $this->client->request('DELETE', '/api/entries/list?urls='.json_encode($list)); | |
850 | ||
851 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
852 | ||
853 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
854 | ||
855 | $this->assertTrue($content[0]['entry']); | |
856 | $this->assertEquals('http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', $content[0]['url']); | |
857 | ||
858 | $this->assertFalse($content[1]['entry']); | |
859 | $this->assertEquals('http://0.0.0.0/entry3', $content[1]['url']); | |
860 | } | |
861 | ||
862 | public function testLimitBulkAction() | |
863 | { | |
864 | $list = [ | |
865 | 'http://0.0.0.0/entry1', | |
866 | 'http://0.0.0.0/entry1', | |
867 | 'http://0.0.0.0/entry1', | |
868 | 'http://0.0.0.0/entry1', | |
869 | 'http://0.0.0.0/entry1', | |
870 | 'http://0.0.0.0/entry1', | |
871 | 'http://0.0.0.0/entry1', | |
872 | 'http://0.0.0.0/entry1', | |
873 | 'http://0.0.0.0/entry1', | |
874 | 'http://0.0.0.0/entry1', | |
875 | 'http://0.0.0.0/entry1', | |
876 | ]; | |
877 | ||
878 | $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list)); | |
879 | ||
880 | $this->assertEquals(400, $this->client->getResponse()->getStatusCode()); | |
881 | $this->assertContains('API limit reached', $this->client->getResponse()->getContent()); | |
882 | } | |
883 | } |