|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Icon validator tests. |
| 4 | + * |
| 5 | + * @package Pronamic\Pay\Icons |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Pronamic\Pay\Icons\Tests; |
| 9 | + |
| 10 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | +use Pronamic\Pay\Icons\IconValidator; |
| 13 | + |
| 14 | +/** |
| 15 | + * Tests the icon validator. |
| 16 | + */ |
| 17 | +final class IconValidatorTest extends TestCase { |
| 18 | + /** |
| 19 | + * Test slug validation. |
| 20 | + * |
| 21 | + * @param string $slug Icon slug. |
| 22 | + * @param bool $expected Expected result. |
| 23 | + */ |
| 24 | + #[DataProvider( 'slug_provider' )] |
| 25 | + public function test_slug_validation( string $slug, bool $expected ): void { |
| 26 | + $this->assertSame( $expected, IconValidator::is_valid_slug( $slug ) ); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Slug provider. |
| 31 | + * |
| 32 | + * @return array<string, array{string, bool}> |
| 33 | + */ |
| 34 | + public static function slug_provider(): array { |
| 35 | + return [ |
| 36 | + 'single letter' => [ 'a', true ], |
| 37 | + 'single digit' => [ '0', true ], |
| 38 | + 'hyphenated' => [ 'apple-pay', true ], |
| 39 | + 'repeated hyphen' => [ 'apple--pay', true ], |
| 40 | + 'empty' => [ '', false ], |
| 41 | + 'leading hyphen' => [ '-apple', false ], |
| 42 | + 'trailing hyphen' => [ 'apple-', false ], |
| 43 | + 'uppercase' => [ 'Apple', false ], |
| 44 | + 'underscore' => [ 'apple_pay', false ], |
| 45 | + 'slash' => [ 'apple/pay', false ], |
| 46 | + 'non-ASCII character' => [ 'äpple', false ], |
| 47 | + ]; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Test asset path validation. |
| 52 | + * |
| 53 | + * @param string $path Asset path. |
| 54 | + * @param bool $expected Expected result. |
| 55 | + */ |
| 56 | + #[DataProvider( 'asset_path_provider' )] |
| 57 | + public function test_asset_path_validation( string $path, bool $expected ): void { |
| 58 | + $this->assertSame( $expected, IconValidator::is_valid_asset_path( $path ) ); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Asset path provider. |
| 63 | + * |
| 64 | + * @return array<string, array{string, bool}> |
| 65 | + */ |
| 66 | + public static function asset_path_provider(): array { |
| 67 | + return [ |
| 68 | + 'valid' => [ 'methods/apple-pay/method-apple-pay-640x360.svg', true ], |
| 69 | + 'valid variant' => [ 'methods/ideal-in3/method-ideal-in3-on-white-640x360.svg', true ], |
| 70 | + 'valid asset hyphen' => [ 'methods/a/method---640x360.svg', true ], |
| 71 | + 'empty asset name' => [ 'methods/a/method--640x360.svg', false ], |
| 72 | + 'empty method slug' => [ 'methods//method-apple-pay-640x360.svg', false ], |
| 73 | + 'trailing slug hyphen' => [ 'methods/apple-/method-apple-640x360.svg', false ], |
| 74 | + 'wrong directory' => [ 'providers/apple-pay/method-apple-pay-640x360.svg', false ], |
| 75 | + 'extra path segment' => [ 'methods/apple-pay/icons/method-apple-pay-640x360.svg', false ], |
| 76 | + 'uppercase asset name' => [ 'methods/apple-pay/method-Apple-Pay-640x360.svg', false ], |
| 77 | + 'unsupported character' => [ 'methods/apple-pay/method-apple_pay-640x360.svg', false ], |
| 78 | + 'wrong filename prefix' => [ 'methods/apple-pay/icon-apple-pay-640x360.svg', false ], |
| 79 | + 'wrong dimensions' => [ 'methods/apple-pay/method-apple-pay-320x180.svg', false ], |
| 80 | + 'wrong extension' => [ 'methods/apple-pay/method-apple-pay-640x360.png', false ], |
| 81 | + 'extension suffix' => [ 'methods/apple-pay/method-apple-pay-640x360.svg.php', false ], |
| 82 | + 'query string' => [ 'methods/apple-pay/method-apple-pay-640x360.svg?x=1', false ], |
| 83 | + 'traversal' => [ 'methods/apple-pay/../method-apple-pay-640x360.svg', false ], |
| 84 | + ]; |
| 85 | + } |
| 86 | +} |
0 commit comments