]>
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 | ||
b60a666d | 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 | ||
900c8448 NL |
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 | ||
5a619812 | 201 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
900c8448 NL |
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 | ||
5a619812 | 228 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
900c8448 NL |
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 | ||
5a619812 | 255 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
900c8448 NL |
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 | ||
5a619812 | 282 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
900c8448 NL |
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 | ||
5a619812 | 310 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
900c8448 NL |
311 | } |
312 | ||
313 | public function testDeleteEntry() | |
314 | { | |
315 | $entry = $this->client->getContainer() | |
316 | ->get('doctrine.orm.entity_manager') | |
317 | ->getRepository('WallabagCoreBundle:Entry') | |
dcbebc17 | 318 | ->findOneByUser(1, ['id' => 'asc']); |
900c8448 NL |
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', | |
e668a812 JB |
345 | 'content' => 'my content', |
346 | 'language' => 'de_DE', | |
347 | 'published_at' => '2016-09-08T11:55:58+0200', | |
900c8448 NL |
348 | ]); |
349 | ||
350 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
351 | ||
352 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
353 | ||
354 | $this->assertGreaterThan(0, $content['id']); | |
355 | $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']); | |
356 | $this->assertEquals(false, $content['is_archived']); | |
357 | $this->assertEquals(false, $content['is_starred']); | |
358 | $this->assertEquals('New title for my article', $content['title']); | |
359 | $this->assertEquals(1, $content['user_id']); | |
fdd725f5 | 360 | $this->assertCount(2, $content['tags']); |
e668a812 JB |
361 | $this->assertSame('my content', $content['content']); |
362 | $this->assertSame('de_DE', $content['language']); | |
363 | $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']); | |
900c8448 NL |
364 | } |
365 | ||
366 | public function testPostSameEntry() | |
367 | { | |
368 | $this->client->request('POST', '/api/entries.json', [ | |
369 | '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', | |
370 | 'archive' => '1', | |
371 | 'tags' => 'google, apple', | |
372 | ]); | |
373 | ||
374 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
375 | ||
376 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
377 | ||
378 | $this->assertGreaterThan(0, $content['id']); | |
379 | $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']); | |
380 | $this->assertEquals(true, $content['is_archived']); | |
381 | $this->assertEquals(false, $content['is_starred']); | |
fdd725f5 | 382 | $this->assertCount(3, $content['tags']); |
900c8448 NL |
383 | } |
384 | ||
08f29ae7 | 385 | public function testPostEntryWhenFetchContentFails() |
386 | { | |
387 | /** @var \Symfony\Component\DependencyInjection\Container $container */ | |
388 | $container = $this->client->getContainer(); | |
389 | $contentProxy = $this->getMockBuilder(ContentProxy::class) | |
390 | ->disableOriginalConstructor() | |
391 | ->setMethods(['updateEntry']) | |
392 | ->getMock(); | |
393 | $contentProxy->expects($this->any()) | |
394 | ->method('updateEntry') | |
395 | ->willThrowException(new \Exception('Test Fetch content fails')); | |
396 | $container->set('wallabag_core.content_proxy', $contentProxy); | |
397 | ||
a9357a83 | 398 | try { |
399 | $this->client->request('POST', '/api/entries.json', [ | |
400 | 'url' => 'http://www.example.com/', | |
401 | ]); | |
402 | ||
403 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
404 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
405 | $this->assertGreaterThan(0, $content['id']); | |
406 | $this->assertEquals('http://www.example.com/', $content['url']); | |
407 | } finally { | |
408 | // Remove the created entry to avoid side effects on other tests | |
409 | if (isset($content['id'])) { | |
410 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
411 | $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']); | |
412 | $em->remove($entry); | |
413 | $em->flush(); | |
414 | } | |
415 | } | |
08f29ae7 | 416 | } |
417 | ||
900c8448 NL |
418 | public function testPostArchivedAndStarredEntry() |
419 | { | |
420 | $this->client->request('POST', '/api/entries.json', [ | |
421 | 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', | |
422 | 'archive' => '1', | |
423 | 'starred' => '1', | |
424 | ]); | |
425 | ||
426 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
427 | ||
428 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
429 | ||
430 | $this->assertGreaterThan(0, $content['id']); | |
431 | $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']); | |
432 | $this->assertEquals(true, $content['is_archived']); | |
433 | $this->assertEquals(true, $content['is_starred']); | |
434 | $this->assertEquals(1, $content['user_id']); | |
435 | } | |
436 | ||
437 | public function testPostArchivedAndStarredEntryWithoutQuotes() | |
438 | { | |
439 | $this->client->request('POST', '/api/entries.json', [ | |
440 | 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', | |
441 | 'archive' => 0, | |
442 | 'starred' => 1, | |
443 | ]); | |
444 | ||
445 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
446 | ||
447 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
448 | ||
449 | $this->assertGreaterThan(0, $content['id']); | |
450 | $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']); | |
451 | $this->assertEquals(false, $content['is_archived']); | |
452 | $this->assertEquals(true, $content['is_starred']); | |
453 | } | |
454 | ||
455 | public function testPatchEntry() | |
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 | $this->assertEquals(1, $content['user_id']); | |
485 | } | |
486 | ||
487 | public function testPatchEntryWithoutQuotes() | |
488 | { | |
489 | $entry = $this->client->getContainer() | |
490 | ->get('doctrine.orm.entity_manager') | |
491 | ->getRepository('WallabagCoreBundle:Entry') | |
492 | ->findOneByUser(1); | |
493 | ||
494 | if (!$entry) { | |
495 | $this->markTestSkipped('No content found in db.'); | |
496 | } | |
497 | ||
498 | // hydrate the tags relations | |
499 | $nbTags = count($entry->getTags()); | |
500 | ||
501 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | |
502 | 'title' => 'New awesome title', | |
503 | 'tags' => 'new tag '.uniqid(), | |
504 | 'starred' => 1, | |
505 | 'archive' => 0, | |
506 | ]); | |
507 | ||
508 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
509 | ||
510 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
511 | ||
512 | $this->assertEquals($entry->getId(), $content['id']); | |
513 | $this->assertEquals($entry->getUrl(), $content['url']); | |
514 | $this->assertEquals('New awesome title', $content['title']); | |
515 | $this->assertGreaterThan($nbTags, count($content['tags'])); | |
516 | } | |
517 | ||
518 | public function testGetTagsEntry() | |
519 | { | |
520 | $entry = $this->client->getContainer() | |
521 | ->get('doctrine.orm.entity_manager') | |
522 | ->getRepository('WallabagCoreBundle:Entry') | |
523 | ->findOneWithTags($this->user->getId()); | |
524 | ||
525 | $entry = $entry[0]; | |
526 | ||
527 | if (!$entry) { | |
528 | $this->markTestSkipped('No content found in db.'); | |
529 | } | |
530 | ||
531 | $tags = []; | |
532 | foreach ($entry->getTags() as $tag) { | |
533 | $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()]; | |
534 | } | |
535 | ||
536 | $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags'); | |
537 | ||
538 | $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent()); | |
539 | } | |
540 | ||
541 | public function testPostTagsOnEntry() | |
542 | { | |
543 | $entry = $this->client->getContainer() | |
544 | ->get('doctrine.orm.entity_manager') | |
545 | ->getRepository('WallabagCoreBundle:Entry') | |
546 | ->findOneByUser(1); | |
547 | ||
548 | if (!$entry) { | |
549 | $this->markTestSkipped('No content found in db.'); | |
550 | } | |
551 | ||
552 | $nbTags = count($entry->getTags()); | |
553 | ||
554 | $newTags = 'tag1,tag2,tag3'; | |
555 | ||
556 | $this->client->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]); | |
557 | ||
558 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
559 | ||
560 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
561 | ||
562 | $this->assertArrayHasKey('tags', $content); | |
563 | $this->assertEquals($nbTags + 3, count($content['tags'])); | |
564 | ||
565 | $entryDB = $this->client->getContainer() | |
566 | ->get('doctrine.orm.entity_manager') | |
567 | ->getRepository('WallabagCoreBundle:Entry') | |
568 | ->find($entry->getId()); | |
569 | ||
570 | $tagsInDB = []; | |
571 | foreach ($entryDB->getTags()->toArray() as $tag) { | |
572 | $tagsInDB[$tag->getId()] = $tag->getLabel(); | |
573 | } | |
574 | ||
575 | foreach (explode(',', $newTags) as $tag) { | |
576 | $this->assertContains($tag, $tagsInDB); | |
577 | } | |
578 | } | |
579 | ||
580 | public function testDeleteOneTagEntry() | |
581 | { | |
582 | $entry = $this->client->getContainer() | |
583 | ->get('doctrine.orm.entity_manager') | |
584 | ->getRepository('WallabagCoreBundle:Entry') | |
585 | ->findOneWithTags($this->user->getId()); | |
586 | $entry = $entry[0]; | |
587 | ||
588 | if (!$entry) { | |
589 | $this->markTestSkipped('No content found in db.'); | |
590 | } | |
591 | ||
592 | // hydrate the tags relations | |
593 | $nbTags = count($entry->getTags()); | |
594 | $tag = $entry->getTags()[0]; | |
595 | ||
596 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json'); | |
597 | ||
598 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
599 | ||
600 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
601 | ||
602 | $this->assertArrayHasKey('tags', $content); | |
603 | $this->assertEquals($nbTags - 1, count($content['tags'])); | |
604 | } | |
605 | ||
606 | public function testSaveIsArchivedAfterPost() | |
607 | { | |
608 | $entry = $this->client->getContainer() | |
609 | ->get('doctrine.orm.entity_manager') | |
610 | ->getRepository('WallabagCoreBundle:Entry') | |
611 | ->findOneBy(['user' => 1, 'isArchived' => 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_archived']); | |
626 | } | |
627 | ||
628 | public function testSaveIsStarredAfterPost() | |
629 | { | |
630 | $entry = $this->client->getContainer() | |
631 | ->get('doctrine.orm.entity_manager') | |
632 | ->getRepository('WallabagCoreBundle:Entry') | |
633 | ->findOneBy(['user' => 1, 'isStarred' => true]); | |
634 | ||
635 | if (!$entry) { | |
636 | $this->markTestSkipped('No content found in db.'); | |
637 | } | |
638 | ||
639 | $this->client->request('POST', '/api/entries.json', [ | |
640 | 'url' => $entry->getUrl(), | |
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_starred']); | |
648 | } | |
649 | ||
650 | public function testSaveIsArchivedAfterPatch() | |
651 | { | |
652 | $entry = $this->client->getContainer() | |
653 | ->get('doctrine.orm.entity_manager') | |
654 | ->getRepository('WallabagCoreBundle:Entry') | |
655 | ->findOneBy(['user' => 1, 'isArchived' => true]); | |
656 | ||
657 | if (!$entry) { | |
658 | $this->markTestSkipped('No content found in db.'); | |
659 | } | |
660 | ||
661 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | |
662 | 'title' => $entry->getTitle().'++', | |
663 | ]); | |
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['is_archived']); | |
670 | } | |
671 | ||
672 | public function testSaveIsStarredAfterPatch() | |
673 | { | |
674 | $entry = $this->client->getContainer() | |
675 | ->get('doctrine.orm.entity_manager') | |
676 | ->getRepository('WallabagCoreBundle:Entry') | |
677 | ->findOneBy(['user' => 1, 'isStarred' => true]); | |
678 | ||
679 | if (!$entry) { | |
680 | $this->markTestSkipped('No content found in db.'); | |
681 | } | |
682 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | |
683 | 'title' => $entry->getTitle().'++', | |
684 | ]); | |
685 | ||
686 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
687 | ||
688 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
689 | ||
690 | $this->assertEquals(true, $content['is_starred']); | |
691 | } | |
692 | ||
693 | public function testGetEntriesExists() | |
694 | { | |
695 | $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2'); | |
696 | ||
697 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
698 | ||
699 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
700 | ||
ca9a83ee | 701 | $this->assertEquals(2, $content['exists']); |
900c8448 NL |
702 | } |
703 | ||
704 | public function testGetEntriesExistsWithManyUrls() | |
705 | { | |
706 | $url1 = 'http://0.0.0.0/entry2'; | |
707 | $url2 = 'http://0.0.0.0/entry10'; | |
708 | $this->client->request('GET', '/api/entries/exists?urls[]='.$url1.'&urls[]='.$url2); | |
709 | ||
710 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
711 | ||
712 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
713 | ||
714 | $this->assertArrayHasKey($url1, $content); | |
715 | $this->assertArrayHasKey($url2, $content); | |
ca9a83ee | 716 | $this->assertEquals(2, $content[$url1]); |
900c8448 NL |
717 | $this->assertEquals(false, $content[$url2]); |
718 | } | |
719 | ||
720 | public function testGetEntriesExistsWhichDoesNotExists() | |
721 | { | |
722 | $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2'); | |
723 | ||
724 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
725 | ||
726 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
727 | ||
728 | $this->assertEquals(false, $content['exists']); | |
729 | } | |
730 | ||
731 | public function testGetEntriesExistsWithNoUrl() | |
732 | { | |
733 | $this->client->request('GET', '/api/entries/exists?url='); | |
734 | ||
735 | $this->assertEquals(403, $this->client->getResponse()->getStatusCode()); | |
736 | } | |
0a6f4568 JB |
737 | |
738 | public function testReloadEntryErrorWhileFetching() | |
739 | { | |
70584b42 | 740 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') |
0a6f4568 | 741 | ->getRepository('WallabagCoreBundle:Entry') |
70584b42 | 742 | ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); |
0a6f4568 JB |
743 | |
744 | if (!$entry) { | |
745 | $this->markTestSkipped('No content found in db.'); | |
746 | } | |
747 | ||
748 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'/reload.json'); | |
5cd0857e | 749 | $this->assertEquals(304, $this->client->getResponse()->getStatusCode()); |
0a6f4568 JB |
750 | } |
751 | ||
752 | public function testReloadEntry() | |
753 | { | |
754 | $this->client->request('POST', '/api/entries.json', [ | |
755 | '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', | |
756 | 'archive' => '1', | |
757 | 'tags' => 'google, apple', | |
758 | ]); | |
759 | ||
760 | $json = json_decode($this->client->getResponse()->getContent(), true); | |
761 | ||
762 | $this->setUp(); | |
763 | ||
764 | $this->client->request('PATCH', '/api/entries/'.$json['id'].'/reload.json'); | |
765 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
766 | ||
767 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
768 | ||
769 | $this->assertNotEmpty($content['title']); | |
770 | ||
771 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
772 | } | |
d1fc5902 NL |
773 | |
774 | public function testPostEntriesTagsListAction() | |
775 | { | |
80299ed2 NL |
776 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') |
777 | ->getRepository('WallabagCoreBundle:Entry') | |
dcbebc17 | 778 | ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); |
80299ed2 NL |
779 | |
780 | $tags = $entry->getTags(); | |
781 | ||
dcbebc17 | 782 | $this->assertCount(2, $tags); |
80299ed2 | 783 | |
d1fc5902 NL |
784 | $list = [ |
785 | [ | |
dcbebc17 | 786 | 'url' => 'http://0.0.0.0/entry4', |
80299ed2 | 787 | 'tags' => 'new tag 1, new tag 2', |
d1fc5902 | 788 | ], |
80299ed2 NL |
789 | ]; |
790 | ||
791 | $this->client->request('POST', '/api/entries/tags/lists?list='.json_encode($list)); | |
792 | ||
793 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
794 | ||
795 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
796 | ||
797 | $this->assertInternalType('int', $content[0]['entry']); | |
dcbebc17 | 798 | $this->assertEquals('http://0.0.0.0/entry4', $content[0]['url']); |
80299ed2 NL |
799 | |
800 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') | |
801 | ->getRepository('WallabagCoreBundle:Entry') | |
dcbebc17 | 802 | ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); |
80299ed2 NL |
803 | |
804 | $tags = $entry->getTags(); | |
dcbebc17 | 805 | $this->assertCount(4, $tags); |
80299ed2 NL |
806 | } |
807 | ||
808 | public function testDeleteEntriesTagsListAction() | |
809 | { | |
810 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') | |
811 | ->getRepository('WallabagCoreBundle:Entry') | |
dcbebc17 | 812 | ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); |
80299ed2 NL |
813 | |
814 | $tags = $entry->getTags(); | |
815 | ||
dcbebc17 | 816 | $this->assertCount(4, $tags); |
80299ed2 NL |
817 | |
818 | $list = [ | |
d1fc5902 | 819 | [ |
dcbebc17 | 820 | 'url' => 'http://0.0.0.0/entry4', |
d1fc5902 | 821 | 'tags' => 'new tag 1, new tag 2', |
d1fc5902 NL |
822 | ], |
823 | ]; | |
824 | ||
7d2d1d68 | 825 | $this->client->request('DELETE', '/api/entries/tags/list?list='.json_encode($list)); |
1eca7831 NL |
826 | } |
827 | ||
a7abcc7b | 828 | public function testPostEntriesListAction() |
1eca7831 NL |
829 | { |
830 | $list = [ | |
a7abcc7b | 831 | 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', |
7fa844a3 | 832 | 'http://0.0.0.0/entry2', |
1eca7831 NL |
833 | ]; |
834 | ||
a7abcc7b | 835 | $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list)); |
d1fc5902 NL |
836 | |
837 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
838 | ||
839 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
840 | ||
80299ed2 | 841 | $this->assertInternalType('int', $content[0]['entry']); |
a7abcc7b | 842 | $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']); |
d1fc5902 | 843 | |
1eca7831 | 844 | $this->assertInternalType('int', $content[1]['entry']); |
7fa844a3 | 845 | $this->assertEquals('http://0.0.0.0/entry2', $content[1]['url']); |
a7abcc7b | 846 | } |
d1fc5902 | 847 | |
a7abcc7b NL |
848 | public function testDeleteEntriesListAction() |
849 | { | |
850 | $list = [ | |
719ba257 | 851 | 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', |
a7abcc7b NL |
852 | 'http://0.0.0.0/entry3', |
853 | ]; | |
854 | ||
855 | $this->client->request('DELETE', '/api/entries/list?urls='.json_encode($list)); | |
d1fc5902 | 856 | |
a7abcc7b NL |
857 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
858 | ||
859 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
860 | ||
861 | $this->assertTrue($content[0]['entry']); | |
719ba257 | 862 | $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']); |
1eca7831 | 863 | |
a7abcc7b NL |
864 | $this->assertFalse($content[1]['entry']); |
865 | $this->assertEquals('http://0.0.0.0/entry3', $content[1]['url']); | |
d1fc5902 | 866 | } |
efd351c9 | 867 | |
efd351c9 NL |
868 | public function testLimitBulkAction() |
869 | { | |
870 | $list = [ | |
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 | 'http://0.0.0.0/entry1', | |
877 | 'http://0.0.0.0/entry1', | |
878 | 'http://0.0.0.0/entry1', | |
879 | 'http://0.0.0.0/entry1', | |
880 | 'http://0.0.0.0/entry1', | |
881 | 'http://0.0.0.0/entry1', | |
882 | ]; | |
883 | ||
884 | $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list)); | |
72db15ca JB |
885 | |
886 | $this->assertEquals(400, $this->client->getResponse()->getStatusCode()); | |
887 | $this->assertContains('API limit reached', $this->client->getResponse()->getContent()); | |
d1fc5902 | 888 | } |
900c8448 | 889 | } |