Exemplo n.º 1
0
function setupRootContainer()
{
    // before running tests, delete the TestingEnvironmentVariables file, since it can indirectly mess w/
    // phpunit's class loading (if a test class is loaded in bootstrap.php, phpunit can't load it from a file,
    // so executing the tests in a file will fail)
    $vars = new TestingEnvironmentVariables();
    $vars->delete();
    Environment::setGlobalEnvironmentManipulator(new TestingEnvironmentManipulator($vars));
    $rootTestEnvironment = new \Piwik\Application\Environment(null);
    $rootTestEnvironment->init();
}
Exemplo n.º 2
0
 /**
  * We will be simulating an HTTP request here (by including index.php).
  *
  * To avoid weird side-effects (e.g. the logging output messing up the HTTP response on the CLI output)
  * we need to recreate the container with the default environment instead of the CLI environment.
  */
 private function recreateContainerWithWebEnvironment()
 {
     StaticContainer::clearContainer();
     Log::unsetInstance();
     $this->environment = new Environment(null);
     $this->environment->init();
 }
Exemplo n.º 3
0
 protected function initEnvironment(OutputInterface $output)
 {
     try {
         if ($this->environment === null) {
             $this->environment = new Environment('cli');
             $this->environment->init();
         }
         $config = Config::getInstance();
         return $config;
     } catch (\Exception $e) {
         $output->writeln($e->getMessage() . "\n");
     }
 }
Exemplo n.º 4
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     if (!defined('PIWIK_TEST_MODE')) {
         define('PIWIK_TEST_MODE', true);
     }
     Environment::setGlobalEnvironmentManipulator(new TestingEnvironmentManipulator(new TestingEnvironmentVariables()));
     $serverGlobal = $input->getOption('server-global');
     if ($serverGlobal) {
         $_SERVER = json_decode($serverGlobal, true);
     }
     if (Config::getInstance()->database_tests['tables_prefix'] !== '') {
         throw new \Exception("To generate OmniFixture for the UI tests, you must set an empty tables_prefix in [database_tests]");
     }
     $this->requireFixtureFiles($input);
     $this->setIncludePathAsInTestBootstrap();
     $host = Url::getHost();
     if (empty($host)) {
         $host = 'localhost';
         Url::setHost('localhost');
     }
     $configDomainToSave = $input->getOption('save-config');
     if (!empty($configDomainToSave)) {
         $pathToDomainConfig = PIWIK_INCLUDE_PATH . '/config/' . $host . '.config.ini.php';
         if (!file_exists($pathToDomainConfig)) {
             link(PIWIK_INCLUDE_PATH . '/config/config.ini.php', $pathToDomainConfig);
         }
     }
     if ($input->getOption('set-phantomjs-symlinks')) {
         $this->createSymbolicLinksForUITests();
     }
     $fixture = $this->createFixture($input, $allowSave = !empty($configDomainToSave));
     $this->setupDatabaseOverrides($input, $fixture);
     // perform setup and/or teardown
     if ($input->getOption('teardown')) {
         $fixture->getTestEnvironment()->save();
         $fixture->performTearDown();
     } else {
         $fixture->performSetUp();
     }
     $this->writeSuccessMessage($output, array("Fixture successfully setup!"));
     $sqlDumpPath = $input->getOption('sqldump');
     if ($sqlDumpPath) {
         $this->createSqlDump($sqlDumpPath, $output);
     }
     if (!empty($configDomainToSave)) {
         Config::getInstance()->forceSave();
     }
 }
