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