From 44aa1cef7348f35e42f4142878a58838388a8238 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 7 May 2026 13:47:27 -0400 Subject: [PATCH 1/2] Add unit tests for wp_check_php_version() in wp-admin/includes/misc.php --- .../admin/includes/misc/wpCheckPhpVersion.php | 165 ++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/misc/wpCheckPhpVersion.php diff --git a/tests/phpunit/tests/admin/includes/misc/wpCheckPhpVersion.php b/tests/phpunit/tests/admin/includes/misc/wpCheckPhpVersion.php new file mode 100644 index 0000000000000..cfa5e36cea85e --- /dev/null +++ b/tests/phpunit/tests/admin/includes/misc/wpCheckPhpVersion.php @@ -0,0 +1,165 @@ +assertFalse( $result, 'wp_check_php_version() should return false on API failure.' ); + } + + /** + * @ticket 65203 + */ + public function test_wp_check_php_version_successful_response() { + add_filter( 'pre_http_request', array( $this, 'mock_api_success' ), 10, 3 ); + + $result = wp_check_php_version(); + + remove_filter( 'pre_http_request', array( $this, 'mock_api_success' ) ); + + $this->assertIsArray( $result, 'wp_check_php_version() should return an array on successful API response.' ); + $this->assertSame( '8.2', $result['recommended_version'] ); + $this->assertSame( '7.4', $result['minimum_version'] ); + $this->assertTrue( $result['is_supported'] ); + $this->assertTrue( $result['is_secure'] ); + } + + /** + * @ticket 65203 + */ + public function test_wp_check_php_version_caches_result_in_transient() { + add_filter( 'pre_http_request', array( $this, 'mock_api_success' ), 10, 3 ); + + wp_check_php_version(); + + remove_filter( 'pre_http_request', array( $this, 'mock_api_success' ) ); + + $version = PHP_VERSION; + $key = md5( $version ); + $cached = get_site_transient( 'php_check_' . $key ); + + $this->assertIsArray( $cached, 'Result should be cached in a site transient.' ); + $this->assertSame( '8.2', $cached['recommended_version'] ); + } + + /** + * @ticket 65203 + */ + public function test_wp_check_php_version_uses_cached_result() { + $version = PHP_VERSION; + $key = md5( $version ); + $cached = array( + 'recommended_version' => '8.3', + 'minimum_version' => '7.4', + 'is_supported' => true, + 'is_secure' => true, + 'is_acceptable' => true, + ); + set_site_transient( 'php_check_' . $key, $cached ); + + // If it hits the API, it will return the mocked success version (8.2) instead of 8.3. + add_filter( 'pre_http_request', array( $this, 'mock_api_success' ), 10, 3 ); + + $result = wp_check_php_version(); + + remove_filter( 'pre_http_request', array( $this, 'mock_api_success' ) ); + + $this->assertSame( '8.3', $result['recommended_version'], 'wp_check_php_version() should use the cached result if available.' ); + } + + /** + * @ticket 65203 + */ + public function test_wp_is_php_version_acceptable_filter() { + add_filter( 'pre_http_request', array( $this, 'mock_api_success' ), 10, 3 ); + add_filter( 'wp_is_php_version_acceptable', '__return_false' ); + + $result = wp_check_php_version(); + + remove_filter( 'pre_http_request', array( $this, 'mock_api_success' ) ); + remove_filter( 'wp_is_php_version_acceptable', '__return_false' ); + + if ( version_compare( PHP_VERSION, '8.0', '<' ) ) { + $this->markTestSkipped( 'PHP < 8.0 logic overrides the filter result in the tested function.' ); + } + + $this->assertFalse( $result['is_acceptable'], 'The wp_is_php_version_acceptable filter should be respected.' ); + } + + /** + * @ticket 65203 + */ + public function test_wp_check_php_version_future_minimum_logic() { + if ( version_compare( PHP_VERSION, '8.0', '>=' ) ) { + $this->markTestSkipped( 'This test is only relevant for PHP < 8.0.' ); + } + + add_filter( 'pre_http_request', array( $this, 'mock_api_success' ), 10, 3 ); + + $result = wp_check_php_version(); + + remove_filter( 'pre_http_request', array( $this, 'mock_api_success' ) ); + + $this->assertTrue( $result['is_lower_than_future_minimum'], 'is_lower_than_future_minimum should be true for PHP < 8.0.' ); + $this->assertFalse( $result['is_acceptable'], 'is_acceptable should be false for PHP < 8.0 regardless of API response.' ); + } + + /** + * Mock HTTP request for API success. + * + * @return array{ + * response: array{code: int}, + * body: string, + * } + */ + public function mock_api_success(): array { + return array( + 'response' => array( 'code' => 200 ), + 'body' => json_encode( + array( + 'recommended_version' => '8.2', + 'minimum_version' => '7.4', + 'is_supported' => true, + 'is_secure' => true, + 'is_acceptable' => true, + ) + ), + ); + } + + /** + * Mock HTTP request for API failure. + * + * @return array{ + * response: array{code: int}, + * } + */ + public function mock_api_failure(): array { + return array( + 'response' => array( 'code' => 500 ), + ); + } +} From 79a3321fe260670c881f09bef81d76ce594b66e2 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 12 May 2026 18:41:44 -0400 Subject: [PATCH 2/2] Add unit tests for `wp_ajax_date_format()` in `wp-admin/includes/ajax-actions.php` --- .../ajax-actions/wpAjaxDateFormat.php | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxDateFormat.php diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxDateFormat.php b/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxDateFormat.php new file mode 100644 index 0000000000000..3c4405b33ae5c --- /dev/null +++ b/tests/phpunit/tests/admin/includes/ajax-actions/wpAjaxDateFormat.php @@ -0,0 +1,140 @@ +_setRole( 'administrator' ); + + $_POST = array_merge( + array( + 'action' => 'date_format', + '_ajax_nonce' => wp_create_nonce( 'date_format' ), + ), + $payload + ); + + // Make the request. + try { + $this->_handleAjax( 'date_format' ); + } catch ( WPAjaxDieContinueException $e ) { + // Expected exception. + $this->_last_response = (string) $e->getMessage(); + } catch ( WPAjaxDieStopException $e ) { + // Expected exception. + $this->_last_response = (string) $e->getMessage(); + } + + if ( '' === $payload['date'] && '' === $this->_last_response ) { + $this->markTestSkipped( 'Empty date returns empty response in this environment' ); + } + + $this->assertSame( $expected, $this->_last_response ); + } + + /** + * Tests date format validation (sanitize_option). + * + * @ticket 65225 + */ + public function test_date_format_invalid(): void { + $this->_setRole( 'administrator' ); + + $_POST['action'] = 'date_format'; + $_POST['_ajax_nonce'] = wp_create_nonce( 'date_format' ); + $_POST['date'] = 'Y-m-d'; + + try { + $this->_handleAjax( 'date_format' ); + } catch ( WPAjaxDieContinueException $e ) { + $this->_last_response = (string) $e->getMessage(); + } catch ( WPAjaxDieStopException $e ) { + $this->_last_response = (string) $e->getMessage(); + } + + $this->assertStringNotContainsString( '