getPostStatus.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * @group post
  4. */
  5. class Tests_Post_GetPostStatus extends WP_UnitTestCase {
  6. /**
  7. * Array of post IDs.
  8. *
  9. * @var int[]
  10. */
  11. public static $post_ids;
  12. /**
  13. * Create shared fixtures.
  14. */
  15. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  16. $post_statuses = array( 'publish', 'future', 'draft', 'auto-draft', 'trash', 'private', 'delete' );
  17. foreach ( $post_statuses as $post_status ) {
  18. $date = '';
  19. $actual_status = $post_status;
  20. if ( 'future' === $post_status ) {
  21. $date = strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 year' ) );
  22. } elseif ( in_array( $post_status, array( 'trash', 'delete' ), true ) ) {
  23. $actual_status = 'publish';
  24. }
  25. self::$post_ids[ $post_status ] = $factory->post->create(
  26. array(
  27. 'post_status' => $actual_status,
  28. 'post_date' => $date,
  29. 'post_name' => "$post_status-post",
  30. )
  31. );
  32. // Attachments without parent or media.
  33. self::$post_ids[ "$post_status-attachment-no-parent" ] = $factory->attachment->create_object(
  34. array(
  35. 'post_status' => $actual_status,
  36. 'post_name' => "$post_status-attachment-no-parent",
  37. 'post_date' => $date,
  38. )
  39. );
  40. // Attachments without media.
  41. self::$post_ids[ "$post_status-attachment" ] = $factory->attachment->create_object(
  42. array(
  43. 'post_parent' => self::$post_ids[ $post_status ],
  44. 'post_status' => 'inherit',
  45. 'post_name' => "$post_status-attachment",
  46. 'post_date' => $date,
  47. )
  48. );
  49. }
  50. // Attachment with incorrect parent ID.
  51. self::$post_ids['badly-parented-attachment'] = $factory->attachment->create_object(
  52. array(
  53. 'post_parent' => PHP_INT_MAX, // Impossibly large number.
  54. 'post_status' => 'inherit',
  55. 'post_name' => "$post_status-attachment",
  56. 'post_date' => $date,
  57. )
  58. );
  59. // Trash the trash post and attachment.
  60. wp_trash_post( self::$post_ids['trash'] );
  61. wp_trash_post( self::$post_ids['trash-attachment-no-parent'] );
  62. // Force delete parent and unattached post objects.
  63. wp_delete_post( self::$post_ids['delete'], true );
  64. wp_delete_post( self::$post_ids['delete-attachment-no-parent'], true );
  65. }
  66. /**
  67. * Ensure `get_post_status()` resolves correctly for posts and attachments.
  68. *
  69. * @ticket 52326
  70. * @dataProvider data_get_post_status_resolves
  71. *
  72. * @param string $post_key The post key in self::$post_ids.
  73. * @param string $expected The expected get_post_status() return value.
  74. */
  75. public function test_get_post_status_resolves( $post_key, $expected ) {
  76. $this->assertSame( $expected, get_post_status( self::$post_ids[ $post_key ] ) );
  77. }
  78. /**
  79. * Data provider for test_get_post_status_resolves().
  80. *
  81. * @return array[] {
  82. * @type string $post_key The post key in self::$post_ids.
  83. * @type string $expected The expected get_post_status() return value.
  84. * }
  85. */
  86. public function data_get_post_status_resolves() {
  87. return array(
  88. array( 'publish', 'publish' ),
  89. array( 'future', 'future' ),
  90. array( 'draft', 'draft' ),
  91. array( 'auto-draft', 'auto-draft' ),
  92. array( 'trash', 'trash' ),
  93. array( 'private', 'private' ),
  94. array( 'delete', false ),
  95. // Attachment with `inherit` status from parent.
  96. array( 'publish-attachment', 'publish' ),
  97. array( 'future-attachment', 'future' ),
  98. array( 'draft-attachment', 'draft' ),
  99. array( 'auto-draft-attachment', 'auto-draft' ),
  100. array( 'trash-attachment', 'publish' ),
  101. array( 'private-attachment', 'private' ),
  102. array( 'delete-attachment', 'publish' ),
  103. // Attachment with native status (rather than inheriting from parent).
  104. array( 'publish-attachment-no-parent', 'publish' ),
  105. array( 'future-attachment-no-parent', 'publish' ), // Attachments can't have future status.
  106. array( 'draft-attachment-no-parent', 'publish' ), // Attachments can't have draft status.
  107. array( 'auto-draft-attachment-no-parent', 'auto-draft' ),
  108. array( 'trash-attachment-no-parent', 'trash' ),
  109. array( 'private-attachment-no-parent', 'private' ),
  110. array( 'delete-attachment-no-parent', false ),
  111. // Attachment attempting to inherit from an invalid parent number.
  112. array( 'badly-parented-attachment', 'publish' ),
  113. );
  114. }
  115. /**
  116. * Ensure post status resolves after trashing parent posts.
  117. *
  118. * @ticket 52326
  119. * @dataProvider data_get_post_status_after_trashing
  120. *
  121. * @param string $post_to_test The post key in self::$post_ids.
  122. * @param string $post_to_trash The post key to trash then delete in self::$post_ids.
  123. * @param string $expected The expected result after trashing the post.
  124. */
  125. public function test_get_post_status_after_trashing( $post_to_test, $post_to_trash, $expected ) {
  126. wp_trash_post( self::$post_ids[ $post_to_trash ] );
  127. $this->assertSame( $expected, get_post_status( self::$post_ids[ $post_to_test ] ) );
  128. // Now delete the post, expect publish.
  129. wp_delete_post( self::$post_ids[ $post_to_trash ], true );
  130. $this->assertSame( 'publish', get_post_status( self::$post_ids[ $post_to_test ] ) );
  131. }
  132. /**
  133. * Data provider for test_get_post_status_after_trashing().
  134. * @return array[] {
  135. * @type string $post_to_test The post key in self::$post_ids.
  136. * @type string $post_to_trash The post key to trash then delete in self::$post_ids.
  137. * @type string $expected The expected result after trashing the post.
  138. * }
  139. */
  140. public function data_get_post_status_after_trashing() {
  141. return array(
  142. array( 'publish-attachment', 'publish', 'publish' ),
  143. array( 'future-attachment', 'future', 'future' ),
  144. array( 'draft-attachment', 'draft', 'draft' ),
  145. array( 'auto-draft-attachment', 'auto-draft', 'auto-draft' ),
  146. array( 'private-attachment', 'private', 'private' ),
  147. array( 'delete-attachment', 'publish', 'publish' ),
  148. );
  149. }
  150. }