diff --git a/plugin/wp-cli-login-server.php b/plugin/wp-cli-login-server.php index 32e609b..bc90e3e 100644 --- a/plugin/wp-cli-login-server.php +++ b/plugin/wp-cli-login-server.php @@ -6,7 +6,7 @@ * Author URI: https://aaemnnost.tv * Plugin URI: https://aaemnnost.tv/wp-cli-commands/login/ * - * Version: 1.5 + * Version: 1.5.0 */ namespace WP_CLI_Login; diff --git a/src/ServerPlugin.php b/src/ServerPlugin.php index c20f140..ac1d009 100644 --- a/src/ServerPlugin.php +++ b/src/ServerPlugin.php @@ -2,7 +2,7 @@ namespace WP_CLI_Login; -use Composer\Semver\Semver; +use UnexpectedValueException; use WP_CLI_Login\WP_CLI_Login_Server; class ServerPlugin @@ -124,7 +124,26 @@ public function version() */ public function versionSatisfies($constraints) { - return Semver::satisfies($this->version(), $constraints); + $constraints = is_string($constraints) ? trim($constraints) : ''; + $version = is_string($this->version()) ? trim($this->version()) : ''; + + if ($constraints === '' || $version === '') { + // Saved installs prior to 1.5 did not persist a constraint; force reset flow. + return false; + } + + try { + $semver = 'Composer\\Semver\\Semver'; + + if (!class_exists($semver)) { + return false; + } + + return call_user_func([$semver, 'satisfies'], $version, $constraints); + } catch (UnexpectedValueException $exception) { + // Bail gracefully if either side is not a valid semver string. + return false; + } } /** @@ -136,7 +155,7 @@ public function data() { static $loaded; - if (! $loaded) { + if (!$loaded) { $loaded = get_plugin_data($this->file); } diff --git a/tests/unit/ServerPluginTest.php b/tests/unit/ServerPluginTest.php new file mode 100644 index 0000000..ffc5a25 --- /dev/null +++ b/tests/unit/ServerPluginTest.php @@ -0,0 +1,60 @@ +version = $version; + } + + public function version() + { + return $this->version; + } + }; + } + + /** @test */ + public function empty_constraint_requires_update() + { + $plugin = $this->pluginWithVersion('1.5.0'); + + Assert::assertFalse($plugin->versionSatisfies('')); + Assert::assertFalse($plugin->versionSatisfies(null)); + } + + /** @test */ + public function empty_version_requires_update() + { + $plugin = $this->pluginWithVersion(''); + + Assert::assertFalse($plugin->versionSatisfies('^1.5')); + } + + /** @test */ + public function invalid_constraint_requires_update() + { + $plugin = $this->pluginWithVersion('1.5.0'); + + Assert::assertFalse($plugin->versionSatisfies('not-a-version')); + } + + /** @test */ + public function valid_constraint_comparison_still_works() + { + $plugin = $this->pluginWithVersion('1.5.2'); + + Assert::assertTrue($plugin->versionSatisfies('^1.5')); + Assert::assertFalse($plugin->versionSatisfies('^2.0')); + } +}