| 1 |
<?php |
| 2 |
/** |
| 3 |
* Unit tests asserting the release metadata is internally consistent. |
| 4 |
* |
| 5 |
* A version bump has to land in four places at once: the plugin header, the |
| 6 |
* PHP constant, the readme's Stable tag and the readme changelog. Missing one |
| 7 |
* ships a release that WordPress.org and the update checker disagree about. |
| 8 |
* These tests fail the build when they drift apart. |
| 9 |
* |
| 10 |
* @package Custom_404_Pro |
| 11 |
*/ |
| 12 |
|
| 13 |
use PHPUnit\Framework\TestCase; |
| 14 |
|
| 15 |
/** |
| 16 |
* Release metadata test case. |
| 17 |
*/ |
| 18 |
class VersionTest extends TestCase { |
| 19 |
|
| 20 |
/** |
| 21 |
* Contents of the main plugin file. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
private $plugin_file; |
| 26 |
|
| 27 |
/** |
| 28 |
* Contents of readme.txt. |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
private $readme; |
| 33 |
|
| 34 |
/** |
| 35 |
* Loads the plugin file and readme once per test. |
| 36 |
*/ |
| 37 |
protected function setUp(): void { |
| 38 |
parent::setUp(); |
| 39 |
$this->plugin_file = file_get_contents( dirname( __DIR__ ) . '/custom-404-pro.php' ); |
| 40 |
$this->readme = file_get_contents( dirname( __DIR__ ) . '/readme.txt' ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Returns the first capture group of a pattern matched against a subject. |
| 45 |
* |
| 46 |
* @param string $pattern Regular expression with one capture group. |
| 47 |
* @param string $subject Text to search. |
| 48 |
* @param string $label Human-readable name used in the failure message. |
| 49 |
* @return string The captured value. |
| 50 |
*/ |
| 51 |
private function capture( string $pattern, string $subject, string $label ): string { |
| 52 |
$matched = preg_match( $pattern, $subject, $matches ); |
| 53 |
$this->assertSame( 1, $matched, "Could not find {$label}." ); |
| 54 |
return trim( $matches[1] ); |
| 55 |
} |
| 56 |
|
| 57 |
// ------------------------------------------------------------------------- |
| 58 |
// Version consistency |
| 59 |
// ------------------------------------------------------------------------- |
| 60 |
|
| 61 |
/** |
| 62 |
* The plugin header version and the PHP constant must agree. |
| 63 |
*/ |
| 64 |
public function test_plugin_header_version_matches_version_constant() { |
| 65 |
$header = $this->capture( '/^\s*\*\s*Version:\s*(.+)$/m', $this->plugin_file, 'the Version: plugin header' ); |
| 66 |
$constant = $this->capture( "/define\(\s*'CUSTOM_404_PRO_VERSION',\s*'([^']+)'\s*\)/", $this->plugin_file, 'the CUSTOM_404_PRO_VERSION constant' ); |
| 67 |
|
| 68 |
$this->assertSame( $header, $constant, 'Plugin header Version and CUSTOM_404_PRO_VERSION must match.' ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* The readme Stable tag must point at the version being shipped. |
| 73 |
*/ |
| 74 |
public function test_readme_stable_tag_matches_plugin_version() { |
| 75 |
$header = $this->capture( '/^\s*\*\s*Version:\s*(.+)$/m', $this->plugin_file, 'the Version: plugin header' ); |
| 76 |
$stable_tag = $this->capture( '/^Stable tag:\s*(.+)$/m', $this->readme, 'the Stable tag' ); |
| 77 |
|
| 78 |
$this->assertSame( $header, $stable_tag, 'readme.txt Stable tag must match the plugin version.' ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Stable tag must name a real release, never trunk. |
| 83 |
*/ |
| 84 |
public function test_readme_stable_tag_is_not_trunk() { |
| 85 |
$stable_tag = $this->capture( '/^Stable tag:\s*(.+)$/m', $this->readme, 'the Stable tag' ); |
| 86 |
$this->assertNotSame( 'trunk', strtolower( $stable_tag ), 'Stable tag must point at a tagged release, not trunk.' ); |
| 87 |
$this->assertMatchesRegularExpression( '/^\d+\.\d+\.\d+$/', $stable_tag, 'Stable tag must be a semantic version.' ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* The version being shipped must have its own changelog entry. |
| 92 |
*/ |
| 93 |
public function test_current_version_has_a_changelog_entry() { |
| 94 |
$version = $this->capture( "/define\(\s*'CUSTOM_404_PRO_VERSION',\s*'([^']+)'\s*\)/", $this->plugin_file, 'the CUSTOM_404_PRO_VERSION constant' ); |
| 95 |
|
| 96 |
$this->assertStringContainsString( |
| 97 |
'= ' . $version . ' =', |
| 98 |
$this->readme, |
| 99 |
"readme.txt must contain a changelog entry for version {$version}." |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
// ------------------------------------------------------------------------- |
| 104 |
// Compatibility declarations |
| 105 |
// ------------------------------------------------------------------------- |
| 106 |
|
| 107 |
/** |
| 108 |
* The plugin header must declare a minimum WordPress version. |
| 109 |
* |
| 110 |
* WordPress uses this to block updates on incompatible sites. Without it, |
| 111 |
* an unsupported install downloads the update and fataled instead. |
| 112 |
*/ |
| 113 |
public function test_plugin_header_declares_requires_at_least() { |
| 114 |
$this->assertMatchesRegularExpression( |
| 115 |
'/^\s*\*\s*Requires at least:\s*\d+\.\d+/m', |
| 116 |
$this->plugin_file, |
| 117 |
'The plugin header must declare "Requires at least".' |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* The plugin header must declare a minimum PHP version. |
| 123 |
*/ |
| 124 |
public function test_plugin_header_declares_requires_php() { |
| 125 |
$this->assertMatchesRegularExpression( |
| 126 |
'/^\s*\*\s*Requires PHP:\s*\d+\.\d+/m', |
| 127 |
$this->plugin_file, |
| 128 |
'The plugin header must declare "Requires PHP".' |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* The plugin header and the readme must declare the same requirements. |
| 134 |
*/ |
| 135 |
public function test_plugin_header_requirements_match_readme() { |
| 136 |
$header_wp = $this->capture( '/^\s*\*\s*Requires at least:\s*(.+)$/m', $this->plugin_file, 'the header Requires at least' ); |
| 137 |
$readme_wp = $this->capture( '/^Requires at least:\s*(.+)$/m', $this->readme, 'the readme Requires at least' ); |
| 138 |
$header_php = $this->capture( '/^\s*\*\s*Requires PHP:\s*(.+)$/m', $this->plugin_file, 'the header Requires PHP' ); |
| 139 |
$readme_php = $this->capture( '/^Requires PHP:\s*(.+)$/m', $this->readme, 'the readme Requires PHP' ); |
| 140 |
|
| 141 |
$this->assertSame( $readme_wp, $header_wp, 'Requires at least must match between the plugin header and readme.txt.' ); |
| 142 |
$this->assertSame( $readme_php, $header_php, 'Requires PHP must match between the plugin header and readme.txt.' ); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* "Tested up to" must name the WordPress release this plugin was verified against. |
| 147 |
* |
| 148 |
* WordPress.org shows a compatibility warning once this falls more than |
| 149 |
* three major releases behind current core. |
| 150 |
*/ |
| 151 |
public function test_readme_is_tested_up_to_wordpress_7_1() { |
| 152 |
$tested = $this->capture( '/^Tested up to:\s*(.+)$/m', $this->readme, 'the Tested up to value' ); |
| 153 |
$this->assertSame( '7.1', $tested, 'readme.txt must declare compatibility with WordPress 7.1.' ); |
| 154 |
} |
| 155 |
} |
| 156 |
|