]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php
CS
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Helper / DownloadImagesTest.php
CommitLineData
7f559418
JB
1<?php
2
3namespace Tests\Wallabag\CoreBundle\Helper;
4
7f559418 5use GuzzleHttp\Client;
448d99f8 6use GuzzleHttp\Psr7\Response;
7f559418 7use GuzzleHttp\Stream\Stream;
f808b016 8use GuzzleHttp\Subscriber\Mock;
bf9ace06 9use Http\Mock\Client as HttpMockClient;
f808b016
JB
10use Monolog\Handler\TestHandler;
11use Monolog\Logger;
bd91bd5c 12use PHPUnit\Framework\TestCase;
f808b016 13use Wallabag\CoreBundle\Helper\DownloadImages;
7f559418 14
bd91bd5c 15class DownloadImagesTest extends TestCase
7f559418 16{
fcad69a4
JB
17 public function dataForSuccessImage()
18 {
19 return [
20 'imgur' => [
21 '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>',
22 'http://imgur.com/gallery/WxtWY',
23 ],
24 'image with &' => [
25 '<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&amp;ssl=1" /></div>',
26 'https://www.tvaddons.ag/realdebrid-kodi-jarvis/',
27 ],
28 ];
29 }
30
31 /**
32 * @dataProvider dataForSuccessImage
33 */
34 public function testProcessHtml($html, $url)
7f559418 35 {
bf9ace06 36 $httpMockClient = new HttpMockClient();
7f559418 37
bf9ace06 38 $httpMockClient->addResponse(new Response(200, ['content-type' => 'image/png'], file_get_contents(__DIR__ . '/../fixtures/unnamed.png')));
7f559418
JB
39
40 $logHandler = new TestHandler();
f808b016 41 $logger = new Logger('test', [$logHandler]);
7f559418 42
bf9ace06 43 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
41ada277 44
fcad69a4 45 $res = $download->processHtml(123, $html, $url);
7f559418 46
fcad69a4
JB
47 // this the base path of all image (since it's calculated using the entry id: 123)
48 $this->assertContains('http://wallabag.io/assets/images/9/b/9b0ead26/', $res);
7f559418
JB
49 }
50
51 public function testProcessHtmlWithBadImage()
52 {
bf9ace06 53 $httpMockClient = new HttpMockClient();
54 $httpMockClient->addResponse(new Response(200, ['content-type' => 'application/json'], ''));
7f559418
JB
55
56 $logHandler = new TestHandler();
f808b016 57 $logger = new Logger('test', [$logHandler]);
7f559418 58
bf9ace06 59 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
e0597476 60 $res = $download->processHtml(123, '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', 'http://imgur.com/gallery/WxtWY');
7f559418
JB
61
62 $this->assertContains('http://i.imgur.com/T9qgcHc.jpg', $res, 'Image were not replace because of content-type');
63 }
64
65 public function singleImage()
66 {
67 return [
68 ['image/pjpeg', 'jpeg'],
69 ['image/jpeg', 'jpeg'],
70 ['image/png', 'png'],
71 ['image/gif', 'gif'],
72 ];
73 }
74
75 /**
76 * @dataProvider singleImage
77 */
78 public function testProcessSingleImage($header, $extension)
79 {
bf9ace06 80 $httpMockClient = new HttpMockClient();
81 $httpMockClient->addResponse(new Response(200, ['content-type' => $header], file_get_contents(__DIR__ . '/../fixtures/unnamed.png')));
7f559418
JB
82
83 $logHandler = new TestHandler();
f808b016 84 $logger = new Logger('test', [$logHandler]);
7f559418 85
bf9ace06 86 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
e0597476 87 $res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY');
7f559418 88
f808b016 89 $this->assertContains('/assets/images/9/b/9b0ead26/ebe60399.' . $extension, $res);
7f559418
JB
90 }
91
48656e0e
JB
92 public function testProcessSingleImageWithBadUrl()
93 {
bf9ace06 94 $httpMockClient = new HttpMockClient();
95 $httpMockClient->addResponse(new Response(404, []));
48656e0e
JB
96
97 $logHandler = new TestHandler();
f808b016 98 $logger = new Logger('test', [$logHandler]);
48656e0e 99
bf9ace06 100 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
e0597476 101 $res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY');
48656e0e
JB
102
103 $this->assertFalse($res, 'Image can not be found, so it will not be replaced');
104 }
105
7f559418
JB
106 public function testProcessSingleImageWithBadImage()
107 {
bf9ace06 108 $httpMockClient = new HttpMockClient();
109 $httpMockClient->addResponse(new Response(200, ['content-type' => 'image/png'], ''));
7f559418
JB
110
111 $logHandler = new TestHandler();
f808b016 112 $logger = new Logger('test', [$logHandler]);
7f559418 113
bf9ace06 114 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
e0597476 115 $res = $download->processSingleImage(123, 'http://i.imgur.com/T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY');
7f559418
JB
116
117 $this->assertFalse($res, 'Image can not be loaded, so it will not be replaced');
118 }
119
120 public function testProcessSingleImageFailAbsolute()
121 {
bf9ace06 122 $httpMockClient = new HttpMockClient();
123 $httpMockClient->addResponse(new Response(200, ['content-type' => 'image/png'], file_get_contents(__DIR__ . '/../fixtures/unnamed.png')));
7f559418
JB
124
125 $logHandler = new TestHandler();
f808b016 126 $logger = new Logger('test', [$logHandler]);
7f559418 127
bf9ace06 128 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
e0597476 129 $res = $download->processSingleImage(123, '/i.imgur.com/T9qgcHc.jpg', 'imgur.com/gallery/WxtWY');
7f559418
JB
130
131 $this->assertFalse($res, 'Absolute image can not be determined, so it will not be replaced');
132 }
577c0b6d
JB
133
134 public function testProcessRealImage()
135 {
bf9ace06 136 $httpMockClient = new HttpMockClient();
137 $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg')));
577c0b6d
JB
138
139 $logHandler = new TestHandler();
f808b016 140 $logger = new Logger('test', [$logHandler]);
577c0b6d 141
bf9ace06 142 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
577c0b6d
JB
143
144 $res = $download->processSingleImage(
145 123,
146 'https://cdn.theconversation.com/files/157200/article/width926/gsj2rjp2-1487348607.jpg',
147 'https://theconversation.com/conversation-avec-gerald-bronner-ce-nest-pas-la-post-verite-qui-nous-menace-mais-lextension-de-notre-credulite-73089'
148 );
149
150 $this->assertContains('http://wallabag.io/assets/images/9/b/9b0ead26/', $res, 'Content-Type was empty but data is ok for an image');
151 $this->assertContains('DownloadImages: Checking extension (alternative)', $logHandler->getRecords()[3]['message']);
152 }
c15bb5ad
S
153
154 public function testProcessImageWithSrcset()
155 {
156 $client = new Client();
157
158 $mock = new Mock([
159 new Response(200, ['content-type' => 'image/jpeg'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))),
160 new Response(200, ['content-type' => 'image/jpeg'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))),
161 new Response(200, ['content-type' => 'image/jpeg'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))),
162 ]);
163
164 $client->getEmitter()->attach($mock);
165
166 $logHandler = new TestHandler();
167 $logger = new Logger('test', [$logHandler]);
168
169 $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
170 $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/');
171
172 $this->assertNotContains('http://piketty.blog.lemonde.fr/', $res, 'Image srcset attribute were not replaced');
173 }
3fbbe0d9 174
e6f12c07
S
175 public function testProcessImageWithTrickySrcset()
176 {
177 $client = new Client();
178
179 $mock = new Mock([
180 new Response(200, ['content-type' => 'image/jpeg'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))),
181 new Response(200, ['content-type' => 'image/jpeg'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))),
182 new Response(200, ['content-type' => 'image/jpeg'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))),
183 ]);
184
185 $client->getEmitter()->attach($mock);
186
187 $logHandler = new TestHandler();
188 $logger = new Logger('test', [$logHandler]);
189
190 $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
191 $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 )
192 (min-width: 1251px) calc( (100vw - 530px) / 2 )
193 (min-width: 1086px) calc(100vw - 480px)
194 (min-width: 626px) calc(100vw - 335px)
195 calc(100vw - 30px)" alt="" /></figure>', 'https://css-tricks.com/the-critical-request/');
196
197 $this->assertNotContains('f_auto,q_auto', $res, 'Image srcset attribute were not replaced');
198 }
199
3fbbe0d9
S
200 public function testProcessImageWithNullPath()
201 {
202 $client = new Client();
203
204 $mock = new Mock([
205 new Response(200, ['content-type' => null], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))),
206 ]);
207
208 $client->getEmitter()->attach($mock);
209
210 $logHandler = new TestHandler();
211 $logger = new Logger('test', [$logHandler]);
212
213 $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
214
215 $res = $download->processSingleImage(
216 123,
217 null,
218 'https://framablog.org/2018/06/30/engagement-atypique/'
219 );
220 $this->assertFalse($res);
221 }
7f559418 222}