Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

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