| 1 | 
           efrain | 
           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 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           34 | 
           final class behat_lib_test extends advanced_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           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;
  | 
        
        
           | 1441 | 
           ariadna | 
           45 | 
                   parent::setUp();
  | 
        
        
           | 1 | 
           efrain | 
           46 | 
              | 
        
        
            | 
            | 
           47 | 
                   if (empty($CFG->behat_wwwroot) || empty($CFG->behat_dataroot) || empty($CFG->behat_prefix)) {
  | 
        
        
            | 
            | 
           48 | 
                       $this->markTestSkipped('Behat not configured');
  | 
        
        
            | 
            | 
           49 | 
                   }
  | 
        
        
            | 
            | 
           50 | 
               }
  | 
        
        
            | 
            | 
           51 | 
              | 
        
        
            | 
            | 
           52 | 
               /**
  | 
        
        
            | 
            | 
           53 | 
                * Tests for behat_is_requested_url() function.
  | 
        
        
            | 
            | 
           54 | 
                *
  | 
        
        
            | 
            | 
           55 | 
                * @dataProvider url_provider
  | 
        
        
            | 
            | 
           56 | 
                * @covers ::behat_is_requested_url
  | 
        
        
            | 
            | 
           57 | 
                *
  | 
        
        
            | 
            | 
           58 | 
                * @param string $url           URL used with behat_is_requested_url() function.
  | 
        
        
            | 
            | 
           59 | 
                * @param bool   $expectedvalue Expected value returned by behat_is_requested_url() function.
  | 
        
        
            | 
            | 
           60 | 
                * @param array  $environment   Values to override $_SERVER global variable.
  | 
        
        
            | 
            | 
           61 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           62 | 
               public function test_behat_is_requested_url($url, $expectedvalue, $environment): void {
  | 
        
        
           | 1 | 
           efrain | 
           63 | 
                   // Save $_SERVER variable.
  | 
        
        
            | 
            | 
           64 | 
                   $server = $_SERVER;
  | 
        
        
            | 
            | 
           65 | 
              | 
        
        
            | 
            | 
           66 | 
                   // Setup $_SERVER variable for test.
  | 
        
        
            | 
            | 
           67 | 
                   list($_SERVER['HTTP_HOST'], $_SERVER['SERVER_PORT'], $_SERVER['SCRIPT_NAME']) = $environment;
  | 
        
        
            | 
            | 
           68 | 
              | 
        
        
            | 
            | 
           69 | 
                   // Test behat_is_requested_url() function.
  | 
        
        
            | 
            | 
           70 | 
                   $this->assertSame($expectedvalue, behat_is_requested_url($url));
  | 
        
        
            | 
            | 
           71 | 
              | 
        
        
            | 
            | 
           72 | 
                   // Restore $_SERVER variable.
  | 
        
        
            | 
            | 
           73 | 
                   $_SERVER = $server;
  | 
        
        
            | 
            | 
           74 | 
               }
  | 
        
        
            | 
            | 
           75 | 
              | 
        
        
            | 
            | 
           76 | 
               /**
  | 
        
        
            | 
            | 
           77 | 
                * Data provider for test_behat_is_requested_url.
  | 
        
        
            | 
            | 
           78 | 
                *
  | 
        
        
            | 
            | 
           79 | 
                * @return array Array of values to test behat_is_requested_url() function.
  | 
        
        
            | 
            | 
           80 | 
                */
  | 
        
        
           | 1441 | 
           ariadna | 
           81 | 
               public static function url_provider(): array {
  | 
        
        
           | 1 | 
           efrain | 
           82 | 
                   return [
  | 
        
        
            | 
            | 
           83 | 
                       // Tests for common ports.
  | 
        
        
            | 
            | 
           84 | 
                       ['http://behat.moodle.org', true, ['behat.moodle.org', 80, '']],
  | 
        
        
            | 
            | 
           85 | 
                       ['https://behat.moodle.org', true, ['behat.moodle.org', 443, '']],
  | 
        
        
            | 
            | 
           86 | 
              | 
        
        
            | 
            | 
           87 | 
                       // Test for custom port.
  | 
        
        
            | 
            | 
           88 | 
                       ['http://behat.moodle.org:8080', true, ['behat.moodle.org', 8080, '']],
  | 
        
        
            | 
            | 
           89 | 
              | 
        
        
            | 
            | 
           90 | 
                       // Test for url with path.
  | 
        
        
            | 
            | 
           91 | 
                       ['http://behat.moodle.org/behat', true, ['behat.moodle.org', 80, '/behat']],
  | 
        
        
            | 
            | 
           92 | 
              | 
        
        
            | 
            | 
           93 | 
                       // Test for url that does not match with environment.
  | 
        
        
            | 
            | 
           94 | 
                       ['http://behat.moodle.org', false, ['moodle.org', 80, '']],
  | 
        
        
            | 
            | 
           95 | 
                   ];
  | 
        
        
            | 
            | 
           96 | 
               }
  | 
        
        
            | 
            | 
           97 | 
           }
  |