]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Tests\Wallabag\ApiBundle\Controller; | |
4 | ||
5 | use Tests\Wallabag\ApiBundle\WallabagApiTestCase; | |
6 | use Wallabag\CoreBundle\Entity\Entry; | |
7 | use Wallabag\CoreBundle\Entity\Tag; | |
8 | use Wallabag\CoreBundle\Helper\ContentProxy; | |
9 | use Wallabag\UserBundle\Entity\User; | |
10 | ||
11 | class EntryRestControllerTest extends WallabagApiTestCase | |
12 | { | |
13 | public function testGetOneEntry() | |
14 | { | |
15 | $entry = $this->client->getContainer() | |
16 | ->get('doctrine.orm.entity_manager') | |
17 | ->getRepository('WallabagCoreBundle:Entry') | |
18 | ->findOneBy(['user' => $this->getUserId(), 'isArchived' => false]); | |
19 | ||
20 | if (!$entry) { | |
21 | $this->markTestSkipped('No content found in db.'); | |
22 | } | |
23 | ||
24 | $this->client->request('GET', '/api/entries/' . $entry->getId() . '.json'); | |
25 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
26 | ||
27 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
28 | ||
29 | $this->assertSame($entry->getTitle(), $content['title']); | |
30 | $this->assertSame($entry->getUrl(), $content['url']); | |
31 | $this->assertCount(\count($entry->getTags()), $content['tags']); | |
32 | $this->assertSame($entry->getUserName(), $content['user_name']); | |
33 | $this->assertSame($entry->getUserEmail(), $content['user_email']); | |
34 | $this->assertSame($entry->getUserId(), $content['user_id']); | |
35 | ||
36 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
37 | } | |
38 | ||
39 | public function testGetOneEntryWithOriginUrl() | |
40 | { | |
41 | $entry = $this->client->getContainer() | |
42 | ->get('doctrine.orm.entity_manager') | |
43 | ->getRepository('WallabagCoreBundle:Entry') | |
44 | ->findOneBy(['user' => $this->getUserId(), 'url' => 'http://0.0.0.0/entry2']); | |
45 | ||
46 | if (!$entry) { | |
47 | $this->markTestSkipped('No content found in db.'); | |
48 | } | |
49 | ||
50 | $this->client->request('GET', '/api/entries/' . $entry->getId() . '.json'); | |
51 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
52 | ||
53 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
54 | ||
55 | $this->assertSame($entry->getOriginUrl(), $content['origin_url']); | |
56 | } | |
57 | ||
58 | public function testExportEntry() | |
59 | { | |
60 | $entry = $this->client->getContainer() | |
61 | ->get('doctrine.orm.entity_manager') | |
62 | ->getRepository('WallabagCoreBundle:Entry') | |
63 | ->findOneBy(['user' => $this->getUserId(), 'isArchived' => false]); | |
64 | ||
65 | if (!$entry) { | |
66 | $this->markTestSkipped('No content found in db.'); | |
67 | } | |
68 | ||
69 | $this->client->request('GET', '/api/entries/' . $entry->getId() . '/export.epub'); | |
70 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
71 | ||
72 | // epub format got the content type in the content | |
73 | $this->assertContains('application/epub', $this->client->getResponse()->getContent()); | |
74 | $this->assertSame('application/epub+zip', $this->client->getResponse()->headers->get('Content-Type')); | |
75 | ||
76 | // re-auth client for mobi | |
77 | $client = $this->createAuthorizedClient(); | |
78 | $client->request('GET', '/api/entries/' . $entry->getId() . '/export.mobi'); | |
79 | $this->assertSame(200, $client->getResponse()->getStatusCode()); | |
80 | ||
81 | $this->assertSame('application/x-mobipocket-ebook', $client->getResponse()->headers->get('Content-Type')); | |
82 | ||
83 | // re-auth client for pdf | |
84 | $client = $this->createAuthorizedClient(); | |
85 | $client->request('GET', '/api/entries/' . $entry->getId() . '/export.pdf'); | |
86 | $this->assertSame(200, $client->getResponse()->getStatusCode()); | |
87 | ||
88 | $this->assertContains('PDF-', $client->getResponse()->getContent()); | |
89 | $this->assertSame('application/pdf', $client->getResponse()->headers->get('Content-Type')); | |
90 | ||
91 | // re-auth client for pdf | |
92 | $client = $this->createAuthorizedClient(); | |
93 | $client->request('GET', '/api/entries/' . $entry->getId() . '/export.txt'); | |
94 | $this->assertSame(200, $client->getResponse()->getStatusCode()); | |
95 | ||
96 | $this->assertContains('text/plain', $client->getResponse()->headers->get('Content-Type')); | |
97 | ||
98 | // re-auth client for pdf | |
99 | $client = $this->createAuthorizedClient(); | |
100 | $client->request('GET', '/api/entries/' . $entry->getId() . '/export.csv'); | |
101 | $this->assertSame(200, $client->getResponse()->getStatusCode()); | |
102 | ||
103 | $this->assertContains('application/csv', $client->getResponse()->headers->get('Content-Type')); | |
104 | } | |
105 | ||
106 | public function testGetOneEntryWrongUser() | |
107 | { | |
108 | $entry = $this->client->getContainer() | |
109 | ->get('doctrine.orm.entity_manager') | |
110 | ->getRepository('WallabagCoreBundle:Entry') | |
111 | ->findOneBy(['user' => $this->getUserId('bob'), 'isArchived' => false]); | |
112 | ||
113 | if (!$entry) { | |
114 | $this->markTestSkipped('No content found in db.'); | |
115 | } | |
116 | ||
117 | $this->client->request('GET', '/api/entries/' . $entry->getId() . '.json'); | |
118 | ||
119 | $this->assertSame(403, $this->client->getResponse()->getStatusCode()); | |
120 | } | |
121 | ||
122 | public function testGetEntries() | |
123 | { | |
124 | $this->client->request('GET', '/api/entries'); | |
125 | ||
126 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
127 | ||
128 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
129 | ||
130 | $this->assertGreaterThanOrEqual(1, \count($content)); | |
131 | $this->assertNotEmpty($content['_embedded']['items']); | |
132 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
133 | $this->assertSame(1, $content['page']); | |
134 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
135 | ||
136 | $this->assertNotNull($content['_embedded']['items'][0]['content']); | |
137 | ||
138 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
139 | } | |
140 | ||
141 | public function testGetEntriesDetailMetadata() | |
142 | { | |
143 | $this->client->request('GET', '/api/entries?detail=metadata'); | |
144 | ||
145 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
146 | ||
147 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
148 | ||
149 | $this->assertGreaterThanOrEqual(1, \count($content)); | |
150 | $this->assertNotEmpty($content['_embedded']['items']); | |
151 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
152 | $this->assertSame(1, $content['page']); | |
153 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
154 | ||
155 | $this->assertNull($content['_embedded']['items'][0]['content']); | |
156 | ||
157 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
158 | } | |
159 | ||
160 | public function testGetEntriesWithFullOptions() | |
161 | { | |
162 | $this->client->request('GET', '/api/entries', [ | |
163 | 'archive' => 1, | |
164 | 'starred' => 1, | |
165 | 'sort' => 'updated', | |
166 | 'order' => 'asc', | |
167 | 'page' => 1, | |
168 | 'perPage' => 2, | |
169 | 'tags' => 'foo', | |
170 | 'since' => 1443274283, | |
171 | 'public' => 0, | |
172 | ]); | |
173 | ||
174 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
175 | ||
176 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
177 | ||
178 | $this->assertGreaterThanOrEqual(1, \count($content)); | |
179 | $this->assertArrayHasKey('items', $content['_embedded']); | |
180 | $this->assertGreaterThanOrEqual(0, $content['total']); | |
181 | $this->assertSame(1, $content['page']); | |
182 | $this->assertSame(2, $content['limit']); | |
183 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
184 | ||
185 | $this->assertArrayHasKey('_links', $content); | |
186 | $this->assertArrayHasKey('self', $content['_links']); | |
187 | $this->assertArrayHasKey('first', $content['_links']); | |
188 | $this->assertArrayHasKey('last', $content['_links']); | |
189 | ||
190 | foreach (['self', 'first', 'last'] as $link) { | |
191 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
192 | $this->assertContains('archive=1', $content['_links'][$link]['href']); | |
193 | $this->assertContains('starred=1', $content['_links'][$link]['href']); | |
194 | $this->assertContains('sort=updated', $content['_links'][$link]['href']); | |
195 | $this->assertContains('order=asc', $content['_links'][$link]['href']); | |
196 | $this->assertContains('tags=foo', $content['_links'][$link]['href']); | |
197 | $this->assertContains('since=1443274283', $content['_links'][$link]['href']); | |
198 | $this->assertContains('public=0', $content['_links'][$link]['href']); | |
199 | } | |
200 | ||
201 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
202 | } | |
203 | ||
204 | public function testGetEntriesPublicOnly() | |
205 | { | |
206 | $entry = $this->client->getContainer() | |
207 | ->get('doctrine.orm.entity_manager') | |
208 | ->getRepository('WallabagCoreBundle:Entry') | |
209 | ->findOneByUser($this->getUserId()); | |
210 | ||
211 | if (!$entry) { | |
212 | $this->markTestSkipped('No content found in db.'); | |
213 | } | |
214 | ||
215 | // generate at least one public entry | |
216 | $entry->generateUid(); | |
217 | ||
218 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
219 | $em->persist($entry); | |
220 | $em->flush(); | |
221 | ||
222 | $this->client->request('GET', '/api/entries', [ | |
223 | 'public' => 1, | |
224 | ]); | |
225 | ||
226 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
227 | ||
228 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
229 | ||
230 | $this->assertGreaterThanOrEqual(1, \count($content)); | |
231 | $this->assertArrayHasKey('items', $content['_embedded']); | |
232 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
233 | $this->assertSame(1, $content['page']); | |
234 | $this->assertSame(30, $content['limit']); | |
235 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
236 | ||
237 | $this->assertArrayHasKey('_links', $content); | |
238 | $this->assertArrayHasKey('self', $content['_links']); | |
239 | $this->assertArrayHasKey('first', $content['_links']); | |
240 | $this->assertArrayHasKey('last', $content['_links']); | |
241 | ||
242 | foreach (['self', 'first', 'last'] as $link) { | |
243 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
244 | $this->assertContains('public=1', $content['_links'][$link]['href']); | |
245 | } | |
246 | ||
247 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
248 | } | |
249 | ||
250 | public function testGetEntriesOnPageTwo() | |
251 | { | |
252 | $this->client->request('GET', '/api/entries', [ | |
253 | 'page' => 2, | |
254 | 'perPage' => 2, | |
255 | ]); | |
256 | ||
257 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
258 | ||
259 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
260 | ||
261 | $this->assertGreaterThanOrEqual(0, $content['total']); | |
262 | $this->assertSame(2, $content['page']); | |
263 | $this->assertSame(2, $content['limit']); | |
264 | } | |
265 | ||
266 | public function testGetStarredEntriesWithBadSort() | |
267 | { | |
268 | $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated', 'order' => 'unknown']); | |
269 | ||
270 | $this->assertSame(400, $this->client->getResponse()->getStatusCode()); | |
271 | ||
272 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
273 | } | |
274 | ||
275 | public function testGetStarredEntries() | |
276 | { | |
277 | $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']); | |
278 | ||
279 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
280 | ||
281 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
282 | ||
283 | $this->assertGreaterThanOrEqual(1, \count($content)); | |
284 | $this->assertNotEmpty($content['_embedded']['items']); | |
285 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
286 | $this->assertSame(1, $content['page']); | |
287 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
288 | ||
289 | $this->assertArrayHasKey('_links', $content); | |
290 | $this->assertArrayHasKey('self', $content['_links']); | |
291 | $this->assertArrayHasKey('first', $content['_links']); | |
292 | $this->assertArrayHasKey('last', $content['_links']); | |
293 | ||
294 | foreach (['self', 'first', 'last'] as $link) { | |
295 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
296 | $this->assertContains('starred=1', $content['_links'][$link]['href']); | |
297 | $this->assertContains('sort=updated', $content['_links'][$link]['href']); | |
298 | } | |
299 | ||
300 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
301 | } | |
302 | ||
303 | public function testGetArchiveEntries() | |
304 | { | |
305 | $this->client->request('GET', '/api/entries', ['archive' => 1]); | |
306 | ||
307 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
308 | ||
309 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
310 | ||
311 | $this->assertGreaterThanOrEqual(1, \count($content)); | |
312 | $this->assertNotEmpty($content['_embedded']['items']); | |
313 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
314 | $this->assertSame(1, $content['page']); | |
315 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
316 | ||
317 | $this->assertArrayHasKey('_links', $content); | |
318 | $this->assertArrayHasKey('self', $content['_links']); | |
319 | $this->assertArrayHasKey('first', $content['_links']); | |
320 | $this->assertArrayHasKey('last', $content['_links']); | |
321 | ||
322 | foreach (['self', 'first', 'last'] as $link) { | |
323 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
324 | $this->assertContains('archive=1', $content['_links'][$link]['href']); | |
325 | } | |
326 | ||
327 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
328 | } | |
329 | ||
330 | public function testGetTaggedEntries() | |
331 | { | |
332 | $this->client->request('GET', '/api/entries', ['tags' => 'foo,bar']); | |
333 | ||
334 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
335 | ||
336 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
337 | ||
338 | $this->assertGreaterThanOrEqual(1, \count($content)); | |
339 | $this->assertNotEmpty($content['_embedded']['items']); | |
340 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
341 | $this->assertSame(1, $content['page']); | |
342 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
343 | ||
344 | $this->assertContains('foo', array_column($content['_embedded']['items'][0]['tags'], 'label'), 'Entries tags should have "foo" tag'); | |
345 | $this->assertContains('bar', array_column($content['_embedded']['items'][0]['tags'], 'label'), 'Entries tags should have "bar" tag'); | |
346 | ||
347 | $this->assertArrayHasKey('_links', $content); | |
348 | $this->assertArrayHasKey('self', $content['_links']); | |
349 | $this->assertArrayHasKey('first', $content['_links']); | |
350 | $this->assertArrayHasKey('last', $content['_links']); | |
351 | ||
352 | foreach (['self', 'first', 'last'] as $link) { | |
353 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
354 | $this->assertContains('tags=' . urlencode('foo,bar'), $content['_links'][$link]['href']); | |
355 | } | |
356 | ||
357 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
358 | } | |
359 | ||
360 | public function testGetTaggedEntriesWithBadParams() | |
361 | { | |
362 | $this->client->request('GET', '/api/entries', ['tags' => ['foo', 'bar']]); | |
363 | ||
364 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
365 | } | |
366 | ||
367 | public function testGetDatedEntries() | |
368 | { | |
369 | $this->client->request('GET', '/api/entries', ['since' => 1443274283]); | |
370 | ||
371 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
372 | ||
373 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
374 | ||
375 | $this->assertGreaterThanOrEqual(1, \count($content)); | |
376 | $this->assertNotEmpty($content['_embedded']['items']); | |
377 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
378 | $this->assertSame(1, $content['page']); | |
379 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
380 | ||
381 | $this->assertArrayHasKey('_links', $content); | |
382 | $this->assertArrayHasKey('self', $content['_links']); | |
383 | $this->assertArrayHasKey('first', $content['_links']); | |
384 | $this->assertArrayHasKey('last', $content['_links']); | |
385 | ||
386 | foreach (['self', 'first', 'last'] as $link) { | |
387 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
388 | $this->assertContains('since=1443274283', $content['_links'][$link]['href']); | |
389 | } | |
390 | ||
391 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
392 | } | |
393 | ||
394 | public function testGetDatedSupEntries() | |
395 | { | |
396 | $future = new \DateTime(date('Y-m-d H:i:s')); | |
397 | $this->client->request('GET', '/api/entries', ['since' => $future->getTimestamp() + 1000]); | |
398 | ||
399 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
400 | ||
401 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
402 | ||
403 | $this->assertGreaterThanOrEqual(1, \count($content)); | |
404 | $this->assertEmpty($content['_embedded']['items']); | |
405 | $this->assertSame(0, $content['total']); | |
406 | $this->assertSame(1, $content['page']); | |
407 | $this->assertSame(1, $content['pages']); | |
408 | ||
409 | $this->assertArrayHasKey('_links', $content); | |
410 | $this->assertArrayHasKey('self', $content['_links']); | |
411 | $this->assertArrayHasKey('first', $content['_links']); | |
412 | $this->assertArrayHasKey('last', $content['_links']); | |
413 | ||
414 | foreach (['self', 'first', 'last'] as $link) { | |
415 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
416 | $this->assertContains('since=' . ($future->getTimestamp() + 1000), $content['_links'][$link]['href']); | |
417 | } | |
418 | ||
419 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
420 | } | |
421 | ||
422 | public function testDeleteEntry() | |
423 | { | |
424 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
425 | $entry = new Entry($em->getReference(User::class, 1)); | |
426 | $entry->setUrl('http://0.0.0.0/test-delete-entry'); | |
427 | $entry->setTitle('Test delete entry'); | |
428 | $em->persist($entry); | |
429 | $em->flush(); | |
430 | ||
431 | $em->clear(); | |
432 | ||
433 | $e = [ | |
434 | 'title' => $entry->getTitle(), | |
435 | 'url' => $entry->getUrl(), | |
436 | 'id' => $entry->getId(), | |
437 | ]; | |
438 | ||
439 | $this->client->request('DELETE', '/api/entries/' . $e['id'] . '.json'); | |
440 | ||
441 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
442 | ||
443 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
444 | ||
445 | $this->assertSame($e['title'], $content['title']); | |
446 | $this->assertSame($e['url'], $content['url']); | |
447 | $this->assertSame($e['id'], $content['id']); | |
448 | ||
449 | // We'll try to delete this entry again | |
450 | $client = $this->createAuthorizedClient(); | |
451 | $client->request('DELETE', '/api/entries/' . $e['id'] . '.json'); | |
452 | ||
453 | $this->assertSame(404, $client->getResponse()->getStatusCode()); | |
454 | } | |
455 | ||
456 | public function testDeleteEntryExpectId() | |
457 | { | |
458 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
459 | $entry = new Entry($em->getReference(User::class, 1)); | |
460 | $entry->setUrl('http://0.0.0.0/test-delete-entry-id'); | |
461 | $em->persist($entry); | |
462 | $em->flush(); | |
463 | ||
464 | $em->clear(); | |
465 | ||
466 | $id = $entry->getId(); | |
467 | ||
468 | $this->client->request('DELETE', '/api/entries/' . $id . '.json?expect=id'); | |
469 | ||
470 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
471 | ||
472 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
473 | ||
474 | $this->assertSame($id, $content['id']); | |
475 | $this->assertArrayNotHasKey('url', $content); | |
476 | ||
477 | // We'll try to delete this entry again | |
478 | $client = $this->createAuthorizedClient(); | |
479 | $client->request('DELETE', '/api/entries/' . $id . '.json'); | |
480 | ||
481 | $this->assertSame(404, $client->getResponse()->getStatusCode()); | |
482 | } | |
483 | ||
484 | public function testDeleteEntryExpectBadRequest() | |
485 | { | |
486 | $this->client->request('DELETE', '/api/entries/1.json?expect=badrequest'); | |
487 | ||
488 | $this->assertSame(400, $this->client->getResponse()->getStatusCode()); | |
489 | } | |
490 | ||
491 | public function testPostEntry() | |
492 | { | |
493 | $this->client->request('POST', '/api/entries.json', [ | |
494 | 'url' => 'https://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', | |
495 | 'tags' => 'google', | |
496 | 'title' => 'New title for my article', | |
497 | 'content' => 'my content', | |
498 | 'language' => 'de', | |
499 | 'published_at' => '2016-09-08T11:55:58+0200', | |
500 | 'authors' => 'bob,helen', | |
501 | 'public' => 1, | |
502 | ]); | |
503 | ||
504 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
505 | ||
506 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
507 | ||
508 | $this->assertGreaterThan(0, $content['id']); | |
509 | $this->assertSame('https://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']); | |
510 | $this->assertSame(0, $content['is_archived']); | |
511 | $this->assertSame(0, $content['is_starred']); | |
512 | $this->assertNull($content['starred_at']); | |
513 | $this->assertNull($content['archived_at']); | |
514 | $this->assertSame('New title for my article', $content['title']); | |
515 | $this->assertSame($this->getUserId(), $content['user_id']); | |
516 | $this->assertCount(2, $content['tags']); | |
517 | $this->assertNull($content['origin_url']); | |
518 | $this->assertSame('my content', $content['content']); | |
519 | $this->assertSame('de', $content['language']); | |
520 | $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']); | |
521 | $this->assertCount(2, $content['published_by']); | |
522 | $this->assertContains('bob', $content['published_by']); | |
523 | $this->assertContains('helen', $content['published_by']); | |
524 | $this->assertTrue($content['is_public'], 'A public link has been generated for that entry'); | |
525 | } | |
526 | ||
527 | public function testPostSameEntry() | |
528 | { | |
529 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
530 | $entry = new Entry($em->getReference(User::class, $this->getUserId())); | |
531 | $entry->setUrl('https://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'); | |
532 | $entry->setArchived(true); | |
533 | $entry->addTag((new Tag())->setLabel('google')); | |
534 | $entry->addTag((new Tag())->setLabel('apple')); | |
535 | $em->persist($entry); | |
536 | $em->flush(); | |
537 | $em->clear(); | |
538 | ||
539 | $this->client->request('POST', '/api/entries.json', [ | |
540 | 'url' => 'https://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', | |
541 | 'archive' => '1', | |
542 | 'tags' => 'google, apple', | |
543 | ]); | |
544 | ||
545 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
546 | ||
547 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
548 | ||
549 | $this->assertGreaterThan(0, $content['id']); | |
550 | $this->assertSame('https://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']); | |
551 | $this->assertSame(1, $content['is_archived']); | |
552 | $this->assertSame(0, $content['is_starred']); | |
553 | $this->assertCount(3, $content['tags']); | |
554 | } | |
555 | ||
556 | public function testPostEntryWhenFetchContentFails() | |
557 | { | |
558 | /** @var \Symfony\Component\DependencyInjection\Container $container */ | |
559 | $container = $this->client->getContainer(); | |
560 | $contentProxy = $this->getMockBuilder(ContentProxy::class) | |
561 | ->disableOriginalConstructor() | |
562 | ->setMethods(['updateEntry']) | |
563 | ->getMock(); | |
564 | $contentProxy->expects($this->any()) | |
565 | ->method('updateEntry') | |
566 | ->willThrowException(new \Exception('Test Fetch content fails')); | |
567 | $container->set('wallabag_core.content_proxy', $contentProxy); | |
568 | ||
569 | try { | |
570 | $this->client->request('POST', '/api/entries.json', [ | |
571 | 'url' => 'http://www.example.com/', | |
572 | ]); | |
573 | ||
574 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
575 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
576 | $this->assertGreaterThan(0, $content['id']); | |
577 | $this->assertSame('http://www.example.com/', $content['url']); | |
578 | $this->assertSame('www.example.com', $content['domain_name']); | |
579 | $this->assertSame('www.example.com', $content['title']); | |
580 | } finally { | |
581 | // Remove the created entry to avoid side effects on other tests | |
582 | if (isset($content['id'])) { | |
583 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
584 | $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']); | |
585 | $em->remove($entry); | |
586 | $em->flush(); | |
587 | } | |
588 | } | |
589 | } | |
590 | ||
591 | public function testPostArchivedAndStarredEntry() | |
592 | { | |
593 | $now = new \DateTime(); | |
594 | $this->client->request('POST', '/api/entries.json', [ | |
595 | 'url' => 'https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', | |
596 | 'archive' => '1', | |
597 | 'starred' => '1', | |
598 | ]); | |
599 | ||
600 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
601 | ||
602 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
603 | ||
604 | $this->assertGreaterThan(0, $content['id']); | |
605 | $this->assertSame('https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']); | |
606 | $this->assertSame(1, $content['is_archived']); | |
607 | $this->assertSame(1, $content['is_starred']); | |
608 | $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['starred_at']))->getTimestamp()); | |
609 | $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['archived_at']))->getTimestamp()); | |
610 | $this->assertSame($this->getUserId(), $content['user_id']); | |
611 | } | |
612 | ||
613 | public function testPostArchivedAndStarredEntryWithoutQuotes() | |
614 | { | |
615 | $this->client->request('POST', '/api/entries.json', [ | |
616 | 'url' => 'https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', | |
617 | 'archive' => 0, | |
618 | 'starred' => 1, | |
619 | ]); | |
620 | ||
621 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
622 | ||
623 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
624 | ||
625 | $this->assertGreaterThan(0, $content['id']); | |
626 | $this->assertSame('https://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']); | |
627 | $this->assertSame(0, $content['is_archived']); | |
628 | $this->assertSame(1, $content['is_starred']); | |
629 | } | |
630 | ||
631 | public function testPostEntryWithOriginUrl() | |
632 | { | |
633 | $this->client->request('POST', '/api/entries.json', [ | |
634 | 'url' => 'https://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', | |
635 | 'tags' => 'google', | |
636 | 'title' => 'New title for my article', | |
637 | 'content' => 'my content', | |
638 | 'language' => 'de', | |
639 | 'published_at' => '2016-09-08T11:55:58+0200', | |
640 | 'authors' => 'bob,helen', | |
641 | 'public' => 1, | |
642 | 'origin_url' => 'http://mysource.tld', | |
643 | ]); | |
644 | ||
645 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
646 | ||
647 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
648 | ||
649 | $this->assertGreaterThan(0, $content['id']); | |
650 | $this->assertSame('https://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']); | |
651 | $this->assertSame('http://mysource.tld', $content['origin_url']); | |
652 | } | |
653 | ||
654 | public function testPatchEntry() | |
655 | { | |
656 | $entry = $this->client->getContainer() | |
657 | ->get('doctrine.orm.entity_manager') | |
658 | ->getRepository('WallabagCoreBundle:Entry') | |
659 | ->findOneByUser($this->getUserId()); | |
660 | ||
661 | if (!$entry) { | |
662 | $this->markTestSkipped('No content found in db.'); | |
663 | } | |
664 | ||
665 | $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [ | |
666 | 'title' => 'New awesome title', | |
667 | 'tags' => 'new tag ' . uniqid(), | |
668 | 'starred' => '1', | |
669 | 'archive' => '0', | |
670 | 'language' => 'de_AT', | |
671 | 'preview_picture' => 'http://preview.io/picture.jpg', | |
672 | 'authors' => 'bob,sponge', | |
673 | 'content' => 'awesome', | |
674 | 'public' => 0, | |
675 | 'published_at' => 1488833381, | |
676 | ]); | |
677 | ||
678 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
679 | ||
680 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
681 | ||
682 | $this->assertSame($entry->getId(), $content['id']); | |
683 | $this->assertSame($entry->getUrl(), $content['url']); | |
684 | $this->assertSame('New awesome title', $content['title']); | |
685 | $this->assertGreaterThanOrEqual(1, \count($content['tags']), 'We force only one tag'); | |
686 | $this->assertSame($this->getUserId(), $content['user_id']); | |
687 | $this->assertSame('de_AT', $content['language']); | |
688 | $this->assertSame('http://preview.io/picture.jpg', $content['preview_picture']); | |
689 | $this->assertContains('sponge', $content['published_by']); | |
690 | $this->assertContains('bob', $content['published_by']); | |
691 | $this->assertSame('awesome', $content['content']); | |
692 | $this->assertFalse($content['is_public'], 'Entry is no more shared'); | |
693 | $this->assertContains('2017-03-06', $content['published_at']); | |
694 | } | |
695 | ||
696 | public function testPatchEntryWithoutQuotes() | |
697 | { | |
698 | $entry = $this->client->getContainer() | |
699 | ->get('doctrine.orm.entity_manager') | |
700 | ->getRepository('WallabagCoreBundle:Entry') | |
701 | ->findOneByUser($this->getUserId()); | |
702 | ||
703 | if (!$entry) { | |
704 | $this->markTestSkipped('No content found in db.'); | |
705 | } | |
706 | ||
707 | $previousContent = $entry->getContent(); | |
708 | $previousLanguage = $entry->getLanguage(); | |
709 | ||
710 | $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [ | |
711 | 'title' => 'New awesome title', | |
712 | 'tags' => 'new tag ' . uniqid(), | |
713 | 'starred' => 1, | |
714 | 'archive' => 0, | |
715 | 'authors' => ['bob', 'sponge'], | |
716 | ]); | |
717 | ||
718 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
719 | ||
720 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
721 | ||
722 | $this->assertSame($entry->getId(), $content['id']); | |
723 | $this->assertSame($entry->getUrl(), $content['url']); | |
724 | $this->assertGreaterThanOrEqual(1, \count($content['tags']), 'We force only one tag'); | |
725 | $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string'); | |
726 | $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved'); | |
727 | $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved'); | |
728 | } | |
729 | ||
730 | public function testPatchEntryWithOriginUrl() | |
731 | { | |
732 | $entry = $this->client->getContainer() | |
733 | ->get('doctrine.orm.entity_manager') | |
734 | ->getRepository('WallabagCoreBundle:Entry') | |
735 | ->findOneByUser($this->getUserId()); | |
736 | ||
737 | if (!$entry) { | |
738 | $this->markTestSkipped('No content found in db.'); | |
739 | } | |
740 | ||
741 | $previousContent = $entry->getContent(); | |
742 | $previousLanguage = $entry->getLanguage(); | |
743 | ||
744 | $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [ | |
745 | 'title' => 'Another awesome title just for profit', | |
746 | 'origin_url' => 'https://myawesomesource.example.com', | |
747 | ]); | |
748 | ||
749 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
750 | ||
751 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
752 | ||
753 | $this->assertSame($entry->getId(), $content['id']); | |
754 | $this->assertSame($entry->getUrl(), $content['url']); | |
755 | $this->assertSame('https://myawesomesource.example.com', $content['origin_url']); | |
756 | $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string'); | |
757 | $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved'); | |
758 | $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved'); | |
759 | } | |
760 | ||
761 | public function testPatchEntryRemoveOriginUrl() | |
762 | { | |
763 | $entry = $this->client->getContainer() | |
764 | ->get('doctrine.orm.entity_manager') | |
765 | ->getRepository('WallabagCoreBundle:Entry') | |
766 | ->findOneByUser($this->getUserId()); | |
767 | ||
768 | if (!$entry) { | |
769 | $this->markTestSkipped('No content found in db.'); | |
770 | } | |
771 | ||
772 | $previousContent = $entry->getContent(); | |
773 | $previousLanguage = $entry->getLanguage(); | |
774 | $previousTitle = $entry->getTitle(); | |
775 | ||
776 | $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [ | |
777 | 'origin_url' => '', | |
778 | ]); | |
779 | ||
780 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
781 | ||
782 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
783 | ||
784 | $this->assertSame($entry->getId(), $content['id']); | |
785 | $this->assertSame($entry->getUrl(), $content['url']); | |
786 | $this->assertEmpty($content['origin_url']); | |
787 | $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string'); | |
788 | $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved'); | |
789 | $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved'); | |
790 | $this->assertSame($previousTitle, $content['title'], 'Ensure title has not moved'); | |
791 | } | |
792 | ||
793 | public function testPatchEntryNullOriginUrl() | |
794 | { | |
795 | $entry = $this->client->getContainer() | |
796 | ->get('doctrine.orm.entity_manager') | |
797 | ->getRepository('WallabagCoreBundle:Entry') | |
798 | ->findOneByUser($this->getUserId()); | |
799 | ||
800 | if (!$entry) { | |
801 | $this->markTestSkipped('No content found in db.'); | |
802 | } | |
803 | ||
804 | $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [ | |
805 | 'origin_url' => null, | |
806 | ]); | |
807 | ||
808 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
809 | ||
810 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
811 | ||
812 | $this->assertNull($content['origin_url']); | |
813 | } | |
814 | ||
815 | public function testGetTagsEntry() | |
816 | { | |
817 | $entry = $this->client->getContainer() | |
818 | ->get('doctrine.orm.entity_manager') | |
819 | ->getRepository('WallabagCoreBundle:Entry') | |
820 | ->findOneWithTags($this->user->getId()); | |
821 | ||
822 | $entry = $entry[0]; | |
823 | ||
824 | if (!$entry) { | |
825 | $this->markTestSkipped('No content found in db.'); | |
826 | } | |
827 | ||
828 | $tags = []; | |
829 | foreach ($entry->getTags() as $tag) { | |
830 | $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()]; | |
831 | } | |
832 | ||
833 | $this->client->request('GET', '/api/entries/' . $entry->getId() . '/tags'); | |
834 | ||
835 | $this->assertSame(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent()); | |
836 | } | |
837 | ||
838 | public function testPostTagsOnEntry() | |
839 | { | |
840 | $entry = $this->client->getContainer() | |
841 | ->get('doctrine.orm.entity_manager') | |
842 | ->getRepository('WallabagCoreBundle:Entry') | |
843 | ->findOneByUser($this->getUserId()); | |
844 | ||
845 | if (!$entry) { | |
846 | $this->markTestSkipped('No content found in db.'); | |
847 | } | |
848 | ||
849 | $nbTags = \count($entry->getTags()); | |
850 | ||
851 | $newTags = 'tag1,tag2,tag3'; | |
852 | ||
853 | $this->client->request('POST', '/api/entries/' . $entry->getId() . '/tags', ['tags' => $newTags]); | |
854 | ||
855 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
856 | ||
857 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
858 | ||
859 | $this->assertArrayHasKey('tags', $content); | |
860 | $this->assertCount($nbTags + 3, $content['tags']); | |
861 | ||
862 | $entryDB = $this->client->getContainer() | |
863 | ->get('doctrine.orm.entity_manager') | |
864 | ->getRepository('WallabagCoreBundle:Entry') | |
865 | ->find($entry->getId()); | |
866 | ||
867 | $tagsInDB = []; | |
868 | foreach ($entryDB->getTags()->toArray() as $tag) { | |
869 | $tagsInDB[$tag->getId()] = $tag->getLabel(); | |
870 | } | |
871 | ||
872 | foreach (explode(',', $newTags) as $tag) { | |
873 | $this->assertContains($tag, $tagsInDB); | |
874 | } | |
875 | } | |
876 | ||
877 | public function testDeleteOneTagEntry() | |
878 | { | |
879 | $entry = $this->client->getContainer() | |
880 | ->get('doctrine.orm.entity_manager') | |
881 | ->getRepository('WallabagCoreBundle:Entry') | |
882 | ->findOneWithTags($this->user->getId()); | |
883 | $entry = $entry[0]; | |
884 | ||
885 | if (!$entry) { | |
886 | $this->markTestSkipped('No content found in db.'); | |
887 | } | |
888 | ||
889 | // hydrate the tags relations | |
890 | $nbTags = \count($entry->getTags()); | |
891 | $tag = $entry->getTags()[0]; | |
892 | ||
893 | $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '/tags/' . $tag->getId() . '.json'); | |
894 | ||
895 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
896 | ||
897 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
898 | ||
899 | $this->assertArrayHasKey('tags', $content); | |
900 | $this->assertCount($nbTags - 1, $content['tags']); | |
901 | } | |
902 | ||
903 | public function testSaveIsArchivedAfterPost() | |
904 | { | |
905 | $entry = $this->client->getContainer() | |
906 | ->get('doctrine.orm.entity_manager') | |
907 | ->getRepository('WallabagCoreBundle:Entry') | |
908 | ->findOneBy(['user' => $this->getUserId(), 'isArchived' => true]); | |
909 | ||
910 | if (!$entry) { | |
911 | $this->markTestSkipped('No content found in db.'); | |
912 | } | |
913 | ||
914 | $this->client->request('POST', '/api/entries.json', [ | |
915 | 'url' => $entry->getUrl(), | |
916 | ]); | |
917 | ||
918 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
919 | ||
920 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
921 | ||
922 | $this->assertSame(1, $content['is_archived']); | |
923 | } | |
924 | ||
925 | public function testSaveIsStarredAfterPost() | |
926 | { | |
927 | $entry = $this->client->getContainer() | |
928 | ->get('doctrine.orm.entity_manager') | |
929 | ->getRepository('WallabagCoreBundle:Entry') | |
930 | ->findOneBy(['user' => $this->getUserId(), 'isStarred' => true]); | |
931 | ||
932 | if (!$entry) { | |
933 | $this->markTestSkipped('No content found in db.'); | |
934 | } | |
935 | ||
936 | $this->client->request('POST', '/api/entries.json', [ | |
937 | 'url' => $entry->getUrl(), | |
938 | ]); | |
939 | ||
940 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
941 | ||
942 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
943 | ||
944 | $this->assertSame(1, $content['is_starred']); | |
945 | } | |
946 | ||
947 | public function testSaveIsArchivedAfterPatch() | |
948 | { | |
949 | $entry = $this->client->getContainer() | |
950 | ->get('doctrine.orm.entity_manager') | |
951 | ->getRepository('WallabagCoreBundle:Entry') | |
952 | ->findOneBy(['user' => $this->getUserId(), 'isArchived' => true]); | |
953 | ||
954 | if (!$entry) { | |
955 | $this->markTestSkipped('No content found in db.'); | |
956 | } | |
957 | ||
958 | $previousTitle = $entry->getTitle(); | |
959 | ||
960 | $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [ | |
961 | 'title' => $entry->getTitle() . '++', | |
962 | ]); | |
963 | ||
964 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
965 | ||
966 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
967 | ||
968 | $this->assertSame(1, $content['is_archived']); | |
969 | $this->assertSame($previousTitle . '++', $content['title']); | |
970 | } | |
971 | ||
972 | public function testSaveIsStarredAfterPatch() | |
973 | { | |
974 | $now = new \DateTime(); | |
975 | $entry = $this->client->getContainer() | |
976 | ->get('doctrine.orm.entity_manager') | |
977 | ->getRepository('WallabagCoreBundle:Entry') | |
978 | ->findOneBy(['user' => $this->getUserId(), 'isStarred' => true]); | |
979 | ||
980 | if (!$entry) { | |
981 | $this->markTestSkipped('No content found in db.'); | |
982 | } | |
983 | $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [ | |
984 | 'title' => $entry->getTitle() . '++', | |
985 | ]); | |
986 | ||
987 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
988 | ||
989 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
990 | ||
991 | $this->assertSame(1, $content['is_starred']); | |
992 | $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['starred_at']))->getTimestamp()); | |
993 | } | |
994 | ||
995 | public function dataForEntriesExistWithUrl() | |
996 | { | |
997 | $url = hash('sha1', 'http://0.0.0.0/entry2'); | |
998 | ||
999 | return [ | |
1000 | 'with_id' => [ | |
1001 | 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1', | |
1002 | 'expectedValue' => 2, | |
1003 | ], | |
1004 | 'without_id' => [ | |
1005 | 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2', | |
1006 | 'expectedValue' => true, | |
1007 | ], | |
1008 | 'hashed_url_with_id' => [ | |
1009 | 'url' => '/api/entries/exists?hashed_url=' . $url . '&return_id=1', | |
1010 | 'expectedValue' => 2, | |
1011 | ], | |
1012 | 'hashed_url_without_id' => [ | |
1013 | 'url' => '/api/entries/exists?hashed_url=' . $url . '', | |
1014 | 'expectedValue' => true, | |
1015 | ], | |
1016 | ]; | |
1017 | } | |
1018 | ||
1019 | /** | |
1020 | * @dataProvider dataForEntriesExistWithUrl | |
1021 | */ | |
1022 | public function testGetEntriesExists($url, $expectedValue) | |
1023 | { | |
1024 | $this->client->request('GET', $url); | |
1025 | ||
1026 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
1027 | ||
1028 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1029 | ||
1030 | $this->assertSame($expectedValue, $content['exists']); | |
1031 | } | |
1032 | ||
1033 | public function testGetEntriesExistsWithManyUrls() | |
1034 | { | |
1035 | $url1 = 'http://0.0.0.0/entry2'; | |
1036 | $url2 = 'http://0.0.0.0/entry10'; | |
1037 | ||
1038 | $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1'); | |
1039 | ||
1040 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
1041 | ||
1042 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1043 | ||
1044 | $this->assertArrayHasKey($url1, $content); | |
1045 | $this->assertArrayHasKey($url2, $content); | |
1046 | // it returns a database id, we don't know it, so we only check it's greater than the lowest possible value | |
1047 | $this->assertGreaterThan(1, $content[$url1]); | |
1048 | $this->assertNull($content[$url2]); | |
1049 | } | |
1050 | ||
1051 | public function testGetEntriesExistsWithManyUrlsReturnBool() | |
1052 | { | |
1053 | $url1 = 'http://0.0.0.0/entry2'; | |
1054 | $url2 = 'http://0.0.0.0/entry10'; | |
1055 | $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2); | |
1056 | ||
1057 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
1058 | ||
1059 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1060 | ||
1061 | $this->assertArrayHasKey($url1, $content); | |
1062 | $this->assertArrayHasKey($url2, $content); | |
1063 | $this->assertTrue($content[$url1]); | |
1064 | $this->assertFalse($content[$url2]); | |
1065 | } | |
1066 | ||
1067 | public function testGetEntriesExistsWithManyUrlsHashed() | |
1068 | { | |
1069 | $url1 = 'http://0.0.0.0/entry2'; | |
1070 | $url2 = 'http://0.0.0.0/entry10'; | |
1071 | $this->client->request('GET', '/api/entries/exists?hashed_urls[]=' . hash('sha1', $url1) . '&hashed_urls[]=' . hash('sha1', $url2) . '&return_id=1'); | |
1072 | ||
1073 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
1074 | ||
1075 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1076 | ||
1077 | $this->assertArrayHasKey(hash('sha1', $url1), $content); | |
1078 | $this->assertArrayHasKey(hash('sha1', $url2), $content); | |
1079 | $this->assertSame(2, $content[hash('sha1', $url1)]); | |
1080 | $this->assertNull($content[hash('sha1', $url2)]); | |
1081 | } | |
1082 | ||
1083 | public function testGetEntriesExistsWithManyUrlsHashedReturnBool() | |
1084 | { | |
1085 | $url1 = 'http://0.0.0.0/entry2'; | |
1086 | $url2 = 'http://0.0.0.0/entry10'; | |
1087 | $this->client->request('GET', '/api/entries/exists?hashed_urls[]=' . hash('sha1', $url1) . '&hashed_urls[]=' . hash('sha1', $url2)); | |
1088 | ||
1089 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
1090 | ||
1091 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1092 | ||
1093 | $this->assertArrayHasKey(hash('sha1', $url1), $content); | |
1094 | $this->assertArrayHasKey(hash('sha1', $url2), $content); | |
1095 | $this->assertTrue($content[hash('sha1', $url1)]); | |
1096 | $this->assertFalse($content[hash('sha1', $url2)]); | |
1097 | } | |
1098 | ||
1099 | public function testGetEntriesExistsWhichDoesNotExists() | |
1100 | { | |
1101 | $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2'); | |
1102 | ||
1103 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
1104 | ||
1105 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1106 | ||
1107 | $this->assertFalse($content['exists']); | |
1108 | } | |
1109 | ||
1110 | public function testGetEntriesExistsWhichDoesNotExistsWithHashedUrl() | |
1111 | { | |
1112 | $this->client->request('GET', '/api/entries/exists?hashed_url=' . hash('sha1', 'http://google.com/entry2')); | |
1113 | ||
1114 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
1115 | ||
1116 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1117 | ||
1118 | $this->assertFalse($content['exists']); | |
1119 | } | |
1120 | ||
1121 | public function testGetEntriesExistsWithNoUrl() | |
1122 | { | |
1123 | $this->client->request('GET', '/api/entries/exists?url='); | |
1124 | ||
1125 | $this->assertSame(403, $this->client->getResponse()->getStatusCode()); | |
1126 | } | |
1127 | ||
1128 | public function testGetEntriesExistsWithNoHashedUrl() | |
1129 | { | |
1130 | $this->client->request('GET', '/api/entries/exists?hashed_url='); | |
1131 | ||
1132 | $this->assertSame(403, $this->client->getResponse()->getStatusCode()); | |
1133 | } | |
1134 | ||
1135 | public function testReloadEntryErrorWhileFetching() | |
1136 | { | |
1137 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') | |
1138 | ->getRepository('WallabagCoreBundle:Entry') | |
1139 | ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getUserId()); | |
1140 | ||
1141 | if (!$entry) { | |
1142 | $this->markTestSkipped('No content found in db.'); | |
1143 | } | |
1144 | ||
1145 | $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json'); | |
1146 | $this->assertSame(304, $this->client->getResponse()->getStatusCode()); | |
1147 | } | |
1148 | ||
1149 | public function testReloadEntry() | |
1150 | { | |
1151 | $this->client->request('POST', '/api/entries.json', [ | |
1152 | 'url' => 'https://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', | |
1153 | 'archive' => '1', | |
1154 | 'tags' => 'google, apple', | |
1155 | ]); | |
1156 | ||
1157 | $json = json_decode($this->client->getResponse()->getContent(), true); | |
1158 | ||
1159 | $this->setUp(); | |
1160 | ||
1161 | $this->client->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json'); | |
1162 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
1163 | ||
1164 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1165 | ||
1166 | $this->assertNotEmpty($content['title']); | |
1167 | ||
1168 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
1169 | } | |
1170 | ||
1171 | public function testPostEntriesTagsListAction() | |
1172 | { | |
1173 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') | |
1174 | ->getRepository('WallabagCoreBundle:Entry') | |
1175 | ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getUserId()); | |
1176 | ||
1177 | $tags = $entry->getTags(); | |
1178 | ||
1179 | $this->assertCount(2, $tags); | |
1180 | ||
1181 | $list = [ | |
1182 | [ | |
1183 | 'url' => 'http://0.0.0.0/entry4', | |
1184 | 'tags' => 'new tag 1, new tag 2', | |
1185 | ], | |
1186 | ]; | |
1187 | ||
1188 | $this->client->request('POST', '/api/entries/tags/lists?list=' . json_encode($list)); | |
1189 | ||
1190 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
1191 | ||
1192 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1193 | ||
1194 | $this->assertInternalType('int', $content[0]['entry']); | |
1195 | $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']); | |
1196 | ||
1197 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') | |
1198 | ->getRepository('WallabagCoreBundle:Entry') | |
1199 | ->findByUrlAndUserId('http://0.0.0.0/entry4', $this->getUserId()); | |
1200 | ||
1201 | $tags = $entry->getTags(); | |
1202 | $this->assertCount(4, $tags); | |
1203 | } | |
1204 | ||
1205 | public function testPostEntriesTagsListActionNoList() | |
1206 | { | |
1207 | $this->client->request('POST', '/api/entries/tags/lists?list=' . json_encode([])); | |
1208 | ||
1209 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
1210 | ||
1211 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1212 | ||
1213 | $this->assertEmpty($content); | |
1214 | } | |
1215 | ||
1216 | public function testDeleteEntriesTagsListAction() | |
1217 | { | |
1218 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
1219 | $entry = new Entry($em->getReference(User::class, $this->getUserId())); | |
1220 | $entry->setUrl('http://0.0.0.0/test-entry'); | |
1221 | $entry->addTag((new Tag())->setLabel('foo-tag')); | |
1222 | $entry->addTag((new Tag())->setLabel('bar-tag')); | |
1223 | $em->persist($entry); | |
1224 | $em->flush(); | |
1225 | ||
1226 | $em->clear(); | |
1227 | ||
1228 | $list = [ | |
1229 | [ | |
1230 | 'url' => 'http://0.0.0.0/test-entry', | |
1231 | 'tags' => 'foo-tag, bar-tag', | |
1232 | ], | |
1233 | ]; | |
1234 | ||
1235 | $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list)); | |
1236 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
1237 | ||
1238 | $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId()); | |
1239 | $this->assertCount(0, $entry->getTags()); | |
1240 | } | |
1241 | ||
1242 | public function testDeleteEntriesTagsListActionNoList() | |
1243 | { | |
1244 | $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode([])); | |
1245 | ||
1246 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
1247 | ||
1248 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1249 | ||
1250 | $this->assertEmpty($content); | |
1251 | } | |
1252 | ||
1253 | public function testPostEntriesListAction() | |
1254 | { | |
1255 | $list = [ | |
1256 | 'https://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', | |
1257 | 'http://0.0.0.0/entry2', | |
1258 | ]; | |
1259 | ||
1260 | $this->client->request('POST', '/api/entries/lists?urls=' . json_encode($list)); | |
1261 | ||
1262 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
1263 | ||
1264 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1265 | ||
1266 | $this->assertInternalType('int', $content[0]['entry']); | |
1267 | $this->assertSame('https://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']); | |
1268 | ||
1269 | $this->assertInternalType('int', $content[1]['entry']); | |
1270 | $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']); | |
1271 | } | |
1272 | ||
1273 | public function testPostEntriesListActionWithNoUrls() | |
1274 | { | |
1275 | $this->client->request('POST', '/api/entries/lists?urls=' . json_encode([])); | |
1276 | ||
1277 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
1278 | ||
1279 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1280 | ||
1281 | $this->assertEmpty($content); | |
1282 | } | |
1283 | ||
1284 | public function testDeleteEntriesListAction() | |
1285 | { | |
1286 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
1287 | $em->persist((new Entry($em->getReference(User::class, $this->getUserId())))->setUrl('http://0.0.0.0/test-entry1')); | |
1288 | ||
1289 | $em->flush(); | |
1290 | $em->clear(); | |
1291 | $list = [ | |
1292 | 'http://0.0.0.0/test-entry1', | |
1293 | 'http://0.0.0.0/test-entry-not-exist', | |
1294 | ]; | |
1295 | ||
1296 | $this->client->request('DELETE', '/api/entries/list?urls=' . json_encode($list)); | |
1297 | ||
1298 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
1299 | ||
1300 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1301 | ||
1302 | $this->assertTrue($content[0]['entry']); | |
1303 | $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']); | |
1304 | ||
1305 | $this->assertFalse($content[1]['entry']); | |
1306 | $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']); | |
1307 | } | |
1308 | ||
1309 | public function testDeleteEntriesListActionWithNoUrls() | |
1310 | { | |
1311 | $this->client->request('DELETE', '/api/entries/list?urls=' . json_encode([])); | |
1312 | ||
1313 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
1314 | ||
1315 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1316 | ||
1317 | $this->assertEmpty($content); | |
1318 | } | |
1319 | ||
1320 | public function testLimitBulkAction() | |
1321 | { | |
1322 | $list = [ | |
1323 | 'http://0.0.0.0/entry1', | |
1324 | 'http://0.0.0.0/entry1', | |
1325 | 'http://0.0.0.0/entry1', | |
1326 | 'http://0.0.0.0/entry1', | |
1327 | 'http://0.0.0.0/entry1', | |
1328 | 'http://0.0.0.0/entry1', | |
1329 | 'http://0.0.0.0/entry1', | |
1330 | 'http://0.0.0.0/entry1', | |
1331 | 'http://0.0.0.0/entry1', | |
1332 | 'http://0.0.0.0/entry1', | |
1333 | 'http://0.0.0.0/entry1', | |
1334 | ]; | |
1335 | ||
1336 | $this->client->request('POST', '/api/entries/lists?urls=' . json_encode($list)); | |
1337 | ||
1338 | $this->assertSame(400, $this->client->getResponse()->getStatusCode()); | |
1339 | $this->assertContains('API limit reached', $this->client->getResponse()->getContent()); | |
1340 | } | |
1341 | ||
1342 | public function testRePostEntryAndReUsePublishedAt() | |
1343 | { | |
1344 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
1345 | $entry = new Entry($em->getReference(User::class, $this->getUserId())); | |
1346 | $entry->setTitle('Antoine de Caunes : « Je veux avoir le droit de tâtonner »'); | |
1347 | $entry->setContent('hihi'); | |
1348 | $entry->setUrl('https://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html'); | |
1349 | $entry->setPublishedAt(new \DateTime('2017-06-26T07:46:02+0200')); | |
1350 | $em->persist($entry); | |
1351 | $em->flush(); | |
1352 | $em->clear(); | |
1353 | ||
1354 | $this->client->request('POST', '/api/entries.json', [ | |
1355 | 'url' => 'https://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html', | |
1356 | ]); | |
1357 | ||
1358 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
1359 | ||
1360 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1361 | ||
1362 | $this->assertGreaterThan(0, $content['id']); | |
1363 | $this->assertSame('https://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html', $content['url']); | |
1364 | } | |
1365 | } |