]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php
Merge pull request #3984 from wallabag/2.4
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Helper / DownloadImagesTest.php
CommitLineData
7f559418
JB
1<?php
2
3namespace Tests\Wallabag\CoreBundle\Helper;
4
448d99f8 5use GuzzleHttp\Psr7\Response;
bf9ace06 6use Http\Mock\Client as HttpMockClient;
f808b016
JB
7use Monolog\Handler\TestHandler;
8use Monolog\Logger;
bd91bd5c 9use PHPUnit\Framework\TestCase;
f808b016 10use Wallabag\CoreBundle\Helper\DownloadImages;
7f559418 11
bd91bd5c 12class DownloadImagesTest extends TestCase
7f559418 13{
fcad69a4
JB
14 public function dataForSuccessImage()
15 {
16 return [
17 'imgur' => [
18 '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>',
19 'http://imgur.com/gallery/WxtWY',
20 ],
21 'image with &' => [
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&amp;ssl=1" /></div>',
23 'https://www.tvaddons.ag/realdebrid-kodi-jarvis/',
24 ],
25 ];
26 }
27
28 /**
29 * @dataProvider dataForSuccessImage
30 */
31 public function testProcessHtml($html, $url)
7f559418 32 {
bf9ace06 33 $httpMockClient = new HttpMockClient();
7f559418 34
bf9ace06 35 $httpMockClient->addResponse(new Response(200, ['content-type' => 'image/png'], file_get_contents(__DIR__ . '/../fixtures/unnamed.png')));
7f559418
JB
36
37 $logHandler = new TestHandler();
f808b016 38 $logger = new Logger('test', [$logHandler]);
7f559418 39
bf9ace06 40 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
41ada277 41
fcad69a4 42 $res = $download->processHtml(123, $html, $url);
7f559418 43
fcad69a4
JB
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);
7f559418
JB
46 }
47
48 public function testProcessHtmlWithBadImage()
49 {
bf9ace06 50 $httpMockClient = new HttpMockClient();
51 $httpMockClient->addResponse(new Response(200, ['content-type' => 'application/json'], ''));
7f559418
JB
52
53 $logHandler = new TestHandler();
f808b016 54 $logger = new Logger('test', [$logHandler]);
7f559418 55
bf9ace06 56 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
e0597476 57 $res = $download->processHtml(123, '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', 'http://imgur.com/gallery/WxtWY');
7f559418
JB
58
59 $this->assertContains('http://i.imgur.com/T9qgcHc.jpg', $res, 'Image were not replace because of content-type');
60 }
61
62 public function singleImage()
63 {
64 return [
65 ['image/pjpeg', 'jpeg'],
66 ['image/jpeg', 'jpeg'],
67 ['image/png', 'png'],
68 ['image/gif', 'gif'],
69 ];
70 }
71
72 /**
73 * @dataProvider singleImage
74 */
75 public function testProcessSingleImage($header, $extension)
76 {
bf9ace06 77 $httpMockClient = new HttpMockClient();
78 $httpMockClient->addResponse(new Response(200, ['content-type' => $header], file_get_contents(__DIR__ . '/../fixtures/unnamed.png')));
7f559418
JB
79
80 $logHandler = new TestHandler();
f808b016 81 $logger = new Logger('test', [$logHandler]);
7f559418 82
bf9ace06 83 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
e0597476 84 $res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY');
7f559418 85
f808b016 86 $this->assertContains('/assets/images/9/b/9b0ead26/ebe60399.' . $extension, $res);
7f559418
JB
87 }
88
48656e0e
JB
89 public function testProcessSingleImageWithBadUrl()
90 {
bf9ace06 91 $httpMockClient = new HttpMockClient();
92 $httpMockClient->addResponse(new Response(404, []));
48656e0e
JB
93
94 $logHandler = new TestHandler();
f808b016 95 $logger = new Logger('test', [$logHandler]);
48656e0e 96
bf9ace06 97 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
e0597476 98 $res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY');
48656e0e
JB
99
100 $this->assertFalse($res, 'Image can not be found, so it will not be replaced');
101 }
102
7f559418
JB
103 public function testProcessSingleImageWithBadImage()
104 {
bf9ace06 105 $httpMockClient = new HttpMockClient();
106 $httpMockClient->addResponse(new Response(200, ['content-type' => 'image/png'], ''));
7f559418
JB
107
108 $logHandler = new TestHandler();
f808b016 109 $logger = new Logger('test', [$logHandler]);
7f559418 110
bf9ace06 111 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
e0597476 112 $res = $download->processSingleImage(123, 'http://i.imgur.com/T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY');
7f559418
JB
113
114 $this->assertFalse($res, 'Image can not be loaded, so it will not be replaced');
115 }
116
117 public function testProcessSingleImageFailAbsolute()
118 {
bf9ace06 119 $httpMockClient = new HttpMockClient();
120 $httpMockClient->addResponse(new Response(200, ['content-type' => 'image/png'], file_get_contents(__DIR__ . '/../fixtures/unnamed.png')));
7f559418
JB
121
122 $logHandler = new TestHandler();
f808b016 123 $logger = new Logger('test', [$logHandler]);
7f559418 124
bf9ace06 125 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
e0597476 126 $res = $download->processSingleImage(123, '/i.imgur.com/T9qgcHc.jpg', 'imgur.com/gallery/WxtWY');
7f559418
JB
127
128 $this->assertFalse($res, 'Absolute image can not be determined, so it will not be replaced');
129 }
577c0b6d
JB
130
131 public function testProcessRealImage()
132 {
bf9ace06 133 $httpMockClient = new HttpMockClient();
134 $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg')));
577c0b6d
JB
135
136 $logHandler = new TestHandler();
f808b016 137 $logger = new Logger('test', [$logHandler]);
577c0b6d 138
bf9ace06 139 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
577c0b6d
JB
140
141 $res = $download->processSingleImage(
142 123,
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'
145 );
146
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']);
149 }
c15bb5ad
S
150
151 public function testProcessImageWithSrcset()
152 {
b6c1e1ba
JB
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')));
c15bb5ad
S
157
158 $logHandler = new TestHandler();
159 $logger = new Logger('test', [$logHandler]);
160
b6c1e1ba 161 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
c15bb5ad
S
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/');
163
164 $this->assertNotContains('http://piketty.blog.lemonde.fr/', $res, 'Image srcset attribute were not replaced');
165 }
3fbbe0d9 166
e6f12c07
S
167 public function testProcessImageWithTrickySrcset()
168 {
b6c1e1ba
JB
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')));
e6f12c07
S
173
174 $logHandler = new TestHandler();
175 $logger = new Logger('test', [$logHandler]);
176
b6c1e1ba 177 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
e6f12c07
S
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/');
183
184 $this->assertNotContains('f_auto,q_auto', $res, 'Image srcset attribute were not replaced');
185 }
186
3fbbe0d9
S
187 public function testProcessImageWithNullPath()
188 {
b6c1e1ba
JB
189 $httpMockClient = new HttpMockClient();
190 $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg')));
3fbbe0d9
S
191
192 $logHandler = new TestHandler();
193 $logger = new Logger('test', [$logHandler]);
194
b6c1e1ba 195 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
3fbbe0d9
S
196
197 $res = $download->processSingleImage(
198 123,
199 null,
200 'https://framablog.org/2018/06/30/engagement-atypique/'
201 );
202 $this->assertFalse($res);
203 }
7f559418 204}