diff options
author | Jérémy Benoist <j0k3r@users.noreply.github.com> | 2017-10-23 11:09:17 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-23 11:09:17 +0200 |
commit | 1953a872932a63792293b4aec087880265ba89f7 (patch) | |
tree | fd16599e737fcdaf193c933ef3ec4a4ee248b117 /tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php | |
parent | d83d25dadec2c38460a32d96f5d2903426fec9d3 (diff) | |
parent | 702f2d67d60ca963492b90dad74cb5f8dcc84e51 (diff) | |
download | wallabag-1953a872932a63792293b4aec087880265ba89f7.tar.gz wallabag-1953a872932a63792293b4aec087880265ba89f7.tar.zst wallabag-1953a872932a63792293b4aec087880265ba89f7.zip |
Merge pull request #3011 from wallabag/2.3
wallabag 2.3.0
Diffstat (limited to 'tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php')
-rw-r--r-- | tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php | 89 |
1 files changed, 66 insertions, 23 deletions
diff --git a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php index 85f12d87..c61f65d0 100644 --- a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php +++ b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php | |||
@@ -2,34 +2,52 @@ | |||
2 | 2 | ||
3 | namespace Tests\Wallabag\CoreBundle\Helper; | 3 | namespace Tests\Wallabag\CoreBundle\Helper; |
4 | 4 | ||
5 | use Wallabag\CoreBundle\Helper\DownloadImages; | ||
6 | use Monolog\Logger; | ||
7 | use Monolog\Handler\TestHandler; | ||
8 | use GuzzleHttp\Client; | 5 | use GuzzleHttp\Client; |
9 | use GuzzleHttp\Subscriber\Mock; | ||
10 | use GuzzleHttp\Message\Response; | 6 | use GuzzleHttp\Message\Response; |
11 | use GuzzleHttp\Stream\Stream; | 7 | use GuzzleHttp\Stream\Stream; |
8 | use GuzzleHttp\Subscriber\Mock; | ||
9 | use Monolog\Handler\TestHandler; | ||
10 | use Monolog\Logger; | ||
11 | use Wallabag\CoreBundle\Helper\DownloadImages; | ||
12 | 12 | ||
13 | class DownloadImagesTest extends \PHPUnit_Framework_TestCase | 13 | class DownloadImagesTest extends \PHPUnit_Framework_TestCase |
14 | { | 14 | { |
15 | public function testProcessHtml() | 15 | public function dataForSuccessImage() |
16 | { | ||
17 | return [ | ||
18 | 'imgur' => [ | ||
19 | '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', | ||
20 | 'http://imgur.com/gallery/WxtWY', | ||
21 | ], | ||
22 | 'image with &' => [ | ||
23 | '<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>', | ||
24 | 'https://www.tvaddons.ag/realdebrid-kodi-jarvis/', | ||
25 | ], | ||
26 | ]; | ||
27 | } | ||
28 | |||
29 | /** | ||
30 | * @dataProvider dataForSuccessImage | ||
31 | */ | ||
32 | public function testProcessHtml($html, $url) | ||
16 | { | 33 | { |
17 | $client = new Client(); | 34 | $client = new Client(); |
18 | 35 | ||
19 | $mock = new Mock([ | 36 | $mock = new Mock([ |
20 | new Response(200, ['content-type' => 'image/png'], Stream::factory(file_get_contents(__DIR__.'/../fixtures/unnamed.png'))), | 37 | new Response(200, ['content-type' => 'image/png'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/unnamed.png'))), |
21 | ]); | 38 | ]); |
22 | 39 | ||
23 | $client->getEmitter()->attach($mock); | 40 | $client->getEmitter()->attach($mock); |
24 | 41 | ||
25 | $logHandler = new TestHandler(); | 42 | $logHandler = new TestHandler(); |
26 | $logger = new Logger('test', array($logHandler)); | 43 | $logger = new Logger('test', [$logHandler]); |
27 | 44 | ||
28 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger); | 45 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); |
29 | 46 | ||
30 | $res = $download->processHtml(123, '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', 'http://imgur.com/gallery/WxtWY'); | 47 | $res = $download->processHtml(123, $html, $url); |
31 | 48 | ||
32 | $this->assertContains('http://wallabag.io/assets/images/9/b/9b0ead26/c638b4c2.png', $res); | 49 | // this the base path of all image (since it's calculated using the entry id: 123) |
50 | $this->assertContains('http://wallabag.io/assets/images/9/b/9b0ead26/', $res); | ||
33 | } | 51 | } |
34 | 52 | ||
35 | public function testProcessHtmlWithBadImage() | 53 | public function testProcessHtmlWithBadImage() |
@@ -43,9 +61,9 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase | |||
43 | $client->getEmitter()->attach($mock); | 61 | $client->getEmitter()->attach($mock); |
44 | 62 | ||
45 | $logHandler = new TestHandler(); | 63 | $logHandler = new TestHandler(); |
46 | $logger = new Logger('test', array($logHandler)); | 64 | $logger = new Logger('test', [$logHandler]); |
47 | 65 | ||
48 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger); | 66 | $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'); | 67 | $res = $download->processHtml(123, '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', 'http://imgur.com/gallery/WxtWY'); |
50 | 68 | ||
51 | $this->assertContains('http://i.imgur.com/T9qgcHc.jpg', $res, 'Image were not replace because of content-type'); | 69 | $this->assertContains('http://i.imgur.com/T9qgcHc.jpg', $res, 'Image were not replace because of content-type'); |
@@ -69,18 +87,18 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase | |||
69 | $client = new Client(); | 87 | $client = new Client(); |
70 | 88 | ||
71 | $mock = new Mock([ | 89 | $mock = new Mock([ |
72 | new Response(200, ['content-type' => $header], Stream::factory(file_get_contents(__DIR__.'/../fixtures/unnamed.png'))), | 90 | new Response(200, ['content-type' => $header], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/unnamed.png'))), |
73 | ]); | 91 | ]); |
74 | 92 | ||
75 | $client->getEmitter()->attach($mock); | 93 | $client->getEmitter()->attach($mock); |
76 | 94 | ||
77 | $logHandler = new TestHandler(); | 95 | $logHandler = new TestHandler(); |
78 | $logger = new Logger('test', array($logHandler)); | 96 | $logger = new Logger('test', [$logHandler]); |
79 | 97 | ||
80 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger); | 98 | $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'); | 99 | $res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); |
82 | 100 | ||
83 | $this->assertContains('/assets/images/9/b/9b0ead26/ebe60399.'.$extension, $res); | 101 | $this->assertContains('/assets/images/9/b/9b0ead26/ebe60399.' . $extension, $res); |
84 | } | 102 | } |
85 | 103 | ||
86 | public function testProcessSingleImageWithBadUrl() | 104 | public function testProcessSingleImageWithBadUrl() |
@@ -94,9 +112,9 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase | |||
94 | $client->getEmitter()->attach($mock); | 112 | $client->getEmitter()->attach($mock); |
95 | 113 | ||
96 | $logHandler = new TestHandler(); | 114 | $logHandler = new TestHandler(); |
97 | $logger = new Logger('test', array($logHandler)); | 115 | $logger = new Logger('test', [$logHandler]); |
98 | 116 | ||
99 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger); | 117 | $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'); | 118 | $res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); |
101 | 119 | ||
102 | $this->assertFalse($res, 'Image can not be found, so it will not be replaced'); | 120 | $this->assertFalse($res, 'Image can not be found, so it will not be replaced'); |
@@ -113,9 +131,9 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase | |||
113 | $client->getEmitter()->attach($mock); | 131 | $client->getEmitter()->attach($mock); |
114 | 132 | ||
115 | $logHandler = new TestHandler(); | 133 | $logHandler = new TestHandler(); |
116 | $logger = new Logger('test', array($logHandler)); | 134 | $logger = new Logger('test', [$logHandler]); |
117 | 135 | ||
118 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger); | 136 | $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'); | 137 | $res = $download->processSingleImage(123, 'http://i.imgur.com/T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); |
120 | 138 | ||
121 | $this->assertFalse($res, 'Image can not be loaded, so it will not be replaced'); | 139 | $this->assertFalse($res, 'Image can not be loaded, so it will not be replaced'); |
@@ -126,17 +144,42 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase | |||
126 | $client = new Client(); | 144 | $client = new Client(); |
127 | 145 | ||
128 | $mock = new Mock([ | 146 | $mock = new Mock([ |
129 | new Response(200, ['content-type' => 'image/png'], Stream::factory(file_get_contents(__DIR__.'/../fixtures/unnamed.png'))), | 147 | new Response(200, ['content-type' => 'image/png'], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/unnamed.png'))), |
130 | ]); | 148 | ]); |
131 | 149 | ||
132 | $client->getEmitter()->attach($mock); | 150 | $client->getEmitter()->attach($mock); |
133 | 151 | ||
134 | $logHandler = new TestHandler(); | 152 | $logHandler = new TestHandler(); |
135 | $logger = new Logger('test', array($logHandler)); | 153 | $logger = new Logger('test', [$logHandler]); |
136 | 154 | ||
137 | $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', 'http://wallabag.io/', $logger); | 155 | $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'); | 156 | $res = $download->processSingleImage(123, '/i.imgur.com/T9qgcHc.jpg', 'imgur.com/gallery/WxtWY'); |
139 | 157 | ||
140 | $this->assertFalse($res, 'Absolute image can not be determined, so it will not be replaced'); | 158 | $this->assertFalse($res, 'Absolute image can not be determined, so it will not be replaced'); |
141 | } | 159 | } |
160 | |||
161 | public function testProcessRealImage() | ||
162 | { | ||
163 | $client = new Client(); | ||
164 | |||
165 | $mock = new Mock([ | ||
166 | new Response(200, ['content-type' => null], Stream::factory(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))), | ||
167 | ]); | ||
168 | |||
169 | $client->getEmitter()->attach($mock); | ||
170 | |||
171 | $logHandler = new TestHandler(); | ||
172 | $logger = new Logger('test', [$logHandler]); | ||
173 | |||
174 | $download = new DownloadImages($client, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger); | ||
175 | |||
176 | $res = $download->processSingleImage( | ||
177 | 123, | ||
178 | 'https://cdn.theconversation.com/files/157200/article/width926/gsj2rjp2-1487348607.jpg', | ||
179 | 'https://theconversation.com/conversation-avec-gerald-bronner-ce-nest-pas-la-post-verite-qui-nous-menace-mais-lextension-de-notre-credulite-73089' | ||
180 | ); | ||
181 | |||
182 | $this->assertContains('http://wallabag.io/assets/images/9/b/9b0ead26/', $res, 'Content-Type was empty but data is ok for an image'); | ||
183 | $this->assertContains('DownloadImages: Checking extension (alternative)', $logHandler->getRecords()[3]['message']); | ||
184 | } | ||
142 | } | 185 | } |