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