MDL-64285 core: Fix environment check test
[moodle.git] / lib / tests / environment_test.php
CommitLineData
7dc1feaa
PS
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
26defined('MOODLE_INTERNAL') || die();
27
28
29/**
30 * Do standard environment.xml tests.
31 */
5b0d1b81 32class core_environment_testcase extends advanced_testcase {
7dc1feaa 33
b05cfa0e 34 /**
c482e6bd 35 * Test the environment check status.
b05cfa0e 36 */
c482e6bd 37 public function test_environment_check_status() {
7dc1feaa 38 global $CFG;
c482e6bd
JP
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 }
7dc1feaa 47
c482e6bd
JP
48 /**
49 * Data provider for Moodle environment check tests.
50 *
51 * @return array
52 */
53 public function environment_provider() {
54 global $CFG;
7dc1feaa 55 require_once($CFG->libdir.'/environmentlib.php');
7dc1feaa 56
c482e6bd
JP
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) {
ab3759ff
CB
72 $sslmessages = ['ssl/tls configuration not supported', 'invalid ssl/tls configuration'];
73
c482e6bd
JP
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)) {
ab3759ff 85 $this->markTestSkipped('Up-to-date TLS libraries are not necessary for unit testing.');
ab3759ff 86 }
7dc1feaa 87 }
c482e6bd
JP
88 $info = "{$result->part}:{$result->info}";
89 $this->assertTrue($result->getStatus(), "Problem detected in environment ($info), fix all warnings and errors!");
7dc1feaa 90 }
8072628a
NM
91
92 /**
93 * Test the get_list_of_environment_versions() function.
94 */
95 public function test_get_list_of_environment_versions() {
96 global $CFG;
97 require_once($CFG->libdir.'/environmentlib.php');
98 // Build a sample xmlised environment.xml.
99 $xml = <<<END
100<COMPATIBILITY_MATRIX>
101 <MOODLE version="1.9">
102 <PHP_EXTENSIONS>
103 <PHP_EXTENSION name="xsl" level="required" />
104 </PHP_EXTENSIONS>
105 </MOODLE>
106 <MOODLE version="2.5">
107 <PHP_EXTENSIONS>
108 <PHP_EXTENSION name="xsl" level="required" />
109 </PHP_EXTENSIONS>
110 </MOODLE>
111 <MOODLE version="2.6">
112 <PHP_EXTENSIONS>
113 <PHP_EXTENSION name="xsl" level="required" />
114 </PHP_EXTENSIONS>
115 </MOODLE>
116 <MOODLE version="2.7">
117 <PHP_EXTENSIONS>
118 <PHP_EXTENSION name="xsl" level="required" />
119 </PHP_EXTENSIONS>
120 </MOODLE>
121 <PLUGIN name="block_test">
122 <PHP_EXTENSIONS>
123 <PHP_EXTENSION name="xsl" level="required" />
124 </PHP_EXTENSIONS>
125 </PLUGIN>
126</COMPATIBILITY_MATRIX>
127END;
128 $environemt = xmlize($xml);
129 $versions = get_list_of_environment_versions($environemt);
130 $this->assertCount(5, $versions);
131 $this->assertContains('1.9', $versions);
132 $this->assertContains('2.5', $versions);
133 $this->assertContains('2.6', $versions);
134 $this->assertContains('2.7', $versions);
135 $this->assertContains('all', $versions);
136 }
137
138 /**
139 * Test the environment_verify_plugin() function.
140 */
141 public function test_verify_plugin() {
142 global $CFG;
143 require_once($CFG->libdir.'/environmentlib.php');
144 // Build sample xmlised environment file fragments.
145 $plugin1xml = <<<END
146<PLUGIN name="block_testcase">
147 <PHP_EXTENSIONS>
148 <PHP_EXTENSION name="xsl" level="required" />
149 </PHP_EXTENSIONS>
150</PLUGIN>
151END;
152 $plugin1 = xmlize($plugin1xml);
153 $plugin2xml = <<<END
154<PLUGIN>
155 <PHP_EXTENSIONS>
156 <PHP_EXTENSION name="xsl" level="required" />
157 </PHP_EXTENSIONS>
158</PLUGIN>
159END;
160 $plugin2 = xmlize($plugin2xml);
161 $this->assertTrue(environment_verify_plugin('block_testcase', $plugin1['PLUGIN']));
162 $this->assertFalse(environment_verify_plugin('block_testcase', $plugin2['PLUGIN']));
163 $this->assertFalse(environment_verify_plugin('mod_someother', $plugin1['PLUGIN']));
164 $this->assertFalse(environment_verify_plugin('mod_someother', $plugin2['PLUGIN']));
165 }
19452dc4
RW
166
167 /**
168 * Test the restrict_php_version() function returns true if the current
169 * PHP version is greater than the restricted version
170 */
171 public function test_restrict_php_version_greater_than_restricted_version() {
172 global $CFG;
173 require_once($CFG->libdir.'/environmentlib.php');
174
175 $result = new environment_results('php');
176 $delimiter = '.';
177 // Get the current PHP version.
178 $currentversion = explode($delimiter, normalize_version(phpversion()));
179 // Lets drop back one major version to ensure we trip the restriction.
180 $currentversion[0]--;
181 $restrictedversion = implode($delimiter, $currentversion);
182
183 // Make sure the status is true before the test to see it flip to false.
184 $result->setStatus(true);
185
186 $this->assertTrue(restrict_php_version($result, $restrictedversion),
187 'restrict_php_version returns true if the current version exceeds the restricted version');
188 }
189
190 /**
191 * Test the restrict_php_version() function returns true if the current
192 * PHP version is equal to the restricted version
193 */
194 public function test_restrict_php_version_equal_to_restricted_version() {
195 global $CFG;
196 require_once($CFG->libdir.'/environmentlib.php');
197
198 $result = new environment_results('php');
199 // Get the current PHP version.
200 $currentversion = normalize_version(phpversion());
201
202 // Make sure the status is true before the test to see it flip to false.
203 $result->setStatus(true);
204
205 $this->assertTrue(restrict_php_version($result, $currentversion),
206 'restrict_php_version returns true if the current version is equal to the restricted version');
207 }
208
209 /**
210 * Test the restrict_php_version() function returns false if the current
211 * PHP version is less than the restricted version
212 */
213 public function test_restrict_php_version_less_than_restricted_version() {
214 global $CFG;
215 require_once($CFG->libdir.'/environmentlib.php');
216
217 $result = new environment_results('php');
218 $delimiter = '.';
219 // Get the current PHP version.
220 $currentversion = explode($delimiter, normalize_version(phpversion()));
221 // Lets increase the major version to ensure don't trip the restriction.
222 $currentversion[0]++;
223 $restrictedversion = implode($delimiter, $currentversion);
224
225 // Make sure the status is true before the test to see it flip to false.
226 $result->setStatus(true);
227
228 $this->assertFalse(restrict_php_version($result, $restrictedversion),
229 'restrict_php_version returns false if the current version is less than the restricted version');
230 }
7dc1feaa 231}