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
defined('MOODLE_INTERNAL') || die();
20
 
21
// Global $CFG not used here intentionally to make sure it is not required inside the lib.
22
require_once(__DIR__ . '/../configonlylib.php');
23
 
24
/**
25
 * Unit tests for config only library functions.
26
 *
27
 * @package    core
28
 * @category   test
29
 * @copyright  2012 Petr Skoda {@link http://skodak.org}
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
class configonlylib_test extends \advanced_testcase {
33
 
34
    /**
35
     * Test cleaning of invalid utf-8 entities.
36
     */
37
    public function test_min_fix_utf8() {
38
        $this->assertSame('abc', min_fix_utf8('abc'));
39
        $this->assertSame("žlutý koníček přeskočil potůček \n\t\r", min_fix_utf8("žlutý koníček přeskočil potůček \n\t\r\0"));
40
        $this->assertSame('aš', min_fix_utf8('a'.chr(130).'š'), 'This fails with buggy iconv() when mbstring extenstion is not available as fallback.');
41
    }
42
 
43
    /**
44
     * Test minimalistic parameter cleaning.
45
     */
46
    public function test_min_clean_param() {
47
        $this->assertSame('foo', min_clean_param('foo', 'RAW'));
48
        $this->assertSame('aš', min_clean_param('a'.chr(130).'š', 'RAW'));
49
 
50
        $this->assertSame(1, min_clean_param('1', 'INT'));
51
        $this->assertSame(1, min_clean_param('1aa', 'INT'));
52
 
53
        $this->assertSame('1abc-d_f', min_clean_param('/.1ačž"b?;c-d{}\\_f.', 'SAFEDIR'));
54
        $this->assertSame(1, min_clean_param('1aa', 'INT'));
55
 
56
        $this->assertSame('/a/b/./c5', min_clean_param('/a*?$//b/.../c5', 'SAFEPATH'));
57
        $this->assertSame(1, min_clean_param('1aa', 'INT'));
58
    }
59
 
60
    /**
61
     * Test minimalistic getting of page parameters.
62
     */
63
    public function test_min_optional_param() {
64
        $this->resetAfterTest();
65
 
66
        $_GET['foo'] = 'bar';
67
        $_GET['num'] = '1';
68
        $_GET['xnum'] = '1aa';
69
 
70
        $_POST['foo'] = 'rebar';
71
        $_POST['oof'] = 'rab';
72
 
73
        $this->assertSame('bar', min_optional_param('foo', null, 'RAW'));
74
        $this->assertSame(null, min_optional_param('foo2', null, 'RAW'));
75
        $this->assertSame('rab', min_optional_param('oof', null, 'RAW'));
76
 
77
        $this->assertSame(1, min_optional_param('num', null, 'INT'));
78
        $this->assertSame(1, min_optional_param('xnum', null, 'INT'));
79
    }
80
 
81
    /**
82
     * Test fail-safe minimalistic slashargument processing.
83
     */
84
    public function test_min_get_slash_argument() {
85
        global $CFG;
86
 
87
        $this->resetAfterTest();
88
        $this->assertEquals('https://www.example.com/moodle', $CFG->wwwroot);
89
 
90
        $_SERVER = array();
91
        $_SERVER['SERVER_SOFTWARE'] = 'Apache/2.2.22 (Unix)';
92
        $_SERVER['QUERY_STRING'] = 'theme=standard&component=core&rev=5&image=u/f1';
93
        $_SERVER['REQUEST_URI'] = '/moodle/theme/image.php?theme=standard&component=core&rev=5&image=u/f1';
94
        $_SERVER['SCRIPT_NAME'] = '/moodle/theme/image.php';
95
        $this->assertSame('', min_get_slash_argument());
96
 
97
        $_SERVER = array();
98
        $_SERVER['SERVER_SOFTWARE'] = 'Apache/2.2.22 (Unix)';
99
        $_SERVER['QUERY_STRING'] = '';
100
        $_SERVER['REQUEST_URI'] = '/moodle/theme/image.php/standard/core/5/u/f1';
101
        $_SERVER['PATH_INFO'] = '/standard/core/5/u/f1';
102
        $_SERVER['SCRIPT_NAME'] = '/moodle/theme/image.php';
103
        $_GET = array();
104
        $this->assertSame('/standard/core/5/u/f1', min_get_slash_argument());
105
 
106
        // IIS no url rewriting.
107
        $_SERVER = array();
108
        $_SERVER['SERVER_SOFTWARE'] = 'Microsoft-IIS/7.0';
109
        $_SERVER['QUERY_STRING'] = '';
110
        $_SERVER['REQUEST_URI'] = '/moodle/theme/image.php/standard/core/5/u/f1';
111
        $_SERVER['PATH_INFO'] = '/standard/core/5/u/f1';
112
        $_SERVER['SCRIPT_NAME'] = '/moodle/theme/image.php';
113
        $_GET = array();
114
        $this->assertSame('/standard/core/5/u/f1', min_get_slash_argument());
115
 
116
        // IIS with url rewriting.
117
        $_SERVER = array();
118
        $_SERVER['SERVER_SOFTWARE'] = 'Microsoft-IIS/7.0';
119
        $_SERVER['QUERY_STRING'] = 'file=/standard/core/5/u/f1';
120
        $_SERVER['REQUEST_URI'] = '/moodle/theme/image.php/standard/core/5/u/f1';
121
        $_SERVER['PATH_INFO'] = '/';
122
        $_SERVER['SCRIPT_NAME'] = '/moodle/theme/image.php';
123
        $_GET = array();
124
        $_GET['file'] = '/standard/core/5/u/f1';
125
        $this->assertSame('/standard/core/5/u/f1', min_get_slash_argument());
126
 
127
        $_SERVER = array();
128
        $_SERVER['SERVER_SOFTWARE'] = 'Weird server';
129
        $_SERVER['QUERY_STRING'] = '';
130
        $_SERVER['REQUEST_URI'] = '/moodle/theme/image.php/standard/core/5/u/f1';
131
        $_SERVER['PATH_INFO'] = '/moodle/theme/image.php/standard/core/5/u/f1';
132
        $_SERVER['SCRIPT_NAME'] = '/moodle/theme/image.php';
133
        $_GET = array();
134
        $this->assertSame('/standard/core/5/u/f1', min_get_slash_argument());
135
 
136
        $_SERVER = array();
137
        $_SERVER['SERVER_SOFTWARE'] = 'Hacker server';
138
        $_SERVER['QUERY_STRING'] = '';
139
        $_SERVER['REQUEST_URI'] = '/moodle/theme/image.php/standard/core/5/u/f1';
140
        $_SERVER['PATH_INFO'] = '/moodle/theme/image.php/standard\\core/..\\../5/u/f1';
141
        $_SERVER['SCRIPT_NAME'] = '/moodle/theme/image.php';
142
        $_GET = array();
143
        // Windows dir separators are removed, multiple ... gets collapsed to one .
144
        $this->assertSame('/standardcore/./5/u/f1', min_get_slash_argument());
145
    }
