Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
 */
11 efrain 29
final class environment_test extends \advanced_testcase {
1 efrain 30
 
31
    /**
32
     * Test the environment check status.
33
     */
11 efrain 34
    public function test_environment_check_status(): void {
1 efrain 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
     */
11 efrain 50
    public static function environment_provider(): array {
1 efrain 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
     */
11 efrain 68
    public function test_environment($result): void {
1 efrain 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
 
11 efrain 78
        if ($result->part === 'php_extension'
79
                && $result->getPluginName() !== ''
80
                && $result->getLevel() === 'optional'
81
                && $result->getStatus() === false) {
82
            $this->markTestSkipped('Optional plugin extension is not necessary for unit testing.');
83
        }
84
 
1 efrain 85
        if ($result->part === 'custom_check'
86
                && $result->getLevel() === 'optional'
87
                && $result->getStatus() === false) {
88
            if (in_array($result->info, $sslmessages)) {
89
                $this->markTestSkipped('Up-to-date TLS libraries are not necessary for unit testing.');
90
            }
91
            if ($result->info === 'php not 64 bits' && PHP_INT_SIZE == 4) {
92
                // If we're on a 32-bit system, skip 64-bit check. 32-bit PHP has PHP_INT_SIZE set to 4.
93
                $this->markTestSkipped('64-bit check is not necessary for unit testing.');
94
            }
95
            if ($result->info === 'oracle_database_usage') {
96
                // If we're on a system that uses the Oracle database, skip the Oracle check.
97
                $this->markTestSkipped('Oracle database check is not necessary for unit testing.');
98
            }
99
        }
100
        $info = "{$result->part}:{$result->info}";
101
        $this->assertTrue($result->getStatus(), "Problem detected in environment ($info), fix all warnings and errors!");
102
    }
103
 
104
    /**
105
     * Test the get_list_of_environment_versions() function.
106
     */
11 efrain 107
    public function test_get_list_of_environment_versions(): void {
1 efrain 108
        global $CFG;
109
        require_once($CFG->libdir.'/environmentlib.php');
110
        // Build a sample xmlised environment.xml.
111
        $xml = <<<END
112
<COMPATIBILITY_MATRIX>
113
    <MOODLE version="1.9">
114
        <PHP_EXTENSIONS>
115
            <PHP_EXTENSION name="xsl" level="required" />
116
        </PHP_EXTENSIONS>
117
    </MOODLE>
118
    <MOODLE version="2.5">
119
        <PHP_EXTENSIONS>
120
            <PHP_EXTENSION name="xsl" level="required" />
121
        </PHP_EXTENSIONS>
122
    </MOODLE>
123
    <MOODLE version="2.6">
124
        <PHP_EXTENSIONS>
125
            <PHP_EXTENSION name="xsl" level="required" />
126
        </PHP_EXTENSIONS>
127
    </MOODLE>
128
    <MOODLE version="2.7">
129
        <PHP_EXTENSIONS>
130
            <PHP_EXTENSION name="xsl" level="required" />
131
        </PHP_EXTENSIONS>
132
    </MOODLE>
133
    <PLUGIN name="block_test">
134
        <PHP_EXTENSIONS>
135
            <PHP_EXTENSION name="xsl" level="required" />
136
        </PHP_EXTENSIONS>
137
    </PLUGIN>
138
</COMPATIBILITY_MATRIX>
139
END;
140
        $environemt = xmlize($xml);
141
        $versions = get_list_of_environment_versions($environemt);
142
        $this->assertCount(5, $versions);
143
        $this->assertContains('1.9', $versions);
144
        $this->assertContains('2.5', $versions);
145
        $this->assertContains('2.6', $versions);
146
        $this->assertContains('2.7', $versions);
147
        $this->assertContains('all', $versions);
148
    }
149
 
150
    /**
151
     * Test the environment_verify_plugin() function.
152
     */
11 efrain 153
    public function test_verify_plugin(): void {
1 efrain 154
        global $CFG;
155
        require_once($CFG->libdir.'/environmentlib.php');
156
        // Build sample xmlised environment file fragments.
157
        $plugin1xml = <<<END
158
<PLUGIN name="block_testcase">
159
    <PHP_EXTENSIONS>
160
        <PHP_EXTENSION name="xsl" level="required" />
161
    </PHP_EXTENSIONS>
162
</PLUGIN>
163
END;
164
        $plugin1 = xmlize($plugin1xml);
165
        $plugin2xml = <<<END
166
<PLUGIN>
167
    <PHP_EXTENSIONS>
168
        <PHP_EXTENSION name="xsl" level="required" />
169
    </PHP_EXTENSIONS>
170
</PLUGIN>
171
END;
172
        $plugin2 = xmlize($plugin2xml);
173
        $this->assertTrue(environment_verify_plugin('block_testcase', $plugin1['PLUGIN']));
174
        $this->assertFalse(environment_verify_plugin('block_testcase', $plugin2['PLUGIN']));
175
        $this->assertFalse(environment_verify_plugin('mod_someother', $plugin1['PLUGIN']));
176
        $this->assertFalse(environment_verify_plugin('mod_someother', $plugin2['PLUGIN']));
177
    }
178
 
179
    /**
180
     * Test the restrict_php_version() function returns true if the current
181
     * PHP version is greater than the restricted version
182
     */
11 efrain 183
    public function test_restrict_php_version_greater_than_restricted_version(): void {
1 efrain 184
        global $CFG;
185
        require_once($CFG->libdir.'/environmentlib.php');
186
 
187
        $result = new environment_results('php');
188
        $delimiter = '.';
189
        // Get the current PHP version.
190
        $currentversion = explode($delimiter, normalize_version(phpversion()));
191
        // Lets drop back one major version to ensure we trip the restriction.
192
        $currentversion[0]--;
193
        $restrictedversion = implode($delimiter, $currentversion);
194
 
195
        // Make sure the status is true before the test to see it flip to false.
196
        $result->setStatus(true);
197
 
198
        $this->assertTrue(restrict_php_version($result, $restrictedversion),
199
            'restrict_php_version returns true if the current version exceeds the restricted version');
200
    }
201
 
202
    /**
203
     * Test the restrict_php_version() function returns true if the current
204
     * PHP version is equal to the restricted version
205
     */
11 efrain 206
    public function test_restrict_php_version_equal_to_restricted_version(): void {
1 efrain 207
        global $CFG;
208
        require_once($CFG->libdir.'/environmentlib.php');
209
 
210
        $result = new environment_results('php');
211
        // Get the current PHP version.
212
        $currentversion = normalize_version(phpversion());
213
 
214
        // Make sure the status is true before the test to see it flip to false.
215
        $result->setStatus(true);
216
 
217
        $this->assertTrue(restrict_php_version($result, $currentversion),
218
            'restrict_php_version returns true if the current version is equal to the restricted version');
219
    }
220
 
221
    /**
222
     * Test the restrict_php_version() function returns false if the current
223
     * PHP version is less than the restricted version
224
     */
11 efrain 225
    public function test_restrict_php_version_less_than_restricted_version(): void {
1 efrain 226
        global $CFG;
227
        require_once($CFG->libdir.'/environmentlib.php');
228
 
229
        $result = new environment_results('php');
230
        $delimiter = '.';
231
        // Get the current PHP version.
232
        $currentversion = explode($delimiter, normalize_version(phpversion()));
233
        // Lets increase the major version to ensure don't trip the restriction.
234
        $currentversion[0]++;
235
        $restrictedversion = implode($delimiter, $currentversion);
236
 
237
        // Make sure the status is true before the test to see it flip to false.
238
        $result->setStatus(true);
239
 
240
        $this->assertFalse(restrict_php_version($result, $restrictedversion),
241
            'restrict_php_version returns false if the current version is less than the restricted version');
242
    }
243
}