Newer
Older
}
private function loadExpectedFile($filePath)
{
$result = @file_get_contents($filePath);
if(empty($result))
{
$expectedDir = dirname($filePath);
$this->missingExpectedFiles[] = $filePath;
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
return null;
}
return $result;
}
/**
* Returns an array describing the API methods to call & compare with
* expected output.
*
* The returned array must be of the following format:
* <code>
* array(
* array('SomeAPI.method', array('testOption1' => 'value1', 'testOption2' => 'value2'),
* array(array('SomeAPI.method', 'SomeOtherAPI.method'), array(...)),
* .
* .
* .
* )
* </code>
*
* Valid test options:
* <ul>
* <li><b>testSuffix</b> The suffix added to the test name. Helps determine
* the filename of the expected output.</li>
* <li><b>format</b> The desired format of the output. Defaults to 'xml'.</li>
* <li><b>idSite</b> The id of the website to get data for.</li>
* <li><b>date</b> The date to get data for.</li>
* <li><b>periods</b> The period or periods to get data for. Can be an array.</li>
* <li><b>setDateLastN</b> Flag describing whether to query for a set of
* dates or not.</li>
* <li><b>language</b> The language to use.</li>
* <li><b>segment</b> The segment to use.</li>
* <li><b>visitorId</b> The visitor ID to use.</li>
* <li><b>abandonedCarts</b> Whether to look for abandoned carts or not.</li>
* <li><b>idGoal</b> The goal ID to use.</li>
* <li><b>apiModule</b> The value to use in the apiModule request parameter.</li>
* <li><b>apiAction</b> The value to use in the apiAction request parameter.</li>
* <li><b>otherRequestParameters</b> An array of extra request parameters to use.</li>
* <li><b>disableArchiving</b> Disable archiving before running tests.</li>
* </ul>
*
* All test options are optional, except 'idSite' & 'date'.
*/
public function getApiForTesting() {
return array();
}
/**
* Gets the string prefix used in the name of the expected/processed output files.
*/
public function getOutputPrefix()
{
return str_replace('Test_Piwik_Integration_', '', get_class($this));
}
protected function _setCallableApi($api)
{
if ($api == 'all')
{
self::setApiToCall(array());
self::setApiNotToCall(self::$defaultApiNotToCall);
}
else
{
if (!is_array($api))
{
$api = array($api);
}
self::setApiToCall($api);
if(!in_array('UserCountry.getLocationFromIP', $api)) {
self::setApiNotToCall( array(
'API.getPiwikVersion',
'UserCountry.getLocationFromIP'
));
}
}
}
/**
* Runs API tests.
*/
protected function runApiTests($api, $params)
{
$testName = 'test_' . $this->getOutputPrefix();
$this->missingExpectedFiles = array();
$this->comparisonFailures = array();
$this->_setCallableApi($api);
if (isset($params['disableArchiving']) && $params['disableArchiving'] === true)
{
Piwik_ArchiveProcessing::$forceDisableArchiving = true;
}
else
{
Piwik_ArchiveProcessing::$forceDisableArchiving = false;
}
if (isset($params['language']))
{
$this->changeLanguage($params['language']);
}
$testSuffix = isset($params['testSuffix']) ? $params['testSuffix'] : '';
$requestUrls = $this->_generateApiUrls(
isset($params['format']) ? $params['format'] : 'xml',
isset($params['idSite']) ? $params['idSite'] : false,
isset($params['date']) ? $params['date'] : false,
isset($params['periods']) ? $params['periods'] : false,
isset($params['setDateLastN']) ? $params['setDateLastN'] : false,
isset($params['language']) ? $params['language'] : false,
isset($params['segment']) ? $params['segment'] : false,
isset($params['visitorId']) ? $params['visitorId'] : false,
isset($params['abandonedCarts']) ? $params['abandonedCarts'] : false,
isset($params['idGoal']) ? $params['idGoal'] : false,
isset($params['apiModule']) ? $params['apiModule'] : false,
isset($params['apiAction']) ? $params['apiAction'] : false,
benakamoorthi
a validé
isset($params['otherRequestParameters']) ? $params['otherRequestParameters'] : array(),
isset($params['supertableApi']) ? $params['supertableApi'] : false,
isset($params['fileExtension']) ? $params['fileExtension'] : false);
foreach($requestUrls as $apiId => $requestUrl)
{
$this->_testApiUrl( $testName . $testSuffix, $apiId, $requestUrl);
}
// change the language back to en
if ($this->lastLanguage != 'en')
{
$this->changeLanguage('en');
if (!empty($this->missingExpectedFiles))
{
$expectedDir = dirname(reset($this->missingExpectedFiles));
$this->markTestIncomplete(" ERROR: Could not find expected API output '"
. implode("', '", $this->missingExpectedFiles)
. "'. For new tests, to pass the test, you can copy files from the processed/ directory into"
. " $expectedDir after checking that the output is valid. %s ");
}
// Display as one error all sub-failures
if (!empty($this->comparisonFailures))
{
$messages = '';
$i = 1;
foreach($this->comparisonFailures as $failure) {
$msg = $failure->getMessage();
$msg = strtok($msg, "\n");
$messages .= "\n#" . $i++ . ": " . $msg;
}
$messages .= " \n ";
print($messages);
$first = reset($this->comparisonFailures);
throw $first;
}
}
/**
* changing the language within one request is a bit fancy
* in order to keep the core clean, we need a little hack here
*
* @param string $langId
*/
protected function changeLanguage( $langId )
{
if ($this->lastLanguage != $langId)
{
$_GET['language'] = $langId;
Piwik_Translate::reset();
Piwik_Translate::getInstance()->reloadLanguage($langId);
}
$this->lastLanguage = $langId;
}
/**
* Path where expected/processed output files are stored. Can be overridden.
*/
public function getPathToTestDirectory()
{
return dirname(__FILE__).DIRECTORY_SEPARATOR.'Integration';
}
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
/**
* Returns an array associating table names w/ lists of row data.
*
* @return array
*/
protected static function getDbTablesWithData()
{
$result = array();
foreach (Piwik::getTablesInstalled() as $tableName)
{
$result[$tableName] = Piwik_FetchAll("SELECT * FROM $tableName");
}
return $result;
}
/**
* Truncates all tables then inserts the data in $tables into each
* mapped table.
*
* @param array $tables Array mapping table names with arrays of row data.
*/
protected static function restoreDbTables( $tables )
{
// truncate existing tables
Piwik::truncateAllTables();
// insert data
$existingTables = Piwik::getTablesInstalled();
foreach ($tables as $table => $rows)
{
// create table if it's an archive table
if (strpos($table, 'archive_') !== false && !in_array($table, $existingTables))
{
$tableType = strpos($table, 'archive_numeric') !== false ? 'archive_numeric' : 'archive_blob';
$createSql = Piwik::getTableCreateSql($tableType);
$createSql = str_replace(Piwik_Common::prefixTable($tableType), $table, $createSql);
Piwik_Query($createSql);
}
if (empty($rows))
{
continue;
}
$rowsSql = array();
foreach ($rows as $row)
{
$values = array();
foreach ($row as $name => $value)
{
if (is_null($value))
{
$values[] = 'NULL';
}
else if (is_numeric($value))
{
$values[] = $value;
}
else if (!ctype_print($value))
{
$values[] = "x'".bin2hex(substr($value, 1))."'";
}
else
{
$values[] = "'$value'";
}
}
$rowsSql[] = "(".implode(',', $values).")";
}
$sql = "INSERT INTO $table VALUES ".implode(',', $rowsSql);
Piwik_Query($sql);
}
}
/**
* Drops all archive tables.
*/
public static function deleteArchiveTables()
{
foreach (Piwik::getTablesArchivesInstalled() as $table)
{
Piwik_Query("DROP TABLE IF EXISTS $table");
}
Piwik_TablePartitioning::$tablesAlreadyInstalled = Piwik::getTablesInstalled($forceReload = true);
}
benakamoorthi
a validé
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
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;
try
{
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)
{
throw new Exception("gunzip failed($return): ".implode("\n", $output));
}
}
catch (Exception $ex)
{
self::markTestSkipped(
"Cannot download GeoIp DBs, skipping: ".$ex->getMessage()."\n".$ex->getTraceAsString());
}
}