wpTaxonomy.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * @group taxonomy
  4. */
  5. class Tests_WP_Taxonomy extends WP_UnitTestCase {
  6. public function test_instances() {
  7. global $wp_taxonomies;
  8. foreach ( $wp_taxonomies as $taxonomy ) {
  9. $this->assertInstanceOf( 'WP_Taxonomy', $taxonomy );
  10. }
  11. }
  12. public function test_does_not_add_query_var_if_not_public() {
  13. $this->set_permalink_structure( '/%postname%' );
  14. /* @var WP $wp */
  15. global $wp;
  16. $taxonomy = rand_str();
  17. $taxonomy_object = new WP_Taxonomy( $taxonomy, 'post' );
  18. $taxonomy_object->add_rewrite_rules();
  19. $this->assertFalse( in_array( 'foobar', $wp->public_query_vars, true ) );
  20. }
  21. public function test_adds_query_var_if_public() {
  22. $this->set_permalink_structure( '/%postname%' );
  23. /* @var WP $wp */
  24. global $wp;
  25. $taxonomy = rand_str();
  26. $taxonomy_object = new WP_Taxonomy(
  27. $taxonomy,
  28. 'post',
  29. array(
  30. 'public' => true,
  31. 'rewrite' => false,
  32. 'query_var' => 'foobar',
  33. )
  34. );
  35. $taxonomy_object->add_rewrite_rules();
  36. $in_array = in_array( 'foobar', $wp->public_query_vars, true );
  37. $taxonomy_object->remove_rewrite_rules();
  38. $in_array_after = in_array( 'foobar', $wp->public_query_vars, true );
  39. $this->assertTrue( $in_array );
  40. $this->assertFalse( $in_array_after );
  41. }
  42. public function test_adds_rewrite_rules() {
  43. $this->set_permalink_structure( '/%postname%' );
  44. /* @var WP_Rewrite $wp_rewrite */
  45. global $wp_rewrite;
  46. $taxonomy = rand_str();
  47. $taxonomy_object = new WP_Taxonomy(
  48. $taxonomy,
  49. 'post',
  50. array(
  51. 'public' => true,
  52. 'rewrite' => true,
  53. )
  54. );
  55. $taxonomy_object->add_rewrite_rules();
  56. $rewrite_tags = $wp_rewrite->rewritecode;
  57. $taxonomy_object->remove_rewrite_rules();
  58. $rewrite_tags_after = $wp_rewrite->rewritecode;
  59. $this->assertNotFalse( array_search( "%$taxonomy%", $rewrite_tags, true ) );
  60. $this->assertFalse( array_search( "%$taxonomy%", $rewrite_tags_after, true ) );
  61. }
  62. public function test_adds_ajax_callback() {
  63. $taxonomy = rand_str();
  64. $taxonomy_object = new WP_Taxonomy(
  65. $taxonomy,
  66. 'post',
  67. array(
  68. 'public' => true,
  69. 'rewrite' => true,
  70. )
  71. );
  72. $taxonomy_object->add_hooks();
  73. $has_action = has_action( "wp_ajax_add-$taxonomy", '_wp_ajax_add_hierarchical_term' );
  74. $taxonomy_object->remove_hooks();
  75. $has_action_after = has_action( "wp_ajax_add-$taxonomy", '_wp_ajax_add_hierarchical_term' );
  76. $this->assertSame( 10, $has_action );
  77. $this->assertFalse( $has_action_after );
  78. }
  79. }