]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php
Fix tests after rebase
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Helper / DownloadImagesTest.php
1 <?php
2
3 namespace Tests\Wallabag\CoreBundle\Helper;
4
5 use GuzzleHttp\Psr7\Response;
6 use Http\Mock\Client as HttpMockClient;
7 use Monolog\Handler\TestHandler;
8 use Monolog\Logger;
9 use PHPUnit\Framework\TestCase;
10 use Wallabag\CoreBundle\Helper\DownloadImages;
11
12 class DownloadImagesTest extends TestCase
13 {
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)
32 {
33 $httpMockClient = new HttpMockClient();
34
35 $httpMockClient->addResponse(new Response(200, ['content-type' => 'image/png'], file_get_contents(__DIR__ . '/../fixtures/unnamed.png')));
36
37 $logHandler = new TestHandler();
38 $logger = new Logger('test', [$logHandler]);
39
40 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
41
42 $res = $download->processHtml(123, $html, $url);
43
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);
46 }
47
48 public function testProcessHtmlWithBadImage()
49 {
50 $httpMockClient = new HttpMockClient();
51 $httpMockClient->addResponse(new Response(200, ['content-type' => 'application/json'], ''));
52
53 $logHandler = new TestHandler();
54 $logger = new Logger('test', [$logHandler]);
55
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');
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 {
77 $httpMockClient = new HttpMockClient();
78 $httpMockClient->addResponse(new Response(200, ['content-type' => $header], file_get_contents(__DIR__ . '/../fixtures/unnamed.png')));
79
80 $logHandler = new TestHandler();
81 $logger = new Logger('test', [$logHandler]);
82
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');
85
86 $this->assertContains('/assets/images/9/b/9b0ead26/ebe60399.' . $extension, $res);
87 }
88
89 public function testProcessSingleImageWithBadUrl()
90 {
91 $httpMockClient = new HttpMockClient();
92 $httpMockClient->addResponse(new Response(404, []));
93
94 $logHandler = new TestHandler();
95 $logger = new Logger('test', [$logHandler]);
96
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');
99
100 $this->assertFalse($res, 'Image can not be found, so it will not be replaced');
101 }
102
103 public function testProcessSingleImageWithBadImage()
104 {
105 $httpMockClient = new HttpMockClient();
106 $httpMockClient->addResponse(new Response(200, ['content-type' => 'image/png'], ''));
107
108 $logHandler = new TestHandler();
109 $logger = new Logger('test', [$logHandler]);
110
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');
113
114 $this->assertFalse($res, 'Image can not be loaded, so it will not be replaced');
115 }
116
117 public function testProcessSingleImageFailAbsolute()
118 {
119 $httpMockClient = new HttpMockClient();
120 $httpMockClient->addResponse(new Response(200, ['content-type' => 'image/png'], file_get_contents(__DIR__ . '/../fixtures/unnamed.png')));
121
122 $logHandler = new TestHandler();
123 $logger = new Logger('test', [$logHandler]);
124
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');
127
128 $this->assertFalse($res, 'Absolute image can not be determined, so it will not be replaced');
129 }
130
131 public function testProcessRealImage()
132 {
133 $httpMockClient = new HttpMockClient();
134 $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg')));
135
136 $logHandler = new TestHandler();
137 $logger = new Logger('test', [$logHandler]);
138
139 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
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 }
150
151 public function testProcessImageWithSrcset()
152 {
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')));
157
158 $logHandler = new TestHandler();
159 $logger = new Logger('test', [$logHandler]);
160
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/');
163
164 $this->assertNotContains('http://piketty.blog.lemonde.fr/', $res, 'Image srcset attribute were not replaced');
165 }
166
167 public function testProcessImageWithTrickySrcset()
168 {
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')));
173
174 $logHandler = new TestHandler();
175 $logger = new Logger('test', [$logHandler]);
176
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/');
183
184 $this->assertNotContains('f_auto,q_auto', $res, 'Image srcset attribute were not replaced');
185 }
186
187 public function testProcessImageWithNullPath()
188 {
189 $httpMockClient = new HttpMockClient();
190 $httpMockClient->addResponse(new Response(200, ['content-type' => null], file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg')));
191
192 $logHandler = new TestHandler();
193 $logger = new Logger('test', [$logHandler]);
194
195 $download = new DownloadImages($httpMockClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
196
197 $res = $download->processSingleImage(
198 123,
199 null,
200 'https://framablog.org/2018/06/30/engagement-atypique/'
201 );
202 $this->assertFalse($res);
203 }
204 }