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/>.
19 use environment_results
;
22 * Moodle environment test.
26 * @copyright 2013 Petr Skoda {@link http://skodak.org}
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 class environment_test
extends \advanced_testcase
{
32 * Test the environment check status.
34 public function test_environment_check_status() {
36 require_once($CFG->libdir
.'/environmentlib.php');
38 $results = check_moodle_environment(normalize_version($CFG->release
), ENV_SELECT_RELEASE
);
40 // The first element of the results array contains the environment check status.
41 $status = reset($results);
42 $this->assertTrue($status);
46 * Data provider for Moodle environment check tests.
50 public function environment_provider() {
52 require_once($CFG->libdir
.'/environmentlib.php');
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) {
59 }, $environmentresults);
63 * Test the environment.
65 * @dataProvider environment_provider
66 * @param environment_results $result
68 public function test_environment($result) {
69 $sslmessages = ['ssl/tls configuration not supported', 'invalid ssl/tls configuration'];
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.');
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.');
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.');
89 $info = "{$result->part}:{$result->info}";
90 $this->assertTrue($result->getStatus(), "Problem detected in environment ($info), fix all warnings and errors!");
94 * Test the get_list_of_environment_versions() function.
96 public function test_get_list_of_environment_versions() {
98 require_once($CFG->libdir
.'/environmentlib.php');
99 // Build a sample xmlised environment.xml.
101 <COMPATIBILITY_MATRIX>
102 <MOODLE version="1.9">
104 <PHP_EXTENSION name="xsl" level="required" />
107 <MOODLE version="2.5">
109 <PHP_EXTENSION name="xsl" level="required" />
112 <MOODLE version="2.6">
114 <PHP_EXTENSION name="xsl" level="required" />
117 <MOODLE version="2.7">
119 <PHP_EXTENSION name="xsl" level="required" />
122 <PLUGIN name="block_test">
124 <PHP_EXTENSION name="xsl" level="required" />
127 </COMPATIBILITY_MATRIX>
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);
140 * Test the environment_verify_plugin() function.
142 public function test_verify_plugin() {
144 require_once($CFG->libdir
.'/environmentlib.php');
145 // Build sample xmlised environment file fragments.
147 <PLUGIN name="block_testcase">
149 <PHP_EXTENSION name="xsl" level="required" />
153 $plugin1 = xmlize($plugin1xml);
157 <PHP_EXTENSION name="xsl" level="required" />
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']));
169 * Test the restrict_php_version() function returns true if the current
170 * PHP version is greater than the restricted version
172 public function test_restrict_php_version_greater_than_restricted_version() {
174 require_once($CFG->libdir
.'/environmentlib.php');
176 $result = new environment_results('php');
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);
184 // Make sure the status is true before the test to see it flip to false.
185 $result->setStatus(true);
187 $this->assertTrue(restrict_php_version($result, $restrictedversion),
188 'restrict_php_version returns true if the current version exceeds the restricted version');
192 * Test the restrict_php_version() function returns true if the current
193 * PHP version is equal to the restricted version
195 public function test_restrict_php_version_equal_to_restricted_version() {
197 require_once($CFG->libdir
.'/environmentlib.php');
199 $result = new environment_results('php');
200 // Get the current PHP version.
201 $currentversion = normalize_version(phpversion());
203 // Make sure the status is true before the test to see it flip to false.
204 $result->setStatus(true);
206 $this->assertTrue(restrict_php_version($result, $currentversion),
207 'restrict_php_version returns true if the current version is equal to the restricted version');
211 * Test the restrict_php_version() function returns false if the current
212 * PHP version is less than the restricted version
214 public function test_restrict_php_version_less_than_restricted_version() {
216 require_once($CFG->libdir
.'/environmentlib.php');
218 $result = new environment_results('php');
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);
226 // Make sure the status is true before the test to see it flip to false.
227 $result->setStatus(true);
229 $this->assertFalse(restrict_php_version($result, $restrictedversion),
230 'restrict_php_version returns false if the current version is less than the restricted version');