Skip to content

Commit

Permalink
Merge branch 'master' into 5940_testRenamingAndCorrectFolders
Browse files Browse the repository at this point in the history
Conflicts:
	plugins/CoreConsole/Commands/TestsRun.php
  • Loading branch information
tsteur committed Oct 9, 2014
2 parents d73cc85 + 9c32a94 commit 5b79f74
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 102 deletions.
57 changes: 2 additions & 55 deletions core/Tracker.php
Expand Up @@ -44,9 +44,6 @@ class Tracker
const LENGTH_HEX_ID_STRING = 16;
const LENGTH_BINARY_ID = 8;

protected static $forcedDateTime = null;
protected static $forcedIpString = null;

protected static $pluginsNotToLoad = array();
protected static $pluginsToLoad = array();

Expand Down Expand Up @@ -90,21 +87,9 @@ protected function outputAccessControlHeaders()

public function clear()
{
self::$forcedIpString = null;
self::$forcedDateTime = null;
$this->stateValid = self::STATE_NOTHING_TO_NOTICE;
}

public static function setForceIp($ipString)
{
self::$forcedIpString = $ipString;
}

public static function setForceDateTime($dateTime)
{
self::$forcedDateTime = $dateTime;
}

/**
* Do not load the specified plugins (used during testing, to disable Provider plugin)
* @param array $plugins
Expand Down Expand Up @@ -488,15 +473,13 @@ public static function getDatetimeFromTimestamp($timestamp)

/**
* Initialization
* @param Request $request
*/
protected function init(Request $request)
{
$this->loadTrackerPlugins($request);
$this->handleTrackingApi($request);
$this->handleDisabledTracker();
$this->handleEmptyRequest($request);

Common::printDebug("Current datetime: " . date("Y-m-d H:i:s", $request->getCurrentTimestamp()));
}

/**
Expand Down Expand Up @@ -748,29 +731,6 @@ protected function getTokenAuth()
return Common::getRequestVar('token_auth', false);
}

/**
* This method allows to set custom IP + server time + visitor ID, when using Tracking API.
* These two attributes can be only set by the Super User (passing token_auth).
*/
protected function handleTrackingApi(Request $request)
{
if (!$request->isAuthenticated()) {
return;
}

// Custom IP to use for this visitor
$customIp = $request->getParam('cip');
if (!empty($customIp)) {
$this->setForceIp($customIp);
}

// Custom server date time to use
$customDatetime = $request->getParam('cdt');
if (!empty($customDatetime)) {
$this->setForceDateTime($customDatetime);
}
}

