PluginProbe
Simple Analytics / 1.110
Simple Analytics v1.110
1.110 1.109 1.108 1.107 1.106 1.73 1.74 1.75 1.76 1.77 1.78 1.79 1.8 1.80 1.81 1.82 1.83 1.84 1.85 1.86 1.87 1.88 1.89 1.9 1.90 All 99 releases
simpleanalytics / tests / Regression / ip-sanitization.php

ip-sanitization.php in Simple Analytics 1.110, at tests/Regression/ip-sanitization.php

27 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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