Skip to content

Commit 8e94678

Browse files
authored
Fix sorting of Geolocation providers (matomo-org#17835)
* Fix sorting of Geolocation providers * Adds some tests for provider sorting
1 parent 59a25a5 commit 8e94678

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

plugins/UserCountry/LocationProvider.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -311,16 +311,17 @@ public static function getAllProviderInfo($newline = "\n", $includeExtra = false
311311
$info['isVisible'] = $provider->isVisible();
312312
$info['usageWarning'] = $provider->getUsageWarning();
313313

314-
$allInfo[$info['order']] = $info;
314+
$allInfo[$info['id']] = $info;
315315
}
316316

317-
ksort($allInfo);
317+
uasort($allInfo, function($a, $b) {
318+
if ($a['order'] == $b['order']) {
319+
return strcmp($a['id'], $b['id']);
320+
}
321+
return $a['order'] - $b['order'];
322+
});
318323

319-
$result = array();
320-
foreach ($allInfo as $info) {
321-
$result[$info['id']] = $info;
322-
}
323-
return $result;
324+
return $allInfo;
324325
}
325326

326327
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Matomo - free/libre analytics platform
4+
*
5+
* @link https://matomo.org
6+
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7+
*/
8+
9+
namespace Piwik\Plugins\UserCountry\tests\Integration;
10+
11+
use Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2\Php;
12+
use Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2\ServerModule;
13+
use Piwik\Plugins\UserCountry\LocationProvider;
14+
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
15+
16+
/**
17+
* @group UserCountry
18+
* @group LocationProvider
19+
*/
20+
class LocationProviderTest extends IntegrationTestCase
21+
{
22+
public function testGetAllProviderInfo()
23+
{
24+
$allProviders = LocationProvider::getAllProviderInfo();
25+
26+
// We currently have 3 Providers shipped with core
27+
$this->assertSame(3, count($allProviders));
28+
$this->assertEquals(['default', 'geoip2php', 'geoip2server'], array_keys($allProviders));
29+
}
30+
31+
public function testGetAllProviderInfoWithDuplicateOrder()
32+
{
33+
\Piwik\Tests\Framework\Mock\LocationProvider::$locations = [
34+
[
35+
LocationProvider::CITY_NAME_KEY => 'Manchaster',
36+
LocationProvider::REGION_CODE_KEY => '15',
37+
LocationProvider::COUNTRY_CODE_KEY => 'US',
38+
LocationProvider::LATITUDE_KEY => '12',
39+
LocationProvider::LONGITUDE_KEY => '11',
40+
LocationProvider::ISP_KEY => 'Facebook',
41+
],
42+
];
43+
44+
LocationProvider::$providers = [
45+
new LocationProvider\DefaultProvider(),
46+
new Php(),
47+
new ServerModule(),
48+
new \Piwik\Tests\Framework\Mock\LocationProvider(),
49+
];
50+
51+
$allProviders = LocationProvider::getAllProviderInfo();
52+
53+
$this->assertSame(4, count($allProviders));
54+
$this->assertEquals(['default', 'geoip2php', 'mock_provider', 'geoip2server'], array_keys($allProviders));
55+
}
56+
}

tests/PHPUnit/Framework/Mock/LocationProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function getLocation($info)
4545

4646
public function getInfo()
4747
{
48-
return array('id' => self::ID, 'title' => 'mock provider', 'description' => 'mock provider', 'order' => 10);
48+
return array('id' => self::ID, 'title' => 'mock provider', 'description' => 'mock provider', 'order' => 2);
4949
}
5050

5151
public function isAvailable()

0 commit comments

Comments
 (0)