From e509c6b65d745132f0d8173e218a9683f20ebc09 Mon Sep 17 00:00:00 2001 From: Pavle Date: Thu, 2 Dec 2021 14:53:50 +0100 Subject: [PATCH] Release 3.7 --- .gitignore | 5 + IntegrationConfigHelpers.php | 24 +- KnownUser.php | 40 +- Models.php | 25 +- QueueITHelpers.php | 9 + README.md | 36 +- Tests/HttpRequestProviderMock.php | 12 +- Tests/IntegrationConfigHelpersTest.php | 181 +++-- Tests/KnownUserTest.php | 643 ++++++++++-------- Tests/TestSuite.php | 3 +- Tests/UserInQueueServiceTest.php | 185 ++--- .../UserInQueueStateCookieRepositoryTest.php | 281 +++++--- UserInQueueService.php | 30 +- UserInQueueStateCookieRepository.php | 126 ++-- 14 files changed, 958 insertions(+), 642 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c59b8bb --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/nbproject/private/ +Tests/vendor +Tests/composer.lock +/simpletest +/.vs diff --git a/IntegrationConfigHelpers.php b/IntegrationConfigHelpers.php index 5d63b19..5cc6ed3 100644 --- a/IntegrationConfigHelpers.php +++ b/IntegrationConfigHelpers.php @@ -22,7 +22,7 @@ public function getMatchedIntegrationConfig(array $customerIntegration, $current return false; } if ($this->evaluateTrigger($trigger, $currentPageUrl, $request)) { - return $integrationConfig; + return $integrationConfig; } } } @@ -70,6 +70,8 @@ private function evaluateTriggerPart(array $triggerPart, $currentPageUrl, $reque return UserAgentValidatorHelper::evaluate($triggerPart, $request->getUserAgent()); case "HttpHeaderValidator": return HttpHeaderValidatorHelper::evaluate($triggerPart, $request->getHeaderArray()); + case "RequestBodyValidator": + return RequestBodyValidatorHelper::evaluate($triggerPart, $request->getRequestBodyAsString()); default: return false; } @@ -181,6 +183,26 @@ public static function evaluate(array $triggerPart, array $headerList) { } } +class RequestBodyValidatorHelper +{ + public static function evaluate(array $triggerPart, $requestBody) { + + if (!array_key_exists("Operator", $triggerPart) || + !array_key_exists("IsNegative", $triggerPart) || + !array_key_exists("IsIgnoreCase", $triggerPart)) { + return false; + } + + return ComparisonOperatorHelper::Evaluate( + $triggerPart["Operator"], + $triggerPart["IsNegative"], + $triggerPart["IsIgnoreCase"], + $requestBody, + array_key_exists("ValueToCompare",$triggerPart)? $triggerPart["ValueToCompare"]: null, + array_key_exists("ValuesToCompare",$triggerPart)? $triggerPart["ValuesToCompare"]: null); + } +} + class ComparisonOperatorHelper { public static function evaluate($opt, $isNegative, $isIgnoreCase, $value, $valueToCompare, $valuesToCompare) { diff --git a/KnownUser.php b/KnownUser.php index 0f7b6bd..c2cb6c2 100644 --- a/KnownUser.php +++ b/KnownUser.php @@ -10,7 +10,7 @@ class KnownUser { - const QueueITAjaxHeaderKey = "x-queueit-ajaxpageurl"; + const QueueITAjaxHeaderKey = "x-queueit-ajaxpageurl"; //used for unittest private static $userInQueueService = null; @@ -22,6 +22,10 @@ private static function getUserInQueueService() return KnownUser::$userInQueueService; } + public static function setHttpRequestProvider(IHttpRequestProvider $customHttpRequestProvider){ + KnownUser::$httpRequestProvider = $customHttpRequestProvider; + } + //used for unittest private static $httpRequestProvider = null; private static function getHttpRequestProvider() @@ -33,7 +37,7 @@ private static function getHttpRequestProvider() } private static $debugInfoArray = null; - public static function extendQueueCookie($eventId, $cookieValidityMinute, $cookieDomain, $secretKey) + public static function extendQueueCookie($eventId, $cookieValidityMinute, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, $secretKey) { if (empty($eventId)) { throw new KnownUserException("eventId can not be null or empty."); @@ -45,7 +49,7 @@ public static function extendQueueCookie($eventId, $cookieValidityMinute, $cooki throw new KnownUserException("cookieValidityMinute should be integer greater than 0."); } $userInQueueService = KnownUser::getUserInQueueService(); - $userInQueueService->extendQueueCookie($eventId, $cookieValidityMinute, $cookieDomain, $secretKey); + $userInQueueService->extendQueueCookie($eventId, $cookieValidityMinute, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, $secretKey); } private static function _cancelRequestByLocalConfig( @@ -107,7 +111,7 @@ public static function cancelRequestByLocalConfig( } try { - $result = KnownUser::_cancelRequestByLocalConfig($targetUrl,$queueitToken,$cancelConfig,$customerId,$secretKey,$connectorDiagnostics->isEnabled); + $result = KnownUser::_cancelRequestByLocalConfig($targetUrl, $queueitToken, $cancelConfig, $customerId, $secretKey, $connectorDiagnostics->isEnabled); KnownUser::sendDebugCookie(); return $result; } catch (\Exception $e) { @@ -217,14 +221,14 @@ public static function validateRequestByIntegrationConfig($currentUrlWithoutQueu public static function resolveQueueRequestByLocalConfig($targetUrl, $queueitToken, QueueEventConfig $queueConfig, $customerId, $secretKey) { $connectorDiagnostics = ConnectorDiagnostics::verify($customerId, $secretKey, $queueitToken); - + if ($connectorDiagnostics->hasError) { return $connectorDiagnostics->validationResult; } try { $targetUrl = KnownUser::generateTargetUrl($targetUrl); - $result = KnownUser::_resolveQueueRequestByLocalConfig($targetUrl, $queueitToken, $queueConfig, $customerId, $secretKey,$connectorDiagnostics->isEnabled); + $result = KnownUser::_resolveQueueRequestByLocalConfig($targetUrl, $queueitToken, $queueConfig, $customerId, $secretKey, $connectorDiagnostics->isEnabled); KnownUser::sendDebugCookie(); return $result; } catch (\Exception $e) { @@ -291,12 +295,14 @@ private static function handleQueueAction( $eventConfig = new QueueEventConfig(); $targetUrl = ""; $eventConfig->eventId = $matchedConfig["EventId"]; - $eventConfig->queueDomain = $matchedConfig["QueueDomain"]; $eventConfig->layoutName = $matchedConfig["LayoutName"]; $eventConfig->culture = $matchedConfig["Culture"]; - $eventConfig->cookieDomain = $matchedConfig["CookieDomain"]; + $eventConfig->queueDomain = $matchedConfig["QueueDomain"]; $eventConfig->extendCookieValidity = $matchedConfig["ExtendCookieValidity"]; $eventConfig->cookieValidityMinute = $matchedConfig["CookieValidityMinute"]; + $eventConfig->cookieDomain = $matchedConfig["CookieDomain"]; + $eventConfig->isCookieHttpOnly = array_key_exists("IsCookieHttpOnly", $matchedConfig) ? $matchedConfig["IsCookieHttpOnly"] : false; + $eventConfig->isCookieSecure = array_key_exists("IsCookieSecure", $matchedConfig) ? $matchedConfig["IsCookieSecure"] : false; $eventConfig->version = $customerIntegration["Version"]; $eventConfig->actionName = $matchedConfig["Name"]; @@ -325,10 +331,12 @@ private static function handleCancelAction( $isDebug ) { $cancelEventConfig = new CancelEventConfig(); - $cancelEventConfig->eventId = $matchedConfig["EventId"]; $cancelEventConfig->queueDomain = $matchedConfig["QueueDomain"]; - $cancelEventConfig->cookieDomain = $matchedConfig["CookieDomain"]; + $cancelEventConfig->eventId = $matchedConfig["EventId"]; $cancelEventConfig->version = $customerIntegration["Version"]; + $cancelEventConfig->cookieDomain = $matchedConfig["CookieDomain"]; + $cancelEventConfig->isCookieHttpOnly = array_key_exists("IsCookieHttpOnly", $matchedConfig) ? $matchedConfig["IsCookieHttpOnly"] : false; + $cancelEventConfig->isCookieSecure = array_key_exists("IsCookieSecure", $matchedConfig) ? $matchedConfig["IsCookieSecure"] : false; $cancelEventConfig->actionName = $matchedConfig["Name"]; return KnownUser::_cancelRequestByLocalConfig($currentUrlWithoutQueueITToken, $queueitToken, $cancelEventConfig, $customerId, $secretKey, $isDebug); @@ -374,7 +382,7 @@ private static function sendDebugCookie() foreach (KnownUser::$debugInfoArray as $key => $value) { array_push($cookieNameValues, $key . '=' . $value); } - KnownUser::getHttpRequestProvider()->getCookieManager()->setCookie("queueitdebug", implode('|', $cookieNameValues), 0, null); + KnownUser::getHttpRequestProvider()->getCookieManager()->setCookie("queueitdebug", implode('|', $cookieNameValues), 0, null, false, false); } } @@ -413,12 +421,12 @@ public function getCookie($cookieName) } } - public function setCookie($name, $value, $expire, $domain) + public function setCookie($name, $value, $expire, $domain, $isHttpOnly, $isSecure) { if ($domain == null) { $domain = ""; } - setcookie($name, $value, $expire, "/", $domain, false, false); + setcookie($name, $value, $expire, "/", $domain, $isSecure, $isHttpOnly); } public function getCookieArray() @@ -438,6 +446,7 @@ function getUserHostAddress(); function getCookieManager(); function getAbsoluteUri(); function getHeaderArray(); + function getRequestBodyAsString(); } class HttpRequestProvider implements IHttpRequestProvider @@ -489,6 +498,11 @@ function getHeaderArray() } return $this->allHeadersLowerCaseKeyArray; } + + function getRequestBodyAsString() + { + return ''; + } } //https://github.com/ralouphie/getallheaders/blob/master/src/getallheaders.php diff --git a/Models.php b/Models.php index 76ca2dc..90d2932 100644 --- a/Models.php +++ b/Models.php @@ -11,6 +11,8 @@ class QueueEventConfig public $extendCookieValidity; public $cookieValidityMinute; public $cookieDomain; + public $isCookieHttpOnly; + public $isCookieSecure; public $version; public $actionName; @@ -23,8 +25,14 @@ public function getString() { return "EventId:" . $this->eventId . "&Version:" . $this->version . "&ActionName:" . $this->actionName - . "&QueueDomain:" . $this->queueDomain . "&CookieDomain:" . $this->cookieDomain . "&ExtendCookieValidity:" . $this->extendCookieValidity - . "&CookieValidityMinute:" . $this->cookieValidityMinute . "&LayoutName:" . $this->layoutName . "&Culture:" . $this->culture; + . "&QueueDomain:" . $this->queueDomain + . "&CookieDomain:" . $this->cookieDomain + . "&IsCookieHttpOnly:" . Utils::boolToString($this->isCookieHttpOnly) + . "&IsCookieSecure:" . Utils::boolToString($this->isCookieSecure) + . "&ExtendCookieValidity:" . Utils::boolToString($this->extendCookieValidity) + . "&CookieValidityMinute:" . $this->cookieValidityMinute + . "&LayoutName:" . $this->layoutName + . "&Culture:" . $this->culture; } } @@ -33,6 +41,8 @@ class CancelEventConfig public $eventId; public $queueDomain; public $cookieDomain; + public $isCookieHttpOnly; + public $isCookieSecure; public $version; public $actionName; @@ -44,8 +54,11 @@ function __construct() { public function getString() { return "EventId:" . $this->eventId . "&Version:" . $this->version - . "&ActionName:" . $this->actionName - . "&QueueDomain:" . $this->queueDomain . "&CookieDomain:" . $this->cookieDomain; + . "&QueueDomain:" . $this->queueDomain + . "&CookieDomain:" . $this->cookieDomain + . "&IsCookieHttpOnly:" . Utils::boolToString($this->isCookieHttpOnly) + . "&IsCookieSecure:" . Utils::boolToString($this->isCookieSecure) + . "&ActionName:" . $this->actionName; } } @@ -55,7 +68,7 @@ class RequestValidationResult public $redirectUrl; public $queueId; public $actionType; - public $redirectType; + public $redirectType; public $actionName; public $isAjaxResult; @@ -64,7 +77,7 @@ function __construct($actionType, $eventId, $queueId, $redirectUrl, $redirectTyp $this->eventId = $eventId; $this->queueId = $queueId; $this->redirectUrl = $redirectUrl; - $this->redirectType = $redirectType; + $this->redirectType = $redirectType; $this->actionName = $actionName; } diff --git a/QueueITHelpers.php b/QueueITHelpers.php index f470562..e46d92d 100644 --- a/QueueITHelpers.php +++ b/QueueITHelpers.php @@ -8,6 +8,15 @@ public static function isNullOrEmptyString($value) { return (!isset($value) || trim($value) === ''); } + + public static function boolToString($value) + { + if(is_null($value)) { + return "null"; + } + + return $value ? "true" : "false"; + } } class QueueUrlParams diff --git a/README.md b/README.md index aa2d2a4..9f99c14 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,8 @@ try else { header('HTTP/1.0: 200'); - header($result->getAjaxQueueRedirectHeaderKey() . ': '. $result->getAjaxRedirectUrl()); + header($result->getAjaxQueueRedirectHeaderKey() . ': ' . $result->getAjaxRedirectUrl()); + header("Access-Control-Expose-Headers" . ': ' . $result->getAjaxQueueRedirectHeaderKey()); } die(); @@ -155,3 +156,36 @@ catch(\Exception $e) // This was a configuration error, so we let the user continue } ``` +## Request body trigger (advanced) + +The connector supports triggering on request body content. An example could be a POST call with specific item ID where you want end-users to queue up for. +For this to work, you will need to contact Queue-it support or enable request body triggers in your integration settings in your GO Queue-it platform account. +Once enabled you will need to update your integration so request body is available for the connector. +You need to create a new context provider similar to this one: + +```php + +class HttpRequestBodyProvider extends QueueIT\KnownUserV3\SDK\HttpRequestProvider +{ + function getRequestBodyAsString() + { + $requestBody = file_get_contents('php://input'); + + if(isset($requestBody)){ + return $requestBody; + } + else{ + return ''; + } + } +} + +``` + +And then use it instead of default `HttpRequestProvider` + +```php +// Default implementation of HttpRequestProvider always returns empty string as request body. +// Use following line to set a custom httpRequestBodyProvider +QueueIT\KnownUserV3\SDK\KnownUser::setHttpRequestProvider(new HttpRequestBodyProvider()); +``` \ No newline at end of file diff --git a/Tests/HttpRequestProviderMock.php b/Tests/HttpRequestProviderMock.php index 419023e..0d7f91e 100644 --- a/Tests/HttpRequestProviderMock.php +++ b/Tests/HttpRequestProviderMock.php @@ -4,17 +4,18 @@ class HttpRequestProviderMock implements QueueIT\KnownUserV3\SDK\IHttpRequestProvider { public $userAgent; - public $userHostAddress; + public $userHostAddress; public $cookieManager; public $absoluteUri; public $headerArray; + public $requestBody; public function getUserAgent() { return $this->userAgent; } - public function getUserHostAddress() { - return $this->userHostAddress; - } + public function getUserHostAddress() { + return $this->userHostAddress; + } public function getCookieManager() { return $this->cookieManager; } @@ -26,5 +27,8 @@ public function getHeaderArray() { return array(); return $this->headerArray; } + public function getRequestBodyAsString() { + return $this->requestBody; + } } ?> \ No newline at end of file diff --git a/Tests/IntegrationConfigHelpersTest.php b/Tests/IntegrationConfigHelpersTest.php index 7fa83a1..dc208e5 100644 --- a/Tests/IntegrationConfigHelpersTest.php +++ b/Tests/IntegrationConfigHelpersTest.php @@ -1,4 +1,6 @@ assertTrue( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("Equals", false, false, "test1", "test1",NULL)); - $this->assertFalse( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("Equals", false, false, "test1", "Test1",NULL)); - $this->assertTrue( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("Equals", false, true, "test1", "Test1",NULL)); - $this->assertTrue( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("Equals", true, false, "test1", "Test1",NULL)); - $this->assertFalse( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("Equals", true, false, "test1", "test1",NULL)); - $this->assertFalse( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("Equals", true, true, "test1", "Test1",NULL)); + $this->assertTrue( SDK\ComparisonOperatorHelper::evaluate("Equals", false, false, "test1", "test1",NULL)); + $this->assertFalse( SDK\ComparisonOperatorHelper::evaluate("Equals", false, false, "test1", "Test1",NULL)); + $this->assertTrue( SDK\ComparisonOperatorHelper::evaluate("Equals", false, true, "test1", "Test1",NULL)); + $this->assertTrue( SDK\ComparisonOperatorHelper::evaluate("Equals", true, false, "test1", "Test1",NULL)); + $this->assertFalse( SDK\ComparisonOperatorHelper::evaluate("Equals", true, false, "test1", "test1",NULL)); + $this->assertFalse( SDK\ComparisonOperatorHelper::evaluate("Equals", true, true, "test1", "Test1",NULL)); } function test_evaluate_contains() { - $this->assertTrue( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("Contains", false, false, "test_test1_test", "test1",NULL)); - $this->assertFalse( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("Contains",false, false, "test_test1_test", "Test1",NULL)); - $this->assertTrue( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("Contains",false, true, "test_test1_test", "Test1",NULL)); - $this->assertTrue( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("Contains", true, false, "test_test1_test", "Test1",NULL)); - $this->assertFalse( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("Contains",true, true, "test_test1", "Test1",NULL)); - $this->assertFalse( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("Contains",true, false, "test_test1", "test1",NULL)); - $this->assertTrue( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("Contains",false, false, "test_dsdsdsdtest1", "*",NULL)); - $this->assertFalse( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("Contains",false, false, "", "*",NULL)); + $this->assertTrue( SDK\ComparisonOperatorHelper::evaluate("Contains", false, false, "test_test1_test", "test1",NULL)); + $this->assertFalse( SDK\ComparisonOperatorHelper::evaluate("Contains",false, false, "test_test1_test", "Test1",NULL)); + $this->assertTrue( SDK\ComparisonOperatorHelper::evaluate("Contains",false, true, "test_test1_test", "Test1",NULL)); + $this->assertTrue( SDK\ComparisonOperatorHelper::evaluate("Contains", true, false, "test_test1_test", "Test1",NULL)); + $this->assertFalse( SDK\ComparisonOperatorHelper::evaluate("Contains",true, true, "test_test1", "Test1",NULL)); + $this->assertFalse( SDK\ComparisonOperatorHelper::evaluate("Contains",true, false, "test_test1", "test1",NULL)); + $this->assertTrue( SDK\ComparisonOperatorHelper::evaluate("Contains",false, false, "test_dsdsdsdtest1", "*",NULL)); + $this->assertFalse( SDK\ComparisonOperatorHelper::evaluate("Contains",false, false, "", "*",NULL)); } function test_evaluate_EqualsAny() { - $this->assertTrue( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("EqualsAny",false, false, "test1", NULL,array("test1"))); - $this->assertFalse( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("EqualsAny",false, false, "test1", NULL,array("Test1"))); - $this->assertTrue( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("EqualsAny",false, true, "test1", NULL,array("Test1"))); - $this->assertTrue( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("EqualsAny",true, false, "test1", NULL,array("Test1"))); - $this->assertFalse( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("EqualsAny",true, false, "test1", NULL,array("test1"))); - $this->assertFalse( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("EqualsAny",true, true, "test1", NULL,array("Test1"))); + $this->assertTrue( SDK\ComparisonOperatorHelper::evaluate("EqualsAny",false, false, "test1", NULL,array("test1"))); + $this->assertFalse( SDK\ComparisonOperatorHelper::evaluate("EqualsAny",false, false, "test1", NULL,array("Test1"))); + $this->assertTrue( SDK\ComparisonOperatorHelper::evaluate("EqualsAny",false, true, "test1", NULL,array("Test1"))); + $this->assertTrue( SDK\ComparisonOperatorHelper::evaluate("EqualsAny",true, false, "test1", NULL,array("Test1"))); + $this->assertFalse( SDK\ComparisonOperatorHelper::evaluate("EqualsAny",true, false, "test1", NULL,array("test1"))); + $this->assertFalse( SDK\ComparisonOperatorHelper::evaluate("EqualsAny",true, true, "test1", NULL,array("Test1"))); } function test_evaluate_ContainsAny() { - $this->assertTrue( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("ContainsAny",false, false, "test_test1_test", NULL,array("test1"))); - $this->assertFalse( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("ContainsAny",false, false, "test_test1_test", NULL,array("Test1"))); - $this->assertTrue( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("ContainsAny",false, true, "test_test1_test", NULL,array("Test1"))); - $this->assertTrue( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("ContainsAny",true, false, "test_test1_test", NULL,array("Test1"))); - $this->assertFalse( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("ContainsAny",true, true, "test_test1", NULL,array("Test1"))); - $this->assertFalse( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("ContainsAny",true, false, "test_test1", NULL,array("test1"))); - $this->assertTrue( QueueIT\KnownUserV3\SDK\ComparisonOperatorHelper::evaluate("ContainsAny",false, false, "test_dsdsdsdtest1", NULL,array("*"))); + $this->assertTrue( SDK\ComparisonOperatorHelper::evaluate("ContainsAny",false, false, "test_test1_test", NULL,array("test1"))); + $this->assertFalse( SDK\ComparisonOperatorHelper::evaluate("ContainsAny",false, false, "test_test1_test", NULL,array("Test1"))); + $this->assertTrue( SDK\ComparisonOperatorHelper::evaluate("ContainsAny",false, true, "test_test1_test", NULL,array("Test1"))); + $this->assertTrue( SDK\ComparisonOperatorHelper::evaluate("ContainsAny",true, false, "test_test1_test", NULL,array("Test1"))); + $this->assertFalse( SDK\ComparisonOperatorHelper::evaluate("ContainsAny",true, true, "test_test1", NULL,array("Test1"))); + $this->assertFalse( SDK\ComparisonOperatorHelper::evaluate("ContainsAny",true, false, "test_test1", NULL,array("test1"))); + $this->assertTrue( SDK\ComparisonOperatorHelper::evaluate("ContainsAny",false, false, "test_dsdsdsdtest1", NULL,array("*"))); } } @@ -61,42 +63,42 @@ function test_evaluate() $triggerPart ["IsIgnoreCase"] = true; $triggerPart ["IsNegative"] = false; $triggerPart ["ValueToCompare"]= "http://test.tesdomain.com:8080/test?q=1"; - $this->assertFalse( QueueIT\KnownUserV3\SDK\UrlValidatorHelper::evaluate($triggerPart, "http://test.tesdomain.com:8080/test?q=2")); + $this->assertFalse( SDK\UrlValidatorHelper::evaluate($triggerPart, "http://test.tesdomain.com:8080/test?q=2")); $triggerPart ["ValueToCompare"] = "/Test/t1"; $triggerPart ["UrlPart"] = "PagePath"; $triggerPart ["Operator"]= "Equals"; $triggerPart ["IsIgnoreCase"] = true; $triggerPart ["IsNegative"] = false; - $this->assertTrue( QueueIT\KnownUserV3\SDK\UrlValidatorHelper::evaluate($triggerPart, "http://test.tesdomain.com:8080/test/t1?q=2&y02")); + $this->assertTrue( SDK\UrlValidatorHelper::evaluate($triggerPart, "http://test.tesdomain.com:8080/test/t1?q=2&y02")); $triggerPart ["UrlPart"] = "HostName"; $triggerPart ["ValueToCompare"] = "test.tesdomain.com"; $triggerPart ["Operator"]= "Contains"; $triggerPart ["IsIgnoreCase"] = true; $triggerPart ["IsNegative"] = false; - $this->assertTrue( QueueIT\KnownUserV3\SDK\UrlValidatorHelper::evaluate($triggerPart, "http://m.test.tesdomain.com:8080/test?q=2")); + $this->assertTrue( SDK\UrlValidatorHelper::evaluate($triggerPart, "http://m.test.tesdomain.com:8080/test?q=2")); $triggerPart ["UrlPart"] = "HostName"; $triggerPart ["ValueToCompare"] = "test.tesdomain.com"; $triggerPart ["Operator"]= "Contains"; $triggerPart ["IsIgnoreCase"] = true; $triggerPart ["IsNegative"] = true; - $this->assertFalse( QueueIT\KnownUserV3\SDK\UrlValidatorHelper::evaluate($triggerPart,"http://m.test.tesdomain.com:8080/test?q=2")); + $this->assertFalse( SDK\UrlValidatorHelper::evaluate($triggerPart,"http://m.test.tesdomain.com:8080/test?q=2")); $triggerPart ["UrlPart"] = "HostName"; $triggerPart ["ValuesToCompare"] = array("balablaba","test.tesdomain.com"); $triggerPart ["Operator"]= "Contains"; $triggerPart ["IsIgnoreCase"] = true; $triggerPart ["IsNegative"] = false; - $this->assertTrue( QueueIT\KnownUserV3\SDK\UrlValidatorHelper::evaluate($triggerPart,"http://m.test.tesdomain.com:8080/test?q=2")); + $this->assertTrue( SDK\UrlValidatorHelper::evaluate($triggerPart,"http://m.test.tesdomain.com:8080/test?q=2")); $triggerPart ["ValuesToCompare"] = array("ssss_SSss","/Test/t1"); $triggerPart ["UrlPart"] = "PagePath"; $triggerPart ["Operator"]= "EqualsAny"; $triggerPart ["IsIgnoreCase"] = true; $triggerPart ["IsNegative"] = false; - $this->assertTrue( QueueIT\KnownUserV3\SDK\UrlValidatorHelper::evaluate($triggerPart, "http://test.tesdomain.com:8080/test/t1?q=2&y02")); + $this->assertTrue( SDK\UrlValidatorHelper::evaluate($triggerPart, "http://test.tesdomain.com:8080/test/t1?q=2&y02")); } } @@ -110,13 +112,13 @@ function test_evaluate() $triggerPart ["IsIgnoreCase"] = true; $triggerPart ["IsNegative"] = false; $triggerPart ["ValueToCompare"] = "1"; - $this->assertFalse( QueueIT\KnownUserV3\SDK\CookieValidatorHelper::evaluate($triggerPart, array("c1"=>"hhh"))); + $this->assertFalse( SDK\CookieValidatorHelper::evaluate($triggerPart, array("c1"=>"hhh"))); $triggerPart = array(); $triggerPart ["CookieName"] = "c1"; $triggerPart ["Operator"] = "Contains"; $triggerPart ["ValueToCompare"] = "1"; - $this->assertFalse( QueueIT\KnownUserV3\SDK\CookieValidatorHelper::evaluate($triggerPart, array("c2"=>"ddd","c1"=>"1"))); + $this->assertFalse( SDK\CookieValidatorHelper::evaluate($triggerPart, array("c2"=>"ddd","c1"=>"1"))); $triggerPart = array(); $triggerPart ["CookieName"] = "c1"; @@ -124,7 +126,7 @@ function test_evaluate() $triggerPart ["ValueToCompare"] = "1"; $triggerPart ["IsNegative"] = false; $triggerPart ["IsIgnoreCase"] = true; - $this->assertTrue( QueueIT\KnownUserV3\SDK\CookieValidatorHelper::evaluate($triggerPart,array("c2"=>"ddd","c1"=>"1"))); + $this->assertTrue( SDK\CookieValidatorHelper::evaluate($triggerPart,array("c2"=>"ddd","c1"=>"1"))); $triggerPart = array(); $triggerPart ["CookieName"] = "c1"; @@ -132,7 +134,7 @@ function test_evaluate() $triggerPart ["ValueToCompare"] = "1"; $triggerPart ["IsNegative"] = true; $triggerPart ["IsIgnoreCase"] = true; - $this->assertFalse( QueueIT\KnownUserV3\SDK\CookieValidatorHelper::evaluate($triggerPart,array("c2"=>"ddd","c1"=>"1"))); + $this->assertFalse( SDK\CookieValidatorHelper::evaluate($triggerPart,array("c2"=>"ddd","c1"=>"1"))); $triggerPart = array(); $triggerPart ["CookieName"] = "c1"; @@ -140,7 +142,7 @@ function test_evaluate() $triggerPart ["ValuesToCompare"] = array("cookievalue","value"); $triggerPart ["IsIgnoreCase"] = true; $triggerPart ["IsNegative"] = false; - $this->assertTrue( QueueIT\KnownUserV3\SDK\CookieValidatorHelper::evaluate($triggerPart,array("c2"=>"ddd","c1"=>"cookie value value value"))); + $this->assertTrue( SDK\CookieValidatorHelper::evaluate($triggerPart,array("c2"=>"ddd","c1"=>"cookie value value value"))); $triggerPart = array(); $triggerPart ["CookieName"] = "c1"; @@ -148,75 +150,74 @@ function test_evaluate() $triggerPart ["ValuesToCompare"] = array("cookievalue","1"); $triggerPart ["IsIgnoreCase"] = true; $triggerPart ["IsNegative"] = true; - $this->assertFalse( QueueIT\KnownUserV3\SDK\CookieValidatorHelper::evaluate($triggerPart,array("c2"=>"ddd","c1"=>"1"))); + $this->assertFalse( SDK\CookieValidatorHelper::evaluate($triggerPart,array("c2"=>"ddd","c1"=>"1"))); } } class UserAgentValidatorHelperTest extends UnitTestCase { - function test_evaluate() + function test_evaluate() { $triggerPart = array(); $triggerPart ["Operator"] = "Contains"; $triggerPart ["IsIgnoreCase"] = false; $triggerPart ["IsNegative"] = false; $triggerPart ["ValueToCompare"] = "googlebot"; - $this->assertFalse( QueueIT\KnownUserV3\SDK\UserAgentValidatorHelper::evaluate($triggerPart, "Googlebot sample useraagent")); + $this->assertFalse( SDK\UserAgentValidatorHelper::evaluate($triggerPart, "Googlebot sample useraagent")); $triggerPart = array(); $triggerPart ["Operator"] = "Equals"; $triggerPart ["ValueToCompare"] = "googlebot"; $triggerPart ["IsIgnoreCase"] = true; $triggerPart ["IsNegative"] = true; - $this->assertTrue( QueueIT\KnownUserV3\SDK\UserAgentValidatorHelper::evaluate($triggerPart,"oglebot sample useraagent")); + $this->assertTrue( SDK\UserAgentValidatorHelper::evaluate($triggerPart,"oglebot sample useraagent")); $triggerPart = array(); $triggerPart ["Operator"] = "Contains"; $triggerPart ["ValueToCompare"] = "googlebot"; $triggerPart ["IsIgnoreCase"] = false; $triggerPart ["IsNegative"] = true; - $this->assertFalse( QueueIT\KnownUserV3\SDK\UserAgentValidatorHelper::evaluate($triggerPart, "googlebot")); + $this->assertFalse( SDK\UserAgentValidatorHelper::evaluate($triggerPart, "googlebot")); $triggerPart = array(); $triggerPart ["Operator"] = "Contains"; $triggerPart ["ValueToCompare"] = "googlebot"; $triggerPart ["IsIgnoreCase"] = true; $triggerPart ["IsNegative"] = false; - $this->assertTrue( QueueIT\KnownUserV3\SDK\UserAgentValidatorHelper::evaluate($triggerPart, "Googlebot")); + $this->assertTrue( SDK\UserAgentValidatorHelper::evaluate($triggerPart, "Googlebot")); $triggerPart = array(); $triggerPart ["Operator"] = "ContainsAny"; $triggerPart ["ValuesToCompare"] = array("googlebot"); $triggerPart ["IsIgnoreCase"] = true; $triggerPart ["IsNegative"] = false; - $this->assertTrue( QueueIT\KnownUserV3\SDK\UserAgentValidatorHelper::evaluate($triggerPart, "Googlebot")); + $this->assertTrue( SDK\UserAgentValidatorHelper::evaluate($triggerPart, "Googlebot")); $triggerPart = array(); $triggerPart ["Operator"] = "EqualsAny"; $triggerPart ["ValuesToCompare"] =array("googlebot"); $triggerPart ["IsIgnoreCase"] = true; $triggerPart ["IsNegative"] = true; - $this->assertTrue( QueueIT\KnownUserV3\SDK\UserAgentValidatorHelper::evaluate($triggerPart, "oglebot sample useraagent")); + $this->assertTrue( SDK\UserAgentValidatorHelper::evaluate($triggerPart, "oglebot sample useraagent")); } } -class HttoheaderValidatorHelperTest extends UnitTestCase +class HttpHeaderValidatorHelperTest extends UnitTestCase { - function test_evaluate() + function test_evaluate() { $triggerPart = array(); $triggerPart ["Operator"] = "Contains"; $triggerPart ["IsIgnoreCase"] = false; $triggerPart ["IsNegative"] = false; $triggerPart ["ValueToCompare"] = "googlebot"; - $this->assertFalse( QueueIT\KnownUserV3\SDK\HttpHeaderValidatorHelper::evaluate($triggerPart, array(""))); + $this->assertFalse( SDK\HttpHeaderValidatorHelper::evaluate($triggerPart, array(""))); $triggerPart = array(); $triggerPart ["Operator"] = "Contains"; $triggerPart ["IsIgnoreCase"] = false; $triggerPart ["IsNegative"] = false; - - $this->assertFalse( QueueIT\KnownUserV3\SDK\HttpHeaderValidatorHelper::evaluate($triggerPart, array("c2"=>"t1","c3"=>"t1"))); + $this->assertFalse( SDK\HttpHeaderValidatorHelper::evaluate($triggerPart, array("c2"=>"t1","c3"=>"t1"))); $triggerPart = array(); $triggerPart ["Operator"] = "Equals"; @@ -224,7 +225,7 @@ function test_evaluate() $triggerPart ["IsNegative"] = true; $triggerPart ["ValueToCompare"] = "t1"; $triggerPart ["HttpHeaderName"] = "c1"; - $this->assertTrue( QueueIT\KnownUserV3\SDK\HttpHeaderValidatorHelper::evaluate($triggerPart,array("c2"=>"t1","c3"=>"t1"))); + $this->assertTrue( SDK\HttpHeaderValidatorHelper::evaluate($triggerPart,array("c2"=>"t1","c3"=>"t1"))); $triggerPart = array(); $triggerPart ["Operator"] = "Contains"; @@ -232,7 +233,7 @@ function test_evaluate() $triggerPart ["IsNegative"] = true; $triggerPart ["ValueToCompare"] = "t1"; $triggerPart ["HttpHeaderName"] = "C1"; - $this->assertFalse( QueueIT\KnownUserV3\SDK\HttpHeaderValidatorHelper::evaluate($triggerPart, array("c2"=>"t1","c3"=>"t1","c1"=>"test t1 test "))); + $this->assertFalse( SDK\HttpHeaderValidatorHelper::evaluate($triggerPart, array("c2"=>"t1","c3"=>"t1","c1"=>"test t1 test "))); $triggerPart = array(); $triggerPart ["Operator"] = "Contains"; @@ -240,7 +241,7 @@ function test_evaluate() $triggerPart ["IsNegative"] = false; $triggerPart ["ValueToCompare"] = "t1"; $triggerPart ["HttpHeaderName"] = "C1"; - $this->assertTrue( QueueIT\KnownUserV3\SDK\HttpHeaderValidatorHelper::evaluate($triggerPart, array("c2"=>"t1","c3"=>"t1","c1"=>"test T1 test "))); + $this->assertTrue( SDK\HttpHeaderValidatorHelper::evaluate($triggerPart, array("c2"=>"t1","c3"=>"t1","c1"=>"test T1 test "))); $triggerPart = array(); $triggerPart ["Operator"] = "ContainsAny"; @@ -248,7 +249,7 @@ function test_evaluate() $triggerPart ["IsNegative"] = false; $triggerPart ["ValuesToCompare"] = array("blabalabala","t1","t2"); $triggerPart ["HttpHeaderName"] = "C1"; - $this->assertTrue( QueueIT\KnownUserV3\SDK\HttpHeaderValidatorHelper::evaluate($triggerPart, array("c2"=>"t1","c3"=>"t1","c1"=>"test T1 test "))); + $this->assertTrue( SDK\HttpHeaderValidatorHelper::evaluate($triggerPart, array("c2"=>"t1","c3"=>"t1","c1"=>"test T1 test "))); $triggerPart = array(); $triggerPart ["Operator"] = "EqualsAny"; @@ -256,7 +257,55 @@ function test_evaluate() $triggerPart ["IsNegative"] = true; $triggerPart ["ValuesToCompare"] =array("bla","bla", "t1"); $triggerPart ["HttpHeaderName"] = "c1"; - $this->assertFalse( QueueIT\KnownUserV3\SDK\HttpHeaderValidatorHelper::evaluate($triggerPart,array("c2"=>"t1","c3"=>"t1","c1"=>"t1"))); + $this->assertFalse( SDK\HttpHeaderValidatorHelper::evaluate($triggerPart,array("c2"=>"t1","c3"=>"t1","c1"=>"t1"))); + } +} + +class HttpRequestBodyValidatorHelperTest extends UnitTestCase +{ + function test_evaluate() + { + $triggerPart = array(); + $triggerPart ["Operator"] = "Contains"; + $triggerPart ["IsIgnoreCase"] = false; + $triggerPart ["IsNegative"] = false; + $triggerPart ["ValueToCompare"] = "admin123"; + $this->assertFalse( SDK\RequestBodyValidatorHelper::evaluate($triggerPart, "Admin123@admin.com")); + + $triggerPart = array(); + $triggerPart ["Operator"] = "Equals"; + $triggerPart ["ValueToCompare"] = "enduser"; + $triggerPart ["IsIgnoreCase"] = true; + $triggerPart ["IsNegative"] = true; + $this->assertTrue( SDK\RequestBodyValidatorHelper::evaluate($triggerPart,"nduser")); + + $triggerPart = array(); + $triggerPart ["Operator"] = "Contains"; + $triggerPart ["ValueToCompare"] = "product123"; + $triggerPart ["IsIgnoreCase"] = false; + $triggerPart ["IsNegative"] = true; + $this->assertFalse( SDK\RequestBodyValidatorHelper::evaluate($triggerPart, "product123")); + + $triggerPart = array(); + $triggerPart ["Operator"] = "Contains"; + $triggerPart ["ValueToCompare"] = "product123"; + $triggerPart ["IsIgnoreCase"] = true; + $triggerPart ["IsNegative"] = false; + $this->assertTrue( SDK\RequestBodyValidatorHelper::evaluate($triggerPart, "Product123")); + + $triggerPart = array(); + $triggerPart ["Operator"] = "ContainsAny"; + $triggerPart ["ValuesToCompare"] = array("product123"); + $triggerPart ["IsIgnoreCase"] = true; + $triggerPart ["IsNegative"] = false; + $this->assertTrue( SDK\RequestBodyValidatorHelper::evaluate($triggerPart, "Product123")); + + $triggerPart = array(); + $triggerPart ["Operator"] = "EqualsAny"; + $triggerPart ["ValuesToCompare"] =array("product123"); + $triggerPart ["IsIgnoreCase"] = true; + $triggerPart ["IsNegative"] = true; + $this->assertTrue( SDK\RequestBodyValidatorHelper::evaluate($triggerPart, "roduct123 in the basket")); } } @@ -298,7 +347,7 @@ function test_getMatchedIntegrationConfig_OneTrigger_And_NotMatched() $url = "http://test.tesdomain.com:8080/test?q=2"; - $testObject = new QueueIT\KnownUserV3\SDK\IntegrationEvaluator(); + $testObject = new SDK\IntegrationEvaluator(); $this->assertTrue( $testObject->getMatchedIntegrationConfig($integrationConfig, $url, $request) === null); } @@ -341,7 +390,7 @@ function test_getMatchedIntegrationConfig_OneTrigger_And_Matched() ); $url = "http://test.tesdomain.com:8080/test?q=2"; - $testObject = new QueueIT\KnownUserV3\SDK\IntegrationEvaluator(); + $testObject = new SDK\IntegrationEvaluator(); $result = $testObject->getMatchedIntegrationConfig($integrationConfig, $url, $request); $this->assertTrue($result["Name"]==="integration1"); @@ -392,7 +441,7 @@ function test_getMatchedIntegrationConfig_OneTrigger_And_NotMatched_UserAgent() ); $url = "http://test.tesdomain.com:8080/test?q=2"; - $testObject = new QueueIT\KnownUserV3\SDK\IntegrationEvaluator(); + $testObject = new SDK\IntegrationEvaluator(); $this->assertTrue($testObject->getMatchedIntegrationConfig($integrationConfig, $url,$request)==NULL); @@ -445,7 +494,7 @@ function test_getMatchedIntegrationConfig_OneTrigger_And_NotMatched_HttpHeader() ); $url = "http://test.tesdomain.com:8080/test?q=2"; - $testObject = new QueueIT\KnownUserV3\SDK\IntegrationEvaluator(); + $testObject = new SDK\IntegrationEvaluator(); $this->assertTrue($testObject->getMatchedIntegrationConfig($integrationConfig, $url,$request)==NULL); @@ -487,7 +536,7 @@ function test_getMatchedIntegrationConfig_OneTrigger_Or_NotMatched() ); $url = "http://test.tesdomain.com:8080/test?q=2"; - $testObject = new QueueIT\KnownUserV3\SDK\IntegrationEvaluator(); + $testObject = new SDK\IntegrationEvaluator(); $this->assertTrue($testObject->getMatchedIntegrationConfig($integrationConfig, $url,$request)==null); @@ -529,7 +578,7 @@ function test_getMatchedIntegrationConfig_OneTrigger_Or_Matched() ); $url = "http://test.tesdomain.com:8080/test?q=2"; - $testObject = new QueueIT\KnownUserV3\SDK\IntegrationEvaluator(); + $testObject = new SDK\IntegrationEvaluator(); $result = $testObject->getMatchedIntegrationConfig($integrationConfig, $url, $request); $this->assertTrue($result["Name"]==="integration1"); @@ -587,7 +636,7 @@ function test_getMatchedIntegrationConfig_TwoTriggers_Matched() $url = "http://test.tesdomain.com:8080/test?q=2"; - $testObject = new QueueIT\KnownUserV3\SDK\IntegrationEvaluator(); + $testObject = new SDK\IntegrationEvaluator(); $result = $testObject->getMatchedIntegrationConfig($integrationConfig, $url,$request); $this->assertTrue($result["Name"]=="integration1"); @@ -659,14 +708,14 @@ function test_getMatchedIntegrationConfig_ThreeIntegrationsInOrder_SecondMatched $url = "http://test.tesdomain.com:8080/test?q=2"; - $testObject = new QueueIT\KnownUserV3\SDK\IntegrationEvaluator(); + $testObject = new SDK\IntegrationEvaluator(); $result = $testObject->getMatchedIntegrationConfig($integrationConfig,$url,$request); $this->assertTrue($result["Name"]=="integration1"); } } -class IntegrationConfigHelpersCookieManagerMock implements QueueIT\KnownUserV3\SDK\ICookieManager +class IntegrationConfigHelpersCookieManagerMock implements SDK\ICookieManager { public $debugInfoCookie; public $cookieArray; @@ -674,7 +723,7 @@ public function getCookie($cookieName) { return $this->debugInfoCookie; } - public function setCookie($name, $value, $expire, $domain) { + public function setCookie($name, $value, $expire, $domain, $isCookieHttpOnly, $isCookieSecure) { if ($domain == NULL) { $domain = ""; } diff --git a/Tests/KnownUserTest.php b/Tests/KnownUserTest.php index 3e58f7d..d6b4207 100644 --- a/Tests/KnownUserTest.php +++ b/Tests/KnownUserTest.php @@ -19,7 +19,7 @@ public function getCookie($cookieName) return $this->debugInfoCookie; } - public function setCookie($name, $value, $expire, $domain) + public function setCookie($name, $value, $expire, $domain, $isCookieHttpOnly, $isCookieSecure) { if ($domain == NULL) { $domain = ""; @@ -110,12 +110,16 @@ public function extendQueueCookie( $eventId, $cookieValidityMinute, $cookieDomain, + $isCookieHttpOnly, + $isCookieSecure, $secretKey ) { array_push($this->arrayFunctionCallsArgs['extendQueueCookie'], array( $eventId, $cookieValidityMinute, $cookieDomain, + $isCookieHttpOnly, + $isCookieSecure, $secretKey )); } @@ -348,7 +352,7 @@ function test_extendQueueCookie_null_EventId() $exceptionThrown = false; try { - QueueIT\KnownUserV3\SDK\KnownUser::extendQueueCookie(NULL, 10, "cookieDomain", "secretkey"); + QueueIT\KnownUserV3\SDK\KnownUser::extendQueueCookie(NULL, 10, "cookieDomain", false, false, "secretkey"); } catch (Exception $e) { $exceptionThrown = $e->getMessage() == "eventId can not be null or empty."; } @@ -367,7 +371,7 @@ function test_extendQueueCookie_null_SecretKey() $exceptionThrown = false; try { - QueueIT\KnownUserV3\SDK\KnownUser::extendQueueCookie("event1", 10, "cookieDomain", NULL); + QueueIT\KnownUserV3\SDK\KnownUser::extendQueueCookie("event1", 10, "cookieDomain", false, false, NULL); } catch (Exception $e) { $exceptionThrown = $e->getMessage() == "secretKey can not be null or empty."; } @@ -380,7 +384,7 @@ function test_extendQueueCookie_Invalid_CookieValidityMinute() $exceptionThrown = false; try { - QueueIT\KnownUserV3\SDK\KnownUser::extendQueueCookie("event1", "invalidInt", "cookieDomain", "secretkey"); + QueueIT\KnownUserV3\SDK\KnownUser::extendQueueCookie("event1", "invalidInt", "cookieDomain", false, false, "secretkey"); } catch (Exception $e) { $exceptionThrown = $e->getMessage() == "cookieValidityMinute should be integer greater than 0."; } @@ -392,7 +396,7 @@ function test_extendQueueCookie_Negative_CookieValidityMinute() $this->setHttpHeaderRequestProvider(); $exceptionThrown = false; try { - QueueIT\KnownUserV3\SDK\KnownUser::extendQueueCookie("event1", -1, "cookieDomain", "secretkey"); + QueueIT\KnownUserV3\SDK\KnownUser::extendQueueCookie("event1", -1, "cookieDomain", false, false, "secretkey"); } catch (Exception $e) { $exceptionThrown = $e->getMessage() == "cookieValidityMinute should be integer greater than 0."; } @@ -407,9 +411,9 @@ function test_extendQueueCookie() $r->setAccessible(true); $r->setValue(null, $userInQueueservice); - QueueIT\KnownUserV3\SDK\KnownUser::extendQueueCookie("eventid", 10, "cookieDomain", "secretkey"); + QueueIT\KnownUserV3\SDK\KnownUser::extendQueueCookie("eventid", 10, "cookieDomain", true, true, "secretkey"); - $this->assertTrue($userInQueueservice->expectCall('extendQueueCookie', 1, array("eventid", 10, "cookieDomain", "secretkey"))); + $this->assertTrue($userInQueueservice->expectCall('extendQueueCookie', 1, array("eventid", 10, "cookieDomain", true, true, "secretkey"))); } function test_resolveQueueRequestByLocalConfig_empty_eventId() @@ -679,6 +683,8 @@ function test_validateRequestByIntegrationConfig() "ActionType": "Queue", "EventId": "event1", "CookieDomain": ".test.com", + "IsCookieHttpOnly": false, + "IsCookieSecure": false, "LayoutName": "Christmas Layout by Queue-it", "Culture": "", "ExtendCookieValidity": true, @@ -687,19 +693,19 @@ function test_validateRequestByIntegrationConfig() { "TriggerParts": [ { - "Operator": "Contains", - "ValueToCompare": "event1", - "UrlPart": "PageUrl", - "ValidatorType": "UrlValidator", - "IsNegative": false, - "IsIgnoreCase": true + "Operator": "Contains", + "ValueToCompare": "event1", + "UrlPart": "PageUrl", + "ValidatorType": "UrlValidator", + "IsNegative": false, + "IsIgnoreCase": true }, { - "Operator": "Contains", - "ValueToCompare": "googlebot", - "ValidatorType": "UserAgentValidator", - "IsNegative": false, - "IsIgnoreCase": false + "Operator": "Contains", + "ValueToCompare": "googlebot", + "ValidatorType": "UserAgentValidator", + "IsNegative": false, + "IsIgnoreCase": false } ], "LogicalOperator": "And" @@ -764,6 +770,8 @@ function test_validateRequestByIntegrationConfig_AjaxCall() "ActionType": "Queue", "EventId": "event1", "CookieDomain": ".test.com", + "IsCookieHttpOnly": false, + "IsCookieSecure": false, "LayoutName": "Christmas Layout by Queue-it", "Culture": "", "ExtendCookieValidity": true, @@ -772,19 +780,19 @@ function test_validateRequestByIntegrationConfig_AjaxCall() { "TriggerParts": [ { - "Operator": "Contains", - "ValueToCompare": "event1", - "UrlPart": "PageUrl", - "ValidatorType": "UrlValidator", - "IsNegative": false, - "IsIgnoreCase": true + "Operator": "Contains", + "ValueToCompare": "event1", + "UrlPart": "PageUrl", + "ValidatorType": "UrlValidator", + "IsNegative": false, + "IsIgnoreCase": true }, { - "Operator": "Contains", - "ValueToCompare": "googlebot", - "ValidatorType": "UserAgentValidator", - "IsNegative": false, - "IsIgnoreCase": false + "Operator": "Contains", + "ValueToCompare": "googlebot", + "ValidatorType": "UserAgentValidator", + "IsNegative": false, + "IsIgnoreCase": false } ], "LogicalOperator": "And" @@ -865,6 +873,8 @@ function test_validateRequestByIntegrationConfig_ForcedTargeturl() "ActionType": "Queue", "EventId": "event1", "CookieDomain": ".test.com", + "IsCookieHttpOnly": false, + "IsCookieSecure": false, "LayoutName": "Christmas Layout by Queue-it", "Culture": "", "ExtendCookieValidity": true, @@ -929,6 +939,8 @@ function test_validateRequestByIntegrationConfig_ForcedTargeturl_AjaxCall() "ActionType": "Queue", "EventId": "event1", "CookieDomain": ".test.com", + "IsCookieHttpOnly": false, + "IsCookieSecure": false, "LayoutName": "Christmas Layout by Queue-it", "Culture": "", "ExtendCookieValidity": true, @@ -986,6 +998,8 @@ function test_validateRequestByIntegrationConfig_ForecedTargeturl() "ActionType": "Queue", "EventId": "event1", "CookieDomain": ".test.com", + "IsCookieHttpOnly": false, + "IsCookieSecure": false, "LayoutName": "Christmas Layout by Queue-it", "Culture": "", "ExtendCookieValidity": true, @@ -1044,6 +1058,8 @@ function test_validateRequestByIntegrationConfig_EventTargetUrl() "ActionType": "Queue", "EventId": "event1", "CookieDomain": ".test.com", + "IsCookieHttpOnly": false, + "IsCookieSecure": false, "LayoutName": "Christmas Layout by Queue-it", "Culture": "", "ExtendCookieValidity": true, @@ -1108,6 +1124,8 @@ function test_validateRequestByIntegrationConfig_EventTargetUrl_AjaxCall() "ActionType": "Queue", "EventId": "event1", "CookieDomain": ".test.com", + "IsCookieHttpOnly": false, + "IsCookieSecure": false, "LayoutName": "Christmas Layout by Queue-it", "Culture": "", "ExtendCookieValidity": true, @@ -1157,38 +1175,40 @@ function test_validateRequestByIntegrationConfig_CancelAction() $var = "some text"; $integrationConfigString = <<setAccessible(true); $r->setValue(null, $userInQueueservice); - $var = "some text"; $integrationConfigString = <<generateHashDebugValidHash("secretkey", $hashTimestamp); @@ -1460,7 +1487,13 @@ function test_validateRequestByIntegrationConfig_debug() "|SdkVersion=" . QueueIT\KnownUserV3\SDK\UserInQueueService::getSDKVersion() . "|RunTime=" . phpversion() . "|QueueitToken=" . $token . - "|CancelConfig=EventId:event1&Version:3&ActionName:event1action&QueueDomain:knownusertest.queue-it.net&CookieDomain:.test.com" . + "|CancelConfig=EventId:event1" . + "&Version:3" . + "&QueueDomain:knownusertest.queue-it.net" . + "&CookieDomain:.test.com" . + "&IsCookieHttpOnly:false" . + "&IsCookieSecure:false" . + "&ActionName:event1action" . "|OriginalUrl=OriginalURL" . "|ServerUtcTime=" . $timestamp . "|RequestIP=userIP" . @@ -1498,38 +1531,40 @@ function test_validateRequestByIntegrationConfig_withoutmatch_debug() $userInQueueservice->validateCancelRequestResult = new QueueIT\KnownUserV3\SDK\RequestValidationResult("Cancel", "eventid", "queueid", "redirectUrl", null, null); $integrationConfigString = <<generateHashDebugValidHash("secretkey", $hashTimestamp); @@ -1584,38 +1619,40 @@ function test_validateRequestByIntegrationConfig_notvalidhash_debug() $userInQueueservice->validateCancelRequestResult = new QueueIT\KnownUserV3\SDK\RequestValidationResult("Cancel", "eventid", "queueid", "redirectUrl", null, null); $integrationConfigString = <<generateHashDebugValidHash("secretkey", $hashTimestamp); @@ -1848,38 +1885,40 @@ function test_validateRequestByIntegrationConfig_Exception_NoDebugToken_NoDebugC $var = "some text"; $integrationConfigString = <<setValue(null, NULL); $eventconfig = new \QueueIT\KnownUserV3\SDK\QueueEventConfig(); - $eventconfig->cookieDomain = "cookieDomain"; + $eventconfig->eventId = "eventId"; $eventconfig->layoutName = "layoutName"; $eventconfig->culture = "culture"; - $eventconfig->eventId = "eventId"; $eventconfig->queueDomain = "queueDomain"; $eventconfig->extendCookieValidity = true; $eventconfig->cookieValidityMinute = 10; + $eventconfig->cookieDomain = "cookieDomain"; + $eventconfig->isCookieHttpOnly = false; + $eventconfig->isCookieSecure = false; $eventconfig->version = 12; $eventconfig->actionName = "event1action"; @@ -1941,7 +1982,17 @@ function test_resolveQueueRequestByLocalConfig_debug() "|SdkVersion=" . QueueIT\KnownUserV3\SDK\UserInQueueService::getSDKVersion() . "|RunTime=" . phpversion() . "|QueueitToken=" . $token . - "|QueueConfig=EventId:eventId&Version:12&ActionName:event1action&QueueDomain:queueDomain&CookieDomain:cookieDomain&ExtendCookieValidity:1&CookieValidityMinute:10&LayoutName:layoutName&Culture:culture" . + "|QueueConfig=EventId:eventId" . + "&Version:12" . + "&ActionName:event1action" . + "&QueueDomain:queueDomain" . + "&CookieDomain:cookieDomain" . + "&IsCookieHttpOnly:false" . + "&IsCookieSecure:false" . + "&ExtendCookieValidity:true" . + "&CookieValidityMinute:10" . + "&LayoutName:layoutName" . + "&Culture:culture" . "|OriginalUrl=OriginalURL" . "|ServerUtcTime=" . $timestamp . "|RequestIP=userIP" . @@ -2104,10 +2155,12 @@ function test_cancelRequestByLocalConfig_debug() $r->setValue(null, NULL); $cancelEventconfig = new \QueueIT\KnownUserV3\SDK\CancelEventConfig(); - $cancelEventconfig->cookieDomain = "cookiedomain"; $cancelEventconfig->eventId = "eventid"; $cancelEventconfig->queueDomain = "queuedomain"; $cancelEventconfig->version = 1; + $cancelEventconfig->cookieDomain = "cookiedomain"; + $cancelEventconfig->isCookieHttpOnly = false; + $cancelEventconfig->isCookieSecure = false; $cancelEventconfig->actionName = "cancelAction"; $hashTimestamp = strval(time() + (3 * 60)); @@ -2120,7 +2173,13 @@ function test_cancelRequestByLocalConfig_debug() "|SdkVersion=" . QueueIT\KnownUserV3\SDK\UserInQueueService::getSDKVersion() . "|RunTime=" . phpversion() . "|QueueitToken=" . $token . - "|CancelConfig=EventId:eventid&Version:1&ActionName:cancelAction&QueueDomain:queuedomain&CookieDomain:cookiedomain" . + "|CancelConfig=EventId:eventid" . + "&Version:1" . + "&QueueDomain:queuedomain" . + "&CookieDomain:cookiedomain" . + "&IsCookieHttpOnly:false" . + "&IsCookieSecure:false" . + "&ActionName:cancelAction" . "|OriginalUrl=OriginalURL" . "|ServerUtcTime=" . $timestamp . "|RequestIP=userIP" . diff --git a/Tests/TestSuite.php b/Tests/TestSuite.php index 27f29e0..d6e0e11 100644 --- a/Tests/TestSuite.php +++ b/Tests/TestSuite.php @@ -1,7 +1,6 @@ addFile('Tests/QueueUrlParamsTest.php'); $this->addFile('Tests/UserInQueueServiceTest.php'); $this->addFile('Tests/UserInQueueStateCookieRepositoryTest.php'); - } + } } ?> \ No newline at end of file diff --git a/Tests/UserInQueueServiceTest.php b/Tests/UserInQueueServiceTest.php index 1491f09..3f63c7f 100644 --- a/Tests/UserInQueueServiceTest.php +++ b/Tests/UserInQueueServiceTest.php @@ -29,45 +29,56 @@ function __construct() { ); } - public function store($eventId, $queueId, $fixedCookieValidityMinutes, $cookieDomain, $redirectType, $secretKey) { + public function store($eventId, $queueId, $fixedCookieValidityMinutes, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, $redirectType, $secretKey) { array_push( - $this->arrayFunctionCallsArgs['store'], - array( - $eventId, + $this->arrayFunctionCallsArgs['store'], + array( + $eventId, $queueId, $fixedCookieValidityMinutes, $cookieDomain, + $isCookieHttpOnly, + $isCookieSecure, $redirectType, $secretKey) - ); + ); } public function getState($eventId, $cookieValidityMinutes, $secretKey, $validateTime) { array_push( - $this->arrayFunctionCallsArgs['getState'], - array( - $eventId, - $cookieValidityMinutes, - $secretKey, - $validateTime) - ); + $this->arrayFunctionCallsArgs['getState'], + array( + $eventId, + $cookieValidityMinutes, + $secretKey, + $validateTime) + ); return $this->arrayReturns['getState'][count($this->arrayFunctionCallsArgs['getState']) - 1]; } - public function cancelQueueCookie($eventId, $cookieDomain) { - array_push($this->arrayFunctionCallsArgs['cancelQueueCookie'], array($eventId, $cookieDomain)); + public function cancelQueueCookie($eventId, $cookieDomain, $isCookieHttpOnly, $isCookieSecure) { + array_push( + $this->arrayFunctionCallsArgs['cancelQueueCookie'], + array( + $eventId, + $cookieDomain, + $isCookieHttpOnly, + $isCookieSecure) + ); } - public function reissueQueueCookie($eventId, $cookieValidityMinutes, $cookieDomain, $secretKey) { + public function reissueQueueCookie($eventId, $cookieValidityMinutes, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, $secretKey) { array_push( - $this->arrayFunctionCallsArgs['store'], - array( - $eventId, - $cookieValidityMinutes, - $cookieDomain, - $secretKey) - ); + $this->arrayFunctionCallsArgs['store'], + array( + $eventId, + $cookieValidityMinutes, + $cookieDomain, + $isCookieHttpOnly, + $isCookieSecure, + $secretKey) + ); } public function expectCall($functionName, $secquenceNo, array $argument) { @@ -108,10 +119,10 @@ public function test_validateQueueRequest_ValidState_ExtendableCookie_NoCookieEx $eventConfig->extendCookieValidity = false; $eventConfig->actionName = "QueueAction"; $cookieProviderMock = new UserInQueueStateRepositoryMockClass (); - + array_push($cookieProviderMock->arrayReturns['getState'], new QueueIT\KnownUserV3\SDK\StateInfo(true, true, "queueId", null, "idle")); $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueService($cookieProviderMock); - + $result = $testObject->validateQueueRequest("url", "token", $eventConfig, "customerid", "key"); $this->assertTrue(!$result->doRedirect()); @@ -120,30 +131,32 @@ public function test_validateQueueRequest_ValidState_ExtendableCookie_NoCookieEx $this->assertTrue($cookieProviderMock->expectCall('getState', 1, array("e1", 10, 'key', true))); $this->assertTrue(strtolower($result->actionName) == strtolower('QueueAction')); } - + public function test_validateQueueRequest_ValidState_ExtendableCookie_CookieExtensionFromConfig_DoNotRedirectDoStoreCookieWithExtension() { $eventConfig = new QueueIT\KnownUserV3\SDK\QueueEventConfig(); $eventConfig->eventId = "e1"; $eventConfig->queueDomain = "testDomain.com"; $eventConfig->cookieDomain = "testDomain"; + $eventConfig->isCookieHttpOnly = false; + $eventConfig->isCookieSecure = false; $eventConfig->cookieValidityMinute=10; $eventConfig->extendCookieValidity=true; $eventConfig->actionName = "QueueAction"; $cookieProviderMock = new UserInQueueStateRepositoryMockClass (); array_push($cookieProviderMock->arrayReturns['getState'], new QueueIT\KnownUserV3\SDK\StateInfo(true, true, "queueId", null, "disabled")); - + $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueService($cookieProviderMock); - + $result = $testObject->validateQueueRequest("url", "token", $eventConfig, "customerid", "key"); $this->assertTrue(!$result->doRedirect()); $this->assertTrue($result->eventId == 'e1'); $this->assertTrue($result->queueId == "queueId"); - $this->assertTrue($cookieProviderMock->expectCall('store', 1, array("e1", 'queueId', null, 'testDomain', "disabled", "key"))); + $this->assertTrue($cookieProviderMock->expectCall('store', 1, array("e1", 'queueId', null, 'testDomain', false, false, "disabled", "key"))); $this->assertTrue(strtolower($result->actionName) == strtolower('QueueAction')); } - + public function test_validateQueueRequest_ValidState_NoExtendableCookie_DoNotRedirectDoNotStoreCookieWithExtension() { $eventConfig = new QueueIT\KnownUserV3\SDK\QueueEventConfig(); $eventConfig->eventId = "e1"; @@ -151,12 +164,12 @@ public function test_validateQueueRequest_ValidState_NoExtendableCookie_DoNotRed $eventConfig->cookieValidityMinute = 10; $eventConfig->extendCookieValidity = true; $eventConfig->actionName = "QueueAction"; - + $cookieProviderMock = new UserInQueueStateRepositoryMockClass (); array_push($cookieProviderMock->arrayReturns['getState'], new QueueIT\KnownUserV3\SDK\StateInfo(true, true, "queueId", 3, "idle")); - + $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueService($cookieProviderMock); - + $result = $testObject->validateQueueRequest("url", "token", $eventConfig, "customerid", "key"); $this->assertTrue(!$result->doRedirect()); $this->assertTrue($result->eventId == 'e1'); @@ -164,7 +177,7 @@ public function test_validateQueueRequest_ValidState_NoExtendableCookie_DoNotRed $this->assertFalse($cookieProviderMock->expectCallAny('store', 1)); $this->assertTrue(strtolower($result->actionName) == strtolower('QueueAction')); } - + public function test_validateQueueRequest_NoCookie_TampredToken_RedirectToErrorPageWithHashError_DoNotStoreCookie() { $key = "4e1db821-a825-49da-acd0-5d376f2068db"; $eventConfig = new QueueIT\KnownUserV3\SDK\QueueEventConfig(); @@ -187,11 +200,11 @@ public function test_validateQueueRequest_NoCookie_TampredToken_RedirectToErrorP . "&man=" . rawurlencode($eventConfig->actionName) . "&queueittoken=" . $token . "&t=" . rawurlencode($url); - + $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueService($cookieProviderMock); $result = $testObject->validateQueueRequest($url, $token, $eventConfig, "testCustomer", $key); $this->assertFalse($cookieProviderMock->expectCallAny('store')); - + $this->assertTrue($result->doRedirect()); $this->assertTrue($result->eventId == 'e1'); $matches = array(); @@ -203,7 +216,7 @@ public function test_validateQueueRequest_NoCookie_TampredToken_RedirectToErrorP $this->assertTrue(strtolower($urlWithoutTimeStamp) == strtolower($expectedErrorUrl)); $this->assertTrue(strtolower($result->actionName) == strtolower($eventConfig->actionName)); } - + public function test_validateQueueRequest_NoCookie_ExpiredTimeStampInToken_RedirectToErrorPageWithTimeStampError_DoNotStoreCookie() { $key = "4e1db821-a825-49da-acd0-5d376f2068db"; $eventConfig = new QueueIT\KnownUserV3\SDK\QueueEventConfig(); @@ -217,18 +230,18 @@ public function test_validateQueueRequest_NoCookie_ExpiredTimeStampInToken_Redir $cookieProviderMock = new UserInQueueStateRepositoryMockClass (); array_push($cookieProviderMock->arrayReturns['getState'], new QueueIT\KnownUserV3\SDK\StateInfo(false, false, null, null, null)); $token = $this->generateHash('e1','queueId', strval(time() - (3 * 60)), 'False', null, 'queue', $key); - + $expectedErrorUrl = "https://testDomain.com/error/timestamp/?c=testCustomer&e=e1" . "&ver=".QueueIT\KnownUserV3\SDK\UserInQueueService::getSDKVersion() . "&cver=11" . "&man=" . rawurlencode($eventConfig->actionName) . "&queueittoken=" . $token . "&t=" . rawurlencode($url); - + $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueService($cookieProviderMock); $result = $testObject->validateQueueRequest($url, $token, $eventConfig, "testCustomer", $key); $this->assertFalse($cookieProviderMock->expectCallAny('store')); - + $this->assertTrue($result->doRedirect()); $this->assertTrue($result->eventId == 'e1'); $matches = array(); @@ -236,12 +249,12 @@ public function test_validateQueueRequest_NoCookie_ExpiredTimeStampInToken_Redir $timestamp = str_replace("&ts=", "", $matches[0]); $timestamp = str_replace("&", "", $timestamp); $this->assertTrue(time() - intval($timestamp) < 100); - + $urlWithoutTimeStamp = preg_replace("/&ts=[^&]*/", "", $result->redirectUrl); $this->assertTrue(strtolower($urlWithoutTimeStamp) == strtolower($expectedErrorUrl)); $this->assertTrue($result->actionName == $eventConfig->actionName); } - + public function test_validateQueueRequest_NoCookie_EventIdMismatch_RedirectToErrorPageWithEventIdMissMatchError_DoNotStoreCookie() { $key = "4e1db821-a825-49da-acd0-5d376f2068db"; $eventConfig = new QueueIT\KnownUserV3\SDK\QueueEventConfig(); @@ -255,18 +268,18 @@ public function test_validateQueueRequest_NoCookie_EventIdMismatch_RedirectToErr $cookieProviderMock = new UserInQueueStateRepositoryMockClass (); array_push($cookieProviderMock->arrayReturns['getState'], new QueueIT\KnownUserV3\SDK\StateInfo(false, false, null, null, null)); $token = $this->generateHash('e1', 'queueId',strval(time() - (3 * 60)), 'False', null, 'queue', $key); - + $expectedErrorUrl = "https://testDomain.com/error/eventid/?c=testCustomer&e=e2" . "&ver=".QueueIT\KnownUserV3\SDK\UserInQueueService::getSDKVersion() . "&cver=10" . "&man=" . rawurlencode($eventConfig->actionName) . "&queueittoken=" . $token . "&t=" . rawurlencode($url); - + $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueService($cookieProviderMock); $result = $testObject->validateQueueRequest($url, $token, $eventConfig, "testCustomer", $key); $this->assertFalse($cookieProviderMock->expectCallAny('store')); - + $this->assertTrue($result->doRedirect()); $this->assertTrue($result->eventId == 'e2'); $matches = array(); @@ -274,12 +287,12 @@ public function test_validateQueueRequest_NoCookie_EventIdMismatch_RedirectToErr $timestamp = str_replace("&ts=", "", $matches[0]); $timestamp = str_replace("&", "", $timestamp); $this->assertTrue(time() - intval($timestamp) < 100); - + $urlWithoutTimeStamp = preg_replace("/&ts=[^&]*/", "", $result->redirectUrl); $this->assertTrue(strtolower($urlWithoutTimeStamp) == strtolower($expectedErrorUrl)); $this->assertTrue($result->actionName == $eventConfig->actionName); } - + public function test_validateQueueRequest_NoCookie_ValidToken_ExtendableCookie_DoNotRedirect_StoreExtendableCookie() { $key = "4e1db821-a825-49da-acd0-5d376f2068db"; $eventConfig = new QueueIT\KnownUserV3\SDK\QueueEventConfig(); @@ -287,6 +300,8 @@ public function test_validateQueueRequest_NoCookie_ValidToken_ExtendableCookie_D $eventConfig->queueDomain = "testDomain.com"; $eventConfig->cookieValidityMinute = 10; $eventConfig->cookieDomain = "testDomain"; + $eventConfig->isCookieHttpOnly = false; + $eventConfig->isCookieSecure = false; $eventConfig->extendCookieValidity = true; $eventConfig->version = 11; //$eventConfig->actionName = "QueueAction"; @@ -300,10 +315,10 @@ public function test_validateQueueRequest_NoCookie_ValidToken_ExtendableCookie_D $this->assertTrue(!$result->doRedirect()); $this->assertTrue($result->eventId == 'e1'); $this->assertTrue($result->queueId == 'queueId'); - $this->assertTrue($result->redirectType == 'queue'); - $this->assertTrue($cookieProviderMock->expectCall('store', 1, array("e1",'queueId', null, 'testDomain', 'queue', $key))); + $this->assertTrue($result->redirectType == 'queue'); + $this->assertTrue($cookieProviderMock->expectCall('store', 1, array("e1",'queueId', null, 'testDomain', false, false, 'queue', $key))); } - + public function test_validateQueueRequest_NoCookie_ValidToken_CookieValidityMinuteFromToken_DoNotRedirect_StoreNonExtendableCookie() { $key = "4e1db821-a825-49da-acd0-5d376f2068db"; $eventConfig = new QueueIT\KnownUserV3\SDK\QueueEventConfig(); @@ -311,6 +326,8 @@ public function test_validateQueueRequest_NoCookie_ValidToken_CookieValidityMinu $eventConfig->queueDomain = "testDomain.com"; $eventConfig->cookieValidityMinute = 30; $eventConfig->cookieDomain = "testDomain"; + $eventConfig->isCookieHttpOnly = false; + $eventConfig->isCookieSecure = false; $eventConfig->extendCookieValidity = true; $eventConfig->version = 11; //$eventConfig->actionName = "QueueAction"; @@ -318,16 +335,16 @@ public function test_validateQueueRequest_NoCookie_ValidToken_CookieValidityMinu $cookieProviderMock = new UserInQueueStateRepositoryMockClass (); array_push($cookieProviderMock->arrayReturns['getState'], new QueueIT\KnownUserV3\SDK\StateInfo(false, false, null, null, null)); $token = $this->generateHash('e1', 'queueId',strval(time() + (3 * 60)), 'false', 3, 'DirectLink', $key); - + $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueService($cookieProviderMock); $result = $testObject->validateQueueRequest($url, $token, $eventConfig, "testCustomer", $key); $this->assertTrue(!$result->doRedirect()); $this->assertTrue($result->eventId == 'e1'); $this->assertTrue($result->queueId == 'queueId'); - $this->assertTrue($result->redirectType == 'DirectLink'); - $this->assertTrue($cookieProviderMock->expectCall('store', 1, array("e1",'queueId', 3, 'testDomain', 'DirectLink', $key))); + $this->assertTrue($result->redirectType == 'DirectLink'); + $this->assertTrue($cookieProviderMock->expectCall('store', 1, array("e1",'queueId', 3, 'testDomain', false, false, 'DirectLink', $key))); } - + public function test_NoCookie_NoValidToken_WithoutToken_RedirectToQueue() { $key = "4e1db821-a825-49da-acd0-5d376f2068db"; $eventConfig = new QueueIT\KnownUserV3\SDK\QueueEventConfig(); @@ -339,12 +356,12 @@ public function test_NoCookie_NoValidToken_WithoutToken_RedirectToQueue() { $eventConfig->actionName = "Queue Action (._~-) !*|'\""; $eventConfig->culture = 'en-US'; $eventConfig->layoutName = 'testlayout'; - + $url = "http://test.test.com?b=h"; $cookieProviderMock = new UserInQueueStateRepositoryMockClass (); array_push($cookieProviderMock->arrayReturns['getState'], new QueueIT\KnownUserV3\SDK\StateInfo(false, false, null, null, null)); $token = ""; - + $expectedRedirectUrl = "https://testDomain.com/?c=testCustomer&e=e1" . "&ver=".QueueIT\KnownUserV3\SDK\UserInQueueService::getSDKVersion() . "&cver=11" @@ -352,19 +369,19 @@ public function test_NoCookie_NoValidToken_WithoutToken_RedirectToQueue() { . "&cid=en-US" . "&l=testlayout" . "&t=" . rawurlencode($url); - + $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueService($cookieProviderMock); $result = $testObject->validateQueueRequest($url, $token, $eventConfig, "testCustomer", $key); $this->assertFalse($cookieProviderMock->expectCallAny('store')); - + $this->assertTrue($result->doRedirect()); $this->assertTrue($result->eventId == 'e1'); $this->assertTrue($result->queueId == null); $this->assertTrue(strtolower($result->redirectUrl) == strtolower($expectedRedirectUrl)); } - - public function test_ValidateRequest_NoCookie_WithoutToken_RedirectToQueue_NotargetUrl() { - $key = "4e1db821-a825-49da-acd0-5d376f2068db"; + + public function test_ValidateRequest_NoCookie_WithoutToken_RedirectToQueue_NotargetUrl() { + $key = "4e1db821-a825-49da-acd0-5d376f2068db"; $eventConfig = new QueueIT\KnownUserV3\SDK\QueueEventConfig(); $eventConfig->eventId = "e1"; $eventConfig->queueDomain = "testDomain.com"; @@ -374,31 +391,31 @@ public function test_ValidateRequest_NoCookie_WithoutToken_RedirectToQueue_Notar $eventConfig->actionName = "Queue Action (._~-) !*|'\""; $eventConfig->culture = null; $eventConfig->layoutName = 'testlayout'; - + $url = "http://test.test.com?b=h"; $cookieProviderMock = new UserInQueueStateRepositoryMockClass (); array_push($cookieProviderMock->arrayReturns['getState'], new QueueIT\KnownUserV3\SDK\StateInfo(false, false, null, null, null)); $token = ""; - + $expectedRedirectUrl = "https://testDomain.com/?c=testCustomer&e=e1" . "&ver=".QueueIT\KnownUserV3\SDK\UserInQueueService::getSDKVersion() . "&cver=10" . "&man=" . rawurlencode($eventConfig->actionName) . "&l=testlayout"; - + $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueService($cookieProviderMock); $result = $testObject->validateQueueRequest(null, $token, $eventConfig, "testCustomer", $key); $this->assertFalse($cookieProviderMock->expectCallAny('store')); - + $this->assertTrue($result->doRedirect()); $this->assertTrue($result->eventId == 'e1'); $this->assertTrue($result->queueId == null); $this->assertTrue(strtolower($result->redirectUrl) == strtolower($expectedRedirectUrl)); $this->assertTrue(strtolower($result->actionName) == strtolower( $eventConfig->actionName)); - } + } public function test_ValidateRequest_InvalidCookie_WithoutToken_RedirectToQueue_CancelCookie() { - $key = "4e1db821-a825-49da-acd0-5d376f2068db"; + $key = "4e1db821-a825-49da-acd0-5d376f2068db"; $eventConfig = new QueueIT\KnownUserV3\SDK\QueueEventConfig(); $eventConfig->eventId = "e1"; $eventConfig->queueDomain = "testDomain.com"; @@ -408,29 +425,29 @@ public function test_ValidateRequest_InvalidCookie_WithoutToken_RedirectToQueue_ $eventConfig->actionName = "Queue Action (._~-) !*|'\""; $eventConfig->culture = null; $eventConfig->layoutName = 'testlayout'; - + $url = "http://test.test.com?b=h"; $cookieProviderMock = new UserInQueueStateRepositoryMockClass (); array_push($cookieProviderMock->arrayReturns['getState'], new QueueIT\KnownUserV3\SDK\StateInfo(true, false, null, null, null)); $token = ""; - + $expectedRedirectUrl = "https://testDomain.com/?c=testCustomer&e=e1" . "&ver=".QueueIT\KnownUserV3\SDK\UserInQueueService::getSDKVersion() . "&cver=10" . "&man=" . rawurlencode($eventConfig->actionName) . "&l=testlayout"; - + $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueService($cookieProviderMock); $result = $testObject->validateQueueRequest(null, $token, $eventConfig, "testCustomer", $key); $this->assertFalse($cookieProviderMock->expectCallAny('store')); - + $this->assertTrue($result->doRedirect()); $this->assertTrue($result->eventId == 'e1'); $this->assertTrue($result->queueId == null); $this->assertTrue(strtolower($result->redirectUrl) == strtolower($expectedRedirectUrl)); $this->assertTrue(strtolower($result->actionName) == strtolower( $eventConfig->actionName)); $this->assertTrue($cookieProviderMock->expectCallAny('cancelQueueCookie')); - } + } public function test_validateQueueRequest_NoCookie_InValidToken() { $key = "4e1db821-a825-49da-acd0-5d376f2068db"; @@ -443,16 +460,16 @@ public function test_validateQueueRequest_NoCookie_InValidToken() { $eventConfig->actionName = "QueueAction"; $eventConfig->culture = 'en-US'; $eventConfig->layoutName = 'testlayout'; - + $url = "http://test.test.com?b=h"; $cookieProviderMock = new UserInQueueStateRepositoryMockClass (); array_push($cookieProviderMock->arrayReturns['getState'], new QueueIT\KnownUserV3\SDK\StateInfo(false, false, null, null, null)); $token = ""; - + $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueService($cookieProviderMock); $result = $testObject->validateQueueRequest($url, "ts_sasa~cv_adsasa~ce_falwwwse~q_944c1f44-60dd-4e37-aabc-f3e4bb1c8895", $eventConfig, "testCustomer", $key); $this->assertFalse($cookieProviderMock->expectCallAny('store')); - + $this->assertTrue($result->doRedirect()); $this->assertTrue($result->eventId == 'e1'); $this->assertTrue($result->queueId == null); @@ -471,16 +488,16 @@ public function test_validateQueueRequest_InvalidCookie_InvalidToken_CancelCooki $eventConfig->actionName = "QueueAction"; $eventConfig->culture = 'en-US'; $eventConfig->layoutName = 'testlayout'; - + $url = "http://test.test.com?b=h"; $cookieProviderMock = new UserInQueueStateRepositoryMockClass (); array_push($cookieProviderMock->arrayReturns['getState'], new QueueIT\KnownUserV3\SDK\StateInfo(true, false, null, null, null)); $token = ""; - + $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueService($cookieProviderMock); $result = $testObject->validateQueueRequest($url, "ts_sasa~cv_adsasa~ce_falwwwse~q_944c1f44-60dd-4e37-aabc-f3e4bb1c8895", $eventConfig, "testCustomer", $key); $this->assertFalse($cookieProviderMock->expectCallAny('store')); - + $this->assertTrue($result->doRedirect()); $this->assertTrue($result->eventId == 'e1'); $this->assertTrue($result->queueId == null); @@ -501,20 +518,22 @@ public function test_validateCancelRequest() { $cookieProviderMock = new UserInQueueStateRepositoryMockClass (); array_push($cookieProviderMock->arrayReturns['getState'], new QueueIT\KnownUserV3\SDK\StateInfo(true, true, "queueid", 3, "idle")); $token = ""; - + $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueService($cookieProviderMock); - $expectedUrl = "https://testDomain.com/cancel/testCustomer/e1/?c=testCustomer&e=e1" + $expectedUrl = "https://testDomain.com/cancel/testCustomer/e1/queueid" + ."?c=testCustomer" + ."&e=e1" ."&ver=".QueueIT\KnownUserV3\SDK\UserInQueueService::getSDKVersion() ."&cver=10" ."&man=" . $eventConfig->actionName ."&r=http%3A%2F%2Ftest.test.com%3Fb%3Dh"; $result = $testObject->validateCancelRequest($url, $eventConfig, "testCustomer", $key); $this->assertFalse($cookieProviderMock->expectCallAny('store')); - + $this->assertTrue($result->doRedirect()); $this->assertTrue($result->eventId == 'e1'); $this->assertTrue($result->queueId == "queueid"); - + $this->assertTrue($result->redirectUrl == $expectedUrl); $this->assertTrue(strtolower($result->actionName) == strtolower($eventConfig->actionName)); } @@ -523,7 +542,7 @@ public function test_getIgnoreActionResult() { $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueService(new UserInQueueStateRepositoryMockClass ()); $actionName = "IgnoreAction"; $result = $testObject->getIgnoreActionResult($actionName); - + $this->assertFalse($result->doRedirect()); $this->assertTrue($result->eventId == NULL); $this->assertTrue($result->queueId == NULL); @@ -538,6 +557,6 @@ public function generateHash($eventId, $queueId, $timestamp, $extendableCookie, $token = $token . '~cv_' . $cookieValidityMinutes; if (isset($redirectType)) $token = $token . '~rt_' . $redirectType; - return $token . '~h_' . hash_hmac('sha256', $token, $secretKey); + return $token . '~h_' . hash_hmac('sha256', $token, $secretKey); } } \ No newline at end of file diff --git a/Tests/UserInQueueStateCookieRepositoryTest.php b/Tests/UserInQueueStateCookieRepositoryTest.php index 2320012..b7f0162 100644 --- a/Tests/UserInQueueStateCookieRepositoryTest.php +++ b/Tests/UserInQueueStateCookieRepositoryTest.php @@ -17,17 +17,22 @@ function __construct() { $this->getCookieCalls = array(); } - public function setCookie($cookieName, $value, $expire, $domain) { + public function setCookie($cookieName, $value, $expire, $domain, $isHttpOnly, $isSecure) { $this->cookieList[$cookieName] = array( "name" => $cookieName, "value" => $value, "expiration" => $expire, - "cookieDomain" => $domain + "cookieDomain" => $domain, + "isHttpOnly" => $isHttpOnly, + "isSecure" => $isSecure ); - $this->setCookieCalls[count($this->setCookieCalls)] = array("name" => $cookieName, + $this->setCookieCalls[count($this->setCookieCalls)] = array( + "name" => $cookieName, "value" => $value, "expiration" => $expire, - "cookieDomain" => $domain); + "cookieDomain" => $domain, + "isHttpOnly" => $isHttpOnly, + "isSecure" => $isSecure); } public function getCookie($cookieName) { @@ -46,302 +51,362 @@ public function getCookieArray() class UserInQueueStateCookieRepositoryTest extends UnitTestCase { - private function generateHash($eventId, $queueId, $fixedCookieValidityMinutes, $redirectType, $issueTime, $secretKey) { - return hash_hmac('sha256', $eventId . $queueId . $fixedCookieValidityMinutes . $redirectType . $issueTime, $secretKey); - } + private function generateHash($eventId, $queueId, $fixedCookieValidityMinutes, $redirectType, $issueTime, $secretKey) { + return hash_hmac('sha256', $eventId . $queueId . $fixedCookieValidityMinutes . $redirectType . $issueTime, $secretKey); + } public function test_store_hasValidState_ExtendableCookie_CookieIsSaved() { $eventId = "event1"; $secretKey = "4e1deweb821-a82ew5-49da-acdqq0-5d3476f2068db"; $cookieDomain = ".test.com"; + $isCookieHttpOnly = true; + $isCookieSecure = true; $queueId = "queueId"; $cookieValidity = 10; $cookieKey = QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository::getCookieKey($eventId); - + $cookieManager = new UserInQueueStateCookieManagerMock(); $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository($cookieManager); - - $testObject->store($eventId, $queueId, null, $cookieDomain, "Queue", $secretKey); + + $testObject->store($eventId, $queueId, null, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, "Queue", $secretKey); $state = $testObject->getState($eventId, $cookieValidity, $secretKey, true); - + $this->assertTrue($state->isValid); $this->assertTrue($state->queueId == $queueId); $this->assertTrue($state->isStateExtendable()); - $this->assertTrue($state->redirectType === "Queue"); + $this->assertTrue($state->redirectType === "Queue"); $this->assertTrue(abs(intval($cookieManager->cookieList[$cookieKey]["expiration"]) - time() - 24 * 60 * 60) < 100); $this->assertTrue($cookieManager->cookieList[$cookieKey]["cookieDomain"] == $cookieDomain); + $this->assertEqual($isCookieHttpOnly, $cookieManager->cookieList[$cookieKey]["isHttpOnly"]); + $this->assertEqual($isCookieSecure, $cookieManager->cookieList[$cookieKey]["isSecure"]); } - - public function test_store_hasValidState_nonExtendableCookie_CookieIsSaved() { - $eventId = "event1"; + + public function test_store_hasValidState_nonExtendableCookie_CookieIsSaved() { + $eventId = "event1"; $secretKey = "4e1deweb821-a82ew5-49da-acdqq0-5d3476f2068db"; $cookieDomain = ".test.com"; + $isCookieHttpOnly = true; + $isCookieSecure = true; $queueId = "queueId"; $cookieValidity = 3; $cookieKey = QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository::getCookieKey($eventId); - + $cookieManager = new UserInQueueStateCookieManagerMock(); $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository($cookieManager); - - $testObject->store($eventId, $queueId, $cookieValidity, $cookieDomain, "Idle", $secretKey); + + $testObject->store($eventId, $queueId, $cookieValidity, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, "Idle", $secretKey); $state = $testObject->getState($eventId, $cookieValidity, $secretKey, true); - + $this->assertTrue($state->isValid); $this->assertTrue($state->queueId == $queueId); $this->assertFalse($state->isStateExtendable()); - $this->assertTrue($state->redirectType === "Idle"); - $this->assertTrue($state->fixedCookieValidityMinutes === 3); + $this->assertTrue($state->redirectType === "Idle"); + $this->assertTrue($state->fixedCookieValidityMinutes === 3); $this->assertTrue(abs(intval($cookieManager->cookieList[$cookieKey]["expiration"]) - time() - 24 * 60 * 60) < 100); $this->assertTrue($cookieManager->cookieList[$cookieKey]["cookieDomain"] == $cookieDomain); - } + $this->assertEqual($isCookieHttpOnly, $cookieManager->cookieList[$cookieKey]["isHttpOnly"]); + $this->assertEqual($isCookieSecure, $cookieManager->cookieList[$cookieKey]["isSecure"]); + } public function test_store_hasValidState_tamperedCookie_stateIsNotValid_isCookieExtendable() { $eventId = "event1"; $secretKey = "4e1deweb821-a82ew5-49da-acdqq0-5d3476f2068db"; $cookieDomain = ".test.com"; + $isCookieHttpOnly = false; + $isCookieSecure = false; $queueId = "queueId"; $cookieValidity = 10; $cookieKey = QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository::getCookieKey($eventId); - + $cookieManager = new UserInQueueStateCookieManagerMock(); $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository($cookieManager); - - $testObject->store($eventId, $queueId, 3, $cookieDomain, "Idle", $secretKey); + + $testObject->store($eventId, $queueId, 3, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, "Idle", $secretKey); $state = $testObject->getState($eventId, $cookieValidity, $secretKey, true); $this->assertTrue($state->isValid); - + $oldCookieValue = $cookieManager->cookieList[$cookieKey]["value"]; $cookieManager->cookieList[$cookieKey]["value"] = str_replace("FixedValidityMins=3", "FixedValidityMins=10", $oldCookieValue); $state2 = $testObject->getState($eventId, $cookieValidity, $secretKey, true); $this->assertFalse($state2->isValid); - $this->assertFalse($state->isStateExtendable()); + $this->assertFalse($state->isStateExtendable()); } - public function test_store_hasValidState_tamperedCookie_stateIsNotValid_eventId() { - $eventId = "event1"; + public function test_store_hasValidState_tamperedCookie_stateIsNotValid_eventId() { + $eventId = "event1"; $secretKey = "4e1deweb821-a82ew5-49da-acdqq0-5d3476f2068db"; $cookieDomain = ".test.com"; + $isCookieHttpOnly = false; + $isCookieSecure = false; $queueId = "queueId"; $cookieValidity = 10; $cookieKey = QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository::getCookieKey($eventId); - + $cookieManager = new UserInQueueStateCookieManagerMock(); $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository($cookieManager); - - $testObject->store($eventId, $queueId, 3, $cookieDomain, "Idle", $secretKey); + + $testObject->store($eventId, $queueId, 3, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, "Idle", $secretKey); $state = $testObject->getState($eventId, $cookieValidity, $secretKey, true); $this->assertTrue($state->isValid); - + $oldCookieValue = $cookieManager->cookieList[$cookieKey]["value"]; $cookieManager->cookieList[$cookieKey]["value"] = str_replace("EventId=event1", "EventId=event2", $oldCookieValue); $state2 = $testObject->getState($eventId, $cookieValidity, $secretKey, true); $this->assertFalse($state2->isValid); - $this->assertFalse($state->isStateExtendable()); - } - + $this->assertFalse($state->isStateExtendable()); + } + public function test_store_hasValidState_expiredCookie_stateIsNotValid() { $eventId = "event1"; $secretKey = "4e1deweb821-a82ew5-49da-acdqq0-5d3476f2068db"; $cookieDomain = ".test.com"; + $isCookieHttpOnly = false; + $isCookieSecure = false; $queueId = "queueId"; - $cookieValidity = -1; + $cookieValidity = -1; $cookieManager = new UserInQueueStateCookieManagerMock(); $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository($cookieManager); - - $testObject->store($eventId, $queueId, null, $cookieDomain, "Idle", $secretKey); + + $testObject->store($eventId, $queueId, null, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, "Idle", $secretKey); $state = $testObject->getState($eventId, $cookieValidity, $secretKey, true); $this->assertFalse($state->isValid); } - + public function test_store_hasValidState_differentEventId_stateIsNotValid() { $eventId = "event1"; $secretKey = "4e1deweb821-a82ew5-49da-acdqq0-5d3476f2068db"; $cookieDomain = ".test.com"; + $isCookieHttpOnly = false; + $isCookieSecure = false; $queueId = "queueId"; - $cookieValidity = 10; + $cookieValidity = 10; $cookieManager = new UserInQueueStateCookieManagerMock(); $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository($cookieManager); - - $testObject->store($eventId, $queueId, null, $cookieDomain, "Queue", $secretKey); + + $testObject->store($eventId, $queueId, null, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, "Queue", $secretKey); $state = $testObject->getState($eventId, $cookieValidity, $secretKey, true); $this->assertTrue($state->isValid); - + $state2 = $testObject->getState("event2", $cookieValidity, $secretKey, true); $this->assertTrue(!$state2->isValid); } - - public function test_hasValidState_noCookie_stateIsNotValid() { - $eventId = "event1"; + + public function test_hasValidState_noCookie_stateIsNotValid() { + $eventId = "event1"; $secretKey = "4e1deweb821-a82ew5-49da-acdqq0-5d3476f2068db"; $cookieDomain = ".test.com"; $queueId = "queueId"; $cookieKey = "key"; - $cookieValidity = 10; + $cookieValidity = 10; $cookieManager = new UserInQueueStateCookieManagerMock(); $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository($cookieManager); - + $state = $testObject->getState($eventId, $cookieValidity, $secretKey, true); $this->assertFalse($state->isValid); - } + } public function test_hasValidState_invalidCookie_stateIsNotValid() { $eventId = "event1"; $secretKey = "4e1deweb821-a82ew5-49da-acdqq0-5d3476f2068db"; $cookieDomain = ".test.com"; + $isCookieHttpOnly = false; + $isCookieSecure = false; $queueId = "queueId"; $cookieKey = QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository::getCookieKey($eventId); - $cookieValidity = 10; + $cookieValidity = 10; $cookieManager = new UserInQueueStateCookieManagerMock(); $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository($cookieManager); - - $testObject->store($eventId, $queueId, 20, $cookieDomain, "Queue", $secretKey); + + $testObject->store($eventId, $queueId, 20, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, "Queue", $secretKey); $state = $testObject->getState($eventId, $cookieValidity, $secretKey, true); $this->assertTrue($state->isValid); - + $cookieManager->cookieList[$cookieKey]["value"] = "IsCookieExtendable=ooOOO&Expires=|||&QueueId=000&Hash=23232$$$"; $state2 = $testObject->getState($eventId, $cookieValidity, $secretKey, true); $this->assertFalse($state2->isValid); } - + public function test_cancelQueueCookie() { $eventId = "event1"; $secretKey = "4e1deweb821-a82ew5-49da-acdqq0-5d3476f2068db"; $cookieDomain = ".test.com"; + $isCookieHttpOnly = false; + $isCookieSecure = false; $queueId = "queueId"; - $cookieValidity = 20; + $cookieValidity = 20; $cookieManager = new UserInQueueStateCookieManagerMock(); $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository($cookieManager); - $testObject->store($eventId, $queueId, 20, $cookieDomain, "Queue", $secretKey); + $testObject->store($eventId, $queueId, 20, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, "Queue", $secretKey); $state = $testObject->getState($eventId, $cookieValidity, $secretKey, true); $this->assertTrue($state->isValid); - - $testObject->cancelQueueCookie($eventId, $cookieDomain); + + $testObject->cancelQueueCookie($eventId, $cookieDomain, $isCookieHttpOnly, $isCookieSecure); $state2 = $testObject->getState($eventId, $cookieValidity, $secretKey, true); $this->assertTrue(!$state2->isValid); - + $this->assertTrue(intval($cookieManager->setCookieCalls[1]["expiration"]) == -1); $this->assertTrue($cookieManager->setCookieCalls[1]["cookieDomain"] == $cookieDomain); $this->assertTrue($cookieManager->setCookieCalls[1]["value"] == null); } - + public function test_extendQueueCookie_cookieExist() { $eventId = "event1"; $secretKey = "4e1deweb821-a82ew5-49da-acdqq0-5d3476f2068db"; $cookieDomain = ".test.com"; + $isCookieHttpOnly = true; + $isCookieSecure = true; $queueId = "queueId"; $cookieKey = QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository::getCookieKey($eventId); - + $cookieManager = new UserInQueueStateCookieManagerMock(); $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository($cookieManager); - $testObject->store($eventId, $queueId, null, $cookieDomain, "Queue", $secretKey); - $testObject->reissueQueueCookie($eventId, 12, $cookieDomain, $secretKey); - + $testObject->store($eventId, $queueId, null, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, "Queue", $secretKey); + $testObject->reissueQueueCookie($eventId, 12, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, $secretKey); + $state = $testObject->getState($eventId, 5, $secretKey, true); $this->assertTrue($state->isValid); $this->assertTrue($state->queueId == $queueId); $this->assertTrue($state->isStateExtendable()); $this->assertTrue(abs(intval($cookieManager->cookieList[$cookieKey]["expiration"]) - time() - 24 * 60 * 60) < 100); $this->assertTrue($cookieManager->cookieList[$cookieKey]["cookieDomain"] == $cookieDomain); + $this->assertTrue($cookieManager->cookieList[$cookieKey]["isHttpOnly"] == $isCookieHttpOnly); + $this->assertTrue($cookieManager->cookieList[$cookieKey]["isSecure"] == $isCookieSecure); } - public function test_extendQueueCookie_cookieDoesNotExist() { + public function test_extendQueueCookie_cookieDoesNotExist() { $eventId = "event1"; $secretKey = "4e1deweb821-a82ew5-49da-acdqq0-5d3476f2068db"; $cookieDomain = ".test.com"; + $isCookieHttpOnly = false; + $isCookieSecure = false; $queueId = "queueId"; - + $cookieManager = new UserInQueueStateCookieManagerMock(); $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository($cookieManager); - $testObject->store("event2", $queueId, 20, $cookieDomain, "Queue", $secretKey); - $testObject->reissueQueueCookie($eventId, 12, $cookieDomain, $secretKey); + $testObject->store("event2", $queueId, 20, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, "Queue", $secretKey); + $testObject->reissueQueueCookie($eventId, 12, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, $secretKey); $this->assertTrue(count($cookieManager->setCookieCalls) == 1); } - public function test_getState_validCookieFormat_extendable() { - $eventId = "event1"; + public function test_getState_validCookieFormat_extendable() { + $eventId = "event1"; $secretKey = "4e1deweb821-a82ew5-49da-acdqq0-5d3476f2068db"; $cookieDomain = ".test.com"; + $isCookieHttpOnly = false; + $isCookieSecure = false; $queueId = "queueId"; $cookieKey = QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository::getCookieKey($eventId); - $issueTime = time(); - $hash = $this->generateHash($eventId, $queueId, null, "queue", $issueTime, $secretKey); + $issueTime = time(); + $hash = $this->generateHash($eventId, $queueId, null, "queue", $issueTime, $secretKey); - $cookieManager = new UserInQueueStateCookieManagerMock(); + $cookieManager = new UserInQueueStateCookieManagerMock(); $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository($cookieManager); - - $cookieManager->setCookie($cookieKey, "EventId=".$eventId."&QueueId=".$queueId."&RedirectType=queue&IssueTime=".$issueTime."&Hash=".$hash, time() + (24*60*60), $cookieDomain); - $state = $testObject->getState($eventId, 10, $secretKey, true); + + $cookieValue = "EventId=" . $eventId . + "&QueueId=" . $queueId . + "&RedirectType=queue" . + "&IssueTime=". $issueTime . + "&Hash=" . $hash; + + $cookieManager->setCookie($cookieKey, $cookieValue, time() + (24*60*60), $cookieDomain, $isCookieHttpOnly, $isCookieSecure); + $state = $testObject->getState($eventId, 10, $secretKey, true); $this->assertTrue($state->isStateExtendable()); $this->assertTrue($state->isValid); $this->assertTrue($state->isFound); $this->assertTrue($state->queueId == $queueId); - $this->assertTrue($state->redirectType == "queue"); - } + $this->assertTrue($state->redirectType == "queue"); + } - public function test_getState_oldCookie_invalid_expiredCookie_extendable() { - $eventId = "event1"; + public function test_getState_oldCookie_invalid_expiredCookie_extendable() { + $eventId = "event1"; $secretKey = "4e1deweb821-a82ew5-49da-acdqq0-5d3476f2068db"; $cookieDomain = ".test.com"; + $isCookieHttpOnly = false; + $isCookieSecure = false; $queueId = "queueId"; $cookieKey = QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository::getCookieKey($eventId); - $issueTime = time() - (11*60); - $hash = $this->generateHash($eventId, $queueId, null, "queue", $issueTime, $secretKey); + $issueTime = time() - (11*60); + $hash = $this->generateHash($eventId, $queueId, null, "queue", $issueTime, $secretKey); - $cookieManager = new UserInQueueStateCookieManagerMock(); + $cookieManager = new UserInQueueStateCookieManagerMock(); $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository($cookieManager); - - $cookieManager->setCookie($cookieKey, "EventId=".$eventId."&QueueId=".$queueId."&RedirectType=queue&IssueTime=".$issueTime."&Hash=".$hash, time() + (24*60*60), $cookieDomain); - $state = $testObject->getState($eventId, 10, $secretKey, true); + + $cookieValue = "EventId=" . $eventId . + "&QueueId=" . $queueId . + "&RedirectType=queue" . + "&IssueTime=" . $issueTime . + "&Hash=".$hash; + + $cookieManager->setCookie($cookieKey, $cookieValue, time() + (24*60*60), $cookieDomain, $isCookieHttpOnly, $isCookieSecure); + $state = $testObject->getState($eventId, 10, $secretKey, true); $this->assertFalse($state->isValid); $this->assertTrue($state->isFound); - } - - public function test_getState_oldCookie_invalid_expiredCookie_nonExtendable() { - $eventId = "event1"; + } + + public function test_getState_oldCookie_invalid_expiredCookie_nonExtendable() { + $eventId = "event1"; $secretKey = "4e1deweb821-a82ew5-49da-acdqq0-5d3476f2068db"; $cookieDomain = ".test.com"; + $isCookieHttpOnly = false; + $isCookieSecure = false; $queueId = "queueId"; $cookieKey = QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository::getCookieKey($eventId); - $issueTime = time() - (4*60); - $hash = $this->generateHash($eventId, $queueId, 3, "idle", $issueTime, $secretKey); + $issueTime = time() - (4*60); + $hash = $this->generateHash($eventId, $queueId, 3, "idle", $issueTime, $secretKey); - $cookieManager = new UserInQueueStateCookieManagerMock(); + $cookieManager = new UserInQueueStateCookieManagerMock(); $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository($cookieManager); - - $cookieManager->setCookie($cookieKey, "EventId=".$eventId."&QueueId=".$queueId."&FixedValidityMins=3&RedirectType=idle&IssueTime=".$issueTime."&Hash=".$hash, time() + (24*60*60), $cookieDomain); - $state = $testObject->getState($eventId, 10, $secretKey, true); + + $cookieValue = "EventId=" . $eventId . + "&QueueId=" . $queueId . + "&FixedValidityMins=3". + "&RedirectType=idle" . + "&IssueTime=" . $issueTime . + "&Hash=" . $hash; + + $cookieManager->setCookie($cookieKey, $cookieValue, time() + (24*60*60), $cookieDomain, $isCookieHttpOnly, $isCookieSecure); + $state = $testObject->getState($eventId, 10, $secretKey, true); $this->assertFalse($state->isValid); $this->assertTrue($state->isFound); - } + } - public function test_getState_validCookieFormat_nonExtendable() { - $eventId = "event1"; + public function test_getState_validCookieFormat_nonExtendable() { + $eventId = "event1"; $secretKey = "4e1deweb821-a82ew5-49da-acdqq0-5d3476f2068db"; $cookieDomain = ".test.com"; + $isCookieHttpOnly = false; + $isCookieSecure = false; $queueId = "queueId"; $cookieKey = QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository::getCookieKey($eventId); - $issueTime = time(); - $hash = $this->generateHash($eventId, $queueId, 3, "idle", $issueTime, $secretKey); + $issueTime = time(); + $hash = $this->generateHash($eventId, $queueId, 3, "idle", $issueTime, $secretKey); - $cookieManager = new UserInQueueStateCookieManagerMock(); + $cookieManager = new UserInQueueStateCookieManagerMock(); $testObject = new QueueIT\KnownUserV3\SDK\UserInQueueStateCookieRepository($cookieManager); - - $cookieManager->setCookie($cookieKey, "EventId=".$eventId."&QueueId=".$queueId."&FixedValidityMins=3&RedirectType=idle&IssueTime=".$issueTime."&Hash=".$hash, time() + (24*60*60), $cookieDomain); - $state = $testObject->getState($eventId, 10, $secretKey, true); + + $cookieValue = "EventId=" . $eventId . + "&QueueId=" . $queueId . + "&FixedValidityMins=3" . + "&RedirectType=idle" . + "&IssueTime=" . $issueTime . + "&Hash=" . $hash; + + $cookieManager->setCookie($cookieKey, $cookieValue, time() + (24*60*60), $cookieDomain, $isCookieHttpOnly, $isCookieSecure); + $state = $testObject->getState($eventId, 10, $secretKey, true); - $this->assertFalse($state->isStateExtendable()); + $this->assertFalse($state->isStateExtendable()); $this->assertTrue($state->isValid); $this->assertTrue($state->isFound); $this->assertTrue($state->queueId == $queueId); - $this->assertTrue($state->redirectType == "idle"); + $this->assertTrue($state->redirectType == "idle"); } public function test_getState_NoCookie() { diff --git a/UserInQueueService.php b/UserInQueueService.php index 20e7ad3..8579e52 100644 --- a/UserInQueueService.php +++ b/UserInQueueService.php @@ -27,6 +27,8 @@ public function extendQueueCookie( $eventId, $cookieValidityMinutes, $cookieDomain, + $isCookieHttpOnly, + $isCookieSecure, $secretKey ); @@ -39,7 +41,7 @@ class UserInQueueService implements IUserInQueueService { public static function getSDKVersion() { - return "v3-php-" . "3.6.1"; + return "v3-php-" . "3.7.0"; } private $userInQueueStateRepository; @@ -65,6 +67,8 @@ public function validateQueueRequest( $state->queueId, null, !Utils::isNullOrEmptyString($config->cookieDomain) ? $config->cookieDomain : '', + $config->isCookieHttpOnly, + $config->isCookieSecure, $state->redirectType, $secretKey ); @@ -101,7 +105,11 @@ public function validateQueueRequest( if ($state->isFound && !$isTokenValid) { - $this->userInQueueStateRepository->cancelQueueCookie($config->eventId, $config->cookieDomain); + $this->userInQueueStateRepository->cancelQueueCookie( + $config->eventId, + $config->cookieDomain, + $config->isCookieHttpOnly, + $config->isCookieSecure); } return $requestValidationResult; @@ -117,6 +125,8 @@ private function getValidTokenResult( $queueParams->queueId, $queueParams->cookieValidityMinutes, !Utils::isNullOrEmptyString($config->cookieDomain) ? $config->cookieDomain : '', + $config->isCookieHttpOnly, + $config->isCookieSecure, $queueParams->redirectType, $secretKey ); @@ -221,9 +231,11 @@ public function extendQueueCookie( $eventId, $cookieValidityMinutes, $cookieDomain, + $isCookieHttpOnly, + $isCookieSecure, $secretKey ) { - $this->userInQueueStateRepository->reissueQueueCookie($eventId, $cookieValidityMinutes, $cookieDomain, $secretKey); + $this->userInQueueStateRepository->reissueQueueCookie($eventId, $cookieValidityMinutes, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, $secretKey); } public function validateCancelRequest($targetUrl, CancelEventConfig $cancelConfig, $customerId, $secretKey) @@ -232,13 +244,19 @@ public function validateCancelRequest($targetUrl, CancelEventConfig $cancelConfi $state = $this->userInQueueStateRepository->getState($cancelConfig->eventId, -1, $secretKey, false); if ($state->isValid) { - $this->userInQueueStateRepository->cancelQueueCookie($cancelConfig->eventId, $cancelConfig->cookieDomain); + $this->userInQueueStateRepository->cancelQueueCookie( + $cancelConfig->eventId, + $cancelConfig->cookieDomain, + $cancelConfig->isCookieHttpOnly, + $cancelConfig->isCookieSecure); $query = $this->getQueryString($customerId, $cancelConfig->eventId, $cancelConfig->version, null, null, $cancelConfig->actionName) . (!Utils::isNullOrEmptyString($targetUrl) ? ("&r=" . rawurlencode($targetUrl)) : ""); - - $uriPath = "cancel/" . $customerId . "/" . $cancelConfig->eventId . "/"; + $uriPath = "cancel/" . $customerId . "/" . $cancelConfig->eventId; + if(!Utils::isNullOrEmptyString($state->queueId)) { + $uriPath = $uriPath . "/" . $state->queueId; + } $redirectUrl = $this->generateRedirectUrl($cancelConfig->queueDomain, $uriPath, $query); diff --git a/UserInQueueStateCookieRepository.php b/UserInQueueStateCookieRepository.php index 042bfa3..a8a1d61 100644 --- a/UserInQueueStateCookieRepository.php +++ b/UserInQueueStateCookieRepository.php @@ -5,15 +5,15 @@ interface IUserInQueueStateRepository { - public function store($eventId, $queueId, $fixedCookieValidityMinutes, $cookieDomain, $redirectType, $secretKey); + public function store($eventId, $queueId, $fixedCookieValidityMinutes, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, $redirectType, $secretKey); public function getState($eventId, $cookieValidityMinutes, $secretKey, $validateTime); - public function cancelQueueCookie($eventId, $cookieDomain); - public function reissueQueueCookie($eventId, $cookieValidityMinutes, $cookieDomain, $secretKey); + public function cancelQueueCookie($eventId, $cookieDomain, $isCookieHttpOnly, $isCookieSecure); + public function reissueQueueCookie($eventId, $cookieValidityMinutes, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, $secretKey); } interface ICookieManager { - public function setCookie($name, $value, $expire, $domain); + public function setCookie($name, $value, $expire, $domain, $isCookieHttpOnly, $isCookieSecure); public function getCookie($cookieName); public function getCookieArray(); } @@ -27,38 +27,38 @@ function __construct(ICookieManager $cookieManager) { $this->cookieManager = $cookieManager; } - public function cancelQueueCookie($eventId, $cookieDomain) { + public function cancelQueueCookie($eventId, $cookieDomain, $isCookieHttpOnly, $isCookieSecure) { $cookieKey = self::getCookieKey($eventId); - $this->cookieManager->setCookie($cookieKey, null, -1, $cookieDomain); + $this->cookieManager->setCookie($cookieKey, null, -1, $cookieDomain, $isCookieHttpOnly, $isCookieSecure); } public static function getCookieKey($eventId) { return self::_QueueITDataKey . '_' . $eventId; } - public function store($eventId, $queueId, $fixedCookieValidityMinutes, $cookieDomain, $redirectType, $secretKey) { + public function store($eventId, $queueId, $fixedCookieValidityMinutes, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, $redirectType, $secretKey) { $cookieKey = self::getCookieKey($eventId); $cookieValue = $this->createCookieValue($eventId, $queueId, strval($fixedCookieValidityMinutes), $redirectType, $secretKey); - $this->cookieManager->setCookie($cookieKey, $cookieValue, time() + (24 * 60 * 60), $cookieDomain); + $this->cookieManager->setCookie($cookieKey, $cookieValue, time() + (24 * 60 * 60), $cookieDomain, $isCookieHttpOnly, $isCookieSecure); } private function createCookieValue($eventId, $queueId, $fixedCookieValidityMinutes, $redirectType, $secretKey) { $issueTime = time(); - $hashValue = $this->generateHash($eventId, $queueId, $fixedCookieValidityMinutes, $redirectType, $issueTime, $secretKey); - - $fixedCookieValidityMinutesPart = ""; - if(!Utils::isNullOrEmptyString($fixedCookieValidityMinutes)) { - $fixedCookieValidityMinutesPart = "&FixedValidityMins=" . $fixedCookieValidityMinutes; - } - - $cookieValue = "EventId=" . $eventId . "&QueueId=" . $queueId . $fixedCookieValidityMinutesPart . "&RedirectType=" . $redirectType . "&IssueTime=" . $issueTime . "&Hash=" . $hashValue; + $hashValue = $this->generateHash($eventId, $queueId, $fixedCookieValidityMinutes, $redirectType, $issueTime, $secretKey); + + $fixedCookieValidityMinutesPart = ""; + if(!Utils::isNullOrEmptyString($fixedCookieValidityMinutes)) { + $fixedCookieValidityMinutesPart = "&FixedValidityMins=" . $fixedCookieValidityMinutes; + } + + $cookieValue = "EventId=" . $eventId . "&QueueId=" . $queueId . $fixedCookieValidityMinutesPart . "&RedirectType=" . $redirectType . "&IssueTime=" . $issueTime . "&Hash=" . $hashValue; return $cookieValue; } private function getCookieNameValueMap($cookieValue) { $result = array(); $cookieNameValues = explode("&", $cookieValue); - $length = count($cookieNameValues); + $length = count($cookieNameValues); for ($i = 0; $i < $length; ++$i) { $arr = explode("=", $cookieNameValues[$i]); @@ -70,64 +70,64 @@ private function getCookieNameValueMap($cookieValue) { return $result; } - private function generateHash($eventId, $queueId, $fixedCookieValidityMinutes, $redirectType, $issueTime, $secretKey) { - return hash_hmac('sha256', $eventId . $queueId . $fixedCookieValidityMinutes . $redirectType . $issueTime, $secretKey); - } + private function generateHash($eventId, $queueId, $fixedCookieValidityMinutes, $redirectType, $issueTime, $secretKey) { + return hash_hmac('sha256', $eventId . $queueId . $fixedCookieValidityMinutes . $redirectType . $issueTime, $secretKey); + } private function isCookieValid($secretKey, array $cookieNameValueMap, $eventId, $cookieValidityMinutes, $validateTime) { if (!array_key_exists("EventId", $cookieNameValueMap)) { return false; } - if (!array_key_exists("QueueId", $cookieNameValueMap)) { + if (!array_key_exists("QueueId", $cookieNameValueMap)) { return false; } - if (!array_key_exists("RedirectType", $cookieNameValueMap)) { + if (!array_key_exists("RedirectType", $cookieNameValueMap)) { return false; } - if (!array_key_exists("IssueTime", $cookieNameValueMap)) { + if (!array_key_exists("IssueTime", $cookieNameValueMap)) { return false; } - if (!array_key_exists("Hash", $cookieNameValueMap)) { - return false; + if (!array_key_exists("Hash", $cookieNameValueMap)) { + return false; } - $fixedCookieValidityMinutes = ""; - if (array_key_exists("FixedValidityMins", $cookieNameValueMap)) { + $fixedCookieValidityMinutes = ""; + if (array_key_exists("FixedValidityMins", $cookieNameValueMap)) { $fixedCookieValidityMinutes = $cookieNameValueMap["FixedValidityMins"]; } $hashValue = $this->generateHash( - $cookieNameValueMap["EventId"], - $cookieNameValueMap["QueueId"], - $fixedCookieValidityMinutes, - $cookieNameValueMap["RedirectType"], - $cookieNameValueMap["IssueTime"], - $secretKey); + $cookieNameValueMap["EventId"], + $cookieNameValueMap["QueueId"], + $fixedCookieValidityMinutes, + $cookieNameValueMap["RedirectType"], + $cookieNameValueMap["IssueTime"], + $secretKey); if ($hashValue !== $cookieNameValueMap["Hash"]) { return false; } if(strtolower($eventId) !== strtolower($cookieNameValueMap["EventId"])) { - return false; - } - - if($validateTime) { - $validity = $cookieValidityMinutes; - if(!Utils::isNullOrEmptyString($fixedCookieValidityMinutes)) { - $validity = intval($fixedCookieValidityMinutes); - } - - $expirationTime = $cookieNameValueMap["IssueTime"] + ($validity*60); - if($expirationTime < time()) { - return false; - } + return false; + } + + if($validateTime) { + $validity = $cookieValidityMinutes; + if(!Utils::isNullOrEmptyString($fixedCookieValidityMinutes)) { + $validity = intval($fixedCookieValidityMinutes); + } + + $expirationTime = $cookieNameValueMap["IssueTime"] + ($validity*60); + if($expirationTime < time()) { + return false; + } } return true; } - public function reissueQueueCookie($eventId, $cookieValidityMinutes, $cookieDomain, $secretKey) { + public function reissueQueueCookie($eventId, $cookieValidityMinutes, $cookieDomain, $isCookieHttpOnly, $isCookieSecure, $secretKey) { $cookieKey = self::getCookieKey($eventId); if ($this->cookieManager->getCookie($cookieKey) === null) { return; @@ -136,19 +136,25 @@ public function reissueQueueCookie($eventId, $cookieValidityMinutes, $cookieDoma if (!$this->isCookieValid($secretKey, $cookieNameValueMap, $eventId, $cookieValidityMinutes, true)) { return; } - $fixedCookieValidityMinutes = ""; - if (array_key_exists("FixedValidityMins", $cookieNameValueMap)) { + $fixedCookieValidityMinutes = ""; + if (array_key_exists("FixedValidityMins", $cookieNameValueMap)) { $fixedCookieValidityMinutes = $cookieNameValueMap["FixedValidityMins"]; } $cookieValue = $this->createCookieValue( - $eventId, - $cookieNameValueMap["QueueId"], - $fixedCookieValidityMinutes, - $cookieNameValueMap["RedirectType"], - $secretKey); - - $this->cookieManager->setCookie($cookieKey, $cookieValue, time() + (24 * 60 * 60), $cookieDomain); + $eventId, + $cookieNameValueMap["QueueId"], + $fixedCookieValidityMinutes, + $cookieNameValueMap["RedirectType"], + $secretKey); + + $this->cookieManager->setCookie( + $cookieKey, + $cookieValue, + time() + (24 * 60 * 60), + $cookieDomain, + $isCookieHttpOnly, + $isCookieSecure); } public function getState($eventId, $cookieValidityMinutes, $secretKey, $validateTime) { @@ -194,10 +200,10 @@ public function __construct($isFound, $isValid, $queueId, $fixedCookieValidityMi $this->isValid = $isValid; $this->queueId = $queueId; $this->fixedCookieValidityMinutes = $fixedCookieValidityMinutes; - $this->redirectType = $redirectType; + $this->redirectType = $redirectType; } - public function isStateExtendable() { - return $this->isValid && $this->fixedCookieValidityMinutes === null; - } + public function isStateExtendable() { + return $this->isValid && $this->fixedCookieValidityMinutes === null; + } }