Make WordPress Core

Changeset 52418


Ignore:
Timestamp:
12/26/2021 11:40:50 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Tests: Separate the tests for recommended PHP and MySQL versions in readme.html

This aims to simplify the individual tests and reduce duplication using a helper function.

Follow-up to [52260], [52319], [52358].

See #41490.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/external-http/basic.php

    r52260 r52418  
    1010     * @covers ::wp_remote_retrieve_body
    1111     */
    12     public function test_readme() {
     12    public function test_readme_php_version() {
    1313        // This test is designed to only run on trunk/master.
    1414        $this->skipOnAutomatedBranches();
     
    1818        preg_match( '#Recommendations.*PHP</a> version <strong>([0-9.]*)#s', $readme, $matches );
    1919
    20         $response = wp_remote_get( 'https://www.php.net/supported-versions.php' );
     20        $response_body = $this->get_response_body( 'https://www.php.net/supported-versions.php' );
     21
     22        preg_match_all( '#<tr class="stable">\s*<td>\s*<a [^>]*>\s*([0-9.]*)#s', $response_body, $phpmatches );
     23
     24        // TODO: Enable this check once PHP 8.0 compatibility is achieved.
     25        // $this->assertContains( $matches[1], $phpmatches[1], "readme.html's Recommended PHP version is too old. Remember to update the WordPress.org Requirements page, too." );
     26    }
     27
     28    /**
     29     * @covers ::wp_remote_get
     30     * @covers ::wp_remote_retrieve_response_code
     31     * @covers ::wp_remote_retrieve_body
     32     */
     33    public function test_readme_mysql_version() {
     34        // This test is designed to only run on trunk/master.
     35        $this->skipOnAutomatedBranches();
     36
     37        $readme = file_get_contents( ABSPATH . 'readme.html' );
     38
     39        preg_match( '#Recommendations.*MySQL</a> version <strong>([0-9.]*)#s', $readme, $matches );
     40
     41        $response_body = $this->get_response_body( "https://dev.mysql.com/doc/relnotes/mysql/{$matches[1]}/en/" );
     42
     43        preg_match( '#(\d{4}-\d{2}-\d{2}), General Availability#', $response_body, $mysqlmatches );
     44
     45        // Per https://www.mysql.com/support/, Oracle actively supports MySQL releases for 5 years from GA release.
     46        $mysql_eol = strtotime( $mysqlmatches[1] . ' +5 years' );
     47
     48        $this->assertLessThan( $mysql_eol, time(), "readme.html's Recommended MySQL version is too old. Remember to update the WordPress.org Requirements page, too." );
     49    }
     50
     51    /**
     52     * Helper function to retrieve the response body or skip the test on HTTP timeout.
     53     *
     54     * @param string $url The URL to retrieve the response from.
     55     * @return string The response body.
     56     */
     57    public function get_response_body( $url ) {
     58        $response = wp_remote_get( $url );
    2159
    2260        $this->skipTestOnTimeout( $response );
     
    2664
    2765        if ( 200 !== $response_code ) {
     66            $parsed_url = parse_url( $url );
     67
    2868            $error_message = sprintf(
    29                 'Could not contact PHP.net to check versions. Response code: %s. Response body: %s',
     69                'Could not contact %1$s to check versions. Response code: %2$s. Response body: %3$s',
     70                $parsed_url['host'],
    3071                $response_code,
    3172                $response_body
     
    3980        }
    4081
    41         preg_match_all( '#<tr class="stable">\s*<td>\s*<a [^>]*>\s*([0-9.]*)#s', $response_body, $phpmatches );
    42 
    43         // TODO: Enable this check once PHP 8.0 compatibility is achieved.
    44         // $this->assertContains( $matches[1], $phpmatches[1], "readme.html's Recommended PHP version is too old. Remember to update the WordPress.org Requirements page, too." );
    45 
    46         preg_match( '#Recommendations.*MySQL</a> version <strong>([0-9.]*)#s', $readme, $matches );
    47 
    48         $response = wp_remote_get( "https://dev.mysql.com/doc/relnotes/mysql/{$matches[1]}/en/" );
    49 
    50         $this->skipTestOnTimeout( $response );
    51 
    52         $response_code = wp_remote_retrieve_response_code( $response );
    53         $response_body = wp_remote_retrieve_body( $response );
    54 
    55         if ( 200 !== $response_code ) {
    56             $error_message = sprintf(
    57                 'Could not contact dev.MySQL.com to check versions. Response code: %s. Response body: %s',
    58                 $response_code,
    59                 $response_body
    60             );
    61 
    62             if ( 503 === $response_code ) {
    63                 $this->markTestSkipped( $error_message );
    64             }
    65 
    66             $this->fail( $error_message );
    67         }
    68 
    69         preg_match( '#(\d{4}-\d{2}-\d{2}), General Availability#', $response_body, $mysqlmatches );
    70 
    71         // Per https://www.mysql.com/support/, Oracle actively supports MySQL releases for 5 years from GA release.
    72         $mysql_eol = strtotime( $mysqlmatches[1] . ' +5 years' );
    73 
    74         $this->assertLessThan( $mysql_eol, time(), "readme.html's Recommended MySQL version is too old. Remember to update the WordPress.org Requirements page, too." );
     82        return $response_body;
    7583    }
    7684}
Note: See TracChangeset for help on using the changeset viewer.