Make WordPress Core

Changeset 53558


Ignore:
Timestamp:
06/23/2022 03:01:19 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Remove dynamic properties in Tests_Media.

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 class contains a set_up() method that sets a group of properties, which are used by the tests, but never changed by the tests.

In other words, setting these properties in the set_up() is an unnecessary overhead and the properties should be changed to class constants.

Notes:

  • As the $img_html property, which was previously being set, is not actually used in any of the tests, that property has not been converted to a constant.
  • The values which were previously being set using a heredoc, now use a nowdoc (supported since PHP 5.3), as they don't contain any interpolation.
  • The use of constant scalar expressions (IMG_URL) and constant arrays (IMG_META) in class constants is supported since PHP 5.6.

Follow-up to [711/tests], [1260/tests], [34855], [41724], [53557].

Props jrf.
See #56033.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/media.php

    r53481 r53558  
    66 */
    77class Tests_Media extends WP_UnitTestCase {
     8
     9    const CAPTION           = 'A simple caption.';
     10    const ALTERNATE_CAPTION = 'Alternate caption.';
     11
     12    const HTML_CONTENT = <<<'CAP'
     13A <strong class='classy'>bolded</strong> <em>caption</em> with a <a href="#">link</a>.
     14CAP;
     15    const IMG_CONTENT  = <<<'CAP'
     16<img src="pic.jpg" id='anId' alt="pic"/>
     17CAP;
     18
     19    const IMG_NAME = 'image.jpg';
     20    const IMG_URL  = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . self::IMG_NAME;
     21    const IMG_META = array(
     22        'width'  => 100,
     23        'height' => 100,
     24        'sizes'  => '',
     25    );
     26
    827    protected static $large_id;
    928    protected static $_sizes;
     
    5776    }
    5877
    59     public function set_up() {
    60         parent::set_up();
    61         $this->caption           = 'A simple caption.';
    62         $this->alternate_caption = 'Alternate caption.';
    63         $this->html_content      = <<<CAP
    64 A <strong class='classy'>bolded</strong> <em>caption</em> with a <a href="#">link</a>.
    65 CAP;
    66         $this->img_content       = <<<CAP
    67 <img src="pic.jpg" id='anId' alt="pic"/>
    68 CAP;
    69         $this->img_name          = 'image.jpg';
    70         $this->img_url           = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $this->img_name;
    71         $this->img_html          = '<img src="' . $this->img_url . '"/>';
    72         $this->img_meta          = array(
    73             'width'  => 100,
    74             'height' => 100,
    75             'sizes'  => '',
    76         );
    77     }
    78 
    7978    public function test_img_caption_shortcode_added() {
    8079        global $shortcode_tags;
     
    9291     */
    9392    public function test_img_caption_shortcode_with_empty_params_but_content() {
    94         $result = img_caption_shortcode( array(), $this->caption );
    95         $this->assertSame( $this->caption, $result );
     93        $result = img_caption_shortcode( array(), self::CAPTION );
     94        $this->assertSame( self::CAPTION, $result );
    9695    }
    9796
     
    102101        add_filter( 'img_caption_shortcode', array( $this, 'return_alt_caption' ) );
    103102
    104         $result = img_caption_shortcode( array(), $this->caption );
    105         $this->assertSame( $this->alternate_caption, $result );
     103        $result = img_caption_shortcode( array(), self::CAPTION );
     104        $this->assertSame( self::ALTERNATE_CAPTION, $result );
    106105    }
    107106
     
    110109     */
    111110    public function return_alt_caption() {
    112         return $this->alternate_caption;
     111        return self::ALTERNATE_CAPTION;
    113112    }
    114113
     
    121120                'width' => 0,
    122121            ),
    123             $this->caption
    124         );
    125         $this->assertSame( $this->caption, $result );
     122            self::CAPTION
     123        );
     124        $this->assertSame( self::CAPTION, $result );
    126125    }
    127126
     
    146145                'caption' => '',
    147146            ),
    148             $this->caption
    149         );
    150         $this->assertSame( $this->caption, $result );
     147            self::CAPTION
     148        );
     149        $this->assertSame( self::CAPTION, $result );
    151150    }
    152151
     
    155154            array(
    156155                'width'   => 20,
    157                 'caption' => $this->caption,
     156                'caption' => self::CAPTION,
    158157            )
    159158        );
     
    161160        $this->assertSame( 2, preg_match_all( '/wp-caption/', $result, $_r ) );
    162161        $this->assertSame( 1, preg_match_all( '/alignnone/', $result, $_r ) );
    163         $this->assertSame( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) );
     162        $this->assertSame( 1, preg_match_all( '/' . self::CAPTION . '/', $result, $_r ) );
    164163
    165164        if ( current_theme_supports( 'html5', 'caption' ) ) {
     
    174173            array(
    175174                'width'   => 20,
    176                 'caption' => $this->caption,
     175                'caption' => self::CAPTION,
    177176                'id'      => '"myId',
    178177                'align'   => '&myAlignment',
     
    181180        $this->assertSame( 1, preg_match_all( '/wp-caption &amp;myAlignment/', $result, $_r ) );
    182181        $this->assertSame( 1, preg_match_all( '/id="myId"/', $result, $_r ) );
    183         $this->assertSame( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) );
     182        $this->assertSame( 1, preg_match_all( '/' . self::CAPTION . '/', $result, $_r ) );
    184183    }
    185184
     
    189188                'width'   => 20,
    190189                'class'   => 'some-class another-class',
    191                 'caption' => $this->caption,
     190                'caption' => self::CAPTION,
    192191            )
    193192        );
     
    200199            array(
    201200                'width'   => 20,
    202                 'caption' => $this->html_content,
     201                'caption' => self::HTML_CONTENT,
    203202            )
    204203        );
    205         $our_preg = preg_quote( $this->html_content );
     204        $our_preg = preg_quote( self::HTML_CONTENT );
    206205
    207206        $this->assertSame( 1, preg_match_all( "~{$our_preg}~", $result, $_r ) );
     
    211210        $result       = img_caption_shortcode(
    212211            array( 'width' => 20 ),
    213             $this->img_content . $this->html_content
    214         );
    215         $img_preg     = preg_quote( $this->img_content );
    216         $content_preg = preg_quote( $this->html_content );
     212            self::IMG_CONTENT . self::HTML_CONTENT
     213        );
     214        $img_preg     = preg_quote( self::IMG_CONTENT );
     215        $content_preg = preg_quote( self::HTML_CONTENT );
    217216
    218217        $this->assertSame( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
     
    221220
    222221    public function test_new_img_caption_shortcode_new_format_and_linked_image() {
    223         $linked_image = "<a href='#'>{$this->img_content}</a>";
     222        $linked_image = "<a href='#'>" . self::IMG_CONTENT . '</a>';
    224223        $result       = img_caption_shortcode(
    225224            array( 'width' => 20 ),
    226             $linked_image . $this->html_content
     225            $linked_image . self::HTML_CONTENT
    227226        );
    228227        $img_preg     = preg_quote( $linked_image );
    229         $content_preg = preg_quote( $this->html_content );
     228        $content_preg = preg_quote( self::HTML_CONTENT );
    230229
    231230        $this->assertSame( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
     
    234233
    235234    public function test_new_img_caption_shortcode_new_format_and_linked_image_with_newline() {
    236         $linked_image = "<a href='#'>{$this->img_content}</a>";
     235        $linked_image = "<a href='#'>" . self::IMG_CONTENT . '</a>';
    237236        $result       = img_caption_shortcode(
    238237            array( 'width' => 20 ),
    239             $linked_image . "\n\n" . $this->html_content
     238            $linked_image . "\n\n" . self::HTML_CONTENT
    240239        );
    241240        $img_preg     = preg_quote( $linked_image );
    242         $content_preg = preg_quote( $this->html_content );
     241        $content_preg = preg_quote( self::HTML_CONTENT );
    243242
    244243        $this->assertSame( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
     
    255254                'id'    => 'myId',
    256255            ),
    257             $this->img_content . $this->html_content
     256            self::IMG_CONTENT . self::HTML_CONTENT
    258257        );
    259258
     
    495494        $post_id       = self::factory()->post->create();
    496495        $attachment_id = self::factory()->attachment->create_object(
    497             $this->img_name,
     496            self::IMG_NAME,
    498497            $post_id,
    499498            array(
     
    522521                )
    523522            );
    524             $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
     523            $metadata      = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META );
    525524            wp_update_attachment_metadata( $attachment_id, $metadata );
    526525            $ids1[]      = $attachment_id;
     
    539538                )
    540539            );
    541             $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
     540            $metadata      = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META );
    542541            wp_update_attachment_metadata( $attachment_id, $metadata );
    543542            $ids2[]      = $attachment_id;
     
    573572                )
    574573            );
    575             $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
     574            $metadata      = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META );
    576575            wp_update_attachment_metadata( $attachment_id, $metadata );
    577576            $ids1[]      = $attachment_id;
     
    590589                )
    591590            );
    592             $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
     591            $metadata      = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META );
    593592            wp_update_attachment_metadata( $attachment_id, $metadata );
    594593            $ids2[]      = $attachment_id;
     
    623622                0
    624623            );
    625             $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
     624            $metadata      = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META );
    626625            wp_update_attachment_metadata( $attachment_id, $metadata );
    627626            $ids[]      = $attachment_id;
     
    661660                0
    662661            );
    663             $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
     662            $metadata      = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META );
    664663            wp_update_attachment_metadata( $attachment_id, $metadata );
    665664            $ids[]      = $attachment_id;
     
    703702                )
    704703            );
    705             $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
     704            $metadata      = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META );
    706705            wp_update_attachment_metadata( $attachment_id, $metadata );
    707706            $ids[]      = $attachment_id;
     
    746745                )
    747746            );
    748             $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
     747            $metadata      = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META );
    749748            wp_update_attachment_metadata( $attachment_id, $metadata );
    750749            $ids[]      = $attachment_id;
     
    789788                )
    790789            );
    791             $metadata      = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
     790            $metadata      = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META );
    792791            wp_update_attachment_metadata( $attachment_id, $metadata );
    793792            $ids[]      = $attachment_id;
     
    11821181     */
    11831182    public function test_attachment_url_to_postid() {
    1184         $image_path    = '2014/11/' . $this->img_name;
     1183        $image_path    = '2014/11/' . self::IMG_NAME;
    11851184        $attachment_id = self::factory()->attachment->create_object(
    11861185            $image_path,
     
    12001199     */
    12011200    public function test_attachment_url_to_postid_with_different_scheme() {
    1202         $image_path    = '2014/11/' . $this->img_name;
     1201        $image_path    = '2014/11/' . self::IMG_NAME;
    12031202        $attachment_id = self::factory()->attachment->create_object(
    12041203            $image_path,
     
    12181217     */
    12191218    public function test_attachment_url_to_postid_should_be_case_sensitive() {
    1220         $image_path_lower_case    = '2014/11/' . $this->img_name;
     1219        $image_path_lower_case    = '2014/11/' . self::IMG_NAME;
    12211220        $attachment_id_lower_case = self::factory()->attachment->create_object(
    12221221            $image_path_lower_case,
     
    12281227        );
    12291228
    1230         $image_path_upper_case    = '2014/11/' . ucfirst( $this->img_name );
     1229        $image_path_upper_case    = '2014/11/' . ucfirst( self::IMG_NAME );
    12311230        $attachment_id_upper_case = self::factory()->attachment->create_object(
    12321231            $image_path_upper_case,
     
    12431242
    12441243    public function test_attachment_url_to_postid_filtered() {
    1245         $image_path    = '2014/11/' . $this->img_name;
     1244        $image_path    = '2014/11/' . self::IMG_NAME;
    12461245        $attachment_id = self::factory()->attachment->create_object(
    12471246            $image_path,
     
    15331532        $post_id       = self::factory()->post->create();
    15341533        $attachment_id = self::factory()->attachment->create_object(
    1535             $this->img_name,
     1534            self::IMG_NAME,
    15361535            $post_id,
    15371536            array(
     
    15561555        $post_id       = self::factory()->post->create();
    15571556        $attachment_id = self::factory()->attachment->create_object(
    1558             $this->img_name,
     1557            self::IMG_NAME,
    15591558            $post_id,
    15601559            array(
     
    15761575        $post_id       = self::factory()->post->create();
    15771576        $attachment_id = self::factory()->attachment->create_object(
    1578             $this->img_name,
     1577            self::IMG_NAME,
    15791578            $post_id,
    15801579            array(
     
    33343333    public function test_wp_image_file_matches_image_meta_invalid_meta() {
    33353334        $image_meta = ''; // Attachment is not an image.
    3336         $image_src  = $this->img_url;
     3335        $image_src  = self::IMG_URL;
    33373336
    33383337        $this->assertFalse( wp_image_file_matches_image_meta( $image_src, $image_meta ) );
     
    33443343    public function test_wp_image_file_matches_image_meta_different_meta() {
    33453344        $image_meta = wp_get_attachment_metadata( self::$large_id );
    3346         $image_src  = $this->img_url; // Different image.
     3345        $image_src  = self::IMG_URL; // Different image.
    33473346
    33483347        $this->assertFalse( wp_image_file_matches_image_meta( $image_src, $image_meta ) );
Note: See TracChangeset for help on using the changeset viewer.