WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/21/2015 03:17:36 AM (5 years ago)
Author:
wonderboymusic
Message:

AJAX UNIT TESTS: Have you ever wondered why these take 600 forevers to run? They all eventually call do_action( 'admin_init' ), which has _maybe_update_core, _maybe_update_plugins, and _maybe_update_themes hooked to it. REMOVE THEM, and AJAX unit tests run like the wind. Tests_Ajax_Response is still slow.

See #30017, #33968.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/ajax/Autosave.php

    r35244 r35311  
    2222    protected $_post = null;
    2323
    24     /**
    25      * user_id
    26      * @var int
    27      */
    28     protected $user_id = 0;
     24    protected static $admin_id = 0;
     25    protected static $editor_id = 0;
     26    protected static $post;
     27    protected static $post_id;
     28    protected static $user_ids = array();
     29
     30    public static function wpSetUpBeforeClass( $factory ) {
     31        self::$user_ids[] = self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
     32        self::$user_ids[] = self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
     33
     34        self::$post_id = $factory->post->create( array( 'post_status' => 'draft' ) );
     35        self::$post = get_post( self::$post_id );
     36    }
     37
     38    public static function wpTearDownAfterClass() {
     39        foreach ( self::$user_ids as $user_id ) {
     40            self::delete_user( $user_id );
     41        }
     42
     43        wp_delete_post( self::$post_id, true );
     44    }
    2945
    3046    /**
     
    3450        parent::setUp();
    3551        // Set a user so the $post has 'post_author'
    36         $this->user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
    37         wp_set_current_user( $this->user_id );
    38 
    39         $post_id = self::factory()->post->create( array( 'post_status' => 'draft' ) );
    40         $this->_post = get_post( $post_id );
     52        wp_set_current_user( self::$admin_id );
    4153    }
    4254
     
    4759    public function test_autosave_post() {
    4860        // The original post_author
    49         wp_set_current_user( $this->user_id );
     61        wp_set_current_user( self::$admin_id );
    5062
    5163        // Set up the $_POST request
     
    5668            'data' => array(
    5769                'wp_autosave' => array(
    58                     'post_id'       => $this->_post->ID,
    59                     '_wpnonce'      => wp_create_nonce( 'update-post_' . $this->_post->ID ),
    60                     'post_content'  => $this->_post->post_content . PHP_EOL . $md5,
     70                    'post_id'       => self::$post_id,
     71                    '_wpnonce'      => wp_create_nonce( 'update-post_' . self::$post->ID ),
     72                    'post_content'  => self::$post->post_content . PHP_EOL . $md5,
    6173                    'post_type'     => 'post',
    6274                ),
     
    7991
    8092        // Check that the edit happened
    81         $post = get_post( $this->_post->ID );
    82         $this->assertGreaterThanOrEqual( 0, strpos( $post->post_content, $md5 ) );
     93        $post = get_post( self::$post_id );
     94        $this->assertGreaterThanOrEqual( 0, strpos( self::$post->post_content, $md5 ) );
    8395    }
    8496
     
    89101    public function test_autosave_locked_post() {
    90102        // Lock the post to another user
    91         $another_user_id = self::factory()->user->create( array( 'role' => 'editor' ) );
    92         wp_set_current_user( $another_user_id );
    93         wp_set_post_lock( $this->_post->ID );
     103        wp_set_current_user( self::$editor_id );
     104        wp_set_post_lock( self::$post_id );
    94105
    95         wp_set_current_user( $this->user_id );
     106        wp_set_current_user( self::$admin_id );
    96107
    97108        // Ensure post is locked
    98         $this->assertEquals( $another_user_id, wp_check_post_lock( $this->_post->ID ) );
     109        $this->assertEquals( self::$editor_id, wp_check_post_lock( self::$post_id ) );
    99110
    100111        // Set up the $_POST request
     
    105116            'data' => array(
    106117                'wp_autosave' => array(
    107                     'post_id'       => $this->_post->ID,
    108                     '_wpnonce'      => wp_create_nonce( 'update-post_' . $this->_post->ID ),
    109                     'post_content'  => $this->_post->post_content . PHP_EOL . $md5,
     118                    'post_id'       => self::$post_id,
     119                    '_wpnonce'      => wp_create_nonce( 'update-post_' . self::$post_id ),
     120                    'post_content'  => self::$post->post_content . PHP_EOL . $md5,
    110121                    'post_type'     => 'post',
    111122                ),
     
    127138
    128139        // Check that the original post was NOT edited
    129         $post = get_post( $this->_post->ID );
     140        $post = get_post( self::$post_id );
    130141        $this->assertFalse( strpos( $post->post_content, $md5 ) );
    131142
    132143        // Check if the autosave post was created
    133         $autosave = wp_get_post_autosave( $this->_post->ID, get_current_user_id() );
     144        $autosave = wp_get_post_autosave( self::$post_id, get_current_user_id() );
    134145        $this->assertNotEmpty( $autosave );
    135146        $this->assertGreaterThanOrEqual( 0, strpos( $autosave->post_content, $md5 ) );
     
    142153    public function test_with_invalid_nonce( ) {
    143154
    144         wp_set_current_user( $this->user_id );
     155        wp_set_current_user( self::$admin_id );
    145156
    146157        // Set up the $_POST request
     
    150161            'data' => array(
    151162                'wp_autosave' => array(
    152                     'post_id'  => $this->_post->ID,
     163                    'post_id'  => self::$post_id,
    153164                    '_wpnonce' => substr( md5( uniqid() ), 0, 10 ),
    154165                ),
Note: See TracChangeset for help on using the changeset viewer.