MDL-75708 reportbuilder: consider stress tests as requiring longtest.
[moodle.git] / iplookup / tests / geoip_test.php
blobb547c186ff1e6059ad3265739427c013b095769b
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.
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 namespace core;
19 defined('MOODLE_INTERNAL') || die();
21 global $CFG;
23 require_once("{$CFG->libdir}/filelib.php");
24 require_once("{$CFG->dirroot}/iplookup/lib.php");
27 /**
28 * GeoIp data file parsing test.
30 * @package core
31 * @category test
32 * @copyright 2012 Petr Skoda {@link http://skodak.org}
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class geoip_test extends \advanced_testcase {
36 public function setUp(): void {
37 $this->resetAfterTest();
40 /**
41 * Setup the GeoIP2File system.
43 public function setup_geoip2file() {
44 global $CFG;
45 $CFG->geoip2file = "$CFG->dirroot/iplookup/tests/fixtures/GeoIP2-City-Test.mmdb";
48 /**
49 * Test the format of data returned in the iplookup_find_location function.
51 * @dataProvider ip_provider
52 * @param string $ip The IP to test
54 public function test_ip($ip) {
55 $this->setup_geoip2file();
57 // Note: The results we get from the iplookup tests are beyond our control.
58 // We used to check a specific IP to a known location, but these have become less reliable and change too
59 // frequently to be used for testing.
61 $result = iplookup_find_location($ip);
63 $this->assertIsArray($result);
64 $this->assertIsFloat($result['latitude']);
65 $this->assertIsFloat($result['longitude']);
66 $this->assertIsString($result['city']);
67 $this->assertIsString($result['country']);
68 $this->assertIsArray($result['title']);
69 $this->assertIsString($result['title'][0]);
70 $this->assertIsString($result['title'][1]);
71 $this->assertNull($result['error']);
74 /**
75 * Data provider for IP lookup test.
77 * @return array
79 public function ip_provider() {
80 return [
81 'IPv4: IPV4 test' => ['81.2.69.142'],
82 'IPv6: IPV6 test' => ['2001:252:1::1:1:1'],