Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace core;
18
 
19
use environment_results;
20
 
21
/**
22
 * Moodle environment test.
23
 *
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
28
 */
29
class environment_test extends \advanced_testcase {
30
 
31
    /**
32
     * Test the environment check status.
33
     */
34
    public function test_environment_check_status() {
35
        global $CFG;
36
        require_once($CFG->libdir.'/environmentlib.php');
37
 
38
        $results = check_moodle_environment(normalize_version($CFG->release), ENV_SELECT_RELEASE);
39
 
40
        // The first element of the results array contains the environment check status.
41
        $status = reset($results);
42
        $this->assertTrue($status);
43
    }
44
 
45
    /**
46
     * Data provider for Moodle environment check tests.
47
     *
48
     * @return array
49
     */
50
    public function environment_provider() {
51
        global $CFG;
52
        require_once($CFG->libdir.'/environmentlib.php');
53
 
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);
60
    }
61
 
62
    /**
63
     * Test the environment.
64
     *
65
     * @dataProvider environment_provider
66
     * @param environment_results $result
67
     */
68
    public function test_environment($result) {
69
        $sslmessages = ['ssl/tls configuration not supported', 'invalid ssl/tls configuration'];
70
 
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.');
76
        }
77
 
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.');
83
            }
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.');
87
            }
88
            if ($result->info === 'oracle_database_usage') {
89
                // If we're on a system that uses the Oracle database, skip the Oracle check.
90
                $this->markTestSkipped('Oracle database check is not necessary for unit testing.');
91
            }
92
        }
93
        $info = "{$result->part}:{$result->info}";
94
        $this->assertTrue($result->getStatus(), "Problem detected in environment ($info), fix all warnings and errors!");
95
    }
96
 
97
    /**
98
     * Test the get_list_of_environment_versions() function.
99
     */
100
    public function test_get_list_of_environment_versions() {
101
        global $CFG;
102
        require_once($CFG->libdir.'/environmentlib.php');
103
        // Build a sample xmlised environment.xml.
104
        $xml = <<<END
105
<COMPATIBILITY_MATRIX>
106
    <MOODLE version="1.9">
107
        <PHP_EXTENSIONS>
108
            <PHP_EXTENSION name="xsl" level="required" />
109
        </PHP_EXTENSIONS>
110
    </MOODLE>
111
    <MOODLE version="2.5">
112
        <PHP_EXTENSIONS>
113
            <PHP_EXTENSION name="xsl" level="required" />
114
        </PHP_EXTENSIONS>
115
    </MOODLE>
116
    <MOODLE version="2.6">
117
        <PHP_EXTENSIONS>
118
            <PHP_EXTENSION name="xsl" level="required" />
119
        </PHP_EXTENSIONS>
120
    </MOODLE>
121
    <MOODLE version="2.7">
122
        <PHP_EXTENSIONS>
123
            <PHP_EXTENSION name="xsl" level="required" />
124
        </PHP_EXTENSIONS>
125
    </MOODLE>
126
    <PLUGIN name="block_test">
127
        <PHP_EXTENSIONS>
128
            <PHP_EXTENSION name="xsl" level="required" />
129
        </PHP_EXTENSIONS>
130
    </PLUGIN>
131
</COMPATIBILITY_MATRIX>
132
END;
133
        $environemt = xmlize($xml);
134
        $versions = get_list_of_environment_versions($environemt);
135
        $this->assertCount(5, $versions);
136
        $this->assertContains('1.9', $versions);
137
        $this->assertContains('2.5', $versions);
138
        $this->assertContains('2.6', $versions);
139
        $this->assertContains('2.7', $versions);
140
        $this->assertContains('all', $versions);
141
    }
142
 
143
    /**
144
     * Test the environment_verify_plugin() function.
145
     */
146
    public function test_verify_plugin() {
147
        global $CFG;
148
        require_once($CFG->libdir.'/environmentlib.php');
149
        // Build sample xmlised environment file fragments.
150
        $plugin1xml = <<<END
151
<PLUGIN name="block_testcase">
152
    <PHP_EXTENSIONS>
153
        <PHP_EXTENSION name="xsl" level="required" />
154
    </PHP_EXTENSIONS>
155
</PLUGIN>
156
END;
157
        $plugin1 = xmlize($plugin1xml);
158
        $plugin2xml = <<<END
159
<PLUGIN>
160
    <PHP_EXTENSIONS>
161
        <PHP_EXTENSION name="xsl" level="required" />
162
    </PHP_EXTENSIONS>
163
</PLUGIN>
164
END;
165
        $plugin2 = xmlize($plugin2xml);
166
        $this->assertTrue(environment_verify_plugin('block_testcase', $plugin1['PLUGIN']));
167
        $this->assertFalse(environment_verify_plugin('block_testcase', $plugin2['PLUGIN']));
168
        $this->assertFalse(environment_verify_plugin('mod_someother', $plugin1['PLUGIN']));
169
        $this->assertFalse(environment_verify_plugin('mod_someother', $plugin2['PLUGIN']));
170
    }
171
 
172
    /**
173
     * Test the restrict_php_version() function returns true if the current
174
     * PHP version is greater than the restricted version
175
     */
176
    public function test_restrict_php_version_greater_than_restricted_version() {
177
        global $CFG;
178
        require_once($CFG->libdir.'/environmentlib.php');
179
 
180
        $result = new environment_results('php');
181
        $delimiter = '.';
182
        // Get the current PHP version.
183
        $currentversion = explode($delimiter, normalize_version(phpversion()));
184
        // Lets drop back one major version to ensure we trip the restriction.
185
        $currentversion[0]--;
186
        $restrictedversion = implode($delimiter, $currentversion);
187
 
188
        // Make sure the status is true before the test to see it flip to false.
189
        $result->setStatus(true);
190
 
191
        $this->assertTrue(restrict_php_version($result, $restrictedversion),
192
            'restrict_php_version returns true if the current version exceeds the restricted version');
193
    }
194
 
195
    /**
196
     * Test the restrict_php_version() function returns true if the current
197
     * PHP version is equal to the restricted version
198
     */
199
    public function test_restrict_php_version_equal_to_restricted_version() {
200
        global $CFG;
201
        require_once($CFG->libdir.'/environmentlib.php');
202
 
203
        $result = new environment_results('php');
204
        // Get the current PHP version.
205
        $currentversion = normalize_version(phpversion());
206
 
207
        // Make sure the status is true before the test to see it flip to false.
208
        $result->setStatus(true);
209
 
210
        $this->assertTrue(restrict_php_version($result, $currentversion),
211
            'restrict_php_version returns true if the current version is equal to the restricted version');
212
    }
213
 
214
    /**
215
     * Test the restrict_php_version() function returns false if the current
216
     * PHP version is less than the restricted version
217
     */
218
    public function test_restrict_php_version_less_than_restricted_version() {
219
        global $CFG;
220
        require_once($CFG->libdir.'/environmentlib.php');
221
 
222
        $result = new environment_results('php');
223
        $delimiter = '.';
224
        // Get the current PHP version.
225
        $currentversion = explode($delimiter, normalize_version(phpversion()));
226
        // Lets increase the major version to ensure don't trip the restriction.
227
        $currentversion[0]++;
228
        $restrictedversion = implode($delimiter, $currentversion);
229
 
230
        // Make sure the status is true before the test to see it flip to false.
231
        $result->setStatus(true);
232
 
233
        $this->assertFalse(restrict_php_version($result, $restrictedversion),
234
            'restrict_php_version returns false if the current version is less than the restricted version');
235
    }
236
}