Exemplo n.º 5
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $systemCheck = new SystemCheck();
     $systemCheck->checkRedisIsInstalled();
     $trackerEnvironment = new Environment('tracker');
     $trackerEnvironment->init();
     Log::unsetInstance();
     $trackerEnvironment->getContainer()->get('Piwik\\Access')->setSuperUserAccess(false);
     $trackerEnvironment->getContainer()->get('Piwik\\Plugin\\Manager')->setTrackerPluginsNotToLoad(array('Provider'));
     Tracker::loadTrackerEnvironment();
     if (OutputInterface::VERBOSITY_VERY_VERBOSE <= $output->getVerbosity()) {
         $GLOBALS['PIWIK_TRACKER_DEBUG'] = true;
     }
     $backend = Queue\Factory::makeBackend();
     $queueManager = Queue\Factory::makeQueueManager($backend);
     if (!$queueManager->canAcquireMoreLocks()) {
         $trackerEnvironment->destroy();
         $this->writeSuccessMessage($output, array("Nothing to proccess. Already max number of workers in process."));
         return;
     }
     $shouldProcess = false;
     foreach ($queueManager->getAllQueues() as $queue) {
         if ($queue->shouldProcess()) {
             $shouldProcess = true;
             break;
         }
     }
     if (!$shouldProcess) {
         $trackerEnvironment->destroy();
         $this->writeSuccessMessage($output, array("No queue currently needs processing"));
         return;
     }
     $output->writeln("<info>Starting to process request sets, this can take a while</info>");
     register_shutdown_function(function () use($queueManager) {
         $queueManager->unlock();
     });
     $startTime = microtime(true);
     $processor = new Processor($queueManager);
     $processor->setNumberOfMaxBatchesToProcess(1000);
     $tracker = $processor->process();
     $neededTime = microtime(true) - $startTime;
     $numRequestsTracked = $tracker->getCountOfLoggedRequests();
     $requestsPerSecond = $this->getNumberOfRequestsPerSecond($numRequestsTracked, $neededTime);
     Piwik::postEvent('Tracker.end');
     $trackerEnvironment->destroy();
     $this->writeSuccessMessage($output, array(sprintf('This worker finished queue processing with %sreq/s (%s requests in %02.2f seconds)', $requestsPerSecond, $numRequestsTracked, $neededTime)));
 }
Exemplo n.º 6
0
 private function recreateLogSingleton($backend, $level = 'INFO')
 {
     $newEnv = new Environment('test', array('ini.log.log_writers' => array($backend), 'ini.log.log_level' => $level, 'ini.log.string_message_format' => self::STRING_MESSAGE_FORMAT, 'ini.log.logger_file_path' => self::getLogFileLocation(), 'Psr\\Log\\LoggerInterface' => \DI\get('Monolog\\Logger')));
     $newEnv->init();
     $newMonologLogger = $newEnv->getContainer()->make('Psr\\Log\\LoggerInterface');
     $oldLogger = new Log($newMonologLogger);
     Log::setSingletonInstance($oldLogger);
 }
Exemplo n.º 7
0
 public function destroyEnvironment()
 {
     if ($this->piwikEnvironment === null) {
         return;
     }
     $this->piwikEnvironment->destroy();
     $this->piwikEnvironment = null;
 }
Exemplo n.º 8
0
define('PIWIK_ENABLE_SESSION_START', false);
require_once PIWIK_INCLUDE_PATH . "/index.php";
if (!Common::isPhpCliMode()) {
    return;
}
$testmode = in_array('--testmode', $_SERVER['argv']);
if ($testmode) {
    define('PIWIK_TEST_MODE', true);
    Environment::setGlobalEnvironmentManipulator(new TestingEnvironmentManipulator(new TestingEnvironmentVariables()));
}
function getPiwikDomain()
{
    foreach ($_SERVER['argv'] as $param) {
        $pattern = '--piwik-domain=';
        if (false !== strpos($param, $pattern)) {
            return substr($param, strlen($pattern));
        }
    }
    return null;
}
$environment = new Environment('cli');
$environment->init();
$piwikDomain = getPiwikDomain();
if ($piwikDomain) {
    Url::setHost($piwikDomain);
}
$token = Db::get()->fetchOne("SELECT token_auth\n                              FROM " . Common::prefixTable("user") . "\n                              WHERE superuser_access = 1\n                              ORDER BY date_registered ASC");
$filename = $environment->getContainer()->get('path.tmp') . '/cache/token.php';
$content = "<?php exit; //\t" . $token;
file_put_contents($filename, $content);
echo $filename;
Exemplo n.º 9
0
<?php

use Piwik\Application\Environment;
use Piwik\Tests\Framework\TestingEnvironmentManipulator;
use Piwik\Tests\Framework\TestingEnvironmentVariables;
define('PIWIK_ARCHIVE_NO_TRUNCATE', true);
require realpath(dirname(__FILE__)) . "/includes.php";
Environment::setGlobalEnvironmentManipulator(new TestingEnvironmentManipulator(new TestingEnvironmentVariables()));
// include archive.php, and let 'er rip
require_once PIWIK_INCLUDE_PATH . "/misc/cron/archive.php";
Exemplo n.º 10
0
 *  - Allows to overwrite the Visitor IP, and Server datetime
 *
 */
