]>
Commit | Line | Data |
---|---|---|
900c8448 NL |
1 | <?php |
2 | ||
3 | namespace Tests\Wallabag\ApiBundle\Controller; | |
4 | ||
5 | use Tests\Wallabag\ApiBundle\WallabagApiTestCase; | |
7ab5eb95 | 6 | use Wallabag\CoreBundle\Entity\Entry; |
900c8448 | 7 | use Wallabag\CoreBundle\Entity\Tag; |
08f29ae7 | 8 | use Wallabag\CoreBundle\Helper\ContentProxy; |
7ab5eb95 | 9 | use Wallabag\UserBundle\Entity\User; |
900c8448 NL |
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->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
26 | ||
27 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
28 | ||
29 | $this->assertEquals($entry->getTitle(), $content['title']); | |
30 | $this->assertEquals($entry->getUrl(), $content['url']); | |
31 | $this->assertCount(count($entry->getTags()), $content['tags']); | |
32 | $this->assertEquals($entry->getUserName(), $content['user_name']); | |
33 | $this->assertEquals($entry->getUserEmail(), $content['user_email']); | |
34 | $this->assertEquals($entry->getUserId(), $content['user_id']); | |
35 | ||
5a619812 JB |
36 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
37 | } | |
38 | ||
39 | public function testExportEntry() | |
40 | { | |
41 | $entry = $this->client->getContainer() | |
42 | ->get('doctrine.orm.entity_manager') | |
43 | ->getRepository('WallabagCoreBundle:Entry') | |
44 | ->findOneBy(['user' => 1, 'isArchived' => false]); | |
45 | ||
46 | if (!$entry) { | |
47 | $this->markTestSkipped('No content found in db.'); | |
48 | } | |
49 | ||
50 | $this->client->request('GET', '/api/entries/'.$entry->getId().'/export.epub'); | |
51 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
52 | ||
53 | // epub format got the content type in the content | |
54 | $this->assertContains('application/epub', $this->client->getResponse()->getContent()); | |
55 | $this->assertEquals('application/epub+zip', $this->client->getResponse()->headers->get('Content-Type')); | |
56 | ||
57 | // re-auth client for mobi | |
58 | $client = $this->createAuthorizedClient(); | |
59 | $client->request('GET', '/api/entries/'.$entry->getId().'/export.mobi'); | |
60 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
61 | ||
62 | $this->assertEquals('application/x-mobipocket-ebook', $client->getResponse()->headers->get('Content-Type')); | |
63 | ||
64 | // re-auth client for pdf | |
65 | $client = $this->createAuthorizedClient(); | |
66 | $client->request('GET', '/api/entries/'.$entry->getId().'/export.pdf'); | |
67 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
68 | ||
69 | $this->assertContains('PDF-', $client->getResponse()->getContent()); | |
70 | $this->assertEquals('application/pdf', $client->getResponse()->headers->get('Content-Type')); | |
71 | ||
72 | // re-auth client for pdf | |
73 | $client = $this->createAuthorizedClient(); | |
74 | $client->request('GET', '/api/entries/'.$entry->getId().'/export.txt'); | |
75 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
76 | ||
77 | $this->assertContains('text/plain', $client->getResponse()->headers->get('Content-Type')); | |
78 | ||
79 | // re-auth client for pdf | |
80 | $client = $this->createAuthorizedClient(); | |
81 | $client->request('GET', '/api/entries/'.$entry->getId().'/export.csv'); | |
82 | $this->assertEquals(200, $client->getResponse()->getStatusCode()); | |
83 | ||
84 | $this->assertContains('application/csv', $client->getResponse()->headers->get('Content-Type')); | |
900c8448 NL |
85 | } |
86 | ||
87 | public function testGetOneEntryWrongUser() | |
88 | { | |
89 | $entry = $this->client->getContainer() | |
90 | ->get('doctrine.orm.entity_manager') | |
91 | ->getRepository('WallabagCoreBundle:Entry') | |
92 | ->findOneBy(['user' => 2, 'isArchived' => false]); | |
93 | ||
94 | if (!$entry) { | |
95 | $this->markTestSkipped('No content found in db.'); | |
96 | } | |
97 | ||
98 | $this->client->request('GET', '/api/entries/'.$entry->getId().'.json'); | |
99 | ||
100 | $this->assertEquals(403, $this->client->getResponse()->getStatusCode()); | |
101 | } | |
102 | ||
103 | public function testGetEntries() | |
104 | { | |
105 | $this->client->request('GET', '/api/entries'); | |
106 | ||
107 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
108 | ||
109 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
110 | ||
111 | $this->assertGreaterThanOrEqual(1, count($content)); | |
112 | $this->assertNotEmpty($content['_embedded']['items']); | |
113 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
114 | $this->assertEquals(1, $content['page']); | |
115 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
116 | ||
5a619812 | 117 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
900c8448 NL |
118 | } |
119 | ||
120 | public function testGetEntriesWithFullOptions() | |
121 | { | |
122 | $this->client->request('GET', '/api/entries', [ | |
123 | 'archive' => 1, | |
124 | 'starred' => 1, | |
125 | 'sort' => 'updated', | |
126 | 'order' => 'asc', | |
127 | 'page' => 1, | |
128 | 'perPage' => 2, | |
129 | 'tags' => 'foo', | |
130 | 'since' => 1443274283, | |
131 | ]); | |
132 | ||
133 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
134 | ||
135 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
136 | ||
137 | $this->assertGreaterThanOrEqual(1, count($content)); | |
138 | $this->assertArrayHasKey('items', $content['_embedded']); | |
139 | $this->assertGreaterThanOrEqual(0, $content['total']); | |
140 | $this->assertEquals(1, $content['page']); | |
141 | $this->assertEquals(2, $content['limit']); | |
142 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
143 | ||
144 | $this->assertArrayHasKey('_links', $content); | |
145 | $this->assertArrayHasKey('self', $content['_links']); | |
146 | $this->assertArrayHasKey('first', $content['_links']); | |
147 | $this->assertArrayHasKey('last', $content['_links']); | |
148 | ||
149 | foreach (['self', 'first', 'last'] as $link) { | |
150 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
151 | $this->assertContains('archive=1', $content['_links'][$link]['href']); | |
152 | $this->assertContains('starred=1', $content['_links'][$link]['href']); | |
153 | $this->assertContains('sort=updated', $content['_links'][$link]['href']); | |
154 | $this->assertContains('order=asc', $content['_links'][$link]['href']); | |
155 | $this->assertContains('tags=foo', $content['_links'][$link]['href']); | |
156 | $this->assertContains('since=1443274283', $content['_links'][$link]['href']); | |
157 | } | |
158 | ||
5a619812 | 159 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
900c8448 NL |
160 | } |
161 | ||
b60a666d | 162 | public function testGetEntriesOnPageTwo() |
163 | { | |
164 | $this->client->request('GET', '/api/entries', [ | |
165 | 'page' => 2, | |
166 | 'perPage' => 2, | |
167 | ]); | |
168 | ||
169 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
170 | ||
171 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
172 | ||
173 | $this->assertGreaterThanOrEqual(0, $content['total']); | |
174 | $this->assertEquals(2, $content['page']); | |
175 | $this->assertEquals(2, $content['limit']); | |
176 | } | |
177 | ||
900c8448 NL |
178 | public function testGetStarredEntries() |
179 | { | |
180 | $this->client->request('GET', '/api/entries', ['starred' => 1, 'sort' => 'updated']); | |
181 | ||
182 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
183 | ||
184 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
185 | ||
186 | $this->assertGreaterThanOrEqual(1, count($content)); | |
187 | $this->assertNotEmpty($content['_embedded']['items']); | |
188 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
189 | $this->assertEquals(1, $content['page']); | |
190 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
191 | ||
192 | $this->assertArrayHasKey('_links', $content); | |
193 | $this->assertArrayHasKey('self', $content['_links']); | |
194 | $this->assertArrayHasKey('first', $content['_links']); | |
195 | $this->assertArrayHasKey('last', $content['_links']); | |
196 | ||
197 | foreach (['self', 'first', 'last'] as $link) { | |
198 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
199 | $this->assertContains('starred=1', $content['_links'][$link]['href']); | |
200 | $this->assertContains('sort=updated', $content['_links'][$link]['href']); | |
201 | } | |
202 | ||
5a619812 | 203 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
900c8448 NL |
204 | } |
205 | ||
206 | public function testGetArchiveEntries() | |
207 | { | |
208 | $this->client->request('GET', '/api/entries', ['archive' => 1]); | |
209 | ||
210 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
211 | ||
212 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
213 | ||
214 | $this->assertGreaterThanOrEqual(1, count($content)); | |
215 | $this->assertNotEmpty($content['_embedded']['items']); | |
216 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
217 | $this->assertEquals(1, $content['page']); | |
218 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
219 | ||
220 | $this->assertArrayHasKey('_links', $content); | |
221 | $this->assertArrayHasKey('self', $content['_links']); | |
222 | $this->assertArrayHasKey('first', $content['_links']); | |
223 | $this->assertArrayHasKey('last', $content['_links']); | |
224 | ||
225 | foreach (['self', 'first', 'last'] as $link) { | |
226 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
227 | $this->assertContains('archive=1', $content['_links'][$link]['href']); | |
228 | } | |
229 | ||
5a619812 | 230 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
900c8448 NL |
231 | } |
232 | ||
233 | public function testGetTaggedEntries() | |
234 | { | |
235 | $this->client->request('GET', '/api/entries', ['tags' => 'foo,bar']); | |
236 | ||
237 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
238 | ||
239 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
240 | ||
241 | $this->assertGreaterThanOrEqual(1, count($content)); | |
242 | $this->assertNotEmpty($content['_embedded']['items']); | |
243 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
244 | $this->assertEquals(1, $content['page']); | |
245 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
246 | ||
247 | $this->assertArrayHasKey('_links', $content); | |
248 | $this->assertArrayHasKey('self', $content['_links']); | |
249 | $this->assertArrayHasKey('first', $content['_links']); | |
250 | $this->assertArrayHasKey('last', $content['_links']); | |
251 | ||
252 | foreach (['self', 'first', 'last'] as $link) { | |
253 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
254 | $this->assertContains('tags='.urlencode('foo,bar'), $content['_links'][$link]['href']); | |
255 | } | |
256 | ||
5a619812 | 257 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
900c8448 NL |
258 | } |
259 | ||
260 | public function testGetDatedEntries() | |
261 | { | |
262 | $this->client->request('GET', '/api/entries', ['since' => 1443274283]); | |
263 | ||
264 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
265 | ||
266 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
267 | ||
268 | $this->assertGreaterThanOrEqual(1, count($content)); | |
269 | $this->assertNotEmpty($content['_embedded']['items']); | |
270 | $this->assertGreaterThanOrEqual(1, $content['total']); | |
271 | $this->assertEquals(1, $content['page']); | |
272 | $this->assertGreaterThanOrEqual(1, $content['pages']); | |
273 | ||
274 | $this->assertArrayHasKey('_links', $content); | |
275 | $this->assertArrayHasKey('self', $content['_links']); | |
276 | $this->assertArrayHasKey('first', $content['_links']); | |
277 | $this->assertArrayHasKey('last', $content['_links']); | |
278 | ||
279 | foreach (['self', 'first', 'last'] as $link) { | |
280 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
281 | $this->assertContains('since=1443274283', $content['_links'][$link]['href']); | |
282 | } | |
283 | ||
5a619812 | 284 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
900c8448 NL |
285 | } |
286 | ||
287 | public function testGetDatedSupEntries() | |
288 | { | |
289 | $future = new \DateTime(date('Y-m-d H:i:s')); | |
290 | $this->client->request('GET', '/api/entries', ['since' => $future->getTimestamp() + 1000]); | |
291 | ||
292 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
293 | ||
294 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
295 | ||
296 | $this->assertGreaterThanOrEqual(1, count($content)); | |
297 | $this->assertEmpty($content['_embedded']['items']); | |
298 | $this->assertEquals(0, $content['total']); | |
299 | $this->assertEquals(1, $content['page']); | |
300 | $this->assertEquals(1, $content['pages']); | |
301 | ||
302 | $this->assertArrayHasKey('_links', $content); | |
303 | $this->assertArrayHasKey('self', $content['_links']); | |
304 | $this->assertArrayHasKey('first', $content['_links']); | |
305 | $this->assertArrayHasKey('last', $content['_links']); | |
306 | ||
307 | foreach (['self', 'first', 'last'] as $link) { | |
308 | $this->assertArrayHasKey('href', $content['_links'][$link]); | |
309 | $this->assertContains('since='.($future->getTimestamp() + 1000), $content['_links'][$link]['href']); | |
310 | } | |
311 | ||
5a619812 | 312 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
900c8448 NL |
313 | } |
314 | ||
315 | public function testDeleteEntry() | |
316 | { | |
317 | $entry = $this->client->getContainer() | |
318 | ->get('doctrine.orm.entity_manager') | |
319 | ->getRepository('WallabagCoreBundle:Entry') | |
dcbebc17 | 320 | ->findOneByUser(1, ['id' => 'asc']); |
900c8448 NL |
321 | |
322 | if (!$entry) { | |
323 | $this->markTestSkipped('No content found in db.'); | |
324 | } | |
325 | ||
326 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json'); | |
327 | ||
328 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
329 | ||
330 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
331 | ||
332 | $this->assertEquals($entry->getTitle(), $content['title']); | |
333 | $this->assertEquals($entry->getUrl(), $content['url']); | |
334 | ||
335 | // We'll try to delete this entry again | |
336 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'.json'); | |
337 | ||
338 | $this->assertEquals(404, $this->client->getResponse()->getStatusCode()); | |
339 | } | |
340 | ||
341 | public function testPostEntry() | |
342 | { | |
343 | $this->client->request('POST', '/api/entries.json', [ | |
344 | 'url' => 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', | |
345 | 'tags' => 'google', | |
346 | 'title' => 'New title for my article', | |
e668a812 | 347 | 'content' => 'my content', |
e9056dd9 | 348 | 'language' => 'de', |
e668a812 | 349 | 'published_at' => '2016-09-08T11:55:58+0200', |
fb436e8c | 350 | 'authors' => 'bob,helen', |
900c8448 NL |
351 | ]); |
352 | ||
353 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
354 | ||
355 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
356 | ||
357 | $this->assertGreaterThan(0, $content['id']); | |
358 | $this->assertEquals('http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', $content['url']); | |
359 | $this->assertEquals(false, $content['is_archived']); | |
360 | $this->assertEquals(false, $content['is_starred']); | |
361 | $this->assertEquals('New title for my article', $content['title']); | |
362 | $this->assertEquals(1, $content['user_id']); | |
fdd725f5 | 363 | $this->assertCount(2, $content['tags']); |
e668a812 | 364 | $this->assertSame('my content', $content['content']); |
e9056dd9 | 365 | $this->assertSame('de', $content['language']); |
e668a812 | 366 | $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']); |
fb436e8c JB |
367 | $this->assertCount(2, $content['published_by']); |
368 | $this->assertContains('bob', $content['published_by']); | |
369 | $this->assertContains('helen', $content['published_by']); | |
900c8448 NL |
370 | } |
371 | ||
372 | public function testPostSameEntry() | |
373 | { | |
374 | $this->client->request('POST', '/api/entries.json', [ | |
375 | 'url' => 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', | |
376 | 'archive' => '1', | |
377 | 'tags' => 'google, apple', | |
378 | ]); | |
379 | ||
380 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
381 | ||
382 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
383 | ||
384 | $this->assertGreaterThan(0, $content['id']); | |
385 | $this->assertEquals('http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', $content['url']); | |
386 | $this->assertEquals(true, $content['is_archived']); | |
387 | $this->assertEquals(false, $content['is_starred']); | |
fdd725f5 | 388 | $this->assertCount(3, $content['tags']); |
900c8448 NL |
389 | } |
390 | ||
08f29ae7 | 391 | public function testPostEntryWhenFetchContentFails() |
392 | { | |
393 | /** @var \Symfony\Component\DependencyInjection\Container $container */ | |
394 | $container = $this->client->getContainer(); | |
395 | $contentProxy = $this->getMockBuilder(ContentProxy::class) | |
396 | ->disableOriginalConstructor() | |
397 | ->setMethods(['updateEntry']) | |
398 | ->getMock(); | |
399 | $contentProxy->expects($this->any()) | |
400 | ->method('updateEntry') | |
401 | ->willThrowException(new \Exception('Test Fetch content fails')); | |
402 | $container->set('wallabag_core.content_proxy', $contentProxy); | |
403 | ||
a9357a83 | 404 | try { |
405 | $this->client->request('POST', '/api/entries.json', [ | |
406 | 'url' => 'http://www.example.com/', | |
407 | ]); | |
408 | ||
409 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
410 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
411 | $this->assertGreaterThan(0, $content['id']); | |
412 | $this->assertEquals('http://www.example.com/', $content['url']); | |
413 | } finally { | |
414 | // Remove the created entry to avoid side effects on other tests | |
415 | if (isset($content['id'])) { | |
416 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
417 | $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']); | |
418 | $em->remove($entry); | |
419 | $em->flush(); | |
420 | } | |
421 | } | |
08f29ae7 | 422 | } |
423 | ||
900c8448 NL |
424 | public function testPostArchivedAndStarredEntry() |
425 | { | |
426 | $this->client->request('POST', '/api/entries.json', [ | |
427 | 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', | |
428 | 'archive' => '1', | |
429 | 'starred' => '1', | |
430 | ]); | |
431 | ||
432 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
433 | ||
434 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
435 | ||
436 | $this->assertGreaterThan(0, $content['id']); | |
437 | $this->assertEquals('http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']); | |
438 | $this->assertEquals(true, $content['is_archived']); | |
439 | $this->assertEquals(true, $content['is_starred']); | |
440 | $this->assertEquals(1, $content['user_id']); | |
441 | } | |
442 | ||
443 | public function testPostArchivedAndStarredEntryWithoutQuotes() | |
444 | { | |
445 | $this->client->request('POST', '/api/entries.json', [ | |
446 | 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', | |
447 | 'archive' => 0, | |
448 | 'starred' => 1, | |
449 | ]); | |
450 | ||
451 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
452 | ||
453 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
454 | ||
455 | $this->assertGreaterThan(0, $content['id']); | |
456 | $this->assertEquals('http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', $content['url']); | |
457 | $this->assertEquals(false, $content['is_archived']); | |
458 | $this->assertEquals(true, $content['is_starred']); | |
459 | } | |
460 | ||
461 | public function testPatchEntry() | |
462 | { | |
463 | $entry = $this->client->getContainer() | |
464 | ->get('doctrine.orm.entity_manager') | |
465 | ->getRepository('WallabagCoreBundle:Entry') | |
466 | ->findOneByUser(1); | |
467 | ||
468 | if (!$entry) { | |
469 | $this->markTestSkipped('No content found in db.'); | |
470 | } | |
471 | ||
472 | // hydrate the tags relations | |
473 | $nbTags = count($entry->getTags()); | |
474 | ||
475 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | |
476 | 'title' => 'New awesome title', | |
477 | 'tags' => 'new tag '.uniqid(), | |
478 | 'starred' => '1', | |
479 | 'archive' => '0', | |
e9056dd9 | 480 | 'language' => 'de_AT', |
645291e8 JB |
481 | 'preview_picture' => 'http://preview.io/picture.jpg', |
482 | 'authors' => 'bob,sponge', | |
483 | 'content' => 'awesome', | |
900c8448 NL |
484 | ]); |
485 | ||
486 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
487 | ||
488 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
489 | ||
490 | $this->assertEquals($entry->getId(), $content['id']); | |
491 | $this->assertEquals($entry->getUrl(), $content['url']); | |
492 | $this->assertEquals('New awesome title', $content['title']); | |
493 | $this->assertGreaterThan($nbTags, count($content['tags'])); | |
494 | $this->assertEquals(1, $content['user_id']); | |
e9056dd9 | 495 | $this->assertEquals('de_AT', $content['language']); |
645291e8 JB |
496 | $this->assertEquals('http://preview.io/picture.jpg', $content['preview_picture']); |
497 | $this->assertContains('sponge', $content['published_by']); | |
498 | $this->assertContains('bob', $content['published_by']); | |
499 | $this->assertEquals('awesome', $content['content']); | |
900c8448 NL |
500 | } |
501 | ||
502 | public function testPatchEntryWithoutQuotes() | |
503 | { | |
504 | $entry = $this->client->getContainer() | |
505 | ->get('doctrine.orm.entity_manager') | |
506 | ->getRepository('WallabagCoreBundle:Entry') | |
507 | ->findOneByUser(1); | |
508 | ||
509 | if (!$entry) { | |
510 | $this->markTestSkipped('No content found in db.'); | |
511 | } | |
512 | ||
513 | // hydrate the tags relations | |
514 | $nbTags = count($entry->getTags()); | |
515 | ||
516 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | |
517 | 'title' => 'New awesome title', | |
518 | 'tags' => 'new tag '.uniqid(), | |
519 | 'starred' => 1, | |
520 | 'archive' => 0, | |
645291e8 | 521 | 'authors' => ['bob', 'sponge'], |
900c8448 NL |
522 | ]); |
523 | ||
524 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
525 | ||
526 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
527 | ||
528 | $this->assertEquals($entry->getId(), $content['id']); | |
529 | $this->assertEquals($entry->getUrl(), $content['url']); | |
530 | $this->assertEquals('New awesome title', $content['title']); | |
531 | $this->assertGreaterThan($nbTags, count($content['tags'])); | |
645291e8 | 532 | $this->assertTrue(empty($content['published_by']), 'Authors were not saved because of an array instead of a string'); |
900c8448 NL |
533 | } |
534 | ||
535 | public function testGetTagsEntry() | |
536 | { | |
537 | $entry = $this->client->getContainer() | |
538 | ->get('doctrine.orm.entity_manager') | |
539 | ->getRepository('WallabagCoreBundle:Entry') | |
540 | ->findOneWithTags($this->user->getId()); | |
541 | ||
542 | $entry = $entry[0]; | |
543 | ||
544 | if (!$entry) { | |
545 | $this->markTestSkipped('No content found in db.'); | |
546 | } | |
547 | ||
548 | $tags = []; | |
549 | foreach ($entry->getTags() as $tag) { | |
550 | $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()]; | |
551 | } | |
552 | ||
553 | $this->client->request('GET', '/api/entries/'.$entry->getId().'/tags'); | |
554 | ||
555 | $this->assertEquals(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent()); | |
556 | } | |
557 | ||
558 | public function testPostTagsOnEntry() | |
559 | { | |
560 | $entry = $this->client->getContainer() | |
561 | ->get('doctrine.orm.entity_manager') | |
562 | ->getRepository('WallabagCoreBundle:Entry') | |
563 | ->findOneByUser(1); | |
564 | ||
565 | if (!$entry) { | |
566 | $this->markTestSkipped('No content found in db.'); | |
567 | } | |
568 | ||
569 | $nbTags = count($entry->getTags()); | |
570 | ||
571 | $newTags = 'tag1,tag2,tag3'; | |
572 | ||
573 | $this->client->request('POST', '/api/entries/'.$entry->getId().'/tags', ['tags' => $newTags]); | |
574 | ||
575 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
576 | ||
577 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
578 | ||
579 | $this->assertArrayHasKey('tags', $content); | |
580 | $this->assertEquals($nbTags + 3, count($content['tags'])); | |
581 | ||
582 | $entryDB = $this->client->getContainer() | |
583 | ->get('doctrine.orm.entity_manager') | |
584 | ->getRepository('WallabagCoreBundle:Entry') | |
585 | ->find($entry->getId()); | |
586 | ||
587 | $tagsInDB = []; | |
588 | foreach ($entryDB->getTags()->toArray() as $tag) { | |
589 | $tagsInDB[$tag->getId()] = $tag->getLabel(); | |
590 | } | |
591 | ||
592 | foreach (explode(',', $newTags) as $tag) { | |
593 | $this->assertContains($tag, $tagsInDB); | |
594 | } | |
595 | } | |
596 | ||
597 | public function testDeleteOneTagEntry() | |
598 | { | |
599 | $entry = $this->client->getContainer() | |
600 | ->get('doctrine.orm.entity_manager') | |
601 | ->getRepository('WallabagCoreBundle:Entry') | |
602 | ->findOneWithTags($this->user->getId()); | |
603 | $entry = $entry[0]; | |
604 | ||
605 | if (!$entry) { | |
606 | $this->markTestSkipped('No content found in db.'); | |
607 | } | |
608 | ||
609 | // hydrate the tags relations | |
610 | $nbTags = count($entry->getTags()); | |
611 | $tag = $entry->getTags()[0]; | |
612 | ||
613 | $this->client->request('DELETE', '/api/entries/'.$entry->getId().'/tags/'.$tag->getId().'.json'); | |
614 | ||
615 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
616 | ||
617 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
618 | ||
619 | $this->assertArrayHasKey('tags', $content); | |
620 | $this->assertEquals($nbTags - 1, count($content['tags'])); | |
621 | } | |
622 | ||
623 | public function testSaveIsArchivedAfterPost() | |
624 | { | |
625 | $entry = $this->client->getContainer() | |
626 | ->get('doctrine.orm.entity_manager') | |
627 | ->getRepository('WallabagCoreBundle:Entry') | |
628 | ->findOneBy(['user' => 1, 'isArchived' => true]); | |
629 | ||
630 | if (!$entry) { | |
631 | $this->markTestSkipped('No content found in db.'); | |
632 | } | |
633 | ||
634 | $this->client->request('POST', '/api/entries.json', [ | |
635 | 'url' => $entry->getUrl(), | |
636 | ]); | |
637 | ||
638 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
639 | ||
640 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
641 | ||
642 | $this->assertEquals(true, $content['is_archived']); | |
643 | } | |
644 | ||
645 | public function testSaveIsStarredAfterPost() | |
646 | { | |
647 | $entry = $this->client->getContainer() | |
648 | ->get('doctrine.orm.entity_manager') | |
649 | ->getRepository('WallabagCoreBundle:Entry') | |
650 | ->findOneBy(['user' => 1, 'isStarred' => true]); | |
651 | ||
652 | if (!$entry) { | |
653 | $this->markTestSkipped('No content found in db.'); | |
654 | } | |
655 | ||
656 | $this->client->request('POST', '/api/entries.json', [ | |
657 | 'url' => $entry->getUrl(), | |
658 | ]); | |
659 | ||
660 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
661 | ||
662 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
663 | ||
664 | $this->assertEquals(true, $content['is_starred']); | |
665 | } | |
666 | ||
667 | public function testSaveIsArchivedAfterPatch() | |
668 | { | |
669 | $entry = $this->client->getContainer() | |
670 | ->get('doctrine.orm.entity_manager') | |
671 | ->getRepository('WallabagCoreBundle:Entry') | |
672 | ->findOneBy(['user' => 1, 'isArchived' => true]); | |
673 | ||
674 | if (!$entry) { | |
675 | $this->markTestSkipped('No content found in db.'); | |
676 | } | |
677 | ||
678 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | |
679 | 'title' => $entry->getTitle().'++', | |
680 | ]); | |
681 | ||
682 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
683 | ||
684 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
685 | ||
686 | $this->assertEquals(true, $content['is_archived']); | |
687 | } | |
688 | ||
689 | public function testSaveIsStarredAfterPatch() | |
690 | { | |
691 | $entry = $this->client->getContainer() | |
692 | ->get('doctrine.orm.entity_manager') | |
693 | ->getRepository('WallabagCoreBundle:Entry') | |
694 | ->findOneBy(['user' => 1, 'isStarred' => true]); | |
695 | ||
696 | if (!$entry) { | |
697 | $this->markTestSkipped('No content found in db.'); | |
698 | } | |
699 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'.json', [ | |
700 | 'title' => $entry->getTitle().'++', | |
701 | ]); | |
702 | ||
703 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
704 | ||
705 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
706 | ||
707 | $this->assertEquals(true, $content['is_starred']); | |
708 | } | |
709 | ||
710 | public function testGetEntriesExists() | |
711 | { | |
712 | $this->client->request('GET', '/api/entries/exists?url=http://0.0.0.0/entry2'); | |
713 | ||
714 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
715 | ||
716 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
717 | ||
ca9a83ee | 718 | $this->assertEquals(2, $content['exists']); |
900c8448 NL |
719 | } |
720 | ||
721 | public function testGetEntriesExistsWithManyUrls() | |
722 | { | |
723 | $url1 = 'http://0.0.0.0/entry2'; | |
724 | $url2 = 'http://0.0.0.0/entry10'; | |
725 | $this->client->request('GET', '/api/entries/exists?urls[]='.$url1.'&urls[]='.$url2); | |
726 | ||
727 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
728 | ||
729 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
730 | ||
731 | $this->assertArrayHasKey($url1, $content); | |
732 | $this->assertArrayHasKey($url2, $content); | |
ca9a83ee | 733 | $this->assertEquals(2, $content[$url1]); |
900c8448 NL |
734 | $this->assertEquals(false, $content[$url2]); |
735 | } | |
736 | ||
737 | public function testGetEntriesExistsWhichDoesNotExists() | |
738 | { | |
739 | $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2'); | |
740 | ||
741 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
742 | ||
743 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
744 | ||
745 | $this->assertEquals(false, $content['exists']); | |
746 | } | |
747 | ||
748 | public function testGetEntriesExistsWithNoUrl() | |
749 | { | |
750 | $this->client->request('GET', '/api/entries/exists?url='); | |
751 | ||
752 | $this->assertEquals(403, $this->client->getResponse()->getStatusCode()); | |
753 | } | |
0a6f4568 JB |
754 | |
755 | public function testReloadEntryErrorWhileFetching() | |
756 | { | |
70584b42 | 757 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') |
0a6f4568 | 758 | ->getRepository('WallabagCoreBundle:Entry') |
70584b42 | 759 | ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); |
0a6f4568 JB |
760 | |
761 | if (!$entry) { | |
762 | $this->markTestSkipped('No content found in db.'); | |
763 | } | |
764 | ||
765 | $this->client->request('PATCH', '/api/entries/'.$entry->getId().'/reload.json'); | |
5cd0857e | 766 | $this->assertEquals(304, $this->client->getResponse()->getStatusCode()); |
0a6f4568 JB |
767 | } |
768 | ||
769 | public function testReloadEntry() | |
770 | { | |
771 | $this->client->request('POST', '/api/entries.json', [ | |
772 | 'url' => 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', | |
773 | 'archive' => '1', | |
774 | 'tags' => 'google, apple', | |
775 | ]); | |
776 | ||
777 | $json = json_decode($this->client->getResponse()->getContent(), true); | |
778 | ||
779 | $this->setUp(); | |
780 | ||
781 | $this->client->request('PATCH', '/api/entries/'.$json['id'].'/reload.json'); | |
782 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
783 | ||
784 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
785 | ||
786 | $this->assertNotEmpty($content['title']); | |
787 | ||
788 | $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); | |
789 | } | |
d1fc5902 NL |
790 | |
791 | public function testPostEntriesTagsListAction() | |
792 | { | |
80299ed2 NL |
793 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') |
794 | ->getRepository('WallabagCoreBundle:Entry') | |
dcbebc17 | 795 | ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); |
80299ed2 NL |
796 | |
797 | $tags = $entry->getTags(); | |
798 | ||
dcbebc17 | 799 | $this->assertCount(2, $tags); |
80299ed2 | 800 | |
d1fc5902 NL |
801 | $list = [ |
802 | [ | |
dcbebc17 | 803 | 'url' => 'http://0.0.0.0/entry4', |
80299ed2 | 804 | 'tags' => 'new tag 1, new tag 2', |
d1fc5902 | 805 | ], |
80299ed2 NL |
806 | ]; |
807 | ||
808 | $this->client->request('POST', '/api/entries/tags/lists?list='.json_encode($list)); | |
809 | ||
810 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
811 | ||
812 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
813 | ||
814 | $this->assertInternalType('int', $content[0]['entry']); | |
dcbebc17 | 815 | $this->assertEquals('http://0.0.0.0/entry4', $content[0]['url']); |
80299ed2 NL |
816 | |
817 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') | |
818 | ->getRepository('WallabagCoreBundle:Entry') | |
dcbebc17 | 819 | ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); |
80299ed2 NL |
820 | |
821 | $tags = $entry->getTags(); | |
dcbebc17 | 822 | $this->assertCount(4, $tags); |
80299ed2 NL |
823 | } |
824 | ||
825 | public function testDeleteEntriesTagsListAction() | |
826 | { | |
7ab5eb95 | 827 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); |
828 | $entry = new Entry($em->getReference(User::class, 1)); | |
829 | $entry->setUrl('http://0.0.0.0/test-entry'); | |
830 | $entry->addTag((new Tag())->setLabel('foo-tag')); | |
831 | $entry->addTag((new Tag())->setLabel('bar-tag')); | |
832 | $em->persist($entry); | |
833 | $em->flush(); | |
80299ed2 | 834 | |
7ab5eb95 | 835 | $em->clear(); |
80299ed2 NL |
836 | |
837 | $list = [ | |
d1fc5902 | 838 | [ |
7ab5eb95 | 839 | 'url' => 'http://0.0.0.0/test-entry', |
840 | 'tags' => 'foo-tag, bar-tag', | |
d1fc5902 NL |
841 | ], |
842 | ]; | |
843 | ||
7d2d1d68 | 844 | $this->client->request('DELETE', '/api/entries/tags/list?list='.json_encode($list)); |
7ab5eb95 | 845 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
846 | ||
847 | $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId()); | |
848 | $this->assertCount(0, $entry->getTags()); | |
1eca7831 NL |
849 | } |
850 | ||
a7abcc7b | 851 | public function testPostEntriesListAction() |
1eca7831 NL |
852 | { |
853 | $list = [ | |
a7abcc7b | 854 | 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', |
7fa844a3 | 855 | 'http://0.0.0.0/entry2', |
1eca7831 NL |
856 | ]; |
857 | ||
a7abcc7b | 858 | $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list)); |
d1fc5902 NL |
859 | |
860 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | |
861 | ||
862 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
863 | ||
80299ed2 | 864 | $this->assertInternalType('int', $content[0]['entry']); |
a7abcc7b | 865 | $this->assertEquals('http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', $content[0]['url']); |
d1fc5902 | 866 | |
1eca7831 | 867 | $this->assertInternalType('int', $content[1]['entry']); |
7fa844a3 | 868 | $this->assertEquals('http://0.0.0.0/entry2', $content[1]['url']); |
a7abcc7b | 869 | } |
d1fc5902 | 870 | |
a7abcc7b NL |
871 | public function testDeleteEntriesListAction() |
872 | { | |
7ab5eb95 | 873 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); |
874 | $em->persist((new Entry($em->getReference(User::class, 1)))->setUrl('http://0.0.0.0/test-entry1')); | |
875 | ||
876 | $em->flush(); | |
877 | $em->clear(); | |
a7abcc7b | 878 | $list = [ |
7ab5eb95 | 879 | 'http://0.0.0.0/test-entry1', |
880 | 'http://0.0.0.0/test-entry-not-exist', | |
a7abcc7b NL |
881 | ]; |
882 | ||
883 | $this->client->request('DELETE', '/api/entries/list?urls='.json_encode($list)); | |
d1fc5902 | 884 | |
a7abcc7b NL |
885 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
886 | ||
887 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
888 | ||
889 | $this->assertTrue($content[0]['entry']); | |
7ab5eb95 | 890 | $this->assertEquals('http://0.0.0.0/test-entry1', $content[0]['url']); |
1eca7831 | 891 | |
a7abcc7b | 892 | $this->assertFalse($content[1]['entry']); |
7ab5eb95 | 893 | $this->assertEquals('http://0.0.0.0/test-entry-not-exist', $content[1]['url']); |
d1fc5902 | 894 | } |
efd351c9 | 895 | |
efd351c9 NL |
896 | public function testLimitBulkAction() |
897 | { | |
898 | $list = [ | |
899 | 'http://0.0.0.0/entry1', | |
900 | 'http://0.0.0.0/entry1', | |
901 | 'http://0.0.0.0/entry1', | |
902 | 'http://0.0.0.0/entry1', | |
903 | 'http://0.0.0.0/entry1', | |
904 | 'http://0.0.0.0/entry1', | |
905 | 'http://0.0.0.0/entry1', | |
906 | 'http://0.0.0.0/entry1', | |
907 | 'http://0.0.0.0/entry1', | |
908 | 'http://0.0.0.0/entry1', | |
909 | 'http://0.0.0.0/entry1', | |
910 | ]; | |
911 | ||
912 | $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list)); | |
72db15ca JB |
913 | |
914 | $this->assertEquals(400, $this->client->getResponse()->getStatusCode()); | |
915 | $this->assertContains('API limit reached', $this->client->getResponse()->getContent()); | |
d1fc5902 | 916 | } |
900c8448 | 917 | } |