From 780d4df17e42083c5ae01f5dbb5682fd584fb8c1 Mon Sep 17 00:00:00 2001 From: Meritoo Date: Mon, 25 Dec 2017 23:50:47 +0100 Subject: [PATCH] Tests - increase code coverage --- src/Utilities/Uri.php | 94 +++++++++--- tests/Utilities/UriTest.php | 287 +++++++++++++++++++++++++++++++++++- 2 files changed, 356 insertions(+), 25 deletions(-) diff --git a/src/Utilities/Uri.php b/src/Utilities/Uri.php index 96253b0..c5e470d 100644 --- a/src/Utilities/Uri.php +++ b/src/Utilities/Uri.php @@ -24,13 +24,25 @@ class Uri */ public static function getFullUri($withoutHost = false) { - $effect = Miscellaneous::getSafelyGlobalVariable(INPUT_SERVER, 'REQUEST_URI'); + $requestedUrl = Miscellaneous::getSafelyGlobalVariable(INPUT_SERVER, 'REQUEST_URI'); - if ($withoutHost) { - return $effect; + /* + * Unknown requested url? + * Nothing to do + */ + if (empty($requestedUrl)) { + return ''; } - return self::getServerNameOrIp(true) . $effect; + /* + * Without host / server name? + * All is done + */ + if ($withoutHost) { + return $requestedUrl; + } + + return self::getServerNameOrIp(true) . $requestedUrl; } /** @@ -41,13 +53,25 @@ class Uri */ public static function getServerNameOrIp($withProtocol = false) { - $protocol = ''; + $host = Miscellaneous::getSafelyGlobalVariable(INPUT_SERVER, 'HTTP_HOST'); - if ($withProtocol) { - $protocol .= self::getProtocolName() . '://'; + /* + * Unknown host / server? + * Nothing to do + */ + if (empty($host)) { + return ''; } - return $protocol . Miscellaneous::getSafelyGlobalVariable(INPUT_SERVER, 'HTTP_HOST'); + /* + * With protocol? + * Let's include the protocol + */ + if ($withProtocol) { + return sprintf('%s://%s', self::getProtocolName(), $host); + } + + return $host; } /** @@ -57,8 +81,6 @@ class Uri */ public static function getProtocolName() { - $effect = ''; - $matches = []; $protocolData = Miscellaneous::getSafelyGlobalVariable(INPUT_SERVER, 'SERVER_PROTOCOL'); // e.g. HTTP/1.1 $matchCount = preg_match('|(.+)\/(.+)|', $protocolData, $matches); @@ -68,11 +90,14 @@ class Uri * $matches[2] - protocol version, e.g. 1.1 */ - if ($matchCount > 0) { - $effect = strtolower($matches[1]); + /* + * Oops, cannot match protocol + */ + if (0 == $matchCount) { + return ''; } - return $effect; + return strtolower($matches[1]); } /** @@ -82,13 +107,7 @@ class Uri */ public static function getRefererUri() { - $effect = ''; - - if (filter_has_var(INPUT_SERVER, 'HTTP_REFERER')) { - $effect = Miscellaneous::getSafelyGlobalVariable(INPUT_SERVER, 'HTTP_REFERER'); - } - - return $effect; + return Miscellaneous::getSafelyGlobalVariable(INPUT_SERVER, 'HTTP_REFERER'); } /** @@ -214,10 +233,35 @@ class Uri */ public static function isExternalUrl($url) { + /* + * Unknown url or it's just slash? + * Nothing to do + */ + if (empty($url) || '/' === $url) { + return false; + } + $currentUrl = self::getServerNameOrIp(true); $url = self::replenishProtocol($url); - return !Regex::contains($currentUrl, $url); + /* + * Let's prepare pattern of current url + */ + $search = [ + ':', + '/', + '.', + ]; + + $replace = [ + '\:', + '\/', + '\.', + ]; + + $currentUrlPattern = str_replace($search, $replace, $currentUrl); + + return !Regex::contains($url, $currentUrlPattern); } /** @@ -273,6 +317,14 @@ class Uri */ public static function getSecuredUrl($url, $user = '', $password = '') { + /* + * Url is not provided? + * Nothing to do + */ + if (empty($url)) { + return ''; + } + $protocol = self::getProtocolName(); $host = self::getServerNameOrIp(); diff --git a/tests/Utilities/UriTest.php b/tests/Utilities/UriTest.php index 6c195d9..80fbba7 100644 --- a/tests/Utilities/UriTest.php +++ b/tests/Utilities/UriTest.php @@ -8,6 +8,7 @@ namespace Meritoo\Common\Test\Utilities; +use Generator; use Meritoo\Common\Test\Base\BaseTestCase; use Meritoo\Common\Utilities\Uri; @@ -55,19 +56,199 @@ class UriTest extends BaseTestCase * @param string $protocol (optional) The protocol which is replenished. If is empty, protocol of current request * is used. * - * @dataProvider provideUrlsToReplenishProtocol + * @dataProvider provideUrlToReplenishProtocol */ public function testReplenishProtocol($expected, $url, $protocol = '') { self::assertSame($expected, Uri::replenishProtocol($url, $protocol)); } + public function testGetServerNameOrIpWithoutProtocol() + { + $_SERVER['HTTP_HOST'] = ''; + self::assertEquals('', Uri::getServerNameOrIp()); + + $host = 'lorem.com'; + $_SERVER['HTTP_HOST'] = $host; + + self::assertEquals($host, Uri::getServerNameOrIp()); + } + + public function testGetServerNameOrIpWithProtocol() + { + $_SERVER['HTTP_HOST'] = ''; + $_SERVER['SERVER_PROTOCOL'] = ''; + + self::assertEquals('', Uri::getServerNameOrIp(true)); + + $host = 'lorem.com'; + $protocol = 'HTTP/1.1'; + + $_SERVER['HTTP_HOST'] = $host; + $_SERVER['SERVER_PROTOCOL'] = $protocol; + + self::assertEquals(sprintf('http://%s', $host), Uri::getServerNameOrIp(true)); + } + + public function testGetFullUriWithHost() + { + $_SERVER['HTTP_HOST'] = ''; + $_SERVER['SERVER_PROTOCOL'] = ''; + $_SERVER['REQUEST_URI'] = ''; + + self::assertEquals('', Uri::getFullUri()); + + $host = 'lorem.com'; + $protocol = 'HTTP/1.1'; + $requestedUrl = '/test/123'; + + $_SERVER['HTTP_HOST'] = $host; + $_SERVER['SERVER_PROTOCOL'] = $protocol; + $_SERVER['REQUEST_URI'] = $requestedUrl; + + self::assertEquals(sprintf('http://%s%s', $host, $requestedUrl), Uri::getFullUri()); + } + + public function testGetFullUriWithoutHost() + { + $_SERVER['HTTP_HOST'] = ''; + $_SERVER['SERVER_PROTOCOL'] = ''; + $_SERVER['REQUEST_URI'] = ''; + + self::assertEquals('', Uri::getFullUri(true)); + + $requestedUrl = '/test/123'; + $_SERVER['REQUEST_URI'] = $requestedUrl; + + self::assertEquals($requestedUrl, Uri::getFullUri(true)); + } + + public function testGetProtocolName() + { + $_SERVER['SERVER_PROTOCOL'] = ''; + self::assertEquals('', Uri::getProtocolName()); + + $protocol = 'HTTP/1.1'; + $_SERVER['SERVER_PROTOCOL'] = $protocol; + + self::assertEquals('http', Uri::getProtocolName()); + } + + public function testGetRefererUri() + { + $_SERVER['HTTP_REFERER'] = ''; + self::assertEquals('', Uri::getRefererUri()); + + $refererUrl = 'http://lorem.com/test/123'; + $_SERVER['HTTP_REFERER'] = $refererUrl; + + self::assertEquals($refererUrl, Uri::getRefererUri()); + } + + public function testGetUserAddressIp() + { + $_SERVER['REMOTE_ADDR'] = ''; + self::assertEquals('', Uri::getUserAddressIp()); + + $userAddressIp = '1.2.3.4'; + $_SERVER['REMOTE_ADDR'] = $userAddressIp; + + self::assertEquals($userAddressIp, Uri::getUserAddressIp()); + } + + public function testGetUserWebBrowserInfo() + { + $_SERVER['HTTP_USER_AGENT'] = ''; + self::assertEquals('', Uri::getUserWebBrowserInfo()); + + $browserInfo = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.2.5 (KHTML, like Gecko)' + . ' Version/8.0.2 Safari/600.2.5'; + + $_SERVER['HTTP_USER_AGENT'] = $browserInfo; + self::assertEquals($browserInfo, Uri::getUserWebBrowserInfo()); + } + + public function testGetUserWebBrowserNameWithoutVersion() + { + $_SERVER['HTTP_USER_AGENT'] = ''; + self::assertEquals('', Uri::getUserWebBrowserName()); + + $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.2.5 (KHTML, like' + . ' Gecko) Version/8.0.2 Safari/600.2.5'; + + self::assertEquals('Apple Safari', Uri::getUserWebBrowserName()); + } + + public function testGetUserWebBrowserNameWithVersion() + { + $_SERVER['HTTP_USER_AGENT'] = ''; + self::assertEquals('', Uri::getUserWebBrowserName(true)); + + $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.2.5 (KHTML, like' + . ' Gecko) Version/8.0.2 Safari/600.2.5'; + + self::assertEquals('Apple Safari 600.2.5', Uri::getUserWebBrowserName(true)); + } + + public function testGetUserOperatingSystemName() + { + $_SERVER['HTTP_USER_AGENT'] = ''; + self::assertEquals('', Uri::getUserOperatingSystemName()); + + $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.2.5 (KHTML, like' + . ' Gecko) Version/8.0.2 Safari/600.2.5'; + + self::assertEquals('Mac OS', Uri::getUserOperatingSystemName()); + } + + public function testIsServerLocalhost() + { + $_SERVER['HTTP_HOST'] = ''; + self::assertFalse(Uri::isServerLocalhost()); + + $_SERVER['HTTP_HOST'] = '127.0.0.1'; + self::assertTrue(Uri::isServerLocalhost()); + } + /** - * Provides urls to replenish protocol + * @param string $url The url to check + * @param bool $expected Information if verified url is external * - * @return \Generator + * @dataProvider provideUrlToVerifyIfIsExternal */ - public function provideUrlsToReplenishProtocol() + public function testIsExternalUrl($url, $expected) + { + $host = 'lorem.com'; + $protocol = 'HTTP/1.1'; + + $_SERVER['HTTP_HOST'] = $host; + $_SERVER['SERVER_PROTOCOL'] = $protocol; + + self::assertEquals($expected, Uri::isExternalUrl($url)); + } + + /** + * @param string $url A path / url to some resource, e.g. page, image, css file + * @param string $user User name used to log in + * @param string $password User password used to log in + * @param string $expectedUrl Expected, secured url + * + * @dataProvider provideDataForSecuredUrl + */ + public function testGetSecuredUrl($url, $user, $password, $expectedUrl) + { + $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; + $_SERVER['HTTP_HOST'] = 'lorem.com'; + + self::assertEquals($expectedUrl, Uri::getSecuredUrl($url, $user, $password)); + } + + /** + * Provides url to replenish protocol + * + * @return Generator + */ + public function provideUrlToReplenishProtocol() { yield[ '://test', @@ -81,4 +262,102 @@ class UriTest extends BaseTestCase 'ftp', ]; } + + /** + * Provides url used to verify if it's external, from another server / domain + * + * @return Generator + */ + public function provideUrlToVerifyIfIsExternal() + { + yield[ + '', + false, + ]; + + yield[ + '/', + false, + ]; + + yield[ + 'http://something.different/first-page', + true, + ]; + + yield[ + 'something.different/first-page', + true, + ]; + + yield[ + 'http://lorem.com', + false, + ]; + + yield[ + 'http://lorem.com/contact', + false, + ]; + + yield[ + 'lorem.com', + false, + ]; + + yield[ + 'lorem.com/contact', + false, + ]; + } + + /** + * Provides data used to build secured url + * + * @return Generator + */ + public function provideDataForSecuredUrl() + { + yield[ + '', + '', + '', + '', + ]; + + yield[ + '/', + '', + '', + 'http://lorem.com/', + ]; + + yield[ + 'contact', + '', + '', + 'http://lorem.com/contact', + ]; + + yield[ + 'contact', + 'john', + '', + 'http://lorem.com/contact', + ]; + + yield[ + 'contact', + '', + 'pass123', + 'http://lorem.com/contact', + ]; + + yield[ + 'contact', + 'john', + 'pass123', + 'http://john:pass123@lorem.com/contact', + ]; + } }