Skip to content
Extraits de code Groupes Projets
Valider 6db23447 rédigé par Benaka Moorthi's avatar Benaka Moorthi
Parcourir les fichiers

Fix maxmind downloading mechanism.

parent a2d24124
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -84,7 +84,7 @@ class Piwik_UserCountry_GeoIPAutoUpdater ...@@ -84,7 +84,7 @@ class Piwik_UserCountry_GeoIPAutoUpdater
* from this URL. * from this URL.
* @throws Exception * @throws Exception
*/ */
private function downloadFile($dbType, $url) protected function downloadFile($dbType, $url)
{ {
$ext = Piwik_UserCountry_GeoIPAutoUpdater::getGeoIPUrlExtension($url); $ext = Piwik_UserCountry_GeoIPAutoUpdater::getGeoIPUrlExtension($url);
$zippedFilename = Piwik_UserCountry_LocationProvider_GeoIp::$dbNames[$dbType][0] . '.' . $ext; $zippedFilename = Piwik_UserCountry_LocationProvider_GeoIp::$dbNames[$dbType][0] . '.' . $ext;
...@@ -421,15 +421,34 @@ class Piwik_UserCountry_GeoIPAutoUpdater ...@@ -421,15 +421,34 @@ class Piwik_UserCountry_GeoIPAutoUpdater
{ {
// check for &suffix= query param that is special to MaxMind URLs // check for &suffix= query param that is special to MaxMind URLs
if (preg_match('/suffix=([^&]+)/', $url, $matches)) { if (preg_match('/suffix=([^&]+)/', $url, $matches)) {
return $matches[1]; $ext = $matches[1];
} }
// use basename of url // use basename of url
$filenameParts = explode('.', basename($url), 2); $filenameParts = explode('.', basename($url), 2);
if (count($filenameParts) > 1) { if (count($filenameParts) > 1) {
return end($filenameParts); $ext = end($filenameParts);
} else { } else {
return reset($filenameParts); $ext = reset($filenameParts);
}
self::checkForSupportedArchiveType($ext);
return $ext;
}
/**
* Avoid downloading archive types we don't support. No point in downloading it,
* if we can't unzip it...
*
* @param string $ext The URL file's extension.
*/
private static function checkForSupportedArchiveType($ext)
{
if ($ext != 'tar.gz'
&& $ext != 'gz'
) {
throw new Exception(Piwik_Translate('UserCountry_UnsupportedArchiveType', "'$ext'"));
} }
} }
......
...@@ -69,7 +69,12 @@ class Test_Piwik_UserCountry extends PHPUnit_Framework_Testcase ...@@ -69,7 +69,12 @@ class Test_Piwik_UserCountry extends PHPUnit_Framework_Testcase
} }
} }
// test that redundant checks work /**
* Test that redundant checks work.
*
* @group Plugins
* @group UserCountry
*/
public function testGeoIpUpdaterRedundantChecks() public function testGeoIpUpdaterRedundantChecks()
{ {
Piwik_UserCountry_LocationProvider_GeoIp::$geoIPDatabaseDir = 'tests/lib/geoip-files'; Piwik_UserCountry_LocationProvider_GeoIp::$geoIPDatabaseDir = 'tests/lib/geoip-files';
...@@ -79,7 +84,7 @@ class Test_Piwik_UserCountry extends PHPUnit_Framework_Testcase ...@@ -79,7 +84,7 @@ class Test_Piwik_UserCountry extends PHPUnit_Framework_Testcase
$this->createEmptyISPOrgFiles(); $this->createEmptyISPOrgFiles();
// run redundant checks // run redundant checks
$updater = new Piwik_UserCountry_GeoIPAutoUpdater_publictestRedundantChecks(); $updater = new Piwik_UserCountry_GeoIPAutoUpdater_publictest();
$updater->performRedundantDbChecks(); $updater->performRedundantDbChecks();
// check that files are renamed correctly // check that files are renamed correctly
...@@ -93,6 +98,29 @@ class Test_Piwik_UserCountry extends PHPUnit_Framework_Testcase ...@@ -93,6 +98,29 @@ class Test_Piwik_UserCountry extends PHPUnit_Framework_Testcase
$this->checkBrokenGeoIPState(); $this->checkBrokenGeoIPState();
} }
/**
* @group Plugins
* @group UserCountry
* @dataProvider getInvalidGeoIpUrlsToTest
*/
public function testGeoIpDownloadInvalidUrl($url)
{
$updater = new Piwik_UserCountry_GeoIPAutoUpdater_publictest();
try {
$updater->downloadFile('loc', $url);
$this->fail("Downloading invalid url succeeded!");
} catch (Exception $ex) {
$this->assertEquals("UserCountry_UnsupportedArchiveType", $ex->getMessage());
}
}
public function getInvalidGeoIpUrlsToTest()
{
return array(array("http://localhost/tests/resources/geoip.tar"),
array("http://localhost/tests/resources/geoip.tar.bz2"),
array("http://localhost/tests/resources/geoip.dat"));
}
public function setUp() public function setUp()
{ {
Piwik::$shouldLog = null; Piwik::$shouldLog = null;
...@@ -136,10 +164,15 @@ class Test_Piwik_UserCountry extends PHPUnit_Framework_Testcase ...@@ -136,10 +164,15 @@ class Test_Piwik_UserCountry extends PHPUnit_Framework_Testcase
} }
} }
class Piwik_UserCountry_GeoIPAutoUpdater_publictestRedundantChecks extends Piwik_UserCountry_GeoIPAutoUpdater class Piwik_UserCountry_GeoIPAutoUpdater_publictest extends Piwik_UserCountry_GeoIPAutoUpdater
{ {
public function performRedundantDbChecks() public function performRedundantDbChecks()
{ {
parent::performRedundantDbChecks(); parent::performRedundantDbChecks();
} }
public function downloadFile($type, $url)
{
parent::downloadFile($type, $url);
}
} }
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter