Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

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