/tests/user/slashes.php

https://gitlab.com/Blueprint-Marketing/wordpress-unit-tests · PHP · 210 lines · 162 code · 25 blank · 23 comment · 0 complexity · 7c0a6244cf00fafdb6d8561b7a3785b4 MD5 · raw file

  1. <?php
  2. /**
  3. * @group user
  4. * @group slashes
  5. * @ticket 21767
  6. */
  7. class Tests_User_Slashes extends WP_UnitTestCase {
  8. function setUp() {
  9. parent::setUp();
  10. $this->author_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
  11. $this->old_current_user = get_current_user_id();
  12. wp_set_current_user( $this->author_id );
  13. // it is important to test with both even and odd numbered slashes as
  14. // kses does a strip-then-add slashes in some of it's function calls
  15. $this->slash_1 = 'String with 1 slash \\';
  16. $this->slash_2 = 'String with 2 slashes \\\\';
  17. $this->slash_3 = 'String with 3 slashes \\\\\\';
  18. $this->slash_4 = 'String with 4 slashes \\\\\\\\';
  19. $this->slash_5 = 'String with 5 slashes \\\\\\\\\\';
  20. $this->slash_6 = 'String with 6 slashes \\\\\\\\\\\\';
  21. $this->slash_7 = 'String with 7 slashes \\\\\\\\\\\\\\';
  22. }
  23. function tearDown() {
  24. wp_set_current_user( $this->old_current_user );
  25. parent::tearDown();
  26. }
  27. /**
  28. * Tests the controller function that expects slashed data
  29. *
  30. */
  31. function test_add_user() {
  32. $_POST = $_GET = $_REQUEST = array();
  33. $_POST['user_login'] = 'slash_example_user_1';
  34. $_POST['pass1'] = 'password';
  35. $_POST['pass2'] = 'password';
  36. $_POST['role'] = 'subscriber';
  37. $_POST['email'] = 'user1@example.com';
  38. $_POST['first_name'] = $this->slash_1;
  39. $_POST['last_name'] = $this->slash_3;
  40. $_POST['nickname'] = $this->slash_5;
  41. $_POST['display_name'] = $this->slash_7;
  42. $_POST['description'] = $this->slash_3;
  43. $_POST = add_magic_quotes( $_POST ); // the edit_post() function will strip slashes
  44. $id = add_user();
  45. $user = get_user_to_edit( $id );
  46. $this->assertEquals( $this->slash_1, $user->first_name );
  47. $this->assertEquals( $this->slash_3, $user->last_name );
  48. $this->assertEquals( $this->slash_5, $user->nickname );
  49. $this->assertEquals( $this->slash_7, $user->display_name );
  50. $this->assertEquals( $this->slash_3, $user->description );
  51. $_POST = $_GET = $_REQUEST = array();
  52. $_POST['user_login'] = 'slash_example_user_2';
  53. $_POST['pass1'] = 'password';
  54. $_POST['pass2'] = 'password';
  55. $_POST['role'] = 'subscriber';
  56. $_POST['email'] = 'user2@example.com';
  57. $_POST['first_name'] = $this->slash_2;
  58. $_POST['last_name'] = $this->slash_4;
  59. $_POST['nickname'] = $this->slash_6;
  60. $_POST['display_name'] = $this->slash_2;
  61. $_POST['description'] = $this->slash_4;
  62. $_POST = add_magic_quotes( $_POST ); // the edit_post() function will strip slashes
  63. $id = add_user();
  64. $user = get_user_to_edit( $id );
  65. $this->assertEquals( $this->slash_2, $user->first_name );
  66. $this->assertEquals( $this->slash_4, $user->last_name );
  67. $this->assertEquals( $this->slash_6, $user->nickname );
  68. $this->assertEquals( $this->slash_2, $user->display_name );
  69. $this->assertEquals( $this->slash_4, $user->description );
  70. }
  71. /**
  72. * Tests the controller function that expects slashed data
  73. *
  74. */
  75. function test_edit_user() {
  76. $id = $this->factory->user->create();
  77. $_POST = $_GET = $_REQUEST = array();
  78. $_POST['role'] = 'subscriber';
  79. $_POST['email'] = 'user1@example.com';
  80. $_POST['first_name'] = $this->slash_1;
  81. $_POST['last_name'] = $this->slash_3;
  82. $_POST['nickname'] = $this->slash_5;
  83. $_POST['display_name'] = $this->slash_7;
  84. $_POST['description'] = $this->slash_3;
  85. $_POST = add_magic_quotes( $_POST ); // the edit_post() function will strip slashes
  86. $id = edit_user( $id );
  87. $user = get_user_to_edit( $id );
  88. $this->assertEquals( $this->slash_1, $user->first_name );
  89. $this->assertEquals( $this->slash_3, $user->last_name );
  90. $this->assertEquals( $this->slash_5, $user->nickname );
  91. $this->assertEquals( $this->slash_7, $user->display_name );
  92. $this->assertEquals( $this->slash_3, $user->description );
  93. $_POST = $_GET = $_REQUEST = array();
  94. $_POST['role'] = 'subscriber';
  95. $_POST['email'] = 'user2@example.com';
  96. $_POST['first_name'] = $this->slash_2;
  97. $_POST['last_name'] = $this->slash_4;
  98. $_POST['nickname'] = $this->slash_6;
  99. $_POST['display_name'] = $this->slash_2;
  100. $_POST['description'] = $this->slash_4;
  101. $_POST = add_magic_quotes( $_POST ); // the edit_post() function will strip slashes
  102. $id = edit_user( $id );
  103. $user = get_user_to_edit( $id );
  104. $this->assertEquals( $this->slash_2, $user->first_name );
  105. $this->assertEquals( $this->slash_4, $user->last_name );
  106. $this->assertEquals( $this->slash_6, $user->nickname );
  107. $this->assertEquals( $this->slash_2, $user->display_name );
  108. $this->assertEquals( $this->slash_4, $user->description );
  109. }
  110. /**
  111. * Tests the model function that expects slashed data
  112. *
  113. */
  114. function test_wp_insert_user() {
  115. $id = wp_insert_user(array(
  116. 'user_login' => 'slash_example_user_3',
  117. 'role' => 'subscriber',
  118. 'email' => 'user3@example.com',
  119. 'first_name' => $this->slash_1,
  120. 'last_name' => $this->slash_3,
  121. 'nickname' => $this->slash_5,
  122. 'display_name' => $this->slash_7,
  123. 'description' => $this->slash_3,
  124. ));
  125. $user = get_user_to_edit( $id );
  126. $this->assertEquals( wp_unslash( $this->slash_1 ), $user->first_name );
  127. $this->assertEquals( wp_unslash( $this->slash_3 ), $user->last_name );
  128. $this->assertEquals( wp_unslash( $this->slash_5 ), $user->nickname );
  129. $this->assertEquals( wp_unslash( $this->slash_7 ), $user->display_name );
  130. $this->assertEquals( wp_unslash( $this->slash_3 ), $user->description );
  131. $id = wp_insert_user(array(
  132. 'user_login' => 'slash_example_user_4',
  133. 'role' => 'subscriber',
  134. 'email' => 'user3@example.com',
  135. 'first_name' => $this->slash_2,
  136. 'last_name' => $this->slash_4,
  137. 'nickname' => $this->slash_6,
  138. 'display_name' => $this->slash_2,
  139. 'description' => $this->slash_4,
  140. ));
  141. $user = get_user_to_edit( $id );
  142. $this->assertEquals( wp_unslash( $this->slash_2 ), $user->first_name );
  143. $this->assertEquals( wp_unslash( $this->slash_4 ), $user->last_name );
  144. $this->assertEquals( wp_unslash( $this->slash_6 ), $user->nickname );
  145. $this->assertEquals( wp_unslash( $this->slash_2 ), $user->display_name );
  146. $this->assertEquals( wp_unslash( $this->slash_4 ), $user->description );
  147. }
  148. /**
  149. * Tests the model function that expects slashed data
  150. *
  151. */
  152. function test_wp_update_user() {
  153. $id = $this->factory->user->create();
  154. $id = wp_update_user(array(
  155. 'ID' => $id,
  156. 'role' => 'subscriber',
  157. 'first_name' => $this->slash_1,
  158. 'last_name' => $this->slash_3,
  159. 'nickname' => $this->slash_5,
  160. 'display_name' => $this->slash_7,
  161. 'description' => $this->slash_3,
  162. ));
  163. $user = get_user_to_edit( $id );
  164. $this->assertEquals( wp_unslash( $this->slash_1 ), $user->first_name );
  165. $this->assertEquals( wp_unslash( $this->slash_3 ), $user->last_name );
  166. $this->assertEquals( wp_unslash( $this->slash_5 ), $user->nickname );
  167. $this->assertEquals( wp_unslash( $this->slash_7 ), $user->display_name );
  168. $this->assertEquals( wp_unslash( $this->slash_3 ), $user->description );
  169. $id = wp_update_user(array(
  170. 'ID' => $id,
  171. 'role' => 'subscriber',
  172. 'first_name' => $this->slash_2,
  173. 'last_name' => $this->slash_4,
  174. 'nickname' => $this->slash_6,
  175. 'display_name' => $this->slash_2,
  176. 'description' => $this->slash_4,
  177. ));
  178. $user = get_user_to_edit( $id );
  179. $this->assertEquals( wp_unslash( $this->slash_2 ), $user->first_name );
  180. $this->assertEquals( wp_unslash( $this->slash_4 ), $user->last_name );
  181. $this->assertEquals( wp_unslash( $this->slash_6 ), $user->nickname );
  182. $this->assertEquals( wp_unslash( $this->slash_2 ), $user->display_name );
  183. $this->assertEquals( wp_unslash( $this->slash_4 ), $user->description );
  184. }
  185. }