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.
   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   * Unit tests for parts of /lib/behat/lib.php.
  19   *
  20   * @package    core
  21   * @category   test
  22   * @copyright  2021 Université Rennes 2 {@link https://www.univ-rennes2.fr}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  /**
  27   * Unit tests for parts of /lib/behat/lib.php.
  28   *
  29   * @package    core
  30   * @category   test
  31   * @copyright  2021 Université Rennes 2 {@link https://www.univ-rennes2.fr}
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class behat_lib_test extends advanced_testcase {
  35  
  36      /**
  37       * Setup function
  38       *
  39       * Skip these tests if behat is not configured.
  40       *
  41       * @return void
  42       */
  43      public function setUp(): void {
  44          global $CFG;
  45  
  46          if (empty($CFG->behat_wwwroot) || empty($CFG->behat_dataroot) || empty($CFG->behat_prefix)) {
  47              $this->markTestSkipped('Behat not configured');
  48          }
  49      }
  50  
  51      /**
  52       * Tests for behat_is_requested_url() function.
  53       *
  54       * @dataProvider url_provider
  55       * @covers ::behat_is_requested_url
  56       *
  57       * @param string $url           URL used with behat_is_requested_url() function.
  58       * @param bool   $expectedvalue Expected value returned by behat_is_requested_url() function.
  59       * @param array  $environment   Values to override $_SERVER global variable.
  60       */
  61      public function test_behat_is_requested_url($url, $expectedvalue, $environment) {
  62          // Save $_SERVER variable.
  63          $server = $_SERVER;
  64  
  65          // Setup $_SERVER variable for test.
  66          list($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['SCRIPT_NAME']) = $environment;
  67  
  68          // Test behat_is_requested_url() function.
  69          $this->assertSame($expectedvalue, behat_is_requested_url($url));
  70  
  71          // Restore $_SERVER variable.
  72          $_SERVER = $server;
  73      }
  74  
  75      /**
  76       * Data provider for test_behat_is_requested_url.
  77       *
  78       * @return array Array of values to test behat_is_requested_url() function.
  79       */
  80      public function url_provider() {
  81          return [
  82              // Tests for common ports.
  83              ['http://behat.moodle.org', true, ['behat.moodle.org', 80, '']],
  84              ['https://behat.moodle.org', true, ['behat.moodle.org', 443, '']],
  85  
  86              // Test for custom port.
  87              ['http://behat.moodle.org:8080', true, ['behat.moodle.org', 8080, '']],
  88  
  89              // Test for url with path.
  90              ['http://behat.moodle.org/behat', true, ['behat.moodle.org', 80, '/behat']],
  91  
  92              // Test for url that does not match with environment.
  93              ['http://behat.moodle.org', false, ['moodle.org', 80, '']],
  94          ];
  95      }
  96  }