public static function setTestEnvironment($args = null, $requestMethod = null)
{
if (is_null($args)) {
Expand Down Expand Up @@ -816,18 +776,6 @@ public static function setTestEnvironment($args = null, $requestMethod = null)
\Piwik\Plugins\PrivacyManager\IPAnonymizer::activate();
}

// Custom IP to use for this visitor
$customIp = Common::getRequestVar('cip', false, null, $args);
if (!empty($customIp)) {
self::setForceIp($customIp);
}

// Custom server date time to use
$customDatetime = Common::getRequestVar('cdt', false, null, $args);
if (!empty($customDatetime)) {
self::setForceDateTime($customDatetime);
}

$pluginsDisabled = array('Provider');

// Disable provider plugin, because it is so slow to do many reverse ip lookups
Expand Down Expand Up @@ -870,8 +818,7 @@ protected function trackRequest($params, $tokenAuth)

try {
if ($this->isVisitValid()) {
$request->setForceDateTime(self::$forcedDateTime);
$request->setForceIp(self::$forcedIpString);
Common::printDebug("Current datetime: " . date("Y-m-d H:i:s", $request->getCurrentTimestamp()));

$visit = $this->getNewVisitObject();
$visit->setRequest($request);
Expand Down
93 changes: 66 additions & 27 deletions core/Tracker/Request.php
Expand Up @@ -35,6 +35,8 @@ class Request

const UNKNOWN_RESOLUTION = 'unknown';

const CUSTOM_TIMESTAMP_DOES_NOT_REQUIRE_TOKENAUTH_WHEN_NEWER_THAN = 14400; // 4 hours

/**
* @param $params
* @param bool|string $tokenAuth
Expand All @@ -47,7 +49,6 @@ public function __construct($params, $tokenAuth = false)
$this->params = $params;
$this->tokenAuth = $tokenAuth;
$this->timestamp = time();
$this->enforcedIp = false;

// When the 'url' and referrer url parameter are not given, we might be in the 'Simple Image Tracker' mode.
// The URL can default to the Referrer, which will be in this case
Expand Down Expand Up @@ -319,13 +320,54 @@ public function getParams()

public function getCurrentTimestamp()
{
$cdt = $this->getCustomTimestamp();
if(!empty($cdt)) {
return $cdt;
}
return $this->timestamp;
}

protected function isTimestampValid($time)
protected function getCustomTimestamp()
{
$cdt = $this->getParam('cdt');
if (empty($cdt)) {
return false;
}
if (!is_numeric($cdt)) {
$cdt = strtotime($cdt);
}
if (!$this->isTimestampValid($cdt, $this->timestamp)) {
Common::printDebug(sprintf("Datetime %s is not valid", date("Y-m-d H:i:m", $cdt)));
return false;
}

// If timestamp in the past, token_auth is required
$timeFromNow = $this->timestamp - $cdt;
$isTimestampRecent = $timeFromNow < self::CUSTOM_TIMESTAMP_DOES_NOT_REQUIRE_TOKENAUTH_WHEN_NEWER_THAN;
if (!$isTimestampRecent) {
if(!$this->isAuthenticated()) {
Common::printDebug(sprintf("Custom timestamp is %s seconds old, requires &token_auth...", $timeFromNow));
Common::printDebug("WARN: Tracker API 'cdt' was used with invalid token_auth");
return false;
}
}
return $cdt;
}

/**
* Returns true if the timestamp is valid ie. timestamp is sometime in the last 10 years and is not in the future.
*
* @param $time int Timestamp to test
* @param $now int Current timestamp
* @return bool
*/
protected function isTimestampValid($time, $now = null)
{
return $time <= $this->getCurrentTimestamp()
&& $time > $this->getCurrentTimestamp() - 10 * 365 * 86400;
if(empty($now)) {
$now = $this->getCurrentTimestamp();
}
return $time <= $now
&& $time > $now - 10 * 365 * 86400;
}

public function getIdSite()
Expand Down Expand Up @@ -521,33 +563,11 @@ public function getVisitorId()

public function getIp()
{
if (!empty($this->enforcedIp)) {
$ipString = $this->enforcedIp;
} else {
$ipString = IP::getIpFromHeader();
}

$ipString = $this->getIpString();
$ip = IP::P2N($ipString);
return $ip;
}

public function setForceIp($ip)
{
if (!empty($ip)) {
$this->enforcedIp = $ip;
}
}

public function setForceDateTime($dateTime)
{
if (!is_numeric($dateTime)) {
$dateTime = strtotime($dateTime);
}
if (!empty($dateTime)) {
$this->timestamp = $dateTime;
}
}

public function getForcedUserId()
{
$userId = $this->getParam('uid');
Expand Down Expand Up @@ -611,4 +631,23 @@ public function getUserIdHashed($userId)
{
return substr( sha1( $userId ), 0, 16);
}

/**
* @return mixed|string
* @throws Exception
*/
private function getIpString()
{
$cip = $this->getParam('cip');

if(empty($cip)) {
return IP::getIpFromHeader();
}

if(!$this->isAuthenticated()) {
Common::printDebug("WARN: Tracker API 'cip' was used with invalid token_auth");
return IP::getIpFromHeader();
}
return $cip;
}
}
1 change: 1 addition & 0 deletions core/Tracker/Visit.php
Expand Up @@ -467,6 +467,7 @@ private function printVisitorInformation()
$debugVisitInfo = $this->visitorInfo;
$debugVisitInfo['idvisitor'] = bin2hex($debugVisitInfo['idvisitor']);
$debugVisitInfo['config_id'] = bin2hex($debugVisitInfo['config_id']);
$debugVisitInfo['location_ip'] = IP::N2P($debugVisitInfo['location_ip']);
Common::printDebug($debugVisitInfo);
}

Expand Down
6 changes: 5 additions & 1 deletion core/testMinimumPhpVersion.php
Expand Up @@ -91,7 +91,11 @@ function Piwik_ExitWithMessage($message, $optionalTrace = false, $optionalLinks
{
if (!headers_sent()) {
header('Content-Type: text/html; charset=utf-8');
header('HTTP/1.1 500 Internal Server Error');

$isInternalServerError = preg_match('/(sql|database|mysql)/i', $message);
if($isInternalServerError) {
header('HTTP/1.1 500 Internal Server Error');
}
}

if ($optionalTrace) {
Expand Down
2 changes: 1 addition & 1 deletion misc/log-analytics/README.md
Expand Up @@ -20,7 +20,7 @@ and will not track bots, static files, or error requests.

If you wish to track all requests the following command would be used:

python /path/to/piwik/misc/log-analytics/import_logs.py --url=http://mysite/piwik/ access.log --idsite=1234 --recorders=4 --enable-http-errors --enable-http-redirects --enable-static --enable-bots
python /path/to/piwik/misc/log-analytics/import_logs.py --url=http://mysite/piwik/ --idsite=1234 --recorders=4 --enable-http-errors --enable-http-redirects --enable-static --enable-bots access.log

## How to import your logs automatically every day?

Expand Down
18 changes: 15 additions & 3 deletions plugins/CoreConsole/Commands/TestsRun.php
Expand Up @@ -90,7 +90,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
private function executeTestFile($testFile, $options, $command, OutputInterface $output)
{
$params = $options . " " . $testFile;
$cmd = sprintf("cd %s/tests/PHPUnit && %s %s", PIWIK_DOCUMENT_ROOT, $command, $params);
$cmd = $this->getCommand($command, $params);
$output->writeln('Executing command: <info>' . $cmd . '</info>');
passthru($cmd);
$output->writeln("");
Expand All @@ -104,11 +104,12 @@ private function executeTestGroups($suite, $groups, $options, $command, OutputIn

foreach ($groups as $group) {
$params = '--group ' . $group . ' ' . str_replace('%group%', $group, $options);

if (!empty($suite)) {
$params .= ' --testsuite ' . $suite;
}

$cmd = sprintf('cd %s/tests/PHPUnit && %s %s', PIWIK_DOCUMENT_ROOT, $command, $params);
$cmd = $this->getCommand($command, $params);
$output->writeln('Executing command: <info>' . $cmd . '</info>');
passthru($cmd);
$output->writeln("");
Expand All @@ -119,4 +120,15 @@ private function getTestsGroups()
{
return array('Core', 'Plugins', 'UI');
}

/**
* @param $command
* @param $params
* @return string
*/
private function getCommand($command, $params)
{
$cmd = sprintf('cd %s/tests/PHPUnit && %s %s', PIWIK_DOCUMENT_ROOT, $command, $params);
return $cmd;
}
}
1 change: 1 addition & 0 deletions plugins/UserSettings/tests/Fixtures/LanguageFixture.php
Expand Up @@ -54,6 +54,7 @@ private function trackVisits() {
$this->dateTime,
$defaultInit = false
);
$tracker->setTokenAuth(self::getTokenAuth());

$hour = 1;
foreach ($this->getBrowserLangs() as $browserLang) {
Expand Down
2 changes: 0 additions & 2 deletions tests/LocalTracker.php
Expand Up @@ -45,8 +45,6 @@ protected function sendRequest($url, $method = 'GET', $data = null, $force = fal

// unset cached values
Cache::$trackerCache = null;
Tracker::setForceIp(null);
Tracker::setForceDateTime(null);

// save some values
$plugins = Config::getInstance()->Plugins['Plugins'];
Expand Down
Expand Up @@ -41,7 +41,7 @@ private function trackVisits()
{
$dateTime = $this->dateTime;
$idSite = $this->idSite;
$t = self::getTracker($idSite, $dateTime, $defaultInit = true, $useThirdPartyCookie = 1);
$t = self::getTracker($idSite, $dateTime, $defaultInit = true);

$t->setUrlReferrer('http://www.google.com/search?q=piwik');
$t->setUrl('http://example.org/foo/bar.html');
Expand Down
Expand Up @@ -63,6 +63,7 @@ private function trackVisits()

// Create a new Tracker object, with different attributes
$t2 = self::getTracker($idSite, $dateTime, $defaultInit = false);
$t2->setTokenAuth(self::getTokenAuth());

// Make sure the ID is different at first
$visitorId2 = $t2->getVisitorId();
Expand Down
1 change: 1 addition & 0 deletions tests/PHPUnit/Integration/Tracker/ActionTest.php
Expand Up @@ -22,6 +22,7 @@
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
* @group Core
* @group ActionTest
*/
class Core_Tracker_ActionTest extends IntegrationTestCase
{
Expand Down
5 changes: 1 addition & 4 deletions tests/PHPUnit/Integration/Tracker/Visit2Test.php
Expand Up @@ -135,6 +135,7 @@ protected function updateExistingVisit($valuesToUpdate)

/**
* @group Core
* @group VisitTest
*/
class VisitTest extends IntegrationTestCase
{
Expand All @@ -157,7 +158,6 @@ public function tearDown()
public function test_handleNewVisitWithoutConversion_shouldTriggerDimensions()
{
$request = new \Piwik\Tracker\Request(array());
$request->setForceIp('127.0.0.1');
$visitor = new \Piwik\Tracker\Visitor($request, '');

$visit = new FakeTrackerVisit($request);
Expand All @@ -180,7 +180,6 @@ public function test_handleNewVisitWithoutConversion_shouldTriggerDimensions()
public function test_handleNewVisitWithConversion_shouldTriggerDimensions()
{
$request = new \Piwik\Tracker\Request(array());
$request->setForceIp('127.0.0.1');
$visitor = new \Piwik\Tracker\Visitor($request, '');

$visit = new FakeTrackerVisit($request);
Expand All @@ -199,7 +198,6 @@ public function test_handleNewVisitWithConversion_shouldTriggerDimensions()
public function test_handleExistingVisitWithoutConversion_shouldTriggerDimensions()
{
$request = new \Piwik\Tracker\Request(array());
$request->setForceIp('127.0.0.1');
$visitor = new \Piwik\Tracker\Visitor($request, '');

$visit = new FakeTrackerVisit($request);
Expand All @@ -223,7 +221,6 @@ public function test_handleExistingVisitWithoutConversion_shouldTriggerDimension
public function test_handleExistingVisitWithConversion_shouldTriggerDimensions()
{
$request = new \Piwik\Tracker\Request(array());
$request->setForceIp('127.0.0.1');
$visitor = new \Piwik\Tracker\Visitor($request, '');

$visit = new FakeTrackerVisit($request);
Expand Down

0 comments on commit 5b79f74

Please sign in to comment.