Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

   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   */
  27  class geoplugin_test extends \advanced_testcase {
  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");
  35      }
  36  
  37      /**
  38       * In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
  39       */
  40      public function setUp(): void {
  41          if (!PHPUNIT_LONGTEST) {
  42              $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
  43          }
  44      }
  45  
  46      /**
  47       * Test IPv4 address
  48       *
  49       * @covers ::iplookup_find_location
  50       */
  51      public function test_ipv4(): void {
  52          $result = iplookup_find_location('50.0.184.0');
  53  
  54          $this->assertIsArray($result);
  55          $this->assertIsFloat($result['latitude']);
  56          $this->assertIsFloat($result['longitude']);
  57          $this->assertIsString($result['city']);
  58          $this->assertIsString($result['country']);
  59          $this->assertIsArray($result['title']);
  60          $this->assertIsString($result['title'][0]);
  61          $this->assertIsString($result['title'][1]);
  62          $this->assertNull($result['error']);
  63      }
  64  
  65      /**
  66       * Test IPv6 address (unsupported by Geoplugin)
  67       *
  68       * @covers ::iplookup_find_location
  69       */
  70      public function test_ipv6(): void {
  71          $result = iplookup_find_location('2a01:8900:2:3:8c6c:c0db:3d33:9ce6');
  72          $this->assertEquals($result['error'], get_string('invalidipformat', 'error'));
  73      }
  74  }