Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 403] [Versions 39 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 core;
  18  
  19  use environment_results;
  20  
  21  /**
  22   * Moodle environment test.
  23   *
  24   * @package    core
  25   * @category   phpunit
  26   * @copyright  2013 Petr Skoda {@link http://skodak.org}
  27   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  class environment_test extends \advanced_testcase {
  30  
  31      /**
  32       * Test the environment check status.
  33       */
  34      public function test_environment_check_status() {
  35          global $CFG;
  36          require_once($CFG->libdir.'/environmentlib.php');
  37  
  38          $results = check_moodle_environment(normalize_version($CFG->release), ENV_SELECT_RELEASE);
  39  
  40          // The first element of the results array contains the environment check status.
  41          $status = reset($results);
  42          $this->assertTrue($status);
  43      }
  44  
  45      /**
  46       * Data provider for Moodle environment check tests.
  47       *
  48       * @return array
  49       */
  50      public function environment_provider() {
  51          global $CFG;
  52          require_once($CFG->libdir.'/environmentlib.php');
  53  
  54          $results = check_moodle_environment(normalize_version($CFG->release), ENV_SELECT_RELEASE);
  55          // The second element of the results array contains the list of environment results.
  56          $environmentresults = end($results);
  57          return array_map(function($result) {
  58              return [$result];
  59          }, $environmentresults);
  60      }
  61  
  62      /**
  63       * Test the environment.
  64       *
  65       * @dataProvider environment_provider
  66       * @param environment_results $result
  67       */
  68      public function test_environment($result) {
  69          $sslmessages = ['ssl/tls configuration not supported', 'invalid ssl/tls configuration'];
  70  
  71          if ($result->part === 'php_setting'
  72                  && $result->info === 'opcache.enable'
  73                  && $result->getLevel() === 'optional'
  74                  && $result->getStatus() === false) {
  75              $this->markTestSkipped('OPCache extension is not necessary for unit testing.');
  76          }
  77  
  78          if ($result->part === 'custom_check'
  79                  && $result->getLevel() === 'optional'
  80                  && $result->getStatus() === false) {
  81              if (in_array($result->info, $sslmessages)) {
  82                  $this->markTestSkipped('Up-to-date TLS libraries are not necessary for unit testing.');
  83              }
  84              if ($result->info === 'php not 64 bits' && PHP_INT_SIZE == 4) {
  85                  // If we're on a 32-bit system, skip 64-bit check. 32-bit PHP has PHP_INT_SIZE set to 4.
  86                  $this->markTestSkipped('64-bit check is not necessary for unit testing.');
  87              }
  88          }
  89          $info = "{$result->part}:{$result->info}";
  90          $this->assertTrue($result->getStatus(), "Problem detected in environment ($info), fix all warnings and errors!");
  91      }
  92  
  93      /**
  94       * Test the get_list_of_environment_versions() function.
  95       */
  96      public function test_get_list_of_environment_versions() {
  97          global $CFG;
  98          require_once($CFG->libdir.'/environmentlib.php');
  99          // Build a sample xmlised environment.xml.
 100          $xml = <<<END
 101  <COMPATIBILITY_MATRIX>
 102      <MOODLE version="1.9">
 103          <PHP_EXTENSIONS>
 104              <PHP_EXTENSION name="xsl" level="required" />
 105          </PHP_EXTENSIONS>
 106      </MOODLE>
 107      <MOODLE version="2.5">
 108          <PHP_EXTENSIONS>
 109              <PHP_EXTENSION name="xsl" level="required" />
 110          </PHP_EXTENSIONS>
 111      </MOODLE>
 112      <MOODLE version="2.6">
 113          <PHP_EXTENSIONS>
 114              <PHP_EXTENSION name="xsl" level="required" />
 115          </PHP_EXTENSIONS>
 116      </MOODLE>
 117      <MOODLE version="2.7">
 118          <PHP_EXTENSIONS>
 119              <PHP_EXTENSION name="xsl" level="required" />
 120          </PHP_EXTENSIONS>
 121      </MOODLE>
 122      <PLUGIN name="block_test">
 123          <PHP_EXTENSIONS>
 124              <PHP_EXTENSION name="xsl" level="required" />
 125          </PHP_EXTENSIONS>
 126      </PLUGIN>
 127  </COMPATIBILITY_MATRIX>
 128  END;
 129          $environemt = xmlize($xml);
 130          $versions = get_list_of_environment_versions($environemt);
 131          $this->assertCount(5, $versions);
 132          $this->assertContains('1.9', $versions);
 133          $this->assertContains('2.5', $versions);
 134          $this->assertContains('2.6', $versions);
 135          $this->assertContains('2.7', $versions);
 136          $this->assertContains('all', $versions);
 137      }
 138  
 139      /**
 140       * Test the environment_verify_plugin() function.
 141       */
 142      public function test_verify_plugin() {
 143          global $CFG;
 144          require_once($CFG->libdir.'/environmentlib.php');
 145          // Build sample xmlised environment file fragments.
 146          $plugin1xml = <<<END
 147  <PLUGIN name="block_testcase">
 148      <PHP_EXTENSIONS>
 149          <PHP_EXTENSION name="xsl" level="required" />
 150      </PHP_EXTENSIONS>
 151  </PLUGIN>
 152  END;
 153          $plugin1 = xmlize($plugin1xml);
 154          $plugin2xml = <<<END
 155  <PLUGIN>
 156      <PHP_EXTENSIONS>
 157          <PHP_EXTENSION name="xsl" level="required" />
 158      </PHP_EXTENSIONS>
 159  </PLUGIN>
 160  END;
 161          $plugin2 = xmlize($plugin2xml);
 162          $this->assertTrue(environment_verify_plugin('block_testcase', $plugin1['PLUGIN']));
 163          $this->assertFalse(environment_verify_plugin('block_testcase', $plugin2['PLUGIN']));
 164          $this->assertFalse(environment_verify_plugin('mod_someother', $plugin1['PLUGIN']));
 165          $this->assertFalse(environment_verify_plugin('mod_someother', $plugin2['PLUGIN']));
 166      }
 167  
 168      /**
 169       * Test the restrict_php_version() function returns true if the current
 170       * PHP version is greater than the restricted version
 171       */
 172      public function test_restrict_php_version_greater_than_restricted_version() {
 173          global $CFG;
 174          require_once($CFG->libdir.'/environmentlib.php');
 175  
 176          $result = new environment_results('php');
 177          $delimiter = '.';
 178          // Get the current PHP version.
 179          $currentversion = explode($delimiter, normalize_version(phpversion()));
 180          // Lets drop back one major version to ensure we trip the restriction.
 181          $currentversion[0]--;
 182          $restrictedversion = implode($delimiter, $currentversion);
 183  
 184          // Make sure the status is true before the test to see it flip to false.
 185          $result->setStatus(true);
 186  
 187          $this->assertTrue(restrict_php_version($result, $restrictedversion),
 188              'restrict_php_version returns true if the current version exceeds the restricted version');
 189      }
 190  
 191      /**
 192       * Test the restrict_php_version() function returns true if the current
 193       * PHP version is equal to the restricted version
 194       */
 195      public function test_restrict_php_version_equal_to_restricted_version() {
 196          global $CFG;
 197          require_once($CFG->libdir.'/environmentlib.php');
 198  
 199          $result = new environment_results('php');
 200          // Get the current PHP version.
 201          $currentversion = normalize_version(phpversion());
 202  
 203          // Make sure the status is true before the test to see it flip to false.
 204          $result->setStatus(true);
 205  
 206          $this->assertTrue(restrict_php_version($result, $currentversion),
 207              'restrict_php_version returns true if the current version is equal to the restricted version');
 208      }
 209  
 210      /**
 211       * Test the restrict_php_version() function returns false if the current
 212       * PHP version is less than the restricted version
 213       */
 214      public function test_restrict_php_version_less_than_restricted_version() {
 215          global $CFG;
 216          require_once($CFG->libdir.'/environmentlib.php');
 217  
 218          $result = new environment_results('php');
 219          $delimiter = '.';
 220          // Get the current PHP version.
 221          $currentversion = explode($delimiter, normalize_version(phpversion()));
 222          // Lets increase the major version to ensure don't trip the restriction.
 223          $currentversion[0]++;
 224          $restrictedversion = implode($delimiter, $currentversion);
 225  
 226          // Make sure the status is true before the test to see it flip to false.
 227          $result->setStatus(true);
 228  
 229          $this->assertFalse(restrict_php_version($result, $restrictedversion),
 230              'restrict_php_version returns false if the current version is less than the restricted version');
 231      }
 232  }