Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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