getTermBy.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. * @group taxonomy
  4. */
  5. class Tests_Term_GetTermBy extends WP_UnitTestCase {
  6. function test_get_term_by_slug() {
  7. $term1 = wp_insert_term( 'Foo', 'category', array( 'slug' => 'foo' ) );
  8. $term2 = get_term_by( 'slug', 'foo', 'category' );
  9. $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );
  10. }
  11. function test_get_term_by_name() {
  12. $term1 = wp_insert_term( 'Foo', 'category', array( 'slug' => 'foo' ) );
  13. $term2 = get_term_by( 'name', 'Foo', 'category' );
  14. $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );
  15. }
  16. function test_get_term_by_id() {
  17. $term1 = wp_insert_term( 'Foo', 'category', array( 'slug' => 'foo' ) );
  18. $term2 = get_term_by( 'id', $term1['term_id'], 'category' );
  19. $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );
  20. }
  21. /**
  22. * 'term_id' is an alias of 'id'.
  23. */
  24. function test_get_term_by_term_id() {
  25. $term1 = wp_insert_term( 'Foo', 'category', array( 'slug' => 'foo' ) );
  26. $term2 = get_term_by( 'term_id', $term1['term_id'], 'category' );
  27. $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );
  28. }
  29. /**
  30. * @ticket 45163
  31. */
  32. function test_get_term_by_uppercase_id() {
  33. $term1 = wp_insert_term( 'Foo', 'category', array( 'slug' => 'foo' ) );
  34. $term2 = get_term_by( 'ID', $term1['term_id'], 'category' );
  35. $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );
  36. }
  37. /**
  38. * @ticket 21651
  39. */
  40. function test_get_term_by_tt_id() {
  41. $term1 = wp_insert_term( 'Foo', 'category' );
  42. $term2 = get_term_by( 'term_taxonomy_id', $term1['term_taxonomy_id'], 'category' );
  43. $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );
  44. }
  45. function test_get_term_by_unknown() {
  46. wp_insert_term( 'Foo', 'category', array( 'slug' => 'foo' ) );
  47. $term2 = get_term_by( 'unknown', 'foo', 'category' );
  48. $this->assertFalse( $term2 );
  49. }
  50. /**
  51. * @ticket 33281
  52. */
  53. function test_get_term_by_with_nonexistent_id_should_return_false() {
  54. $term = get_term_by( 'id', 123456, 'category' );
  55. $this->assertFalse( $term );
  56. }
  57. /**
  58. * @ticket 16282
  59. */
  60. public function test_get_term_by_slug_should_match_nonaccented_equivalents() {
  61. register_taxonomy( 'wptests_tax', 'post' );
  62. $slug = 'ńaș';
  63. $t = self::factory()->term->create(
  64. array(
  65. 'slug' => $slug,
  66. 'taxonomy' => 'wptests_tax',
  67. )
  68. );
  69. $found = get_term_by( 'slug', 'nas', 'wptests_tax' );
  70. $this->assertSame( $t, $found->term_id );
  71. }
  72. /**
  73. * @ticket 30620
  74. */
  75. public function test_taxonomy_should_be_ignored_if_matching_by_term_taxonomy_id() {
  76. global $wpdb;
  77. register_taxonomy( 'wptests_tax', 'post' );
  78. $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
  79. $term = get_term( $t, 'wptests_tax' );
  80. $new_ttid = $term->term_taxonomy_id + 1;
  81. // Offset just to be sure.
  82. $wpdb->update(
  83. $wpdb->term_taxonomy,
  84. array( 'term_taxonomy_id' => $new_ttid ),
  85. array( 'term_id' => $t )
  86. );
  87. $found = get_term_by( 'term_taxonomy_id', $new_ttid, 'foo' );
  88. $this->assertSame( $t, $found->term_id );
  89. }
  90. /**
  91. * @ticket 14162
  92. */
  93. public function test_should_prime_term_cache() {
  94. global $wpdb;
  95. register_taxonomy( 'wptests_tax', 'post' );
  96. $t = self::factory()->term->create(
  97. array(
  98. 'taxonomy' => 'wptests_tax',
  99. 'slug' => 'foo',
  100. )
  101. );
  102. clean_term_cache( $t, 'wptests_tax' );
  103. $num_queries = $wpdb->num_queries;
  104. $found = get_term_by( 'slug', 'foo', 'wptests_tax' );
  105. $num_queries++;
  106. $this->assertTrue( $found instanceof WP_Term );
  107. $this->assertSame( $t, $found->term_id );
  108. $this->assertSame( $num_queries, $wpdb->num_queries );
  109. // Calls to `get_term()` should now hit cache.
  110. $found2 = get_term( $t );
  111. $this->assertSame( $t, $found->term_id );
  112. $this->assertSame( $num_queries, $wpdb->num_queries );
  113. }
  114. /**
  115. * @ticket 21760
  116. */
  117. public function test_should_unslash_name() {
  118. register_taxonomy( 'wptests_tax', 'post' );
  119. $term_name = 'Foo " \o/';
  120. $term_name_slashed = wp_slash( $term_name );
  121. $t = self::factory()->term->create(
  122. array(
  123. 'taxonomy' => 'wptests_tax',
  124. 'name' => $term_name_slashed,
  125. )
  126. );
  127. $found = get_term_by( 'name', $term_name_slashed, 'wptests_tax' );
  128. $this->assertTrue( $found instanceof WP_Term );
  129. $this->assertSame( $t, $found->term_id );
  130. $this->assertSame( $term_name, $found->name );
  131. }
  132. /**
  133. * @ticket 21760
  134. */
  135. public function test_should_sanitize_slug() {
  136. register_taxonomy( 'wptests_tax', 'post' );
  137. $t1 = self::factory()->term->create(
  138. array(
  139. 'taxonomy' => 'wptests_tax',
  140. 'slug' => 'foo-foo',
  141. )
  142. );
  143. // Whitespace should get replaced by a '-'.
  144. $found1 = get_term_by( 'slug', 'foo foo', 'wptests_tax' );
  145. $this->assertTrue( $found1 instanceof WP_Term );
  146. $this->assertSame( $t1, $found1->term_id );
  147. $t2 = self::factory()->term->create(
  148. array(
  149. 'taxonomy' => 'wptests_tax',
  150. 'slug' => '%e4%bb%aa%e8%a1%a8%e7%9b%98',
  151. )
  152. );
  153. // Slug should get urlencoded.
  154. $found2 = get_term_by( 'slug', '仪表盘', 'wptests_tax' );
  155. $this->assertTrue( $found2 instanceof WP_Term );
  156. $this->assertSame( $t2, $found2->term_id );
  157. }
  158. /**
  159. * @ticket 21760
  160. */
  161. public function test_query_should_not_contain_order_by_clause() {
  162. global $wpdb;
  163. $term_id = $this->factory->term->create(
  164. array(
  165. 'name' => 'burrito',
  166. 'taxonomy' => 'post_tag',
  167. )
  168. );
  169. $found = get_term_by( 'name', 'burrito', 'post_tag' );
  170. $this->assertSame( $term_id, $found->term_id );
  171. $this->assertNotContains( 'ORDER BY', $wpdb->last_query );
  172. }
  173. /**
  174. * @ticket 21760
  175. */
  176. public function test_query_should_contain_limit_clause() {
  177. global $wpdb;
  178. $term_id = $this->factory->term->create(
  179. array(
  180. 'name' => 'burrito',
  181. 'taxonomy' => 'post_tag',
  182. )
  183. );
  184. $found = get_term_by( 'name', 'burrito', 'post_tag' );
  185. $this->assertSame( $term_id, $found->term_id );
  186. $this->assertContains( 'LIMIT 1', $wpdb->last_query );
  187. }
  188. /**
  189. * @ticket 21760
  190. */
  191. public function test_prevent_recursion_by_get_terms_filter() {
  192. $action = new MockAction();
  193. add_filter( 'get_terms', array( $action, 'filter' ) );
  194. get_term_by( 'name', 'burrito', 'post_tag' );
  195. remove_filter( 'get_terms', array( $action, 'filter' ) );
  196. $this->assertSame( 0, $action->get_call_count() );
  197. }
  198. /**
  199. * @ticket 21760
  200. */
  201. public function test_get_term_by_name_with_string_0() {
  202. register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
  203. $term_id = $this->factory->term->create(
  204. array(
  205. 'name' => '0',
  206. 'taxonomy' => 'wptests_tax',
  207. )
  208. );
  209. $found = get_term_by( 'name', '0', 'wptests_tax' );
  210. $this->assertSame( $term_id, $found->term_id );
  211. }
  212. /**
  213. * @ticket 21760
  214. */
  215. public function test_get_term_by_slug_with_string_0() {
  216. register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
  217. $term_id = $this->factory->term->create(
  218. array(
  219. 'taxonomy' => 'wptests_tax',
  220. 'name' => '0',
  221. 'slug' => '0',
  222. )
  223. );
  224. $found = get_term_by( 'slug', '0', 'wptests_tax' );
  225. $this->assertSame( $term_id, $found->term_id );
  226. }
  227. /**
  228. * @ticket 21760
  229. */
  230. public function test_get_term_by_with_empty_string() {
  231. register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
  232. $found_by_slug = get_term_by( 'slug', '', 'wptests_tax' );
  233. $found_by_name = get_term_by( 'name', '', 'wptests_tax' );
  234. $this->assertFalse( $found_by_slug );
  235. $this->assertFalse( $found_by_name );
  236. }
  237. }