diff options
author | Stefan Giehl <stefan@matomo.org> | 2021-08-02 00:20:40 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-02 00:20:40 +0300 |
commit | 8e94678be5328cf3a7aa913e1639d5c6127acfd4 (patch) | |
tree | 5b4c194608ebc2f93dcb7586aedde3df9c35424b | |
parent | 59a25a5198285cd1a5e1853abddc95b96864a801 (diff) |
Fix sorting of Geolocation providers (#17835)
* Fix sorting of Geolocation providers
* Adds some tests for provider sorting
-rw-r--r-- | plugins/UserCountry/LocationProvider.php | 15 | ||||
-rw-r--r-- | plugins/UserCountry/tests/Integration/LocationProviderTest.php | 56 | ||||
-rw-r--r-- | tests/PHPUnit/Framework/Mock/LocationProvider.php | 2 |
3 files changed, 65 insertions, 8 deletions
diff --git a/plugins/UserCountry/LocationProvider.php b/plugins/UserCountry/LocationProvider.php index 53af2f7447..cbc2117b33 100644 --- a/plugins/UserCountry/LocationProvider.php +++ b/plugins/UserCountry/LocationProvider.php @@ -311,16 +311,17 @@ abstract class LocationProvider $info['isVisible'] = $provider->isVisible(); $info['usageWarning'] = $provider->getUsageWarning(); - $allInfo[$info['order']] = $info; + $allInfo[$info['id']] = $info; } - ksort($allInfo); + uasort($allInfo, function($a, $b) { + if ($a['order'] == $b['order']) { + return strcmp($a['id'], $b['id']); + } + return $a['order'] - $b['order']; + }); - $result = array(); - foreach ($allInfo as $info) { - $result[$info['id']] = $info; - } - return $result; + return $allInfo; } /** diff --git a/plugins/UserCountry/tests/Integration/LocationProviderTest.php b/plugins/UserCountry/tests/Integration/LocationProviderTest.php new file mode 100644 index 0000000000..262317653f --- /dev/null +++ b/plugins/UserCountry/tests/Integration/LocationProviderTest.php @@ -0,0 +1,56 @@ +<?php +/** + * Matomo - free/libre analytics platform + * + * @link https://matomo.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Plugins\UserCountry\tests\Integration; + +use Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2\Php; +use Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2\ServerModule; +use Piwik\Plugins\UserCountry\LocationProvider; +use Piwik\Tests\Framework\TestCase\IntegrationTestCase; + +/** + * @group UserCountry + * @group LocationProvider + */ +class LocationProviderTest extends IntegrationTestCase +{ + public function testGetAllProviderInfo() + { + $allProviders = LocationProvider::getAllProviderInfo(); + + // We currently have 3 Providers shipped with core + $this->assertSame(3, count($allProviders)); + $this->assertEquals(['default', 'geoip2php', 'geoip2server'], array_keys($allProviders)); + } + + public function testGetAllProviderInfoWithDuplicateOrder() + { + \Piwik\Tests\Framework\Mock\LocationProvider::$locations = [ + [ + LocationProvider::CITY_NAME_KEY => 'Manchaster', + LocationProvider::REGION_CODE_KEY => '15', + LocationProvider::COUNTRY_CODE_KEY => 'US', + LocationProvider::LATITUDE_KEY => '12', + LocationProvider::LONGITUDE_KEY => '11', + LocationProvider::ISP_KEY => 'Facebook', + ], + ]; + + LocationProvider::$providers = [ + new LocationProvider\DefaultProvider(), + new Php(), + new ServerModule(), + new \Piwik\Tests\Framework\Mock\LocationProvider(), + ]; + + $allProviders = LocationProvider::getAllProviderInfo(); + + $this->assertSame(4, count($allProviders)); + $this->assertEquals(['default', 'geoip2php', 'mock_provider', 'geoip2server'], array_keys($allProviders)); + } +}
\ No newline at end of file diff --git a/tests/PHPUnit/Framework/Mock/LocationProvider.php b/tests/PHPUnit/Framework/Mock/LocationProvider.php index 3ceb79d01e..f7f8396012 100644 --- a/tests/PHPUnit/Framework/Mock/LocationProvider.php +++ b/tests/PHPUnit/Framework/Mock/LocationProvider.php @@ -45,7 +45,7 @@ class LocationProvider extends CountryLocationProvider public function getInfo() { - return array('id' => self::ID, 'title' => 'mock provider', 'description' => 'mock provider', 'order' => 10); + return array('id' => self::ID, 'title' => 'mock provider', 'description' => 'mock provider', 'order' => 2); } public function isAvailable() |