f6970c403cebeb62c99484dd4ec016d2b1df430d
[moodle.git] / iplookup / tests / geoip_test.php
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/>.
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  */
26 defined('MOODLE_INTERNAL') || die();
29 /**
30  * GeoIp data file parsing test.
31  */
32 class core_iplookup_geoip_testcase extends advanced_testcase {
34     public function setUp() {
35         global $CFG;
36         require_once("$CFG->libdir/filelib.php");
37         require_once("$CFG->dirroot/iplookup/lib.php");
39         if (!PHPUNIT_LONGTEST) {
40             // this may take a long time
41             $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
42         }
44         $this->resetAfterTest();
46         // let's store the file somewhere
47         $gzfile = "$CFG->dataroot/phpunit/geoip/GeoLite2-City.mmdb.gz";
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         }
54         if (!file_exists($gzfile)) {
55             download_file_content('http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz',
56                 null, null, false, 300, 20, false, $gzfile);
57         }
59         $this->assertTrue(file_exists($gzfile));
61         $geoipfile = str_replace('.gz', '', $gzfile);
63         // Open our files (in binary mode).
64         $file = gzopen($gzfile, 'rb');
65         $geoipfilebuf = fopen($geoipfile, 'wb');
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         }
74         // Files are done, close files.
75         fclose($geoipfilebuf);
76         gzclose($file);
78         $this->assertTrue(file_exists($geoipfile));
80         $CFG->geoip2file = $geoipfile;
81     }
83     public function test_ipv4() {
85         $result = iplookup_find_location('131.111.150.25');
87         $this->assertEquals('array', gettype($result));
88         $this->assertEquals('Cambridge', $result['city']);
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);
91         $this->assertNull($result['error']);
92         $this->assertEquals('array', gettype($result['title']));
93         $this->assertEquals('Cambridge', $result['title'][0]);
94         $this->assertEquals('United Kingdom', $result['title'][1]);
95     }
97     public function test_ipv6() {
98         // NOTE: these tests can be altered by the geoip dataset, there has been an attempt to get
99         // a 'reliable' result.
101         $result = iplookup_find_location('2607:f010:3fe:fff1::ff:fe00:25');
103         $this->assertEquals('array', gettype($result));
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);
107         $this->assertNull($result['error']);
108         $this->assertEquals('array', gettype($result['title']));
109         $this->assertEquals('Los Angeles', $result['title'][0]);
110         $this->assertEquals('United States', $result['title'][1]);
111     }