2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * GeoIp data file parsing test.
24 * @copyright 2012 Petr Skoda {@link http://skodak.org}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 class geoplugin_test extends \advanced_testcase {
29 public function setUp(): void {
31 require_once("$CFG->libdir/filelib.php");
32 require_once("$CFG->dirroot/iplookup/lib.php");
34 if (!PHPUNIT_LONGTEST) {
35 // we do not want to DDOS their server, right?
36 $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
39 $this->resetAfterTest();
44 public function test_ipv4() {
45 $result = iplookup_find_location('50.0.184.0');
47 $this->assertEquals('array', gettype($result));
48 $this->assertEquals('Santa Rosa', $result['city']);
49 $this->assertEqualsWithDelta(-122.7128, $result['longitude'], 0.1, 'Coordinates are out of accepted tolerance');
50 $this->assertEqualsWithDelta(38.4354, $result['latitude'], 0.1, 'Coordinates are out of accepted tolerance');
51 $this->assertNull($result['error']);
52 $this->assertEquals('array', gettype($result['title']));
53 $this->assertEquals('Santa Rosa', $result['title'][0]);
54 $this->assertEquals('United States', $result['title'][1]);
57 public function test_geoip_ipv6() {
58 $result = iplookup_find_location('2a01:8900:2:3:8c6c:c0db:3d33:9ce6');
60 $this->assertNotNull($result['error']);
61 $this->assertEquals($result['error'], get_string('invalidipformat', 'error'));