Skip to content

Commit f656e84

Browse files
committed
(bug) Fix issue with missing version in database
* Gracefully handle missing version constraints. * Set plugin version to semver requirements 0.0.0
1 parent 6aabb74 commit f656e84

3 files changed

Lines changed: 83 additions & 4 deletions

File tree

plugin/wp-cli-login-server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Author URI: https://aaemnnost.tv
77
* Plugin URI: https://aaemnnost.tv/wp-cli-commands/login/
88
*
9-
* Version: 1.5
9+
* Version: 1.5.0
1010
*/
1111

1212
namespace WP_CLI_Login;

src/ServerPlugin.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace WP_CLI_Login;
44

5-
use Composer\Semver\Semver;
5+
use UnexpectedValueException;
66
use WP_CLI_Login\WP_CLI_Login_Server;
77

88
class ServerPlugin
@@ -124,7 +124,26 @@ public function version()
124124
*/
125125
public function versionSatisfies($constraints)
126126
{
127-
return Semver::satisfies($this->version(), $constraints);
127+
$constraints = is_string($constraints) ? trim($constraints) : '';
128+
$version = is_string($this->version()) ? trim($this->version()) : '';
129+
130+
if ($constraints === '' || $version === '') {
131+
// Saved installs prior to 1.5 did not persist a constraint; force reset flow.
132+
return false;
133+
}
134+
135+
try {
136+
$semver = 'Composer\\Semver\\Semver';
137+
138+
if (!class_exists($semver)) {
139+
return false;
140+
}
141+
142+
return call_user_func([$semver, 'satisfies'], $version, $constraints);
143+
} catch (UnexpectedValueException $exception) {
144+
// Bail gracefully if either side is not a valid semver string.
145+
return false;
146+
}
128147
}
129148

130149
/**
@@ -136,7 +155,7 @@ public function data()
136155
{
137156
static $loaded;
138157

139-
if (! $loaded) {
158+
if (!$loaded) {
140159
$loaded = get_plugin_data($this->file);
141160
}
142161

tests/unit/ServerPluginTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
use PHPUnit\Framework\Assert;
4+
use PHPUnit\Framework\TestCase;
5+
use WP_CLI_Login\ServerPlugin;
6+
7+
class ServerPluginTest extends TestCase
8+
{
9+
private function pluginWithVersion($version)
10+
{
11+
return new class('dummy', $version) extends ServerPlugin {
12+
private $version;
13+
14+
public function __construct($file, $version)
15+
{
16+
parent::__construct($file);
17+
$this->version = $version;
18+
}
19+
20+
public function version()
21+
{
22+
return $this->version;
23+
}
24+
};
25+
}
26+
27+
/** @test */
28+
public function empty_constraint_requires_update()
29+
{
30+
$plugin = $this->pluginWithVersion('1.5.0');
31+
32+
Assert::assertFalse($plugin->versionSatisfies(''));
33+
Assert::assertFalse($plugin->versionSatisfies(null));
34+
}
35+
36+
/** @test */
37+
public function empty_version_requires_update()
38+
{
39+
$plugin = $this->pluginWithVersion('');
40+
41+
Assert::assertFalse($plugin->versionSatisfies('^1.5'));
42+
}
43+
44+
/** @test */
45+
public function invalid_constraint_requires_update()
46+
{
47+
$plugin = $this->pluginWithVersion('1.5.0');
48+
49+
Assert::assertFalse($plugin->versionSatisfies('not-a-version'));
50+
}
51+
52+
/** @test */
53+
public function valid_constraint_comparison_still_works()
54+
{
55+
$plugin = $this->pluginWithVersion('1.5.2');
56+
57+
Assert::assertTrue($plugin->versionSatisfies('^1.5'));
58+
Assert::assertFalse($plugin->versionSatisfies('^2.0'));
59+
}
60+
}

0 commit comments

Comments
 (0)