weekly release 3.5dev
[moodle.git] / iplookup / tests / geoip_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_geoip_testcase extends advanced_testcase {
64b0f3ab 33
3fad0f1a 34 public function setUp() {
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 // this may take a long time
3fad0f1a 41 $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
64b0f3ab
PS
42 }
43
44 $this->resetAfterTest();
45
46 // let's store the file somewhere
3fad0f1a 47 $gzfile = "$CFG->dataroot/phpunit/geoip/GeoLite2-City.mmdb.gz";
64b0f3ab
PS
48 check_dir_exists(dirname($gzfile));
49 if (file_exists($gzfile) and (filemtime($gzfile) < time() - 60*60*24*30)) {
50 // delete file if older than 1 month
51 unlink($gzfile);
52 }
53
54 if (!file_exists($gzfile)) {
3fad0f1a
DP
55 download_file_content('http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz',
56 null, null, false, 300, 20, false, $gzfile);
64b0f3ab
PS
57 }
58
59 $this->assertTrue(file_exists($gzfile));
60
3fad0f1a 61 $geoipfile = str_replace('.gz', '', $gzfile);
64b0f3ab 62
3fad0f1a
DP
63 // Open our files (in binary mode).
64 $file = gzopen($gzfile, 'rb');
65 $geoipfilebuf = fopen($geoipfile, 'wb');
66
67 // Keep repeating until the end of the input file.
68 while (!gzeof($file)) {
69 // Read buffer-size bytes.
70 // Both fwrite and gzread and binary-safe.
71 fwrite($geoipfilebuf, gzread($file, 4096));
72 }
73
74 // Files are done, close files.
75 fclose($geoipfilebuf);
76 gzclose($file);
64b0f3ab
PS
77
78 $this->assertTrue(file_exists($geoipfile));
79
3fad0f1a
DP
80 $CFG->geoip2file = $geoipfile;
81 }
82
83 public function test_ipv4() {
64b0f3ab 84
c3076897 85 $result = iplookup_find_location('131.111.150.25');
64b0f3ab
PS
86
87 $this->assertEquals('array', gettype($result));
c3076897 88 $this->assertEquals('Cambridge', $result['city']);
a4a815f3
DP
89 $this->assertEquals(0.1167, $result['longitude'], 'Coordinates are out of accepted tolerance', 0.01);
90 $this->assertEquals(52.2, $result['latitude'], 'Coordinates are out of accepted tolerance', 0.01);
64b0f3ab
PS
91 $this->assertNull($result['error']);
92 $this->assertEquals('array', gettype($result['title']));
c3076897
DP
93 $this->assertEquals('Cambridge', $result['title'][0]);
94 $this->assertEquals('United Kingdom', $result['title'][1]);
64b0f3ab 95 }
3fad0f1a
DP
96
97 public function test_ipv6() {
2fe28e0d
DP
98 // NOTE: these tests can be altered by the geoip dataset, there has been an attempt to get
99 // a 'reliable' result.
3fad0f1a 100
2fe28e0d 101 $result = iplookup_find_location('2607:f010:3fe:fff1::ff:fe00:25');
3fad0f1a
DP
102
103 $this->assertEquals('array', gettype($result));
2fe28e0d
DP
104 $this->assertEquals('Los Angeles', $result['city']);
105 $this->assertEquals(-118.2987, $result['longitude'], 'Coordinates are out of accepted tolerance', 0.01);
106 $this->assertEquals(33.7866, $result['latitude'], 'Coordinates are out of accepted tolerance', 0.01);
3fad0f1a
DP
107 $this->assertNull($result['error']);
108 $this->assertEquals('array', gettype($result['title']));
2fe28e0d
DP
109 $this->assertEquals('Los Angeles', $result['title'][0]);
110 $this->assertEquals('United States', $result['title'][1]);
3fad0f1a 111 }
64b0f3ab
PS
112}
113