From 4328fd817349f70a2210f5e4a7fcd233c4e29387 Mon Sep 17 00:00:00 2001 From: mattab Date: Thu, 13 Mar 2014 16:44:59 +1300 Subject: [PATCH] Refs #3632 Small refactor and support parameter forceLargeWindowLookBackForVisitor=1 in tests when replaying tracking logs --- core/Tracker.php | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/core/Tracker.php b/core/Tracker.php index 7c790f09345..787d5c38304 100644 --- a/core/Tracker.php +++ b/core/Tracker.php @@ -165,7 +165,7 @@ static private function updateTrackerConfig($name, $value) protected function initRequests($args) { - $rawData = file_get_contents("php://input"); + $rawData = self::getRawBulkRequest(); if (!empty($rawData)) { $this->usingBulkTracking = strpos($rawData, '"requests"') || strpos($rawData, "'requests'"); if ($this->usingBulkTracking) { @@ -177,7 +177,7 @@ protected function initRequests($args) $this->requests = $args ? $args : (!empty($_GET) || !empty($_POST) ? array($_GET + $_POST) : array()); } - private function authenticateBulkTrackingRequests($rawData) + private static function getRequestsArrayFromBulkRequest($rawData) { $rawData = trim($rawData); $rawData = Common::sanitizeLineBreaks($rawData); @@ -185,14 +185,23 @@ private function authenticateBulkTrackingRequests($rawData) // POST data can be array of string URLs or array of arrays w/ visit info $jsonData = json_decode($rawData, $assoc = true); + $tokenAuth = Common::getRequestVar('token_auth', false, 'string', $jsonData); + + $requests = array(); if (isset($jsonData['requests'])) { - $this->requests = $jsonData['requests']; + $requests = $jsonData['requests']; } - $tokenAuth = Common::getRequestVar('token_auth', false, 'string', $jsonData); + return array( $requests, $tokenAuth); + } + + private function authenticateBulkTrackingRequests($rawData) + { + list($this->requests, $tokenAuth) = $this->getRequestsArrayFromBulkRequest($rawData); if (empty($tokenAuth)) { - throw new Exception("token_auth must be specified when using Bulk Tracking Import. See Tracking Doc"); + throw new Exception( "token_auth must be specified when using Bulk Tracking Import. " + ." See Tracking Doc"); } if (!empty($this->requests)) { @@ -213,7 +222,8 @@ private function authenticateBulkTrackingRequests($rawData) // a Bulk Tracking request that is not authenticated should fail if (!$requestObj->isAuthenticated()) { - throw new Exception(sprintf("token_auth specified does not have Admin permission for idsite=%s", $requestObj->getIdSite())); + throw new Exception(sprintf("token_auth specified does not have Admin permission for idsite=%s", + $requestObj->getIdSite())); } $request = $requestObj; @@ -730,7 +740,8 @@ protected function handleTrackingApi(Request $request) public static function setTestEnvironment($args = null, $requestMethod = null) { if (is_null($args)) { - $args = $_GET + $_POST; + $postData = self::getRequestsArrayFromBulkRequest(self::getRawBulkRequest()); + $args = $_GET + $postData; } if (is_null($requestMethod) && array_key_exists('REQUEST_METHOD', $_SERVER)) { $requestMethod = $_SERVER['REQUEST_METHOD']; @@ -753,7 +764,9 @@ public static function setTestEnvironment($args = null, $requestMethod = null) } // Tests using window_look_back_for_visitor - if (Common::getRequestVar('forceLargeWindowLookBackForVisitor', false, null, $args) == 1) { + if (Common::getRequestVar('forceLargeWindowLookBackForVisitor', false, null, $args) == 1 + // also look for this in bulk requests (see fake_logs_replay.log) + || strpos( $args, '"forceLargeWindowLookBackForVisitor":"1"' ) !== false) { self::updateTrackerConfig('window_look_back_for_visitor', 2678400); } @@ -866,4 +879,12 @@ protected function runScheduledTasksIfAllowed($isAuthenticated) $this->exitWithException($e); } } + + /** + * @return string + */ + protected static function getRawBulkRequest() + { + return file_get_contents("php://input"); + } }