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/user/query.php

    r51367 r51462  
    176176        );
    177177
    178         $this->assertContains( "ORDER BY $full_key", $q->query_orderby );
     178        $this->assertStringContainsString( "ORDER BY $full_key", $q->query_orderby );
    179179    }
    180180
     
    364364        );
    365365
    366         $this->assertContains( 'ORDER BY user_login', $q->query_orderby );
     366        $this->assertStringContainsString( 'ORDER BY user_login', $q->query_orderby );
    367367    }
    368368
     
    382382
    383383        $expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . self::$author_ids[1] . ',' . self::$author_ids[0] . ',' . self::$author_ids[3] . ' )';
    384         $this->assertContains( $expected_orderby, $q->query_orderby );
     384        $this->assertStringContainsString( $expected_orderby, $q->query_orderby );
    385385
    386386        // assertEquals() respects order but ignores type (get_results() returns numeric strings).
     
    403403
    404404        $expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . self::$author_ids[1] . ',' . self::$author_ids[0] . ',' . self::$author_ids[3] . ' )';
    405         $this->assertContains( $expected_orderby, $q->query_orderby );
     405        $this->assertStringContainsString( $expected_orderby, $q->query_orderby );
    406406
    407407        // assertEquals() respects order but ignores type (get_results() returns numeric strings).
     
    420420        );
    421421
    422         $this->assertContains( 'ORDER BY user_login ASC, user_nicename ASC', $q->query_orderby );
     422        $this->assertStringContainsString( 'ORDER BY user_login ASC, user_nicename ASC', $q->query_orderby );
    423423    }
    424424
     
    433433        );
    434434
    435         $this->assertContains( 'ORDER BY user_login ASC, user_nicename ASC', $q->query_orderby );
     435        $this->assertStringContainsString( 'ORDER BY user_login ASC, user_nicename ASC', $q->query_orderby );
    436436    }
    437437
     
    446446        );
    447447
    448         $this->assertContains( 'ORDER BY user_login ASC, user_nicename ASC', $q->query_orderby );
     448        $this->assertStringContainsString( 'ORDER BY user_login ASC, user_nicename ASC', $q->query_orderby );
    449449    }
    450450
     
    459459        );
    460460
    461         $this->assertContains( 'ORDER BY user_login', $q->query_orderby );
     461        $this->assertStringContainsString( 'ORDER BY user_login', $q->query_orderby );
    462462    }
    463463
     
    476476        );
    477477
    478         $this->assertContains( 'ORDER BY user_login DESC, user_nicename ASC, user_email DESC', $q->query_orderby );
     478        $this->assertStringContainsString( 'ORDER BY user_login DESC, user_nicename ASC, user_email DESC', $q->query_orderby );
    479479    }
    480480
     
    493493        );
    494494
    495         $this->assertContains( 'ORDER BY user_login DESC, user_email ASC', $q->query_orderby );
     495        $this->assertStringContainsString( 'ORDER BY user_login DESC, user_email ASC', $q->query_orderby );
    496496    }
    497497
     
    10661066        $expected = array( self::$author_ids[0] );
    10671067
    1068         $this->assertContains( "AND user_nicename = 'peter'", $q->query_where );
     1068        $this->assertStringContainsString( "AND user_nicename = 'peter'", $q->query_where );
    10691069        $this->assertSameSets( $expected, $found );
    10701070    }
     
    11041104        $expected = array( self::$author_ids[0], self::$author_ids[1], self::$author_ids[2] );
    11051105
    1106         $this->assertContains( "AND user_nicename IN ( 'peter','paul','mary' )", $q->query_where );
     1106        $this->assertStringContainsString( "AND user_nicename IN ( 'peter','paul','mary' )", $q->query_where );
    11071107        $this->assertSameSets( $expected, $found );
    11081108    }
     
    11421142        $expected_count = 10; // 13 total users minus 3 from query.
    11431143
    1144         $this->assertContains( "AND user_nicename NOT IN ( 'peter','paul','mary' )", $q->query_where );
     1144        $this->assertStringContainsString( "AND user_nicename NOT IN ( 'peter','paul','mary' )", $q->query_where );
    11451145        $this->assertSame( $expected_count, $found_count );
    11461146    }
     
    11811181        $expected = array( self::$author_ids[2], self::$author_ids[0], self::$author_ids[1] );
    11821182
    1183         $this->assertContains( "FIELD( user_nicename, 'mary','peter','paul' )", $q->query_orderby );
     1183        $this->assertStringContainsString( "FIELD( user_nicename, 'mary','peter','paul' )", $q->query_orderby );
    11841184        $this->assertSame( $expected, $found );
    11851185    }
     
    12011201        $expected = array( self::$author_ids[0] );
    12021202
    1203         $this->assertContains( "AND user_login = '$user_login'", $q->query_where );
     1203        $this->assertStringContainsString( "AND user_login = '$user_login'", $q->query_where );
    12041204        $this->assertSameSets( $expected, $found );
    12051205    }
     
    12221222        $expected = array( self::$author_ids[0], self::$author_ids[1], self::$author_ids[2] );
    12231223
    1224         $this->assertContains( "AND user_login IN ( '$user_login1','$user_login2','$user_login3' )", $q->query_where );
     1224        $this->assertStringContainsString( "AND user_login IN ( '$user_login1','$user_login2','$user_login3' )", $q->query_where );
    12251225        $this->assertSameSets( $expected, $found );
    12261226    }
     
    12431243        $expected_count = 10; // 13 total users minus 3 from query.
    12441244
    1245         $this->assertContains( "AND user_login NOT IN ( '$user_login1','$user_login2','$user_login3' )", $q->query_where );
     1245        $this->assertStringContainsString( "AND user_login NOT IN ( '$user_login1','$user_login2','$user_login3' )", $q->query_where );
    12461246        $this->assertSame( $expected_count, $found_count );
    12471247    }
     
    12651265        $expected = array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] );
    12661266
    1267         $this->assertContains( "FIELD( user_login, '$user_login2','$user_login3','$user_login1' )", $q->query_orderby );
     1267        $this->assertStringContainsString( "FIELD( user_login, '$user_login2','$user_login3','$user_login1' )", $q->query_orderby );
    12681268        $this->assertSame( $expected, $found );
    12691269    }
Note: See TracChangeset for help on using the changeset viewer.