From 40008178277b364ff5da36a4fc687160384f0241 Mon Sep 17 00:00:00 2001 From: mattab Date: Mon, 15 Dec 2014 15:37:38 +1300 Subject: [PATCH 1/3] adding failing integration test for this issue --- tests/PHPUnit/Integration/TrackerTest.php | 60 ++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/tests/PHPUnit/Integration/TrackerTest.php b/tests/PHPUnit/Integration/TrackerTest.php index 273acbbc5da..9f261074eaa 100644 --- a/tests/PHPUnit/Integration/TrackerTest.php +++ b/tests/PHPUnit/Integration/TrackerTest.php @@ -9,6 +9,7 @@ namespace Piwik\Tests\Integration; use Piwik\Common; +use Piwik\Config; use Piwik\EventDispatcher; use Piwik\Piwik; use Piwik\Plugin; @@ -66,7 +67,10 @@ public function setUp() public function tearDown() { - $this->tracker->disconnectDatabase(); + $this->restoreConfigFile(); + if($this->tracker) { + $this->tracker->disconnectDatabase(); + } EventDispatcher::getInstance()->clearObservers('Tracker.makeNewVisitObject'); if (array_key_exists('PIWIK_TRACKER_DEBUG', $GLOBALS)) { unset($GLOBALS['PIWIK_TRACKER_DEBUG']); @@ -126,13 +130,40 @@ public function test_loadTrackerEnvironment_shouldSetGlobalsDebugVar() public function test_loadTrackerEnvironment_shouldEnableTrackerMode() { + $this->assertTrue(!array_key_exists('PIWIK_TRACKER_DEBUG', $GLOBALS)); + + $this->assertFalse(SettingsServer::isTrackerApiRequest()); + + Tracker::loadTrackerEnvironment(); + + $this->assertTrue(SettingsServer::isTrackerApiRequest()); + } + + public function test_loadTrackerEnvironment_shouldNotThrow_whenConfigNotFound() + { + $this->assertTrue(!array_key_exists('PIWIK_TRACKER_DEBUG', $GLOBALS)); + $this->assertFalse(SettingsServer::isTrackerApiRequest()); + $this->assertTrue(Config::getInstance()->existsLocalConfig()); + + $this->removeConfigFile(); + Config::getInstance()->clear(); + + $this->assertFalse(Config::getInstance()->existsLocalConfig()); + Tracker::loadTrackerEnvironment(); $this->assertTrue(SettingsServer::isTrackerApiRequest()); } + protected function restoreConfigFile() + { + $backupConfig = $this->getLocalConfigPathMoved(); + $localConfig = $this->getLocalConfigPath(); + @shell_exec("mv $backupConfig $localConfig 2> /dev/null"); + } + public function test_isDatabaseConnected_shouldReturnFalse_IfNotConnected() { $this->tracker->disconnectDatabase(); @@ -318,4 +349,31 @@ private function buildRequest($params) return new Request($params); } + /** + * @return string + */ + protected function getLocalConfigPath() + { + return PIWIK_USER_PATH . "/config/config.ini.php "; + } + + /** + * @return string + */ + protected function getLocalConfigPathMoved() + { + return PIWIK_USER_PATH . "/config/tmp-config.ini.php"; + } + + /** + * @return array + */ + protected function removeConfigFile() + { + $localConfig = $this->getLocalConfigPath(); + $backupConfig = $this->getLocalConfigPathMoved(); + shell_exec("mv $localConfig $backupConfig"); + return array($localConfig, $backupConfig); + } + } \ No newline at end of file From 4d0cc39b96a86db5947fb8da897a1d5016ed3d18 Mon Sep 17 00:00:00 2001 From: mattab Date: Mon, 15 Dec 2014 15:38:32 +1300 Subject: [PATCH 2/3] Fixes #6841 - catch the exception when loading tracker environment to prevent error on screen --- piwik.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/piwik.php b/piwik.php index 271825f81a6..edce11447e4 100644 --- a/piwik.php +++ b/piwik.php @@ -67,11 +67,15 @@ require_once PIWIK_INCLUDE_PATH . '/core/Tracker/Request.php'; require_once PIWIK_INCLUDE_PATH . '/core/Cookie.php'; -Tracker::loadTrackerEnvironment(); - session_cache_limiter('nocache'); @date_default_timezone_set('UTC'); +try { + Tracker::loadTrackerEnvironment(); +} catch (Exception $e) { + // eg. Piwik is not installed yet +} + $tracker = new Tracker(); $requestSet = new RequestSet(); From 2e9a262f59e2ae86a1d596cbaa0646af0ef4d9b5 Mon Sep 17 00:00:00 2001 From: mattab Date: Mon, 15 Dec 2014 15:57:18 +1300 Subject: [PATCH 3/3] Proper fix --- core/Tracker.php | 7 ++++++- piwik.php | 6 +----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/core/Tracker.php b/core/Tracker.php index cb7b4494c59..fae00a57efd 100644 --- a/core/Tracker.php +++ b/core/Tracker.php @@ -62,7 +62,12 @@ public function shouldRecordStatistics() public static function loadTrackerEnvironment() { SettingsServer::setIsTrackerApiRequest(); - $GLOBALS['PIWIK_TRACKER_DEBUG'] = (bool) TrackerConfig::getConfigValue('debug'); + try { + $debug = (bool)TrackerConfig::getConfigValue('debug'); + } catch(Exception $e) { + $debug = false; + } + $GLOBALS['PIWIK_TRACKER_DEBUG'] = $debug; PluginManager::getInstance()->loadTrackerPlugins(); } diff --git a/piwik.php b/piwik.php index edce11447e4..cd58b3d526f 100644 --- a/piwik.php +++ b/piwik.php @@ -70,11 +70,7 @@ session_cache_limiter('nocache'); @date_default_timezone_set('UTC'); -try { - Tracker::loadTrackerEnvironment(); -} catch (Exception $e) { - // eg. Piwik is not installed yet -} +Tracker::loadTrackerEnvironment(); $tracker = new Tracker(); $requestSet = new RequestSet();