slashes.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * @group meta
  4. * @group slashes
  5. * @ticket 21767
  6. */
  7. class Tests_Meta_Slashes extends WP_UnitTestCase {
  8. protected static $editor_id;
  9. protected static $post_id;
  10. protected static $comment_id;
  11. protected static $user_id;
  12. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  13. self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
  14. self::$post_id = $factory->post->create();
  15. self::$comment_id = $factory->comment->create( array( 'comment_post_ID' => self::$post_id ) );
  16. self::$user_id = $factory->user->create();
  17. }
  18. function setUp() {
  19. parent::setUp();
  20. wp_set_current_user( self::$editor_id );
  21. $this->slash_1 = 'String with 1 slash \\';
  22. $this->slash_2 = 'String with 2 slashes \\\\';
  23. $this->slash_3 = 'String with 3 slashes \\\\\\';
  24. $this->slash_4 = 'String with 4 slashes \\\\\\\\';
  25. $this->slash_5 = 'String with 5 slashes \\\\\\\\\\';
  26. $this->slash_6 = 'String with 6 slashes \\\\\\\\\\\\';
  27. $this->slash_7 = 'String with 7 slashes \\\\\\\\\\\\\\';
  28. }
  29. /**
  30. * Tests the controller function that expects slashed data.
  31. */
  32. function test_edit_post() {
  33. $post_id = self::$post_id;
  34. if ( function_exists( 'wp_add_post_meta' ) ) {
  35. $meta_1 = wp_add_post_meta( $post_id, 'slash_test_1', 'foo' );
  36. $meta_2 = wp_add_post_meta( $post_id, 'slash_test_2', 'foo' );
  37. $meta_3 = wp_add_post_meta( $post_id, 'slash_test_3', 'foo' );
  38. } else {
  39. // Expects slashed data.
  40. $meta_1 = add_post_meta( $post_id, 'slash_test_1', addslashes( 'foo' ) );
  41. $meta_2 = add_post_meta( $post_id, 'slash_test_2', addslashes( 'foo' ) );
  42. $meta_3 = add_post_meta( $post_id, 'slash_test_3', addslashes( 'foo' ) );
  43. }
  44. $_POST = array();
  45. $_POST['post_ID'] = $post_id;
  46. $_POST['metakeyselect'] = '#NONE#';
  47. $_POST['metakeyinput'] = 'slash_test_0';
  48. $_POST['metavalue'] = $this->slash_6;
  49. $_POST['meta'] = array(
  50. $meta_1 => array(
  51. 'key' => 'slash_test_1',
  52. 'value' => $this->slash_1,
  53. ),
  54. $meta_2 => array(
  55. 'key' => 'slash_test_2',
  56. 'value' => $this->slash_3,
  57. ),
  58. $meta_3 => array(
  59. 'key' => 'slash_test_3',
  60. 'value' => $this->slash_4,
  61. ),
  62. );
  63. $_POST = add_magic_quotes( $_POST ); // The edit_post() function will strip slashes.
  64. edit_post();
  65. $post = get_post( $post_id );
  66. $this->assertSame( $this->slash_6, get_post_meta( $post_id, 'slash_test_0', true ) );
  67. $this->assertSame( $this->slash_1, get_post_meta( $post_id, 'slash_test_1', true ) );
  68. $this->assertSame( $this->slash_3, get_post_meta( $post_id, 'slash_test_2', true ) );
  69. $this->assertSame( $this->slash_4, get_post_meta( $post_id, 'slash_test_3', true ) );
  70. $_POST = array();
  71. $_POST['post_ID'] = $post_id;
  72. $_POST['metakeyselect'] = '#NONE#';
  73. $_POST['metakeyinput'] = 'slash_test_0';
  74. $_POST['metavalue'] = $this->slash_7;
  75. $_POST['meta'] = array(
  76. $meta_1 => array(
  77. 'key' => 'slash_test_1',
  78. 'value' => $this->slash_2,
  79. ),
  80. $meta_2 => array(
  81. 'key' => 'slash_test_2',
  82. 'value' => $this->slash_4,
  83. ),
  84. $meta_3 => array(
  85. 'key' => 'slash_test_3',
  86. 'value' => $this->slash_5,
  87. ),
  88. );
  89. $_POST = add_magic_quotes( $_POST ); // The edit_post() function will strip slashes.
  90. edit_post();
  91. $post = get_post( $post_id );
  92. $this->assertSame( $this->slash_2, get_post_meta( $post_id, 'slash_test_1', true ) );
  93. $this->assertSame( $this->slash_4, get_post_meta( $post_id, 'slash_test_2', true ) );
  94. $this->assertSame( $this->slash_5, get_post_meta( $post_id, 'slash_test_3', true ) );
  95. }
  96. /**
  97. * Tests the legacy model function that expects slashed data.
  98. */
  99. function test_add_post_meta() {
  100. $post_id = self::$post_id;
  101. add_post_meta( $post_id, 'slash_test_1', addslashes( $this->slash_1 ) );
  102. add_post_meta( $post_id, 'slash_test_2', addslashes( $this->slash_3 ) );
  103. add_post_meta( $post_id, 'slash_test_3', addslashes( $this->slash_4 ) );
  104. $this->assertSame( $this->slash_1, get_post_meta( $post_id, 'slash_test_1', true ) );
  105. $this->assertSame( $this->slash_3, get_post_meta( $post_id, 'slash_test_2', true ) );
  106. $this->assertSame( $this->slash_4, get_post_meta( $post_id, 'slash_test_3', true ) );
  107. }
  108. /**
  109. * Tests the legacy model function that expects slashed data.
  110. */
  111. function test_update_post_meta() {
  112. $post_id = self::$post_id;
  113. update_post_meta( $post_id, 'slash_test_1', addslashes( $this->slash_1 ) );
  114. update_post_meta( $post_id, 'slash_test_2', addslashes( $this->slash_3 ) );
  115. update_post_meta( $post_id, 'slash_test_3', addslashes( $this->slash_4 ) );
  116. $this->assertSame( $this->slash_1, get_post_meta( $post_id, 'slash_test_1', true ) );
  117. $this->assertSame( $this->slash_3, get_post_meta( $post_id, 'slash_test_2', true ) );
  118. $this->assertSame( $this->slash_4, get_post_meta( $post_id, 'slash_test_3', true ) );
  119. }
  120. /**
  121. * Tests the model function that expects slashed data.
  122. */
  123. function test_add_comment_meta() {
  124. $comment_id = self::$comment_id;
  125. add_comment_meta( $comment_id, 'slash_test_1', $this->slash_1 );
  126. add_comment_meta( $comment_id, 'slash_test_2', $this->slash_3 );
  127. add_comment_meta( $comment_id, 'slash_test_3', $this->slash_5 );
  128. $this->assertSame( wp_unslash( $this->slash_1 ), get_comment_meta( $comment_id, 'slash_test_1', true ) );
  129. $this->assertSame( wp_unslash( $this->slash_3 ), get_comment_meta( $comment_id, 'slash_test_2', true ) );
  130. $this->assertSame( wp_unslash( $this->slash_5 ), get_comment_meta( $comment_id, 'slash_test_3', true ) );
  131. add_comment_meta( $comment_id, 'slash_test_4', $this->slash_2 );
  132. add_comment_meta( $comment_id, 'slash_test_5', $this->slash_4 );
  133. add_comment_meta( $comment_id, 'slash_test_6', $this->slash_6 );
  134. $this->assertSame( wp_unslash( $this->slash_2 ), get_comment_meta( $comment_id, 'slash_test_4', true ) );
  135. $this->assertSame( wp_unslash( $this->slash_4 ), get_comment_meta( $comment_id, 'slash_test_5', true ) );
  136. $this->assertSame( wp_unslash( $this->slash_6 ), get_comment_meta( $comment_id, 'slash_test_6', true ) );
  137. }
  138. /**
  139. * Tests the model function that expects slashed data.
  140. */
  141. function test_update_comment_meta() {
  142. $comment_id = self::$comment_id;
  143. add_comment_meta( $comment_id, 'slash_test_1', 'foo' );
  144. add_comment_meta( $comment_id, 'slash_test_2', 'foo' );
  145. add_comment_meta( $comment_id, 'slash_test_3', 'foo' );
  146. update_comment_meta( $comment_id, 'slash_test_1', $this->slash_1 );
  147. update_comment_meta( $comment_id, 'slash_test_2', $this->slash_3 );
  148. update_comment_meta( $comment_id, 'slash_test_3', $this->slash_5 );
  149. $this->assertSame( wp_unslash( $this->slash_1 ), get_comment_meta( $comment_id, 'slash_test_1', true ) );
  150. $this->assertSame( wp_unslash( $this->slash_3 ), get_comment_meta( $comment_id, 'slash_test_2', true ) );
  151. $this->assertSame( wp_unslash( $this->slash_5 ), get_comment_meta( $comment_id, 'slash_test_3', true ) );
  152. update_comment_meta( $comment_id, 'slash_test_1', $this->slash_2 );
  153. update_comment_meta( $comment_id, 'slash_test_2', $this->slash_4 );
  154. update_comment_meta( $comment_id, 'slash_test_3', $this->slash_6 );
  155. $this->assertSame( wp_unslash( $this->slash_2 ), get_comment_meta( $comment_id, 'slash_test_1', true ) );
  156. $this->assertSame( wp_unslash( $this->slash_4 ), get_comment_meta( $comment_id, 'slash_test_2', true ) );
  157. $this->assertSame( wp_unslash( $this->slash_6 ), get_comment_meta( $comment_id, 'slash_test_3', true ) );
  158. }
  159. /**
  160. * Tests the model function that expects slashed data.
  161. */
  162. function test_add_user_meta() {
  163. $user_id = self::$user_id;
  164. add_user_meta( $user_id, 'slash_test_1', $this->slash_1 );
  165. add_user_meta( $user_id, 'slash_test_2', $this->slash_3 );
  166. add_user_meta( $user_id, 'slash_test_3', $this->slash_5 );
  167. $this->assertSame( wp_unslash( $this->slash_1 ), get_user_meta( $user_id, 'slash_test_1', true ) );
  168. $this->assertSame( wp_unslash( $this->slash_3 ), get_user_meta( $user_id, 'slash_test_2', true ) );
  169. $this->assertSame( wp_unslash( $this->slash_5 ), get_user_meta( $user_id, 'slash_test_3', true ) );
  170. add_user_meta( $user_id, 'slash_test_4', $this->slash_2 );
  171. add_user_meta( $user_id, 'slash_test_5', $this->slash_4 );
  172. add_user_meta( $user_id, 'slash_test_6', $this->slash_6 );
  173. $this->assertSame( wp_unslash( $this->slash_2 ), get_user_meta( $user_id, 'slash_test_4', true ) );
  174. $this->assertSame( wp_unslash( $this->slash_4 ), get_user_meta( $user_id, 'slash_test_5', true ) );
  175. $this->assertSame( wp_unslash( $this->slash_6 ), get_user_meta( $user_id, 'slash_test_6', true ) );
  176. }
  177. /**
  178. * Tests the model function that expects slashed data.
  179. */
  180. function test_update_user_meta() {
  181. $user_id = self::$user_id;
  182. add_user_meta( $user_id, 'slash_test_1', 'foo' );
  183. add_user_meta( $user_id, 'slash_test_2', 'foo' );
  184. add_user_meta( $user_id, 'slash_test_3', 'foo' );
  185. update_user_meta( $user_id, 'slash_test_1', $this->slash_1 );
  186. update_user_meta( $user_id, 'slash_test_2', $this->slash_3 );
  187. update_user_meta( $user_id, 'slash_test_3', $this->slash_5 );
  188. $this->assertSame( wp_unslash( $this->slash_1 ), get_user_meta( $user_id, 'slash_test_1', true ) );
  189. $this->assertSame( wp_unslash( $this->slash_3 ), get_user_meta( $user_id, 'slash_test_2', true ) );
  190. $this->assertSame( wp_unslash( $this->slash_5 ), get_user_meta( $user_id, 'slash_test_3', true ) );
  191. update_user_meta( $user_id, 'slash_test_1', $this->slash_2 );
  192. update_user_meta( $user_id, 'slash_test_2', $this->slash_4 );
  193. update_user_meta( $user_id, 'slash_test_3', $this->slash_6 );
  194. $this->assertSame( wp_unslash( $this->slash_2 ), get_user_meta( $user_id, 'slash_test_1', true ) );
  195. $this->assertSame( wp_unslash( $this->slash_4 ), get_user_meta( $user_id, 'slash_test_2', true ) );
  196. $this->assertSame( wp_unslash( $this->slash_6 ), get_user_meta( $user_id, 'slash_test_3', true ) );
  197. }
  198. }