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