Newer
Older
diosmosis
a validé
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
use Piwik\Common;
use Piwik\Config;
use Piwik\Date;
mattab
a validé
use Piwik\Plugins\MobileMessaging\MobileMessaging;
use Piwik\Plugins\ScheduledReports\API as APIScheduledReports;
use Piwik\Plugins\ScheduledReports\ScheduledReports;
use Piwik\Plugins\SitesManager\API as APISitesManager;
mattab
a validé
use Piwik\Plugins\UserCountry\LocationProvider;
use Piwik\Plugins\UsersManager\API as APIUsersManager;
use Piwik\ReportRenderer;
diosmosis
a validé
/**
* Base type for all integration test fixtures. Integration test fixtures
* add visit and related data to the database before a test is run. Different
* tests can use the same fixtures.
diosmosis
a validé
* This class defines a set of helper methods for fixture types. The helper
* methods are public, but ideally they should only be used by fixture types.
diosmosis
a validé
* NOTE: YOU SHOULD NOT CREATE A NEW FIXTURE UNLESS THERE IS NO WAY TO MODIFY
* AN EXISTING FIXTURE TO HANDLE YOUR USE CASE.
diosmosis
a validé
* Related TODO: we should try and reduce the amount of existing fixtures by
* merging some together.
*/
abstract class Test_Piwik_BaseFixture extends PHPUnit_Framework_Assert
{
const IMAGES_GENERATED_ONLY_FOR_OS = 'linux';
const IMAGES_GENERATED_FOR_PHP = '5.5';
const IMAGES_GENERATED_FOR_GD = '2.1.1';
const DEFAULT_SITE_NAME = 'Piwik test';
/** Adds data to Piwik. Creates sites, tracks visits, imports log files, etc. */
public abstract function setUp();
/** Does any clean up. Most of the time there will be no need to clean up. */
public abstract function tearDown();
diosmosis
a validé
/**
* Creates a website, then sets its creation date to a day earlier than specified dateTime
* Useful to create a website now, but force data to be archived back in the past.
*
* @param string $dateTime eg '2010-01-01 12:34:56'
* @param int $ecommerce
* @param string $siteName
diosmosis
a validé
*
* @param bool|string $siteUrl
* @param int $siteSearch
* @param null|string $searchKeywordParameters
* @param null|string $searchCategoryParameters
diosmosis
a validé
* @return int idSite of website created
*/
public static function createWebsite($dateTime, $ecommerce = 0, $siteName = false, $siteUrl = false,
$siteSearch = 1, $searchKeywordParameters = null,
$searchCategoryParameters = null, $timezone = null)
diosmosis
a validé
{
if($siteName === false) {
$siteName = self::DEFAULT_SITE_NAME;
}
$idSite = APISitesManager::getInstance()->addSite(
diosmosis
a validé
$siteName,
$siteUrl === false ? "http://piwik.net/" : $siteUrl,
$ecommerce,
$siteSearch, $searchKeywordParameters, $searchCategoryParameters,
diosmosis
a validé
$ips = null,
$excludedQueryParameters = null,
$timezone,
diosmosis
a validé
$currency = null
);
// Manually set the website creation date to a day earlier than the earliest day we record stats for
Benaka Moorthi
a validé
Db::get()->update(Common::prefixTable("site"),
array('ts_created' => Date::factory($dateTime)->subDay(1)->getDatetime()),
diosmosis
a validé
"idsite = $idSite"
);
// Clear the memory Website cache
diosmosis
a validé
return $idSite;
}
/**
* Returns URL to Piwik root.
*
* @return string
*/
public static function getRootUrl()
{
$piwikUrl = Url::getCurrentUrlWithoutFileName();
$pathBeforeRoot = 'tests';
// Running from a plugin
if (strpos($piwikUrl, 'plugins/') !== false) {
$pathBeforeRoot = 'plugins';
}
$testsInPath = strpos($piwikUrl, $pathBeforeRoot . '/');
if ($testsInPath !== false) {
$piwikUrl = substr($piwikUrl, 0, $testsInPath);
}
return $piwikUrl;
}
/**
* Returns URL to the proxy script, used to ensure piwik.php
* uses the test environment, and allows variable overwriting
*
* @return string
*/
public static function getTrackerUrl()
{
return self::getRootUrl() . 'tests/PHPUnit/proxy/piwik.php';
}
diosmosis
a validé
/**
* Returns a PiwikTracker object that you can then use to track pages or goals.
*
diosmosis
a validé
* @param boolean $defaultInit If set to true, the tracker object will have default IP, user agent, time, resolution, etc.
diosmosis
a validé
*
* @return PiwikTracker
*/
public static function getTracker($idSite, $dateTime, $defaultInit = true, $useLocal = false)
diosmosis
a validé
{
if ($useLocal) {
require_once PIWIK_INCLUDE_PATH . '/tests/LocalTracker.php';
$t = new Piwik_LocalTracker($idSite, self::getTrackerUrl());
} else {
$t = new PiwikTracker($idSite, self::getTrackerUrl());
diosmosis
a validé
}
$t->setForceVisitDateTime($dateTime);
if ($defaultInit) {
$t->setTokenAuth(self::getTokenAuth());
diosmosis
a validé
$t->setIp('156.5.3.2');
// Optional tracking
$t->setUserAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729)");
diosmosis
a validé
$t->setBrowserLanguage('fr');
$t->setLocalTime('12:34:06');
$t->setResolution(1024, 768);
diosmosis
a validé
$t->setBrowserHasCookies(true);
$t->setPlugins($flash = true, $java = true, $director = false);
}
return $t;
}
diosmosis
a validé
* Checks that the response is a GIF image as expected.
* Will fail the test if the response is not the expected GIF
*
* @param $response
*/
public static function checkResponse($response)
{
$trans_gif_64 = "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==";
$expectedResponse = base64_decode($trans_gif_64);
$url = "\n =========================== \n URL was: " . PiwikTracker::$DEBUG_LAST_REQUESTED_URL;
diosmosis
a validé
self::assertEquals($expectedResponse, $response, "Expected GIF beacon, got: <br/>\n"
. var_export($response, true)
. "\n If you are stuck, you can enable \$GLOBALS['PIWIK_TRACKER_DEBUG']=true; in piwik.php to get more debug info."
diosmosis
a validé
. base64_encode($response)
diosmosis
a validé
);
}
/**
* Checks that the response from bulk tracking is a valid JSON
* string. Will fail the test if JSON status is not success.
*
* @param $response
*/
public static function checkBulkTrackingResponse($response) {
$data = json_decode($response, true);
mattab
a validé
if (!is_array($data) || empty($response)) {
throw new Exception("Bulk tracking response (".$response.") is not an array: " . var_export($data, true) . "\n");
}
if(!isset($data['status'])) {
throw new Exception("Returned data didn't have a status: " . var_export($data,true));
Benaka Moorthi
a validé
}
self::assertArrayHasKey('status', $data);
self::assertEquals('success', $data['status']);
}
public static function makeLocation($city, $region, $country, $lat = null, $long = null, $isp = null)
mattab
a validé
return array(LocationProvider::CITY_NAME_KEY => $city,
LocationProvider::REGION_CODE_KEY => $region,
LocationProvider::COUNTRY_CODE_KEY => $country,
LocationProvider::LATITUDE_KEY => $lat,
LocationProvider::LONGITUDE_KEY => $long,
LocationProvider::ISP_KEY => $isp);
}
/**
* Returns the Super User token auth that can be used in tests. Can be used to
* do bulk tracking.
*
* @return string
*/
public static function getTokenAuth()
{
$login = 'admin';
$password = '098f6bcd4621d373cade4e832627b4f6';
$token = APIUsersManager::getInstance()->getTokenAuth($login, $password);
$model = new \Piwik\Plugins\UsersManager\Model();
$user = $model->getUserByTokenAuth($token);
if (empty($user)) {
$model->addUser($login, $password, 'hello@example.org', $login, $token, Date::now()->getDatetime());
}
if (empty($user['superuser_access'])) {
$model->setSuperUserAccess($login, true);
}
return $token;
}
/**
* Create three MAIL and two MOBILE scheduled reports
*
* Reports sent by mail can contain PNG graphs when the user specifies it.
* Depending on the system under test, generated images differ slightly.
* Because of this discrepancy, PNG graphs are only tested if the system under test
* has the characteristics described in 'canImagesBeIncludedInScheduledReports'.
* See tests/README.md for more detail.
*
* @see canImagesBeIncludedInScheduledReports
* @param int $idSite id of website created
*/
public static function setUpScheduledReports($idSite)
{
// fake access is needed so API methods can call Piwik::getCurrentUserLogin(), e.g: 'ScheduledReports.addReport'
$pseudoMockAccess = new FakeAccess;
FakeAccess::$superUser = true;
Access::setSingletonInstance($pseudoMockAccess);
// retrieve available reports
$availableReportMetadata = APIScheduledReports::getReportMetadata($idSite, ScheduledReports::EMAIL_TYPE);
$availableReportIds = array();
foreach ($availableReportMetadata as $reportMetadata) {
$availableReportIds[] = $reportMetadata['uniqueId'];
}
//@review should we also test evolution graphs?
// set-up mail report
APIScheduledReports::getInstance()->addReport(
$idSite,
'Mail Test report',
'day', // overridden in getApiForTestingScheduledReports()
0,
ScheduledReports::EMAIL_TYPE,
ReportRenderer::HTML_FORMAT, // overridden in getApiForTestingScheduledReports()
$availableReportIds,
array(ScheduledReports::DISPLAY_FORMAT_PARAMETER => ScheduledReports::DISPLAY_FORMAT_TABLES_ONLY)
);
// set-up sms report for one website
APIScheduledReports::getInstance()->addReport(
$idSite,
'SMS Test report, one website',
'day', // overridden in getApiForTestingScheduledReports()
0,
mattab
a validé
MobileMessaging::MOBILE_TYPE,
MobileMessaging::SMS_FORMAT,
array("MultiSites_getOne"),
array("phoneNumbers" => array())
);
// set-up sms report for all websites
APIScheduledReports::getInstance()->addReport(
$idSite,
'SMS Test report, all websites',
'day', // overridden in getApiForTestingScheduledReports()
0,
mattab
a validé
MobileMessaging::MOBILE_TYPE,
MobileMessaging::SMS_FORMAT,
array("MultiSites_getAll"),
array("phoneNumbers" => array())
);
if (self::canImagesBeIncludedInScheduledReports()) {
// set-up mail report with images
APIScheduledReports::getInstance()->addReport(
$idSite,
'Mail Test report',
'day', // overridden in getApiForTestingScheduledReports()
0,
ScheduledReports::EMAIL_TYPE,
ReportRenderer::HTML_FORMAT, // overridden in getApiForTestingScheduledReports()
$availableReportIds,
array(ScheduledReports::DISPLAY_FORMAT_PARAMETER => ScheduledReports::DISPLAY_FORMAT_TABLES_AND_GRAPHS)
);
// set-up mail report with one row evolution based png graph
APIScheduledReports::getInstance()->addReport(
$idSite,
'Mail Test report',
'day',
0,
ScheduledReports::EMAIL_TYPE,
ReportRenderer::HTML_FORMAT,
array('Actions_getPageTitles'),
array(
ScheduledReports::DISPLAY_FORMAT_PARAMETER => ScheduledReports::DISPLAY_FORMAT_GRAPHS_ONLY,
ScheduledReports::EVOLUTION_GRAPH_PARAMETER => 'true',
);
}
}
/**
* Return true if system under test has Piwik core team's most common configuration
*/
public static function canImagesBeIncludedInScheduledReports()
{
$gdInfo = gd_info();
return
stristr(php_uname(), self::IMAGES_GENERATED_ONLY_FOR_OS) &&
strpos( $gdInfo['GD Version'], self::IMAGES_GENERATED_FOR_GD) !== false;
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
}
public static $geoIpDbUrl = 'http://piwik-team.s3.amazonaws.com/GeoIP.dat.gz';
public static $geoLiteCityDbUrl = 'http://piwik-team.s3.amazonaws.com/GeoLiteCity.dat.gz';
public static function downloadGeoIpDbs()
{
$geoIpOutputDir = PIWIK_INCLUDE_PATH . '/tests/lib/geoip-files';
self::downloadAndUnzip(self::$geoIpDbUrl, $geoIpOutputDir, 'GeoIP.dat');
self::downloadAndUnzip(self::$geoLiteCityDbUrl, $geoIpOutputDir, 'GeoIPCity.dat');
}
public static function downloadAndUnzip($url, $outputDir, $filename)
{
$bufferSize = 1024 * 1024;
if (!is_dir($outputDir)) {
mkdir($outputDir);
}
$deflatedOut = $outputDir . '/' . $filename;
$outfileName = $deflatedOut . '.gz';
if (file_exists($deflatedOut)) {
return;
}
$dump = fopen($url, 'rb');
$outfile = fopen($outfileName, 'wb');
$bytesRead = 0;
while (!feof($dump)) {
fwrite($outfile, fread($dump, $bufferSize), $bufferSize);
$bytesRead += $bufferSize;
}
fclose($dump);
fclose($outfile);
// unzip the dump
exec("gunzip -c \"" . $outfileName . "\" > \"$deflatedOut\"", $output, $return);
if ($return !== 0) {
\Piwik\Log::info("gunzip failed with file that has following contents:");
\Piwik\Log::info(file_get_contents($outfile));
throw new Exception("gunzip failed($return): " . implode("\n", $output));
}
diosmosis
a validé
}
protected static function executeLogImporter($logFile, $options)
{
$python = \Piwik\SettingsServer::isWindows() ? "C:\Python27\python.exe" : 'python';
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// create the command
$cmd = $python
. ' "' . PIWIK_INCLUDE_PATH . '/misc/log-analytics/import_logs.py" ' # script loc
. '-ddd ' // debug
. '--url="' . self::getRootUrl() . 'tests/PHPUnit/proxy/" ' # proxy so that piwik uses test config files
;
foreach ($options as $name => $value) {
$cmd .= $name;
if ($value !== false) {
$cmd .= '="' . $value . '"';
}
$cmd .= ' ';
}
$cmd .= '"' . $logFile . '" 2>&1';
// run the command
exec($cmd, $output, $result);
if ($result !== 0) {
throw new Exception("log importer failed: " . implode("\n", $output) . "\n\ncommand used: $cmd");
}
return $output;
}
diosmosis
a validé
public static function siteCreated($idSite)
{
return Db::fetchOne("SELECT COUNT(*) FROM " . Common::prefixTable('site') . " WHERE idsite = ?", array($idSite)) != 0;
}
public static function goalExists($idSite, $idGoal)
{
return Db::fetchOne("SELECT COUNT(*) FROM " . Common::prefixTable('goal') . " WHERE idgoal = ? AND idsite = ?", array($idGoal, $idSite)) != 0;
}
}