Skip to content

Commit

Permalink
Merge pull request matomo-org#6853 from piwik/6841
Browse files Browse the repository at this point in the history
Tracker should not error out when config/config.ini.php is not created yet (eg. during installation)
  • Loading branch information
Matthieu Aubry committed Dec 15, 2014
2 parents f41b9b3 + 2e9a262 commit 4c079db
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
7 changes: 6 additions & 1 deletion core/Tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
4 changes: 2 additions & 2 deletions piwik.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@
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');

Tracker::loadTrackerEnvironment();

$tracker = new Tracker();
$requestSet = new RequestSet();

Expand Down
60 changes: 59 additions & 1 deletion tests/PHPUnit/Integration/TrackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Piwik\Tests\Integration;

use Piwik\Common;
use Piwik\Config;
use Piwik\EventDispatcher;
use Piwik\Piwik;
use Piwik\Plugin;
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}

}

0 comments on commit 4c079db

Please sign in to comment.