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');
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
public function test_not_enabled(): void {
|
|
|
37 |
global $CFG;
|
|
|
38 |
|
|
|
39 |
$this->resetAfterTest();
|
|
|
40 |
|
|
|
41 |
// Ensure it is disabled.
|
|
|
42 |
$CFG->xsendfile = '';
|
|
|
43 |
|
|
|
44 |
// Use a file that would otherwise pass.
|
|
|
45 |
$this->assertFalse(xsendfile($CFG->dataroot . '/.htaccess'));
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
public function test_file_not_found(): void {
|
|
|
49 |
global $CFG;
|
|
|
50 |
|
|
|
51 |
$this->resetAfterTest();
|
|
|
52 |
|
|
|
53 |
// Ensure it is disabled.
|
|
|
54 |
$CFG->xsendfile = 'X-Accel-Redirect';
|
|
|
55 |
|
|
|
56 |
$this->assertFalse(xsendfile($CFG->dataroot . '/FILE_NOT_FOUND'));
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
public function test_file_found_headers_sent(): void {
|
|
|
60 |
global $CFG;
|
|
|
61 |
|
|
|
62 |
$this->resetAfterTest();
|
|
|
63 |
|
|
|
64 |
// Ensure it is disabled.
|
|
|
65 |
$CFG->xsendfile = 'X-Accel-Redirect';
|
|
|
66 |
|
|
|
67 |
// This is a weird ond - we can't explicitly send headers, but we know that phpunit does.
|
|
|
68 |
$this->assertFalse(xsendfile($CFG->dataroot . '/.htaccess'));
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Test that a file served from a request dir is not served.
|
|
|
73 |
*
|
|
|
74 |
* @runInSeparateProcess
|
|
|
75 |
*/
|
|
|
76 |
public function test_file_found_request_dir(): void {
|
|
|
77 |
global $CFG;
|
|
|
78 |
|
|
|
79 |
$this->resetAfterTest();
|
|
|
80 |
|
|
|
81 |
// Ensure it is disabled.
|
|
|
82 |
$CFG->xsendfile = 'X-Accel-Redirect';
|
|
|
83 |
$CFG->xsendfilealiases = [
|
|
|
84 |
'/request/' => make_request_directory(),
|
|
|
85 |
];
|
|
|
86 |
|
|
|
87 |
$dir = make_request_directory();
|
|
|
88 |
$file = $dir . '/testfile.txt';
|
|
|
89 |
file_put_contents($file, 'Hello, world!');
|
|
|
90 |
|
|
|
91 |
// Use a file that would otherwise pass.
|
|
|
92 |
$this->assertFalse(xsendfile($file));
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Test that a file served from an aliased dir is served.
|
|
|
97 |
*
|
|
|
98 |
* @runInSeparateProcess
|
|
|
99 |
*/
|
|
|
100 |
public function test_nginx_accelerated(): void {
|
|
|
101 |
global $CFG;
|
|
|
102 |
|
|
|
103 |
$this->resetAfterTest();
|
|
|
104 |
|
|
|
105 |
// Ensure it is enabled.
|
|
|
106 |
$CFG->xsendfile = 'X-Accel-Redirect';
|
|
|
107 |
$CFG->xsendfilealiases = [
|
|
|
108 |
'/my/moodle/alias/moodledata/' => $CFG->dataroot,
|
|
|
109 |
];
|
|
|
110 |
|
|
|
111 |
$file = $CFG->dataroot . '/testfile.txt';
|
|
|
112 |
file_put_contents($file, 'Hello, world!');
|
|
|
113 |
|
|
|
114 |
$this->assertTrue(xsendfile($file));
|
|
|
115 |
|
|
|
116 |
// Note: The `headers_list()` method does not work with the CLI SAPI.
|
|
|
117 |
// We can use xdebug if it's enabled.
|
|
|
118 |
// This is mostly to aid debugging as it is not common to have xdebug enabled during CI tests.
|
|
|
119 |
if (extension_loaded('xdebug')) {
|
|
|
120 |
$headers = xdebug_get_headers();
|
|
|
121 |
$this->assertNotEmpty($headers);
|
|
|
122 |
$this->assertContains('X-Accel-Redirect: /my/moodle/alias/moodledata/testfile.txt', $headers);
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
/**
|
|
|
127 |
* Test that a file served from an unknown alias is not served.
|
|
|
128 |
*
|
|
|
129 |
* @runInSeparateProcess
|
|
|
130 |
*/
|
|
|
131 |
public function test_nginx_no_alias(): void {
|
|
|
132 |
global $CFG;
|
|
|
133 |
|
|
|
134 |
$this->resetAfterTest();
|
|
|
135 |
|
|
|
136 |
// Ensure it is enabled.
|
|
|
137 |
$CFG->xsendfile = 'X-Accel-Redirect';
|
|
|
138 |
$CFG->xsendfilealiases = [
|
|
|
139 |
'/my/moodle/alias/requestdir/' => make_request_directory(),
|
|
|
140 |
];
|
|
|
141 |
|
|
|
142 |
$file = $CFG->dataroot . '/testfile.txt';
|
|
|
143 |
file_put_contents($file, 'Hello, world!');
|
|
|
144 |
|
|
|
145 |
$this->assertFalse(xsendfile($file));
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
/**
|
|
|
149 |
* Test that an alias dir which doesn't exist is ignored.
|
|
|
150 |
*
|
|
|
151 |
* @runInSeparateProcess
|
|
|
152 |
*/
|
|
|
153 |
public function test_nginx_alias_dir_not_found(): void {
|
|
|
154 |
global $CFG;
|
|
|
155 |
|
|
|
156 |
$this->resetAfterTest();
|
|
|
157 |
|
|
|
158 |
$filedir = "{$CFG->dataroot}/non/existent/directory";
|
|
|
159 |
|
|
|
160 |
// Ensure it is enabled.
|
|
|
161 |
$CFG->xsendfile = 'X-Accel-Redirect';
|
|
|
162 |
$CFG->xsendfilealiases = [
|
|
|
163 |
'/my/moodle/alias/' => $filedir,
|
|
|
164 |
];
|
|
|
165 |
|
|
|
166 |
$file = $CFG->dataroot . '/testfile.txt';
|
|
|
167 |
file_put_contents($file, 'Hello, world!');
|
|
|
168 |
|
|
|
169 |
$this->assertFalse(xsendfile($file));
|
|
|
170 |
}
|
|
|
171 |
}
|