WordPress.org

Make WordPress Core


Ignore:
Timestamp:
07/19/2021 02:00:11 PM (6 months ago)
Author:
SergeyBiryukov
Message:

Tests: Replace assertContains() with assertStringContainsString() when used with strings.

Using the assertContains() and assertNotContains() methods with string haystacks was deprecated in PHPUnit 8 and removed in PHPUnit 9.

While WordPress test suite currently only supports PHPUnit up to 7.5.x, this allows us to switch to newer assertions ahead of adding full support for PHPUnit 8+.

These methods introduced in PHPUnit 7.5 should be used as an alternative:

  • assertStringContainsString()
  • assertStringContainsStringIgnoringCase
  • assertStringNotContainsString()
  • assertStringNotContainsStringIgnoringCase

As WordPress currently uses PHPUnit 5.7.x to run tests on PHP 5.6, polyfills for these methods were added to the WP_UnitTestCase class for PHPUnit < 7.5.

Follow-up to [51331], [51451], [51461].

Props jrf, dd32, SergeyBiryukov.
See #53363, #46149.

File:
1 edited

Legend:

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

    r51451 r51462  
    824824        );
    825825
    826         $this->assertContains( 'src="https://example.com/foo.mp3', $actual );
    827         $this->assertNotContains( 'loop', $actual );
    828         $this->assertNotContains( 'autoplay', $actual );
    829         $this->assertContains( 'preload="none"', $actual );
    830         $this->assertContains( 'class="wp-audio-shortcode"', $actual );
    831         $this->assertContains( 'style="width: 100%;"', $actual );
     826        $this->assertStringContainsString( 'src="https://example.com/foo.mp3', $actual );
     827        $this->assertStringNotContainsString( 'loop', $actual );
     828        $this->assertStringNotContainsString( 'autoplay', $actual );
     829        $this->assertStringContainsString( 'preload="none"', $actual );
     830        $this->assertStringContainsString( 'class="wp-audio-shortcode"', $actual );
     831        $this->assertStringContainsString( 'style="width: 100%;"', $actual );
    832832
    833833        $actual = wp_audio_shortcode(
     
    842842        );
    843843
    844         $this->assertContains( 'src="https://example.com/foo.mp3', $actual );
    845         $this->assertContains( 'loop="1"', $actual );
    846         $this->assertContains( 'autoplay="1"', $actual );
    847         $this->assertContains( 'preload="1"', $actual );
    848         $this->assertContains( 'class="foobar"', $actual );
    849         $this->assertContains( 'style="padding:0;"', $actual );
     844        $this->assertStringContainsString( 'src="https://example.com/foo.mp3', $actual );
     845        $this->assertStringContainsString( 'loop="1"', $actual );
     846        $this->assertStringContainsString( 'autoplay="1"', $actual );
     847        $this->assertStringContainsString( 'preload="1"', $actual );
     848        $this->assertStringContainsString( 'class="foobar"', $actual );
     849        $this->assertStringContainsString( 'style="padding:0;"', $actual );
    850850    }
    851851
     
    929929        );
    930930
    931         $this->assertContains( 'src="https://example.com/foo.mp4', $actual );
    932         $this->assertNotContains( 'loop', $actual );
    933         $this->assertNotContains( 'autoplay', $actual );
    934         $this->assertContains( 'preload="metadata"', $actual );
    935         $this->assertContains( 'width="640"', $actual );
    936         $this->assertContains( 'height="360"', $actual );
    937         $this->assertContains( 'class="wp-video-shortcode"', $actual );
     931        $this->assertStringContainsString( 'src="https://example.com/foo.mp4', $actual );
     932        $this->assertStringNotContainsString( 'loop', $actual );
     933        $this->assertStringNotContainsString( 'autoplay', $actual );
     934        $this->assertStringContainsString( 'preload="metadata"', $actual );
     935        $this->assertStringContainsString( 'width="640"', $actual );
     936        $this->assertStringContainsString( 'height="360"', $actual );
     937        $this->assertStringContainsString( 'class="wp-video-shortcode"', $actual );
    938938
    939939        $actual = wp_video_shortcode(
     
    950950        );
    951951
    952         $this->assertContains( 'src="https://example.com/foo.mp4', $actual );
    953         $this->assertContains( 'poster="https://example.com/foo.png', $actual );
    954         $this->assertContains( 'loop="1"', $actual );
    955         $this->assertContains( 'autoplay="1"', $actual );
    956         $this->assertContains( 'preload="1"', $actual );
    957         $this->assertContains( 'width="123"', $actual );
    958         $this->assertContains( 'height="456"', $actual );
    959         $this->assertContains( 'class="foobar"', $actual );
     952        $this->assertStringContainsString( 'src="https://example.com/foo.mp4', $actual );
     953        $this->assertStringContainsString( 'poster="https://example.com/foo.png', $actual );
     954        $this->assertStringContainsString( 'loop="1"', $actual );
     955        $this->assertStringContainsString( 'autoplay="1"', $actual );
     956        $this->assertStringContainsString( 'preload="1"', $actual );
     957        $this->assertStringContainsString( 'width="123"', $actual );
     958        $this->assertStringContainsString( 'height="456"', $actual );
     959        $this->assertStringContainsString( 'class="foobar"', $actual );
    960960    }
    961961
     
    971971        );
    972972
    973         $this->assertNotContains( 'feature=youtu.be', $actual );
     973        $this->assertStringNotContainsString( 'feature=youtu.be', $actual );
    974974    }
    975975
     
    985985        );
    986986
    987         $this->assertContains( 'src="https://www.youtube.com/watch?v=72xdCU__XCk', $actual );
     987        $this->assertStringContainsString( 'src="https://www.youtube.com/watch?v=72xdCU__XCk', $actual );
    988988    }
    989989
     
    999999        );
    10001000
    1001         $this->assertContains( 'src="https://vimeo.com/76979871', $actual );
    1002         $this->assertNotContains( 'blah=meh', $actual );
     1001        $this->assertStringContainsString( 'src="https://vimeo.com/76979871', $actual );
     1002        $this->assertStringNotContainsString( 'blah=meh', $actual );
    10031003    }
    10041004
     
    10141014        );
    10151015
    1016         $this->assertContains( 'src="https://vimeo.com/76979871?loop=0', $actual );
     1016        $this->assertStringContainsString( 'src="https://vimeo.com/76979871?loop=0', $actual );
    10171017    }
    10181018
     
    10291029        );
    10301030
    1031         $this->assertContains( 'src="https://vimeo.com/76979871?loop=1', $actual );
     1031        $this->assertStringContainsString( 'src="https://vimeo.com/76979871?loop=1', $actual );
    10321032    }
    10331033
     
    28632863        $img = wp_img_tag_add_loading_attr( $img, 'test' );
    28642864
    2865         $this->assertContains( ' loading="lazy"', $img );
     2865        $this->assertStringContainsString( ' loading="lazy"', $img );
    28662866    }
    28672867
     
    28742874        $img = wp_img_tag_add_loading_attr( $img, 'test' );
    28752875
    2876         $this->assertNotContains( ' loading=', $img );
     2876        $this->assertStringNotContainsString( ' loading=', $img );
    28772877    }
    28782878
     
    28852885        $img = wp_img_tag_add_loading_attr( $img, 'test' );
    28862886
    2887         $this->assertNotContains( ' loading=', $img );
     2887        $this->assertStringNotContainsString( ' loading=', $img );
    28882888
    28892889        // Test specifically that the attribute is not there with double-quotes,
    28902890        // to avoid regressions.
    2891         $this->assertNotContains( ' loading="lazy"', $img );
     2891        $this->assertStringNotContainsString( ' loading="lazy"', $img );
    28922892    }
    28932893
     
    29002900        add_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
    29012901
    2902         $this->assertNotContains( ' loading=', $img );
     2902        $this->assertStringNotContainsString( ' loading=', $img );
    29032903    }
    29042904
     
    29102910        $iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
    29112911
    2912         $this->assertContains( ' loading="lazy"', $iframe );
     2912        $this->assertStringContainsString( ' loading="lazy"', $iframe );
    29132913    }
    29142914
     
    29202920        $iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
    29212921
    2922         $this->assertNotContains( ' loading=', $iframe );
     2922        $this->assertStringNotContainsString( ' loading=', $iframe );
    29232923    }
    29242924
     
    29302930        $iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
    29312931
    2932         $this->assertNotContains( ' loading=', $iframe );
     2932        $this->assertStringNotContainsString( ' loading=', $iframe );
    29332933
    29342934        // Test specifically that the attribute is not there with double-quotes,
    29352935        // to avoid regressions.
    2936         $this->assertNotContains( ' loading="lazy"', $iframe );
     2936        $this->assertStringNotContainsString( ' loading="lazy"', $iframe );
    29372937    }
    29382938
     
    29452945        $iframe = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
    29462946
    2947         $this->assertNotContains( ' loading=', $iframe );
     2947        $this->assertStringNotContainsString( ' loading=', $iframe );
    29482948    }
    29492949
     
    29572957        $iframe   = wp_iframe_tag_add_loading_attr( $iframe, 'test' );
    29582958
    2959         $this->assertNotContains( ' loading=', $iframe );
     2959        $this->assertStringNotContainsString( ' loading=', $iframe );
    29602960    }
    29612961
     
    29672967        $img = wp_get_attachment_image( self::$large_id );
    29682968
    2969         $this->assertContains( ' loading="lazy"', $img );
     2969        $this->assertStringContainsString( ' loading="lazy"', $img );
    29702970    }
    29712971
     
    29792979
    29802980        // There should not be any loading attribute in this case.
    2981         $this->assertNotContains( ' loading=', $img );
     2981        $this->assertStringNotContainsString( ' loading=', $img );
    29822982    }
    29832983
     
    29942994
    29952995        // There should not be any loading attribute in this case.
    2996         $this->assertNotContains( ' loading=', $img );
     2996        $this->assertStringNotContainsString( ' loading=', $img );
    29972997    }
    29982998
     
    31173117            )
    31183118        );
    3119         $this->assertContains( '?attachment_id=', $actual );
     3119        $this->assertStringContainsString( '?attachment_id=', $actual );
    31203120
    31213121        // File: Links to image file URL.
     
    31353135            )
    31363136        );
    3137         $this->assertNotContains( '<a ', $actual );
     3137        $this->assertStringNotContainsString( '<a ', $actual );
    31383138    }
    31393139
Note: See TracChangeset for help on using the changeset viewer.