$CFG->geoip2file = $geoipfile;
}
- public function test_ipv4() {
- $result = iplookup_find_location('192.30.255.112');
-
- $this->assertEquals('array', gettype($result));
- $this->assertEquals('San Francisco', $result['city']);
- $this->assertEquals(-122.3933, $result['longitude'], 'Coordinates are out of accepted tolerance', 0.01);
- $this->assertEquals(37.7697, $result['latitude'], 'Coordinates are out of accepted tolerance', 0.01);
+ /**
+ * Test the format of data returned in the iplookup_find_location function.
+ *
+ * @dataProvider ip_provider
+ * @param string $ip The IP to test
+ */
+ public function test_ip($ip) {
+ $this->setup_geoip2file();
+
+ // Note: The results we get from the iplookup tests are beyond our control.
+ // We used to check a specific IP to a known location, but these have become less reliable and change too
+ // frequently to be used for testing.
+
+ $result = iplookup_find_location($ip);
+
+ $this->assertInternalType('array', $result);
+ $this->assertInternalType('float', $result['latitude']);
+ $this->assertInternalType('float', $result['longitude']);
+ $this->assertInternalType('string', $result['city']);
+ $this->assertInternalType('string', $result['country']);
+ $this->assertInternalType('array', $result['title']);
+ $this->assertInternalType('string', $result['title'][0]);
+ $this->assertInternalType('string', $result['title'][1]);
$this->assertNull($result['error']);
- $this->assertEquals('array', gettype($result['title']));
- $this->assertEquals('San Francisco', $result['title'][0]);
- $this->assertEquals('United States', $result['title'][1]);
}
- public function test_ipv6() {
- // NOTE: these tests can be altered by the geoip dataset, there has been an attempt to get
- // a 'reliable' result.
-
- $result = iplookup_find_location('2607:f010:3fe:fff1::ff:fe00:25');
-
- $this->assertEquals('array', gettype($result));
- $this->assertEquals('Los Angeles', $result['city']);
- $this->assertEquals(-118.2987, $result['longitude'], 'Coordinates are out of accepted tolerance', 0.01);
- $this->assertEquals(33.7866, $result['latitude'], 'Coordinates are out of accepted tolerance', 0.01);
- $this->assertNull($result['error']);
- $this->assertEquals('array', gettype($result['title']));
- $this->assertEquals('Los Angeles', $result['title'][0]);
- $this->assertEquals('United States', $result['title'][1]);
+ /**
+ * Data provider for IP lookup test.
+ *
+ * @return array
+ */
+ public function ip_provider() {
+ return [
+ 'IPv4: Sample suggested by maxmind themselves' => ['24.24.24.24'],
- 'IPv4: github.com' => ['192.30.255.112'],
++ 'IPv4: github.com' => ['192.30.255.112'],
+ 'IPv6: UCLA' => ['2607:f010:3fe:fff1::ff:fe00:25'],
+ ];
}
}