1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
|
<?php
declare(strict_types=1);
namespace WebThumbnailer\Finder;
use WebThumbnailer\Exception\BadRulesException;
use WebThumbnailer\TestCase;
use WebThumbnailer\Utils\DataUtils;
use WebThumbnailer\Utils\FileUtils;
class QueryRegexFinderTest extends TestCase
{
/**
* @var mixed[] Finder rules.
*/
protected static $rules;
/**
* PHP builtin local server URL.
*/
protected const LOCAL_SERVER = 'http://localhost:8081/';
/**
* Before every tests, reset rules and params.
*/
public function setUp(): void
{
self::$rules = [
'image_regex' => '<img class="thumb" src="(.*?)" alt="(.*?)">',
'thumbnail_url' => 'https://domain.tld/pics/${1}?name=${2}',
];
}
/**
* Test find() with a valid thumb found.
*/
public function testQueryRegexFinderValid(): void
{
$url = __DIR__ . '/../resources/queryregex/one-thumb.html';
$expected = 'https://domain.tld/pics/thumb.png?name=text';
$finder = new QueryRegexFinder('domain.tld', $url, self::$rules, null);
$this->assertEquals($expected, $finder->find());
}
/**
* Test find() with 2 valid thumbs matching the regex, we use the first one.
*/
public function testQueryRegexFinderTwoThumbs(): void
{
$url = __DIR__ . '/../resources/queryregex/two-thumb.html';
$expected = 'https://domain.tld/pics/thumb.png?name=text';
$finder = new QueryRegexFinder('domain.tld', $url, self::$rules, null);
$this->assertEquals($expected, $finder->find());
}
/**
* Test find() with parameter.
*/
public function testQueryRegexFinderWithParameter(): void
{
$url = __DIR__ . '/../resources/queryregex/one-thumb.html';
$expected = 'https://domain.tld/pics/thumb.png?param=foobar-other';
self::$rules['thumbnail_url'] = 'https://domain.tld/pics/${1}?param=${option1}-${option2}';
$params = [
'option1' => [
'default' => 'name',
'name' => [
'param' => 'foobar',
]
],
'option2' => [
'default' => 'name',
'name' => [
'param' => 'other',
]
]
];
$finder = new QueryRegexFinder('domain.tld', $url, self::$rules, $params);
$this->assertEquals($expected, $finder->find());
}
/**
* Test find() with a valid thumb found.
*/
public function testQueryRegexFinderCurlValid(): void
{
$url = self::LOCAL_SERVER . 'queryregex/one-thumb.html';
$expected = 'https://domain.tld/pics/thumb.png?name=text';
$finder = new QueryRegexFinder('domain.tld', $url, self::$rules, null);
$this->assertEquals($expected, $finder->find());
}
/**
* Test find() with 2 valid thumbs matching the regex, we use the first one.
*/
public function testQueryRegexFinderCurlTwoThumbs(): void
{
$url = self::LOCAL_SERVER . 'queryregex/two-thumb.html';
$expected = 'https://domain.tld/pics/thumb.png?name=text';
$finder = new QueryRegexFinder('domain.tld', $url, self::$rules, null);
$this->assertEquals($expected, $finder->find());
}
/**
* Test find() with parameter.
*/
public function testQueryRegexFinderCurlWithParameter(): void
{
$url = self::LOCAL_SERVER . 'queryregex/one-thumb.html';
$expected = 'https://domain.tld/pics/thumb.png?param=foobar-other';
self::$rules['thumbnail_url'] = 'https://domain.tld/pics/${1}?param=${option1}-${option2}';
$params = [
'option1' => [
'default' => 'name',
'name' => [
'param' => 'foobar',
]
],
'option2' => [
'default' => 'name',
'name' => [
'param' => 'other',
]
]
];
$finder = new QueryRegexFinder('domain.tld', $url, self::$rules, $params);
$this->assertEquals($expected, $finder->find());
}
/**
* Test the default finder trying to find an image mime-type.
*/
public function testQueryRegexFinderImageMimetype(): void
{
$url = self::LOCAL_SERVER . 'default/image-mimetype.php';
$finder = new QueryRegexFinder('domain.tld', $url, self::$rules, null);
$this->assertFalse($finder->find());
}
/**
* Test the default finder finding a non 200 status code.
*/
public function testQueryRegexFinderStatusError(): void
{
$url = self::LOCAL_SERVER . 'default/status-ko.php';
$finder = new QueryRegexFinder('domain.tld', $url, self::$rules, null);
$this->assertFalse($finder->find());
}
/**
* Test getName().
*/
public function testGetName(): void
{
$rules = [
'image_regex' => 'foo',
'thumbnail_url' => 'bar',
];
$finder = new QueryRegexFinder('', '', $rules, []);
$this->assertEquals('Query Regex', $finder->getName());
}
/**
* Test loading the finder with bad rules (`thumbnail_url`).
*/
public function testQueryRegexFinderBadRulesThumbUrl(): void
{
$this->expectException(BadRulesException::class);
unset(self::$rules['thumbnail_url']);
new QueryRegexFinder('domain.tld', '', self::$rules, null);
}
/**
* Test loading the finder with bad rules (`image_regex`).
*/
public function testQueryRegexFinderBadRulesImageRegex(): void
{
$this->expectException(BadRulesException::class);
unset(self::$rules['image_regex']);
new QueryRegexFinder('domain.tld', '', self::$rules, null);
}
/**
* Test downloading an inaccessible remote content (empty content).
*/
public function testQueryRegexFinderResourceNotReachable(): void
{
$finder = new QueryRegexFinder('domain.tld', '', self::$rules, null);
$this->assertFalse($finder->find());
}
/**
* A page without thumbnails, return false.
*/
public function testQueryRegexFinderNoMatch(): void
{
$url = __DIR__ . '/../resources/queryregex/no-thumb.html';
$finder = new QueryRegexFinder('domain.tld', $url, self::$rules, null);
$this->assertFalse($finder->find());
}
/**
* Not matching placeholder are ignored.
*/
public function testQueryRegexNoEnoughMatch(): void
{
$url = __DIR__ . '/../resources/queryregex/one-thumb.html';
$expected = 'thumb.png text ${3}';
self::$rules['thumbnail_url'] = '${1} ${2} ${3}';
$finder = new QueryRegexFinder('domain.tld', $url, self::$rules, null);
$this->assertEquals($expected, $finder->find());
}
/**
* Use an unknown option in the URL.
*/
public function testQueryRegexUnknownOption(): void
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Unknown option "option" for the finder "Query Regex"');
$url = __DIR__ . '/../resources/queryregex/one-thumb.html';
self::$rules['thumbnail_url'] = '${option}';
$finder = new QueryRegexFinder('domain.tld', $url, self::$rules, null);
$finder->find();
}
/**
* Test Giphy.
*/
public function testQueryRegexGiphy(): void
{
$this->markTestSkipped('Test case depends on non-DFSG resource that was dropped.');
$expected = 'https://media.giphy.com/media/8JQqAqsxNDUXu/giphy-facebook_s.jpg';
$allRules = DataUtils::loadJson(FileUtils::RESOURCES_PATH . 'rules.json');
$rules = $allRules['giphy']['rules'];
$options = $allRules['giphy']['options'];
$url = __DIR__ . '/../resources/giphy/giphy-gif.html';
$finder = new QueryRegexFinder('domain.tld', $url, $rules, $options);
$this->assertEquals($expected, $finder->find());
}
/**
* Test Imgur Album: multiple images on a single page, we take the first (OpenGraph choice).
*/
public function testQueryRegexImgurAlbum(): void
{
$this->markTestSkipped('Test case depends on non-DFSG resource that was dropped.');
$expected = 'https://i.imgur.com/iQxE4BHm.jpg';
$allRules = DataUtils::loadJson(FileUtils::RESOURCES_PATH . 'rules.json');
$rules = $allRules['imgur_album']['rules'];
$options = $allRules['imgur_album']['options'];
$url = __DIR__ . '/../resources/imgur/imgur-album.html';
$finder = new QueryRegexFinder('domain.tld', $url, $rules, $options);
$this->assertEquals($expected, $finder->find());
}
/**
* Test Imgur Gallery: multiple images on a single page, we take the first (OpenGraph choice).
* The difference between albums (/a/) and galleries (/gallery/), is that
* a gallery has been published to the community and includes votes and comments.
*/
public function testQueryRegexImgurGallery(): void
{
$this->markTestSkipped('Test case depends on non-DFSG resource that was dropped.');
$expected = 'https://i.imgur.com/iQxE4BHm.jpg';
$allRules = DataUtils::loadJson(FileUtils::RESOURCES_PATH . 'rules.json');
$rules = $allRules['imgur_album']['rules'];
$options = $allRules['imgur_album']['options'];
$url = __DIR__ . '/../resources/imgur/imgur-gallery.html';
$finder = new QueryRegexFinder('domain.tld', $url, $rules, $options);
$this->assertEquals($expected, $finder->find());
}
/**
* Test Instagram thumb: one picture
*/
public function testQueryRegexInstagramPicture(): void
{
$this->markTestSkipped('Test case depends on non-DFSG resource that was dropped.');
$expected = 'https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-15/e35/'
. '14719286_1129421600429160_916728922148700160_n.jpg'
. '?_nc_ht=scontent-cdg2-1.cdninstagram.com'
. '&_nc_cat=100&_nc_ohc=xWaFFBqAj6wAX_gqYWt&tp=1&oh=dd77c7c72429d2db9ca3666f01c60e60&oe=605B2EDA';
$allRules = DataUtils::loadJson(FileUtils::RESOURCES_PATH . 'rules.json');
$rules = $allRules['instagram']['rules'];
$options = $allRules['instagram']['options'];
$url = __DIR__ . '/../resources/instagram/instagram-picture.html';
$finder = new QueryRegexFinder('domain.tld', $url, $rules, $options);
$this->assertEquals($expected, $finder->find());
}
/**
* Test Instagram thumb: profile, get the avatar
*/
public function testQueryRegexInstagramProfile(): void
{
$this->markTestSkipped('Test case depends on non-DFSG resource that was dropped.');
$expected = 'https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/s150x150/'
. '11351823_506089142881765_717664936_a.jpg'
. '?_nc_ht=scontent-cdg2-1.cdninstagram.com'
. '&_nc_ohc=US3NCxc_VOcAX-WwNIl&tp=1&oh=2bb367a88e579c411c7c484fcc6b1e3e&oe=605C8165';
$allRules = DataUtils::loadJson(FileUtils::RESOURCES_PATH . 'rules.json');
$rules = $allRules['instagram']['rules'];
$options = $allRules['instagram']['options'];
$url = __DIR__ . '/../resources/instagram/instagram-profile.html';
$finder = new QueryRegexFinder('domain.tld', $url, $rules, $options);
$this->assertEquals($expected, $finder->find());
}
/**
* Test Pinterest thumb: single picture
*/
public function testQueryRegexPinterestPicture(): void
{
$this->markTestSkipped('Test case depends on non-DFSG resource that was dropped.');
$expected = 'https://s-media-cache-ak0.pinimg.com/600x315/e0/7d/c0/e07dc09f93e12170fae7caa09329d815.jpg';
$allRules = DataUtils::loadJson(FileUtils::RESOURCES_PATH . 'rules.json');
$rules = $allRules['pinterest']['rules'];
$options = $allRules['pinterest']['options'];
$url = __DIR__ . '/../resources/pinterest/pinterest-picture.html';
$finder = new QueryRegexFinder('domain.tld', $url, $rules, $options);
$this->assertEquals($expected, $finder->find());
}
/**
* Test Pinterest thumb: profile picture
*/
public function testQueryRegexPinterestProfile(): void
{
$this->markTestSkipped('Test case depends on non-DFSG resource that was dropped.');
$expected = 'https://s-media-cache-ak0.pinimg.com/avatars/sjoshua1_1367516806_140.jpg';
$allRules = DataUtils::loadJson(FileUtils::RESOURCES_PATH . 'rules.json');
$rules = $allRules['pinterest']['rules'];
$options = $allRules['pinterest']['options'];
$url = __DIR__ . '/../resources/pinterest/pinterest-profile.html';
$finder = new QueryRegexFinder('domain.tld', $url, $rules, $options);
$this->assertEquals($expected, $finder->find());
}
/**
* Test The Oatmeal comic.
*/
public function testQueryRegexTheOatmealComic(): void
{
$this->markTestSkipped('Test case depends on non-DFSG resource that was dropped.');
$expected = 'http://s3.amazonaws.com/theoatmeal-img/thumbnails/unhappy_big.png';
$allRules = DataUtils::loadJson(FileUtils::RESOURCES_PATH . 'rules.json');
$rules = $allRules['theoatmeal']['rules'];
$options = $allRules['theoatmeal']['options'];
$url = __DIR__ . '/../resources/theoatmeal/theoatmeal-comic.html';
$finder = new QueryRegexFinder('domain.tld', $url, $rules, $options);
$this->assertEquals($expected, $finder->find());
}
/**
* Test Twitter rules: no media, should use the avatar.
*/
public function testQueryRegexTwitterNoMedia(): void
{
$this->markTestSkipped('Test case depends on non-DFSG resource that was dropped.');
$expected = 'https://pbs.twimg.com/profile_images/737009192758870016/I_p72JBK_400x400.jpg';
$allRules = DataUtils::loadJson(FileUtils::RESOURCES_PATH . 'rules.json');
$rules = $allRules['twitter']['rules'];
$options = $allRules['twitter']['options'];
$url = __DIR__ . '/../resources/twitter/twitter-no-media.html';
$finder = new QueryRegexFinder('domain.tld', $url, $rules, $options);
$this->assertEquals($expected, $finder->find());
}
/**
* Test Twitter rules: one media, should use it.
*/
public function testQueryRegexTwitterOneMedia(): void
{
$this->markTestSkipped('Test case depends on non-DFSG resource that was dropped.');
$expected = 'https://pbs.twimg.com/media/CvilUtwWgAAQ46n.jpg:large';
$allRules = DataUtils::loadJson(FileUtils::RESOURCES_PATH . 'rules.json');
$rules = $allRules['twitter']['rules'];
$options = $allRules['twitter']['options'];
$url = __DIR__ . '/../resources/twitter/twitter-single-media.html';
$finder = new QueryRegexFinder('domain.tld', $url, $rules, $options);
$this->assertEquals($expected, $finder->find());
}
/**
* Test Twitter rules: multiple medias, should use the first one.
*/
public function testQueryRegexTwitterMultipleMedia(): void
{
$this->markTestSkipped('Test case depends on non-DFSG resource that was dropped.');
$expected = 'https://pbs.twimg.com/media/CuKCNVBVUAU332-.jpg:large';
$allRules = DataUtils::loadJson(FileUtils::RESOURCES_PATH . 'rules.json');
$rules = $allRules['twitter']['rules'];
$options = $allRules['twitter']['options'];
$url = __DIR__ . '/../resources/twitter/twitter-multiple-media.html';
$finder = new QueryRegexFinder('domain.tld', $url, $rules, $options);
$this->assertEquals($expected, $finder->find());
}
/**
* Test Youtube profile page: use the avatar.
*/
public function testQueryRegexYoutubeProfile(): void
{
$this->markTestSkipped('Test case depends on non-DFSG resource that was dropped.');
$expected = 'https://yt3.ggpht.com/-KLL2Lp8Zqso/AAAAAAAAAAI/AAAAAAAAAAA/Y0qd6h5C_jQ/'
. 's900-c-k-no-mo-rj-c0xffffff/photo.jpg';
$allRules = DataUtils::loadJson(FileUtils::RESOURCES_PATH . 'rules.json');
$rules = $allRules['youtube_profile']['rules'];
$options = $allRules['youtube_profile']['options'];
$url = __DIR__ . '/../resources/youtube/youtube-profile.html';
$finder = new QueryRegexFinder('domain.tld', $url, $rules, $options);
$this->assertEquals($expected, $finder->find());
}
/**
* Test XKCD comic.
*/
public function testQueryRegexXkcdComic(): void
{
$this->markTestSkipped('Test case depends on non-DFSG resource that was dropped.');
$expected = '//imgs.xkcd.com/comics/movie_folder.png';
$allRules = DataUtils::loadJson(FileUtils::RESOURCES_PATH . 'rules.json');
$rules = $allRules['xkcd']['rules'];
$options = $allRules['xkcd']['options'];
$url = __DIR__ . '/../resources/xkcd/xkcd-comic.html';
$finder = new QueryRegexFinder('domain.tld', $url, $rules, $options);
$this->assertEquals($expected, $finder->find());
}
}
|