]>
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']); | |
900c8448 NL |
411 | |
412 | // We'll try to delete this entry again | |
f808b016 | 413 | $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '.json'); |
900c8448 | 414 | |
f808b016 | 415 | $this->assertSame(404, $this->client->getResponse()->getStatusCode()); |
900c8448 NL |
416 | } |
417 | ||
418 | public function testPostEntry() | |
419 | { | |
420 | $this->client->request('POST', '/api/entries.json', [ | |
421 | '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', | |
422 | 'tags' => 'google', | |
423 | 'title' => 'New title for my article', | |
e668a812 | 424 | 'content' => 'my content', |
e9056dd9 | 425 | 'language' => 'de', |
e668a812 | 426 | 'published_at' => '2016-09-08T11:55:58+0200', |
fb436e8c | 427 | 'authors' => 'bob,helen', |
1112e547 | 428 | 'public' => 1, |
900c8448 NL |
429 | ]); |
430 | ||
f808b016 | 431 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
900c8448 NL |
432 | |
433 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
434 | ||
435 | $this->assertGreaterThan(0, $content['id']); | |
f808b016 | 436 | $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 |
437 | $this->assertSame(0, $content['is_archived']); |
438 | $this->assertSame(0, $content['is_starred']); | |
a991c46e | 439 | $this->assertNull($content['starred_at']); |
f808b016 JB |
440 | $this->assertSame('New title for my article', $content['title']); |
441 | $this->assertSame(1, $content['user_id']); | |
fdd725f5 | 442 | $this->assertCount(2, $content['tags']); |
65152fcb | 443 | $this->assertNull($content['origin_url']); |
e668a812 | 444 | $this->assertSame('my content', $content['content']); |
e9056dd9 | 445 | $this->assertSame('de', $content['language']); |
e668a812 | 446 | $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']); |
fb436e8c JB |
447 | $this->assertCount(2, $content['published_by']); |
448 | $this->assertContains('bob', $content['published_by']); | |
449 | $this->assertContains('helen', $content['published_by']); | |
1112e547 | 450 | $this->assertTrue($content['is_public'], 'A public link has been generated for that entry'); |
900c8448 NL |
451 | } |
452 | ||
453 | public function testPostSameEntry() | |
454 | { | |
ff9f89fd JB |
455 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); |
456 | $entry = new Entry($em->getReference(User::class, 1)); | |
457 | $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'); | |
458 | $entry->setArchived(true); | |
459 | $entry->addTag((new Tag())->setLabel('google')); | |
460 | $entry->addTag((new Tag())->setLabel('apple')); | |
461 | $em->persist($entry); | |
462 | $em->flush(); | |
463 | $em->clear(); | |
464 | ||
900c8448 NL |
465 | $this->client->request('POST', '/api/entries.json', [ |
466 | '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', | |
467 | 'archive' => '1', | |
468 | 'tags' => 'google, apple', | |
469 | ]); | |
470 | ||
f808b016 | 471 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
900c8448 NL |
472 | |
473 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
474 | ||
475 | $this->assertGreaterThan(0, $content['id']); | |
f808b016 | 476 | $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 |
477 | $this->assertSame(1, $content['is_archived']); |
478 | $this->assertSame(0, $content['is_starred']); | |
fdd725f5 | 479 | $this->assertCount(3, $content['tags']); |
900c8448 NL |
480 | } |
481 | ||
08f29ae7 | 482 | public function testPostEntryWhenFetchContentFails() |
483 | { | |
484 | /** @var \Symfony\Component\DependencyInjection\Container $container */ | |
485 | $container = $this->client->getContainer(); | |
486 | $contentProxy = $this->getMockBuilder(ContentProxy::class) | |
487 | ->disableOriginalConstructor() | |
488 | ->setMethods(['updateEntry']) | |
489 | ->getMock(); | |
490 | $contentProxy->expects($this->any()) | |
491 | ->method('updateEntry') | |
492 | ->willThrowException(new \Exception('Test Fetch content fails')); | |
493 | $container->set('wallabag_core.content_proxy', $contentProxy); | |
494 | ||
a9357a83 | 495 | try { |
496 | $this->client->request('POST', '/api/entries.json', [ | |
497 | 'url' => 'http://www.example.com/', | |
498 | ]); | |
499 | ||
f808b016 | 500 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
a9357a83 | 501 | $content = json_decode($this->client->getResponse()->getContent(), true); |
502 | $this->assertGreaterThan(0, $content['id']); | |
f808b016 | 503 | $this->assertSame('http://www.example.com/', $content['url']); |
a9357a83 | 504 | } finally { |
505 | // Remove the created entry to avoid side effects on other tests | |
506 | if (isset($content['id'])) { | |
507 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
508 | $entry = $em->getReference('WallabagCoreBundle:Entry', $content['id']); | |
509 | $em->remove($entry); | |
510 | $em->flush(); | |
511 | } | |
512 | } | |
08f29ae7 | 513 | } |
514 | ||
900c8448 NL |
515 | public function testPostArchivedAndStarredEntry() |
516 | { | |
a991c46e | 517 | $now = new \DateTime(); |
900c8448 NL |
518 | $this->client->request('POST', '/api/entries.json', [ |
519 | 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', | |
520 | 'archive' => '1', | |
521 | 'starred' => '1', | |
522 | ]); | |
523 | ||
f808b016 | 524 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
900c8448 NL |
525 | |
526 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
527 | ||
528 | $this->assertGreaterThan(0, $content['id']); | |
f808b016 | 529 | $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 |
530 | $this->assertSame(1, $content['is_archived']); |
531 | $this->assertSame(1, $content['is_starred']); | |
a991c46e | 532 | $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['starred_at']))->getTimestamp()); |
f808b016 | 533 | $this->assertSame(1, $content['user_id']); |
900c8448 NL |
534 | } |
535 | ||
536 | public function testPostArchivedAndStarredEntryWithoutQuotes() | |
537 | { | |
538 | $this->client->request('POST', '/api/entries.json', [ | |
539 | 'url' => 'http://www.lemonde.fr/idees/article/2016/02/08/preserver-la-liberte-d-expression-sur-les-reseaux-sociaux_4861503_3232.html', | |
540 | 'archive' => 0, | |
541 | 'starred' => 1, | |
542 | ]); | |
543 | ||
f808b016 | 544 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
900c8448 NL |
545 | |
546 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
547 | ||
548 | $this->assertGreaterThan(0, $content['id']); | |
f808b016 | 549 | $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 |
550 | $this->assertSame(0, $content['is_archived']); |
551 | $this->assertSame(1, $content['is_starred']); | |
900c8448 NL |
552 | } |
553 | ||
00f2368f KD |
554 | public function testPostEntryWithOriginUrl() |
555 | { | |
556 | $this->client->request('POST', '/api/entries.json', [ | |
557 | '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', | |
558 | 'tags' => 'google', | |
559 | 'title' => 'New title for my article', | |
560 | 'content' => 'my content', | |
561 | 'language' => 'de', | |
562 | 'published_at' => '2016-09-08T11:55:58+0200', | |
563 | 'authors' => 'bob,helen', | |
564 | 'public' => 1, | |
565 | 'origin_url' => 'http://mysource.tld', | |
566 | ]); | |
567 | ||
568 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
569 | ||
570 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
571 | ||
572 | $this->assertGreaterThan(0, $content['id']); | |
573 | $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']); | |
574 | $this->assertSame('http://mysource.tld', $content['origin_url']); | |
575 | } | |
576 | ||
900c8448 NL |
577 | public function testPatchEntry() |
578 | { | |
579 | $entry = $this->client->getContainer() | |
580 | ->get('doctrine.orm.entity_manager') | |
581 | ->getRepository('WallabagCoreBundle:Entry') | |
582 | ->findOneByUser(1); | |
583 | ||
584 | if (!$entry) { | |
585 | $this->markTestSkipped('No content found in db.'); | |
586 | } | |
587 | ||
c18a2476 | 588 | $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [ |
900c8448 | 589 | 'title' => 'New awesome title', |
f808b016 | 590 | 'tags' => 'new tag ' . uniqid(), |
900c8448 NL |
591 | 'starred' => '1', |
592 | 'archive' => '0', | |
e9056dd9 | 593 | 'language' => 'de_AT', |
645291e8 JB |
594 | 'preview_picture' => 'http://preview.io/picture.jpg', |
595 | 'authors' => 'bob,sponge', | |
596 | 'content' => 'awesome', | |
1112e547 | 597 | 'public' => 0, |
a05b6115 | 598 | 'published_at' => 1488833381, |
900c8448 NL |
599 | ]); |
600 | ||
f808b016 | 601 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
900c8448 NL |
602 | |
603 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
604 | ||
f808b016 JB |
605 | $this->assertSame($entry->getId(), $content['id']); |
606 | $this->assertSame($entry->getUrl(), $content['url']); | |
607 | $this->assertSame('New awesome title', $content['title']); | |
a05b6115 | 608 | $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag'); |
f808b016 JB |
609 | $this->assertSame(1, $content['user_id']); |
610 | $this->assertSame('de_AT', $content['language']); | |
611 | $this->assertSame('http://preview.io/picture.jpg', $content['preview_picture']); | |
645291e8 JB |
612 | $this->assertContains('sponge', $content['published_by']); |
613 | $this->assertContains('bob', $content['published_by']); | |
f808b016 | 614 | $this->assertSame('awesome', $content['content']); |
1112e547 | 615 | $this->assertFalse($content['is_public'], 'Entry is no more shared'); |
a05b6115 | 616 | $this->assertContains('2017-03-06', $content['published_at']); |
900c8448 NL |
617 | } |
618 | ||
619 | public function testPatchEntryWithoutQuotes() | |
620 | { | |
621 | $entry = $this->client->getContainer() | |
622 | ->get('doctrine.orm.entity_manager') | |
623 | ->getRepository('WallabagCoreBundle:Entry') | |
624 | ->findOneByUser(1); | |
625 | ||
626 | if (!$entry) { | |
627 | $this->markTestSkipped('No content found in db.'); | |
628 | } | |
629 | ||
a05b6115 JB |
630 | $previousContent = $entry->getContent(); |
631 | $previousLanguage = $entry->getLanguage(); | |
900c8448 | 632 | |
f808b016 | 633 | $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [ |
900c8448 | 634 | 'title' => 'New awesome title', |
f808b016 | 635 | 'tags' => 'new tag ' . uniqid(), |
900c8448 NL |
636 | 'starred' => 1, |
637 | 'archive' => 0, | |
645291e8 | 638 | 'authors' => ['bob', 'sponge'], |
900c8448 NL |
639 | ]); |
640 | ||
f808b016 | 641 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
900c8448 NL |
642 | |
643 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
644 | ||
f808b016 JB |
645 | $this->assertSame($entry->getId(), $content['id']); |
646 | $this->assertSame($entry->getUrl(), $content['url']); | |
a05b6115 | 647 | $this->assertGreaterThanOrEqual(1, count($content['tags']), 'We force only one tag'); |
f808b016 | 648 | $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string'); |
c18a2476 JB |
649 | $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved'); |
650 | $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved'); | |
900c8448 NL |
651 | } |
652 | ||
00f2368f KD |
653 | public function testPatchEntryWithOriginUrl() |
654 | { | |
655 | $entry = $this->client->getContainer() | |
656 | ->get('doctrine.orm.entity_manager') | |
657 | ->getRepository('WallabagCoreBundle:Entry') | |
658 | ->findOneByUser(1); | |
659 | ||
660 | if (!$entry) { | |
661 | $this->markTestSkipped('No content found in db.'); | |
662 | } | |
663 | ||
664 | $previousContent = $entry->getContent(); | |
665 | $previousLanguage = $entry->getLanguage(); | |
666 | ||
667 | $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [ | |
668 | 'title' => 'Another awesome title just for profit', | |
669 | 'origin_url' => 'https://myawesomesource.example.com', | |
670 | ]); | |
671 | ||
672 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
673 | ||
674 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
675 | ||
676 | $this->assertSame($entry->getId(), $content['id']); | |
677 | $this->assertSame($entry->getUrl(), $content['url']); | |
678 | $this->assertSame('https://myawesomesource.example.com', $content['origin_url']); | |
679 | $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string'); | |
680 | $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved'); | |
681 | $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved'); | |
682 | } | |
683 | ||
65152fcb KD |
684 | public function testPatchEntryRemoveOriginUrl() |
685 | { | |
686 | $entry = $this->client->getContainer() | |
687 | ->get('doctrine.orm.entity_manager') | |
688 | ->getRepository('WallabagCoreBundle:Entry') | |
689 | ->findOneByUser(1); | |
690 | ||
691 | if (!$entry) { | |
692 | $this->markTestSkipped('No content found in db.'); | |
693 | } | |
694 | ||
695 | $previousContent = $entry->getContent(); | |
696 | $previousLanguage = $entry->getLanguage(); | |
697 | $previousTitle = $entry->getTitle(); | |
698 | ||
699 | $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [ | |
700 | 'origin_url' => '', | |
701 | ]); | |
702 | ||
703 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
704 | ||
705 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
706 | ||
707 | $this->assertSame($entry->getId(), $content['id']); | |
708 | $this->assertSame($entry->getUrl(), $content['url']); | |
709 | $this->assertEmpty($content['origin_url']); | |
710 | $this->assertEmpty($content['published_by'], 'Authors were not saved because of an array instead of a string'); | |
711 | $this->assertSame($previousContent, $content['content'], 'Ensure content has not moved'); | |
712 | $this->assertSame($previousLanguage, $content['language'], 'Ensure language has not moved'); | |
713 | $this->assertSame($previousTitle, $content['title'], 'Ensure title has not moved'); | |
714 | } | |
eae8138b KD |
715 | |
716 | public function testPatchEntryNullOriginUrl() | |
717 | { | |
718 | $entry = $this->client->getContainer() | |
65152fcb KD |
719 | ->get('doctrine.orm.entity_manager') |
720 | ->getRepository('WallabagCoreBundle:Entry') | |
721 | ->findOneByUser(1); | |
eae8138b KD |
722 | |
723 | if (!$entry) { | |
724 | $this->markTestSkipped('No content found in db.'); | |
725 | } | |
726 | ||
727 | $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [ | |
65152fcb KD |
728 | 'origin_url' => null, |
729 | ]); | |
eae8138b KD |
730 | |
731 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
732 | ||
733 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
734 | ||
735 | $this->assertNull($content['origin_url']); | |
736 | } | |
65152fcb | 737 | |
900c8448 NL |
738 | public function testGetTagsEntry() |
739 | { | |
740 | $entry = $this->client->getContainer() | |
741 | ->get('doctrine.orm.entity_manager') | |
742 | ->getRepository('WallabagCoreBundle:Entry') | |
743 | ->findOneWithTags($this->user->getId()); | |
744 | ||
745 | $entry = $entry[0]; | |
746 | ||
747 | if (!$entry) { | |
748 | $this->markTestSkipped('No content found in db.'); | |
749 | } | |
750 | ||
751 | $tags = []; | |
752 | foreach ($entry->getTags() as $tag) { | |
753 | $tags[] = ['id' => $tag->getId(), 'label' => $tag->getLabel(), 'slug' => $tag->getSlug()]; | |
754 | } | |
755 | ||
f808b016 | 756 | $this->client->request('GET', '/api/entries/' . $entry->getId() . '/tags'); |
900c8448 | 757 | |
f808b016 | 758 | $this->assertSame(json_encode($tags, JSON_HEX_QUOT), $this->client->getResponse()->getContent()); |
900c8448 NL |
759 | } |
760 | ||
761 | public function testPostTagsOnEntry() | |
762 | { | |
763 | $entry = $this->client->getContainer() | |
764 | ->get('doctrine.orm.entity_manager') | |
765 | ->getRepository('WallabagCoreBundle:Entry') | |
766 | ->findOneByUser(1); | |
767 | ||
768 | if (!$entry) { | |
769 | $this->markTestSkipped('No content found in db.'); | |
770 | } | |
771 | ||
772 | $nbTags = count($entry->getTags()); | |
773 | ||
774 | $newTags = 'tag1,tag2,tag3'; | |
775 | ||
f808b016 | 776 | $this->client->request('POST', '/api/entries/' . $entry->getId() . '/tags', ['tags' => $newTags]); |
900c8448 | 777 | |
f808b016 | 778 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
900c8448 NL |
779 | |
780 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
781 | ||
782 | $this->assertArrayHasKey('tags', $content); | |
f808b016 | 783 | $this->assertSame($nbTags + 3, count($content['tags'])); |
900c8448 NL |
784 | |
785 | $entryDB = $this->client->getContainer() | |
786 | ->get('doctrine.orm.entity_manager') | |
787 | ->getRepository('WallabagCoreBundle:Entry') | |
788 | ->find($entry->getId()); | |
789 | ||
790 | $tagsInDB = []; | |
791 | foreach ($entryDB->getTags()->toArray() as $tag) { | |
792 | $tagsInDB[$tag->getId()] = $tag->getLabel(); | |
793 | } | |
794 | ||
795 | foreach (explode(',', $newTags) as $tag) { | |
796 | $this->assertContains($tag, $tagsInDB); | |
797 | } | |
798 | } | |
799 | ||
800 | public function testDeleteOneTagEntry() | |
801 | { | |
802 | $entry = $this->client->getContainer() | |
803 | ->get('doctrine.orm.entity_manager') | |
804 | ->getRepository('WallabagCoreBundle:Entry') | |
805 | ->findOneWithTags($this->user->getId()); | |
806 | $entry = $entry[0]; | |
807 | ||
808 | if (!$entry) { | |
809 | $this->markTestSkipped('No content found in db.'); | |
810 | } | |
811 | ||
812 | // hydrate the tags relations | |
813 | $nbTags = count($entry->getTags()); | |
814 | $tag = $entry->getTags()[0]; | |
815 | ||
f808b016 | 816 | $this->client->request('DELETE', '/api/entries/' . $entry->getId() . '/tags/' . $tag->getId() . '.json'); |
900c8448 | 817 | |
f808b016 | 818 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
900c8448 NL |
819 | |
820 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
821 | ||
822 | $this->assertArrayHasKey('tags', $content); | |
f808b016 | 823 | $this->assertSame($nbTags - 1, count($content['tags'])); |
900c8448 NL |
824 | } |
825 | ||
826 | public function testSaveIsArchivedAfterPost() | |
827 | { | |
828 | $entry = $this->client->getContainer() | |
829 | ->get('doctrine.orm.entity_manager') | |
830 | ->getRepository('WallabagCoreBundle:Entry') | |
831 | ->findOneBy(['user' => 1, 'isArchived' => true]); | |
832 | ||
833 | if (!$entry) { | |
834 | $this->markTestSkipped('No content found in db.'); | |
835 | } | |
836 | ||
837 | $this->client->request('POST', '/api/entries.json', [ | |
838 | 'url' => $entry->getUrl(), | |
839 | ]); | |
840 | ||
f808b016 | 841 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
900c8448 NL |
842 | |
843 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
844 | ||
38520658 | 845 | $this->assertSame(1, $content['is_archived']); |
900c8448 NL |
846 | } |
847 | ||
848 | public function testSaveIsStarredAfterPost() | |
849 | { | |
850 | $entry = $this->client->getContainer() | |
851 | ->get('doctrine.orm.entity_manager') | |
852 | ->getRepository('WallabagCoreBundle:Entry') | |
853 | ->findOneBy(['user' => 1, 'isStarred' => true]); | |
854 | ||
855 | if (!$entry) { | |
856 | $this->markTestSkipped('No content found in db.'); | |
857 | } | |
858 | ||
859 | $this->client->request('POST', '/api/entries.json', [ | |
860 | 'url' => $entry->getUrl(), | |
861 | ]); | |
862 | ||
f808b016 | 863 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
900c8448 NL |
864 | |
865 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
866 | ||
38520658 | 867 | $this->assertSame(1, $content['is_starred']); |
900c8448 NL |
868 | } |
869 | ||
870 | public function testSaveIsArchivedAfterPatch() | |
871 | { | |
872 | $entry = $this->client->getContainer() | |
873 | ->get('doctrine.orm.entity_manager') | |
874 | ->getRepository('WallabagCoreBundle:Entry') | |
875 | ->findOneBy(['user' => 1, 'isArchived' => true]); | |
876 | ||
877 | if (!$entry) { | |
878 | $this->markTestSkipped('No content found in db.'); | |
879 | } | |
880 | ||
a05b6115 JB |
881 | $previousTitle = $entry->getTitle(); |
882 | ||
c18a2476 JB |
883 | $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [ |
884 | 'title' => $entry->getTitle() . '++', | |
900c8448 NL |
885 | ]); |
886 | ||
f808b016 | 887 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
900c8448 NL |
888 | |
889 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
890 | ||
38520658 | 891 | $this->assertSame(1, $content['is_archived']); |
c18a2476 | 892 | $this->assertSame($previousTitle . '++', $content['title']); |
900c8448 NL |
893 | } |
894 | ||
895 | public function testSaveIsStarredAfterPatch() | |
896 | { | |
a991c46e | 897 | $now = new \DateTime(); |
900c8448 NL |
898 | $entry = $this->client->getContainer() |
899 | ->get('doctrine.orm.entity_manager') | |
900 | ->getRepository('WallabagCoreBundle:Entry') | |
901 | ->findOneBy(['user' => 1, 'isStarred' => true]); | |
902 | ||
903 | if (!$entry) { | |
904 | $this->markTestSkipped('No content found in db.'); | |
905 | } | |
f808b016 JB |
906 | $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '.json', [ |
907 | 'title' => $entry->getTitle() . '++', | |
900c8448 NL |
908 | ]); |
909 | ||
f808b016 | 910 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
900c8448 NL |
911 | |
912 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
913 | ||
38520658 | 914 | $this->assertSame(1, $content['is_starred']); |
a991c46e | 915 | $this->assertGreaterThanOrEqual($now->getTimestamp(), (new \DateTime($content['starred_at']))->getTimestamp()); |
900c8448 NL |
916 | } |
917 | ||
18696f77 | 918 | public function dataForEntriesExistWithUrl() |
900c8448 | 919 | { |
18696f77 JB |
920 | return [ |
921 | 'with_id' => [ | |
922 | 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2&return_id=1', | |
923 | 'expectedValue' => 2, | |
924 | ], | |
925 | 'without_id' => [ | |
926 | 'url' => '/api/entries/exists?url=http://0.0.0.0/entry2', | |
927 | 'expectedValue' => true, | |
928 | ], | |
929 | ]; | |
930 | } | |
931 | ||
932 | /** | |
933 | * @dataProvider dataForEntriesExistWithUrl | |
934 | */ | |
935 | public function testGetEntriesExists($url, $expectedValue) | |
936 | { | |
937 | $this->client->request('GET', $url); | |
900c8448 | 938 | |
f808b016 | 939 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
900c8448 NL |
940 | |
941 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
942 | ||
18696f77 | 943 | $this->assertSame($expectedValue, $content['exists']); |
900c8448 NL |
944 | } |
945 | ||
946 | public function testGetEntriesExistsWithManyUrls() | |
18696f77 JB |
947 | { |
948 | $url1 = 'http://0.0.0.0/entry2'; | |
949 | $url2 = 'http://0.0.0.0/entry10'; | |
f808b016 | 950 | $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2 . '&return_id=1'); |
18696f77 | 951 | |
f808b016 | 952 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
18696f77 JB |
953 | |
954 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
955 | ||
956 | $this->assertArrayHasKey($url1, $content); | |
957 | $this->assertArrayHasKey($url2, $content); | |
958 | $this->assertSame(2, $content[$url1]); | |
39ffaba3 | 959 | $this->assertNull($content[$url2]); |
18696f77 JB |
960 | } |
961 | ||
962 | public function testGetEntriesExistsWithManyUrlsReturnBool() | |
900c8448 NL |
963 | { |
964 | $url1 = 'http://0.0.0.0/entry2'; | |
965 | $url2 = 'http://0.0.0.0/entry10'; | |
f808b016 | 966 | $this->client->request('GET', '/api/entries/exists?urls[]=' . $url1 . '&urls[]=' . $url2); |
900c8448 | 967 | |
f808b016 | 968 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
900c8448 NL |
969 | |
970 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
971 | ||
972 | $this->assertArrayHasKey($url1, $content); | |
973 | $this->assertArrayHasKey($url2, $content); | |
39ffaba3 JB |
974 | $this->assertTrue($content[$url1]); |
975 | $this->assertFalse($content[$url2]); | |
900c8448 NL |
976 | } |
977 | ||
978 | public function testGetEntriesExistsWhichDoesNotExists() | |
979 | { | |
980 | $this->client->request('GET', '/api/entries/exists?url=http://google.com/entry2'); | |
981 | ||
f808b016 | 982 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
900c8448 NL |
983 | |
984 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
985 | ||
64a5a6cf | 986 | $this->assertFalse($content['exists']); |
900c8448 NL |
987 | } |
988 | ||
989 | public function testGetEntriesExistsWithNoUrl() | |
990 | { | |
991 | $this->client->request('GET', '/api/entries/exists?url='); | |
992 | ||
f808b016 | 993 | $this->assertSame(403, $this->client->getResponse()->getStatusCode()); |
900c8448 | 994 | } |
0a6f4568 JB |
995 | |
996 | public function testReloadEntryErrorWhileFetching() | |
997 | { | |
70584b42 | 998 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') |
0a6f4568 | 999 | ->getRepository('WallabagCoreBundle:Entry') |
70584b42 | 1000 | ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); |
0a6f4568 JB |
1001 | |
1002 | if (!$entry) { | |
1003 | $this->markTestSkipped('No content found in db.'); | |
1004 | } | |
1005 | ||
f808b016 JB |
1006 | $this->client->request('PATCH', '/api/entries/' . $entry->getId() . '/reload.json'); |
1007 | $this->assertSame(304, $this->client->getResponse()->getStatusCode()); | |
0a6f4568 JB |
1008 | } |
1009 | ||
1010 | public function testReloadEntry() | |
1011 | { | |
1012 | $this->client->request('POST', '/api/entries.json', [ | |
1013 | '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', | |
1014 | 'archive' => '1', | |
1015 | 'tags' => 'google, apple', | |
1016 | ]); | |
1017 | ||
1018 | $json = json_decode($this->client->getResponse()->getContent(), true); | |
1019 | ||
1020 | $this->setUp(); | |
1021 | ||
f808b016 JB |
1022 | $this->client->request('PATCH', '/api/entries/' . $json['id'] . '/reload.json'); |
1023 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
0a6f4568 JB |
1024 | |
1025 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1026 | ||
1027 | $this->assertNotEmpty($content['title']); | |
1028 | ||
f808b016 | 1029 | $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); |
0a6f4568 | 1030 | } |
d1fc5902 NL |
1031 | |
1032 | public function testPostEntriesTagsListAction() | |
1033 | { | |
80299ed2 NL |
1034 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') |
1035 | ->getRepository('WallabagCoreBundle:Entry') | |
dcbebc17 | 1036 | ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); |
80299ed2 NL |
1037 | |
1038 | $tags = $entry->getTags(); | |
1039 | ||
dcbebc17 | 1040 | $this->assertCount(2, $tags); |
80299ed2 | 1041 | |
d1fc5902 NL |
1042 | $list = [ |
1043 | [ | |
dcbebc17 | 1044 | 'url' => 'http://0.0.0.0/entry4', |
80299ed2 | 1045 | 'tags' => 'new tag 1, new tag 2', |
d1fc5902 | 1046 | ], |
80299ed2 NL |
1047 | ]; |
1048 | ||
f808b016 | 1049 | $this->client->request('POST', '/api/entries/tags/lists?list=' . json_encode($list)); |
80299ed2 | 1050 | |
f808b016 | 1051 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
80299ed2 NL |
1052 | |
1053 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1054 | ||
1055 | $this->assertInternalType('int', $content[0]['entry']); | |
f808b016 | 1056 | $this->assertSame('http://0.0.0.0/entry4', $content[0]['url']); |
80299ed2 NL |
1057 | |
1058 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') | |
1059 | ->getRepository('WallabagCoreBundle:Entry') | |
dcbebc17 | 1060 | ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); |
80299ed2 NL |
1061 | |
1062 | $tags = $entry->getTags(); | |
dcbebc17 | 1063 | $this->assertCount(4, $tags); |
80299ed2 NL |
1064 | } |
1065 | ||
a05b6115 JB |
1066 | public function testPostEntriesTagsListActionNoList() |
1067 | { | |
c18a2476 | 1068 | $this->client->request('POST', '/api/entries/tags/lists?list=' . json_encode([])); |
a05b6115 | 1069 | |
c18a2476 | 1070 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
a05b6115 JB |
1071 | |
1072 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1073 | ||
1074 | $this->assertEmpty($content); | |
1075 | } | |
1076 | ||
80299ed2 NL |
1077 | public function testDeleteEntriesTagsListAction() |
1078 | { | |
7ab5eb95 | 1079 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); |
1080 | $entry = new Entry($em->getReference(User::class, 1)); | |
1081 | $entry->setUrl('http://0.0.0.0/test-entry'); | |
1082 | $entry->addTag((new Tag())->setLabel('foo-tag')); | |
1083 | $entry->addTag((new Tag())->setLabel('bar-tag')); | |
1084 | $em->persist($entry); | |
1085 | $em->flush(); | |
80299ed2 | 1086 | |
7ab5eb95 | 1087 | $em->clear(); |
80299ed2 NL |
1088 | |
1089 | $list = [ | |
d1fc5902 | 1090 | [ |
7ab5eb95 | 1091 | 'url' => 'http://0.0.0.0/test-entry', |
1092 | 'tags' => 'foo-tag, bar-tag', | |
d1fc5902 NL |
1093 | ], |
1094 | ]; | |
1095 | ||
f808b016 JB |
1096 | $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode($list)); |
1097 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
7ab5eb95 | 1098 | |
1099 | $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId()); | |
1100 | $this->assertCount(0, $entry->getTags()); | |
1eca7831 NL |
1101 | } |
1102 | ||
a05b6115 JB |
1103 | public function testDeleteEntriesTagsListActionNoList() |
1104 | { | |
c18a2476 | 1105 | $this->client->request('DELETE', '/api/entries/tags/list?list=' . json_encode([])); |
a05b6115 | 1106 | |
c18a2476 | 1107 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
a05b6115 JB |
1108 | |
1109 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1110 | ||
1111 | $this->assertEmpty($content); | |
1112 | } | |
1113 | ||
a7abcc7b | 1114 | public function testPostEntriesListAction() |
1eca7831 NL |
1115 | { |
1116 | $list = [ | |
a7abcc7b | 1117 | 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', |
7fa844a3 | 1118 | 'http://0.0.0.0/entry2', |
1eca7831 NL |
1119 | ]; |
1120 | ||
f808b016 | 1121 | $this->client->request('POST', '/api/entries/lists?urls=' . json_encode($list)); |
d1fc5902 | 1122 | |
f808b016 | 1123 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
d1fc5902 NL |
1124 | |
1125 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1126 | ||
80299ed2 | 1127 | $this->assertInternalType('int', $content[0]['entry']); |
f808b016 | 1128 | $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 | 1129 | |
1eca7831 | 1130 | $this->assertInternalType('int', $content[1]['entry']); |
f808b016 | 1131 | $this->assertSame('http://0.0.0.0/entry2', $content[1]['url']); |
a7abcc7b | 1132 | } |
d1fc5902 | 1133 | |
a05b6115 JB |
1134 | public function testPostEntriesListActionWithNoUrls() |
1135 | { | |
c18a2476 | 1136 | $this->client->request('POST', '/api/entries/lists?urls=' . json_encode([])); |
a05b6115 | 1137 | |
c18a2476 | 1138 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
a05b6115 JB |
1139 | |
1140 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1141 | ||
1142 | $this->assertEmpty($content); | |
1143 | } | |
1144 | ||
a7abcc7b NL |
1145 | public function testDeleteEntriesListAction() |
1146 | { | |
7ab5eb95 | 1147 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); |
1148 | $em->persist((new Entry($em->getReference(User::class, 1)))->setUrl('http://0.0.0.0/test-entry1')); | |
1149 | ||
1150 | $em->flush(); | |
1151 | $em->clear(); | |
a7abcc7b | 1152 | $list = [ |
7ab5eb95 | 1153 | 'http://0.0.0.0/test-entry1', |
1154 | 'http://0.0.0.0/test-entry-not-exist', | |
a7abcc7b NL |
1155 | ]; |
1156 | ||
f808b016 | 1157 | $this->client->request('DELETE', '/api/entries/list?urls=' . json_encode($list)); |
d1fc5902 | 1158 | |
f808b016 | 1159 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
a7abcc7b NL |
1160 | |
1161 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1162 | ||
1163 | $this->assertTrue($content[0]['entry']); | |
f808b016 | 1164 | $this->assertSame('http://0.0.0.0/test-entry1', $content[0]['url']); |
1eca7831 | 1165 | |
a7abcc7b | 1166 | $this->assertFalse($content[1]['entry']); |
f808b016 | 1167 | $this->assertSame('http://0.0.0.0/test-entry-not-exist', $content[1]['url']); |
d1fc5902 | 1168 | } |
efd351c9 | 1169 | |
a05b6115 JB |
1170 | public function testDeleteEntriesListActionWithNoUrls() |
1171 | { | |
c18a2476 | 1172 | $this->client->request('DELETE', '/api/entries/list?urls=' . json_encode([])); |
a05b6115 | 1173 | |
c18a2476 | 1174 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); |
a05b6115 JB |
1175 | |
1176 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1177 | ||
1178 | $this->assertEmpty($content); | |
1179 | } | |
1180 | ||
efd351c9 NL |
1181 | public function testLimitBulkAction() |
1182 | { | |
1183 | $list = [ | |
1184 | 'http://0.0.0.0/entry1', | |
1185 | 'http://0.0.0.0/entry1', | |
1186 | 'http://0.0.0.0/entry1', | |
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 | ]; | |
1196 | ||
f808b016 | 1197 | $this->client->request('POST', '/api/entries/lists?urls=' . json_encode($list)); |
72db15ca | 1198 | |
f808b016 | 1199 | $this->assertSame(400, $this->client->getResponse()->getStatusCode()); |
72db15ca | 1200 | $this->assertContains('API limit reached', $this->client->getResponse()->getContent()); |
d1fc5902 | 1201 | } |
ff9f89fd JB |
1202 | |
1203 | public function testRePostEntryAndReUsePublishedAt() | |
1204 | { | |
1205 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | |
1206 | $entry = new Entry($em->getReference(User::class, 1)); | |
1207 | $entry->setTitle('Antoine de Caunes : « Je veux avoir le droit de tâtonner »'); | |
1208 | $entry->setContent('hihi'); | |
1209 | $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'); | |
1210 | $entry->setPublishedAt(new \DateTime('2017-06-26T07:46:02+0200')); | |
1211 | $em->persist($entry); | |
1212 | $em->flush(); | |
1213 | $em->clear(); | |
1214 | ||
1215 | $this->client->request('POST', '/api/entries.json', [ | |
1216 | 'url' => 'http://www.lemonde.fr/m-perso/article/2017/06/25/antoine-de-caunes-je-veux-avoir-le-droit-de-tatonner_5150728_4497916.html', | |
1217 | ]); | |
1218 | ||
1219 | $this->assertSame(200, $this->client->getResponse()->getStatusCode()); | |
1220 | ||
1221 | $content = json_decode($this->client->getResponse()->getContent(), true); | |
1222 | ||
1223 | $this->assertGreaterThan(0, $content['id']); | |
1224 | $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']); | |
1225 | } | |
900c8448 | 1226 | } |