slashes.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * @group comment
  4. * @group slashes
  5. * @ticket 21767
  6. */
  7. class Tests_Comment_Slashes extends WP_UnitTestCase {
  8. function setUp() {
  9. parent::setUp();
  10. // we need an admin user to bypass comment flood protection
  11. $this->author_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
  12. $this->old_current_user = get_current_user_id();
  13. wp_set_current_user( $this->author_id );
  14. // it is important to test with both even and odd numbered slashes as
  15. // kses does a strip-then-add slashes in some of it's function calls
  16. $this->slash_1 = 'String with 1 slash \\';
  17. $this->slash_2 = 'String with 2 slashes \\\\';
  18. $this->slash_3 = 'String with 3 slashes \\\\\\';
  19. $this->slash_4 = 'String with 4 slashes \\\\\\\\';
  20. $this->slash_5 = 'String with 5 slashes \\\\\\\\\\';
  21. $this->slash_6 = 'String with 6 slashes \\\\\\\\\\\\';
  22. $this->slash_7 = 'String with 7 slashes \\\\\\\\\\\\\\';
  23. $_SERVER['REMOTE_ADDR'] = null;
  24. }
  25. function tearDown() {
  26. wp_set_current_user( $this->old_current_user );
  27. parent::tearDown();
  28. }
  29. /**
  30. * Tests the extended model function that expects slashed data
  31. *
  32. */
  33. function test_wp_new_comment() {
  34. $post_id = $this->factory->post->create();
  35. // not testing comment_author_email or comment_author_url
  36. // as slashes are not permitted in that data
  37. $data = array(
  38. 'comment_post_ID' => $post_id,
  39. 'comment_author' => $this->slash_1,
  40. 'comment_author_url' => '',
  41. 'comment_author_email' => '',
  42. 'comment_type' => '',
  43. 'comment_content' => $this->slash_7,
  44. );
  45. $id = wp_new_comment( $data );
  46. $comment = get_comment($id);
  47. $this->assertEquals( wp_unslash( $this->slash_1 ), $comment->comment_author );
  48. $this->assertEquals( wp_unslash( $this->slash_7 ), $comment->comment_content );
  49. $data = array(
  50. 'comment_post_ID' => $post_id,
  51. 'comment_author' => $this->slash_2,
  52. 'comment_author_url' => '',
  53. 'comment_author_email' => '',
  54. 'comment_type' => '',
  55. 'comment_content' => $this->slash_4,
  56. );
  57. $id = wp_new_comment( $data );
  58. $comment = get_comment($id);
  59. $this->assertEquals( wp_unslash( $this->slash_2 ), $comment->comment_author );
  60. $this->assertEquals( wp_unslash( $this->slash_4 ), $comment->comment_content );
  61. }
  62. /**
  63. * Tests the controller function that expects slashed data
  64. *
  65. */
  66. function test_edit_comment() {
  67. $post_id = $this->factory->post->create();
  68. $comment_id = $this->factory->comment->create(array(
  69. 'comment_post_ID' => $post_id
  70. ));
  71. // not testing comment_author_email or comment_author_url
  72. // as slashes are not permitted in that data
  73. $_POST = array();
  74. $_POST['comment_ID'] = $comment_id;
  75. $_POST['comment_status'] = '';
  76. $_POST['newcomment_author'] = $this->slash_1;
  77. $_POST['newcomment_author_url'] = '';
  78. $_POST['newcomment_author_email'] = '';
  79. $_POST['content'] = $this->slash_7;
  80. $_POST = add_magic_quotes( $_POST );
  81. edit_comment();
  82. $comment = get_comment( $comment_id );
  83. $this->assertEquals( $this->slash_1, $comment->comment_author );
  84. $this->assertEquals( $this->slash_7, $comment->comment_content );
  85. $_POST = array();
  86. $_POST['comment_ID'] = $comment_id;
  87. $_POST['comment_status'] = '';
  88. $_POST['newcomment_author'] = $this->slash_2;
  89. $_POST['newcomment_author_url'] = '';
  90. $_POST['newcomment_author_email'] = '';
  91. $_POST['content'] = $this->slash_4;
  92. $_POST = add_magic_quotes( $_POST );
  93. edit_comment();
  94. $comment = get_comment( $comment_id );
  95. $this->assertEquals( $this->slash_2, $comment->comment_author );
  96. $this->assertEquals( $this->slash_4, $comment->comment_content );
  97. }
  98. /**
  99. * Tests the model function that expects slashed data
  100. *
  101. */
  102. function test_wp_insert_comment() {
  103. $post_id = $this->factory->post->create();
  104. $comment_id = wp_insert_comment(array(
  105. 'comment_post_ID' => $post_id,
  106. 'comment_author' => $this->slash_1,
  107. 'comment_content' => $this->slash_7,
  108. ));
  109. $comment = get_comment( $comment_id );
  110. $this->assertEquals( wp_unslash( $this->slash_1 ), $comment->comment_author );
  111. $this->assertEquals( wp_unslash( $this->slash_7 ), $comment->comment_content );
  112. $comment_id = wp_insert_comment(array(
  113. 'comment_post_ID' => $post_id,
  114. 'comment_author' => $this->slash_2,
  115. 'comment_content' => $this->slash_4,
  116. ));
  117. $comment = get_comment( $comment_id );
  118. $this->assertEquals( wp_unslash( $this->slash_2 ), $comment->comment_author );
  119. $this->assertEquals( wp_unslash( $this->slash_4 ), $comment->comment_content );
  120. }
  121. /**
  122. * Tests the model function that expects slashed data
  123. *
  124. */
  125. function test_wp_update_comment() {
  126. $post_id = $this->factory->post->create();
  127. $comment_id = $this->factory->comment->create(array(
  128. 'comment_post_ID' => $post_id
  129. ));
  130. wp_update_comment(array(
  131. 'comment_ID' => $comment_id,
  132. 'comment_author' => $this->slash_1,
  133. 'comment_content' => $this->slash_7,
  134. ));
  135. $comment = get_comment( $comment_id );
  136. $this->assertEquals( wp_unslash( $this->slash_1 ), $comment->comment_author );
  137. $this->assertEquals( wp_unslash( $this->slash_7 ), $comment->comment_content );
  138. wp_update_comment(array(
  139. 'comment_ID' => $comment_id,
  140. 'comment_author' => $this->slash_2,
  141. 'comment_content' => $this->slash_4,
  142. ));
  143. $comment = get_comment( $comment_id );
  144. $this->assertEquals( wp_unslash( $this->slash_2 ), $comment->comment_author );
  145. $this->assertEquals( wp_unslash( $this->slash_4 ), $comment->comment_content );
  146. }
  147. }