WordPress.org

Make WordPress Core

Changeset 977 in tests


Ignore:
Timestamp:
08/20/12 19:48:38 (20 months ago)
Author:
ryan
Message:

get_post(), WP_Post, and ancestors tests. see #WP21309

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/post/objects.php

    r969 r977  
    1010 
    1111        $post = get_post( $id ); 
     12        $this->assertInstanceOf( 'WP_Post', $post ); 
     13        $this->assertEquals( $id, $post->ID ); 
     14        $this->assertTrue( isset( $post->ancestors ) ); 
     15        $this->assertEquals( array(), $post->ancestors ); 
    1216 
    13         $this->assertEquals( $id, $post->ID ); 
     17        // Unset and then verify that the magic method fills the property again 
     18        unset( $post->ancestors ); 
     19        $this->assertEquals( array(), $post->ancestors ); 
    1420 
    15         $this->assertTrue( isset( $post->post_type ) ); 
     21        // Magic get should make meta accessible as properties 
     22        add_post_meta( $id, 'test', 'test' ); 
     23        $this->assertEquals( 'test', get_post_meta( $id, 'test', true ) ); 
     24        $this->assertEquals( 'test', $post->test ); 
     25 
     26        // Make sure meta does not eclipse true properties 
     27        add_post_meta( $id, 'post_type', 'dummy' ); 
     28        $this->assertEquals( 'dummy', get_post_meta( $id, 'post_type', true ) ); 
    1629        $this->assertEquals( 'post', $post->post_type ); 
    1730 
    18         unset( $post->post_type ); 
    19         $this->assertFalse( isset( $post->post_type ) ); 
     31        // Excercise the output argument 
     32        $post = get_post( $id, ARRAY_A ); 
     33        $this->assertInternalType( 'array', $post ); 
     34        $this->assertEquals( 'post', $post[ 'post_type' ] ); 
     35 
     36        $post = get_post( $id, ARRAY_N ); 
     37        $this->assertInternalType( 'array', $post ); 
     38        $this->assertFalse( isset( $post[ 'post_type' ] ) ); 
     39        $this->assertTrue( in_array( 'post', $post ) ); 
     40 
     41        $post = get_post( $id ); 
     42        $post = get_post( $post, ARRAY_A ); 
     43        $this->assertInternalType( 'array', $post ); 
     44        $this->assertEquals( 'post', $post[ 'post_type' ] ); 
     45        $this->assertEquals( $id, $post[ 'ID' ] ); 
     46 
     47        // Should default to OBJECT when given invalid output argument 
     48        $post = get_post( $id, 'invalid-output-value' ); 
     49        $this->assertInstanceOf( 'WP_Post', $post ); 
     50        $this->assertEquals( $id, $post->ID ); 
     51    } 
     52 
     53    function test_get_post_ancestors() { 
     54        $parent_id = $this->factory->post->create(); 
     55        $child_id = $this->factory->post->create(); 
     56        $grandchild_id = $this->factory->post->create(); 
     57        $updated = wp_update_post( array( 'ID' => $child_id, 'post_parent' => $parent_id ) ); 
     58        $this->assertEquals( $updated, $child_id ); 
     59        $updated = wp_update_post( array( 'ID' => $grandchild_id, 'post_parent' => $child_id ) ); 
     60        $this->assertEquals( $updated, $grandchild_id ); 
     61 
     62        $this->assertEquals( array( $parent_id ), get_post( $child_id )->ancestors ); 
     63        $this->assertEquals( array( $parent_id ), get_post_ancestors( $child_id ) ); 
     64        $this->assertEquals( array( $parent_id ), get_post_ancestors( get_post( $child_id ) ) ); 
     65 
     66        $this->assertEquals( array( $child_id, $parent_id ), get_post( $grandchild_id )->ancestors ); 
     67        $this->assertEquals( array( $child_id, $parent_id ), get_post_ancestors( $grandchild_id ) ); 
     68        $this->assertEquals( array( $child_id, $parent_id ), get_post_ancestors( get_post( $grandchild_id ) ) ); 
     69 
     70        $this->assertEquals( array(), get_post( $parent_id )->ancestors ); 
     71        $this->assertEquals( array(), get_post_ancestors( $parent_id ) ); 
     72        $this->assertEquals( array(), get_post_ancestors( get_post( $parent_id ) ) ); 
    2073    } 
    2174 
     
    3790        $this->assertEquals( 'raw', $raw_post->filter ); 
    3891        $this->assertNotEquals( esc_js( "Mary's home" ), $raw_post->post_title ); 
     92 
     93        $raw_post->filter( 'js' ); 
     94        $this->assertEquals( 'js', $post->filter ); 
     95        $this->assertEquals( esc_js( "Mary's home" ), $raw_post->post_title ); 
    3996    } 
    4097 
Note: See TracChangeset for help on using the changeset viewer.