slashes.php 3.4 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. protected static $author_id;
  9. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  10. self::$author_id = $factory->user->create( array( 'role' => 'administrator' ) );
  11. }
  12. function setUp() {
  13. parent::setUp();
  14. wp_set_current_user( self::$author_id );
  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. /**
  24. * Tests the model function that expects slashed data.
  25. */
  26. function test_wp_insert_term() {
  27. $taxonomies = array(
  28. 'category',
  29. 'post_tag',
  30. );
  31. foreach ( $taxonomies as $taxonomy ) {
  32. $insert = wp_insert_term(
  33. $this->slash_1,
  34. $taxonomy,
  35. array(
  36. 'slug' => 'slash_test_1_' . $taxonomy,
  37. 'description' => $this->slash_3,
  38. )
  39. );
  40. $term = get_term( $insert['term_id'], $taxonomy );
  41. $this->assertSame( wp_unslash( $this->slash_1 ), $term->name );
  42. $this->assertSame( wp_unslash( $this->slash_3 ), $term->description );
  43. $insert = wp_insert_term(
  44. $this->slash_3,
  45. $taxonomy,
  46. array(
  47. 'slug' => 'slash_test_2_' . $taxonomy,
  48. 'description' => $this->slash_5,
  49. )
  50. );
  51. $term = get_term( $insert['term_id'], $taxonomy );
  52. $this->assertSame( wp_unslash( $this->slash_3 ), $term->name );
  53. $this->assertSame( wp_unslash( $this->slash_5 ), $term->description );
  54. $insert = wp_insert_term(
  55. $this->slash_2,
  56. $taxonomy,
  57. array(
  58. 'slug' => 'slash_test_3_' . $taxonomy,
  59. 'description' => $this->slash_4,
  60. )
  61. );
  62. $term = get_term( $insert['term_id'], $taxonomy );
  63. $this->assertSame( wp_unslash( $this->slash_2 ), $term->name );
  64. $this->assertSame( wp_unslash( $this->slash_4 ), $term->description );
  65. }
  66. }
  67. /**
  68. * Tests the model function that expects slashed data.
  69. */
  70. function test_wp_update_term() {
  71. $taxonomies = array(
  72. 'category',
  73. 'post_tag',
  74. );
  75. foreach ( $taxonomies as $taxonomy ) {
  76. $term_id = self::factory()->term->create(
  77. array(
  78. 'taxonomy' => $taxonomy,
  79. )
  80. );
  81. $update = wp_update_term(
  82. $term_id,
  83. $taxonomy,
  84. array(
  85. 'name' => $this->slash_1,
  86. 'description' => $this->slash_3,
  87. )
  88. );
  89. $term = get_term( $term_id, $taxonomy );
  90. $this->assertSame( wp_unslash( $this->slash_1 ), $term->name );
  91. $this->assertSame( wp_unslash( $this->slash_3 ), $term->description );
  92. $update = wp_update_term(
  93. $term_id,
  94. $taxonomy,
  95. array(
  96. 'name' => $this->slash_3,
  97. 'description' => $this->slash_5,
  98. )
  99. );
  100. $term = get_term( $term_id, $taxonomy );
  101. $this->assertSame( wp_unslash( $this->slash_3 ), $term->name );
  102. $this->assertSame( wp_unslash( $this->slash_5 ), $term->description );
  103. $update = wp_update_term(
  104. $term_id,
  105. $taxonomy,
  106. array(
  107. 'name' => $this->slash_2,
  108. 'description' => $this->slash_4,
  109. )
  110. );
  111. $term = get_term( $term_id, $taxonomy );
  112. $this->assertSame( wp_unslash( $this->slash_2 ), $term->name );
  113. $this->assertSame( wp_unslash( $this->slash_4 ), $term->description );
  114. }
  115. }
  116. }