wpPublishPost.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * @group post
  4. */
  5. class Tests_Post_wpPublishPost extends WP_UnitTestCase {
  6. /**
  7. * Auto-draft post ID.
  8. *
  9. * @var int
  10. */
  11. public static $auto_draft_id;
  12. /**
  13. * Create shared fixtures.
  14. *
  15. * @param WP_UnitTest_Factory $factory Test suite factory.
  16. */
  17. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  18. self::$auto_draft_id = $factory->post->create( array( 'post_status' => 'auto-draft' ) );
  19. }
  20. /**
  21. * Ensure wp_publish_post does not add default category in error.
  22. *
  23. * @ticket 51292
  24. */
  25. function test_wp_publish_post_respects_current_categories() {
  26. $post_id = self::$auto_draft_id;
  27. $category_id = $this->factory->term->create( array( 'taxonomy' => 'category' ) );
  28. wp_set_post_categories( $post_id, $category_id );
  29. wp_publish_post( $post_id );
  30. $post_categories = get_the_category( $post_id );
  31. $this->assertCount( 1, $post_categories );
  32. $this->assertSame(
  33. $category_id,
  34. $post_categories[0]->term_id,
  35. 'wp_publish_post replaced set category.'
  36. );
  37. }
  38. /**
  39. * Ensure wp_publish_post adds default category.
  40. *
  41. * @covers ::wp_publish_post
  42. * @ticket 51292
  43. */
  44. function test_wp_publish_post_adds_default_category() {
  45. $post_id = self::$auto_draft_id;
  46. wp_publish_post( $post_id );
  47. $post_categories = get_the_category( $post_id );
  48. $this->assertCount( 1, $post_categories );
  49. $this->assertSame(
  50. (int) get_option( 'default_category' ),
  51. $post_categories[0]->term_id,
  52. 'wp_publish_post failed to add default category.'
  53. );
  54. }
  55. /**
  56. * Ensure wp_publish_post adds default category when tagged.
  57. *
  58. * @covers ::wp_publish_post
  59. * @ticket 51292
  60. */
  61. function test_wp_publish_post_adds_default_category_when_tagged() {
  62. $post_id = self::$auto_draft_id;
  63. $tag_id = $this->factory->term->create( array( 'taxonomy' => 'post_tag' ) );
  64. wp_set_post_tags( $post_id, array( $tag_id ) );
  65. wp_publish_post( $post_id );
  66. $post_categories = get_the_category( $post_id );
  67. $this->assertCount( 1, $post_categories );
  68. $this->assertSame(
  69. (int) get_option( 'default_category' ),
  70. $post_categories[0]->term_id,
  71. 'wp_publish_post failed to add default category.'
  72. );
  73. }
  74. /**
  75. * Ensure wp_publish_post does not add default term in error.
  76. *
  77. * @covers ::wp_publish_post
  78. * @ticket 51292
  79. */
  80. function test_wp_publish_post_respects_current_terms() {
  81. // Create custom taxonomy to test with.
  82. register_taxonomy(
  83. 'tax_51292',
  84. 'post',
  85. array(
  86. 'hierarchical' => true,
  87. 'public' => true,
  88. 'default_term' => array(
  89. 'name' => 'Default 51292',
  90. 'slug' => 'default-51292',
  91. ),
  92. )
  93. );
  94. $post_id = self::$auto_draft_id;
  95. $term_id = $this->factory->term->create( array( 'taxonomy' => 'tax_51292' ) );
  96. wp_set_object_terms( $post_id, array( $term_id ), 'tax_51292' );
  97. wp_publish_post( $post_id );
  98. $post_terms = get_the_terms( $post_id, 'tax_51292' );
  99. $this->assertCount( 1, $post_terms );
  100. $this->assertSame(
  101. $term_id,
  102. $post_terms[0]->term_id,
  103. 'wp_publish_post replaced set term for custom taxonomy.'
  104. );
  105. }
  106. /**
  107. * Ensure wp_publish_post adds default term.
  108. *
  109. * @covers ::wp_publish_post
  110. * @ticket 51292
  111. */
  112. function test_wp_publish_post_adds_default_term() {
  113. // Create custom taxonomy to test with.
  114. register_taxonomy(
  115. 'tax_51292',
  116. 'post',
  117. array(
  118. 'hierarchical' => true,
  119. 'public' => true,
  120. 'default_term' => array(
  121. 'name' => 'Default 51292',
  122. 'slug' => 'default-51292',
  123. ),
  124. )
  125. );
  126. $post_id = self::$auto_draft_id;
  127. wp_publish_post( $post_id );
  128. $post_terms = get_the_terms( $post_id, 'tax_51292' );
  129. $this->assertCount( 1, $post_terms );
  130. $this->assertSame(
  131. get_term_by( 'slug', 'default-51292', 'tax_51292' )->term_id,
  132. $post_terms[0]->term_id,
  133. 'wp_publish_post failed to add default term for custom taxonomy.'
  134. );
  135. }
  136. }