Proyectos de Subversion Moodle

Rev

Rev 11 | | 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
        }
96
        $info = "{$result->part}:{$result->info}";
97
        $this->assertTrue($result->getStatus(), "Problem detected in environment ($info), fix all warnings and errors!");
98
    }
99
 
100
    /**
101
     * Test the get_list_of_environment_versions() function.
102
     */
11 efrain 103
    public function test_get_list_of_environment_versions(): void {
1 efrain 104
        global $CFG;
105
        require_once($CFG->libdir.'/environmentlib.php');
106
        // Build a sample xmlised environment.xml.
107
        $xml = <<<END
108
<COMPATIBILITY_MATRIX>
109
    <MOODLE version="1.9">
110
        <PHP_EXTENSIONS>
111
            <PHP_EXTENSION name="xsl" level="required" />
112
        </PHP_EXTENSIONS>
113
    </MOODLE>
114
    <MOODLE version="2.5">
115
        <PHP_EXTENSIONS>
116
            <PHP_EXTENSION name="xsl" level="required" />
117
        </PHP_EXTENSIONS>
118
    </MOODLE>
119
    <MOODLE version="2.6">
120
        <PHP_EXTENSIONS>
121
            <PHP_EXTENSION name="xsl" level="required" />
122
        </PHP_EXTENSIONS>
123
    </MOODLE>
124
    <MOODLE version="2.7">
125
        <PHP_EXTENSIONS>
126
            <PHP_EXTENSION name="xsl" level="required" />
127
        </PHP_EXTENSIONS>
128
    </MOODLE>
129
    <PLUGIN name="block_test">
130
        <PHP_EXTENSIONS>
131
            <PHP_EXTENSION name="xsl" level="required" />
132
        </PHP_EXTENSIONS>
133
    </PLUGIN>
134
</COMPATIBILITY_MATRIX>
135
END;
136
        $environemt = xmlize($xml);
137
        $versions = get_list_of_environment_versions($environemt);
138
        $this->assertCount(5, $versions);
139
        $this->assertContains('1.9', $versions);
140
        $this->assertContains('2.5', $versions);
141
        $this->assertContains('2.6', $versions);
142
        $this->assertContains('2.7', $versions);
143
        $this->assertContains('all', $versions);
144
    }
145
 
146
    /**
147
     * Test the environment_verify_plugin() function.
148
     */
11 efrain 149
    public function test_verify_plugin(): void {
1 efrain 150
        global $CFG;
151
        require_once($CFG->libdir.'/environmentlib.php');
152
        // Build sample xmlised environment file fragments.
153
        $plugin1xml = <<<END
154
<PLUGIN name="block_testcase">
155
    <PHP_EXTENSIONS>
156
        <PHP_EXTENSION name="xsl" level="required" />
157
    </PHP_EXTENSIONS>
158
</PLUGIN>
159
END;
160
        $plugin1 = xmlize($plugin1xml);
161
        $plugin2xml = <<<END
162
<PLUGIN>
163
    <PHP_EXTENSIONS>
164
        <PHP_EXTENSION name="xsl" level="required" />
165
    </PHP_EXTENSIONS>
166
</PLUGIN>
167
END;
168
        $plugin2 = xmlize($plugin2xml);
169
        $this->assertTrue(environment_verify_plugin('block_testcase', $plugin1['PLUGIN']));
170
        $this->assertFalse(environment_verify_plugin('block_testcase', $plugin2['PLUGIN']));
171
        $this->assertFalse(environment_verify_plugin('mod_someother', $plugin1['PLUGIN']));
172
        $this->assertFalse(environment_verify_plugin('mod_someother', $plugin2['PLUGIN']));
173
    }
174
 
175
    /**
176
     * Test the restrict_php_version() function returns true if the current
177
     * PHP version is greater than the restricted version
178
     */
11 efrain 179
    public function test_restrict_php_version_greater_than_restricted_version(): void {
1 efrain 180
        global $CFG;
181
        require_once($CFG->libdir.'/environmentlib.php');
182
 
183
        $result = new environment_results('php');
184
        $delimiter = '.';
185
        // Get the current PHP version.
186
        $currentversion = explode($delimiter, normalize_version(phpversion()));
187
        // Lets drop back one major version to ensure we trip the restriction.
188
        $currentversion[0]--;
189
        $restrictedversion = implode($delimiter, $currentversion);
190
 
191
        // Make sure the status is true before the test to see it flip to false.
192
        $result->setStatus(true);
193
 
194
        $this->assertTrue(restrict_php_version($result, $restrictedversion),
195
            'restrict_php_version returns true if the current version exceeds the restricted version');
196
    }
197
 
198
    /**
199
     * Test the restrict_php_version() function returns true if the current
200
     * PHP version is equal to the restricted version
201
     */
11 efrain 202
    public function test_restrict_php_version_equal_to_restricted_version(): void {
1 efrain 203
        global $CFG;
204
        require_once($CFG->libdir.'/environmentlib.php');
205
 
206
        $result = new environment_results('php');
207
        // Get the current PHP version.
208
        $currentversion = normalize_version(phpversion());
209
 
210
        // Make sure the status is true before the test to see it flip to false.
211
        $result->setStatus(true);
212
 
213
        $this->assertTrue(restrict_php_version($result, $currentversion),
214
            'restrict_php_version returns true if the current version is equal to the restricted version');
215
    }
216
 
217
    /**
218
     * Test the restrict_php_version() function returns false if the current
219
     * PHP version is less than the restricted version
220
     */
11 efrain 221
    public function test_restrict_php_version_less_than_restricted_version(): void {
1 efrain 222
        global $CFG;
223
        require_once($CFG->libdir.'/environmentlib.php');
224
 
225
        $result = new environment_results('php');
226
        $delimiter = '.';
227
        // Get the current PHP version.
228
        $currentversion = explode($delimiter, normalize_version(phpversion()));
229
        // Lets increase the major version to ensure don't trip the restriction.
230
        $currentversion[0]++;
231
        $restrictedversion = implode($delimiter, $currentversion);
232
 
233
        // Make sure the status is true before the test to see it flip to false.
234
        $result->setStatus(true);
235
 
236
        $this->assertFalse(restrict_php_version($result, $restrictedversion),
237
            'restrict_php_version returns false if the current version is less than the restricted version');
238
    }
239
}