Skip to content

Commit 3722a58

Browse files
committed
Tests: Use independent readme version fixtures and negative cases.
Address review feedback by stopping fixtures from echoing the recommendation under test, and cover unsupported or expired releases with fixed reference dates.
1 parent 158c814 commit 3722a58

1 file changed

Lines changed: 197 additions & 57 deletions

File tree

‎tests/phpunit/tests/readme.php‎

Lines changed: 197 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
/**
33
* Validate recommended versions for dependencies referenced in `readme.html`.
44
*
5-
* Live version-API checks are mocked so the suite can assert parsing and
6-
* comparison logic without contacting external hosts.
5+
* Version-API responses are independently maintained fixtures so the suite can
6+
* assert parsing and support-window logic without contacting external hosts.
7+
* Update the fixtures when bumping recommended versions in `readme.html`.
78
*
89
* @group readme
910
*/
@@ -40,18 +41,29 @@ public function test_readme_php_version() {
4041

4142
preg_match( '#Recommendations.*PHP</a> version <strong>([0-9.]*)#s', $readme, $matches );
4243

43-
$this->mock_http_response_body(
44-
sprintf(
45-
'<tr class="stable"><td><a href="https://www.php.net/releases/">%s</a></td></tr>',
46-
$matches[1]
47-
)
48-
);
44+
$this->mock_http_response_body( $this->get_php_supported_versions_fixture() );
4945

5046
$response_body = $this->get_response_body( 'https://www.php.net/supported-versions.php' );
5147

52-
preg_match_all( '#<tr class="(?:stable|security)">\s*<td>\s*<a [^>]*>\s*([0-9.]*)#s', $response_body, $php_matches );
48+
$this->assert_recommended_php_version_is_supported( $matches[1], $response_body );
49+
}
50+
51+
/**
52+
* @coversNothing
53+
*
54+
* @ticket 63914
55+
*/
56+
public function test_readme_php_version_rejects_unsupported_release() {
57+
$failed = false;
58+
59+
try {
60+
$this->assert_recommended_php_version_is_supported( '0.0', $this->get_php_supported_versions_fixture() );
61+
} catch ( PHPUnit_Framework_AssertionFailedError $e ) {
62+
$failed = true;
63+
$this->assertStringContainsString( 'Recommended PHP version is too old', $e->getMessage() );
64+
}
5365

54-
$this->assertContains( $matches[1], $php_matches[1], "readme.html's Recommended PHP version is too old. Remember to update the WordPress.org Requirements page, too." );
66+
$this->assertTrue( $failed, 'Expected an unsupported PHP version to fail.' );
5567
}
5668

5769
/**
@@ -67,42 +79,33 @@ public function test_readme_mysql_version() {
6779

6880
preg_match( '#Recommendations.*MySQL</a> version <strong>([0-9.]*)#s', $readme, $matches );
6981

70-
/*
71-
* Per https://www.mysql.com/support/, Oracle actively supports MySQL releases for 5 years from GA release.
72-
*
73-
* The currently recommended MySQL 8.0 branch moved from active support to extended support on 2023-04-19.
74-
* As WordPress core may not be fully compatible with MySQL 8.1 at this time, the "supported" period here
75-
* is increased to 8 years to include extended support.
76-
*
77-
* TODO: Reduce this back to 5 years once MySQL 8.1 compatibility is achieved.
78-
*
79-
* Fixture EOL is one year ago so the +8 year support window still includes today.
80-
*/
81-
$this->mock_http_response_body(
82-
wp_json_encode(
83-
array(
84-
array(
85-
'cycle' => $matches[1],
86-
'eol' => gmdate( 'Y-m-d', strtotime( '-1 year' ) ),
87-
),
88-
)
89-
)
90-
);
82+
$this->mock_http_response_body( $this->get_mysql_versions_fixture() );
9183

9284
$response_body = json_decode( $this->get_response_body( 'https://endoflife.date/api/mysql.json' ) );
93-
$eol_date = '';
9485

95-
foreach ( $response_body as $version ) {
96-
if ( $version->cycle === $matches[1] && false !== $version->eol ) {
97-
$eol_date = $version->eol;
98-
break;
99-
}
100-
}
86+
$this->assert_recommended_mysql_version_is_supported( $matches[1], $response_body, gmdate( 'Y-m-d' ) );
87+
}
10188

