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