use Piwik\Application\Environment;
use Piwik\DataTable\Manager;
use Piwik\Option;
use Piwik\Plugins\UserCountry\LocationProvider\GeoIp;
use Piwik\Site;
use Piwik\Tests\Framework\TestingEnvironmentManipulator;
use Piwik\Tests\Framework\TestingEnvironmentVariables;
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 {
    $globalObservers = array(array('Environment.bootstrapped', function () {
        Tracker::setTestEnvironment();
        Manager::getInstance()->deleteAll();
        Option::clearCache();
        Site::clearCache();
    }));
    Environment::setGlobalEnvironmentManipulator(new TestingEnvironmentManipulator(new TestingEnvironmentVariables(), $globalObservers));
    GeoIp::$geoIPDatabaseDir = 'tests/lib/geoip-files';
    include PIWIK_INCLUDE_PATH . '/piwik.php';
} catch (Exception $ex) {
    echo "Unexpected error during tracking: " . $ex->getMessage() . "\n" . $ex->getTraceAsString() . "\n";
}
if (ob_get_level() > 1) {
    ob_end_flush();
}
Exemplo n.º 11
0
 public static function getSegmentsMetadata()
 {
     // Refresh cache for CustomVariables\Model
     Cache::clearCacheGeneral();
     $segments = array();
     $environment = new Environment(null);
     $exception = null;
     try {
         $environment->init();
         $environment->getContainer()->get('Piwik\\Plugin\\Manager')->loadActivatedPlugins();
         foreach (Dimension::getAllDimensions() as $dimension) {
             if ($dimension instanceof CustomVariableName || $dimension instanceof CustomVariableValue) {
                 continue;
                 // added manually below
             }
             foreach ($dimension->getSegments() as $segment) {
                 $segments[] = $segment->getSegment();
             }
         }
         // add CustomVariables manually since the data provider may not have access to the DB
         for ($i = 1; $i != Model::DEFAULT_CUSTOM_VAR_COUNT + 1; ++$i) {
             $segments = array_merge($segments, self::getCustomVariableSegments($i));
         }
         $segments = array_merge($segments, self::getCustomVariableSegments());
     } catch (\Exception $ex) {
         $exception = $ex;
         echo $ex->getMessage() . "\n" . $ex->getTraceAsString() . "\n";
     }
     $environment->destroy();
     if (!empty($exception)) {
         throw $exception;
     }
     return $segments;
 }
Exemplo n.º 12
0
 protected function initEnvironment()
 {
     $this->environment = new Environment($environment = null, $this->provideContainerConfig(), $postBootstrappedEvent = false);
     $this->environment->init();
 }
