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

    r51436 r51462  
    320320
    321321        $q1 = new WP_Query( array( 'post_status' => 'any' ) );
    322         $this->assertContains( "post_status <> 'auto-draft'", $q1->request );
     322        $this->assertStringContainsString( "post_status <> 'auto-draft'", $q1->request );
    323323
    324324        $q2 = new WP_Query( array( 'post_status' => 'any, auto-draft' ) );
    325         $this->assertNotContains( "post_status <> 'auto-draft'", $q2->request );
     325        $this->assertStringNotContainsString( "post_status <> 'auto-draft'", $q2->request );
    326326
    327327        $q3 = new WP_Query( array( 'post_status' => array( 'any', 'auto-draft' ) ) );
    328         $this->assertNotContains( "post_status <> 'auto-draft'", $q3->request );
     328        $this->assertStringNotContainsString( "post_status <> 'auto-draft'", $q3->request );
    329329    }
    330330
     
    343343            )
    344344        );
    345         $this->assertContains(
     345        $this->assertStringContainsString(
    346346            "ORDER BY $wpdb->posts.post_type DESC, $wpdb->posts.post_name ASC",
    347347            $q1->request
     
    349349
    350350        $q2 = new WP_Query( array( 'orderby' => array() ) );
    351         $this->assertNotContains( 'ORDER BY', $q2->request );
    352         $this->assertNotContains( 'ORDER', $q2->request );
     351        $this->assertStringNotContainsString( 'ORDER BY', $q2->request );
     352        $this->assertStringNotContainsString( 'ORDER', $q2->request );
    353353
    354354        $q3 = new WP_Query( array( 'post_type' => 'post' ) );
    355         $this->assertContains(
     355        $this->assertStringContainsString(
    356356            "ORDER BY $wpdb->posts.post_date DESC",
    357357            $q3->request
     
    359359
    360360        $q4 = new WP_Query( array( 'post_type' => 'post' ) );
    361         $this->assertContains(
     361        $this->assertStringContainsString(
    362362            "ORDER BY $wpdb->posts.post_date DESC",
    363363            $q4->request
     
    378378            )
    379379        );
    380         $this->assertContains(
     380        $this->assertStringContainsString(
    381381            "ORDER BY $wpdb->posts.post_type DESC",
    382382            $q1->request
     
    389389            )
    390390        );
    391         $this->assertContains(
     391        $this->assertStringContainsString(
    392392            "ORDER BY $wpdb->posts.post_title DESC",
    393393            $q2->request
     
    399399            )
    400400        );
    401         $this->assertContains(
     401        $this->assertStringContainsString(
    402402            "ORDER BY $wpdb->posts.post_date ASC",
    403403            $q3->request
     
    411411        // 'rand' is a valid value.
    412412        $q = new WP_Query( array( 'orderby' => 'rand' ) );
    413         $this->assertContains( 'ORDER BY RAND()', $q->request );
    414         $this->assertNotContains( 'ASC', $q->request );
    415         $this->assertNotContains( 'DESC', $q->request );
     413        $this->assertStringContainsString( 'ORDER BY RAND()', $q->request );
     414        $this->assertStringNotContainsString( 'ASC', $q->request );
     415        $this->assertStringNotContainsString( 'DESC', $q->request );
    416416
    417417        // This isn't allowed.
    418418        $q2 = new WP_Query( array( 'order' => 'rand' ) );
    419         $this->assertContains( 'ORDER BY', $q2->request );
    420         $this->assertNotContains( 'RAND()', $q2->request );
    421         $this->assertContains( 'DESC', $q2->request );
     419        $this->assertStringContainsString( 'ORDER BY', $q2->request );
     420        $this->assertStringNotContainsString( 'RAND()', $q2->request );
     421        $this->assertStringContainsString( 'DESC', $q2->request );
    422422
    423423        // 'none' is a valid value.
    424424        $q3 = new WP_Query( array( 'orderby' => 'none' ) );
    425         $this->assertNotContains( 'ORDER BY', $q3->request );
    426         $this->assertNotContains( 'DESC', $q3->request );
    427         $this->assertNotContains( 'ASC', $q3->request );
     425        $this->assertStringNotContainsString( 'ORDER BY', $q3->request );
     426        $this->assertStringNotContainsString( 'DESC', $q3->request );
     427        $this->assertStringNotContainsString( 'ASC', $q3->request );
    428428
    429429        // False is a valid value.
    430430        $q4 = new WP_Query( array( 'orderby' => false ) );
    431         $this->assertNotContains( 'ORDER BY', $q4->request );
    432         $this->assertNotContains( 'DESC', $q4->request );
    433         $this->assertNotContains( 'ASC', $q4->request );
     431        $this->assertStringNotContainsString( 'ORDER BY', $q4->request );
     432        $this->assertStringNotContainsString( 'DESC', $q4->request );
     433        $this->assertStringNotContainsString( 'ASC', $q4->request );
    434434
    435435        // Empty array() is a valid value.
    436436        $q5 = new WP_Query( array( 'orderby' => array() ) );
    437         $this->assertNotContains( 'ORDER BY', $q5->request );
    438         $this->assertNotContains( 'DESC', $q5->request );
    439         $this->assertNotContains( 'ASC', $q5->request );
     437        $this->assertStringNotContainsString( 'ORDER BY', $q5->request );
     438        $this->assertStringNotContainsString( 'DESC', $q5->request );
     439        $this->assertStringNotContainsString( 'ASC', $q5->request );
    440440    }
    441441
     
    450450        );
    451451
    452         $this->assertContains( 'ORDER BY RAND(5)', $q->request );
     452        $this->assertStringContainsString( 'ORDER BY RAND(5)', $q->request );
    453453    }
    454454
     
    463463        );
    464464
    465         $this->assertNotContains( 'ORDER BY RAND', $q->request );
     465        $this->assertStringNotContainsString( 'ORDER BY RAND', $q->request );
    466466    }
    467467
     
    476476        );
    477477
    478         $this->assertContains( 'ORDER BY RAND(5)', $q->request );
     478        $this->assertStringContainsString( 'ORDER BY RAND(5)', $q->request );
    479479    }
    480480
Note: See TracChangeset for help on using the changeset viewer.