Proyectos de Subversion Moodle

Rev

Rev 1 | | 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 geoplugin_test extends \advanced_testcase {
1 efrain 28
 
29
    /**
30
     * Load required test libraries
31
     */
32
    public static function setUpBeforeClass(): void {
33
        global $CFG;
34
        require_once("{$CFG->dirroot}/iplookup/lib.php");
1441 ariadna 35
        parent::setUpBeforeClass();
1 efrain 36
    }
37
 
38
    /**
1441 ariadna 39
     * In order to execute this test:
40
     * - PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
41
     * - GeoPlugin API key should be defined in config.php as TEST_GEOIP_APIKEY
1 efrain 42
     */
43
    public function setUp(): void {
1441 ariadna 44
        global $CFG;
45
        parent::setUp();
1 efrain 46
        if (!PHPUNIT_LONGTEST) {
47
            $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
48
        }
1441 ariadna 49
 
50
        if (!defined('TEST_GEOIP_APIKEY') || empty(TEST_GEOIP_APIKEY)) {
51
            $this->markTestSkipped('External geo tests are disabled.');
52
        }
53
        $CFG->geopluginapikey = TEST_GEOIP_APIKEY;
1 efrain 54
    }
55
 
56
    /**
57
     * Test IPv4 address
58
     *
59
     * @covers ::iplookup_find_location
60
     */
61
    public function test_ipv4(): void {
62
        $result = iplookup_find_location('50.0.184.0');
63
 
64
        $this->assertIsArray($result);
65
        $this->assertIsFloat($result['latitude']);
66
        $this->assertIsFloat($result['longitude']);
67
        $this->assertIsString($result['city']);
68
        $this->assertIsString($result['country']);
69
        $this->assertIsArray($result['title']);
70
        $this->assertIsString($result['title'][0]);
71
        $this->assertIsString($result['title'][1]);
72
        $this->assertNull($result['error']);
73
    }
74
 
75
    /**
76
     * Test IPv6 address (unsupported by Geoplugin)
77
     *
78
     * @covers ::iplookup_find_location
79
     */
80
    public function test_ipv6(): void {
81
        $result = iplookup_find_location('2a01:8900:2:3:8c6c:c0db:3d33:9ce6');
82
        $this->assertEquals($result['error'], get_string('invalidipformat', 'error'));
83
    }
84
}