isObjectInTerm.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * @group taxonomy
  4. */
  5. class Tests_IsObjectInTerm extends WP_UnitTestCase {
  6. public function test_terms_are_ints() {
  7. register_taxonomy( 'wptests_tax', 'post' );
  8. $t1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
  9. $t2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
  10. $posts = self::factory()->post->create_many( 2 );
  11. wp_set_object_terms( $posts[0], array( $t1 ), 'wptests_tax' );
  12. $this->assertTrue( is_object_in_term( $posts[0], 'wptests_tax', array( $t1, $t2 ) ) );
  13. $this->assertFalse( is_object_in_term( $posts[1], 'wptests_tax', array( $t1, $t2 ) ) );
  14. _unregister_taxonomy( 'wptests_tax', 'post' );
  15. }
  16. public function test_terms_are_strings_and_match_term_id() {
  17. register_taxonomy( 'wptests_tax', 'post' );
  18. $t1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
  19. $t2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
  20. $posts = self::factory()->post->create_many( 2 );
  21. wp_set_object_terms( $posts[0], array( $t1 ), 'wptests_tax' );
  22. $t1_str = (string) $t1;
  23. $t2_str = (string) $t2;
  24. $this->assertTrue( is_object_in_term( $posts[0], 'wptests_tax', array( $t1_str, $t2_str ) ) );
  25. $this->assertFalse( is_object_in_term( $posts[1], 'wptests_tax', array( $t1_str, $t2_str ) ) );
  26. _unregister_taxonomy( 'wptests_tax', 'post' );
  27. }
  28. public function test_terms_are_strings_and_match_term_name() {
  29. register_taxonomy( 'wptests_tax', 'post' );
  30. $t1 = self::factory()->term->create(
  31. array(
  32. 'taxonomy' => 'wptests_tax',
  33. 'name' => 'Foo',
  34. )
  35. );
  36. $t2 = self::factory()->term->create(
  37. array(
  38. 'taxonomy' => 'wptests_tax',
  39. 'name' => 'Bar',
  40. )
  41. );
  42. $posts = self::factory()->post->create_many( 2 );
  43. wp_set_object_terms( $posts[0], array( $t1 ), 'wptests_tax' );
  44. $this->assertTrue( is_object_in_term( $posts[0], 'wptests_tax', array( 'Foo', 'Bar' ) ) );
  45. $this->assertFalse( is_object_in_term( $posts[1], 'wptests_tax', array( 'Foo', 'Bar' ) ) );
  46. _unregister_taxonomy( 'wptests_tax', 'post' );
  47. }
  48. public function test_terms_are_strings_and_match_term_slug() {
  49. register_taxonomy( 'wptests_tax', 'post' );
  50. $t1 = self::factory()->term->create(
  51. array(
  52. 'taxonomy' => 'wptests_tax',
  53. 'slug' => 'foo',
  54. )
  55. );
  56. $t2 = self::factory()->term->create(
  57. array(
  58. 'taxonomy' => 'wptests_tax',
  59. 'slug' => 'bar',
  60. )
  61. );
  62. $posts = self::factory()->post->create_many( 2 );
  63. wp_set_object_terms( $posts[0], array( $t1 ), 'wptests_tax' );
  64. $this->assertTrue( is_object_in_term( $posts[0], 'wptests_tax', array( 'foo', 'bar' ) ) );
  65. $this->assertFalse( is_object_in_term( $posts[1], 'wptests_tax', array( 'foo', 'bar' ) ) );
  66. _unregister_taxonomy( 'wptests_tax', 'post' );
  67. }
  68. public function test_terms_contain_strings_and_ints_and_match_term_id_as_int() {
  69. register_taxonomy( 'wptests_tax', 'post' );
  70. $t1 = self::factory()->term->create(
  71. array(
  72. 'taxonomy' => 'wptests_tax',
  73. 'slug' => 'foo',
  74. )
  75. );
  76. $t2 = self::factory()->term->create(
  77. array(
  78. 'taxonomy' => 'wptests_tax',
  79. 'slug' => 'bar',
  80. )
  81. );
  82. $posts = self::factory()->post->create_many( 2 );
  83. wp_set_object_terms( $posts[0], array( $t1 ), 'wptests_tax' );
  84. $this->assertTrue( is_object_in_term( $posts[0], 'wptests_tax', array( $t1, 'bar' ) ) );
  85. $this->assertFalse( is_object_in_term( $posts[1], 'wptests_tax', array( $t1, 'bar' ) ) );
  86. _unregister_taxonomy( 'wptests_tax', 'post' );
  87. }
  88. /**
  89. * @ticket 29467
  90. */
  91. public function test_should_not_return_true_if_term_name_begins_with_existing_term_id() {
  92. register_taxonomy( 'wptests_tax', 'post' );
  93. $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
  94. $post_ID = self::factory()->post->create();
  95. wp_set_object_terms( $post_ID, $t, 'wptests_tax' );
  96. $int_tax_name = $t . '_term_name';
  97. $this->assertFalse( is_object_in_term( $post_ID, 'wptests_tax', $int_tax_name ) );
  98. // Verify it works properly when the post is actually in the term.
  99. wp_set_object_terms( $post_ID, array( $int_tax_name ), 'wptests_tax' );
  100. $this->assertTrue( is_object_in_term( $post_ID, 'wptests_tax', $int_tax_name ) );
  101. }
  102. /**
  103. * @ticket 32044
  104. */
  105. public function test_should_populate_and_hit_relationships_cache() {
  106. global $wpdb;
  107. register_taxonomy( 'wptests_tax', 'post' );
  108. $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
  109. $o = 12345;
  110. wp_set_object_terms( $o, $terms[0], 'wptests_tax' );
  111. $num_queries = $wpdb->num_queries;
  112. $this->assertTrue( is_object_in_term( $o, 'wptests_tax', $terms[0] ) );
  113. $num_queries++;
  114. $this->assertSame( $num_queries, $wpdb->num_queries );
  115. $this->assertFalse( is_object_in_term( $o, 'wptests_tax', $terms[1] ) );
  116. $this->assertSame( $num_queries, $wpdb->num_queries );
  117. }
  118. /**
  119. * @ticket 32044
  120. */
  121. public function test_should_not_be_fooled_by_a_stale_relationship_cache() {
  122. global $wpdb;
  123. register_taxonomy( 'wptests_tax', 'post' );
  124. $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
  125. $o = 12345;
  126. wp_set_object_terms( $o, $terms[0], 'wptests_tax' );
  127. $num_queries = $wpdb->num_queries;
  128. $this->assertTrue( is_object_in_term( $o, 'wptests_tax', $terms[0] ) );
  129. $num_queries++;
  130. $this->assertSame( $num_queries, $wpdb->num_queries );
  131. wp_set_object_terms( $o, $terms[1], 'wptests_tax' );
  132. $num_queries = $wpdb->num_queries;
  133. $this->assertTrue( is_object_in_term( $o, 'wptests_tax', $terms[1] ) );
  134. $num_queries++;
  135. $this->assertSame( $num_queries, $wpdb->num_queries );
  136. }
  137. /**
  138. * @ticket 37721
  139. */
  140. public function test_invalid_taxonomy_should_return_wp_error_object() {
  141. $this->assertWPError( is_object_in_term( 12345, 'foo', 'bar' ) );
  142. }
  143. }