Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace core;
18
 
19
/**
20
 * GeoIp data file parsing test.
21
 *
22
 * @package    core
23
 * @category   test
24
 * @copyright  2012 Petr Skoda {@link http://skodak.org}
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
1441 ariadna 27
final class geoip_test extends \advanced_testcase {
28
    #[\Override]
29
    public static function setUpBeforeClass(): void {
30
        global $CFG;
31
 
32
        parent::setUpBeforeClass();
33
 
34
        require_once("{$CFG->libdir}/filelib.php");
35
        require_once("{$CFG->dirroot}/iplookup/lib.php");
1 efrain 36
    }
37
 
38
    /**
39
     * Setup the GeoIP2File system.
40
     */
1441 ariadna 41
    public function setup_geoip2file(): void {
1 efrain 42
        global $CFG;
43
        $CFG->geoip2file = "$CFG->dirroot/iplookup/tests/fixtures/GeoIP2-City-Test.mmdb";
44
    }
45
 
46
    /**
47
     * Test the format of data returned in the iplookup_find_location function.
48
     *
49
     * @dataProvider ip_provider
50
     * @param   string  $ip The IP to test
51
     */
11 efrain 52
    public function test_ip($ip): void {
1441 ariadna 53
        global $CFG;
54
        if (!defined('TEST_GEOIP_APIKEY') || empty(TEST_GEOIP_APIKEY)) {
55
            $this->markTestSkipped('External geo tests are disabled.');
56
        }
57
        $this->resetAfterTest();
58
        $CFG->geopluginapikey = TEST_GEOIP_APIKEY;
1 efrain 59
        $this->setup_geoip2file();
60
 
61
        // Note: The results we get from the iplookup tests are beyond our control.
62
        // We used to check a specific IP to a known location, but these have become less reliable and change too
63
        // frequently to be used for testing.
64
 
65
        $result = iplookup_find_location($ip);
66
 
67
        $this->assertIsArray($result);
68
        $this->assertIsFloat($result['latitude']);
69
        $this->assertIsFloat($result['longitude']);
70
        $this->assertIsString($result['city']);
71
        $this->assertIsString($result['country']);
72
        $this->assertIsArray($result['title']);
73
        $this->assertIsString($result['title'][0]);
74
        $this->assertIsString($result['title'][1]);
75
        $this->assertNull($result['error']);
76
    }
77
 
78
    /**
79
     * Data provider for IP lookup test.
80
     *
81
     * @return array
82
     */
1441 ariadna 83
    public static function ip_provider(): array {
1 efrain 84
        return [
85
            'IPv4: IPV4 test' => ['81.2.69.142'],
86
            'IPv6: IPV6 test' => ['2001:252:1::1:1:1'],
87
        ];
88
    }
89
}