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