MDL-67593 core_iplookup: use MaxMind test database in unit tests
[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 $this->resetAfterTest();
215cd2d8
AN
40 }
41
215cd2d8
AN
42 /**
43 * Setup the GeoIP2File system.
44 */
45 public function setup_geoip2file() {
46 global $CFG;
b145fe21 47 $CFG->geoip2file = "$CFG->dirroot/iplookup/tests/fixtures/GeoIP2-City-Test.mmdb";
3fad0f1a
DP
48 }
49
215cd2d8
AN
50 /**
51 * Test the format of data returned in the iplookup_find_location function.
52 *
53 * @dataProvider ip_provider
54 * @param string $ip The IP to test
55 */
56 public function test_ip($ip) {
57 $this->setup_geoip2file();
58
59 // Note: The results we get from the iplookup tests are beyond our control.
60 // We used to check a specific IP to a known location, but these have become less reliable and change too
61 // frequently to be used for testing.
62
63 $result = iplookup_find_location($ip);
64
65 $this->assertInternalType('array', $result);
66 $this->assertInternalType('float', $result['latitude']);
67 $this->assertInternalType('float', $result['longitude']);
68 $this->assertInternalType('string', $result['city']);
69 $this->assertInternalType('string', $result['country']);
70 $this->assertInternalType('array', $result['title']);
71 $this->assertInternalType('string', $result['title'][0]);
72 $this->assertInternalType('string', $result['title'][1]);
64b0f3ab 73 $this->assertNull($result['error']);
64b0f3ab 74 }
3fad0f1a 75
215cd2d8
AN
76 /**
77 * Data provider for IP lookup test.
78 *
79 * @return array
80 */
81 public function ip_provider() {
82 return [
b145fe21
JD
83 'IPv4: IPV4 test' => ['81.2.69.142'],
84 'IPv6: IPV6 test' => ['2001:252:1::1:1:1'],
215cd2d8 85 ];
3fad0f1a 86 }
64b0f3ab 87}