Merge branch 'MDL-75553-311' of https://github.com/junpataleta/moodle into MOODLE_311...
[moodle.git] / lib / tests / environment_test.php
blob1d9295a1aabea73b94ccea3db79f94ccb79a6c1a
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.
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/>.
17 namespace core;
19 use environment_results;
21 /**
22 * Moodle environment test.
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
29 class environment_test extends \advanced_testcase {
31 /**
32 * Test the environment check status.
34 public function test_environment_check_status() {
35 global $CFG;
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);
45 /**
46 * Data provider for Moodle environment check tests.
48 * @return array
50 public function environment_provider() {
51 global $CFG;
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) {
58 return [$result];
59 }, $environmentresults);
62 /**
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!");
93 /**
94 * Test the get_list_of_environment_versions() function.
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);
140 * Test the environment_verify_plugin() function.
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']));
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() {
173 global $CFG;
174 require_once($CFG->libdir.'/environmentlib.php');
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);
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() {
196 global $CFG;
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() {
215 global $CFG;
216 require_once($CFG->libdir.'/environmentlib.php');
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);
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');