Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--plugins/UserCountry/API.php34
-rwxr-xr-xtests/PHPUnit/Integration/ManyVisitorsOneWebsiteTest.php55
-rwxr-xr-xtests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getLocationFromIP.xml16
3 files changed, 83 insertions, 22 deletions
diff --git a/plugins/UserCountry/API.php b/plugins/UserCountry/API.php
index b684ebdb41..ab7016eee6 100644
--- a/plugins/UserCountry/API.php
+++ b/plugins/UserCountry/API.php
@@ -156,6 +156,40 @@ class Piwik_UserCountry_API
return $dataTable;
}
+ /**
+ * Uses a location provider to find/guess the location of an IP address.
+ *
+ * See Piwik_UserCountry_LocationProvider::getLocation to see the details
+ * of the result of this function.
+ *
+ * @param string $ip The IP address.
+ * @param string|false $provider The ID of the provider to use or false to use the
+ * currently configured one.
+ */
+ public function getLocationFromIP( $ip, $provider = false )
+ {
+ Piwik::checkUserHasSomeViewAccess();
+
+ if ($provider === false)
+ {
+ $provider = Piwik_UserCountry_LocationProvider::getCurrentProviderId();
+ }
+
+ $oProvider = Piwik_UserCountry_LocationProvider::getProviderById($provider);
+ if ($oProvider === false)
+ {
+ throw new Exception("Cannot find the '$provider' provider. It is either an invalid provider "
+ . "ID or the ID of a provider that is not working.");
+ }
+
+ $location = $oProvider->getLocation(array('ip' => $ip));
+ if (empty($location))
+ {
+ throw new Exception("Could not geolocate '$ip'!");
+ }
+ return $location;
+ }
+
protected function getDataTable($name, $idSite, $period, $date, $segment)
{
Piwik::checkUserHasViewAccess( $idSite );
diff --git a/tests/PHPUnit/Integration/ManyVisitorsOneWebsiteTest.php b/tests/PHPUnit/Integration/ManyVisitorsOneWebsiteTest.php
index 34c7699d8b..62ee6819f0 100755
--- a/tests/PHPUnit/Integration/ManyVisitorsOneWebsiteTest.php
+++ b/tests/PHPUnit/Integration/ManyVisitorsOneWebsiteTest.php
@@ -55,6 +55,8 @@ class Test_Piwik_Integration_ManyVisitorsOneWebsiteTest extends IntegrationTestC
self::trackVisits(2, true);
self::trackOtherVisits();
+
+ self::setLocationProvider('GeoIPCity.dat');
} catch(Exception $e) {
// Skip whole test suite if an error occurs while setup
throw new PHPUnit_Framework_SkippedTestSuiteError($e->getMessage());
@@ -79,34 +81,43 @@ class Test_Piwik_Integration_ManyVisitorsOneWebsiteTest extends IntegrationTestC
public function getApiForTesting()
{
+ $apiToCall = array(
+ 'UserCountry.getCountry', 'UserCountry.getContinent', 'UserCountry.getRegion', 'UserCountry.getCity');
+
return array(
- array('UserCountry', array('idSite' => self::$idSite,
- 'date' => self::$dateTime,
- 'periods' => array('month'))),
-
- array('UserCountry', array('idSite' => self::$idSite,
- 'date' => self::$dateTime,
- 'periods' => array('month'),
- 'testSuffix' => '_segment_region',
- 'segment' => 'region==P3;country==gb')),
-
- array('UserCountry', array('idSite' => self::$idSite,
- 'date' => self::$dateTime,
- 'periods' => array('month'),
- 'testSuffix' => '_segment_city',
- 'segment' => 'city==Stratford-upon-Avon;region==P3;country==gb')),
-
- array('UserCountry', array('idSite' => self::$idSite,
- 'date' => self::$dateTime,
- 'periods' => array('month'),
- 'testSuffix' => '_segment_lat_long',
- 'segment' => 'lat>45;lat<49.3;long>-125;long<-122')),
+ array($apiToCall, array('idSite' => self::$idSite,
+ 'date' => self::$dateTime,
+ 'periods' => array('month'))),
+
+ array($apiToCall, array('idSite' => self::$idSite,
+ 'date' => self::$dateTime,
+ 'periods' => array('month'),
+ 'testSuffix' => '_segment_region',
+ 'segment' => 'region==P3;country==gb')),
+
+ array($apiToCall, array('idSite' => self::$idSite,
+ 'date' => self::$dateTime,
+ 'periods' => array('month'),
+ 'testSuffix' => '_segment_city',
+ 'segment' => 'city==Stratford-upon-Avon;region==P3;country==gb')),
+
+ array($apiToCall, array('idSite' => self::$idSite,
+ 'date' => self::$dateTime,
+ 'periods' => array('month'),
+ 'testSuffix' => '_segment_lat_long',
+ 'segment' => 'lat>45;lat<49.3;long>-125;long<-122')),
array('UserCountry.getCountry', array('idSite' => self::$idSite,
'date' => self::$dateTime,
'periods' => array('month'),
'testSuffix' => '_segment_continent',
- 'segment' => 'continent==eur'))
+ 'segment' => 'continent==eur')),
+
+ array('UserCountry.getLocationFromIP', array('idSite' => self::$idSite,
+ 'date' => self::$dateTime,
+ 'periods' => array('month'),
+ 'otherRequestParameters' => array('ip' => '194.57.91.215')
+ )),
);
}
diff --git a/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getLocationFromIP.xml b/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getLocationFromIP.xml
new file mode 100755
index 0000000000..d32e13e3f2
--- /dev/null
+++ b/tests/PHPUnit/Integration/expected/test_ManyVisitorsOneWebsiteTest__UserCountry.getLocationFromIP.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<result>
+ <row>
+ <country_code>FR</country_code>
+ <region_code>A6</region_code>
+ <city_name>Besançon</city_name>
+ <area_code />
+ <lat>47.249</lat>
+ <long>6.018</long>
+ <postal_code />
+ <continent_code>eur</continent_code>
+ <continent_name>Europe</continent_name>
+ <country_name>France</country_name>
+ <region_name>Franche-Comte</region_name>
+ </row>
+</result> \ No newline at end of file