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:
authorStefan Giehl <stefan@matomo.org>2021-08-02 00:20:40 +0300
committerGitHub <noreply@github.com>2021-08-02 00:20:40 +0300
commit8e94678be5328cf3a7aa913e1639d5c6127acfd4 (patch)
tree5b4c194608ebc2f93dcb7586aedde3df9c35424b /plugins/UserCountry
parent59a25a5198285cd1a5e1853abddc95b96864a801 (diff)
Fix sorting of Geolocation providers (#17835)
* Fix sorting of Geolocation providers * Adds some tests for provider sorting
Diffstat (limited to 'plugins/UserCountry')
-rw-r--r--plugins/UserCountry/LocationProvider.php15
-rw-r--r--plugins/UserCountry/tests/Integration/LocationProviderTest.php56
2 files changed, 64 insertions, 7 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