slashes.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @group term
  4. * @group slashes
  5. * @ticket 21767
  6. */
  7. class Tests_Term_Slashes extends WP_Ajax_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. $this->slash_1 = 'String with 1 slash \\';
  14. $this->slash_2 = 'String with 2 slashes \\\\';
  15. $this->slash_3 = 'String with 3 slashes \\\\\\';
  16. $this->slash_4 = 'String with 4 slashes \\\\\\\\';
  17. $this->slash_5 = 'String with 5 slashes \\\\\\\\\\';
  18. $this->slash_6 = 'String with 6 slashes \\\\\\\\\\\\';
  19. $this->slash_7 = 'String with 7 slashes \\\\\\\\\\\\\\';
  20. }
  21. function tearDown() {
  22. wp_set_current_user( $this->old_current_user );
  23. parent::tearDown();
  24. }
  25. /**
  26. * Tests the model function that expects slashed data
  27. *
  28. */
  29. function test_wp_insert_term() {
  30. $taxonomies = array(
  31. 'category',
  32. 'post_tag'
  33. );
  34. foreach ( $taxonomies as $taxonomy ) {
  35. $insert = wp_insert_term(
  36. $this->slash_1,
  37. $taxonomy,
  38. array(
  39. 'slug' => 'slash_test_1_'.$taxonomy,
  40. 'description' => $this->slash_3
  41. )
  42. );
  43. $term = get_term( $insert['term_id'], $taxonomy );
  44. $this->assertEquals( wp_unslash( $this->slash_1 ), $term->name );
  45. $this->assertEquals( wp_unslash( $this->slash_3 ), $term->description );
  46. $insert = wp_insert_term(
  47. $this->slash_3,
  48. $taxonomy,
  49. array(
  50. 'slug' => 'slash_test_2_'.$taxonomy,
  51. 'description' => $this->slash_5
  52. )
  53. );
  54. $term = get_term( $insert['term_id'], $taxonomy );
  55. $this->assertEquals( wp_unslash( $this->slash_3 ), $term->name );
  56. $this->assertEquals( wp_unslash( $this->slash_5 ), $term->description );
  57. $insert = wp_insert_term(
  58. $this->slash_2,
  59. $taxonomy,
  60. array(
  61. 'slug' => 'slash_test_3_'.$taxonomy,
  62. 'description' => $this->slash_4
  63. )
  64. );
  65. $term = get_term( $insert['term_id'], $taxonomy );
  66. $this->assertEquals( wp_unslash( $this->slash_2 ), $term->name );
  67. $this->assertEquals( wp_unslash( $this->slash_4 ), $term->description );
  68. }
  69. }
  70. /**
  71. * Tests the model function that expects slashed data
  72. *
  73. */
  74. function test_wp_update_term() {
  75. $taxonomies = array(
  76. 'category',
  77. 'post_tag'
  78. );
  79. foreach ( $taxonomies as $taxonomy ) {
  80. $id = $this->factory->term->create(array(
  81. 'taxonomy' => $taxonomy
  82. ));
  83. $update = wp_update_term(
  84. $id,
  85. $taxonomy,
  86. array(
  87. 'name' => $this->slash_1,
  88. 'description' => $this->slash_3
  89. )
  90. );
  91. $term = get_term( $id, $taxonomy );
  92. $this->assertEquals( wp_unslash( $this->slash_1 ), $term->name );
  93. $this->assertEquals( wp_unslash( $this->slash_3 ), $term->description );
  94. $update = wp_update_term(
  95. $id,
  96. $taxonomy,
  97. array(
  98. 'name' => $this->slash_3,
  99. 'description' => $this->slash_5
  100. )
  101. );
  102. $term = get_term( $id, $taxonomy );
  103. $this->assertEquals( wp_unslash( $this->slash_3 ), $term->name );
  104. $this->assertEquals( wp_unslash( $this->slash_5 ), $term->description );
  105. $update = wp_update_term(
  106. $id,
  107. $taxonomy,
  108. array(
  109. 'name' => $this->slash_2,
  110. 'description' => $this->slash_4
  111. )
  112. );
  113. $term = get_term( $id, $taxonomy );
  114. $this->assertEquals( wp_unslash( $this->slash_2 ), $term->name );
  115. $this->assertEquals( wp_unslash( $this->slash_4 ), $term->description );
  116. }
  117. }
  118. }