Merge branch 'MDL-82439_501' of https://github.com/jonof/moodle into MOODLE_501_STABLE
This commit is contained in:
@@ -38,7 +38,11 @@ function iplookup_find_location($ip) {
|
||||
|
||||
if (!empty($CFG->geoip2file) and file_exists($CFG->geoip2file)) {
|
||||
$reader = new GeoIp2\Database\Reader($CFG->geoip2file);
|
||||
$record = $reader->city($ip);
|
||||
try {
|
||||
$record = $reader->city($ip);
|
||||
} catch (GeoIp2\Exception\GeoIp2Exception $e) {
|
||||
$record = null;
|
||||
}
|
||||
|
||||
if (empty($record)) {
|
||||
$info['error'] = get_string('iplookupfailed', 'error', $ip);
|
||||
|
||||
@@ -43,6 +43,27 @@ final class geoip_test extends \advanced_testcase {
|
||||
$CFG->geoip2file = "$CFG->dirroot/iplookup/tests/fixtures/GeoIP2-City-Test.mmdb";
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that iplookup_find_location doesn't throw up GeoIp2 exceptions.
|
||||
*
|
||||
* @covers ::iplookup_find_location
|
||||
*/
|
||||
public function test_not_found_ip(): void {
|
||||
$this->resetAfterTest();
|
||||
$this->setup_geoip2file();
|
||||
|
||||
$result = iplookup_find_location('10.0.0.1');
|
||||
|
||||
$this->assertIsArray($result);
|
||||
$this->assertNull($result['latitude']);
|
||||
$this->assertNull($result['longitude']);
|
||||
$this->assertNull($result['city']);
|
||||
$this->assertNull($result['country']);
|
||||
$this->assertIsArray($result['title']);
|
||||
$this->assertEmpty($result['title']);
|
||||
$this->assertEquals('Cannot find geo information about this IP address 10.0.0.1', $result['error']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the format of data returned in the iplookup_find_location function.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user