]>
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 | ||
a9357a83 | 376 | try { |
377 | $this->client->request('POST', '/api/entries.json', [ | |
378 | 'url' => 'http://www.example.com/', | |
379 | ]); | |
380 | ||
381 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
382 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
383 | $this->assertGreaterThan(0, $content['id']); | |
384 | $this->assertEquals('http://www.example.com/', $content['url']); | |
385 | } finally { | |
386 | // Remove the created entry to avoid side effects on other tests | |
387 | if (isset($content['id'])) { | |
388 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
389 | $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']); | |
390 | $em->remove($entry); | |
391 | $em->flush(); | |
392 | } | |
393 | } | |
08f29ae7 | 394 | } |
395 | ||
900c8448 NL |
396 | public function testPostArchivedAndStarredEntry() |
397 | { | |
398 | $this->client->request('POST', '/api/entries.json', [ | |
399 | 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', | |
400 | 'archive' => '1', | |
401 | 'starred' => '1', | |
402 | ]); | |
403 | ||
404 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
405 | ||
406 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
407 | ||
408 | $this->assertGreaterThan(0, $content['id']); | |
409 | $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']); | |
410 | $this->assertEquals(true, $content['is_archived']); | |
411 | $this->assertEquals(true, $content['is_starred']); | |
412 | $this->assertEquals(1, $content['user_id']); | |
413 | } | |
414 | ||
415 | public function testPostArchivedAndStarredEntryWithoutQuotes() | |
416 | { | |
417 | $this->client->request('POST', '/api/entries.json', [ | |
418 | 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', | |
419 | 'archive' => 0, | |
420 | 'starred' => 1, | |
421 | ]); | |
422 | ||
423 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
424 | ||
425 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
426 | ||
427 | $this->assertGreaterThan(0, $content['id']); | |
428 | $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']); | |
429 | $this->assertEquals(false, $content['is_archived']); | |
430 | $this->assertEquals(true, $content['is_starred']); | |
431 | } | |
432 | ||
433 | public function testPatchEntry() | |
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 | $this->assertEquals(1, $content['user_id']); | |
463 | } | |
464 | ||
465 | public function testPatchEntryWithoutQuotes() | |
466 | { | |
467 | $entry = $this->client->getContainer() | |
468 | ->get('doctrine.orm.entity_manager') | |
469 | ->getRepository('WallabagCoreBundle:Entry') | |
470 | ->findOneByUser(1); | |
471 | ||
472 | if (!$entry) { | |
473 | $this->markTestSkipped('No content found in db.'); | |
474 | } | |
475 | ||
476 | // hydrate the tags relations | |
477 | $nbTags = count($entry->getTags()); | |
478 | ||
479 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | |
480 | 'title' => 'New awesome title', | |
481 | 'tags' => 'new tag '.uniqid(), | |
482 | 'starred' => 1, | |
483 | 'archive' => 0, | |
484 | ]); | |
485 | ||
486 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
487 | ||
488 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
489 | ||
490 | $this->assertEquals($entry->getId(), $content['id']); | |
491 | $this->assertEquals($entry->getUrl(), $content['url']); | |
492 | $this->assertEquals('New awesome title', $content['title']); | |
493 | $this->assertGreaterThan($nbTags, count($content['tags'])); | |
494 | } | |
495 | ||
496 | public function testGetTagsEntry() | |
497 | { | |
498 | $entry = $this->client->getContainer() | |
499 | ->get('doctrine.orm.entity_manager') | |
500 | ->getRepository('WallabagCoreBundle:Entry') | |
501 | ->findOneWithTags($this->user->getId()); | |
502 | ||
503 | $entry = $entry[0]; | |
504 | ||
505 | if (!$entry) { | |
506 | $this->markTestSkipped('No content found in db.'); | |
507 | } | |
508 | ||
509 | $tags = []; | |
510 | foreach ($entry->getTags() as $tag) { | |
511 | $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()]; | |
512 | } | |
513 | ||
514 | $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags'); | |
515 | ||
516 | $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent()); | |
517 | } | |
518 | ||
519 | public function testPostTagsOnEntry() | |
520 | { | |
521 | $entry = $this->client->getContainer() | |
522 | ->get('doctrine.orm.entity_manager') | |
523 | ->getRepository('WallabagCoreBundle:Entry') | |
524 | ->findOneByUser(1); | |
525 | ||
526 | if (!$entry) { | |
527 | $this->markTestSkipped('No content found in db.'); | |
528 | } | |
529 | ||
530 | $nbTags = count($entry->getTags()); | |
531 | ||
532 | $newTags = 'tag1,tag2,tag3'; | |
533 | ||
534 | $this->client->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]); | |
535 | ||
536 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
537 | ||
538 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
539 | ||
540 | $this->assertArrayHasKey('tags', $content); | |
541 | $this->assertEquals($nbTags + 3, count($content['tags'])); | |
542 | ||
543 | $entryDB = $this->client->getContainer() | |
544 | ->get('doctrine.orm.entity_manager') | |
545 | ->getRepository('WallabagCoreBundle:Entry') | |
546 | ->find($entry->getId()); | |
547 | ||
548 | $tagsInDB = []; | |
549 | foreach ($entryDB->getTags()->toArray() as $tag) { | |
550 | $tagsInDB[$tag->getId()] = $tag->getLabel(); | |
551 | } | |
552 | ||
553 | foreach (explode(',', $newTags) as $tag) { | |
554 | $this->assertContains($tag, $tagsInDB); | |
555 | } | |
556 | } | |
557 | ||
558 | public function testDeleteOneTagEntry() | |
559 | { | |
560 | $entry = $this->client->getContainer() | |
561 | ->get('doctrine.orm.entity_manager') | |
562 | ->getRepository('WallabagCoreBundle:Entry') | |
563 | ->findOneWithTags($this->user->getId()); | |
564 | $entry = $entry[0]; | |
565 | ||
566 | if (!$entry) { | |
567 | $this->markTestSkipped('No content found in db.'); | |
568 | } | |
569 | ||
570 | // hydrate the tags relations | |
571 | $nbTags = count($entry->getTags()); | |
572 | $tag = $entry->getTags()[0]; | |
573 | ||
574 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json'); | |
575 | ||
576 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
577 | ||
578 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
579 | ||
580 | $this->assertArrayHasKey('tags', $content); | |
581 | $this->assertEquals($nbTags - 1, count($content['tags'])); | |
582 | } | |
583 | ||
584 | public function testSaveIsArchivedAfterPost() | |
585 | { | |
586 | $entry = $this->client->getContainer() | |
587 | ->get('doctrine.orm.entity_manager') | |
588 | ->getRepository('WallabagCoreBundle:Entry') | |
589 | ->findOneBy(['user' => 1, 'isArchived' => true]); | |
590 | ||
591 | if (!$entry) { | |
592 | $this->markTestSkipped('No content found in db.'); | |
593 | } | |
594 | ||
595 | $this->client->request('POST', '/api/entries.json', [ | |
596 | 'url' => $entry->getUrl(), | |
597 | ]); | |
598 | ||
599 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
600 | ||
601 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
602 | ||
603 | $this->assertEquals(true, $content['is_archived']); | |
604 | } | |
605 | ||
606 | public function testSaveIsStarredAfterPost() | |
607 | { | |
608 | $entry = $this->client->getContainer() | |
609 | ->get('doctrine.orm.entity_manager') | |
610 | ->getRepository('WallabagCoreBundle:Entry') | |
611 | ->findOneBy(['user' => 1, 'isStarred' => true]); | |
612 | ||
613 | if (!$entry) { | |
614 | $this->markTestSkipped('No content found in db.'); | |
615 | } | |
616 | ||
617 | $this->client->request('POST', '/api/entries.json', [ | |
618 | 'url' => $entry->getUrl(), | |
619 | ]); | |
620 | ||
621 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
622 | ||
623 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
624 | ||
625 | $this->assertEquals(true, $content['is_starred']); | |
626 | } | |
627 | ||
628 | public function testSaveIsArchivedAfterPatch() | |
629 | { | |
630 | $entry = $this->client->getContainer() | |
631 | ->get('doctrine.orm.entity_manager') | |
632 | ->getRepository('WallabagCoreBundle:Entry') | |
633 | ->findOneBy(['user' => 1, 'isArchived' => true]); | |
634 | ||
635 | if (!$entry) { | |
636 | $this->markTestSkipped('No content found in db.'); | |
637 | } | |
638 | ||
639 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | |
640 | 'title' => $entry->getTitle().'++', | |
641 | ]); | |
642 | ||
643 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
644 | ||
645 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
646 | ||
647 | $this->assertEquals(true, $content['is_archived']); | |
648 | } | |
649 | ||
650 | public function testSaveIsStarredAfterPatch() | |
651 | { | |
652 | $entry = $this->client->getContainer() | |
653 | ->get('doctrine.orm.entity_manager') | |
654 | ->getRepository('WallabagCoreBundle:Entry') | |
655 | ->findOneBy(['user' => 1, 'isStarred' => true]); | |
656 | ||
657 | if (!$entry) { | |
658 | $this->markTestSkipped('No content found in db.'); | |
659 | } | |
660 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | |
661 | 'title' => $entry->getTitle().'++', | |
662 | ]); | |
663 | ||
664 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
665 | ||
666 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
667 | ||
668 | $this->assertEquals(true, $content['is_starred']); | |
669 | } | |
670 | ||
671 | public function testGetEntriesExists() | |
672 | { | |
673 | $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2'); | |
674 | ||
675 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
676 | ||
677 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
678 | ||
679 | $this->assertEquals(true, $content['exists']); | |
680 | } | |
681 | ||
682 | public function testGetEntriesExistsWithManyUrls() | |
683 | { | |
684 | $url1 = 'http://0.0.0.0/entry2'; | |
685 | $url2 = 'http://0.0.0.0/entry10'; | |
686 | $this->client->request('GET', '/api/entries/exists?urls[]='.$url1.'&urls[]='.$url2); | |
687 | ||
688 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
689 | ||
690 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
691 | ||
692 | $this->assertArrayHasKey($url1, $content); | |
693 | $this->assertArrayHasKey($url2, $content); | |
694 | $this->assertEquals(true, $content[$url1]); | |
695 | $this->assertEquals(false, $content[$url2]); | |
696 | } | |
697 | ||
698 | public function testGetEntriesExistsWhichDoesNotExists() | |
699 | { | |
700 | $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2'); | |
701 | ||
702 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
703 | ||
704 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
705 | ||
706 | $this->assertEquals(false, $content['exists']); | |
707 | } | |
708 | ||
709 | public function testGetEntriesExistsWithNoUrl() | |
710 | { | |
711 | $this->client->request('GET', '/api/entries/exists?url='); | |
712 | ||
713 | $this->assertEquals(403, $this->client->getResponse()->getStatusCode()); | |
714 | } | |
0a6f4568 JB |
715 | |
716 | public function testReloadEntryErrorWhileFetching() | |
717 | { | |
70584b42 | 718 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') |
0a6f4568 | 719 | ->getRepository('WallabagCoreBundle:Entry') |
70584b42 | 720 | ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); |
0a6f4568 JB |
721 | |
722 | if (!$entry) { | |
723 | $this->markTestSkipped('No content found in db.'); | |
724 | } | |
725 | ||
726 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'/reload.json'); | |
5cd0857e | 727 | $this->assertEquals(304, $this->client->getResponse()->getStatusCode()); |
0a6f4568 JB |
728 | } |
729 | ||
730 | public function testReloadEntry() | |
731 | { | |
732 | $this->client->request('POST', '/api/entries.json', [ | |
733 | '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', | |
734 | 'archive' => '1', | |
735 | 'tags' => 'google, apple', | |
736 | ]); | |
737 | ||
738 | $json = json_decode($this->client->getResponse()->getContent(), true); | |
739 | ||
740 | $this->setUp(); | |
741 | ||
742 | $this->client->request('PATCH', '/api/entries/'.$json['id'].'/reload.json'); | |
743 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
744 | ||
745 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
746 | ||
747 | $this->assertNotEmpty($content['title']); | |
748 | ||
749 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
750 | } | |
900c8448 | 751 | } |