Make WordPress Core


Ignore:
Timestamp:
07/19/2021 02:00:11 PM (2 years 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/widgets/media-image-widget.php

    r49215 r51462  
    451451
    452452        // No default title.
    453         $this->assertNotContains( 'title="', $output );
     453        $this->assertStringNotContainsString( 'title="', $output );
    454454        // Default image classes.
    455         $this->assertContains( 'class="image wp-image-' . $attachment_id, $output );
    456         $this->assertContains( 'style="max-width: 100%; height: auto;"', $output );
    457         $this->assertContains( 'alt=""', $output );
     455        $this->assertStringContainsString( 'class="image wp-image-' . $attachment_id, $output );
     456        $this->assertStringContainsString( 'style="max-width: 100%; height: auto;"', $output );
     457        $this->assertStringContainsString( 'alt=""', $output );
    458458
    459459        ob_start();
     
    472472
    473473        // Custom image title.
    474         $this->assertContains( 'title="Custom Title"', $output );
     474        $this->assertStringContainsString( 'title="Custom Title"', $output );
    475475        // Custom image class.
    476         $this->assertContains( 'class="image wp-image-' . $attachment_id . ' custom-class', $output );
    477         $this->assertContains( 'alt="A flower"', $output );
    478         $this->assertContains( 'width="100"', $output );
    479         $this->assertContains( 'height="100"', $output );
     476        $this->assertStringContainsString( 'class="image wp-image-' . $attachment_id . ' custom-class', $output );
     477        $this->assertStringContainsString( 'alt="A flower"', $output );
     478        $this->assertStringContainsString( 'width="100"', $output );
     479        $this->assertStringContainsString( 'height="100"', $output );
    480480
    481481        // Embeded images.
     
    494494
    495495        // Custom image class.
    496         $this->assertContains( 'src="http://example.org/url/to/image.jpg"', $output );
     496        $this->assertStringContainsString( 'src="http://example.org/url/to/image.jpg"', $output );
    497497
    498498        // Link settings.
     
    507507
    508508        $link = '<a href="' . wp_get_attachment_url( $attachment_id ) . '"';
    509         $this->assertContains( $link, $output );
     509        $this->assertStringContainsString( $link, $output );
    510510        $this->assertTrue( (bool) preg_match( '#<a href.*?>#', $output, $matches ) );
    511         $this->assertNotContains( ' class="', $matches[0] );
    512         $this->assertNotContains( ' rel="', $matches[0] );
    513         $this->assertNotContains( ' target="', $matches[0] );
     511        $this->assertStringNotContainsString( ' class="', $matches[0] );
     512        $this->assertStringNotContainsString( ' rel="', $matches[0] );
     513        $this->assertStringNotContainsString( ' target="', $matches[0] );
    514514
    515515        ob_start();
     
    525525        $output = ob_get_clean();
    526526
    527         $this->assertContains( '<a href="' . get_attachment_link( $attachment_id ) . '"', $output );
    528         $this->assertContains( 'class="custom-link-class"', $output );
    529         $this->assertContains( 'rel="attachment"', $output );
    530         $this->assertNotContains( 'target=""', $output );
     527        $this->assertStringContainsString( '<a href="' . get_attachment_link( $attachment_id ) . '"', $output );
     528        $this->assertStringContainsString( 'class="custom-link-class"', $output );
     529        $this->assertStringContainsString( 'rel="attachment"', $output );
     530        $this->assertStringNotContainsString( 'target=""', $output );
    531531
    532532        ob_start();
     
    541541        $output = ob_get_clean();
    542542
    543         $this->assertContains( '<a href="https://example.org"', $output );
    544         $this->assertContains( 'target="_blank"', $output );
    545         $this->assertContains( 'rel="noopener"', $output );
     543        $this->assertStringContainsString( '<a href="https://example.org"', $output );
     544        $this->assertStringContainsString( 'target="_blank"', $output );
     545        $this->assertStringContainsString( 'rel="noopener"', $output );
    546546
    547547        // Populate caption in attachment.
     
    561561        );
    562562        $output = ob_get_clean();
    563         $this->assertNotContains( 'wp-caption', $output );
    564         $this->assertNotContains( '<p class="wp-caption-text">', $output );
     563        $this->assertStringNotContainsString( 'wp-caption', $output );
     564        $this->assertStringNotContainsString( '<p class="wp-caption-text">', $output );
    565565
    566566        // If the caption is explicitly null, then the caption of the underlying attachment will be displayed.
     
    573573        );
    574574        $output = ob_get_clean();
    575         $this->assertContains( 'class="wp-caption alignnone"', $output );
    576         $this->assertContains( '<p class="wp-caption-text">Default caption</p>', $output );
     575        $this->assertStringContainsString( 'class="wp-caption alignnone"', $output );
     576        $this->assertStringContainsString( '<p class="wp-caption-text">Default caption</p>', $output );
    577577
    578578        // If caption is provided, then it will be displayed.
     
    585585        );
    586586        $output = ob_get_clean();
    587         $this->assertContains( 'class="wp-caption alignnone"', $output );
    588         $this->assertContains( '<p class="wp-caption-text">Custom caption</p>', $output );
     587        $this->assertStringContainsString( 'class="wp-caption alignnone"', $output );
     588        $this->assertStringContainsString( '<p class="wp-caption-text">Custom caption</p>', $output );
    589589
    590590        // Attachments with custom sizes can render captions.
     
    600600        );
    601601        $output = ob_get_clean();
    602         $this->assertContains( 'style="width: 310px"', $output );
    603         $this->assertContains( '<p class="wp-caption-text">Caption for an image with custom size</p>', $output );
     602        $this->assertStringContainsString( 'style="width: 310px"', $output );
     603        $this->assertStringContainsString( '<p class="wp-caption-text">Caption for an image with custom size</p>', $output );
    604604    }
    605605
     
    629629        $output = ob_get_clean();
    630630
    631         $this->assertContains( '<script type="text/html" id="tmpl-wp-media-widget-image-preview">', $output );
     631        $this->assertStringContainsString( '<script type="text/html" id="tmpl-wp-media-widget-image-preview">', $output );
    632632    }
    633633}
Note: See TracChangeset for help on using the changeset viewer.