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