Make WordPress Core

Changeset 53935


Ignore:
Timestamp:
08/24/2022 01:09:03 PM (4 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r53813 r53935  
    2626        protected $forbidden_cat;
    2727        protected $posts_clauses;
     28
     29        private $attachments_created = false;
    2830
    2931        public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     
    114116                add_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 );
    115117                add_filter( 'posts_clauses', array( $this, 'save_posts_clauses' ), 10, 2 );
     118        }
     119
     120        public function tear_down() {
     121                if ( true === $this->attachments_created ) {
     122                        $this->remove_added_uploads();
     123                        $this->attachments_created = false;
     124                }
     125
     126                parent::tear_down();
    116127        }
    117128
     
    27722783        public function test_create_update_post_with_featured_media() {
    27732784
    2774                 $file                = DIR_TESTDATA . '/images/canola.jpg';
    2775                 $this->attachment_id = $this->factory->attachment->create_object(
     2785                $file          = DIR_TESTDATA . '/images/canola.jpg';
     2786                $attachment_id = $this->factory->attachment->create_object(
    27762787                        $file,
    27772788                        0,
     
    27822793                );
    27832794
     2795                $this->attachments_created = true;
     2796
    27842797                wp_set_current_user( self::$editor_id );
    27852798
     
    27872800                $params  = $this->set_post_data(
    27882801                        array(
    2789                                 'featured_media' => $this->attachment_id,
     2802                                'featured_media' => $attachment_id,
    27902803                        )
    27912804                );
     
    27942807                $data     = $response->get_data();
    27952808                $new_post = get_post( $data['id'] );
    2796                 $this->assertSame( $this->attachment_id, $data['featured_media'] );
    2797                 $this->assertSame( $this->attachment_id, (int) get_post_thumbnail_id( $new_post->ID ) );
     2809                $this->assertSame( $attachment_id, $data['featured_media'] );
     2810                $this->assertSame( $attachment_id, (int) get_post_thumbnail_id( $new_post->ID ) );
    27982811
    27992812                $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . $new_post->ID );
     
    52785291        }
    52795292
    5280         public function tear_down() {
    5281                 if ( isset( $this->attachment_id ) ) {
    5282                         $this->remove_added_uploads();
    5283                 }
    5284 
    5285                 parent::tear_down();
    5286         }
    5287 
    52885293        /**
    52895294         * Internal function used to disable an insert query which
Note: See TracChangeset for help on using the changeset viewer.