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