Merge branch 'MDL-58454-master' of git://github.com/junpataleta/moodle
[moodle.git] / lib / tests / environment_test.php
bloba660edb0a621d4e3520e6a500fd2a1a142aab1ef
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.
37 public function test_environment() {
38 global $CFG;
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
51 ) {
52 $this->markTestSkipped('OPCache extension is not necessary for unit testing.');
53 continue;
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
59 ) {
60 $this->markTestSkipped('Up-to-date TLS libraries are not necessary for unit testing.');
61 continue;
63 $this->assertTrue($environment_result->getStatus(), "Problem detected in environment ($environment_result->part:$environment_result->info), fix all warnings and errors!");
67 /**
68 * Test the get_list_of_environment_versions() function.
70 public function test_get_list_of_environment_versions() {
71 global $CFG;
72 require_once($CFG->libdir.'/environmentlib.php');
73 // Build a sample xmlised environment.xml.
74 $xml = <<<END
75 <COMPATIBILITY_MATRIX>
76 <MOODLE version="1.9">
77 <PHP_EXTENSIONS>
78 <PHP_EXTENSION name="xsl" level="required" />
79 </PHP_EXTENSIONS>
80 </MOODLE>
81 <MOODLE version="2.5">
82 <PHP_EXTENSIONS>
83 <PHP_EXTENSION name="xsl" level="required" />
84 </PHP_EXTENSIONS>
85 </MOODLE>
86 <MOODLE version="2.6">
87 <PHP_EXTENSIONS>
88 <PHP_EXTENSION name="xsl" level="required" />
89 </PHP_EXTENSIONS>
90 </MOODLE>
91 <MOODLE version="2.7">
92 <PHP_EXTENSIONS>
93 <PHP_EXTENSION name="xsl" level="required" />
94 </PHP_EXTENSIONS>
95 </MOODLE>
96 <PLUGIN name="block_test">
97 <PHP_EXTENSIONS>
98 <PHP_EXTENSION name="xsl" level="required" />
99 </PHP_EXTENSIONS>
100 </PLUGIN>
101 </COMPATIBILITY_MATRIX>
102 END;
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() {
117 global $CFG;
118 require_once($CFG->libdir.'/environmentlib.php');
119 // Build sample xmlised environment file fragments.
120 $plugin1xml = <<<END
121 <PLUGIN name="block_testcase">
122 <PHP_EXTENSIONS>
123 <PHP_EXTENSION name="xsl" level="required" />
124 </PHP_EXTENSIONS>
125 </PLUGIN>
126 END;
127 $plugin1 = xmlize($plugin1xml);
128 $plugin2xml = <<<END
129 <PLUGIN>
130 <PHP_EXTENSIONS>
131 <PHP_EXTENSION name="xsl" level="required" />
132 </PHP_EXTENSIONS>
133 </PLUGIN>
134 END;
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() {
147 global $CFG;
148 require_once($CFG->libdir.'/environmentlib.php');
150 $result = new environment_results('php');
151 $delimiter = '.';
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() {
170 global $CFG;
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() {
189 global $CFG;
190 require_once($CFG->libdir.'/environmentlib.php');
192 $result = new environment_results('php');
193 $delimiter = '.';
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');