WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/01/2018 04:02:26 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Tests: Improve tests for wp_get_post_parent_id() added in [42397].

Props frank-klein.
Fixes #42797.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/wpGetPostParentId.php

    r42397 r42635  
    55 */
    66class Tests_Post_WpGetPostParentId extends WP_UnitTestCase {
     7    /**
     8     * Parent post ID.
     9     *
     10     * @var int
     11     */
     12    public static $parent_post_id;
     13
     14    /**
     15     * Post ID.
     16     *
     17     * @var int
     18     */
     19    public static $post_id;
     20
     21    public static function wpSetUpBeforeClass() {
     22        self::$parent_post_id = self::factory()->post->create();
     23        self::$post_id        = self::factory()->post->create( array( 'post_parent' => self::$parent_post_id ) );
     24    }
    725
    826    public function test_wp_get_post_parent_id_with_post_object() {
    9         $p1 = self::factory()->post->create();
    10         $p2 = self::factory()->post->create( array( 'post_parent' => $p1 ) );
    11         $post = get_post( $p2 );
    12         $this->assertTrue( $post instanceof WP_Post );
    13         $this->assertEquals( $p1, wp_get_post_parent_id( $post ) );
     27        $post = get_post( self::$post_id );
     28        $this->assertInstanceOf( 'WP_Post', $post );
     29        $this->assertSame( self::$parent_post_id, wp_get_post_parent_id( $post ) );
    1430    }
    1531
    1632    public function test_wp_get_post_parent_id_with_post_id() {
    17         $p1 = self::factory()->post->create();
    18         $p2 = self::factory()->post->create( array( 'post_parent' => $p1 ) );
    19         $this->assertEquals( $p1, wp_get_post_parent_id( $p2 ) );
     33        $this->assertSame( self::$parent_post_id, wp_get_post_parent_id( self::$post_id ) );
    2034    }
    2135
    2236    public function test_wp_get_post_parent_id_with_non_existing_id_default_to_global_post_id() {
    23         $p1 = self::factory()->post->create();
    24         $GLOBALS['post'] = self::factory()->post->create( array( 'post_parent' => $p1 ) );
    25         $this->assertEquals( $p1, wp_get_post_parent_id( 0 ) );
    26         unset( $GLOBALS['post'] );
     37        $GLOBALS['post'] = get_post( self::$post_id );
     38        $this->assertSame( self::$parent_post_id, wp_get_post_parent_id( 0 ) );
    2739    }
    2840
    2941    public function test_wp_get_post_parent_id_with_boolean_default_to_global_post_id() {
    30         $p1 = self::factory()->post->create();
    31         $GLOBALS['post'] = self::factory()->post->create( array( 'post_parent' => $p1 ) );
    32         $this->assertEquals( $p1, wp_get_post_parent_id( false ) );
    33         unset( $GLOBALS['post'] );
     42        $GLOBALS['post'] = get_post( self::$post_id );
     43        $this->assertSame( self::$parent_post_id, wp_get_post_parent_id( false ) );
    3444    }
    3545
    3646    public function test_wp_get_post_parent_id_with_string_default_to_false() {
    37         $p1 = self::factory()->post->create();
    38         $GLOBALS['post'] = self::factory()->post->create( array( 'post_parent' => $p1 ) );
     47        $GLOBALS['post'] = get_post( self::$post_id );
    3948        $this->assertFalse( wp_get_post_parent_id( 'string' ) );
    40         unset( $GLOBALS['post'] );
    4149    }
    4250}
Note: See TracChangeset for help on using the changeset viewer.