weekly release 3.10.3+
[moodle.git] / iplookup / tests / geoplugin_test.php
CommitLineData
64b0f3ab
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * GeoIP tests
19 *
20 * @package core_iplookup
21 * @category phpunit
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28
29/**
30 * GeoIp data file parsing test.
31 */
8252b7c2 32class core_iplookup_geoplugin_testcase extends advanced_testcase {
64b0f3ab 33
d81a9480 34 public function setUp(): void {
64b0f3ab
PS
35 global $CFG;
36 require_once("$CFG->libdir/filelib.php");
37 require_once("$CFG->dirroot/iplookup/lib.php");
38
39 if (!PHPUNIT_LONGTEST) {
40 // we do not want to DDOS their server, right?
3fad0f1a 41 $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
64b0f3ab
PS
42 }
43
44 $this->resetAfterTest();
45
46 $CFG->geoipfile = '';
3fad0f1a 47 }
64b0f3ab 48
2cf6b060 49 public function test_ipv4() {
f71c8d96 50 $result = iplookup_find_location('50.0.184.0');
64b0f3ab
PS
51
52 $this->assertEquals('array', gettype($result));
2cf6b060 53 $this->assertEquals('San Francisco', $result['city']);
dd17f6b1
MG
54 $this->assertEqualsWithDelta(-122.3933, $result['longitude'], 0.1, 'Coordinates are out of accepted tolerance');
55 $this->assertEqualsWithDelta(37.7697, $result['latitude'], 0.1, 'Coordinates are out of accepted tolerance');
64b0f3ab
PS
56 $this->assertNull($result['error']);
57 $this->assertEquals('array', gettype($result['title']));
2cf6b060
AN
58 $this->assertEquals('San Francisco', $result['title'][0]);
59 $this->assertEquals('United States', $result['title'][1]);
64b0f3ab 60 }
3fad0f1a
DP
61
62 public function test_geoip_ipv6() {
63 $result = iplookup_find_location('2a01:8900:2:3:8c6c:c0db:3d33:9ce6');
64
65 $this->assertNotNull($result['error']);
66 $this->assertEquals($result['error'], get_string('invalidipformat', 'error'));
67 }
64b0f3ab
PS
68}
69