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