3 namespace Tests\Wallabag\CoreBundle\Helper
;
5 use GuzzleHttp\Psr7\Response
;
6 use Http\Mock\Client
as HttpMockClient
;
7 use Monolog\Handler\TestHandler
;
9 use PHPUnit\Framework\TestCase
;
10 use Wallabag\CoreBundle\Helper\DownloadImages
;
12 class DownloadImagesTest
extends TestCase
14 public function dataForSuccessImage()
18 '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>',
19 'http://imgur.com/gallery/WxtWY',
22 '<div><img src="https://i2.wp.com/www.tvaddons.ag/wp-content/uploads/2017/01/Screen-Shot-2017-01-07-at-10.17.40-PM.jpg?w=640&ssl=1" /></div>',
23 'https://www.tvaddons.ag/realdebrid-kodi-jarvis/',
29 * @dataProvider dataForSuccessImage
31 public function testProcessHtml($html, $url)
33 $httpMockClient = new HttpMockClient();
35 $httpMockClient->addResponse(new Response(200, ['content-type' => 'image/png'], file_get_contents(__DIR__
. '/../fixtures/unnamed.png')));
37 $logHandler = new TestHandler();
38 $logger = new Logger('test', [$logHandler]);
40 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
42 $res = $download->processHtml(123, $html, $url);
44 // this the base path of all image (since it's calculated using the entry id: 123)
45 $this->assertContains('http://wallabag.io/assets/images/9/b/9b0ead26/', $res);
48 public function testProcessHtmlWithBadImage()
50 $httpMockClient = new HttpMockClient();
51 $httpMockClient->addResponse(new Response(200, ['content-type' => 'application/json'], ''));
53 $logHandler = new TestHandler();
54 $logger = new Logger('test', [$logHandler]);
56 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
57 $res = $download->processHtml(123, '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', 'http://imgur.com/gallery/WxtWY');
59 $this->assertContains('http://i.imgur.com/T9qgcHc.jpg', $res, 'Image were not replace because of content-type');
62 public function singleImage()
65 ['image/pjpeg', 'jpeg'],
66 ['image/jpeg', 'jpeg'],
73 * @dataProvider singleImage
75 public function testProcessSingleImage($header, $extension)
77 $httpMockClient = new HttpMockClient();
78 $httpMockClient->addResponse(new Response(200, ['content-type' => $header], file_get_contents(__DIR__
. '/../fixtures/unnamed.png')));
80 $logHandler = new TestHandler();
81 $logger = new Logger('test', [$logHandler]);
83 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
84 $res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY');
86 $this->assertContains('/assets/images/9/b/9b0ead26/ebe60399.' . $extension, $res);
89 public function testProcessSingleImageWithBadUrl()
91 $httpMockClient = new HttpMockClient();
92 $httpMockClient->addResponse(new Response(404, []));
94 $logHandler = new TestHandler();
95 $logger = new Logger('test', [$logHandler]);
97 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
98 $res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY');
100 $this->assertFalse($res, 'Image can not be found, so it will not be replaced');
103 public function testProcessSingleImageWithBadImage()
105 $httpMockClient = new HttpMockClient();
106 $httpMockClient->addResponse(new Response(200, ['content-type' => 'image/png'], ''));
108 $logHandler = new TestHandler();
109 $logger = new Logger('test', [$logHandler]);
111 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
112 $res = $download->processSingleImage(123, 'http://i.imgur.com/T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY');
114 $this->assertFalse($res, 'Image can not be loaded, so it will not be replaced');
117 public function testProcessSingleImageFailAbsolute()
119 $httpMockClient = new HttpMockClient();
120 $httpMockClient->addResponse(new Response(200, ['content-type' => 'image/png'], file_get_contents(__DIR__
. '/../fixtures/unnamed.png')));
122 $logHandler = new TestHandler();
123 $logger = new Logger('test', [$logHandler]);
125 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
126 $res = $download->processSingleImage(123, '/i.imgur.com/T9qgcHc.jpg', 'imgur.com/gallery/WxtWY');
128 $this->assertFalse($res, 'Absolute image can not be determined, so it will not be replaced');
131 public function testProcessRealImage()
133 $httpMockClient = new HttpMockClient();
134 $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__
. '/../fixtures/image-no-content-type.jpg')));
136 $logHandler = new TestHandler();
137 $logger = new Logger('test', [$logHandler]);
139 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
141 $res = $download->processSingleImage(
143 'https://cdn.theconversation.com/files/157200/article/width926/gsj2rjp2-1487348607.jpg',
144 'https://theconversation.com/conversation-avec-gerald-bronner-ce-nest-pas-la-post-verite-qui-nous-menace-mais-lextension-de-notre-credulite-73089'
147 $this->assertContains('http://wallabag.io/assets/images/9/b/9b0ead26/', $res, 'Content-Type was empty but data is ok for an image');
148 $this->assertContains('DownloadImages: Checking extension (alternative)', $logHandler->getRecords()[3]['message']);
151 public function testProcessImageWithSrcset()
153 $httpMockClient = new HttpMockClient();
154 $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__
. '/../fixtures/image-no-content-type.jpg')));
155 $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__
. '/../fixtures/image-no-content-type.jpg')));
156 $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__
. '/../fixtures/image-no-content-type.jpg')));
158 $logHandler = new TestHandler();
159 $logger = new Logger('test', [$logHandler]);
161 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
162 $res = $download->processHtml(123, '<p><img class="alignnone wp-image-1153" src="http://piketty.blog.lemonde.fr/files/2017/10/F1FR-530x375.jpg" alt="" width="628" height="444" srcset="http://piketty.blog.lemonde.fr/files/2017/10/F1FR-530x375.jpg 530w, http://piketty.blog.lemonde.fr/files/2017/10/F1FR-768x543.jpg 768w, http://piketty.blog.lemonde.fr/files/2017/10/F1FR-900x636.jpg 900w" sizes="(max-width: 628px) 100vw, 628px" /></p>', 'http://piketty.blog.lemonde.fr/2017/10/12/budget-2018-la-jeunesse-sacrifiee/');
164 $this->assertNotContains('http://piketty.blog.lemonde.fr/', $res, 'Image srcset attribute were not replaced');
167 public function testProcessImageWithTrickySrcset()
169 $httpMockClient = new HttpMockClient();
170 $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__
. '/../fixtures/image-no-content-type.jpg')));
171 $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__
. '/../fixtures/image-no-content-type.jpg')));
172 $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__
. '/../fixtures/image-no-content-type.jpg')));
174 $logHandler = new TestHandler();
175 $logger = new Logger('test', [$logHandler]);
177 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
178 $res = $download->processHtml(123, '<figure id="post-257260" class="align-none media-257260"><img src="https://cdn.css-tricks.com/wp-content/uploads/2017/08/the-critical-request.png" srcset="https://res.cloudinary.com/css-tricks/image/upload/c_scale,w_1000,f_auto,q_auto/v1501594717/the-critical-request_bqdfaa.png 1000w, https://res.cloudinary.com/css-tricks/image/upload/c_scale,w_200,f_auto,q_auto/v1501594717/the-critical-request_bqdfaa.png 200w" sizes="(min-width: 1850px) calc( (100vw - 555px) / 3 )
179 (min-width: 1251px) calc( (100vw - 530px) / 2 )
180 (min-width: 1086px) calc(100vw - 480px)
181 (min-width: 626px) calc(100vw - 335px)
182 calc(100vw - 30px)" alt="" /></figure>', 'https://css-tricks.com/the-critical-request/');
184 $this->assertNotContains('f_auto,q_auto', $res, 'Image srcset attribute were not replaced');
187 public function testProcessImageWithNullPath()
189 $httpMockClient = new HttpMockClient();
190 $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__
. '/../fixtures/image-no-content-type.jpg')));
192 $logHandler = new TestHandler();
193 $logger = new Logger('test', [$logHandler]);
195 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
197 $res = $download->processSingleImage(
200 'https://framablog.org/2018/06/30/engagement-atypique/'
202 $this->assertFalse($res);