term.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. <?php
  2. /**
  3. * @group taxonomy
  4. */
  5. class Tests_Term extends WP_UnitTestCase {
  6. var $taxonomy = 'category';
  7. function setUp() {
  8. parent::setUp();
  9. _clean_term_filters();
  10. // insert one term into every post taxonomy
  11. // otherwise term_ids and term_taxonomy_ids might be identical, which could mask bugs
  12. $term = rand_str();
  13. foreach(get_object_taxonomies('post') as $tax)
  14. wp_insert_term( $term, $tax );
  15. }
  16. function deleted_term_cb( $term, $tt_id, $taxonomy, $deleted_term ) {
  17. $this->assertInternalType( 'object', $deleted_term );
  18. $this->assertInternalType( 'int', $term );
  19. // Pesky string $this->assertInternalType( 'int', $tt_id );
  20. $this->assertEquals( $term, $deleted_term->term_id );
  21. $this->assertEquals( $taxonomy, $deleted_term->taxonomy );
  22. $this->assertEquals( $tt_id, $deleted_term->term_taxonomy_id );
  23. }
  24. function test_wp_insert_delete_term() {
  25. // a new unused term
  26. $term = rand_str();
  27. $this->assertNull( term_exists($term) );
  28. $initial_count = wp_count_terms( $this->taxonomy );
  29. $t = wp_insert_term( $term, $this->taxonomy );
  30. $this->assertInternalType( 'array', $t );
  31. $this->assertFalse( is_wp_error($t) );
  32. $this->assertTrue( $t['term_id'] > 0 );
  33. $this->assertTrue( $t['term_taxonomy_id'] > 0 );
  34. $this->assertEquals( $initial_count + 1, wp_count_terms($this->taxonomy) );
  35. // make sure the term exists
  36. $this->assertTrue( term_exists($term) > 0 );
  37. $this->assertTrue( term_exists($t['term_id']) > 0 );
  38. // now delete it
  39. add_filter( 'delete_term', array( $this, 'deleted_term_cb' ), 10, 4 );
  40. $this->assertTrue( wp_delete_term($t['term_id'], $this->taxonomy) );
  41. remove_filter( 'delete_term', array( $this, 'deleted_term_cb' ), 10, 4 );
  42. $this->assertNull( term_exists($term) );
  43. $this->assertNull( term_exists($t['term_id']) );
  44. $this->assertEquals( $initial_count, wp_count_terms($this->taxonomy) );
  45. }
  46. function test_term_exists_known() {
  47. // insert a term
  48. $term = rand_str();
  49. $t = wp_insert_term( $term, $this->taxonomy );
  50. $this->assertInternalType( 'array', $t );
  51. $this->assertEquals( $t['term_id'], term_exists($t['term_id']) );
  52. $this->assertEquals( $t['term_id'], term_exists($term) );
  53. // clean up
  54. $this->assertTrue( wp_delete_term($t['term_id'], $this->taxonomy) );
  55. }
  56. function test_term_exists_unknown() {
  57. $this->assertNull( term_exists(rand_str()) );
  58. $this->assertEquals( 0, term_exists(0) );
  59. $this->assertEquals( 0, term_exists('') );
  60. $this->assertEquals( 0, term_exists(NULL) );
  61. }
  62. /**
  63. * @ticket 5381
  64. */
  65. function test_is_term_type() {
  66. // insert a term
  67. $term = rand_str();
  68. $t = wp_insert_term( $term, $this->taxonomy );
  69. $this->assertInternalType( 'array', $t );
  70. $term_obj = get_term_by('name', $term, $this->taxonomy);
  71. $this->assertEquals( $t['term_id'], term_exists($term_obj->slug) );
  72. // clean up
  73. $this->assertTrue( wp_delete_term($t['term_id'], $this->taxonomy) );
  74. }
  75. /**
  76. * @ticket 26339
  77. */
  78. function test_get_object_terms() {
  79. $post_id = $this->factory->post->create();
  80. $terms_1 = array('foo', 'bar', 'baz');
  81. wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
  82. add_filter( 'wp_get_object_terms', array( $this, 'filter_get_object_terms' ) );
  83. $terms = wp_get_object_terms( $post_id, $this->taxonomy );
  84. remove_filter( 'wp_get_object_terms', array( $this, 'filter_get_object_terms' ) );
  85. foreach ( $terms as $term ) {
  86. $this->assertInternalType( 'object', $term );
  87. }
  88. }
  89. function filter_get_object_terms( $terms ) {
  90. $term_ids = wp_list_pluck( $terms, 'term_id' );
  91. // all terms should still be objects
  92. return $terms;
  93. }
  94. function test_set_object_terms_by_id() {
  95. $ids = $this->factory->post->create_many(5);
  96. $terms = array();
  97. for ($i=0; $i<3; $i++ ) {
  98. $term = rand_str();
  99. $result = wp_insert_term( $term, $this->taxonomy );
  100. $this->assertInternalType( 'array', $result );
  101. $term_id[$term] = $result['term_id'];
  102. }
  103. foreach ($ids as $id) {
  104. $tt = wp_set_object_terms( $id, array_values($term_id), $this->taxonomy );
  105. // should return three term taxonomy ids
  106. $this->assertEquals( 3, count($tt) );
  107. }
  108. // each term should be associated with every post
  109. foreach ($term_id as $term=>$id) {
  110. $actual = get_objects_in_term($id, $this->taxonomy);
  111. $this->assertEquals( $ids, array_map('intval', $actual) );
  112. }
  113. // each term should have a count of 5
  114. foreach (array_keys($term_id) as $term) {
  115. $t = get_term_by('name', $term, $this->taxonomy);
  116. $this->assertEquals( 5, $t->count );
  117. }
  118. }
  119. function test_set_object_terms_by_name() {
  120. $ids = $this->factory->post->create_many(5);
  121. $terms = array(
  122. rand_str(),
  123. rand_str(),
  124. rand_str());
  125. foreach ($ids as $id) {
  126. $tt = wp_set_object_terms( $id, $terms, $this->taxonomy );
  127. // should return three term taxonomy ids
  128. $this->assertEquals( 3, count($tt) );
  129. // remember which term has which term_id
  130. for ($i=0; $i<3; $i++) {
  131. $term = get_term_by('name', $terms[$i], $this->taxonomy);
  132. $term_id[$terms[$i]] = intval($term->term_id);
  133. }
  134. }
  135. // each term should be associated with every post
  136. foreach ($term_id as $term=>$id) {
  137. $actual = get_objects_in_term($id, $this->taxonomy);
  138. $this->assertEquals( $ids, array_map('intval', $actual) );
  139. }
  140. // each term should have a count of 5
  141. foreach ($terms as $term) {
  142. $t = get_term_by('name', $term, $this->taxonomy);
  143. $this->assertEquals( 5, $t->count );
  144. }
  145. }
  146. function test_change_object_terms_by_name() {
  147. // set some terms on an object; then change them while leaving one intact
  148. $post_id = $this->factory->post->create();
  149. $terms_1 = array('foo', 'bar', 'baz');
  150. $terms_2 = array('bar', 'bing');
  151. // set the initial terms
  152. $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
  153. $this->assertEquals( 3, count($tt_1) );
  154. // make sure they're correct
  155. $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id'));
  156. $this->assertEquals( $terms_1, $terms );
  157. // change the terms
  158. $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy );
  159. $this->assertEquals( 2, count($tt_2) );
  160. // make sure they're correct
  161. $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id'));
  162. $this->assertEquals( $terms_2, $terms );
  163. // make sure the tt id for 'bar' matches
  164. $this->assertEquals( $tt_1[1], $tt_2[0] );
  165. }
  166. /**
  167. * @ticket 22560
  168. */
  169. function test_object_term_cache() {
  170. $post_id = $this->factory->post->create();
  171. $terms_1 = array('foo', 'bar', 'baz');
  172. $terms_2 = array('bar', 'bing');
  173. // Cache should be empty after a set.
  174. $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
  175. $this->assertEquals( 3, count($tt_1) );
  176. $this->assertFalse( wp_cache_get( $post_id, $this->taxonomy . '_relationships') );
  177. // wp_get_object_terms() does not prime the cache.
  178. wp_get_object_terms( $post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't.term_id') );
  179. $this->assertFalse( wp_cache_get( $post_id, $this->taxonomy . '_relationships') );
  180. // get_the_terms() does prime the cache.
  181. $terms = get_the_terms( $post_id, $this->taxonomy );
  182. $cache = wp_cache_get( $post_id, $this->taxonomy . '_relationships');
  183. $this->assertInternalType( 'array', $cache );
  184. // Cache should be empty after a set.
  185. $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy );
  186. $this->assertEquals( 2, count($tt_2) );
  187. $this->assertFalse( wp_cache_get( $post_id, $this->taxonomy . '_relationships') );
  188. }
  189. function test_change_object_terms_by_id() {
  190. // set some terms on an object; then change them while leaving one intact
  191. $post_id = $this->factory->post->create();
  192. // first set: 3 terms
  193. $terms_1 = array();
  194. for ($i=0; $i<3; $i++ ) {
  195. $term = rand_str();
  196. $result = wp_insert_term( $term, $this->taxonomy );
  197. $this->assertInternalType( 'array', $result );
  198. $terms_1[$i] = $result['term_id'];
  199. }
  200. // second set: one of the original terms, plus one new term
  201. $terms_2 = array();
  202. $terms_2[0] = $terms_1[1];
  203. $term = rand_str();
  204. $result = wp_insert_term( $term, $this->taxonomy );
  205. $terms_2[1] = $result['term_id'];
  206. // set the initial terms
  207. $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
  208. $this->assertEquals( 3, count($tt_1) );
  209. // make sure they're correct
  210. $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 't.term_id'));
  211. $this->assertEquals( $terms_1, $terms );
  212. // change the terms
  213. $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy );
  214. $this->assertEquals( 2, count($tt_2) );
  215. // make sure they're correct
  216. $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 't.term_id'));
  217. $this->assertEquals( $terms_2, $terms );
  218. // make sure the tt id for 'bar' matches
  219. $this->assertEquals( $tt_1[1], $tt_2[0] );
  220. }
  221. function test_get_object_terms_by_slug() {
  222. $post_id = $this->factory->post->create();
  223. $terms_1 = array('Foo', 'Bar', 'Baz');
  224. $terms_1_slugs = array('foo', 'bar', 'baz');
  225. // set the initial terms
  226. $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
  227. $this->assertEquals( 3, count($tt_1) );
  228. // make sure they're correct
  229. $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'slugs', 'orderby' => 't.term_id'));
  230. $this->assertEquals( $terms_1_slugs, $terms );
  231. }
  232. function test_set_object_terms_invalid() {
  233. $post_id = $this->factory->post->create();
  234. // bogus taxonomy
  235. $result = wp_set_object_terms( $post_id, array(rand_str()), rand_str() );
  236. $this->assertTrue( is_wp_error($result) );
  237. }
  238. /**
  239. * @ticket 15475
  240. */
  241. function test_wp_add_remove_object_terms() {
  242. $posts = $this->factory->post->create_many( 5 );
  243. $tags = $this->factory->tag->create_many( 5 );
  244. $tt = wp_add_object_terms( $posts[0], $tags[1], 'post_tag' );
  245. $this->assertEquals( 1, count( $tt ) );
  246. $this->assertEquals( array( $tags[1] ), wp_get_object_terms( $posts[0], 'post_tag', array( 'fields' => 'ids' ) ) );
  247. $three_tags = array( $tags[0], $tags[1], $tags[2] );
  248. $tt = wp_add_object_terms( $posts[1], $three_tags, 'post_tag' );
  249. $this->assertEquals( 3, count( $tt ) );
  250. $this->assertEquals( $three_tags, wp_get_object_terms( $posts[1], 'post_tag', array( 'fields' => 'ids' ) ) );
  251. $this->assertTrue( wp_remove_object_terms( $posts[0], $tags[1], 'post_tag' ) );
  252. $this->assertFalse( wp_remove_object_terms( $posts[0], $tags[0], 'post_tag' ) );
  253. $this->assertInstanceOf( 'WP_Error', wp_remove_object_terms( $posts[0], $tags[1], 'non_existing_taxonomy' ) );
  254. $this->assertTrue( wp_remove_object_terms( $posts[1], $three_tags, 'post_tag' ) );
  255. $this->assertEquals( 0, count( wp_get_object_terms( $posts[1], 'post_tag' ) ) );
  256. foreach ( $tags as $term_id )
  257. $this->assertTrue( wp_delete_term( $term_id, 'post_tag' ) );
  258. foreach ( $posts as $post_id )
  259. $this->assertTrue( (bool) wp_delete_post( $post_id, true ) );
  260. }
  261. function test_term_is_ancestor_of( ) {
  262. $term = rand_str();
  263. $term2 = rand_str();
  264. $t = wp_insert_term( $term, 'category' );
  265. $this->assertInternalType( 'array', $t );
  266. $t2 = wp_insert_term( $term, 'category', array( 'parent' => $t['term_id'] ) );
  267. $this->assertInternalType( 'array', $t2 );
  268. if ( function_exists( 'term_is_ancestor_of' ) ) {
  269. $this->assertTrue( term_is_ancestor_of( $t['term_id'], $t2['term_id'], 'category' ) );
  270. $this->assertFalse( term_is_ancestor_of( $t2['term_id'], $t['term_id'], 'category' ) );
  271. }
  272. $this->assertTrue( cat_is_ancestor_of( $t['term_id'], $t2['term_id']) );
  273. $this->assertFalse( cat_is_ancestor_of( $t2['term_id'], $t['term_id']) );
  274. wp_delete_term($t['term_id'], 'category');
  275. wp_delete_term($t2['term_id'], 'category');
  276. }
  277. function test_wp_insert_delete_category() {
  278. $term = rand_str();
  279. $this->assertNull( category_exists( $term ) );
  280. $initial_count = wp_count_terms( 'category' );
  281. $t = wp_insert_category( array( 'cat_name' => $term ) );
  282. $this->assertTrue( is_numeric($t) );
  283. $this->assertFalse( is_wp_error($t) );
  284. $this->assertTrue( $t > 0 );
  285. $this->assertEquals( $initial_count + 1, wp_count_terms( 'category' ) );
  286. // make sure the term exists
  287. $this->assertTrue( term_exists($term) > 0 );
  288. $this->assertTrue( term_exists($t) > 0 );
  289. // now delete it
  290. $this->assertTrue( wp_delete_category($t) );
  291. $this->assertNull( term_exists($term) );
  292. $this->assertNull( term_exists($t) );
  293. $this->assertEquals( $initial_count, wp_count_terms('category') );
  294. }
  295. function test_wp_unique_term_slug() {
  296. // set up test data
  297. $a = wp_insert_term( 'parent', $this->taxonomy );
  298. $this->assertInternalType( 'array', $a );
  299. $b = wp_insert_term( 'child', $this->taxonomy, array( 'parent' => $a['term_id'] ) );
  300. $this->assertInternalType( 'array', $b );
  301. $c = wp_insert_term( 'neighbor', $this->taxonomy );
  302. $this->assertInternalType( 'array', $c );
  303. $d = wp_insert_term( 'pet', $this->taxonomy, array( 'parent' => $c['term_id'] ) );
  304. $this->assertInternalType( 'array', $c );
  305. $a_term = get_term( $a['term_id'], $this->taxonomy );
  306. $b_term = get_term( $b['term_id'], $this->taxonomy );
  307. $c_term = get_term( $c['term_id'], $this->taxonomy );
  308. $d_term = get_term( $d['term_id'], $this->taxonomy );
  309. // a unique slug gets unchanged
  310. $this->assertEquals( 'unique-term', wp_unique_term_slug( 'unique-term', $c_term ) );
  311. // a non-hierarchicial dupe gets suffixed with "-#"
  312. $this->assertEquals( 'parent-2', wp_unique_term_slug( 'parent', $c_term ) );
  313. // a hierarchical dupe initially gets suffixed with its parent term
  314. $this->assertEquals( 'child-neighbor', wp_unique_term_slug( 'child', $d_term ) );
  315. // a hierarchical dupe whose parent already contains the {term}-{parent term}
  316. // term gets suffixed with parent term name and then '-#'
  317. $e = wp_insert_term( 'child-neighbor', $this->taxonomy, array( 'parent' => $c['term_id'] ) );
  318. $this->assertEquals( 'child-neighbor-2', wp_unique_term_slug( 'child', $d_term ) );
  319. $f = wp_insert_term( 'foo', $this->taxonomy );
  320. $this->assertInternalType( 'array', $f );
  321. $f_term = get_term( $f['term_id'], $this->taxonomy );
  322. $this->assertEquals( 'foo', $f_term->slug );
  323. $this->assertEquals( 'foo', wp_unique_term_slug( 'foo', $f_term ) );
  324. $g = wp_insert_term( 'foo', $this->taxonomy );
  325. $this->assertInstanceOf( 'WP_Error', $g );
  326. // clean up
  327. foreach ( array( $a, $b, $c, $d, $e, $f ) as $t )
  328. $this->assertTrue( wp_delete_term( $t['term_id'], $this->taxonomy ) );
  329. }
  330. /**
  331. * @ticket 5809
  332. */
  333. function test_update_shared_term() {
  334. $random_tax = __FUNCTION__;
  335. register_taxonomy( $random_tax, 'post' );
  336. $post_id = $this->factory->post->create();
  337. $old_name = 'Initial';
  338. $t1 = wp_insert_term( $old_name, 'category' );
  339. $t2 = wp_insert_term( $old_name, 'post_tag' );
  340. $this->assertEquals( $t1['term_id'], $t2['term_id'] );
  341. wp_set_post_categories( $post_id, array( $t1['term_id'] ) );
  342. wp_set_post_tags( $post_id, array( (int) $t2['term_id'] ) );
  343. $new_name = 'Updated';
  344. // create the term in a third taxonomy, just to keep things interesting
  345. $t3 = wp_insert_term( $old_name, $random_tax );
  346. wp_set_post_terms( $post_id, array( (int) $t3['term_id'] ), $random_tax );
  347. $this->assertPostHasTerms( $post_id, array( $t3['term_id'] ), $random_tax );
  348. $t2_updated = wp_update_term( $t2['term_id'], 'post_tag', array(
  349. 'name' => $new_name
  350. ) );
  351. $this->assertNotEquals( $t2_updated['term_id'], $t3['term_id'] );
  352. // make sure the terms have split
  353. $this->assertEquals( $old_name, get_term_field( 'name', $t1['term_id'], 'category' ) );
  354. $this->assertEquals( $new_name, get_term_field( 'name', $t2_updated['term_id'], 'post_tag' ) );
  355. // and that they are still assigned to the correct post
  356. $this->assertPostHasTerms( $post_id, array( $t1['term_id'] ), 'category' );
  357. $this->assertPostHasTerms( $post_id, array( $t2_updated['term_id'] ), 'post_tag' );
  358. $this->assertPostHasTerms( $post_id, array( $t3['term_id'] ), $random_tax );
  359. // clean up
  360. unset( $GLOBALS['wp_taxonomies'][ $random_tax ] );
  361. }
  362. /**
  363. * @ticket 17646
  364. */
  365. function test_get_object_terms_types() {
  366. $post_id = $this->factory->post->create();
  367. $term = wp_insert_term( 'one', $this->taxonomy );
  368. wp_set_object_terms( $post_id, $term, $this->taxonomy );
  369. $terms = wp_get_object_terms( $post_id, $this->taxonomy, array( 'fields' => 'all_with_object_id' ) );
  370. $term = array_shift( $terms );
  371. $int_fields = array( 'parent', 'term_id', 'count', 'term_group', 'term_taxonomy_id', 'object_id' );
  372. foreach ( $int_fields as $field )
  373. $this->assertInternalType( 'int', $term->$field, $field );
  374. $terms = wp_get_object_terms( $post_id, $this->taxonomy, array( 'fields' => 'ids' ) );
  375. $term = array_shift( $terms );
  376. $this->assertInternalType( 'int', $term, 'term' );
  377. }
  378. /**
  379. * @ticket 25852
  380. */
  381. function test_sanitize_term_field() {
  382. $term = wp_insert_term( 'foo', $this->taxonomy );
  383. $this->assertEquals( 0, sanitize_term_field( 'parent', 0, $term['term_id'], $this->taxonomy, 'raw' ) );
  384. $this->assertEquals( 1, sanitize_term_field( 'parent', 1, $term['term_id'], $this->taxonomy, 'raw' ) );
  385. $this->assertEquals( 0, sanitize_term_field( 'parent', -1, $term['term_id'], $this->taxonomy, 'raw' ) );
  386. $this->assertEquals( 0, sanitize_term_field( 'parent', '', $term['term_id'], $this->taxonomy, 'raw' ) );
  387. }
  388. private function assertPostHasTerms( $post_id, $expected_term_ids, $taxonomy ) {
  389. $assigned_term_ids = wp_get_object_terms( $post_id, $taxonomy, array(
  390. 'fields' => 'ids'
  391. ) );
  392. $this->assertEquals( $expected_term_ids, $assigned_term_ids );
  393. }
  394. /**
  395. * @ticket 24189
  396. */
  397. function test_object_term_cache_when_term_changes() {
  398. $post_id = $this->factory->post->create();
  399. $tag_id = $this->factory->tag->create( array( 'description' => 'My Amazing Tag' ) );
  400. $tt_1 = wp_set_object_terms( $post_id, $tag_id, 'post_tag' );
  401. $terms = get_the_terms( $post_id, 'post_tag' );
  402. $this->assertEquals( $tag_id, $terms[0]->term_id );
  403. $this->assertEquals( 'My Amazing Tag', $terms[0]->description );
  404. $_updated = wp_update_term( $tag_id, 'post_tag', array(
  405. 'description' => 'This description is even more amazing!'
  406. ) );
  407. $_new_term = get_term( $tag_id, 'post_tag' );
  408. $this->assertEquals( $tag_id, $_new_term->term_id );
  409. $this->assertEquals( 'This description is even more amazing!', $_new_term->description );
  410. $terms = get_the_terms( $post_id, 'post_tag' );
  411. $this->assertEquals( $tag_id, $terms[0]->term_id );
  412. $this->assertEquals( 'This description is even more amazing!', $terms[0]->description );
  413. }
  414. function test_wp_set_post_categories() {
  415. $post_id = $this->factory->post->create();
  416. $post = get_post( $post_id );
  417. $this->assertInternalType( 'array', $post->post_category );
  418. $this->assertEquals( 1, count( $post->post_category ) );
  419. $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
  420. $term1 = wp_insert_term( 'Foo', 'category' );
  421. $term2 = wp_insert_term( 'Bar', 'category' );
  422. $term3 = wp_insert_term( 'Baz', 'category' );
  423. wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ) );
  424. $this->assertEquals( 2, count( $post->post_category ) );
  425. $this->assertEquals( array( $term2['term_id'], $term1['term_id'] ) , $post->post_category );
  426. wp_set_post_categories( $post_id, $term3['term_id'], true );
  427. $this->assertEquals( array( $term2['term_id'], $term3['term_id'], $term1['term_id'] ) , $post->post_category );
  428. $term4 = wp_insert_term( 'Burrito', 'category' );
  429. wp_set_post_categories( $post_id, $term4['term_id'] );
  430. $this->assertEquals( array( $term4['term_id'] ), $post->post_category );
  431. wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ), true );
  432. $this->assertEquals( array( $term2['term_id'], $term4['term_id'], $term1['term_id'] ), $post->post_category );
  433. wp_set_post_categories( $post_id, array(), true );
  434. $this->assertEquals( 1, count( $post->post_category ) );
  435. $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
  436. wp_set_post_categories( $post_id, array() );
  437. $this->assertEquals( 1, count( $post->post_category ) );
  438. $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
  439. }
  440. function test_get_term_by_tt_id() {
  441. $term1 = wp_insert_term( 'Foo', 'category' );
  442. $term2 = get_term_by( 'term_taxonomy_id', $term1['term_taxonomy_id'], 'category' );
  443. $this->assertEquals( get_term( $term1['term_id'], 'category' ), $term2 );
  444. }
  445. function test_wp_count_terms() {
  446. $count = wp_count_terms( 'category', array( 'hide_empty' => true ) );
  447. // the terms inserted in setUp aren't attached to any posts, so should return 0
  448. // this previously returned 2
  449. $this->assertEquals( 0, $count );
  450. }
  451. }