diff options
author | diosmosis <diosmosis@users.noreply.github.com> | 2019-05-29 06:54:15 +0300 |
---|---|---|
committer | Matthieu Aubry <mattab@users.noreply.github.com> | 2019-05-29 06:54:15 +0300 |
commit | f8316ae4ee793ef3a3ca6c4996a3a1309a4dc5bb (patch) | |
tree | 4ac8d93a27642f40461dd63afc5d2bbff57ac43e /plugins/UserCountry | |
parent | 337a5577404ee4d21e94728b962f0b9a3b95e251 (diff) |
Default to current ip in UserCountry.getLocationFromIp and add some tests. (#14497)
Diffstat (limited to 'plugins/UserCountry')
-rw-r--r-- | plugins/UserCountry/API.php | 7 | ||||
-rw-r--r-- | plugins/UserCountry/tests/Integration/APITest.php | 33 |
2 files changed, 39 insertions, 1 deletions
diff --git a/plugins/UserCountry/API.php b/plugins/UserCountry/API.php index 34a00a4add..5969d615d2 100644 --- a/plugins/UserCountry/API.php +++ b/plugins/UserCountry/API.php @@ -13,6 +13,7 @@ use Piwik\Archive; use Piwik\Container\StaticContainer; use Piwik\DataTable; use Piwik\Date; +use Piwik\IP; use Piwik\Option; use Piwik\Period; use Piwik\Piwik; @@ -358,10 +359,14 @@ class API extends \Piwik\Plugin\API * @throws Exception * @return array|false */ - public function getLocationFromIP($ip, $provider = false) + public function getLocationFromIP($ip = false, $provider = false) { Piwik::checkUserHasSomeViewAccess(); + if (empty($ip)) { + $ip = IP::getIpFromHeader(); + } + if (empty($provider)) { $provider = LocationProvider::getCurrentProviderId(); } diff --git a/plugins/UserCountry/tests/Integration/APITest.php b/plugins/UserCountry/tests/Integration/APITest.php index 99f8a03973..819830a90e 100644 --- a/plugins/UserCountry/tests/Integration/APITest.php +++ b/plugins/UserCountry/tests/Integration/APITest.php @@ -81,4 +81,37 @@ class APITest extends IntegrationTestCase $locationProvider = LocationProvider\GeoIp\Php::ID; $this->api->setLocationProvider($locationProvider); } + + /** + * @dataProvider getTestDataForGetLocationFromIP + */ + public function test_getLocationFromIP($ipAddress, $expected, $ipAddressHeader = null) + { + if (!empty($ipAddressHeader)) { + $_SERVER['REMOTE_ADDR'] = $ipAddressHeader; + } + + $location = $this->api->getLocationFromIP($ipAddress); + $this->assertEquals($expected, $location); + } + + public function getTestDataForGetLocationFromIP() + { + return [ + ['113.62.1.1', [ + 'country_code' => 'us', + 'continent_code' => 'amn', + 'continent_name' => 'Intl_Continent_amn', + 'country_name' => 'General_Unknown', + 'ip' => '113.62.1.1', + ]], + [null, [ + 'country_code' => 'us', + 'continent_code' => 'amn', + 'continent_name' => 'Intl_Continent_amn', + 'country_name' => 'General_Unknown', + 'ip' => '151.100.101.92', + ], '151.100.101.92'], + ]; + } } |