WordPress.org

Make WordPress Core

Changeset 51450


Ignore:
Timestamp:
07/17/2021 10:23:57 AM (2 weeks ago)
Author:
SergeyBiryukov
Message:

Tests: Correct the test for autosaving a post with Ajax.

wp_autosave() only updates drafts and auto-drafts created by the current user if the post is not locked.

As a result of previous Ajax test refactoring, setting the current user and creating a test post ended up in different methods, with the user being set after the post is already created.

This resulted in the test post being created with the post_author field set to zero, and the current user check in wp_autosave() failed. Instead of updating the original post as the test intended, it created a new autosave.

The test only passed accidentally due to assertGreaterThanOrEqual() not performing a strict type check.

Follow-up to [26995], [35311].

See #53363.

File:
1 edited

Legend:

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

    r49603 r51450  
    3535        self::$user_ids[] = self::$editor_id;
    3636
     37        // Set a user so the $post has 'post_author'.
     38        wp_set_current_user( self::$admin_id );
     39
    3740        self::$post_id = $factory->post->create( array( 'post_status' => 'draft' ) );
    3841        self::$post    = get_post( self::$post_id );
    39     }
    40 
    41     /**
    42      * Sets up the test fixture.
    43      */
    44     public function setUp() {
    45         parent::setUp();
    46         // Set a user so the $post has 'post_author'.
    47         wp_set_current_user( self::$admin_id );
    4842    }
    4943
     
    6357                'wp_autosave' => array(
    6458                    'post_id'      => self::$post_id,
    65                     '_wpnonce'     => wp_create_nonce( 'update-post_' . self::$post->ID ),
     59                    '_wpnonce'     => wp_create_nonce( 'update-post_' . self::$post_id ),
    6660                    'post_content' => self::$post->post_content . PHP_EOL . $md5,
    6761                    'post_type'    => 'post',
     
    8680        // Check that the edit happened.
    8781        $post = get_post( self::$post_id );
    88         $this->assertGreaterThanOrEqual( 0, strpos( self::$post->post_content, $md5 ) );
     82        $this->assertNotFalse( strpos( $post->post_content, $md5 ) );
    8983    }
    9084
     
    137131        $autosave = wp_get_post_autosave( self::$post_id, get_current_user_id() );
    138132        $this->assertNotEmpty( $autosave );
    139         $this->assertGreaterThanOrEqual( 0, strpos( $autosave->post_content, $md5 ) );
     133        $this->assertNotFalse( strpos( $autosave->post_content, $md5 ) );
    140134    }
    141135
Note: See TracChangeset for help on using the changeset viewer.