MDL-87716 core: Fix failing unit tests

Some tests in core were using paths relative to the process CWD, which
is not safe.

The environment tests needed to be updated to use the Composer
autoloader.
This commit is contained in:
Andrew Nicols
2026-02-11 15:11:59 +08:00
parent 778d3e88c6
commit 80be1c2ede
3 changed files with 13 additions and 7 deletions
+2 -2
View File
@@ -472,7 +472,7 @@ function get_docs_url($path = null) {
*/
function format_backtrace($callers, $plaintext = false) {
// Do not use $CFG->dirroot because it might not be available in destructors.
$dirroot = dirname(__DIR__, 2);
$dirroot = realpath(dirname(__DIR__, 2));
if (empty($callers)) {
return '';
@@ -490,7 +490,7 @@ function format_backtrace($callers, $plaintext = false) {
$line .= sprintf(
'line %d of %s',
$caller['line'],
str_replace($dirroot, '', $caller['file']),
str_replace($dirroot, '', realpath($caller['file'])),
);
if (isset($caller['function'])) {
$line .= ': call to ';
+6 -1
View File
@@ -557,6 +557,11 @@ final class setuplib_test extends \advanced_testcase {
$this->assertStringContainsString(' of /public/lib/tests/setuplib_test.php', $output);
// And a vendor path.
$this->assertStringContainsString(' of /vendor/', $output);
$rootdir = realpath(\Composer\InstalledVersions::getRootPackage()['install_path']);
// If the vendor directory is within the $CFG->root it won't be present.
if (str_starts_with($rootdir, realpath(dirname(__DIR__, 3)))) {
$rootdir = '';
}
$this->assertStringContainsString(" of {$rootdir}/vendor", $output);
}
}
@@ -101,7 +101,8 @@ final class entries_export_test extends \advanced_testcase {
* @covers \mod_data\local\exporter\utils::data_exportdata
*/
public function test_export_csv(): void {
global $DB;
global $CFG, $DB;
[
'data' => $data,
'cm' => $cm,
@@ -151,9 +152,9 @@ final class entries_export_test extends \advanced_testcase {
// file stored in a file and picture field.
// So we expect that this file has to have the same content as sample.png.
// Also, the default value for the subdirectory in the zip archive containing the files is 'files/'.
'files/samplefile.png' => 'public/mod/data/field/picture/pix/sample.png',
'files/samplefile_1.png' => 'public/mod/data/field/picture/pix/sample.png',
'files/picturefile.png' => 'public/mod/data/field/picture/pix/sample.png',
'files/samplefile.png' => "{$CFG->root}/public/mod/data/field/picture/pix/sample.png",
'files/samplefile_1.png' => "{$CFG->root}/public/mod/data/field/picture/pix/sample.png",
'files/picturefile.png' => "{$CFG->root}/public/mod/data/field/picture/pix/sample.png",
// By checking that the content of the exported csv is identical to the fixture file it is verified
// that the filenames in the csv file correspond to the names of the exported file.
// It also verifies that files with identical file names in different fields (or records) will be numbered