/tests/phpunit/tests/functions/anonymization.php

https://gitlab.com/morganestes/wordpress-develop · PHP · 247 lines · 155 code · 10 blank · 82 comment · 2 complexity · f547d16b92dd1e530486829760ba6602 MD5 · raw file

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