File: LocationProviderTest.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (198 lines) | stat: -rw-r--r-- 6,664 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\GeoIp2\tests\Integration;

use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2;
use Piwik\Plugins\UserCountry\LocationProvider\DefaultProvider;
use Piwik\Plugins\UserCountry\VisitorGeolocator;
use Piwik\Tests\Framework\Fixture;

/**
 * @group GeoIp2
 */
class LocationProviderTest extends \PHPUnit\Framework\TestCase
{
    public function testGeoIP2City()
    {
        $locationProvider = new GeoIp2\Php(['loc' => ['GeoIP2-City.mmdb'], 'isp' => []]);
        $result = $locationProvider->getLocation(['ip' => '194.57.91.215']);

        $this->assertEquals([
            'continent_name' => 'Europe',
            'continent_code' => 'EU',
            'country_code' => 'FR',
            'country_name' => 'France',
            'city_name' => 'Besançon',
            'lat' => 47.25,
            'long' => 6.02,
            'postal_code' => '25000',
            'region_code' => 'BFC',
            'region_name' => 'Bourgogne-Franche-Comte',
        ], $result);
    }

    public function testGeoIP2CityWithoutRegionIsoCode()
    {
        // The IP 99.99.99.99 will only return a region name, based on that the region code should be determined
        $locationProvider = new GeoIp2\Php(['loc' => ['GeoIP2-City.mmdb'], 'isp' => []]);
        $result = $locationProvider->getLocation(['ip' => '99.99.99.99']);

        $this->assertEquals([
            'continent_name' => 'North America',
            'continent_code' => 'NA',
            'country_code' => 'US',
            'country_name' => 'United States',
            'city_name' => 'Englewood Cliffs',
            'lat' => 40.89,
            'long' => -73.95,
            'postal_code' => null,
            'region_code' => 'NJ',
            'region_name' => 'New Jersey',
        ], $result);
    }

    public function testGeoIP2CityWithIncorrectlyPrefixedRegionIsoCode()
    {
        // The IP 88.88.88.88 will return a region code that is prefixed with the country code, e.g. US-NJ instead of NJ
        $locationProvider = new GeoIp2\Php(['loc' => ['GeoIP2-City.mmdb'], 'isp' => []]);
        $result = $locationProvider->getLocation(['ip' => '88.88.88.88']);

        $this->assertEquals([
            'continent_name' => 'North America',
            'continent_code' => 'NA',
            'country_code' => 'US',
            'country_name' => 'United States',
            'city_name' => 'Englewood Cliffs',
            'lat' => 40.89,
            'long' => -73.95,
            'postal_code' => null,
            'region_code' => 'NJ',
            'region_name' => 'New Jersey',
        ], $result);
    }

    public function testGeoIP2Country()
    {
        $locationProvider = new GeoIp2\Php(['loc' => ['GeoIP2-Country.mmdb'], 'isp' => []]);
        $result = $locationProvider->getLocation(['ip' => '194.57.91.215']);

        $this->assertEquals([
            'continent_name' => 'Europe',
            'continent_code' => 'EU',
            'country_code' => 'FR',
            'country_name' => 'France',
        ], $result);
    }

    public function testGeoIP2ASN()
    {
        $locationProvider = new GeoIp2\Php(['loc' => [], 'isp' => ['GeoLite2-ASN.mmdb']]);
        $result = $locationProvider->getLocation(['ip' => '194.57.91.215']);

        $this->assertEquals([
            'isp' => 'Matomo Internet',
            'org' => 'Matomo Internet',
        ], $result);
    }

    public function testGeoIP2ISP()
    {
        $locationProvider = new GeoIp2\Php(['loc' => [], 'isp' => ['GeoIP2-ISP.mmdb']]);
        $result = $locationProvider->getLocation(['ip' => '194.57.91.215']);

        $this->assertEquals([
            'isp' => 'Matomo Internet',
            'org' => 'Innocraft',
        ], $result);
    }

    public function testGeoIP2ISPWhenIspDisabledIspNotReturnsAnyResult()
    {
        $this->setIspEnabled(false);

        $locationProvider = new GeoIp2\Php(['loc' => [], 'isp' => ['GeoIP2-ISP.mmdb']]);
        $result = $locationProvider->getLocation(['ip' => '194.57.91.215']);

        $this->setIspEnabled(true);

        $this->assertFalse($result);
    }

    public function testGeoIP2ISPWhenIspDisabledLocStillReturnsResult()
    {
        $this->setIspEnabled(false);

        $locationProvider = new GeoIp2\Php(['loc' => ['GeoIP2-Country.mmdb'], 'isp' => []]);
        $result = $locationProvider->getLocation(['ip' => '194.57.91.215']);

        $this->setIspEnabled(true);

        $this->assertNotEmpty($result);
    }

    private function setIspEnabled($enabled)
    {
        StaticContainer::getContainer()->set('geopip2.ispEnabled', $enabled);
    }

    public function testGeoIP2CityAndISP()
    {
        $locationProvider = new GeoIp2\Php(['loc' => ['GeoIP2-City.mmdb'], 'isp' => ['GeoIP2-ISP.mmdb']]);
        $result = $locationProvider->getLocation(['ip' => '194.57.91.215']);

        $this->assertEquals([
            'continent_name' => 'Europe',
            'continent_code' => 'EU',
            'country_code' => 'FR',
            'country_name' => 'France',
            'city_name' => 'Besançon',
            'lat' => 47.25,
            'long' => 6.02,
            'postal_code' => '25000',
            'region_code' => 'BFC',
            'region_name' => 'Bourgogne-Franche-Comte',
            'isp' => 'Matomo Internet',
            'org' => 'Innocraft',
        ], $result);
    }

    public function testGeoIP2NoResultFallback()
    {
        Fixture::loadAllTranslations();
        $locationProvider = new GeoIp2\Php(['loc' => ['GeoIP2-City.mmdb'], 'isp' => []]);
        $geolocator = new VisitorGeolocator($locationProvider, new DefaultProvider());

        $result = $geolocator->getLocation(['ip' => '221.0.0.9', 'lang' => 'de-ch'], false);

        $this->assertEquals([
            'country_code' => 'ch',
            'country_name' => 'Switzerland',
            'continent_code' => 'eur',
            'continent_name' => 'Europe',
        ], $result);
    }

    public function testGeoIP2NoResultFallbackDisabled()
    {
        Fixture::loadAllTranslations();
        Config::getInstance()->Tracker['enable_default_location_provider'] = 0;

        $locationProvider = new GeoIp2\Php(['loc' => ['GeoIP2-City.mmdb'], 'isp' => []]);
        $geolocator = new VisitorGeolocator($locationProvider);

        $result = $geolocator->getLocation(['ip' => '221.0.0.9', 'lang' => 'de-ch'], false);

        $this->assertEquals([
            'country_code' => 'xx',
        ], $result);
    }
}