From 8e94678be5328cf3a7aa913e1639d5c6127acfd4 Mon Sep 17 00:00:00 2001 From: Stefan Giehl Date: Sun, 1 Aug 2021 23:20:40 +0200 Subject: Fix sorting of Geolocation providers (#17835) * Fix sorting of Geolocation providers * Adds some tests for provider sorting --- plugins/UserCountry/LocationProvider.php | 15 +++--- .../tests/Integration/LocationProviderTest.php | 56 ++++++++++++++++++++++ 2 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 plugins/UserCountry/tests/Integration/LocationProviderTest.php (limited to 'plugins/UserCountry') 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 @@ +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 -- cgit v1.2.3