]>
Commit | Line | Data |
---|---|---|
7ec2897e JB |
1 | <?php |
2 | ||
23634d5d | 3 | namespace Tests\Wallabag\ImportBundle\Import; |
7ec2897e JB |
4 | |
5 | use Wallabag\UserBundle\Entity\User; | |
79d0e38e | 6 | use Wallabag\CoreBundle\Entity\Entry; |
7ec2897e | 7 | use Wallabag\ImportBundle\Import\PocketImport; |
7ec2897e JB |
8 | use GuzzleHttp\Client; |
9 | use GuzzleHttp\Subscriber\Mock; | |
10 | use GuzzleHttp\Message\Response; | |
11 | use GuzzleHttp\Stream\Stream; | |
252ebd60 JB |
12 | use Monolog\Logger; |
13 | use Monolog\Handler\TestHandler; | |
14 | ||
7ec2897e JB |
15 | class PocketImportTest extends \PHPUnit_Framework_TestCase |
16 | { | |
17 | protected $token; | |
18 | protected $user; | |
7ec2897e | 19 | protected $em; |
252ebd60 JB |
20 | protected $contentProxy; |
21 | protected $logHandler; | |
7ec2897e | 22 | |
ef75e122 | 23 | private function getPocketImport($consumerKey = 'ConsumerKey') |
7ec2897e JB |
24 | { |
25 | $this->user = new User(); | |
26 | ||
252ebd60 JB |
27 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') |
28 | ->disableOriginalConstructor() | |
29 | ->getMock(); | |
30 | ||
7ec2897e JB |
31 | $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') |
32 | ->disableOriginalConstructor() | |
33 | ->getMock(); | |
34 | ||
63e40f2d JB |
35 | $config = $this->getMockBuilder('Craue\ConfigBundle\Util\Config') |
36 | ->disableOriginalConstructor() | |
37 | ->getMock(); | |
38 | ||
39 | $config->expects($this->any()) | |
40 | ->method('get') | |
41 | ->with('pocket_consumer_key') | |
42 | ->willReturn($consumerKey); | |
43 | ||
02f64895 | 44 | $pocket = new PocketImport( |
7ec2897e | 45 | $this->em, |
252ebd60 | 46 | $this->contentProxy, |
ef75e122 | 47 | $config |
7ec2897e | 48 | ); |
ef75e122 | 49 | $pocket->setUser($this->user); |
252ebd60 JB |
50 | |
51 | $this->logHandler = new TestHandler(); | |
4094ea47 | 52 | $logger = new Logger('test', [$this->logHandler]); |
252ebd60 JB |
53 | $pocket->setLogger($logger); |
54 | ||
55 | return $pocket; | |
7ec2897e JB |
56 | } |
57 | ||
58 | public function testInit() | |
59 | { | |
60 | $pocketImport = $this->getPocketImport(); | |
61 | ||
62 | $this->assertEquals('Pocket', $pocketImport->getName()); | |
7019c7cf | 63 | $this->assertNotEmpty($pocketImport->getUrl()); |
0d42217e | 64 | $this->assertEquals('import.pocket.description', $pocketImport->getDescription()); |
7ec2897e JB |
65 | } |
66 | ||
67 | public function testOAuthRequest() | |
68 | { | |
69 | $client = new Client(); | |
70 | ||
71 | $mock = new Mock([ | |
252ebd60 | 72 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['code' => 'wunderbar_code']))), |
7ec2897e JB |
73 | ]); |
74 | ||
75 | $client->getEmitter()->attach($mock); | |
76 | ||
77 | $pocketImport = $this->getPocketImport(); | |
78 | $pocketImport->setClient($client); | |
79 | ||
252ebd60 | 80 | $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect'); |
7ec2897e | 81 | |
252ebd60 JB |
82 | $this->assertEquals('wunderbar_code', $code); |
83 | } | |
84 | ||
85 | public function testOAuthRequestBadResponse() | |
86 | { | |
87 | $client = new Client(); | |
88 | ||
89 | $mock = new Mock([ | |
90 | new Response(403), | |
91 | ]); | |
92 | ||
93 | $client->getEmitter()->attach($mock); | |
94 | ||
95 | $pocketImport = $this->getPocketImport(); | |
96 | $pocketImport->setClient($client); | |
97 | ||
98 | $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect'); | |
99 | ||
100 | $this->assertFalse($code); | |
101 | ||
102 | $records = $this->logHandler->getRecords(); | |
103 | $this->assertContains('PocketImport: Failed to request token', $records[0]['message']); | |
104 | $this->assertEquals('ERROR', $records[0]['level_name']); | |
7ec2897e JB |
105 | } |
106 | ||
107 | public function testOAuthAuthorize() | |
108 | { | |
109 | $client = new Client(); | |
110 | ||
111 | $mock = new Mock([ | |
252ebd60 | 112 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), |
7ec2897e JB |
113 | ]); |
114 | ||
115 | $client->getEmitter()->attach($mock); | |
116 | ||
117 | $pocketImport = $this->getPocketImport(); | |
118 | $pocketImport->setClient($client); | |
119 | ||
252ebd60 | 120 | $res = $pocketImport->authorize('wunderbar_code'); |
7ec2897e | 121 | |
252ebd60 JB |
122 | $this->assertTrue($res); |
123 | $this->assertEquals('wunderbar_token', $pocketImport->getAccessToken()); | |
7ec2897e JB |
124 | } |
125 | ||
252ebd60 JB |
126 | public function testOAuthAuthorizeBadResponse() |
127 | { | |
128 | $client = new Client(); | |
129 | ||
130 | $mock = new Mock([ | |
131 | new Response(403), | |
132 | ]); | |
133 | ||
134 | $client->getEmitter()->attach($mock); | |
135 | ||
136 | $pocketImport = $this->getPocketImport(); | |
137 | $pocketImport->setClient($client); | |
138 | ||
139 | $res = $pocketImport->authorize('wunderbar_code'); | |
140 | ||
141 | $this->assertFalse($res); | |
142 | ||
143 | $records = $this->logHandler->getRecords(); | |
144 | $this->assertContains('PocketImport: Failed to authorize client', $records[0]['message']); | |
145 | $this->assertEquals('ERROR', $records[0]['level_name']); | |
146 | } | |
147 | ||
148 | /** | |
149 | * Will sample results from https://getpocket.com/developer/docs/v3/retrieve. | |
150 | */ | |
7ec2897e JB |
151 | public function testImport() |
152 | { | |
153 | $client = new Client(); | |
154 | ||
155 | $mock = new Mock([ | |
252ebd60 JB |
156 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), |
157 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(' | |
158 | { | |
159 | "status": 1, | |
160 | "list": { | |
161 | "229279689": { | |
162 | "item_id": "229279689", | |
163 | "resolved_id": "229279689", | |
164 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | |
165 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | |
166 | "favorite": "1", | |
167 | "status": "1", | |
7f753117 JB |
168 | "time_added": "1473020899", |
169 | "time_updated": "1473020899", | |
170 | "time_read": "0", | |
171 | "time_favorited": "0", | |
172 | "sort_id": 0, | |
252ebd60 JB |
173 | "resolved_title": "The Massive Ryder Cup Preview", |
174 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | |
175 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", | |
176 | "is_article": "1", | |
7f753117 | 177 | "is_index": "0", |
252ebd60 JB |
178 | "has_video": "1", |
179 | "has_image": "1", | |
180 | "word_count": "3197", | |
181 | "images": { | |
182 | "1": { | |
183 | "item_id": "229279689", | |
184 | "image_id": "1", | |
185 | "src": "http://a.espncdn.com/combiner/i?img=/photo/2012/0927/grant_g_ryder_cr_640.jpg&w=640&h=360", | |
186 | "width": "0", | |
187 | "height": "0", | |
188 | "credit": "Jamie Squire/Getty Images", | |
189 | "caption": "" | |
190 | } | |
191 | }, | |
192 | "videos": { | |
193 | "1": { | |
194 | "item_id": "229279689", | |
195 | "video_id": "1", | |
196 | "src": "http://www.youtube.com/v/Er34PbFkVGk?version=3&hl=en_US&rel=0", | |
197 | "width": "420", | |
198 | "height": "315", | |
199 | "type": "1", | |
200 | "vid": "Er34PbFkVGk" | |
201 | } | |
202 | }, | |
203 | "tags": { | |
204 | "grantland": { | |
205 | "item_id": "1147652870", | |
206 | "tag": "grantland" | |
207 | }, | |
208 | "Ryder Cup": { | |
209 | "item_id": "1147652870", | |
210 | "tag": "Ryder Cup" | |
211 | } | |
212 | } | |
213 | }, | |
214 | "229279690": { | |
215 | "item_id": "229279689", | |
216 | "resolved_id": "229279689", | |
217 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | |
218 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | |
219 | "favorite": "1", | |
220 | "status": "1", | |
7f753117 JB |
221 | "time_added": "1473020899", |
222 | "time_updated": "1473020899", | |
223 | "time_read": "0", | |
224 | "time_favorited": "0", | |
225 | "sort_id": 1, | |
252ebd60 JB |
226 | "resolved_title": "The Massive Ryder Cup Preview", |
227 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | |
228 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", | |
229 | "is_article": "1", | |
7f753117 | 230 | "is_index": "0", |
252ebd60 JB |
231 | "has_video": "0", |
232 | "has_image": "0", | |
233 | "word_count": "3197" | |
234 | } | |
235 | } | |
236 | } | |
237 | ')), | |
238 | ]); | |
239 | ||
240 | $client->getEmitter()->attach($mock); | |
241 | ||
242 | $pocketImport = $this->getPocketImport(); | |
243 | ||
244 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
245 | ->disableOriginalConstructor() | |
246 | ->getMock(); | |
247 | ||
248 | $entryRepo->expects($this->exactly(2)) | |
78833672 | 249 | ->method('findByUrlAndUserId') |
252ebd60 JB |
250 | ->will($this->onConsecutiveCalls(false, true)); |
251 | ||
252ebd60 | 252 | $this->em |
c2656f96 | 253 | ->expects($this->exactly(2)) |
252ebd60 | 254 | ->method('getRepository') |
c2656f96 | 255 | ->willReturn($entryRepo); |
252ebd60 | 256 | |
79d0e38e | 257 | $entry = new Entry($this->user); |
252ebd60 JB |
258 | |
259 | $this->contentProxy | |
260 | ->expects($this->once()) | |
261 | ->method('updateEntry') | |
262 | ->willReturn($entry); | |
263 | ||
264 | $pocketImport->setClient($client); | |
265 | $pocketImport->authorize('wunderbar_code'); | |
266 | ||
267 | $res = $pocketImport->import(); | |
268 | ||
269 | $this->assertTrue($res); | |
270 | $this->assertEquals(['skipped' => 1, 'imported' => 1], $pocketImport->getSummary()); | |
271 | } | |
272 | ||
79d0e38e JB |
273 | /** |
274 | * Will sample results from https://getpocket.com/developer/docs/v3/retrieve. | |
275 | */ | |
276 | public function testImportAndMarkAllAsRead() | |
277 | { | |
278 | $client = new Client(); | |
279 | ||
280 | $mock = new Mock([ | |
281 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | |
282 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(' | |
283 | { | |
284 | "status": 1, | |
285 | "list": { | |
286 | "229279689": { | |
287 | "item_id": "229279689", | |
288 | "resolved_id": "229279689", | |
289 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | |
290 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | |
291 | "favorite": "1", | |
292 | "status": "1", | |
7f753117 JB |
293 | "time_added": "1473020899", |
294 | "time_updated": "1473020899", | |
295 | "time_read": "0", | |
296 | "time_favorited": "0", | |
297 | "sort_id": 0, | |
79d0e38e JB |
298 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", |
299 | "is_article": "1", | |
300 | "has_video": "1", | |
301 | "has_image": "1", | |
302 | "word_count": "3197" | |
303 | }, | |
304 | "229279690": { | |
305 | "item_id": "229279689", | |
306 | "resolved_id": "229279689", | |
307 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview/2", | |
308 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | |
309 | "favorite": "1", | |
310 | "status": "0", | |
7f753117 JB |
311 | "time_added": "1473020899", |
312 | "time_updated": "1473020899", | |
313 | "time_read": "0", | |
314 | "time_favorited": "0", | |
315 | "sort_id": 1, | |
79d0e38e JB |
316 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", |
317 | "is_article": "1", | |
318 | "has_video": "0", | |
319 | "has_image": "0", | |
320 | "word_count": "3197" | |
321 | } | |
322 | } | |
323 | } | |
324 | ')), | |
325 | ]); | |
326 | ||
327 | $client->getEmitter()->attach($mock); | |
328 | ||
329 | $pocketImport = $this->getPocketImport(); | |
330 | ||
331 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
332 | ->disableOriginalConstructor() | |
333 | ->getMock(); | |
334 | ||
335 | $entryRepo->expects($this->exactly(2)) | |
336 | ->method('findByUrlAndUserId') | |
337 | ->will($this->onConsecutiveCalls(false, false)); | |
338 | ||
339 | $this->em | |
340 | ->expects($this->exactly(2)) | |
341 | ->method('getRepository') | |
342 | ->willReturn($entryRepo); | |
343 | ||
344 | // check that every entry persisted are archived | |
345 | $this->em | |
346 | ->expects($this->any()) | |
347 | ->method('persist') | |
cebb4223 | 348 | ->with($this->callback(function ($persistedEntry) { |
79d0e38e JB |
349 | return $persistedEntry->isArchived(); |
350 | })); | |
351 | ||
352 | $entry = new Entry($this->user); | |
353 | ||
354 | $this->contentProxy | |
355 | ->expects($this->exactly(2)) | |
356 | ->method('updateEntry') | |
357 | ->willReturn($entry); | |
358 | ||
359 | $pocketImport->setClient($client); | |
360 | $pocketImport->authorize('wunderbar_code'); | |
361 | ||
362 | $res = $pocketImport->setMarkAsRead(true)->import(); | |
363 | ||
364 | $this->assertTrue($res); | |
365 | $this->assertEquals(['skipped' => 0, 'imported' => 2], $pocketImport->getSummary()); | |
366 | } | |
367 | ||
13470c35 JB |
368 | /** |
369 | * Will sample results from https://getpocket.com/developer/docs/v3/retrieve. | |
370 | */ | |
371 | public function testImportWithRabbit() | |
372 | { | |
373 | $client = new Client(); | |
374 | ||
375 | $body = <<<'JSON' | |
376 | { | |
377 | "item_id": "229279689", | |
378 | "resolved_id": "229279689", | |
379 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | |
380 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | |
381 | "favorite": "1", | |
382 | "status": "1", | |
7f753117 JB |
383 | "time_added": "1473020899", |
384 | "time_updated": "1473020899", | |
385 | "time_read": "0", | |
386 | "time_favorited": "0", | |
387 | "sort_id": 0, | |
13470c35 JB |
388 | "resolved_title": "The Massive Ryder Cup Preview", |
389 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | |
390 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", | |
391 | "is_article": "1", | |
392 | "has_video": "0", | |
393 | "has_image": "0", | |
394 | "word_count": "3197" | |
395 | } | |
396 | JSON; | |
397 | ||
398 | $mock = new Mock([ | |
399 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | |
400 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(' | |
401 | { | |
402 | "status": 1, | |
403 | "list": { | |
404 | "229279690": '.$body.' | |
405 | } | |
406 | } | |
407 | ')), | |
408 | ]); | |
409 | ||
410 | $client->getEmitter()->attach($mock); | |
411 | ||
412 | $pocketImport = $this->getPocketImport(); | |
413 | ||
414 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
415 | ->disableOriginalConstructor() | |
416 | ->getMock(); | |
417 | ||
418 | $entryRepo->expects($this->never()) | |
419 | ->method('findByUrlAndUserId'); | |
420 | ||
421 | $this->em | |
422 | ->expects($this->never()) | |
423 | ->method('getRepository'); | |
424 | ||
425 | $entry = new Entry($this->user); | |
426 | ||
427 | $this->contentProxy | |
428 | ->expects($this->never()) | |
429 | ->method('updateEntry'); | |
430 | ||
431 | $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer') | |
432 | ->disableOriginalConstructor() | |
433 | ->getMock(); | |
434 | ||
435 | $bodyAsArray = json_decode($body, true); | |
436 | // because with just use `new User()` so it doesn't have an id | |
437 | $bodyAsArray['userId'] = null; | |
438 | ||
439 | $producer | |
440 | ->expects($this->once()) | |
441 | ->method('publish') | |
442 | ->with(json_encode($bodyAsArray)); | |
443 | ||
444 | $pocketImport->setClient($client); | |
445 | $pocketImport->setRabbitmqProducer($producer); | |
446 | $pocketImport->authorize('wunderbar_code'); | |
447 | ||
448 | $res = $pocketImport->setMarkAsRead(true)->import(); | |
449 | ||
450 | $this->assertTrue($res); | |
451 | $this->assertEquals(['skipped' => 0, 'imported' => 1], $pocketImport->getSummary()); | |
452 | } | |
453 | ||
252ebd60 JB |
454 | public function testImportBadResponse() |
455 | { | |
456 | $client = new Client(); | |
457 | ||
458 | $mock = new Mock([ | |
459 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | |
460 | new Response(403), | |
7ec2897e JB |
461 | ]); |
462 | ||
463 | $client->getEmitter()->attach($mock); | |
464 | ||
465 | $pocketImport = $this->getPocketImport(); | |
466 | $pocketImport->setClient($client); | |
252ebd60 JB |
467 | $pocketImport->authorize('wunderbar_code'); |
468 | ||
469 | $res = $pocketImport->import(); | |
7ec2897e | 470 | |
252ebd60 | 471 | $this->assertFalse($res); |
7ec2897e | 472 | |
252ebd60 JB |
473 | $records = $this->logHandler->getRecords(); |
474 | $this->assertContains('PocketImport: Failed to import', $records[0]['message']); | |
475 | $this->assertEquals('ERROR', $records[0]['level_name']); | |
7ec2897e | 476 | } |
19d9efab JB |
477 | |
478 | public function testImportWithExceptionFromGraby() | |
479 | { | |
480 | $client = new Client(); | |
481 | ||
482 | $mock = new Mock([ | |
483 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | |
484 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(' | |
485 | { | |
486 | "status": 1, | |
487 | "list": { | |
488 | "229279689": { | |
489 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview" | |
490 | } | |
491 | } | |
492 | } | |
493 | ')), | |
494 | ]); | |
495 | ||
496 | $client->getEmitter()->attach($mock); | |
497 | ||
498 | $pocketImport = $this->getPocketImport(); | |
499 | ||
500 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
501 | ->disableOriginalConstructor() | |
502 | ->getMock(); | |
503 | ||
504 | $entryRepo->expects($this->once()) | |
505 | ->method('findByUrlAndUserId') | |
506 | ->will($this->onConsecutiveCalls(false, true)); | |
507 | ||
508 | $this->em | |
509 | ->expects($this->once()) | |
510 | ->method('getRepository') | |
511 | ->willReturn($entryRepo); | |
512 | ||
513 | $entry = new Entry($this->user); | |
514 | ||
515 | $this->contentProxy | |
516 | ->expects($this->once()) | |
517 | ->method('updateEntry') | |
518 | ->will($this->throwException(new \Exception())); | |
519 | ||
520 | $pocketImport->setClient($client); | |
521 | $pocketImport->authorize('wunderbar_code'); | |
522 | ||
523 | $res = $pocketImport->import(); | |
524 | ||
525 | $this->assertTrue($res); | |
526 | $this->assertEquals(['skipped' => 1, 'imported' => 0], $pocketImport->getSummary()); | |
527 | } | |
7ec2897e | 528 | } |