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] [Versions 401 and 402] [Versions 401 and 403]

   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 quizaccess_ipaddress;
  18  
  19  use quiz;
  20  use quizaccess_ipaddress;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  global $CFG;
  25  require_once($CFG->dirroot . '/mod/quiz/accessrule/ipaddress/rule.php');
  26  
  27  
  28  /**
  29   * Unit tests for the quizaccess_ipaddress plugin.
  30   *
  31   * @package    quizaccess_ipaddress
  32   * @category   test
  33   * @copyright  2008 The Open University
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class rule_test extends \basic_testcase {
  37      public function test_ipaddress_access_rule() {
  38          $quiz = new \stdClass();
  39          $attempt = new \stdClass();
  40          $cm = new \stdClass();
  41          $cm->id = 0;
  42  
  43          // Test the allowed case by getting the user's IP address. However, this
  44          // does not always work, for example using the mac install package on my laptop.
  45          $quiz->subnet = getremoteaddr(null);
  46          if (!empty($quiz->subnet)) {
  47              $quizobj = new quiz($quiz, $cm, null);
  48              $rule = new quizaccess_ipaddress($quizobj, 0);
  49  
  50              $this->assertFalse($rule->prevent_access());
  51              $this->assertFalse($rule->description());
  52              $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
  53              $this->assertFalse($rule->is_finished(0, $attempt));
  54              $this->assertFalse($rule->end_time($attempt));
  55              $this->assertFalse($rule->time_left_display($attempt, 0));
  56          }
  57  
  58          $quiz->subnet = '0.0.0.0';
  59          $quizobj = new quiz($quiz, $cm, null);
  60          $rule = new quizaccess_ipaddress($quizobj, 0);
  61  
  62          $this->assertNotEmpty($rule->prevent_access());
  63          $this->assertEmpty($rule->description());
  64          $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
  65          $this->assertFalse($rule->is_finished(0, $attempt));
  66          $this->assertFalse($rule->end_time($attempt));
  67          $this->assertFalse($rule->time_left_display($attempt, 0));
  68      }
  69  }