diff --git a/plugin/wp-cli-login-server.php b/plugin/wp-cli-login-server.php index 306f133..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; @@ -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); + } }