102-
$mysql_eol = gmdate( 'Y-m-d', strtotime( $eol_date . ' +8 years' ) );
103-
$current_date = gmdate( 'Y-m-d' );
89+
/**
90+
* @coversNothing
91+
*
92+
* @ticket 63914
93+
*/
94+
public function test_readme_mysql_version_rejects_expired_support_window() {
95+
$failed = false;
96+
97+
try {
98+
$this->assert_recommended_mysql_version_is_supported(
99+
'5.7',
100+
json_decode( $this->get_mysql_versions_fixture() ),
101+
'2035-01-01'
102+
);
103+
} catch ( PHPUnit_Framework_AssertionFailedError $e ) {
104+
$failed = true;
105+
$this->assertStringContainsString( 'Recommended MySQL version is too old', $e->getMessage() );
106+
}
104107

105-
$this->assertLessThan( $mysql_eol, $current_date, "readme.html's Recommended MySQL version is too old. Remember to update the WordPress.org Requirements page, too." );
108+
$this->assertTrue( $failed, 'Expected an expired MySQL support window to fail.' );
106109
}
107110

108111
/**
@@ -118,36 +121,173 @@ public function test_readme_mariadb_version() {
118121

119122
preg_match( '#Recommendations.*MariaDB</a> version <strong>([0-9.]*)#s', $readme, $matches );
120123

121-
$this->mock_http_response_body(
122-
wp_json_encode(
124+
$this->mock_http_response_body( $this->get_mariadb_versions_fixture() );
125+
126+
$response_body = $this->get_response_body( 'https://downloads.mariadb.org/rest-api/mariadb/' );
127+
$releases = json_decode( $response_body, true );
128+
129+
$this->assert_recommended_mariadb_version_is_supported( $matches[1], $releases, gmdate( 'Y-m-d' ) );
130+
}
131+
132+
/**
133+
* @coversNothing
134+
*
135+
* @ticket 63914
136+
*/
137+
public function test_readme_mariadb_version_rejects_unsupported_release() {
138+
$failed = false;
139+
140+
try {
141+
$this->assert_recommended_mariadb_version_is_supported(
142+
'0.0',
143+
json_decode( $this->get_mariadb_versions_fixture(), true ),
144+
gmdate( 'Y-m-d' )
145+
);
146+
} catch ( PHPUnit_Framework_AssertionFailedError $e ) {
147+
$failed = true;
148+
$this->assertStringContainsString( 'is not included in MariaDB\'s list of supported versions', $e->getMessage() );
149+
}
150+
151+
$this->assertTrue( $failed, 'Expected an unsupported MariaDB version to fail.' );
152+
}
153+
154+
/**
155+
* @coversNothing
156+
*
157+
* @ticket 63914
158+
*/
159+
public function test_readme_mariadb_version_rejects_expired_support_window() {
160+
$failed = false;
161+
162+
try {
163+
$this->assert_recommended_mariadb_version_is_supported(
164+
'10.6',
165+
json_decode( $this->get_mariadb_versions_fixture(), true ),
166+
'2027-01-01'
167+
);
168+
} catch ( PHPUnit_Framework_AssertionFailedError $e ) {
169+
$failed = true;
170+
$this->assertStringContainsString( 'Recommended MariaDB version is too old', $e->getMessage() );
171+
}
172+
173+
$this->assertTrue( $failed, 'Expected an expired MariaDB support window to fail.' );
174+
}
175+
176+
/**
177+
* Independently maintained php.net supported-versions HTML fixture.
178+
*
179+
* @return string
180+
*/
181+
private function get_php_supported_versions_fixture() {
182+
return '<table>'
183+
. '<tr class="stable"><td><a href="https://www.php.net/releases/8.3/">8.3</a></td></tr>'
184+
. '<tr class="security"><td><a href="https://www.php.net/releases/8.2/">8.2</a></td></tr>'
185+
. '<tr class="security"><td><a href="https://www.php.net/releases/8.1/">8.1</a></td></tr>'
186+
. '</table>';
187+
}
188+
189+
/**
190+
* Independently maintained endoflife.date MySQL JSON fixture.
191+
*
192+
* Per https://www.mysql.com/support/, Oracle actively supports MySQL releases
193+
* for 5 years from GA. The recommended 8.0 branch uses an 8-year window to
194+
* include extended support until MySQL 8.1 compatibility is achieved.
195+
*
196+
* @return string
197+
*/
198+
private function get_mysql_versions_fixture() {
199+
return wp_json_encode(
200+
array(
123201
array(
124-
'major_releases' => array(
125-
array(
126-
'release_id' => $matches[1],
127-
'release_eol_date' => gmdate( 'Y-m-d', strtotime( '+1 year' ) ),
128-
),
202+
'cycle' => '8.0',
203+
'eol' => '2026-04-30',
204+
),
205+
array(
206+
'cycle' => '5.7',
207+
'eol' => '2023-10-21',
208+
),
209+
)
210+
);
211+
}
212+
213+
/**
214+
* Independently maintained MariaDB major-releases JSON fixture.
215+
*
216+
* @return string
217+
*/
218+
private function get_mariadb_versions_fixture() {
219+
return wp_json_encode(
220+
array(
221+
'major_releases' => array(
222+
array(
223+
'release_id' => '10.11',
224+
'release_eol_date' => '2028-02-16',
129225
),
130-
)
226+
array(
227+
'release_id' => '10.6',
228+
'release_eol_date' => '2026-07-06',
229+
),
230+
),
131231
)
132232
);
233+
}
133234

