Make WordPress Core


Ignore:
Timestamp:
11/10/2016 01:53:08 AM (10 years ago)
Author:
johnbillion
Message:

Build/Test Tools: Re-use a bunch of fixtures in test classes for user and XMLRPC tests.

Shaves a couple of seconds off of the tests.

See #30017, #38716

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/user/author.php

    r37306 r39189  
    88 */
    99class Tests_User_Author_Template extends WP_UnitTestCase {
    10         protected $author_id = 0;
    11         protected $post_id = 0;
     10        protected static $author_id = 0;
     11        protected static $post_id = 0;
    1212
    1313        private $permalink_structure;
     14
     15        public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     16                self::$author_id = $factory->user->create( array(
     17                        'role' => 'author',
     18                        'user_login' => 'test_author',
     19                        'description' => 'test_author',
     20                ) );
     21
     22                self::$post_id = $factory->post->create( array(
     23                        'post_author' => self::$author_id,
     24                        'post_status' => 'publish',
     25                        'post_content' => rand_str(),
     26                        'post_title' => rand_str(),
     27                        'post_type' => 'post'
     28                ) );
     29        }
    1430
    1531        function setUp() {
    1632                parent::setUp();
    1733
    18                 $this->author_id = self::factory()->user->create( array(
    19                         'role' => 'author',
    20                         'user_login' => 'test_author',
    21                         'description' => 'test_author',
    22                 ) );
    23                 $user = new WP_User( $this->author_id );
    24 
    25                 $post = array(
    26                         'post_author' => $this->author_id,
    27                         'post_status' => 'publish',
    28                         'post_content' => rand_str(),
    29                         'post_title' => rand_str(),
    30                         'post_type' => 'post'
    31                 );
    32 
    33                 // insert a post and make sure the ID is ok
    34                 $this->post_id = self::factory()->post->create( $post );
    35 
    36                 setup_postdata( get_post( $this->post_id ) );
     34                setup_postdata( get_post( self::$post_id ) );
    3735        }
    3836
    … …  
    4442        function test_get_the_author() {
    4543                $author_name = get_the_author();
    46                 $user = new WP_User( $this->author_id );
     44                $user = new WP_User( self::$author_id );
    4745
    4846                $this->assertEquals( $user->display_name, $author_name );
    … …  
    5755                $this->assertEquals( 'test_author', get_the_author_meta( 'description' ) );
    5856                $this->assertEquals( 'test_author', get_the_author_meta( 'user_description' ) );
    59                 add_user_meta( $this->author_id, 'user_description', 'user description' );
    60                 $this->assertEquals( 'user description', get_user_meta( $this->author_id, 'user_description', true ) );
     57                add_user_meta( self::$author_id, 'user_description', 'user description' );
     58                $this->assertEquals( 'user description', get_user_meta( self::$author_id, 'user_description', true ) );
    6159                // user_description in meta is ignored. The content of description is returned instead.
    6260                // See #20285
    6361                $this->assertEquals( 'test_author', get_the_author_meta( 'user_description' ) );
    6462                $this->assertEquals( 'test_author', get_the_author_meta( 'description' ) );
    65                 update_user_meta( $this->author_id, 'user_description', '' );
    66                 $this->assertEquals( '', get_user_meta( $this->author_id, 'user_description', true ) );
     63                update_user_meta( self::$author_id, 'user_description', '' );
     64                $this->assertEquals( '', get_user_meta( self::$author_id, 'user_description', true ) );
    6765                $this->assertEquals( 'test_author', get_the_author_meta( 'user_description' ) );
    6866                $this->assertEquals( 'test_author', get_the_author_meta( 'description' ) );
    … …  
    8179                // Test with no global post, result should be 0 because no author is found
    8280                $this->assertEquals( 0, get_the_author_posts() );
    83                 $GLOBALS['post'] = $this->post_id;
     81                $GLOBALS['post'] = self::$post_id;
    8482                $this->assertEquals( 1, get_the_author_posts() );
    8583        }
    … …  
    9290
    9391                $cpt_ids = self::factory()->post->create_many( 2, array(
    94                         'post_author' => $this->author_id,
     92                        'post_author' => self::$author_id,
    9593                        'post_type'   => 'wptests_pt',
    9694                ) );
Note: See TracChangeset for help on using the changeset viewer.