]>
Commit | Line | Data |
---|---|---|
7f559418 JB |
1 | <?php |
2 | ||
3 | namespace Tests\Wallabag\CoreBundle\Helper; | |
4 | ||
5 | use Wallabag\CoreBundle\Helper\DownloadImages; | |
7f559418 JB |
6 | use Monolog\Logger; |
7 | use Monolog\Handler\TestHandler; | |
8 | use GuzzleHttp\Client; | |
9 | use GuzzleHttp\Subscriber\Mock; | |
10 | use GuzzleHttp\Message\Response; | |
11 | use GuzzleHttp\Stream\Stream; | |
12 | ||
13 | class DownloadImagesTest extends \PHPUnit_Framework_TestCase | |
14 | { | |
15 | public function testProcessHtml() | |
16 | { | |
17 | $client = new Client(); | |
18 | ||
19 | $mock = new Mock([ | |
20 | new Response(200, ['content-type' => 'image/png'], Stream::factory(file_get_contents(__DIR__.'/../fixtures/unnamed.png'))), | |
21 | ]); | |
22 | ||
23 | $client->getEmitter()->attach($mock); | |
24 | ||
25 | $logHandler = new TestHandler(); | |
26 | $logger = new Logger('test', array($logHandler)); | |
27 | ||
e0597476 | 28 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger); |
41ada277 | 29 | |
e0597476 | 30 | $res = $download->processHtml(123, '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', 'http://imgur.com/gallery/WxtWY'); |
7f559418 | 31 | |
e0597476 | 32 | $this->assertContains('http://wallabag.io/assets/images/9/b/9b0ead26/c638b4c2.png', $res); |
7f559418 JB |
33 | } |
34 | ||
35 | public function testProcessHtmlWithBadImage() | |
36 | { | |
37 | $client = new Client(); | |
38 | ||
39 | $mock = new Mock([ | |
40 | new Response(200, ['content-type' => 'application/json'], Stream::factory('')), | |
41 | ]); | |
42 | ||
43 | $client->getEmitter()->attach($mock); | |
44 | ||
45 | $logHandler = new TestHandler(); | |
46 | $logger = new Logger('test', array($logHandler)); | |
47 | ||
e0597476 JB |
48 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger); |
49 | $res = $download->processHtml(123, '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', 'http://imgur.com/gallery/WxtWY'); | |
7f559418 JB |
50 | |
51 | $this->assertContains('http://i.imgur.com/T9qgcHc.jpg', $res, 'Image were not replace because of content-type'); | |
52 | } | |
53 | ||
54 | public function singleImage() | |
55 | { | |
56 | return [ | |
57 | ['image/pjpeg', 'jpeg'], | |
58 | ['image/jpeg', 'jpeg'], | |
59 | ['image/png', 'png'], | |
60 | ['image/gif', 'gif'], | |
61 | ]; | |
62 | } | |
63 | ||
64 | /** | |
65 | * @dataProvider singleImage | |
66 | */ | |
67 | public function testProcessSingleImage($header, $extension) | |
68 | { | |
69 | $client = new Client(); | |
70 | ||
71 | $mock = new Mock([ | |
72 | new Response(200, ['content-type' => $header], Stream::factory(file_get_contents(__DIR__.'/../fixtures/unnamed.png'))), | |
73 | ]); | |
74 | ||
75 | $client->getEmitter()->attach($mock); | |
76 | ||
77 | $logHandler = new TestHandler(); | |
78 | $logger = new Logger('test', array($logHandler)); | |
79 | ||
e0597476 JB |
80 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger); |
81 | $res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); | |
7f559418 | 82 | |
e0597476 | 83 | $this->assertContains('/assets/images/9/b/9b0ead26/ebe60399.'.$extension, $res); |
7f559418 JB |
84 | } |
85 | ||
48656e0e JB |
86 | public function testProcessSingleImageWithBadUrl() |
87 | { | |
88 | $client = new Client(); | |
89 | ||
90 | $mock = new Mock([ | |
91 | new Response(404, []), | |
92 | ]); | |
93 | ||
94 | $client->getEmitter()->attach($mock); | |
95 | ||
96 | $logHandler = new TestHandler(); | |
97 | $logger = new Logger('test', array($logHandler)); | |
98 | ||
e0597476 JB |
99 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger); |
100 | $res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); | |
48656e0e JB |
101 | |
102 | $this->assertFalse($res, 'Image can not be found, so it will not be replaced'); | |
103 | } | |
104 | ||
7f559418 JB |
105 | public function testProcessSingleImageWithBadImage() |
106 | { | |
107 | $client = new Client(); | |
108 | ||
109 | $mock = new Mock([ | |
110 | new Response(200, ['content-type' => 'image/png'], Stream::factory('')), | |
111 | ]); | |
112 | ||
113 | $client->getEmitter()->attach($mock); | |
114 | ||
115 | $logHandler = new TestHandler(); | |
116 | $logger = new Logger('test', array($logHandler)); | |
117 | ||
e0597476 JB |
118 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger); |
119 | $res = $download->processSingleImage(123, 'http://i.imgur.com/T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); | |
7f559418 JB |
120 | |
121 | $this->assertFalse($res, 'Image can not be loaded, so it will not be replaced'); | |
122 | } | |
123 | ||
124 | public function testProcessSingleImageFailAbsolute() | |
125 | { | |
126 | $client = new Client(); | |
127 | ||
128 | $mock = new Mock([ | |
129 | new Response(200, ['content-type' => 'image/png'], Stream::factory(file_get_contents(__DIR__.'/../fixtures/unnamed.png'))), | |
130 | ]); | |
131 | ||
132 | $client->getEmitter()->attach($mock); | |
133 | ||
134 | $logHandler = new TestHandler(); | |
135 | $logger = new Logger('test', array($logHandler)); | |
136 | ||
e0597476 JB |
137 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger); |
138 | $res = $download->processSingleImage(123, '/i.imgur.com/T9qgcHc.jpg', 'imgur.com/gallery/WxtWY'); | |
7f559418 JB |
139 | |
140 | $this->assertFalse($res, 'Absolute image can not be determined, so it will not be replaced'); | |
141 | } | |
142 | } |