Code Modernization: Remove dynamic properties in WP_Test_REST_Posts_Controller.

Dynamic (non-explicitly declared) properties are deprecated as of PHP 8.2 and are expected to become a fatal error in PHP 9.0.

In this particular case, the `test_create_update_post_with_featured_media()` method creates an attachment and writes the ID of the attachment to a dynamic (undeclared) property to be used as a flag to determine whether attachments need to be cleaned up after the test in the `tear_down()` method.

As the actual ''value'' of the property is irrelevant for the cleaning up and the property is realistically being used as a “flag”, this is now fixed as follows:

* Have an actual “flag” property declared with a descriptive name — `$attachments_created` — to make the code in the `tear_down()` more easily understandable.
* As for the actual ID of the attachment, save that to a test method local variable as that is the only place where it has any relevance.

Includes moving the `tear_down()` method up to be directly below the `set_up()` method.

Follow-up to [38832], [53557], [53558], [53850], [53851], [53852], [53853], [53854], [53856], [53916].

Props jrf.
See #56033.

git-svn-id: https://develop.svn.wordpress.org/trunk@53935 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2022-08-24 13:09:03 +00:00
parent b351a2f058
commit ad00d45c9b

View File

@@ -26,6 +26,8 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
protected $forbidden_cat;
protected $posts_clauses;
private $attachments_created = false;
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$post_id = $factory->post->create();
@@ -115,6 +117,15 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
add_filter( 'posts_clauses', array( $this, 'save_posts_clauses' ), 10, 2 );
}
public function tear_down() {
if ( true === $this->attachments_created ) {
$this->remove_added_uploads();
$this->attachments_created = false;
}
parent::tear_down();
}
public function wpSetUpBeforeRequest( $result, $server, $request ) {
$this->posts_clauses = array();
return $result;
@@ -2771,8 +2782,8 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
public function test_create_update_post_with_featured_media() {
$file = DIR_TESTDATA . '/images/canola.jpg';
$this->attachment_id = $this->factory->attachment->create_object(
$file = DIR_TESTDATA . '/images/canola.jpg';
$attachment_id = $this->factory->attachment->create_object(
$file,
0,
array(
@@ -2781,20 +2792,22 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
)
);
$this->attachments_created = true;
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
$params = $this->set_post_data(
array(
'featured_media' => $this->attachment_id,
'featured_media' => $attachment_id,
)
);
$request->set_body_params( $params );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$new_post = get_post( $data['id'] );
$this->assertSame( $this->attachment_id, $data['featured_media'] );
$this->assertSame( $this->attachment_id, (int) get_post_thumbnail_id( $new_post->ID ) );
$this->assertSame( $attachment_id, $data['featured_media'] );
$this->assertSame( $attachment_id, (int) get_post_thumbnail_id( $new_post->ID ) );
$request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . $new_post->ID );
$params = $this->set_post_data(
@@ -5277,14 +5290,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
);
}
public function tear_down() {
if ( isset( $this->attachment_id ) ) {
$this->remove_added_uploads();
}
parent::tear_down();
}
/**
* Internal function used to disable an insert query which
* will trigger a wpdb error for testing purposes.