Skip to content

Commit

Permalink
Update minimum required version in PHP. (WordPress#65301)
Browse files Browse the repository at this point in the history
* Update minimum required version in PHP.

* Use a constant.

* Test plugin meta data and constant match.

* Set constant incorrectly to demonstrate failing tests.

* Use regex to extract the constant.

* Match the version in the constant and plugin meta.

Co-authored-by: peterwilsoncc <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
  • Loading branch information
3 people authored Sep 13, 2024
1 parent b0abc74 commit 9e88d86
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
6 changes: 4 additions & 2 deletions gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
### BEGIN AUTO-GENERATED DEFINES
defined( 'GUTENBERG_DEVELOPMENT_MODE' ) or define( 'GUTENBERG_DEVELOPMENT_MODE', true );
### END AUTO-GENERATED DEFINES
defined( 'GUTENBERG_MINIMUM_WP_VERSION' ) or define( 'GUTENBERG_MINIMUM_WP_VERSION', '6.5' );


gutenberg_pre_init();

Expand All @@ -26,7 +28,7 @@
function gutenberg_wordpress_version_notice() {
echo '<div class="error"><p>';
/* translators: %s: Minimum required version */
printf( __( 'Gutenberg requires WordPress %s or later to function properly. Please upgrade WordPress before activating Gutenberg.', 'gutenberg' ), '5.9' );
printf( __( 'Gutenberg requires WordPress %s or later to function properly. Please upgrade WordPress before activating Gutenberg.', 'gutenberg' ), GUTENBERG_MINIMUM_WP_VERSION );
echo '</p></div>';

deactivate_plugins( array( 'gutenberg/gutenberg.php' ) );
Expand Down Expand Up @@ -67,7 +69,7 @@ function gutenberg_pre_init() {
// Compare against major release versions (X.Y) rather than minor (X.Y.Z)
// unless a minor release is the actual minimum requirement. WordPress reports
// X.Y for its major releases.
if ( version_compare( $version, '5.9', '<' ) ) {
if ( version_compare( $version, GUTENBERG_MINIMUM_WP_VERSION, '<' ) ) {
add_action( 'admin_notices', 'gutenberg_wordpress_version_notice' );
return;
}
Expand Down
26 changes: 26 additions & 0 deletions phpunit/class-test-plugin-version-meta-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Unit tests covering the version checks in the plugin file.
*
* @package Gutenberg
*/
class Test_PluginMetaData_Test extends WP_UnitTestCase {
/**
* Test the minimum WordPress version check.
*
* Ensures the constant defined as the minimum required version of WordPress
* matches the minimum version defined in the plugin header.
*/
public function test_minimum_required_wordpress_version() {
$file_meta = get_file_data( __DIR__ . '/../gutenberg.php', array( 'RequiresWP' => 'Requires at least' ) );
/*
* Gutenberg.php isn't loaded in the test environment.
*
* Read the file directly and use regex to extract the constant.
*/
preg_match( '/GUTENBERG_MINIMUM_WP_VERSION\', \'(.*?)\'/', file_get_contents( __DIR__ . '/../gutenberg.php' ), $matches );
$version_in_file = $matches[1];

$this->assertSame( $file_meta['RequiresWP'], $version_in_file, 'The minimum required WordPress version does not match the plugin header.' );
}
}

0 comments on commit 9e88d86

Please sign in to comment.