Make WordPress Core

Changeset 496 in tests


Ignore:
Timestamp:
01/11/2012 09:09:34 PM (13 years ago)
Author:
ryan
Message:

Beginnings of post type tests. Test post deletion and cache clearing.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wp-testcase/test_includes_post.php

    r454 r496  
    77        parent::setUp();
    88        $this->author_id = $this->_make_user('editor');
     9        $this->old_current_user = get_current_user_id();
     10        wp_set_current_user( $this->author_id );
    911        _set_cron_array(array());
    1012        $this->post_ids = array();
     
    1618            wp_delete_post($id);
    1719        wp_delete_user( $this->author_id );
     20        wp_set_current_user( $this->old_current_user );
    1821    }
    1922
     
    2326    }
    2427
    25 
    2628    // test simple valid behavior: insert and get a post
    27     function test_vb_insert_get() {
    28         $post = array(
    29             'post_author' => $this->author_id,
    30             'post_status' => 'publish',
    31             'post_content' => rand_str(),
    32             'post_title' => rand_str(),
    33         );
    34 
    35         // insert a post and make sure the ID is ok
    36         $id = $this->post_ids[] = wp_insert_post($post);
    37         $this->assertTrue(is_numeric($id));
    38         $this->assertTrue($id > 0);
    39 
    40         // fetch the post and make sure it matches
    41         $out = wp_get_single_post($id);
    42 
    43         $this->assertEquals($post['post_content'], $out->post_content);
    44         $this->assertEquals($post['post_title'], $out->post_title);
    45         $this->assertEquals($post['post_status'], $out->post_status);
    46         $this->assertEquals($post['post_author'], $out->post_author);
    47     }
    48 
     29    function test_vb_insert_get_delete() {
     30        register_post_type( 'cpt', array( 'taxonomies' => array( 'post_tag', 'ctax' ) ) );
     31        register_taxonomy( 'ctax', 'cpt' );
     32        $post_types = array( 'post', 'cpt' );
     33
     34        foreach ( $post_types as $post_type ) {
     35            $post = array(
     36                'post_author' => $this->author_id,
     37                'post_status' => 'publish',
     38                'post_content' => rand_str(),
     39                'post_title' => rand_str(),
     40                'tax_input' => array( 'post_tag' => 'tag1,tag2', 'ctax' => 'cterm1,cterm2' ),
     41                'post_type' => $post_type
     42            );
     43
     44            // insert a post and make sure the ID is ok
     45            $id = wp_insert_post($post);
     46            $this->assertTrue(is_numeric($id));
     47            $this->assertTrue($id > 0);
     48
     49            // fetch the post and make sure it matches
     50            $out = wp_get_single_post($id);
     51
     52            $this->assertEquals($post['post_content'], $out->post_content);
     53            $this->assertEquals($post['post_title'], $out->post_title);
     54            $this->assertEquals($post['post_status'], $out->post_status);
     55            $this->assertEquals($post['post_author'], $out->post_author);
     56
     57            // test cache state
     58            $pcache = wp_cache_get( $id, 'posts' );
     59            $this->assertInstanceOf( 'stdClass', $pcache );
     60            $this->assertEquals( $id, $pcache->ID );
     61
     62            update_object_term_cache( $id, $post_type );
     63            $tcache = wp_cache_get( $id, "post_tag_relationships" );
     64            $this->assertInternalType( 'array', $tcache );
     65            $this->assertEquals( 2, count( $tcache ) );
     66
     67            $tcache = wp_cache_get( $id, "ctax_relationships" );
     68            if ( 'cpt' == $post_type ) {
     69                $this->assertInternalType( 'array', $tcache );
     70                $this->assertEquals( 2, count( $tcache ) );
     71            } else {
     72                $this->assertFalse( $tcache );
     73            }
     74
     75            wp_delete_post( $id, true );
     76            $this->assertFalse( wp_cache_get( $id, 'posts' ) );
     77            $this->assertFalse( wp_cache_get( $id, "post_tag_relationships" ) );
     78            $this->assertFalse( wp_cache_get( $id, "ctax_relationships" ) );
     79        }
     80    }
    4981
    5082    function test_vb_insert_future() {
     
    842874    }
    843875}
     876
     877class WPTestPostTypes extends WPTestCase {
     878    function setUp() {
     879        parent::setUp();
     880    }
     881
     882    function tearDown() {
     883        parent::tearDown();
     884    }
     885
     886    function test_register_post_type() {
     887        $this->assertNull( get_post_type_object( 'foo' ) );
     888        register_post_type( 'foo' );
     889
     890        $pobj = get_post_type_object( 'foo' );
     891        $this->assertInstanceOf( 'stdClass', $pobj );
     892        $this->assertEquals( 'foo', $pobj->name );
     893
     894        // Test some defaults
     895        $this->assertFalse( is_post_type_hierarchical( 'foo' ) );
     896        $this->assertEquals( array(), get_object_taxonomies( 'foo' ) );
     897    }
     898
     899    function test_register_taxonomy_for_object_type() {
     900        register_post_type( 'bar' );
     901        register_taxonomy_for_object_type( 'post_tag', 'bar' );
     902        $this->assertEquals( array( 'post_tag' ), get_object_taxonomies( 'bar' ) );
     903        register_taxonomy_for_object_type( 'category', 'bar' );
     904        $this->assertEquals( array( 'category', 'post_tag' ), get_object_taxonomies( 'bar' ) );
     905    }
     906
     907    function test_post_type_exists() {
     908        $this->assertFalse( post_type_exists( 'notaposttype' ) );
     909        $this->assertTrue( post_type_exists( 'post' ) );
     910    }
     911
     912    function test_post_type_supports() {
     913        $this->assertTrue( post_type_supports( 'post', 'post-formats' ) );
     914        $this->assertFalse( post_type_supports( 'page', 'post-formats' ) );
     915        $this->assertFalse( post_type_supports( 'notaposttype', 'post-formats' ) );
     916        $this->assertFalse( post_type_supports( 'post', 'notafeature' ) );
     917        $this->assertFalse( post_type_supports( 'notaposttype', 'notafeature' ) );
     918    }
     919}
     920
    844921?>
Note: See TracChangeset for help on using the changeset viewer.