Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
/**
18
 * Cli script for testing readfile_accel function.
19
 *
20
 * @package    core
21
 * @subpackage fixtures
22
 * @copyright  2025 Catalyst IT
23
 * @author     Trisha Milan <trishamilan@catalyst-au.net>
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
 
27
define('CLI_SCRIPT', true);
28
 
29
require_once(__DIR__ . '/../../../config.php');
30
require_once($CFG->libdir . '/filelib.php');
31
 
32
if (!defined('PHPUNIT_READFILE_ACCEL_TEST')) {
33
    echo 'This script is only intended to be run via PHPUnit.';
34
    exit(1);
35
}
36
 
37
$testdb = moodle_database::get_driver_instance($CFG->dbtype, $CFG->dblibrary);
38
$testdb->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->phpunit_prefix);
39
$DB = $testdb;
40
 
41
set_debugging(DEBUG_DEVELOPER, true);
42
$CFG->tempdir = '/tmp';
43
 
44
/**
45
 * Runs readfile_accel() with a file path or a stored_file to trigger the buffer check.
46
 *
47
 * @param string|stored_file $input
48
 * @param string $mimetype
49
 * @param bool $accelerate
50
 */
51
function run_readfile_accel_test(string|stored_file $input, string $mimetype, bool $accelerate): void {
52
    try {
53
        ob_start();
54
        echo "test text";
55
        $_SERVER['REQUEST_METHOD'] = 'GET';
56
        readfile_accel($input, $mimetype, $accelerate);
57
    } catch (Exception $e) {
58
        echo $e->getMessage() . "\n";
59
    }
60
}
61
 
62
try {
63
    // Prepare test file.
64
    $filename = "readfile_accel.txt";
65
    // Generate temporary local file for testing.
66
    $path = "$CFG->tempdir/$filename";
67
    file_put_contents($path, "\nMoodle test data\n");
68
 
69
    // Populate {files} table.
70
    $fs = get_file_storage();
71
    $filerecord = [
72
        'contextid' => context_system::instance()->id,
73
        'component' => 'test',
74
        'filearea' => 'readfile',
75
        'itemid' => 0,
76
        'filepath' => '/',
77
        'filename' => $filename,
78
    ];
79
    $storedfile = null;
80
    $filerecord['filename'] = $fs->get_unused_filename(
81
        $filerecord['contextid'],
82
        $filerecord['component'],
83
        $filerecord['filearea'],
84
        $filerecord['itemid'],
85
        $filerecord['filepath'],
86
        $filerecord['filename']
87
    );
88
    $storedfile = $fs->create_file_from_pathname($filerecord, $path);
89
    $mimetype = get_mimetype_for_sending($storedfile->get_filename());
90
    $accelerate = true;
91
 
92
    // Run the test with direct path.
93
    run_readfile_accel_test($path, $mimetype, $accelerate);
94
 
95
    // Run the test with direct stored_file.
96
    run_readfile_accel_test($storedfile, $mimetype, $accelerate);
97
} finally {
98
    // Clean up {files} table.
99
    if (!is_null($fs)) {
100
        @$fs->delete_area_files(
101
            $filerecord['contextid'],
102
            $filerecord['component'],
103
            $filerecord['filearea'],
104
            $filerecord['itemid']
105
        );
106
    }
107
 
108
    // Clean up testing file.
109
    if ($path !== "") {
110
        @unlink($path);
111
    }
112
}