WordPress.org

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/rest-api/rest-tags-controller.php

    r51461 r51462  
    592592        );
    593593        $this->assertStringNotContainsString( 'rel="prev"', $headers['Link'] );
    594         $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
     594        $this->assertStringContainsString( '<' . $next_link . '>; rel="next"', $headers['Link'] );
    595595
    596596        // 3rd page.
     
    610610            rest_url( 'wp/v2/tags' )
    611611        );
    612         $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
     612        $this->assertStringContainsString( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
    613613        $next_link = add_query_arg(
    614614            array(
     
    617617            rest_url( 'wp/v2/tags' )
    618618        );
    619         $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
     619        $this->assertStringContainsString( '<' . $next_link . '>; rel="next"', $headers['Link'] );
    620620
    621621        // Last page.
     
    632632            rest_url( 'wp/v2/tags' )
    633633        );
    634         $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
     634        $this->assertStringContainsString( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
    635635        $this->assertStringNotContainsString( 'rel="next"', $headers['Link'] );
    636636
     
    648648            rest_url( 'wp/v2/tags' )
    649649        );
    650         $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
     650        $this->assertStringContainsString( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
    651651        $this->assertStringNotContainsString( 'rel="next"', $headers['Link'] );
    652652    }
     
    760760        $headers = $response->get_headers();
    761761        $data    = $response->get_data();
    762         $this->assertContains( '/wp/v2/tags/' . $data['id'], $headers['Location'] );
     762        $this->assertStringContainsString( '/wp/v2/tags/' . $data['id'], $headers['Location'] );
    763763        $this->assertSame( 'My Awesome Term', $data['name'] );
    764764        $this->assertSame( 'This term is so awesome.', $data['description'] );
     
    777777        $headers = $response->get_headers();
    778778        $data    = $response->get_data();
    779         $this->assertContains( '/wp/v2/tags/' . $data['id'], $headers['Location'] );
     779        $this->assertStringContainsString( '/wp/v2/tags/' . $data['id'], $headers['Location'] );
    780780        $this->assertSame( 'My Awesome Term', $data['name'] );
    781781        $this->assertSame( 'This term is so awesome.', $data['description'] );
     
    820820        $headers = $response->get_headers();
    821821        $data    = $response->get_data();
    822         $this->assertContains( '/wp/v2/tags/' . $data['id'], $headers['Location'] );
     822        $this->assertStringContainsString( '/wp/v2/tags/' . $data['id'], $headers['Location'] );
    823823        $this->assertSame( 'My Awesome Term', $data['name'] );
    824824        $this->assertSame( 'hello', get_term_meta( $data['id'], 'test_tag_single', true ) );
     
    838838        $headers = $response->get_headers();
    839839        $data    = $response->get_data();
    840         $this->assertContains( '/wp/v2/tags/' . $data['id'], $headers['Location'] );
     840        $this->assertStringContainsString( '/wp/v2/tags/' . $data['id'], $headers['Location'] );
    841841        $this->assertSame( 'My Awesome Term', $data['name'] );
    842842        $this->assertSame( '', get_term_meta( $existing_tag_id, 'test_tag_single', true ) );
     
    14401440        }
    14411441        $this->assertSameSets( $expected_links, array_keys( $links ) );
    1442         $this->assertContains( 'wp/v2/taxonomies/' . $term->taxonomy, $links['about'][0]['href'] );
     1442        $this->assertStringContainsString( 'wp/v2/taxonomies/' . $term->taxonomy, $links['about'][0]['href'] );
    14431443        $this->assertSame( add_query_arg( 'tags', $term->term_id, rest_url( 'wp/v2/posts' ) ), $links['https://api.w.org/post_type'][0]['href'] );
    14441444    }
Note: See TracChangeset for help on using the changeset viewer.