Merge branch 'MDL-62279-master' of git://github.com/damyon/moodle
[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
215cd2d8
AN
28global $CFG;
29
30require_once("{$CFG->libdir}/filelib.php");
31require_once("{$CFG->dirroot}/iplookup/lib.php");
32
64b0f3ab
PS
33
34/**
35 * GeoIp data file parsing test.
36 */
8252b7c2 37class core_iplookup_geoip_testcase extends advanced_testcase {
3fad0f1a 38 public function setUp() {
64b0f3ab 39 if (!PHPUNIT_LONGTEST) {
215cd2d8 40 // These tests are intensive and required downloads.
3fad0f1a 41 $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
64b0f3ab
PS
42 }
43
44 $this->resetAfterTest();
215cd2d8
AN
45 }
46
64b0f3ab 47
215cd2d8
AN
48 /**
49 * Setup the GeoIP2File system.
50 */
51 public function setup_geoip2file() {
52 global $CFG;
53
54 // Store the file somewhere where it won't be wiped out..
3fad0f1a 55 $gzfile = "$CFG->dataroot/phpunit/geoip/GeoLite2-City.mmdb.gz";
64b0f3ab
PS
56 check_dir_exists(dirname($gzfile));
57 if (file_exists($gzfile) and (filemtime($gzfile) < time() - 60*60*24*30)) {
215cd2d8 58 // Delete file if older than 1 month.
64b0f3ab
PS
59 unlink($gzfile);
60 }
61
62 if (!file_exists($gzfile)) {
3fad0f1a
DP
63 download_file_content('http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz',
64 null, null, false, 300, 20, false, $gzfile);
64b0f3ab
PS
65 }
66
67 $this->assertTrue(file_exists($gzfile));
68
3fad0f1a 69 $geoipfile = str_replace('.gz', '', $gzfile);
64b0f3ab 70
3fad0f1a
DP
71 // Open our files (in binary mode).
72 $file = gzopen($gzfile, 'rb');
73 $geoipfilebuf = fopen($geoipfile, 'wb');
74
75 // Keep repeating until the end of the input file.
76 while (!gzeof($file)) {
77 // Read buffer-size bytes.
78 // Both fwrite and gzread and binary-safe.
79 fwrite($geoipfilebuf, gzread($file, 4096));
80 }
81
82 // Files are done, close files.
83 fclose($geoipfilebuf);
84 gzclose($file);
64b0f3ab
PS
85
86 $this->assertTrue(file_exists($geoipfile));
87
3fad0f1a
DP
88 $CFG->geoip2file = $geoipfile;
89 }
90
215cd2d8
AN
91 /**
92 * Test the format of data returned in the iplookup_find_location function.
93 *
94 * @dataProvider ip_provider
95 * @param string $ip The IP to test
96 */
97 public function test_ip($ip) {
98 $this->setup_geoip2file();
99
100 // Note: The results we get from the iplookup tests are beyond our control.
101 // We used to check a specific IP to a known location, but these have become less reliable and change too
102 // frequently to be used for testing.
103
104 $result = iplookup_find_location($ip);
105
106 $this->assertInternalType('array', $result);
107 $this->assertInternalType('float', $result['latitude']);
108 $this->assertInternalType('float', $result['longitude']);
109 $this->assertInternalType('string', $result['city']);
110 $this->assertInternalType('string', $result['country']);
111 $this->assertInternalType('array', $result['title']);
112 $this->assertInternalType('string', $result['title'][0]);
113 $this->assertInternalType('string', $result['title'][1]);
64b0f3ab 114 $this->assertNull($result['error']);
64b0f3ab 115 }
3fad0f1a 116
215cd2d8
AN
117 /**
118 * Data provider for IP lookup test.
119 *
120 * @return array
121 */
122 public function ip_provider() {
123 return [
124 'IPv4: Sample suggested by maxmind themselves' => ['24.24.24.24'],
83c64f78 125 'IPv4: github.com' => ['192.30.255.112'],
215cd2d8
AN
126 'IPv6: UCLA' => ['2607:f010:3fe:fff1::ff:fe00:25'],
127 ];
3fad0f1a 128 }
64b0f3ab 129}