|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @group admin |
| 5 | + * |
| 6 | + * @covers ::wp_check_php_version |
| 7 | + */ |
| 8 | +class Tests_Admin_Includes_Misc_WpCheckPhpVersion_Test extends WP_UnitTestCase { |
| 9 | + |
| 10 | + /** |
| 11 | + * Cleans up transients after each test. |
| 12 | + */ |
| 13 | + public function tear_down() { |
| 14 | + $key = md5( PHP_VERSION ); |
| 15 | + delete_site_transient( 'php_check_' . $key ); |
| 16 | + parent::tear_down(); |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * @ticket 65203 |
| 21 | + */ |
| 22 | + public function test_wp_check_php_version_returns_false_on_api_failure() { |
| 23 | + add_filter( 'pre_http_request', array( $this, 'mock_api_failure' ), 10, 3 ); |
| 24 | + |
| 25 | + $result = wp_check_php_version(); |
| 26 | + |
| 27 | + remove_filter( 'pre_http_request', array( $this, 'mock_api_failure' ) ); |
| 28 | + |
| 29 | + $this->assertFalse( $result, 'wp_check_php_version() should return false on API failure.' ); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @ticket 65203 |
| 34 | + */ |
| 35 | + public function test_wp_check_php_version_successful_response() { |
| 36 | + add_filter( 'pre_http_request', array( $this, 'mock_api_success' ), 10, 3 ); |
| 37 | + |
| 38 | + $result = wp_check_php_version(); |
| 39 | + |
| 40 | + remove_filter( 'pre_http_request', array( $this, 'mock_api_success' ) ); |
| 41 | + |
| 42 | + $this->assertIsArray( $result, 'wp_check_php_version() should return an array on successful API response.' ); |
| 43 | + $this->assertSame( '8.2', $result['recommended_version'] ); |
| 44 | + $this->assertSame( '7.4', $result['minimum_version'] ); |
| 45 | + $this->assertTrue( $result['is_supported'] ); |
| 46 | + $this->assertTrue( $result['is_secure'] ); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @ticket 65203 |
| 51 | + */ |
| 52 | + public function test_wp_check_php_version_caches_result_in_transient() { |
| 53 | + add_filter( 'pre_http_request', array( $this, 'mock_api_success' ), 10, 3 ); |
| 54 | + |
| 55 | + wp_check_php_version(); |
| 56 | + |
| 57 | + remove_filter( 'pre_http_request', array( $this, 'mock_api_success' ) ); |
| 58 | + |
| 59 | + $key = md5( PHP_VERSION ); |
| 60 | + $cached = get_site_transient( 'php_check_' . $key ); |
| 61 | + |
| 62 | + $this->assertIsArray( $cached, 'Result should be cached in a site transient.' ); |
| 63 | + $this->assertSame( '8.2', $cached['recommended_version'] ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @ticket 65203 |
| 68 | + */ |
| 69 | + public function test_wp_check_php_version_uses_cached_result() { |
| 70 | + $key = md5( PHP_VERSION ); |
| 71 | + $cached = array( |
| 72 | + 'recommended_version' => '8.3', |
| 73 | + 'minimum_version' => '7.4', |
| 74 | + 'is_supported' => true, |
| 75 | + 'is_secure' => true, |
| 76 | + 'is_acceptable' => true, |
| 77 | + ); |
| 78 | + set_site_transient( 'php_check_' . $key, $cached ); |
| 79 | + |
| 80 | + // If it hits the API, it will return the mocked success version (8.2) instead of 8.3. |
| 81 | + add_filter( 'pre_http_request', array( $this, 'mock_api_success' ), 10, 3 ); |
| 82 | + |
| 83 | + $result = wp_check_php_version(); |
| 84 | + |
| 85 | + remove_filter( 'pre_http_request', array( $this, 'mock_api_success' ) ); |
| 86 | + |
| 87 | + $this->assertSame( '8.3', $result['recommended_version'], 'wp_check_php_version() should use the cached result if available.' ); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @ticket 65203 |
| 92 | + * |
| 93 | + * @requires PHP >= 8.0 |
| 94 | + */ |
| 95 | + public function test_wp_is_php_version_acceptable_filter() { |
| 96 | + add_filter( 'pre_http_request', array( $this, 'mock_api_success' ), 10, 3 ); |
| 97 | + add_filter( 'wp_is_php_version_acceptable', '__return_false' ); |
| 98 | + |
| 99 | + $result = wp_check_php_version(); |
| 100 | + |
| 101 | + remove_filter( 'pre_http_request', array( $this, 'mock_api_success' ) ); |
| 102 | + remove_filter( 'wp_is_php_version_acceptable', '__return_false' ); |
| 103 | + |
| 104 | + $this->assertFalse( $result['is_acceptable'], 'The wp_is_php_version_acceptable filter should be respected.' ); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @ticket 65203 |
| 109 | + * |
| 110 | + * @requires PHP < 8.0 |
| 111 | + */ |
| 112 | + public function test_wp_check_php_version_future_minimum_logic() { |
| 113 | + add_filter( 'pre_http_request', array( $this, 'mock_api_success' ), 10, 3 ); |
| 114 | + |
| 115 | + $result = wp_check_php_version(); |
| 116 | + |
| 117 | + remove_filter( 'pre_http_request', array( $this, 'mock_api_success' ) ); |
| 118 | + |
| 119 | + $this->assertTrue( $result['is_lower_than_future_minimum'], 'is_lower_than_future_minimum should be true for PHP < 8.0.' ); |
| 120 | + $this->assertFalse( $result['is_acceptable'], 'is_acceptable should be false for PHP < 8.0 regardless of API response.' ); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Mock HTTP request for API success. |
| 125 | + * |
| 126 | + * @return array{ |
| 127 | + * response: array{code: int}, |
| 128 | + * body: string, |
| 129 | + * } |
| 130 | + */ |
| 131 | + public function mock_api_success(): array { |
| 132 | + return array( |
| 133 | + 'response' => array( 'code' => 200 ), |
| 134 | + 'body' => json_encode( |
| 135 | + array( |
| 136 | + 'recommended_version' => '8.2', |
| 137 | + 'minimum_version' => '7.4', |
| 138 | + 'is_supported' => true, |
| 139 | + 'is_secure' => true, |
| 140 | + 'is_acceptable' => true, |
| 141 | + ) |
| 142 | + ), |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Mock HTTP request for API failure. |
| 148 | + * |
| 149 | + * @return array{ |
| 150 | + * response: array{code: int}, |
| 151 | + * } |
| 152 | + */ |
| 153 | + public function mock_api_failure(): array { |
| 154 | + return array( |
| 155 | + 'response' => array( 'code' => 500 ), |
| 156 | + ); |
| 157 | + } |
| 158 | +} |
0 commit comments