From a94737f41cb2deba9af9c52fcc9e3893df60e59b Mon Sep 17 00:00:00 2001 From: Mojtaba Sarooghi Date: Thu, 15 Feb 2018 18:07:00 +0100 Subject: [PATCH] -Support for Ajax calls --- KnownUser.php | 147 ++++++----- Models.php | 15 +- Tests/KnownUserTest.php | 527 +++++++++++++++++++++++++++++++++++++++- UserInQueueService.php | 2 +- 4 files changed, 619 insertions(+), 72 deletions(-) diff --git a/KnownUser.php b/KnownUser.php index b1fc23a..6211907 100644 --- a/KnownUser.php +++ b/KnownUser.php @@ -11,6 +11,7 @@ class KnownUser { + const QueueITAjaxHeaderKey = "x-queueit-ajaxpageurl" ; //used for unittest private static $userInQueueService = null; private static function getUserInQueueService() { @@ -45,46 +46,10 @@ public static function extendQueueCookie($eventId, $cookieValidityMinute, $cooki $userInQueueService->extendQueueCookie($eventId, $cookieValidityMinute, $cookieDomain, $secretKey); } - public static function resolveRequestByLocalEventConfig($targetUrl, $queueitToken, QueueEventConfig $queueConfig, $customerId, $secretKey) { - if (KnownUser::getIsDebug($queueitToken, $secretKey)) - { - $dic = array( - "TargetUrl"=> $targetUrl, - "QueueitToken"=> $queueitToken, - "QueueConfig"=>$queueConfig != null ? $queueConfig->getString() : "NULL", - "OriginalUrl"=> KnownUser::getHttpRequestProvider()->getAbsoluteUri()); - KnownUser::logMoreRequestDetails($dic); - KnownUser::doCookieLog($dic); - } - if (Utils::isNullOrEmptyString($customerId)) { - throw new KnownUserException("customerId can not be null or empty."); - } - - if (Utils::isNullOrEmptyString($secretKey)) { - throw new KnownUserException("secretKey can not be null or empty."); - } - - if (Utils::isNullOrEmptyString($queueConfig->eventId)) { - throw new KnownUserException("eventId from queueConfig can not be null or empty."); - } - if (Utils::isNullOrEmptyString($queueConfig->queueDomain)) { - throw new KnownUserException("queueDomain from queueConfig can not be null or empty."); - } - - if (!is_int($queueConfig->cookieValidityMinute) || intval($queueConfig->cookieValidityMinute) <= 0) { - throw new KnownUserException("cookieValidityMinute from queueConfig should be integer greater than 0."); - } - - if (!is_bool($queueConfig->extendCookieValidity)) { - throw new KnownUserException("extendCookieValidity from queueConfig should be valid boolean."); - } - - $userInQueueService = KnownUser::getUserInQueueService(); - return $userInQueueService->validateQueueRequest($targetUrl, $queueitToken, $queueConfig, $customerId, $secretKey); - } public static function cancelRequestByLocalConfig($targetUrl, $queueitToken,CancelEventConfig $cancelConfig, $customerId, $secretKey) { + $targetUrl = KnownUser::generateTargetUrl($targetUrl); if (KnownUser::getIsDebug($queueitToken, $secretKey)) { $dic = array( @@ -114,8 +79,11 @@ public static function cancelRequestByLocalConfig($targetUrl, $queueitToken,Canc if (Utils::isNullOrEmptyString($cancelConfig->queueDomain)) { throw new KnownUserException("queueDomain from cancelConfig can not be null or empty."); } + $userInQueueService = KnownUser::getUserInQueueService(); - return $userInQueueService->validateCancelRequest($targetUrl, $cancelConfig, $customerId, $secretKey); + $result = $userInQueueService->validateCancelRequest($targetUrl, $cancelConfig, $customerId, $secretKey); + $result->isAjaxResult = KnownUser::isQueueAjaxCall(); + return $result; } public static function validateRequestByIntegrationConfig($currentUrlWithoutQueueITToken, $queueitToken, $integrationsConfigString, $customerId, $secretKey) { @@ -175,7 +143,9 @@ public static function validateRequestByIntegrationConfig($currentUrlWithoutQueu else //IgnoreAction { $userInQueueService = KnownUser::getUserInQueueService(); - return $userInQueueService->getIgnoreActionResult(); + $result = $userInQueueService->getIgnoreActionResult(); + $result->isAjaxResult = KnownUser::isQueueAjaxCall(); + return $result; } } catch (\Exception $e) { @@ -184,7 +154,52 @@ public static function validateRequestByIntegrationConfig($currentUrlWithoutQueu } + public static function resolveRequestByLocalEventConfig($targetUrl, $queueitToken, QueueEventConfig $queueConfig, $customerId, $secretKey) { + $targetUrl = KnownUser::generateTargetUrl($targetUrl); + return KnownUser::_resolveRequestByLocalEventConfig($targetUrl, $queueitToken, $queueConfig, $customerId, $secretKey); + } + private static function _resolveRequestByLocalEventConfig($targetUrl, $queueitToken, QueueEventConfig $queueConfig, $customerId, $secretKey) { + + if (KnownUser::getIsDebug($queueitToken, $secretKey)) + { + $dic = array( + "TargetUrl"=> $targetUrl, + "QueueitToken"=> $queueitToken, + "QueueConfig"=>$queueConfig != null ? $queueConfig->getString() : "NULL", + "OriginalUrl"=> KnownUser::getHttpRequestProvider()->getAbsoluteUri()); + KnownUser::logMoreRequestDetails($dic); + KnownUser::doCookieLog($dic); + } + if (Utils::isNullOrEmptyString($customerId)) { + throw new KnownUserException("customerId can not be null or empty."); + } + + if (Utils::isNullOrEmptyString($secretKey)) { + throw new KnownUserException("secretKey can not be null or empty."); + } + + if (Utils::isNullOrEmptyString($queueConfig->eventId)) { + throw new KnownUserException("eventId from queueConfig can not be null or empty."); + } + if (Utils::isNullOrEmptyString($queueConfig->queueDomain)) { + throw new KnownUserException("queueDomain from queueConfig can not be null or empty."); + } + + if (!is_int($queueConfig->cookieValidityMinute) || intval($queueConfig->cookieValidityMinute) <= 0) { + throw new KnownUserException("cookieValidityMinute from queueConfig should be integer greater than 0."); + } + + if (!is_bool($queueConfig->extendCookieValidity)) { + throw new KnownUserException("extendCookieValidity from queueConfig should be valid boolean."); + } + + + $userInQueueService = KnownUser::getUserInQueueService(); + $result = $userInQueueService->validateQueueRequest($targetUrl, $queueitToken, $queueConfig, $customerId, $secretKey); + $result->isAjaxResult = KnownUser::isQueueAjaxCall(); + return $result; + } private static function handleQueueAction( $currentUrlWithoutQueueITToken, $queueitToken, $customerIntegration, $customerId, @@ -202,19 +217,20 @@ private static function handleQueueAction( $eventConfig->cookieValidityMinute = $matchedConfig["CookieValidityMinute"]; $eventConfig->version = $customerIntegration["Version"]; - switch ($matchedConfig["RedirectLogic"]) { - case "ForcedTargetUrl": - case "ForecedTargetUrl": - $targetUrl = $matchedConfig["ForcedTargetUrl"]; - break; - case "EventTargetUrl": - $targetUrl = ""; - break; - default : - $targetUrl = $currentUrlWithoutQueueITToken; - } - return KnownUser::resolveRequestByLocalEventConfig($targetUrl, $queueitToken, $eventConfig, $customerId, $secretKey); - } + switch ($matchedConfig["RedirectLogic"]) { + case "ForcedTargetUrl": + case "ForecedTargetUrl": + $targetUrl = $matchedConfig["ForcedTargetUrl"]; + break; + case "EventTargetUrl": + $targetUrl = ""; + break; + default : + $targetUrl = KnownUser::generateTargetUrl($currentUrlWithoutQueueITToken); + } + + return KnownUser::_resolveRequestByLocalEventConfig($targetUrl, $queueitToken, $eventConfig, $customerId, $secretKey); + } private static function handleCancelAction( $currentUrlWithoutQueueITToken, $queueitToken, @@ -222,12 +238,12 @@ private static function handleCancelAction( $secretKey, $matchedConfig) { - $cancelEventConfig = new CancelEventConfig(); - $cancelEventConfig->eventId = $matchedConfig["EventId"]; - $cancelEventConfig->queueDomain = $matchedConfig["QueueDomain"]; - $cancelEventConfig->cookieDomain = $matchedConfig["CookieDomain"]; - $cancelEventConfig->version = $customerIntegration["Version"]; - return KnownUser::cancelRequestByLocalConfig($currentUrlWithoutQueueITToken, $queueitToken, $cancelEventConfig, $customerId, $secretKey); + $cancelEventConfig = new CancelEventConfig(); + $cancelEventConfig->eventId = $matchedConfig["EventId"]; + $cancelEventConfig->queueDomain = $matchedConfig["QueueDomain"]; + $cancelEventConfig->cookieDomain = $matchedConfig["CookieDomain"]; + $cancelEventConfig->version = $customerIntegration["Version"]; + return KnownUser::cancelRequestByLocalConfig($currentUrlWithoutQueueITToken, $queueitToken, $cancelEventConfig, $customerId, $secretKey); } private static function logMoreRequestDetails(array &$debugInfos) { @@ -276,6 +292,21 @@ private static function getIsDebug($queueitToken, $secretKey) } return false; } + + private static function generateTargetUrl($originalTargetUrl) + { + return !KnownUser::isQueueAjaxCall() ? + $originalTargetUrl : + urldecode(KnownUser::getHttpRequestProvider()->getHeaderArray()[KnownUser::QueueITAjaxHeaderKey]); + } + private static function isQueueAjaxCall() + { + return array_key_exists(KnownUser::QueueITAjaxHeaderKey, + KnownUser::getHttpRequestProvider()->getHeaderArray()); + + } + + } class CookieManager implements ICookieManager diff --git a/Models.php b/Models.php index 1ab93bc..b28ebf3 100644 --- a/Models.php +++ b/Models.php @@ -46,7 +46,7 @@ class RequestValidationResult public $queueId; public $actionType; public $redirectType; - + public $isAjaxResult; function __construct($actionType, $eventId, $queueId, $redirectUrl, $redirectType) { $this->actionType = $actionType; $this->eventId = $eventId; @@ -57,7 +57,18 @@ function __construct($actionType, $eventId, $queueId, $redirectUrl, $redirectTyp public function doRedirect() { return !Utils::isNullOrEmptyString($this->redirectUrl); - } + } + public function getAjaxQueueRedirectHeaderKey() + { + return "x-queueit-redirect"; + } + public function getAjaxRedirectUrl() { + if (!Utils::isNullOrEmptyString($this->redirectUrl)) + { + return urlencode($this->redirectUrl); + } + return ""; + } } class KnownUserException extends \Exception diff --git a/Tests/KnownUserTest.php b/Tests/KnownUserTest.php index c83d682..6543aea 100644 --- a/Tests/KnownUserTest.php +++ b/Tests/KnownUserTest.php @@ -59,6 +59,7 @@ class UserInQueueServiceMock implements QueueIT\KnownUserV3\SDK\IUserInQueueServ public $arrayFunctionCallsArgs; public $arrayReturns; public $validateCancelRequestResult; + public $validateQueueRequestResult; function __construct() { @@ -84,6 +85,7 @@ public function validateQueueRequest( $config, $customerId, $secretKey)); + return $this->validateQueueRequestResult; } public function validateCancelRequest( @@ -139,8 +141,27 @@ public function expectCallAny($functionName) { class KnownUserTest extends UnitTestCase { + function setHttpHeaderRequestProvider() + { + $httpRequestProvider = new HttpRequestProviderMock(); + $httpRequestProvider->headerArray = array(); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'httpRequestProvider'); + $r->setAccessible(true); + $r->setValue(null, $httpRequestProvider); + } function test_cancelRequestByLocalConfig() { + + $httpRequestProvider = new HttpRequestProviderMock(); + $httpRequestProvider->headerArray = array( + "forwarded" => "f", + "x-forwarded-for" => "xff", + "x-forwarded-proto" => "xfp"); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'httpRequestProvider'); + $r->setAccessible(true); + $r->setValue(null, $httpRequestProvider); + $userInQueueservice = new UserInQueueServiceMock(); + $userInQueueservice->validateCancelRequestResult = new QueueIT\KnownUserV3\SDK\RequestValidationResult("Cancel", "eventid", "queueid", "http://q.qeuue-it.com", null); $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); $r->setAccessible(true); $r->setValue(null, $userInQueueservice); @@ -151,7 +172,7 @@ function test_cancelRequestByLocalConfig() { $cancelEventconfig->queueDomain = "queuedomain"; $cancelEventconfig->version = 1; - QueueIT\KnownUserV3\SDK\KnownUser::cancelRequestByLocalConfig("url","queueittoken" ,$cancelEventconfig,"customerid","secretkey"); + $result = QueueIT\KnownUserV3\SDK\KnownUser::cancelRequestByLocalConfig("url","queueittoken" ,$cancelEventconfig,"customerid","secretkey"); $this->assertTrue("url"==$userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][0]); $this->assertTrue("customerid"== $userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][2]); @@ -160,8 +181,53 @@ function test_cancelRequestByLocalConfig() { $this->assertTrue("queuedomain"==$userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][1]->queueDomain); $this->assertTrue("cookiedomain"==$userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][1]->cookieDomain); $this->assertTrue("1"==$userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][1]->version); + $this->assertFalse($result->isAjaxResult); + } + + function test_cancelRequestByLocalConfig_AjaxCall() { + $httpRequestProvider = new HttpRequestProviderMock(); + $httpRequestProvider->headerArray = array( + "forwarded" => "f", + "x-forwarded-for" => "xff", + "x-queueit-ajaxpageurl" => "http%3a%2f%2furl", + "x-forwarded-proto" => "xfp"); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'httpRequestProvider'); + $r->setAccessible(true); + $r->setValue(null, $httpRequestProvider); + + $userInQueueservice = new UserInQueueServiceMock(); + $userInQueueservice->validateCancelRequestResult = new QueueIT\KnownUserV3\SDK\RequestValidationResult("Cancel", "eventid", "queueid", "http://q.qeuue-it.com", null); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); + $r->setAccessible(true); + $r->setValue(null, $userInQueueservice); + + $cancelEventconfig = new \QueueIT\KnownUserV3\SDK\CancelEventConfig(); + $cancelEventconfig->cookieDomain = "cookiedomain"; + $cancelEventconfig->eventId = "eventid"; + $cancelEventconfig->queueDomain = "queuedomain"; + $cancelEventconfig->version = 1; + + $result = QueueIT\KnownUserV3\SDK\KnownUser::cancelRequestByLocalConfig("url","queueittoken" ,$cancelEventconfig,"customerid","secretkey"); + + $this->assertTrue("http://url"==$userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][0]); + $this->assertTrue("customerid"== $userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][2]); + $this->assertTrue("secretkey"== $userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][3]); + $this->assertTrue("eventid"==$userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][1]->eventId); + $this->assertTrue("queuedomain"==$userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][1]->queueDomain); + $this->assertTrue("cookiedomain"==$userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][1]->cookieDomain); + $this->assertTrue("1"==$userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][1]->version); + $this->assertTrue($result->isAjaxResult); + + $this->assertTrue(strtolower($result->getAjaxRedirectUrl())=="http%3a%2f%2fq.qeuue-it.com"); } function test_cancelRequestByLocalConfig_empty_eventId() { + + $httpRequestProvider = new HttpRequestProviderMock(); + $httpRequestProvider->headerArray = array(); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'httpRequestProvider'); + $r->setAccessible(true); + $r->setValue(null, $httpRequestProvider); + $eventconfig = new \QueueIT\KnownUserV3\SDK\CancelEventConfig(); $eventconfig->cookieDomain = "cookieDomain"; $eventconfig->queueDomain = "queueDomain"; @@ -177,6 +243,12 @@ function test_cancelRequestByLocalConfig_empty_eventId() { $this->assertTrue($exceptionThrown); } function test_cancelRequestByLocalConfig_empty_secreteKey() { + $httpRequestProvider = new HttpRequestProviderMock(); + $httpRequestProvider->headerArray = array(); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'httpRequestProvider'); + $r->setAccessible(true); + $r->setValue(null, $httpRequestProvider); + $eventconfig = new \QueueIT\KnownUserV3\SDK\CancelEventConfig(); $eventconfig->cookieDomain = "cookieDomain"; $eventconfig->eventId = "eventId"; @@ -192,6 +264,8 @@ function test_cancelRequestByLocalConfig_empty_secreteKey() { $this->assertTrue($exceptionThrown); } function test_cancelRequestByLocalConfig_empty_queueDomain() { +$this->setHttpHeaderRequestProvider(); + $eventconfig = new \QueueIT\KnownUserV3\SDK\CancelEventConfig(); $eventconfig->cookieDomain = "cookieDomain"; $eventconfig->eventId = "eventId"; @@ -207,6 +281,8 @@ function test_cancelRequestByLocalConfig_empty_queueDomain() { $this->assertTrue($exceptionThrown); } function test_cancelRequestByLocalConfig_empty_customerId() { + $this->setHttpHeaderRequestProvider(); + $eventconfig = new \QueueIT\KnownUserV3\SDK\CancelEventConfig(); $eventconfig->cookieDomain = "cookieDomain"; $eventconfig->eventId = "eventId"; @@ -223,6 +299,8 @@ function test_cancelRequestByLocalConfig_empty_customerId() { } function test_cancelRequestByLocalConfig_empty_targeturl() { + $this->setHttpHeaderRequestProvider(); + $eventconfig = new \QueueIT\KnownUserV3\SDK\CancelEventConfig(); $eventconfig->cookieDomain = "cookieDomain"; $eventconfig->eventId = "eventId"; @@ -238,6 +316,14 @@ function test_cancelRequestByLocalConfig_empty_targeturl() { } function test_extendQueueCookie_null_EventId() { + $this->setHttpHeaderRequestProvider(); + + $httpRequestProvider = new HttpRequestProviderMock(); + $httpRequestProvider->headerArray = array(); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'httpRequestProvider'); + $r->setAccessible(true); + $r->setValue(null, $httpRequestProvider); + $exceptionThrown = false; try { QueueIT\KnownUserV3\SDK\KnownUser::extendQueueCookie(NULL, 10, "cookieDomain", "secretkey"); @@ -248,6 +334,14 @@ function test_extendQueueCookie_null_EventId() { } function test_extendQueueCookie_null_SecretKey() { + $this->setHttpHeaderRequestProvider(); + + $httpRequestProvider = new HttpRequestProviderMock(); + $httpRequestProvider->headerArray = array(); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'httpRequestProvider'); + $r->setAccessible(true); + $r->setValue(null, $httpRequestProvider); + $exceptionThrown = false; try { QueueIT\KnownUserV3\SDK\KnownUser::extendQueueCookie("event1", 10, "cookieDomain", NULL); @@ -258,6 +352,8 @@ function test_extendQueueCookie_null_SecretKey() { } function test_extendQueueCookie_Invalid_CookieValidityMinute() { + $this->setHttpHeaderRequestProvider(); + $exceptionThrown = false; try { QueueIT\KnownUserV3\SDK\KnownUser::extendQueueCookie("event1", "invalidInt", "cookieDomain", "secretkey"); @@ -268,6 +364,7 @@ function test_extendQueueCookie_Invalid_CookieValidityMinute() { } function test_extendQueueCookie_Negative_CookieValidityMinute() { + $this->setHttpHeaderRequestProvider(); $exceptionThrown = false; try { QueueIT\KnownUserV3\SDK\KnownUser::extendQueueCookie("event1", -1, "cookieDomain", "secretkey"); @@ -278,6 +375,7 @@ function test_extendQueueCookie_Negative_CookieValidityMinute() { } function test_extendQueueCookie() { + $this->setHttpHeaderRequestProvider(); $userInQueueservice = new UserInQueueServiceMock(); $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); $r->setAccessible(true); @@ -289,7 +387,7 @@ function test_extendQueueCookie() { } function test_resolveRequestByLocalEventConfig_empty_eventId() { - + $this->setHttpHeaderRequestProvider(); $eventconfig = new \QueueIT\KnownUserV3\SDK\QueueEventConfig(); $eventconfig->cookieDomain = "cookieDomain"; $eventconfig->layoutName = "layoutName"; @@ -310,6 +408,8 @@ function test_resolveRequestByLocalEventConfig_empty_eventId() { $this->assertTrue($exceptionThrown); } function test_resolveRequestByLocalEventConfig_empty_secreteKey() { + $this->setHttpHeaderRequestProvider(); + $eventconfig = new \QueueIT\KnownUserV3\SDK\QueueEventConfig(); $eventconfig->cookieDomain = "cookieDomain"; $eventconfig->layoutName = "layoutName"; @@ -329,6 +429,8 @@ function test_resolveRequestByLocalEventConfig_empty_secreteKey() { $this->assertTrue($exceptionThrown); } function test_resolveRequestByLocalEventConfig_empty_queueDomain() { + $this->setHttpHeaderRequestProvider(); + $eventconfig = new \QueueIT\KnownUserV3\SDK\QueueEventConfig(); $eventconfig->cookieDomain = "cookieDomain"; $eventconfig->layoutName = "layoutName"; @@ -348,6 +450,8 @@ function test_resolveRequestByLocalEventConfig_empty_queueDomain() { $this->assertTrue($exceptionThrown); } function test_resolveRequestByLocalEventConfig_empty_customerId() { + $this->setHttpHeaderRequestProvider(); + $eventconfig = new \QueueIT\KnownUserV3\SDK\QueueEventConfig(); //$eventconfig->cookieDomain = "cookieDomain"; $eventconfig->layoutName = "layoutName"; @@ -367,6 +471,8 @@ function test_resolveRequestByLocalEventConfig_empty_customerId() { $this->assertTrue($exceptionThrown); } function test_resolveRequestByLocalEventConfig_Invalid_extendCookieValidity() { + $this->setHttpHeaderRequestProvider(); + $eventconfig = new \QueueIT\KnownUserV3\SDK\QueueEventConfig(); $eventconfig->cookieDomain = "cookieDomain"; $eventconfig->layoutName = "layoutName"; @@ -386,6 +492,8 @@ function test_resolveRequestByLocalEventConfig_Invalid_extendCookieValidity() { $this->assertTrue($exceptionThrown); } function test_resolveRequestByLocalEventConfig_Invalid_cookieValidityMinute() { + $this->setHttpHeaderRequestProvider(); + $eventconfig = new \QueueIT\KnownUserV3\SDK\QueueEventConfig(); $eventconfig->cookieDomain = "cookieDomain"; $eventconfig->layoutName = "layoutName"; @@ -405,6 +513,7 @@ function test_resolveRequestByLocalEventConfig_Invalid_cookieValidityMinute() { $this->assertTrue($exceptionThrown); } function test_resolveRequestByLocalEventConfig_zero_cookieValidityMinute() { + $this->setHttpHeaderRequestProvider(); $eventconfig = new \QueueIT\KnownUserV3\SDK\QueueEventConfig(); $eventconfig->cookieDomain = "cookieDomain"; $eventconfig->layoutName = "layoutName"; @@ -424,7 +533,9 @@ function test_resolveRequestByLocalEventConfig_zero_cookieValidityMinute() { $this->assertTrue($exceptionThrown); } function test_resolveRequestByLocalEventConfig() { + $this->setHttpHeaderRequestProvider(); $userInQueueservice = new UserInQueueServiceMock(); + $userInQueueservice->validateQueueRequestResult = new QueueIT\KnownUserV3\SDK\RequestValidationResult("Queue","eventid","","http://q.qeuue-it.com",""); $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); $r->setAccessible(true); $r->setValue(null, $userInQueueservice); @@ -438,12 +549,49 @@ function test_resolveRequestByLocalEventConfig() { $eventconfig->cookieValidityMinute = 10; $eventconfig->version = 12; - QueueIT\KnownUserV3\SDK\KnownUser::resolveRequestByLocalEventConfig("targeturl", "queueIttoken", $eventconfig, "customerid", "secretkey"); + $result = QueueIT\KnownUserV3\SDK\KnownUser::resolveRequestByLocalEventConfig("targeturl", "queueIttoken", $eventconfig, "customerid", "secretkey"); $this->assertTrue($userInQueueservice->expectCall('validateRequest', 1, array("targeturl", "queueIttoken", $eventconfig, "customerid", "secretkey"))); + $this->assertFalse($result->isAjaxResult); } + function test_resolveRequestByLocalEventConfig_AjaxCall() { + $httpRequestProvider = new HttpRequestProviderMock(); + $httpRequestProvider->headerArray = array( + + "forwarded" => "f", + "x-forwarded-for" => "xff", + "x-queueit-ajaxpageurl" => "http%3a%2f%2furl", + "x-forwarded-proto" => "xfp"); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'httpRequestProvider'); + $r->setAccessible(true); + $r->setValue(null, $httpRequestProvider); + + $userInQueueservice = new UserInQueueServiceMock(); + $userInQueueservice->validateQueueRequestResult = new QueueIT\KnownUserV3\SDK\RequestValidationResult("Queue","eventid","","http://q.qeuue-it.com",""); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); + $r->setAccessible(true); + $r->setValue(null, $userInQueueservice); + $eventconfig = new \QueueIT\KnownUserV3\SDK\QueueEventConfig(); + $eventconfig->cookieDomain = "cookieDomain"; + $eventconfig->layoutName = "layoutName"; + $eventconfig->culture = "culture"; + $eventconfig->eventId = "eventId"; + $eventconfig->queueDomain = "queueDomain"; + $eventconfig->extendCookieValidity = true; + $eventconfig->cookieValidityMinute = 10; + $eventconfig->version = 12; + + $result = QueueIT\KnownUserV3\SDK\KnownUser::resolveRequestByLocalEventConfig("targeturl", "queueIttoken", $eventconfig, "customerid", "secretkey"); + + $this->assertTrue($userInQueueservice->expectCall('validateRequest', 1, array("http://url", "queueIttoken", $eventconfig, "customerid", "secretkey"))); + $this->assertTrue($result->isAjaxResult); + $this->assertTrue(strtolower($result->getAjaxRedirectUrl())=="http%3a%2f%2fq.qeuue-it.com"); + } + function test_validateRequestByIntegrationConfig_empty_currentUrl() { + $this->setHttpHeaderRequestProvider(); + $exceptionThrown = false; try { QueueIT\KnownUserV3\SDK\KnownUser::validateRequestByIntegrationConfig("", "queueIttoken", "{}","customerId", "secretkey"); @@ -464,12 +612,100 @@ function test_validateRequestByIntegrationConfig_empty_integrationsConfigString( function test_validateRequestByIntegrationConfig() { + + $httpRequestProvider = new HttpRequestProviderMock(); + $httpRequestProvider->userAgent="googlebot"; + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'httpRequestProvider'); + $r->setAccessible(true); + $r->setValue(null, $httpRequestProvider); + + $userInQueueservice = new UserInQueueServiceMock(); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); + $r->setAccessible(true); + $r->setValue(null, $userInQueueservice); + $userInQueueservice->validateQueueRequestResult = new QueueIT\KnownUserV3\SDK\RequestValidationResult("Queue","eventid","","http://q.qeuue-it.com",""); + + + $integrationConfigString = <<assertTrue(count($userInQueueservice->arrayFunctionCallsArgs['validateRequest']) == 1); + $this->assertTrue($userInQueueservice->arrayFunctionCallsArgs['validateRequest'][0][0] == "http://test.com?event1=true"); + $this->assertTrue($userInQueueservice->arrayFunctionCallsArgs['validateRequest'][0][1] == "queueIttoken"); + $this->assertTrue($userInQueueservice->arrayFunctionCallsArgs['validateRequest'][0][3] == "customerid"); + $this->assertTrue($userInQueueservice->arrayFunctionCallsArgs['validateRequest'][0][4] == "secretkey"); + + $this->assertTrue($userInQueueservice->arrayFunctionCallsArgs['validateRequest'][0][2]->queueDomain == "knownusertest.queue-it.net"); + $this->assertTrue($userInQueueservice->arrayFunctionCallsArgs['validateRequest'][0][2]->eventId == "event1"); + $this->assertTrue($userInQueueservice->arrayFunctionCallsArgs['validateRequest'][0][2]->culture == ""); + $this->assertTrue($userInQueueservice->arrayFunctionCallsArgs['validateRequest'][0][2]->layoutName == "Christmas Layout by Queue-it"); + $this->assertTrue($userInQueueservice->arrayFunctionCallsArgs['validateRequest'][0][2]->extendCookieValidity); + $this->assertTrue($userInQueueservice->arrayFunctionCallsArgs['validateRequest'][0][2]->cookieValidityMinute == 20); + $this->assertTrue($userInQueueservice->arrayFunctionCallsArgs['validateRequest'][0][2]->cookieDomain == ".test.com"); + $this->assertTrue($userInQueueservice->arrayFunctionCallsArgs['validateRequest'][0][2]->version == 3); + $this->assertFalse($result->isAjaxResult); + } + + + function test_validateRequestByIntegrationConfig_AjaxCall() { + $userInQueueservice = new UserInQueueServiceMock(); $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); $r->setAccessible(true); $r->setValue(null, $userInQueueservice); + $userInQueueservice->validateQueueRequestResult = new QueueIT\KnownUserV3\SDK\RequestValidationResult("Queue","eventid","","http://q.qeuue-it.com",""); $httpRequestProvider = new HttpRequestProviderMock(); + $httpRequestProvider->headerArray = array( + "a" => "b", + "x-queueit-ajaxpageurl" => "http%3a%2f%2ftest.com%3fevent1%3dtrue", + "e" => "f"); $httpRequestProvider->userAgent="googlebot"; $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'httpRequestProvider'); $r->setAccessible(true); @@ -523,7 +759,7 @@ function test_validateRequestByIntegrationConfig() { } EOT; - QueueIT\KnownUserV3\SDK\KnownUser::validateRequestByIntegrationConfig("http://test.com?event1=true", "queueIttoken", $integrationConfigString, "customerid", "secretkey"); + $result = QueueIT\KnownUserV3\SDK\KnownUser::validateRequestByIntegrationConfig("http://url?event1", "queueIttoken", $integrationConfigString, "customerid", "secretkey"); $this->assertTrue(count($userInQueueservice->arrayFunctionCallsArgs['validateRequest']) == 1); $this->assertTrue($userInQueueservice->arrayFunctionCallsArgs['validateRequest'][0][0] == "http://test.com?event1=true"); $this->assertTrue($userInQueueservice->arrayFunctionCallsArgs['validateRequest'][0][1] == "queueIttoken"); @@ -538,10 +774,13 @@ function test_validateRequestByIntegrationConfig() { $this->assertTrue($userInQueueservice->arrayFunctionCallsArgs['validateRequest'][0][2]->cookieValidityMinute == 20); $this->assertTrue($userInQueueservice->arrayFunctionCallsArgs['validateRequest'][0][2]->cookieDomain == ".test.com"); $this->assertTrue($userInQueueservice->arrayFunctionCallsArgs['validateRequest'][0][2]->version == 3); + $this->assertTrue($result->isAjaxResult); + $this->assertTrue(strtolower($result->getAjaxRedirectUrl())=="http%3a%2f%2fq.qeuue-it.com"); } function test_validateRequestByIntegrationConfig_NotMatch() { + $this->setHttpHeaderRequestProvider(); $userInQueueservice = new UserInQueueServiceMock(); $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); $r->setAccessible(true); @@ -566,8 +805,10 @@ function test_validateRequestByIntegrationConfig_NotMatch() function test_validateRequestByIntegrationConfig_ForcedTargeturl() { + $this->setHttpHeaderRequestProvider(); $userInQueueservice = new UserInQueueServiceMock(); $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); + $userInQueueservice->validateQueueRequestResult = new QueueIT\KnownUserV3\SDK\RequestValidationResult("Queue","eventid","","http://q.qeuue-it.com",""); $r->setAccessible(true); $r->setValue(null, $userInQueueservice); @@ -617,10 +858,75 @@ function test_validateRequestByIntegrationConfig_ForcedTargeturl() $this->assertTrue(count($userInQueueservice->arrayFunctionCallsArgs['validateRequest']) == 1); $this->assertTrue($userInQueueservice->arrayFunctionCallsArgs['validateRequest'][0][0] == "http://test.com"); } + function test_validateRequestByIntegrationConfig_ForcedTargeturl_AjaxCall() + { + $httpRequestProvider = new HttpRequestProviderMock(); + $httpRequestProvider->headerArray = array( + "a" => "b", + "x-queueit-ajaxpageurl" => "http%3a%2f%2fabcdef.com%3fevent1%3dtrue", + "e" => "f"); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'httpRequestProvider'); + $r->setAccessible(true); + $r->setValue(null, $httpRequestProvider); + $userInQueueservice = new UserInQueueServiceMock(); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); + $userInQueueservice->validateQueueRequestResult = new QueueIT\KnownUserV3\SDK\RequestValidationResult("Queue","eventid","","http://q.qeuue-it.com",""); + $r->setAccessible(true); + $r->setValue(null, $userInQueueservice); + + $integrationConfigString = <<assertTrue(count($userInQueueservice->arrayFunctionCallsArgs['validateRequest']) == 1); + $this->assertTrue($userInQueueservice->arrayFunctionCallsArgs['validateRequest'][0][0] == "http://test.com"); + $this->assertTrue($result->isAjaxResult); + } function test_validateRequestByIntegrationConfig_ForecedTargeturl() { + $this->setHttpHeaderRequestProvider(); $userInQueueservice = new UserInQueueServiceMock(); + $userInQueueservice->validateQueueRequestResult = new QueueIT\KnownUserV3\SDK\RequestValidationResult("Queue","eventid","","http://q.qeuue-it.com",""); $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); $r->setAccessible(true); $r->setValue(null, $userInQueueservice); @@ -674,7 +980,9 @@ function test_validateRequestByIntegrationConfig_ForecedTargeturl() function test_validateRequestByIntegrationConfig_EventTargetUrl() { + $this->setHttpHeaderRequestProvider(); $userInQueueservice = new UserInQueueServiceMock(); + $userInQueueservice->validateQueueRequestResult = new QueueIT\KnownUserV3\SDK\RequestValidationResult("Queue","eventid","","http://q.qeuue-it.com",""); $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); $r->setAccessible(true); $r->setValue(null, $userInQueueservice); @@ -725,8 +1033,72 @@ function test_validateRequestByIntegrationConfig_EventTargetUrl() { $this->assertTrue($userInQueueservice->arrayFunctionCallsArgs['validateRequest'][0][0] == ""); } + function test_validateRequestByIntegrationConfig_EventTargetUrl_AjaxCall() { + $httpRequestProvider = new HttpRequestProviderMock(); + $httpRequestProvider->headerArray = array( + "a" => "b", + "x-queueit-ajaxpageurl" => "http%3a%2f%2ftest.com%3fevent1%3dtrue", + "e" => "f"); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'httpRequestProvider'); + $r->setAccessible(true); + $r->setValue(null, $httpRequestProvider); + + $userInQueueservice = new UserInQueueServiceMock(); + $userInQueueservice->validateQueueRequestResult = new QueueIT\KnownUserV3\SDK\RequestValidationResult("Queue","eventid","","http://q.qeuue-it.com",""); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); + $r->setAccessible(true); + $r->setValue(null, $userInQueueservice); + + $var = "some text"; + $integrationConfigString = <<assertTrue(count($userInQueueservice->arrayFunctionCallsArgs['validateRequest']) == 1); + $this->assertTrue($userInQueueservice->arrayFunctionCallsArgs['validateRequest'][0][0] == ""); + $this->assertTrue($result->isAjaxResult); + } function test_validateRequestByIntegrationConfig_CancelAction() { + $this->setHttpHeaderRequestProvider(); + $userInQueueservice = new UserInQueueServiceMock(); $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); $r->setAccessible(true); @@ -778,11 +1150,138 @@ function test_validateRequestByIntegrationConfig_CancelAction() $this->assertTrue("event1"==$userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][1]->eventId); $this->assertTrue("knownusertest.queue-it.net"==$userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][1]->queueDomain); $this->assertTrue(".test.com"==$userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][1]->cookieDomain); - $this->assertTrue("3"==$userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][1]->version); + $this->assertTrue("3"==$userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][1]->version); + $this->assertFalse($result->isAjaxResult); } + function test_validateRequestByIntegrationConfig_CancelAction_AjaxCall() + { + $httpRequestProvider = new HttpRequestProviderMock(); + $httpRequestProvider->headerArray = array( + "a" => "b", + "x-queueit-ajaxpageurl" => "http%3a%2f%2furl", + "e" => "f"); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'httpRequestProvider'); + $r->setAccessible(true); + $r->setValue(null, $httpRequestProvider); + $userInQueueservice = new UserInQueueServiceMock(); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); + $r->setAccessible(true); + $r->setValue(null, $userInQueueservice); + $userInQueueservice->validateCancelRequestResult = new QueueIT\KnownUserV3\SDK\RequestValidationResult("Cancel", "eventid", "queueid", "http://q.qeuue-it.com", null); + + $var = "some text"; + $integrationConfigString = <<assertTrue(strtolower($result->getAjaxRedirectUrl()) =="http%3a%2f%2fq.qeuue-it.com"); + $this->assertTrue(count($userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest']) == 1); + $this->assertTrue($userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][0] == "http://url"); + $this->assertTrue("customerid"== $userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][2]); + $this->assertTrue("secretkey"== $userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][3]); + $this->assertTrue("event1"==$userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][1]->eventId); + $this->assertTrue("knownusertest.queue-it.net"==$userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][1]->queueDomain); + $this->assertTrue(".test.com"==$userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][1]->cookieDomain); + $this->assertTrue("3"==$userInQueueservice->arrayFunctionCallsArgs['validateCancelRequest'][0][1]->version); + $this->assertTrue($result->isAjaxResult); + } function test_validateRequestByIntegrationConfig_IgnoreAction() { +$this->setHttpHeaderRequestProvider(); + + $userInQueueservice = new UserInQueueServiceMock(); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); + $r->setAccessible(true); + $r->setValue(null, $userInQueueservice); + + + $var = "some text"; + $integrationConfigString = <<assertTrue($result->actionType =="Ignore"); + $this->assertTrue(count($userInQueueservice->arrayFunctionCallsArgs['getIgnoreActionResult']) == 1); + $this->assertFalse($result->isAjaxResult); + } + function test_validateRequestByIntegrationConfig_IgnoreAction_AjaxCall() + { + $httpRequestProvider = new HttpRequestProviderMock(); + $httpRequestProvider->headerArray = array( + "a" => "b", + "c" => "d", + "x-queueit-ajaxpageurl" => "http%3a%2f%2furl", + "e" => "f"); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'httpRequestProvider'); + $r->setAccessible(true); + $r->setValue(null, $httpRequestProvider); + $userInQueueservice = new UserInQueueServiceMock(); $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); $r->setAccessible(true); @@ -827,13 +1326,15 @@ function test_validateRequestByIntegrationConfig_IgnoreAction() $result = QueueIT\KnownUserV3\SDK\KnownUser::validateRequestByIntegrationConfig("http://test.com?event1=true", "queueIttoken", $integrationConfigString, "customerid", "secretkey"); $this->assertTrue($result->actionType =="Ignore"); - $this->assertTrue(count($userInQueueservice->arrayFunctionCallsArgs['getIgnoreActionResult']) == 1); + $this->assertTrue(count($userInQueueservice->arrayFunctionCallsArgs['getIgnoreActionResult']) == 1); + $this->assertTrue($result->isAjaxResult); } function test_validateRequestByIntegrationConfig_debug() { $userInQueueservice = new UserInQueueServiceMock(); - $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); + $userInQueueservice->validateCancelRequestResult = new QueueIT\KnownUserV3\SDK\RequestValidationResult("Debug", "eventid", "queueid", "http://q.qeuue-it.com", null); $r->setAccessible(true); $r->setValue(null, $userInQueueservice); @@ -916,7 +1417,8 @@ function test_validateRequestByIntegrationConfig_withoutmatch_debug() { $userInQueueservice = new UserInQueueServiceMock(); - $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); + $userInQueueservice->validateCancelRequestResult = new QueueIT\KnownUserV3\SDK\RequestValidationResult("Debug", "eventid", "queueid", "http://q.qeuue-it.com", null); $r->setAccessible(true); $r->setValue(null, $userInQueueservice); @@ -993,7 +1495,8 @@ function test_validateRequestByIntegrationConfig_withoutmatch_debug() function test_validateRequestByIntegrationConfig_notvalidhash_debug() { $userInQueueservice = new UserInQueueServiceMock(); - $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); + $userInQueueservice->validateCancelRequestResult = new QueueIT\KnownUserV3\SDK\RequestValidationResult("Debug", "eventid", "queueid", "http://q.qeuue-it.com", null); $r->setAccessible(true); $r->setValue(null, $userInQueueservice); @@ -1053,7 +1556,8 @@ function test_validateRequestByIntegrationConfig_notvalidhash_debug() } function test_resolveRequestByLocalEventConfig_debug() { - $userInQueueservice = new UserInQueueServiceMock(); + $userInQueueservice = new UserInQueueServiceMock(); + $userInQueueservice->validateQueueRequestResult = new QueueIT\KnownUserV3\SDK\RequestValidationResult("Debug", "eventid", "queueid", "http://q.qeuue-it.com", null); $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); $r->setAccessible(true); $r->setValue(null, $userInQueueservice); @@ -1107,7 +1611,8 @@ function test_resolveRequestByLocalEventConfig_debug() { function test_cancelRequestByLocalConfig_debug() { $userInQueueservice = new UserInQueueServiceMock(); - $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); + $r = new ReflectionProperty('QueueIT\KnownUserV3\SDK\KnownUser', 'userInQueueService'); + $userInQueueservice->validateCancelRequestResult = new QueueIT\KnownUserV3\SDK\RequestValidationResult("Debug", "eventid", "queueid", "http://q.qeuue-it.com", null); $r->setAccessible(true); $r->setValue(null, $userInQueueservice); $httpRequestProvider = new HttpRequestProviderMock(); diff --git a/UserInQueueService.php b/UserInQueueService.php index 7cf46af..9ff6df5 100644 --- a/UserInQueueService.php +++ b/UserInQueueService.php @@ -31,7 +31,7 @@ public function getIgnoreActionResult(); class UserInQueueService implements IUserInQueueService { - const SDK_VERSION = "3.5.0"; + const SDK_VERSION = "3.5.1"; private $userInQueueStateRepository; function __construct(IUserInQueueStateRepository $userInQueueStateRepository) {