taxonomy.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * @group taxonomy
  4. */
  5. class Tests_Taxonomy extends WP_UnitTestCase {
  6. function test_get_post_taxonomies() {
  7. $this->assertEquals(array('category', 'post_tag', 'post_format'), get_object_taxonomies('post'));
  8. }
  9. function test_get_link_taxonomies() {
  10. $this->assertEquals(array('link_category'), get_object_taxonomies('link'));
  11. }
  12. /**
  13. * @ticket 5417
  14. */
  15. function test_get_unknown_taxonomies() {
  16. // taxonomies for an unknown object type
  17. $this->assertEquals( array(), get_object_taxonomies(rand_str()) );
  18. $this->assertEquals( array(), get_object_taxonomies('') );
  19. $this->assertEquals( array(), get_object_taxonomies(0) );
  20. $this->assertEquals( array(), get_object_taxonomies(NULL) );
  21. }
  22. function test_get_post_taxonomy() {
  23. foreach ( get_object_taxonomies('post') as $taxonomy ) {
  24. $tax = get_taxonomy($taxonomy);
  25. // should return an object with the correct taxonomy object type
  26. $this->assertTrue( is_object( $tax ) );
  27. $this->assertTrue( is_array( $tax->object_type ) );
  28. $this->assertEquals( array( 'post' ), $tax->object_type );
  29. }
  30. }
  31. function test_get_link_taxonomy() {
  32. foreach ( get_object_taxonomies('link') as $taxonomy ) {
  33. $tax = get_taxonomy($taxonomy);
  34. // should return an object with the correct taxonomy object type
  35. $this->assertTrue( is_object($tax) );
  36. $this->assertTrue( is_array( $tax->object_type ) );
  37. $this->assertEquals( array( 'link' ), $tax->object_type );
  38. }
  39. }
  40. function test_taxonomy_exists_known() {
  41. $this->assertTrue( taxonomy_exists('category') );
  42. $this->assertTrue( taxonomy_exists('post_tag') );
  43. $this->assertTrue( taxonomy_exists('link_category') );
  44. }
  45. function test_taxonomy_exists_unknown() {
  46. $this->assertFalse( taxonomy_exists(rand_str()) );
  47. $this->assertFalse( taxonomy_exists('') );
  48. $this->assertFalse( taxonomy_exists(0) );
  49. $this->assertFalse( taxonomy_exists(NULL) );
  50. }
  51. function test_is_taxonomy_hierarchical() {
  52. $this->assertTrue( is_taxonomy_hierarchical('category') );
  53. $this->assertFalse( is_taxonomy_hierarchical('post_tag') );
  54. $this->assertFalse( is_taxonomy_hierarchical('link_category') );
  55. }
  56. function test_is_taxonomy_hierarchical_unknown() {
  57. $this->assertFalse( is_taxonomy_hierarchical(rand_str()) );
  58. $this->assertFalse( is_taxonomy_hierarchical('') );
  59. $this->assertFalse( is_taxonomy_hierarchical(0) );
  60. $this->assertFalse( is_taxonomy_hierarchical(NULL) );
  61. }
  62. function test_register_taxonomy() {
  63. // make up a new taxonomy name, and ensure it's unused
  64. $tax = rand_str();
  65. $this->assertFalse( taxonomy_exists($tax) );
  66. register_taxonomy( $tax, 'post' );
  67. $this->assertTrue( taxonomy_exists($tax) );
  68. $this->assertFalse( is_taxonomy_hierarchical($tax) );
  69. // clean up
  70. unset($GLOBALS['wp_taxonomies'][$tax]);
  71. }
  72. function test_register_hierarchical_taxonomy() {
  73. // make up a new taxonomy name, and ensure it's unused
  74. $tax = rand_str();
  75. $this->assertFalse( taxonomy_exists($tax) );
  76. register_taxonomy( $tax, 'post', array('hierarchical'=>true) );
  77. $this->assertTrue( taxonomy_exists($tax) );
  78. $this->assertTrue( is_taxonomy_hierarchical($tax) );
  79. // clean up
  80. unset($GLOBALS['wp_taxonomies'][$tax]);
  81. }
  82. /**
  83. * @ticket 21593
  84. */
  85. function test_register_long_taxonomy() {
  86. $this->assertInstanceOf( 'WP_Error', register_taxonomy( 'abcdefghijklmnopqrstuvwxyz0123456789', 'post', array() ) );
  87. }
  88. /**
  89. * @ticket 11058
  90. */
  91. function test_registering_taxonomies_to_object_types() {
  92. // Create a taxonomy to test with
  93. $tax = 'test_tax';
  94. $this->assertFalse( taxonomy_exists($tax) );
  95. register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
  96. // Create a post type to test with
  97. $post_type = 'test_cpt';
  98. $this->assertFalse( get_post_type( $post_type ) );
  99. $this->assertObjectHasAttribute( 'name', register_post_type( $post_type ) );
  100. // Core taxonomy, core post type
  101. $this->assertTrue( unregister_taxonomy_for_object_type( 'category', 'post' ) );
  102. $this->assertFalse( unregister_taxonomy_for_object_type( 'category', 'post' ) );
  103. $this->assertTrue( register_taxonomy_for_object_type( 'category', 'post' ) );
  104. // Core taxonomy, non-core post type
  105. $this->assertTrue( register_taxonomy_for_object_type( 'category', $post_type ) );
  106. $this->assertTrue( unregister_taxonomy_for_object_type( 'category', $post_type ) );
  107. $this->assertFalse( unregister_taxonomy_for_object_type( 'category', $post_type ) );
  108. $this->assertTrue( register_taxonomy_for_object_type( 'category', $post_type ) );
  109. // Core taxonomies, non-post object types
  110. $this->assertFalse( register_taxonomy_for_object_type( 'category', 'user' ) );
  111. $this->assertFalse( unregister_taxonomy_for_object_type( 'category', 'user' ) );
  112. // Non-core taxonomy, core post type
  113. $this->assertTrue( unregister_taxonomy_for_object_type( $tax, 'post' ) );
  114. $this->assertFalse( unregister_taxonomy_for_object_type( $tax, 'post' ) );
  115. $this->assertTrue( register_taxonomy_for_object_type( $tax, 'post' ) );
  116. // Non-core taxonomy, non-core post type
  117. $this->assertTrue( register_taxonomy_for_object_type( $tax, $post_type ) );
  118. $this->assertTrue( unregister_taxonomy_for_object_type( $tax, $post_type ) );
  119. $this->assertFalse( unregister_taxonomy_for_object_type( $tax, $post_type ) );
  120. $this->assertTrue( register_taxonomy_for_object_type( $tax, $post_type ) );
  121. // Non-core taxonomies, non-post object types
  122. $this->assertFalse( register_taxonomy_for_object_type( $tax, 'user' ) );
  123. $this->assertFalse( unregister_taxonomy_for_object_type( $tax, 'user' ) );
  124. unset($GLOBALS['wp_taxonomies'][$tax]);
  125. _unregister_post_type( $post_type );
  126. }
  127. /**
  128. * @ticket 25706
  129. */
  130. function test_in_category() {
  131. $post = $this->factory->post->create_and_get();
  132. // in_category() returns false when first parameter is empty()
  133. $this->assertFalse( in_category( '', $post ) );
  134. $this->assertFalse( in_category( false, $post ) );
  135. $this->assertFalse( in_category( null, $post ) );
  136. // Test expected behavior of in_category()
  137. $term = wp_insert_term( 'Test', 'category' );
  138. wp_set_object_terms( $post->ID, $term['term_id'], 'category' );
  139. $this->assertTrue( in_category( $term['term_id'], $post ) );
  140. }
  141. }