mirror of
https://github.com/shlinkio/shlink.git
synced 2025-12-10 22:13:47 -06:00
Test RedirectConditionType::isValid() in isolation
This commit is contained in:
parent
223901324f
commit
c3d3cc6288
@ -16,6 +16,28 @@ const LOOSE_URI_MATCHER = '/(.+)\:(.+)/i'; // Matches anything starting with a s
|
|||||||
const IP_ADDRESS_REQUEST_ATTRIBUTE = 'remote_address';
|
const IP_ADDRESS_REQUEST_ATTRIBUTE = 'remote_address';
|
||||||
const REDIRECT_URL_REQUEST_ATTRIBUTE = 'redirect_url';
|
const REDIRECT_URL_REQUEST_ATTRIBUTE = 'redirect_url';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List of ISO 3166-1 alpha-2 two-letter country codes https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
|
||||||
|
*/
|
||||||
|
const ISO_COUNTRY_CODES = [
|
||||||
|
'AF', 'AX', 'AL', 'DZ', 'AS', 'AD', 'AO', 'AI', 'AQ', 'AG', 'AR', 'AM', 'AW', 'AU', 'AT', 'AZ',
|
||||||
|
'BS', 'BH', 'BD', 'BB', 'BY', 'BE', 'BZ', 'BJ', 'BM', 'BT', 'BO', 'BQ', 'BA', 'BW', 'BV', 'BR',
|
||||||
|
'IO', 'BN', 'BG', 'BF', 'BI', 'CV', 'KH', 'CM', 'CA', 'KY', 'CF', 'TD', 'CL', 'CN', 'CX', 'CC',
|
||||||
|
'CO', 'KM', 'CG', 'CD', 'CK', 'CR', 'CI', 'HR', 'CU', 'CW', 'CY', 'CZ', 'DK', 'DJ', 'DM', 'DO',
|
||||||
|
'EC', 'EG', 'SV', 'GQ', 'ER', 'EE', 'SZ', 'ET', 'FK', 'FO', 'FJ', 'FI', 'FR', 'GF', 'PF', 'TF',
|
||||||
|
'GA', 'GM', 'GE', 'DE', 'GH', 'GI', 'GR', 'GL', 'GD', 'GP', 'GU', 'GT', 'GG', 'GN', 'GW', 'GY',
|
||||||
|
'HT', 'HM', 'VA', 'HN', 'HK', 'HU', 'IS', 'IN', 'ID', 'IR', 'IQ', 'IE', 'IM', 'IL', 'IT', 'JM',
|
||||||
|
'JP', 'JE', 'JO', 'KZ', 'KE', 'KI', 'KP', 'KR', 'KW', 'KG', 'LA', 'LV', 'LB', 'LS', 'LR', 'LY',
|
||||||
|
'LI', 'LT', 'LU', 'MO', 'MG', 'MW', 'MY', 'MV', 'ML', 'MT', 'MH', 'MQ', 'MR', 'MU', 'YT', 'MX',
|
||||||
|
'FM', 'MD', 'MC', 'MN', 'ME', 'MS', 'MA', 'MZ', 'MM', 'NA', 'NR', 'NP', 'NL', 'NC', 'NZ', 'NI',
|
||||||
|
'NE', 'NG', 'NU', 'NF', 'MK', 'MP', 'NO', 'OM', 'PK', 'PW', 'PS', 'PA', 'PG', 'PY', 'PE', 'PH',
|
||||||
|
'PN', 'PL', 'PT', 'PR', 'QA', 'RE', 'RO', 'RU', 'RW', 'BL', 'SH', 'KN', 'LC', 'MF', 'PM', 'VC',
|
||||||
|
'WS', 'SM', 'ST', 'SA', 'SN', 'RS', 'SC', 'SL', 'SG', 'SX', 'SK', 'SI', 'SB', 'SO', 'ZA', 'GS',
|
||||||
|
'SS', 'ES', 'LK', 'SD', 'SR', 'SJ', 'SE', 'CH', 'SY', 'TW', 'TJ', 'TZ', 'TH', 'TL', 'TG', 'TK',
|
||||||
|
'TO', 'TT', 'TN', 'TR', 'TM', 'TC', 'TV', 'UG', 'UA', 'AE', 'GB', 'US', 'UM', 'UY', 'UZ', 'VU',
|
||||||
|
'VE', 'VN', 'VG', 'VI', 'WF', 'EH', 'YE', 'ZM', 'ZW',
|
||||||
|
];
|
||||||
|
|
||||||
/** @deprecated */
|
/** @deprecated */
|
||||||
const DEFAULT_QR_CODE_SIZE = 300;
|
const DEFAULT_QR_CODE_SIZE = 300;
|
||||||
/** @deprecated */
|
/** @deprecated */
|
||||||
|
|||||||
@ -8,6 +8,8 @@ use Shlinkio\Shlink\Core\Util\IpAddressUtils;
|
|||||||
use function Shlinkio\Shlink\Core\ArrayUtils\contains;
|
use function Shlinkio\Shlink\Core\ArrayUtils\contains;
|
||||||
use function Shlinkio\Shlink\Core\enumValues;
|
use function Shlinkio\Shlink\Core\enumValues;
|
||||||
|
|
||||||
|
use const Shlinkio\Shlink\ISO_COUNTRY_CODES;
|
||||||
|
|
||||||
enum RedirectConditionType: string
|
enum RedirectConditionType: string
|
||||||
{
|
{
|
||||||
case DEVICE = 'device';
|
case DEVICE = 'device';
|
||||||
@ -28,26 +30,10 @@ enum RedirectConditionType: string
|
|||||||
RedirectConditionType::DEVICE => contains($value, enumValues(DeviceType::class)),
|
RedirectConditionType::DEVICE => contains($value, enumValues(DeviceType::class)),
|
||||||
// RedirectConditionType::LANGUAGE => TODO Validate at least format,
|
// RedirectConditionType::LANGUAGE => TODO Validate at least format,
|
||||||
RedirectConditionType::IP_ADDRESS => IpAddressUtils::isStaticIpCidrOrWildcard($value),
|
RedirectConditionType::IP_ADDRESS => IpAddressUtils::isStaticIpCidrOrWildcard($value),
|
||||||
RedirectConditionType::GEOLOCATION_COUNTRY_CODE => contains($value, [
|
RedirectConditionType::GEOLOCATION_COUNTRY_CODE => contains($value, ISO_COUNTRY_CODES),
|
||||||
// List of ISO 3166-1 alpha-2 two-letter country codes https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
|
RedirectConditionType::QUERY_PARAM,
|
||||||
'AF', 'AX', 'AL', 'DZ', 'AS', 'AD', 'AO', 'AI', 'AQ', 'AG', 'AR', 'AM', 'AW', 'AU', 'AT', 'AZ',
|
RedirectConditionType::ANY_VALUE_QUERY_PARAM,
|
||||||
'BS', 'BH', 'BD', 'BB', 'BY', 'BE', 'BZ', 'BJ', 'BM', 'BT', 'BO', 'BQ', 'BA', 'BW', 'BV', 'BR',
|
RedirectConditionType::VALUELESS_QUERY_PARAM => $value !== '',
|
||||||
'IO', 'BN', 'BG', 'BF', 'BI', 'CV', 'KH', 'CM', 'CA', 'KY', 'CF', 'TD', 'CL', 'CN', 'CX', 'CC',
|
|
||||||
'CO', 'KM', 'CG', 'CD', 'CK', 'CR', 'CI', 'HR', 'CU', 'CW', 'CY', 'CZ', 'DK', 'DJ', 'DM', 'DO',
|
|
||||||
'EC', 'EG', 'SV', 'GQ', 'ER', 'EE', 'SZ', 'ET', 'FK', 'FO', 'FJ', 'FI', 'FR', 'GF', 'PF', 'TF',
|
|
||||||
'GA', 'GM', 'GE', 'DE', 'GH', 'GI', 'GR', 'GL', 'GD', 'GP', 'GU', 'GT', 'GG', 'GN', 'GW', 'GY',
|
|
||||||
'HT', 'HM', 'VA', 'HN', 'HK', 'HU', 'IS', 'IN', 'ID', 'IR', 'IQ', 'IE', 'IM', 'IL', 'IT', 'JM',
|
|
||||||
'JP', 'JE', 'JO', 'KZ', 'KE', 'KI', 'KP', 'KR', 'KW', 'KG', 'LA', 'LV', 'LB', 'LS', 'LR', 'LY',
|
|
||||||
'LI', 'LT', 'LU', 'MO', 'MG', 'MW', 'MY', 'MV', 'ML', 'MT', 'MH', 'MQ', 'MR', 'MU', 'YT', 'MX',
|
|
||||||
'FM', 'MD', 'MC', 'MN', 'ME', 'MS', 'MA', 'MZ', 'MM', 'NA', 'NR', 'NP', 'NL', 'NC', 'NZ', 'NI',
|
|
||||||
'NE', 'NG', 'NU', 'NF', 'MK', 'MP', 'NO', 'OM', 'PK', 'PW', 'PS', 'PA', 'PG', 'PY', 'PE', 'PH',
|
|
||||||
'PN', 'PL', 'PT', 'PR', 'QA', 'RE', 'RO', 'RU', 'RW', 'BL', 'SH', 'KN', 'LC', 'MF', 'PM', 'VC',
|
|
||||||
'WS', 'SM', 'ST', 'SA', 'SN', 'RS', 'SC', 'SL', 'SG', 'SX', 'SK', 'SI', 'SB', 'SO', 'ZA', 'GS',
|
|
||||||
'SS', 'ES', 'LK', 'SD', 'SR', 'SJ', 'SE', 'CH', 'SY', 'TW', 'TJ', 'TZ', 'TH', 'TL', 'TG', 'TK',
|
|
||||||
'TO', 'TT', 'TN', 'TR', 'TM', 'TC', 'TV', 'UG', 'UA', 'AE', 'GB', 'US', 'UM', 'UY', 'UZ', 'VU',
|
|
||||||
'VE', 'VN', 'VG', 'VI', 'WF', 'EH', 'YE', 'ZM', 'ZW',
|
|
||||||
]),
|
|
||||||
RedirectConditionType::ANY_VALUE_QUERY_PARAM, RedirectConditionType::VALUELESS_QUERY_PARAM => $value !== '',
|
|
||||||
// FIXME We should at least validate the value is not empty
|
// FIXME We should at least validate the value is not empty
|
||||||
// default => $value !== '',
|
// default => $value !== '',
|
||||||
default => true,
|
default => true,
|
||||||
|
|||||||
@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace ShlinkioTest\Shlink\Core\RedirectRule\Model;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\Attributes\Test;
|
||||||
|
use PHPUnit\Framework\Attributes\TestWith;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Shlinkio\Shlink\Core\RedirectRule\Model\RedirectConditionType;
|
||||||
|
|
||||||
|
class RedirectConditionTypeTest extends TestCase
|
||||||
|
{
|
||||||
|
#[Test]
|
||||||
|
#[TestWith([RedirectConditionType::QUERY_PARAM, '', false])]
|
||||||
|
#[TestWith([RedirectConditionType::QUERY_PARAM, 'foo', true])]
|
||||||
|
#[TestWith([RedirectConditionType::ANY_VALUE_QUERY_PARAM, '', false])]
|
||||||
|
#[TestWith([RedirectConditionType::ANY_VALUE_QUERY_PARAM, 'foo', true])]
|
||||||
|
#[TestWith([RedirectConditionType::VALUELESS_QUERY_PARAM, '', false])]
|
||||||
|
#[TestWith([RedirectConditionType::VALUELESS_QUERY_PARAM, 'foo', true])]
|
||||||
|
public function isValidFailsForEmptyQueryParams(
|
||||||
|
RedirectConditionType $conditionType,
|
||||||
|
string $value,
|
||||||
|
bool $expectedIsValid,
|
||||||
|
): void {
|
||||||
|
self::assertEquals($expectedIsValid, $conditionType->isValid($value));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user