| | 1 | <?php |
| | 2 | /** |
| | 3 | * Tests_Functions_Anonymization tests. |
| | 4 | * |
| | 5 | * @package WordPress |
| | 6 | */ |
| | 7 | |
| | 8 | /** |
| | 9 | * Test anonymization functions. |
| | 10 | * |
| | 11 | * @group functions.php |
| | 12 | * @group privacy |
| | 13 | */ |
| | 14 | class Tests_Functions_Anonymization extends WP_UnitTestCase { |
| | 15 | /** |
| | 16 | * Test that wp_privacy_anonymize_ip() properly anonymizes all possible IP address formats. |
| | 17 | * |
| | 18 | * @dataProvider data_wp_privacy_anonymize_ip |
| | 19 | * |
| | 20 | * @ticket 41083 |
| | 21 | * @ticket 43545 |
| | 22 | * |
| | 23 | * @param string $raw_ip Raw IP address. |
| | 24 | * @param string $expected_result Expected result. |
| | 25 | */ |
| | 26 | public function test_wp_privacy_anonymize_ip( $raw_ip, $expected_result ) { |
| | 27 | if ( ! function_exists( 'inet_ntop' ) || ! function_exists( 'inet_pton' ) ) { |
| | 28 | $this->markTestSkipped( 'This test requires both the inet_ntop() and inet_pton() functions.' ); |
| | 29 | } |
| | 30 | |
| | 31 | $actual_result = wp_privacy_anonymize_data( 'ip', $raw_ip ); |
| | 32 | |
| | 33 | /* Todo test ipv6_fallback mode if keeping it.*/ |
| | 34 | |
| | 35 | $this->assertEquals( $expected_result, $actual_result ); |
| | 36 | } |
| | 37 | |
| | 38 | /** |
| | 39 | * Provide test cases for `test_wp_privacy_anonymize_ip()`. |
| | 40 | * |
| | 41 | * @since 5.0.0 Moved from `Test_WP_Community_Events::data_get_unsafe_client_ip_anonymization()`. |
| | 42 | * |
| | 43 | * @return array { |
| | 44 | * @type array { |
| | 45 | * @string string $raw_ip Raw IP address. |
| | 46 | * @string string $expected_result Expected result. |
| | 47 | * } |
| | 48 | * } |
| | 49 | */ |
| | 50 | public function data_wp_privacy_anonymize_ip() { |
| | 51 | return array( |
| | 52 | // Invalid IP. |
| | 53 | array( |
| | 54 | null, |
| | 55 | '0.0.0.0', |
| | 56 | ), |
| | 57 | array( |
| | 58 | '', |
| | 59 | '0.0.0.0', |
| | 60 | ), |
| | 61 | array( |
| | 62 | '0.0.0.0.0', |
| | 63 | '0.0.0.0', |
| | 64 | ), |
| | 65 | array( |
| | 66 | '0000:0000:0000:0000:0000:0000:0127:2258', |
| | 67 | '::', |
| | 68 | ), |
| | 69 | /** |
| | 70 | * Invalid IP. Unrecognized address for inet_pton() and inet_ntop(), that would |
| | 71 | * throw a PHP warning for PHP < 7.1, but should be handled here to give '::'. |
| | 72 | */ |
| | 73 | array( |
| | 74 | ':::', |
| | 75 | '::', |
| | 76 | ), |
| | 77 | // Invalid IP. Sometimes proxies add things like this, or other arbitrary strings. |
| | 78 | array( |
| | 79 | 'unknown', |
| | 80 | '0.0.0.0', |
| | 81 | ), |
| | 82 | // Invalid IP. Sometimes proxies add things like this, or other arbitrary strings. |
| | 83 | array( |
| | 84 | 'or=\"[1000:0000:0000:0000:0000:0000:0000:0001', |
| | 85 | '::', |
| | 86 | ), |
| | 87 | // Invalid IP. Sometimes proxies add things like this, or other arbitrary strings. |
| | 88 | array( |
| | 89 | 'or=\"1000:0000:0000:0000:0000:0000:0000:0001', |
| | 90 | '::', |
| | 91 | ), |
| | 92 | // Invalid IP. Sometimes proxies add things like this, or other arbitrary strings. |
| | 93 | array( |
| | 94 | '1000:0000:0000:0000:0000:0000:0000:0001or=\"', |
| | 95 | '::', |
| | 96 | ), |
| | 97 | // Malformed string with valid IP substring. Sometimes proxies add things like this, or other arbitrary strings. |
| | 98 | array( |
| | 99 | 'or=\"[1000:0000:0000:0000:0000:0000:0000:0001]:400', |
| | 100 | '1000::', |
| | 101 | ), |
| | 102 | // Malformed string with valid IP substring. Sometimes proxies add things like this, or other arbitrary strings. |
| | 103 | array( |
| | 104 | 'or=\"[1000:0000:0000:0000:0000:0000:0000:0001]', |
| | 105 | '1000::', |
| | 106 | ), |
| | 107 | // Malformed string with valid IP substring. Sometimes proxies add things like this, or other arbitrary strings. |
| | 108 | array( |
| | 109 | 'or=\"[1000:0000:0000:0000:0000:0000:0000:0001]400', |
| | 110 | '1000::', |
| | 111 | ), |
| | 112 | // Malformed string with valid IP substring. Sometimes proxies add things like this, or other arbitrary strings. |
| | 113 | array( |
| | 114 | '[1000:0000:0000:0000:0000:0000:0000:0001]:235\"or=', |
| | 115 | '1000::', |
| | 116 | ), |
| | 117 | // IPv4, no port. |
| | 118 | array( |
| | 119 | '10.20.30.45', |
| | 120 | '10.20.30.0', |
| | 121 | ), |
| | 122 | // IPv4, port. |
| | 123 | array( |
| | 124 | '10.20.30.45:20000', |
| | 125 | '10.20.30.0', |
| | 126 | ), |
| | 127 | // IPv4, netmask. |
| | 128 | array( |
| | 129 | '10.20.30.45/24', |
| | 130 | '10.20.30.0', |
| | 131 | ), |
| | 132 | // IPv6, no port. |
| | 133 | array( |
| | 134 | '2a03:2880:2110:df07:face:b00c::1', |
| | 135 | '2a03:2880:2110:df07::', |
| | 136 | ), |
| | 137 | // IPv6, port. |
| | 138 | array( |
| | 139 | '[2a03:2880:2110:df07:face:b00c::1]:20000', |
| | 140 | '2a03:2880:2110:df07::', |
| | 141 | ), |
| | 142 | // IPv6, no port, reducible representation. |
| | 143 | array( |
| | 144 | '0000:0000:0000:0000:0000:0000:0000:0001', |
| | 145 | '::', |
| | 146 | ), |
| | 147 | // IPv6, no port, partially reducible representation. |
| | 148 | array( |
| | 149 | '1000:0000:0000:0000:0000:0000:0000:0001', |
| | 150 | '1000::', |
| | 151 | ), |
| | 152 | // IPv6, port, reducible representation. |
| | 153 | array( |
| | 154 | '[0000:0000:0000:0000:0000:0000:0000:0001]:1234', |
| | 155 | '::', |
| | 156 | ), |
| | 157 | // IPv6, port, partially reducible representation. |
| | 158 | array( |
| | 159 | '[1000:0000:0000:0000:0000:0000:0000:0001]:5678', |
| | 160 | '1000::', |
| | 161 | ), |
| | 162 | // IPv6, no port, reduced representation. |
| | 163 | array( |
| | 164 | '::', |
| | 165 | '::', |
| | 166 | ), |
| | 167 | // IPv6, no port, reduced representation. |
| | 168 | array( |
| | 169 | '::1', |
| | 170 | '::', |
| | 171 | ), |
| | 172 | // IPv6, port, reduced representation. |
| | 173 | array( |
| | 174 | '[::]:20000', |
| | 175 | '::', |
| | 176 | ), |
| | 177 | // IPv6, address brackets without port delimiter and number, reduced representation. |
| | 178 | array( |
| | 179 | '[::1]', |
| | 180 | '::', |
| | 181 | ), |
| | 182 | // IPv6, no port, compatibility mode. |
| | 183 | array( |
| | 184 | '::ffff:10.15.20.25', |
| | 185 | '::ffff:10.15.20.0', |
| | 186 | ), |
| | 187 | // IPv6, port, compatibility mode. |
| | 188 | array( |
| | 189 | '[::FFFF:10.15.20.25]:30000', |
| | 190 | '::ffff:10.15.20.0', |
| | 191 | ), |
| | 192 | // IPv6, no port, compatibility mode shorthand. |
| | 193 | array( |
| | 194 | '::127.0.0.1', |
| | 195 | '::ffff:127.0.0.0', |
| | 196 | ), |
| | 197 | // IPv6, port, compatibility mode shorthand. |
| | 198 | array( |
| | 199 | '[::127.0.0.1]:30000', |
| | 200 | '::ffff:127.0.0.0', |
| | 201 | ), |
| | 202 | // IPv6 with reachability scope. |
| | 203 | array( |
| | 204 | 'fe80::b059:65f4:e877:c40%16', |
| | 205 | 'fe80::', |
| | 206 | ), |
| | 207 | // IPv6 with reachability scope. |
| | 208 | array( |
| | 209 | 'FE80::B059:65F4:E877:C40%eth0', |
| | 210 | 'fe80::', |
| | 211 | ), |
| | 212 | ); |
| | 213 | } |
| | 214 | |
| | 215 | /** |
| | 216 | * Test email anonymization of `wp_privacy_anonymize_data()`. |
| | 217 | */ |
| | 218 | public function test_anonymize_email() { |
| | 219 | $this->assertEquals( 'deleted@site.invalid', wp_privacy_anonymize_data( 'email', 'bar@example.com' ) ); |
| | 220 | } |
| | 221 | |
| | 222 | /** |
| | 223 | * Test url anonymization of `wp_privacy_anonymize_data()`. |
| | 224 | */ |
| | 225 | public function test_anonymize_url() { |
| | 226 | $this->assertEquals( 'https://site.invalid', wp_privacy_anonymize_data( 'url', 'https://example.com/author/username' ) ); |
| | 227 | } |
| | 228 | |
| | 229 | /** |
| | 230 | * Test date anonymization of `wp_privacy_anonymize_data()`. |
| | 231 | */ |
| | 232 | public function test_anonymize_date() { |
| | 233 | $this->assertEquals( '0000-00-00 00:00:00', wp_privacy_anonymize_data( 'date', '2003-12-25 12:34:56' ) ); |
| | 234 | } |
| | 235 | |
| | 236 | /** |
| | 237 | * Test text anonymization of `wp_privacy_anonymize_data()`. |
| | 238 | */ |
| | 239 | public function test_anonymize_text() { |
| | 240 | $text = __( 'Four score and seven years ago' ); |
| | 241 | $this->assertEquals( '[deleted]', wp_privacy_anonymize_data( 'text', $text ) ); |
| | 242 | } |
| | 243 | |
| | 244 | /** |
| | 245 | * Test long text anonymization of `wp_privacy_anonymize_data()`. |
| | 246 | */ |
| | 247 | public function test_anonymize_long_text() { |
| | 248 | $text = __( 'Four score and seven years ago' ); |
| | 249 | $this->assertEquals( 'This content was deleted by the author.', wp_privacy_anonymize_data( 'longtext', $text ) ); |
| | 250 | } |
| | 251 | } |