SanitizeTextField.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * @group formatting
  4. */
  5. class Tests_Formatting_SanitizeTextField extends WP_UnitTestCase {
  6. function data_sanitize_text_field() {
  7. return array(
  8. array(
  9. 'оРангутанг', // Ensure UTF-8 text is safe. The Р is D0 A0 and A0 is the non-breaking space.
  10. 'оРангутанг',
  11. ),
  12. array(
  13. 'САПР', // Ensure UTF-8 text is safe. the Р is D0 A0 and A0 is the non-breaking space.
  14. 'САПР',
  15. ),
  16. array(
  17. 'one is < two',
  18. 'one is &lt; two',
  19. ),
  20. array(
  21. "one is <\n two",
  22. array(
  23. 'oneline' => 'one is &lt; two',
  24. 'multiline' => "one is &lt;\n two",
  25. ),
  26. ),
  27. array(
  28. "foo <div\n> bar",
  29. array(
  30. 'oneline' => 'foo bar',
  31. 'multiline' => 'foo bar',
  32. ),
  33. ),
  34. array(
  35. "foo <\ndiv\n> bar",
  36. array(
  37. 'oneline' => 'foo &lt; div > bar',
  38. 'multiline' => "foo &lt;\ndiv\n> bar",
  39. ),
  40. ),
  41. array(
  42. 'tags <span>are</span> <em>not allowed</em> here',
  43. 'tags are not allowed here',
  44. ),
  45. array(
  46. ' we should trim leading and trailing whitespace ',
  47. 'we should trim leading and trailing whitespace',
  48. ),
  49. array(
  50. 'we trim extra internal whitespace only in single line texts',
  51. array(
  52. 'oneline' => 'we trim extra internal whitespace only in single line texts',
  53. 'multiline' => 'we trim extra internal whitespace only in single line texts',
  54. ),
  55. ),
  56. array(
  57. "tabs \tget removed in single line texts",
  58. array(
  59. 'oneline' => 'tabs get removed in single line texts',
  60. 'multiline' => "tabs \tget removed in single line texts",
  61. ),
  62. ),
  63. array(
  64. "newlines are allowed only\n in multiline texts",
  65. array(
  66. 'oneline' => 'newlines are allowed only in multiline texts',
  67. 'multiline' => "newlines are allowed only\n in multiline texts",
  68. ),
  69. ),
  70. array(
  71. 'We also %AB remove %ab octets',
  72. 'We also remove octets',
  73. ),
  74. array(
  75. 'We don\'t need to wory about %A
  76. B removing %a
  77. b octets even when %a B they are obscured by whitespace',
  78. array(
  79. 'oneline' => 'We don\'t need to wory about %A B removing %a b octets even when %a B they are obscured by whitespace',
  80. 'multiline' => "We don't need to wory about %A\n B removing %a\n b octets even when %a B they are obscured by whitespace",
  81. ),
  82. ),
  83. array(
  84. '%AB%BC%DE', // Just octets.
  85. '', // Emtpy as we strip all the octets out.
  86. ),
  87. array(
  88. 'Invalid octects remain %II',
  89. 'Invalid octects remain %II',
  90. ),
  91. array(
  92. 'Nested octects %%%ABABAB %A%A%ABBB',
  93. 'Nested octects',
  94. ),
  95. array(
  96. array(),
  97. '',
  98. ),
  99. array(
  100. array( 1, 2, 'foo' ),
  101. '',
  102. ),
  103. array(
  104. new WP_Query,
  105. '',
  106. ),
  107. array(
  108. 2,
  109. '2',
  110. ),
  111. array(
  112. false,
  113. '',
  114. ),
  115. array(
  116. true,
  117. '1',
  118. ),
  119. array(
  120. 10.1,
  121. '10.1',
  122. ),
  123. );
  124. }
  125. /**
  126. * @ticket 32257
  127. * @dataProvider data_sanitize_text_field
  128. */
  129. function test_sanitize_text_field( $string, $expected ) {
  130. if ( is_array( $expected ) ) {
  131. $expected_oneline = $expected['oneline'];
  132. $expected_multiline = $expected['multiline'];
  133. } else {
  134. $expected_oneline = $expected;
  135. $expected_multiline = $expected;
  136. }
  137. $this->assertSame( $expected_oneline, sanitize_text_field( $string ) );
  138. $this->assertSameIgnoreEOL( $expected_multiline, sanitize_textarea_field( $string ) );
  139. }
  140. }