146
 
147
    /**
148
     * Test the min_get_minimum_version function.
149
     *
150
     * @covers ::min_get_minimum_version
151
     */
152
    public function test_min_get_minimum_version(): void {
153
        // This is fairly hard to write a test for, but we can at least check that it returns a number
154
        // greater than the version when the feature was first introduced.
155
        $firstintroduced = 1693612800; // Equivalent to 20230902 00:00:00 GMT.
156
        // Deduct our two day tolerance.
157
        $firstintroduced = $firstintroduced - (DAYSECS * 2);
158
        $this->assertGreaterThanOrEqual($firstintroduced, min_get_minimum_revision());
159
    }
160
 
161
    /**
162
     * Test the min_get_maximum_version function.
163
     *
164
     * @covers ::min_get_maximum_version
165
     */
166
    public function test_min_get_maximum_version(): void {
167
        // The maximum version should be set to a time in the near future.
168
        // This is currently defined as "in the next minute".
169
        // Note: We use a 65 second window to allow for slow test runners.
170
        $this->assertGreaterThan(time(), min_get_maximum_revision());
171
        $this->assertLessThanOrEqual(time() + 65, min_get_maximum_revision());
172
    }
173
 
174
    /**
175
     * Test the min_is_revision_valid_and_current function.
176
     *
177
     * @covers ::min_is_revision_valid_and_current
178
     * @dataProvider min_is_revision_valid_and_current_provider
179
     */
180
    public function test_min_is_revision_valid_and_current(int $revision, bool $expected): void {
181
        $this->assertEquals($expected, min_is_revision_valid_and_current($revision));
182
    }
183
 
184
    /**
185
     * Data provider for the min_is_revision_valid_and_current tests.
186
     *
187
     * @return array
188
     */
189
    public function min_is_revision_valid_and_current_provider(): array {
190
        return [
191
            'Negative value' => [-1, false],
192
            'Empty value' => [0, false],
193
            'A time before the minimum accepted value' => [min_get_minimum_revision() - 1, false],
194
            'The minimum accepted value' => [min_get_minimum_revision(), true],
195
            'The current time' => [time(), true],
196
            // Note: We have to be careful using time values because the data provider is run at the start of the phpunit run,
197
            // but the test may not be run for some time.
198
            // On a slower machine and/or database, this could be several hours.
199
            // For a more specific time we must have a specific test function.
200
            'A time in the future' => [time() + DAYSECS, false]
201
        ];
202
    }
203
 
204
 
205
    /**
206
     * Test the min_is_revision_valid_and_current function with close times.
207
     *
208
     * Note: These tests are incompatible with data providers.
209
     *
210
     * @covers ::min_is_revision_valid_and_current
211
     */
212
    public function test_min_is_revision_valid_and_current_close_proximity(): void {
213
        // A time in the near future.
214
        $this->assertTrue(min_is_revision_valid_and_current(time() + 55));
215
 
216
        // A time in the too-far future.
217
        $this->assertFalse(min_is_revision_valid_and_current(time() + 70));
218
 
219
    }
220
}