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/>.
18 * Moodle environment test.
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();
30 * Do standard environment.xml tests.
32 class core_environment_testcase
extends advanced_testcase
{
35 * Test the environment.
37 public function test_environment() {
40 require_once($CFG->libdir
.'/environmentlib.php');
41 list($envstatus, $environment_results) = check_moodle_environment(normalize_version($CFG->release
), ENV_SELECT_RELEASE
);
43 $this->assertNotEmpty($envstatus);
44 foreach ($environment_results as $environment_result) {
45 if ($environment_result->part
=== 'php_setting'
46 and $environment_result->info
=== 'opcache.enable'
47 and $environment_result->getLevel() === 'optional'
48 and $environment_result->getStatus() === false
50 $this->markTestSkipped('OPCache extension is not necessary for unit testing.');
53 $this->assertTrue($environment_result->getStatus(), "Problem detected in environment ($environment_result->part:$environment_result->info), fix all warnings and errors!");
58 * Test the get_list_of_environment_versions() function.
60 public function test_get_list_of_environment_versions() {
62 require_once($CFG->libdir
.'/environmentlib.php');
63 // Build a sample xmlised environment.xml.
65 <COMPATIBILITY_MATRIX>
66 <MOODLE version="1.9">
68 <PHP_EXTENSION name="xsl" level="required" />
71 <MOODLE version="2.5">
73 <PHP_EXTENSION name="xsl" level="required" />
76 <MOODLE version="2.6">
78 <PHP_EXTENSION name="xsl" level="required" />
81 <MOODLE version="2.7">
83 <PHP_EXTENSION name="xsl" level="required" />
86 <PLUGIN name="block_test">
88 <PHP_EXTENSION name="xsl" level="required" />
91 </COMPATIBILITY_MATRIX>
93 $environemt = xmlize($xml);
94 $versions = get_list_of_environment_versions($environemt);
95 $this->assertCount(5, $versions);
96 $this->assertContains('1.9', $versions);
97 $this->assertContains('2.5', $versions);
98 $this->assertContains('2.6', $versions);
99 $this->assertContains('2.7', $versions);
100 $this->assertContains('all', $versions);
104 * Test the environment_verify_plugin() function.
106 public function test_verify_plugin() {
108 require_once($CFG->libdir
.'/environmentlib.php');
109 // Build sample xmlised environment file fragments.
111 <PLUGIN name="block_testcase">
113 <PHP_EXTENSION name="xsl" level="required" />
117 $plugin1 = xmlize($plugin1xml);
121 <PHP_EXTENSION name="xsl" level="required" />
125 $plugin2 = xmlize($plugin2xml);
126 $this->assertTrue(environment_verify_plugin('block_testcase', $plugin1['PLUGIN']));
127 $this->assertFalse(environment_verify_plugin('block_testcase', $plugin2['PLUGIN']));
128 $this->assertFalse(environment_verify_plugin('mod_someother', $plugin1['PLUGIN']));
129 $this->assertFalse(environment_verify_plugin('mod_someother', $plugin2['PLUGIN']));
133 * Test the restrict_php_version() function returns true if the current
134 * PHP version is greater than the restricted version
136 public function test_restrict_php_version_greater_than_restricted_version() {
138 require_once($CFG->libdir
.'/environmentlib.php');
140 $result = new environment_results('php');
142 // Get the current PHP version.
143 $currentversion = explode($delimiter, normalize_version(phpversion()));
144 // Lets drop back one major version to ensure we trip the restriction.
145 $currentversion[0]--;
146 $restrictedversion = implode($delimiter, $currentversion);
148 // Make sure the status is true before the test to see it flip to false.
149 $result->setStatus(true);
151 $this->assertTrue(restrict_php_version($result, $restrictedversion),
152 'restrict_php_version returns true if the current version exceeds the restricted version');
156 * Test the restrict_php_version() function returns true if the current
157 * PHP version is equal to the restricted version
159 public function test_restrict_php_version_equal_to_restricted_version() {
161 require_once($CFG->libdir
.'/environmentlib.php');
163 $result = new environment_results('php');
164 // Get the current PHP version.
165 $currentversion = normalize_version(phpversion());
167 // Make sure the status is true before the test to see it flip to false.
168 $result->setStatus(true);
170 $this->assertTrue(restrict_php_version($result, $currentversion),
171 'restrict_php_version returns true if the current version is equal to the restricted version');
175 * Test the restrict_php_version() function returns false if the current
176 * PHP version is less than the restricted version
178 public function test_restrict_php_version_less_than_restricted_version() {
180 require_once($CFG->libdir
.'/environmentlib.php');
182 $result = new environment_results('php');
184 // Get the current PHP version.
185 $currentversion = explode($delimiter, normalize_version(phpversion()));
186 // Lets increase the major version to ensure don't trip the restriction.
187 $currentversion[0]++
;
188 $restrictedversion = implode($delimiter, $currentversion);
190 // Make sure the status is true before the test to see it flip to false.
191 $result->setStatus(true);
193 $this->assertFalse(restrict_php_version($result, $restrictedversion),
194 'restrict_php_version returns false if the current version is less than the restricted version');