Exemplo n.º 1
0
 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();
         Db::get()->query("SET wait_timeout=28800;");
         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($this->getTestEnvironment(), $this->testCaseClass, $this->extraPluginsToLoad);
     self::updateDatabase();
     self::installAndActivatePlugins();
     $_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}.");
     }
 }
Exemplo n.º 2
0
 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();
 }