From 8c30e7160c403b3b2f04d673bbf7c2c522b4e40a Mon Sep 17 00:00:00 2001 From: Evan Mattson Date: Thu, 27 Oct 2022 21:49:38 -0400 Subject: [PATCH 1/2] update server to support parsing a trailing slash --- plugin/wp-cli-login-server.php | 14 +++++++---- tests/unit/WP_CLI_Login_ServerTest.php | 33 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/plugin/wp-cli-login-server.php b/plugin/wp-cli-login-server.php index 306f133..3b0cc17 100644 --- a/plugin/wp-cli-login-server.php +++ b/plugin/wp-cli-login-server.php @@ -83,10 +83,16 @@ public function __construct($endpoint, $publicKey) */ public static function parseUri($uri) { - return array_slice( - array_merge(['',''], explode('/', $uri)), - -2 - ); + $uri = trim($uri, '/'); + $segments = explode('/', $uri); + + // If there aren't at least 2 segments, + // return empty values to always return an array with the same length. + if (count($segments) < 2) { + return ['', '']; + } + + return array_slice($segments, -2); } /** diff --git a/tests/unit/WP_CLI_Login_ServerTest.php b/tests/unit/WP_CLI_Login_ServerTest.php index 0fcd8aa..c49c946 100644 --- a/tests/unit/WP_CLI_Login_ServerTest.php +++ b/tests/unit/WP_CLI_Login_ServerTest.php @@ -14,6 +14,15 @@ function parses_endpoint_and_key_from_uri() $this->assertSame('key', $public); } + /** @test */ + function parses_endpoint_and_key_from_uri_with_trailing_slash() + { + list($endpoint, $public) = WP_CLI_Login_Server::parseUri('/end/key/'); + + $this->assertSame('end', $endpoint); + $this->assertSame('key', $public); + } + /** @test */ function parses_endpoint_and_key_from_uri_for_subdirectory_site() { @@ -23,4 +32,28 @@ function parses_endpoint_and_key_from_uri_for_subdirectory_site() $this->assertSame('key', $public); } + /** @test */ + function parses_endpoint_and_key_from_uri_with_trailing_slash_for_subdirectory_site() + { + list($endpoint, $public) = WP_CLI_Login_Server::parseUri('/abc/end/key/'); + + $this->assertSame('end', $endpoint); + $this->assertSame('key', $public); + } + + /** @test */ + function parses_an_endpoint_with_a_single_segment() { + list($endpoint, $public) = WP_CLI_Login_Server::parseUri('/abc'); + + $this->assertSame('', $endpoint); + $this->assertSame('', $public); + } + + /** @test */ + function parses_an_endpoint_with_no_segment() { + list($endpoint, $public) = WP_CLI_Login_Server::parseUri('/'); + + $this->assertSame('', $endpoint); + $this->assertSame('', $public); + } } From 1d36e6216af1a76672d0ae08070cc3128e7d5c80 Mon Sep 17 00:00:00 2001 From: Evan Mattson Date: Thu, 27 Oct 2022 21:51:50 -0400 Subject: [PATCH 2/2] bump server version --- plugin/wp-cli-login-server.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/wp-cli-login-server.php b/plugin/wp-cli-login-server.php index 3b0cc17..7c49de6 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.3 + * Version: 1.4 */ namespace WP_CLI_Login;