Make WordPress Core


Ignore:
Timestamp:
07/19/2021 02:00:11 PM (4 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/admin/includesPost.php

    r51027 r51462  
    392392
    393393        $found = get_sample_permalink_html( $p );
    394         $this->assertContains( 'href="' . get_option( 'home' ) . '/?p=' . $p . '"', $found );
    395         $this->assertContains( '>' . get_option( 'home' ) . '/?p=' . $p . '<', $found );
     394        $this->assertStringContainsString( 'href="' . get_option( 'home' ) . '/?p=' . $p . '"', $found );
     395        $this->assertStringContainsString( '>' . get_option( 'home' ) . '/?p=' . $p . '<', $found );
    396396    }
    397397
     
    416416        $found = get_sample_permalink_html( $p );
    417417        $post  = get_post( $p );
    418         $this->assertContains( 'href="' . get_option( 'home' ) . '/' . $post->post_name . '/"', $found );
    419         $this->assertContains( '>' . urldecode( $post->post_name ) . '<', $found );
     418        $this->assertStringContainsString( 'href="' . get_option( 'home' ) . '/' . $post->post_name . '/"', $found );
     419        $this->assertStringContainsString( '>' . urldecode( $post->post_name ) . '<', $found );
    420420    }
    421421
     
    441441        $found = get_sample_permalink_html( $p );
    442442        $post  = get_post( $p );
    443         $this->assertContains( 'href="' . get_option( 'home' ) . '/' . $post->post_name . '/"', $found );
    444         $this->assertContains( '>' . urldecode( get_permalink( $post ) ) . '<', $found );
     443        $this->assertStringContainsString( 'href="' . get_option( 'home' ) . '/' . $post->post_name . '/"', $found );
     444        $this->assertStringContainsString( '>' . urldecode( get_permalink( $post ) ) . '<', $found );
    445445    }
    446446
     
    465465        $post    = get_post( $p );
    466466        $message = 'Published post';
    467         $this->assertContains( 'href="' . get_option( 'home' ) . '/' . $post->post_name . '/"', $found, $message );
    468         $this->assertContains( '>new_slug-صورة<', $found, $message );
     467        $this->assertStringContainsString( 'href="' . get_option( 'home' ) . '/' . $post->post_name . '/"', $found, $message );
     468        $this->assertStringContainsString( '>new_slug-صورة<', $found, $message );
    469469
    470470        // Scheduled posts should use published permalink.
     
    481481        $post    = get_post( $p );
    482482        $message = 'Scheduled post';
    483         $this->assertContains( 'href="' . get_option( 'home' ) . '/' . $post->post_name . '/"', $found, $message );
    484         $this->assertContains( '>new_slug-صورة<', $found, $message );
     483        $this->assertStringContainsString( 'href="' . get_option( 'home' ) . '/' . $post->post_name . '/"', $found, $message );
     484        $this->assertStringContainsString( '>new_slug-صورة<', $found, $message );
    485485
    486486        // Draft posts should use preview link.
     
    499499        $preview_link = add_query_arg( 'preview', 'true', $preview_link );
    500500
    501         $this->assertContains( 'href="' . esc_url( $preview_link ) . '"', $found, $message );
    502         $this->assertContains( '>new_slug-صورة<', $found, $message );
     501        $this->assertStringContainsString( 'href="' . esc_url( $preview_link ) . '"', $found, $message );
     502        $this->assertStringContainsString( '>new_slug-صورة<', $found, $message );
    503503    }
    504504
     
    523523        $found = get_sample_permalink_html( $p );
    524524        $post  = get_post( $p );
    525         $this->assertContains( 'href="' . esc_url( get_preview_post_link( $post ) ), $found );
     525        $this->assertStringContainsString( 'href="' . esc_url( get_preview_post_link( $post ) ), $found );
    526526    }
    527527
Note: See TracChangeset for help on using the changeset viewer.