| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * @group post |
| | 5 | */ |
| | 6 | class Tests_Post_GetPostStatus extends WP_UnitTestCase { |
| | 7 | |
| | 8 | /** |
| | 9 | * Array of post IDs. |
| | 10 | * |
| | 11 | * @var int[] |
| | 12 | */ |
| | 13 | public static $post_ids; |
| | 14 | |
| | 15 | public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { |
| | 16 | $post_statuses = array( 'publish', 'future', 'draft', 'auto-draft', 'trash' ); |
| | 17 | foreach ( $post_statuses as $post_status ) { |
| | 18 | $date = ''; |
| | 19 | if ( 'future' === $post_status ) { |
| | 20 | $date = strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 year' ) ); |
| | 21 | } |
| | 22 | |
| | 23 | self::$post_ids[ $post_status ] = $factory->post->create( |
| | 24 | array( |
| | 25 | 'post_status' => 'trash' === $post_status ? 'publish' : $post_status, |
| | 26 | 'post_date' => $date, |
| | 27 | 'post_name' => "$post_status-post", |
| | 28 | ) |
| | 29 | ); |
| | 30 | |
| | 31 | // Attachments without media. |
| | 32 | self::$post_ids[ "$post_status-attachment" ] = $factory->attachment->create_object( |
| | 33 | array( |
| | 34 | 'post_parent' => self::$post_ids[ $post_status ], |
| | 35 | 'post_status' => 'inherit', |
| | 36 | 'post_name' => "$post_status-attachment", |
| | 37 | 'post_date' => $date, |
| | 38 | ) |
| | 39 | ); |
| | 40 | |
| | 41 | // Attachments without parent or media. |
| | 42 | self::$post_ids[ "$post_status-attachment-no-parent" ] = $factory->attachment->create_object( |
| | 43 | array( |
| | 44 | 'post_status' => 'trash' === $post_status ? 'publish' : $post_status, |
| | 45 | 'post_name' => "$post_status-attachment-no-parent", |
| | 46 | 'post_date' => $date, |
| | 47 | ) |
| | 48 | ); |
| | 49 | } |
| | 50 | |
| | 51 | // Attachment with incorrect parent ID. |
| | 52 | self::$post_ids['badly-parented-attachment'] = $factory->attachment->create_object( |
| | 53 | array( |
| | 54 | 'post_parent' => PHP_INT_MAX, |
| | 55 | 'post_status' => 'inherit', |
| | 56 | 'post_name' => "$post_status-attachment", |
| | 57 | 'post_date' => $date, |
| | 58 | ) |
| | 59 | ); |
| | 60 | |
| | 61 | // Trash the trash post and attachment. |
| | 62 | wp_trash_post( self::$post_ids['trash'] ); |
| | 63 | wp_trash_post( self::$post_ids['trash-attachment-no-parent'] ); |
| | 64 | } |
| | 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 | /** |
| | 80 | * Data provider for test_get_post_status_resolves(). |
| | 81 | * |
| | 82 | * @return array { |
| | 83 | * @type string $post_key The post key in self::$post_ids. |
| | 84 | * @type string $expected The expected get_post_status() return value. |
| | 85 | * } |
| | 86 | */ |
| | 87 | public function data_get_post_status_resolves() { |
| | 88 | return array( |
| | 89 | array( 'publish', 'publish' ), |
| | 90 | array( 'future', 'future' ), |
| | 91 | array( 'draft', 'draft' ), |
| | 92 | array( 'auto-draft', 'auto-draft' ), |
| | 93 | array( 'trash', 'trash' ), |
| | 94 | |
| | 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 | |
| | 102 | // Attachment with native status (rather than inheriting from parent). |
| | 103 | array( 'publish-attachment-no-parent', 'publish' ), |
| | 104 | array( 'future-attachment-no-parent', 'future' ), |
| | 105 | array( 'draft-attachment-no-parent', 'draft' ), |
| | 106 | array( 'auto-draft-attachment-no-parent', 'auto-draft' ), |
| | 107 | array( 'trash-attachment-no-parent', 'trash' ), |
| | 108 | |
| | 109 | // Attachment attempting to inherit from an invalid parent number. |
| | 110 | array( 'badly-parented-attachment', 'publish' ), |
| | 111 | ); |
| | 112 | } |
| | 113 | } |