From 80be1c2ede557f7d081a0856663bc5cbc3567c71 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Fri, 30 Jan 2026 15:42:10 +0800 Subject: [PATCH] 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. --- public/lib/setuplib.php | 4 ++-- public/lib/tests/setuplib_test.php | 7 ++++++- public/mod/data/tests/entries_export_test.php | 9 +++++---- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/public/lib/setuplib.php b/public/lib/setuplib.php index 89a9338eaad..037fe6c1de4 100644 --- a/public/lib/setuplib.php +++ b/public/lib/setuplib.php @@ -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 '; diff --git a/public/lib/tests/setuplib_test.php b/public/lib/tests/setuplib_test.php index 38d820565f9..353b7f92eb3 100644 --- a/public/lib/tests/setuplib_test.php +++ b/public/lib/tests/setuplib_test.php @@ -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); } } diff --git a/public/mod/data/tests/entries_export_test.php b/public/mod/data/tests/entries_export_test.php index 470e65834cd..8bfda0d392f 100644 --- a/public/mod/data/tests/entries_export_test.php +++ b/public/mod/data/tests/entries_export_test.php @@ -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