134-
$response_body = $this->get_response_body( 'https://downloads.mariadb.org/rest-api/mariadb/' );
135-
$releases = json_decode( $response_body, true );
235+
/**
236+
* Asserts a recommended PHP version appears in the supported-versions HTML.
237+
*
238+
* @param string $recommended Recommended PHP version from readme.html.
239+
* @param string $response_body Supported-versions HTML.
240+
*/
241+
private function assert_recommended_php_version_is_supported( $recommended, $response_body ) {
242+
preg_match_all( '#<tr class="(?:stable|security)">\s*<td>\s*<a [^>]*>\s*([0-9.]*)#s', $response_body, $php_matches );
243+
244+
$this->assertContains( $recommended, $php_matches[1], "readme.html's Recommended PHP version is too old. Remember to update the WordPress.org Requirements page, too." );
245+
}
246+
247+
/**
248+
* Asserts a recommended MySQL version is still within the extended support window.
249+
*
250+
* @param string $recommended Recommended MySQL version from readme.html.
251+
* @param array $response_body Decoded endoflife.date payload.
252+
* @param string $reference_date Date used for the support-window comparison (Y-m-d).
253+
*/
254+
private function assert_recommended_mysql_version_is_supported( $recommended, $response_body, $reference_date ) {
255+
$eol_date = '';
136256

257+
foreach ( $response_body as $version ) {
258+
if ( $version->cycle === $recommended && false !== $version->eol ) {
259+
$eol_date = $version->eol;
260+
break;
261+
}
262+
}
263+
264+
/*
265+
* TODO: Reduce this back to 5 years once MySQL 8.1 compatibility is achieved.
266+
*/
267+
$mysql_eol = gmdate( 'Y-m-d', strtotime( $eol_date . ' +8 years' ) );
268+
269+
$this->assertLessThan( $mysql_eol, $reference_date, "readme.html's Recommended MySQL version is too old. Remember to update the WordPress.org Requirements page, too." );
270+
}
271+
272+
/**
273+
* Asserts a recommended MariaDB version is listed and not past its EOL date.
274+
*
275+
* @param string $recommended Recommended MariaDB version from readme.html.
276+
* @param array $releases Decoded MariaDB major-releases payload.
277+
* @param string $reference_date Date used for the support-window comparison (Y-m-d).
278+
*/
279+
private function assert_recommended_mariadb_version_is_supported( $recommended, $releases, $reference_date ) {
137280
foreach ( $releases['major_releases'] as $release ) {
138-
if ( isset( $release['release_id'] ) && $release['release_id'] === $matches[1] ) {
281+
if ( isset( $release['release_id'] ) && $release['release_id'] === $recommended ) {
139282
$mariadb_eol = $release['release_eol_date'];
140283
}
141284
}
142285

143-
// If the release ID is not found the version is unsupported.
144286
if ( ! isset( $mariadb_eol ) ) {
145-
$this->fail( "{$matches[1]} is not included in MariaDB's list of supported versions. Remember to update the WordPress.org Requirements page, too." );
287+
$this->fail( "{$recommended} is not included in MariaDB's list of supported versions. Remember to update the WordPress.org Requirements page, too." );
146288
}
147289

148-
$current_date = gmdate( 'Y-m-d' );
149-
150-
$this->assertLessThan( $mariadb_eol, $current_date, "readme.html's Recommended MariaDB version is too old. Remember to update the WordPress.org Requirements page, too." );
290+
$this->assertLessThan( $mariadb_eol, $reference_date, "readme.html's Recommended MariaDB version is too old. Remember to update the WordPress.org Requirements page, too." );
151291
}
152292

153293
/**

0 commit comments

Comments
 (0)