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
/**
20
 * Tests for lib/xsendfilelib.php.
21
 *
22
 * Please note that the PHP CLI SAPI used by PHPUnit does not return headers so some tests would be pointless to run.
23
 *
24
 * @package    core
25
 * @category   test
26
 * @copyright  2024 Andrew Lyons <andrew@nicols.co.uk>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 * @covers ::xsendfile
29
 */
30
final class xsendfilelib_test extends \advanced_testcase {
31
    public static function setUpBeforeClass(): void {
32
        global $CFG;
33
        require_once($CFG->libdir . '/xsendfilelib.php');
1441 ariadna 34
        parent::setUpBeforeClass();
1 efrain 35
    }
36
 
37
    public function test_not_enabled(): void {
38
        global $CFG;
39
 
40
        $this->resetAfterTest();
41
 
42
        // Ensure it is disabled.
43
        $CFG->xsendfile = '';
44
 
45
        // Use a file that would otherwise pass.
46
        $this->assertFalse(xsendfile($CFG->dataroot . '/.htaccess'));
47
    }
48
 
49
    public function test_file_not_found(): void {
50
        global $CFG;
51
 
52
        $this->resetAfterTest();
53
 
54
        // Ensure it is disabled.
55
        $CFG->xsendfile = 'X-Accel-Redirect';
56
 
57
        $this->assertFalse(xsendfile($CFG->dataroot . '/FILE_NOT_FOUND'));
58
    }
59
 
60
    public function test_file_found_headers_sent(): void {
61
        global $CFG;
62
 
63
        $this->resetAfterTest();
64
 
65
        // Ensure it is disabled.
66
        $CFG->xsendfile = 'X-Accel-Redirect';
67
 
68
        // This is a weird ond - we can't explicitly send headers, but we know that phpunit does.
69
        $this->assertFalse(xsendfile($CFG->dataroot . '/.htaccess'));
70
    }
71
 
72
    /**
73
     * Test that a file served from a request dir is not served.
74
     *
75
     * @runInSeparateProcess
76
     */
77
    public function test_file_found_request_dir(): void {
78
        global $CFG;
79
 
80
        $this->resetAfterTest();
81
 
82
        // Ensure it is disabled.
83
        $CFG->xsendfile = 'X-Accel-Redirect';
84
        $CFG->xsendfilealiases = [
85
            '/request/' => make_request_directory(),
86
        ];
87
 
88
        $dir = make_request_directory();
89
        $file = $dir . '/testfile.txt';
90
        file_put_contents($file, 'Hello, world!');
91
 
92
        // Use a file that would otherwise pass.
93
        $this->assertFalse(xsendfile($file));
94
    }
95
 
96
    /**
97
     * Test that a file served from an aliased dir is served.
98
     *
99
     * @runInSeparateProcess
100
     */
101
    public function test_nginx_accelerated(): void {
102
        global $CFG;
103
 
104
        $this->resetAfterTest();
105
 
106
        // Ensure it is enabled.
107
        $CFG->xsendfile = 'X-Accel-Redirect';
108
        $CFG->xsendfilealiases = [
109
            '/my/moodle/alias/moodledata/' => $CFG->dataroot,
110
        ];
111
 
112
        $file = $CFG->dataroot . '/testfile.txt';
113
        file_put_contents($file, 'Hello, world!');
114
 
115
        $this->assertTrue(xsendfile($file));
116
 
117
        // Note: The `headers_list()` method does not work with the CLI SAPI.
118
        // We can use xdebug if it's enabled.
119
        // This is mostly to aid debugging as it is not common to have xdebug enabled during CI tests.
120
        if (extension_loaded('xdebug')) {
121
            $headers = xdebug_get_headers();
122
            $this->assertNotEmpty($headers);
123
            $this->assertContains('X-Accel-Redirect: /my/moodle/alias/moodledata/testfile.txt', $headers);
124
        }
125
    }
126
 
127
    /**
128
     * Test that a file served from an unknown alias is not served.
129
     *
130
     * @runInSeparateProcess
131
     */
132
    public function test_nginx_no_alias(): void {
133
        global $CFG;
134
 
135
        $this->resetAfterTest();
136
 
137
        // Ensure it is enabled.
138
        $CFG->xsendfile = 'X-Accel-Redirect';
139
        $CFG->xsendfilealiases = [
140
            '/my/moodle/alias/requestdir/' => make_request_directory(),
141
        ];
142
 
143
        $file = $CFG->dataroot . '/testfile.txt';
144
        file_put_contents($file, 'Hello, world!');
145
 
146
        $this->assertFalse(xsendfile($file));
147
    }
148
 
149
    /**
150
     * Test that an alias dir which doesn't exist is ignored.
151
     *
152
     * @runInSeparateProcess
153
     */
154
    public function test_nginx_alias_dir_not_found(): void {
155
        global $CFG;
156
 
157
        $this->resetAfterTest();
158
 
159
        $filedir = "{$CFG->dataroot}/non/existent/directory";
160
 
161
        // Ensure it is enabled.
162
        $CFG->xsendfile = 'X-Accel-Redirect';
163
        $CFG->xsendfilealiases = [
164
            '/my/moodle/alias/' => $filedir,
165
        ];
166
 
167
        $file = $CFG->dataroot . '/testfile.txt';
168
        file_put_contents($file, 'Hello, world!');
169
 
170
        $this->assertFalse(xsendfile($file));
171
    }
172
}