| 1 |
<?php |
| 2 |
|
| 3 |
require __DIR__ . '/../Support/isolated-options.php'; |
| 4 |
|
| 5 |
sa_with_isolated_options(static function () { |
| 6 |
$key = SimpleAnalytics\SettingName::EXCLUDED_IP_ADDRESSES; |
| 7 |
$field = new SimpleAnalytics\Settings\Blocks\Fields\IpList($key, 'IP addresses'); |
| 8 |
$sanitize = $field->getValueSanitizer(); |
| 9 |
register_setting('simpleanalytics-ignore-rules', $key, [ |
| 10 |
'type' => $field->getValueType(), |
| 11 |
'sanitize_callback' => $sanitize, |
| 12 |
]); |
| 13 |
|
| 14 |
delete_option($key); |
| 15 |
update_option($key, "203.0.113.10\n2001:db8::1"); |
| 16 |
$expected = ['203.0.113.10', '2001:db8::1']; |
| 17 |
sa_assert(get_option($key) === $expected, 'First save must retain addresses after double sanitization.'); |
| 18 |
sa_assert($sanitize($expected) === $expected, 'Sanitizing an array must preserve valid addresses.'); |
| 19 |
sa_assert($sanitize($sanitize($expected)) === $expected, 'Sanitization must be idempotent.'); |
| 20 |
sa_assert($sanitize([' 203.0.113.10 ', '203.0.113.10', 'invalid', [], null, new stdClass()]) === ['203.0.113.10'], 'Reject malformed items and remove duplicates.'); |
| 21 |
foreach ([null, false, 123, new stdClass(), ''] as $invalid) { |
| 22 |
sa_assert($sanitize($invalid) === [], 'Malformed or empty input must produce an empty list.'); |
| 23 |
} |
| 24 |
update_option($key, "203.0.113.20\r\ninvalid\r\n203.0.113.20"); |
| 25 |
sa_assert(get_option($key) === ['203.0.113.20'], 'Existing options must still accept textarea updates.'); |
| 26 |
}); |
| 27 |
|