Exemplo n.º 13
0
 /**
  * The actual task is defined in this method. Here you can access any option or argument that was defined on the
  * command line via $input and write anything to the console via $output argument.
  * In case anything went wrong during the execution you should throw an exception to make sure the user will get a
  * useful error message and to make sure the command does not exit with the status code 0.
  *
  * Ideally, the actual command is quite short as it acts like a controller. It should only receive the input values,
  * execute the task by calling a method of another class and output any useful information.
  *
  * Execute the command like: ./console queuedtracking:test --name="The Piwik Team"
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $systemCheck = new SystemCheck();
     $systemCheck->checkRedisIsInstalled();
     $trackerEnvironment = new Environment('tracker');
     $trackerEnvironment->init();
     Tracker::loadTrackerEnvironment();
     $settings = Queue\Factory::getSettings();
     $output->writeln('<comment>Settings that will be used:</comment>');
     $output->writeln('Host: ' . $settings->redisHost->getValue());
     $output->writeln('Port: ' . $settings->redisPort->getValue());
     $output->writeln('Timeout: ' . $settings->redisTimeout->getValue());
     $output->writeln('Password: '******'Database: ' . $settings->redisDatabase->getValue());
     $output->writeln('NumQueueWorkers: ' . $settings->numQueueWorkers->getValue());
     $output->writeln('NumRequestsToProcess: ' . $settings->numRequestsToProcess->getValue());
     $output->writeln('ProcessDuringTrackingRequest: ' . (int) $settings->processDuringTrackingRequest->getValue());
     $output->writeln('QueueEnabled: ' . (int) $settings->queueEnabled->getValue());
     $output->writeln('');
     $output->writeln('<comment>Version / stats:</comment>');
     $output->writeln('PHP version: ' . phpversion());
     $output->writeln('Uname: ' . php_uname());
     $extension = new \ReflectionExtension('redis');
     $output->writeln('PHPRedis version: ' . $extension->getVersion());
     $backend = Queue\Factory::makeBackend();
     $output->writeln('Redis version: ' . $backend->getServerVersion());
     $output->writeln('Memory: ' . var_export($backend->getMemoryStats(), 1));
     $redis = $backend->getConnection();
     $evictionPolicy = $this->getRedisConfig($redis, 'maxmemory-policy');
     $output->writeln('MaxMemory Eviction Policy config: ' . $evictionPolicy);
     if ($evictionPolicy !== 'allkeys-lru' && $evictionPolicy !== 'noeviction') {
         $output->writeln('<error>The eviction policy can likely lead to errors when memory is low. We recommend to use eviction policy <comment>allkeys-lru</comment> or alternatively <comment>noeviction</comment>. Read more here: http://redis.io/topics/lru-cache</error>');
     }
     $evictionPolicy = $this->getRedisConfig($redis, 'maxmemory');
     $output->writeln('MaxMemory config: ' . $evictionPolicy);
     $output->writeln('');
     $output->writeln('<comment>Performing some tests:</comment>');
     if (method_exists($redis, 'isConnected')) {
         $output->writeln('Redis is connected: ' . (int) $redis->isConnected());
     }
     if ($backend->testConnection()) {
         $output->writeln('Connection works in general');
     } else {
         $output->writeln('Connection does not actually work: ' . $redis->getLastError());
     }
     $this->testRedis($redis, 'set', array('testKey', 'value'), 'testKey', $output);
     $this->testRedis($redis, 'setnx', array('testnxkey', 'value'), 'testnxkey', $output);
     $this->testRedis($redis, 'setex', array('testexkey', 5, 'value'), 'testexkey', $output);
     $this->testRedis($redis, 'set', array('testKeyWithNx', 'value', array('nx')), 'testKeyWithNx', $output);
     $this->testRedis($redis, 'set', array('testKeyWithEx', 'value', array('ex' => 5)), 'testKeyWithEx', $output);
     $backend->delete('foo');
     if (!$backend->setIfNotExists('foo', 'bar', 5)) {
         $output->writeln("setIfNotExists(foo, bar, 1) does not work, most likely we won't be able to acquire a lock:" . $redis->getLastError());
     } else {
         $initialTtl = $redis->ttl('foo');
         if ($initialTtl > 3 && $initialTtl <= 5) {
             $output->writeln('Initial expire seems to be set correctly');
         } else {
             $output->writeln('<error>Initial expire seems to be not set correctly: ' . $initialTtl . ' </error>');
         }
         if ($backend->get('foo') == 'bar') {
             $output->writeln('setIfNotExists works fine');
         } else {
             $output->writeln('There might be a problem with setIfNotExists');
         }
         if ($backend->expireIfKeyHasValue('foo', 'bar', 10)) {
             $output->writeln('expireIfKeyHasValue seems to work fine');
         } else {
             $output->writeln('<error>There might be a problem with expireIfKeyHasValue: ' . $redis->getLastError() . '</error>');
         }
         $extendedTtl = $redis->ttl('foo');
         if ($extendedTtl > 8 && $extendedTtl <= 10) {
             $output->writeln('Extending expire seems to be set correctly');
         } else {
             $output->writeln('<error>Extending expire seems to be not set correctly: ' . $extendedTtl . ' </error>');
         }
         if ($backend->expireIfKeyHasValue('foo', 'invalidValue', 10)) {
             $output->writeln('<error>expireIfKeyHasValue expired a key which it should not have since values does not match</error>');
         } else {
             $output->writeln('expireIfKeyHasValue correctly expires only when the value is correct');
         }
         $extendedTtl = $redis->ttl('foo');
         if ($extendedTtl > 7 && $extendedTtl <= 10) {
             $output->writeln('Expire is still set which is correct');
         } else {
             $output->writeln('<error>Expire missing after a wrong extendExpire: ' . $extendedTtl . ' </error>');
         }
         if ($backend->deleteIfKeyHasValue('foo', 'bar')) {
             $output->writeln('deleteIfKeyHasValue seems to work fine');
         } else {
             $output->writeln('<error>There might be a problem with deleteIfKeyHasValue: ' . $redis->getLastError() . '</error>');
         }
     }
     $redis->delete('fooList');
     $backend->appendValuesToList('fooList', array('value1', 'value2', 'value3'));
     $values = $backend->getFirstXValuesFromList('fooList', 2);
     if ($values == array('value1', 'value2')) {
         $backend->removeFirstXValuesFromList('fooList', 1);
         $backend->removeFirstXValuesFromList('fooList', 1);
         $values = $backend->getFirstXValuesFromList('fooList', 2);
         if ($values == array('value3')) {
             $output->writeln('List feature seems to work fine');
         } else {
             $output->writeln('List feature seems to work only partially: ' . var_export($values, 1));
         }
     } else {
         $output->writeln('<error>List feature seems to not work fine: ' . $redis->getLastError() . '</error>');
     }
     $output->writeln('');
     $output->writeln('<comment>Done</comment>');
 }