2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Moodle environment test.
22 * @copyright 2013 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
30 * Do standard environment.xml tests.
32 class core_environment_testcase
extends advanced_testcase
{
35 * Test the environment.
37 public function test_environment() {
40 require_once($CFG->libdir
.'/environmentlib.php');
41 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($CFG->release
), ENV_SELECT_RELEASE
);
43 $sslmessages = ['ssl/tls configuration not supported', 'invalid ssl/tls configuration'];
45 $this->assertNotEmpty($envstatus);
46 foreach ($environment_results as $environment_result) {
47 if ($environment_result->part
=== 'php_setting'
48 and $environment_result->info
=== 'opcache.enable'
49 and $environment_result->getLevel() === 'optional'
50 and $environment_result->getStatus() === false
52 $this->markTestSkipped('OPCache extension is not necessary for unit testing.');
55 if ($environment_result->part
=== 'custom_check'
56 and in_array($environment_result->info
, $sslmessages)
57 and $environment_result->getLevel() === 'optional'
58 and $environment_result->getStatus() === false
60 $this->markTestSkipped('Up-to-date TLS libraries are not necessary for unit testing.');
63 $this->assertTrue($environment_result->getStatus(), "Problem detected in environment ($environment_result->part:$environment_result->info), fix all warnings and errors!");
68 * Test the get_list_of_environment_versions() function.
70 public function test_get_list_of_environment_versions() {
72 require_once($CFG->libdir
.'/environmentlib.php');
73 // Build a sample xmlised environment.xml.
75 <COMPATIBILITY_MATRIX>
76 <MOODLE version="1.9">
78 <PHP_EXTENSION name="xsl" level="required" />
81 <MOODLE version="2.5">
83 <PHP_EXTENSION name="xsl" level="required" />
86 <MOODLE version="2.6">
88 <PHP_EXTENSION name="xsl" level="required" />
91 <MOODLE version="2.7">
93 <PHP_EXTENSION name="xsl" level="required" />
96 <PLUGIN name="block_test">
98 <PHP_EXTENSION name="xsl" level="required" />
101 </COMPATIBILITY_MATRIX>
103 $environemt = xmlize($xml);
104 $versions = get_list_of_environment_versions($environemt);
105 $this->assertCount(5, $versions);
106 $this->assertContains('1.9', $versions);
107 $this->assertContains('2.5', $versions);
108 $this->assertContains('2.6', $versions);
109 $this->assertContains('2.7', $versions);
110 $this->assertContains('all', $versions);
114 * Test the environment_verify_plugin() function.
116 public function test_verify_plugin() {
118 require_once($CFG->libdir
.'/environmentlib.php');
119 // Build sample xmlised environment file fragments.
121 <PLUGIN name="block_testcase">
123 <PHP_EXTENSION name="xsl" level="required" />
127 $plugin1 = xmlize($plugin1xml);
131 <PHP_EXTENSION name="xsl" level="required" />
135 $plugin2 = xmlize($plugin2xml);
136 $this->assertTrue(environment_verify_plugin('block_testcase', $plugin1['PLUGIN']));
137 $this->assertFalse(environment_verify_plugin('block_testcase', $plugin2['PLUGIN']));
138 $this->assertFalse(environment_verify_plugin('mod_someother', $plugin1['PLUGIN']));
139 $this->assertFalse(environment_verify_plugin('mod_someother', $plugin2['PLUGIN']));
143 * Test the restrict_php_version() function returns true if the current
144 * PHP version is greater than the restricted version
146 public function test_restrict_php_version_greater_than_restricted_version() {
148 require_once($CFG->libdir
.'/environmentlib.php');
150 $result = new environment_results('php');
152 // Get the current PHP version.
153 $currentversion = explode($delimiter, normalize_version(phpversion()));
154 // Lets drop back one major version to ensure we trip the restriction.
155 $currentversion[0]--;
156 $restrictedversion = implode($delimiter, $currentversion);
158 // Make sure the status is true before the test to see it flip to false.
159 $result->setStatus(true);
161 $this->assertTrue(restrict_php_version($result, $restrictedversion),
162 'restrict_php_version returns true if the current version exceeds the restricted version');
166 * Test the restrict_php_version() function returns true if the current
167 * PHP version is equal to the restricted version
169 public function test_restrict_php_version_equal_to_restricted_version() {
171 require_once($CFG->libdir
.'/environmentlib.php');
173 $result = new environment_results('php');
174 // Get the current PHP version.
175 $currentversion = normalize_version(phpversion());
177 // Make sure the status is true before the test to see it flip to false.
178 $result->setStatus(true);
180 $this->assertTrue(restrict_php_version($result, $currentversion),
181 'restrict_php_version returns true if the current version is equal to the restricted version');
185 * Test the restrict_php_version() function returns false if the current
186 * PHP version is less than the restricted version
188 public function test_restrict_php_version_less_than_restricted_version() {
190 require_once($CFG->libdir
.'/environmentlib.php');
192 $result = new environment_results('php');
194 // Get the current PHP version.
195 $currentversion = explode($delimiter, normalize_version(phpversion()));
196 // Lets increase the major version to ensure don't trip the restriction.
197 $currentversion[0]++
;
198 $restrictedversion = implode($delimiter, $currentversion);
200 // Make sure the status is true before the test to see it flip to false.
201 $result->setStatus(true);
203 $this->assertFalse(restrict_php_version($result, $restrictedversion),
204 'restrict_php_version returns false if the current version is less than the restricted version');