MDL-68430 filter_mathjaxloader: update default CDN to 2.7.8
[moodle.git] / lib / tests / environment_test.php
blobf422cbe193e1134bda0fadb4a42c08e300032313
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 /**
18 * Moodle environment test.
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
26 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Do standard environment.xml tests.
32 class core_environment_testcase extends advanced_testcase {
34 /**
35 * Test the environment check status.
37 public function test_environment_check_status() {
38 global $CFG;
39 require_once($CFG->libdir.'/environmentlib.php');
41 $results = check_moodle_environment(normalize_version($CFG->release), ENV_SELECT_RELEASE);
43 // The first element of the results array contains the environment check status.
44 $status = reset($results);
45 $this->assertTrue($status);
48 /**
49 * Data provider for Moodle environment check tests.
51 * @return array
53 public function environment_provider() {
54 global $CFG;
55 require_once($CFG->libdir.'/environmentlib.php');
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);
65 /**
66 * Test the environment.
68 * @dataProvider environment_provider
69 * @param environment_results $result
71 public function test_environment($result) {
72 $sslmessages = ['ssl/tls configuration not supported', 'invalid ssl/tls configuration'];
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.');
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.');
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.');
92 $info = "{$result->part}:{$result->info}";
93 $this->assertTrue($result->getStatus(), "Problem detected in environment ($info), fix all warnings and errors!");
96 /**
97 * Test the get_list_of_environment_versions() function.
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);
143 * Test the environment_verify_plugin() function.
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']));
172 * Test the restrict_php_version() function returns true if the current
173 * PHP version is greater than the restricted version
175 public function test_restrict_php_version_greater_than_restricted_version() {
176 global $CFG;
177 require_once($CFG->libdir.'/environmentlib.php');
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);
187 // Make sure the status is true before the test to see it flip to false.
188 $result->setStatus(true);
190 $this->assertTrue(restrict_php_version($result, $restrictedversion),
191 'restrict_php_version returns true if the current version exceeds the restricted version');
195 * Test the restrict_php_version() function returns true if the current
196 * PHP version is equal to the restricted version
198 public function test_restrict_php_version_equal_to_restricted_version() {
199 global $CFG;
200 require_once($CFG->libdir.'/environmentlib.php');
202 $result = new environment_results('php');
203 // Get the current PHP version.
204 $currentversion = normalize_version(phpversion());
206 // Make sure the status is true before the test to see it flip to false.
207 $result->setStatus(true);
209 $this->assertTrue(restrict_php_version($result, $currentversion),
210 'restrict_php_version returns true if the current version is equal to the restricted version');
214 * Test the restrict_php_version() function returns false if the current
215 * PHP version is less than the restricted version
217 public function test_restrict_php_version_less_than_restricted_version() {
218 global $CFG;
219 require_once($CFG->libdir.'/environmentlib.php');
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);
229 // Make sure the status is true before the test to see it flip to false.
230 $result->setStatus(true);
232 $this->assertFalse(restrict_php_version($result, $restrictedversion),
233 'restrict_php_version returns false if the current version is less than the restricted version');