WordPress.org

Make WordPress Core

Changeset 51461


Ignore:
Timestamp:
07/19/2021 01:29:45 PM (3 months ago)
Author:
SergeyBiryukov
Message:

Tests: Use more appropriate assertions in various tests.

This replaces instances of assertFalse( stripos( ... ) ) with assertStringNotContainsString() or assertStringNotContainsStringIgnoringCase() to use native PHPUnit functionality.

Going forward, these methods introduced in PHPUnit 7.5 should be used for similar assertions:

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

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

Follow-up to [51335], [51337], [51367], [51397], [51403], [51404], [51436], [51438], [51448], [51449], [51451], [51453], [51454].

See #53363.

Location:
trunk/tests/phpunit
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/testcase.php

    r51451 r51461  
    391391
    392392    /**
    393      * Asserts that a string haystack does not contain a needle.
     393     * Asserts that a string haystack contains a needle (case-insensitive).
    394394     *
    395395     * This method has been backported from a more recent PHPUnit version,
     
    402402     * @param string $message  Optional. Message to display when the assertion fails.
    403403     */
     404    public static function assertStringContainsStringIgnoringCase( $needle, $haystack, $message = '' ) {
     405        static::assertContains( $needle, $haystack, $message, true );
     406    }
     407
     408    /**
     409     * Asserts that a string haystack does not contain a needle.
     410     *
     411     * This method has been backported from a more recent PHPUnit version,
     412     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     413     *
     414     * @since 5.9.0
     415     *
     416     * @param string $needle   The string to search for.
     417     * @param string $haystack The string to treat as the haystack.
     418     * @param string $message  Optional. Message to display when the assertion fails.
     419     */
    404420    public static function assertStringNotContainsString( $needle, $haystack, $message = '' ) {
    405421        static::assertNotContains( $needle, $haystack, $message );
    406422    }
     423
     424    /**
     425     * Asserts that a string haystack does not contain a needle (case-insensitive).
     426     *
     427     * This method has been backported from a more recent PHPUnit version,
     428     * as tests running on PHP 5.6 use PHPUnit 5.7.x.
     429     *
     430     * @since 5.9.0
     431     *
     432     * @param string $needle   The string to search for.
     433     * @param string $haystack The string to treat as the haystack.
     434     * @param string $message  Optional. Message to display when the assertion fails.
     435     */
     436    public static function assertStringNotContainsStringIgnoringCase( $needle, $haystack, $message = '' ) {
     437        static::assertNotContains( $needle, $haystack, $message, true );
     438    }
    407439}
  • trunk/tests/phpunit/tests/rest-api/rest-categories-controller.php

    r51404 r51461  
    627627            rest_url( 'wp/v2/categories' )
    628628        );
    629         $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
     629        $this->assertStringNotContainsString( 'rel="prev"', $headers['Link'] );
    630630        $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
    631631
     
    671671        );
    672672        $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
    673         $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
     673        $this->assertStringNotContainsString( 'rel="next"', $headers['Link'] );
    674674
    675675        // Out of bounds.
     
    688688        );
    689689        $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
    690         $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
     690        $this->assertStringNotContainsString( 'rel="next"', $headers['Link'] );
    691691    }
    692692
  • trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r51404 r51461  
    822822            rest_url( '/wp/v2/comments' )
    823823        );
    824         $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
     824        $this->assertStringNotContainsString( 'rel="prev"', $headers['Link'] );
    825825        $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
    826826
     
    868868        );
    869869        $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
    870         $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
     870        $this->assertStringNotContainsString( 'rel="next"', $headers['Link'] );
    871871
    872872        // Out of bounds.
     
    884884        );
    885885        $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
    886         $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
     886        $this->assertStringNotContainsString( 'rel="next"', $headers['Link'] );
    887887    }
    888888
  • trunk/tests/phpunit/tests/rest-api/rest-pattern-directory-controller.php

    r51208 r51461  
    177177            $search_field_values = $pattern['title'] . ' ' . $pattern['description'];
    178178
    179             $this->assertNotFalse( stripos( $search_field_values, $search_term ) );
     179            $this->assertStringContainsStringIgnoringCase( $search_term, $search_field_values );
    180180        }
    181181    }
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r51405 r51461  
    15321532            rest_url( '/wp/v2/posts' )
    15331533        );
    1534         $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
     1534        $this->assertStringNotContainsString( 'rel="prev"', $headers['Link'] );
    15351535        $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
    15361536
     
    15741574        );
    15751575        $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
    1576         $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
     1576        $this->assertStringNotContainsString( 'rel="next"', $headers['Link'] );
    15771577
    15781578        // Out of bounds.
  • trunk/tests/phpunit/tests/rest-api/rest-revisions-controller.php

    r51367 r51461  
    483483            rest_url( $rest_route )
    484484        );
    485         $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
     485        $this->assertStringNotContainsString( 'rel="prev"', $headers['Link'] );
    486486        $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
    487487    }
  • trunk/tests/phpunit/tests/rest-api/rest-tags-controller.php

    r51404 r51461  
    591591            rest_url( 'wp/v2/tags' )
    592592        );
    593         $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
     593        $this->assertStringNotContainsString( 'rel="prev"', $headers['Link'] );
    594594        $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
    595595
     
    633633        );
    634634        $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
    635         $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
     635        $this->assertStringNotContainsString( 'rel="next"', $headers['Link'] );
    636636
    637637        // Out of bounds.
     
    649649        );
    650650        $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
    651         $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
     651        $this->assertStringNotContainsString( 'rel="next"', $headers['Link'] );
    652652    }
    653653
  • trunk/tests/phpunit/tests/rest-api/rest-users-controller.php

    r51404 r51461  
    322322            rest_url( 'wp/v2/users' )
    323323        );
    324         $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
     324        $this->assertStringNotContainsString( 'rel="prev"', $headers['Link'] );
    325325        $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
    326326
     
    364364        );
    365365        $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
    366         $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
     366        $this->assertStringNotContainsString( 'rel="next"', $headers['Link'] );
    367367
    368368        // Out of bounds.
     
    380380        );
    381381        $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
    382         $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
     382        $this->assertStringNotContainsString( 'rel="next"', $headers['Link'] );
    383383    }
    384384
Note: See TracChangeset for help on using the changeset viewer.