From a574146f042d52e2acbec3bd4ce98ad1eeaf6057 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 4 Jan 2023 11:38:08 -0500 Subject: [PATCH] Add UriInterface-enabled cleanUri method in Model\GServer - Tests! --- src/Model/GServer.php | 30 +++++++++++-- tests/src/Model/GServerTest.php | 76 +++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 tests/src/Model/GServerTest.php diff --git a/src/Model/GServer.php b/src/Model/GServer.php index 1878befa9f..9e32c7b0cb 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -45,6 +45,7 @@ use Friendica\Util\Strings; use Friendica\Util\XML; use Friendica\Network\HTTPException; use GuzzleHttp\Psr7\Uri; +use Psr\Http\Message\UriInterface; /** * This class handles GServer related functions @@ -442,18 +443,41 @@ class GServer * * @return string cleaned URL * @throws Exception + * @deprecated since 2023.03 Use cleanUri instead */ public static function cleanURL(string $dirtyUrl): string { try { - $url = str_replace('/index.php', '', trim($dirtyUrl, '/')); - return (string)(new Uri($url))->withUserInfo('')->withQuery('')->withFragment(''); + return (string)self::cleanUri(new Uri($dirtyUrl)); } catch (\Throwable $e) { - Logger::warning('Invalid URL', ['dirtyUrl' => $dirtyUrl, 'url' => $url]); + Logger::warning('Invalid URL', ['dirtyUrl' => $dirtyUrl]); return ''; } } + /** + * Remove unwanted content from the given URI + * + * @param UriInterface $dirtyUri + * + * @return UriInterface cleaned URI + * @throws Exception + */ + public static function cleanUri(UriInterface $dirtyUri): string + { + return $dirtyUri + ->withUserInfo('') + ->withQuery('') + ->withFragment('') + ->withPath( + preg_replace( + '#(?:^|/)index\.php#', + '', + rtrim($dirtyUri->getPath(), '/') + ) + ); + } + /** * Detect server data (type, protocol, version number, ...) * The detected data is then updated or inserted in the gserver table. diff --git a/tests/src/Model/GServerTest.php b/tests/src/Model/GServerTest.php new file mode 100644 index 0000000000..a56f4ed6ff --- /dev/null +++ b/tests/src/Model/GServerTest.php @@ -0,0 +1,76 @@ +. + * + */ + +namespace Friendica\Test\src\Model; + +use Friendica\Model\GServer; +use GuzzleHttp\Psr7\Uri; +use Psr\Http\Message\UriInterface; + +class GServerTest extends \PHPUnit\Framework\TestCase +{ + public function dataCleanUri(): array + { + return [ + 'full-monty' => [ + 'expected' => new Uri('https://example.com/path'), + 'dirtyUri' => new Uri('https://user:password@example.com/path?query=string#fragment'), + ], + 'index.php' => [ + 'expected' => new Uri('https://example.com'), + 'dirtyUri' => new Uri('https://example.com/index.php'), + ], + 'index.php-2' => [ + 'expected' => new Uri('https://example.com/path/to/resource'), + 'dirtyUri' => new Uri('https://example.com/index.php/path/to/resource'), + ], + 'index.php-path' => [ + 'expected' => new Uri('https://example.com/path/to'), + 'dirtyUri' => new Uri('https://example.com/path/to/index.php'), + ], + 'index.php-path-2' => [ + 'expected' => new Uri('https://example.com/path/to/path/to/resource'), + 'dirtyUri' => new Uri('https://example.com/path/to/index.php/path/to/resource'), + ], + 'index.php-slash' => [ + 'expected' => new Uri('https://example.com'), + 'dirtyUri' => new Uri('https://example.com/index.php/'), + ], + 'index.php-slash-2' => [ + 'expected' => new Uri('https://example.com/path/to/resource'), + 'dirtyUri' => new Uri('https://example.com/index.php/path/to/resource/'), + ], + ]; + } + + /** + * @dataProvider dataCleanUri + * + * @param UriInterface $expected + * @param UriInterface $dirtyUri + * @return void + * @throws \Exception + */ + public function testCleanUri(UriInterface $expected, UriInterface $dirtyUri) + { + $this->assertEquals($expected, GServer::cleanUri($dirtyUri)); + } +}