Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugin/wp-cli-login-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 22 additions & 3 deletions src/ServerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace WP_CLI_Login;

use Composer\Semver\Semver;
use UnexpectedValueException;
use WP_CLI_Login\WP_CLI_Login_Server;

class ServerPlugin
Expand Down Expand Up @@ -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.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
}
}

/**
Expand All @@ -136,7 +155,7 @@ public function data()
{
static $loaded;

if (! $loaded) {
if (!$loaded) {
$loaded = get_plugin_data($this->file);
}

Expand Down
60 changes: 60 additions & 0 deletions tests/unit/ServerPluginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use WP_CLI_Login\ServerPlugin;

class ServerPluginTest extends TestCase
{
private function pluginWithVersion($version)
{
return new class('dummy', $version) extends ServerPlugin {
private $version;

public function __construct($file, $version)
{
parent::__construct($file);
$this->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'));
}
}