protected function execute(InputInterface $input, OutputInterface $output) { $this->recreateContainerWithWebEnvironment(); $this->initHostAndQueryString($input); if ($this->isTestModeEnabled()) { require_once PIWIK_INCLUDE_PATH . '/tests/PHPUnit/TestingEnvironment.php'; Config::unsetInstance(); StaticContainer::clearContainer(); \Piwik_TestingEnvironment::addHooks(); $indexFile = '/tests/PHPUnit/proxy/'; $this->resetDatabase(); } else { $indexFile = '/'; } $indexFile .= 'index.php'; if (!empty($_GET['pid'])) { $process = new Process($_GET['pid']); if ($process->hasFinished()) { return; } $process->startProcess(); } require_once PIWIK_INCLUDE_PATH . $indexFile; if (!empty($process)) { $process->finishProcess(); } }
<?php /** * Proxy to normal piwik.php, but in testing mode * * - Use the tests database to record Tracking data * - Allows to overwrite the Visitor IP, and Server datetime * */ use Piwik\DataTable\Manager; use Piwik\Option; use Piwik\Plugins\UserCountry\LocationProvider\GeoIp; use Piwik\Site; use Piwik\Tracker\Cache; use Piwik\Tracker; require realpath(dirname(__FILE__)) . "/includes.php"; // Wrapping the request inside ob_start() calls to ensure that the Test // calling us waits for the full request to process before unblocking ob_start(); try { Piwik_TestingEnvironment::addHooks(); GeoIp::$geoIPDatabaseDir = 'tests/lib/geoip-files'; Tracker::setTestEnvironment(); Manager::getInstance()->deleteAll(); Option::clearCache(); Site::clearCache(); include PIWIK_INCLUDE_PATH . '/piwik.php'; } catch (Exception $ex) { echo "Unexpected error during tracking: " . $ex->getMessage() . "\n" . $ex->getTraceAsString() . "\n"; } ob_end_flush();
public static function addHooks() { $testingEnvironment = new Piwik_TestingEnvironment(); if ($testingEnvironment->queryParamOverride) { foreach ($testingEnvironment->queryParamOverride as $key => $value) { $_GET[$key] = $value; } } if ($testingEnvironment->globalsOverride) { foreach ($testingEnvironment->globalsOverride as $key => $value) { $GLOBALS[$key] = $value; } } Config::setSingletonInstance(new Config($testingEnvironment->configFileGlobal, $testingEnvironment->configFileLocal, $testingEnvironment->configFileCommon)); \Piwik\CacheFile::$invalidateOpCacheBeforeRead = true; Piwik::addAction('Access.createAccessSingleton', function ($access) use($testingEnvironment) { if (!$testingEnvironment->testUseRegularAuth) { $access = new Piwik_MockAccess($access); \Piwik\Access::setSingletonInstance($access); } }); if (!$testingEnvironment->dontUseTestConfig) { Piwik::addAction('Config.createConfigSingleton', function (Config $config, &$cache, $local) use($testingEnvironment) { $config->setTestEnvironment($testingEnvironment->configFileLocal, $testingEnvironment->configFileGlobal, $testingEnvironment->configFileCommon); if ($testingEnvironment->configFileLocal) { unset($cache['General']); $config->General['session_save_handler'] = 'dbtable'; } $manager = \Piwik\Plugin\Manager::getInstance(); $pluginsToLoad = $manager->getPluginsToLoadDuringTests(); $config->Plugins = array('Plugins' => $pluginsToLoad); $trackerPluginsToLoad = array_filter($pluginsToLoad, function ($plugin) use($manager) { return $manager->isTrackerPlugin($manager->loadPlugin($plugin)); }); $config->Plugins_Tracker = array('Plugins_Tracker' => $trackerPluginsToLoad); $log = $config->log; $log['log_writers'] = array('file'); $config->log = $log; $manager->unloadPlugins(); // TODO: replace this and below w/ configOverride use if ($testingEnvironment->tablesPrefix) { $cache['database']['tables_prefix'] = $testingEnvironment->tablesPrefix; } if ($testingEnvironment->dbName) { $cache['database']['dbname'] = $testingEnvironment->dbName; } if ($testingEnvironment->configOverride) { $cache = $testingEnvironment->arrayMergeRecursiveDistinct($cache, $testingEnvironment->configOverride); } }); } Piwik::addAction('Request.dispatch', function () { \Piwik\Plugins\CoreVisualizations\Visualizations\Cloud::$debugDisableShuffle = true; \Piwik\Visualization\Sparkline::$enableSparklineImages = false; \Piwik\Plugins\ExampleUI\API::$disableRandomness = true; }); Piwik::addAction('AssetManager.getStylesheetFiles', function (&$stylesheets) { $stylesheets[] = 'tests/resources/screenshot-override/override.css'; }); Piwik::addAction('AssetManager.getJavaScriptFiles', function (&$jsFiles) { $jsFiles[] = 'tests/resources/screenshot-override/override.js'; }); self::addSendMailHook(); Piwik::addAction('Updater.checkForUpdates', function () { try { @\Piwik\Filesystem::deleteAllCacheOnUpdate(); } catch (Exception $ex) { // pass } }); $testingEnvironment->logVariables(); $testingEnvironment->executeSetupTestEnvHook(); }
public function performSetUp($setupEnvironmentOnly = false) { try { if ($this->createConfig) { Config::getInstance()->setTestEnvironment(); } $this->dbName = $this->getDbName(); if ($this->persistFixtureData) { $this->dropDatabaseInSetUp = false; $this->dropDatabaseInTearDown = false; $this->overwriteExisting = false; $this->removeExistingSuperUser = false; Config::getInstance()->database_tests['dbname'] = Config::getInstance()->database['dbname'] = $this->dbName; $this->getTestEnvironment()->dbName = $this->dbName; } if ($this->dbName === false) { // must be after test config is created $this->dbName = Config::getInstance()->database['dbname']; } static::connectWithoutDatabase(); if ($this->dropDatabaseInSetUp || $this->resetPersistedFixture) { $this->dropDatabase(); } DbHelper::createDatabase($this->dbName); DbHelper::disconnectDatabase(); // reconnect once we're sure the database exists Config::getInstance()->database['dbname'] = $this->dbName; Db::createDatabaseObject(); DbHelper::createTables(); \Piwik\Plugin\Manager::getInstance()->unloadPlugins(); } catch (Exception $e) { static::fail("TEST INITIALIZATION FAILED: " . $e->getMessage() . "\n" . $e->getTraceAsString()); } include "DataFiles/SearchEngines.php"; include "DataFiles/Socials.php"; include "DataFiles/Languages.php"; include "DataFiles/Countries.php"; include "DataFiles/Currencies.php"; include "DataFiles/LanguageToCountry.php"; include "DataFiles/Providers.php"; if (!$this->isFixtureSetUp()) { DbHelper::truncateAllTables(); } static::createAccessInstance(); // We need to be SU to create websites for tests Piwik::setUserHasSuperUserAccess(); Cache::deleteTrackerCache(); static::loadAllPlugins(); $_GET = $_REQUEST = array(); $_SERVER['HTTP_REFERER'] = ''; // Make sure translations are loaded to check messages in English if ($this->loadTranslations) { Translate::reloadLanguage('en'); APILanguageManager::getInstance()->setLanguageForUser('superUserLogin', 'en'); } FakeAccess::$superUserLogin = '******'; \Piwik\SettingsPiwik::$cachedKnownSegmentsToArchive = null; \Piwik\CacheFile::$invalidateOpCacheBeforeRead = true; if ($this->configureComponents) { \Piwik\Plugins\PrivacyManager\IPAnonymizer::deactivate(); \Piwik\Plugins\PrivacyManager\DoNotTrackHeaderChecker::deactivate(); } if ($this->createSuperUser) { self::createSuperUser($this->removeExistingSuperUser); } if ($setupEnvironmentOnly) { return; } $this->getTestEnvironment()->save(); $this->getTestEnvironment()->executeSetupTestEnvHook(); Piwik_TestingEnvironment::addSendMailHook(); if ($this->overwriteExisting || !$this->isFixtureSetUp()) { $this->setUp(); $this->markFixtureSetUp(); $this->log("Database {$this->dbName} marked as successfully set up."); } else { $this->log("Using existing database {$this->dbName}."); } }