This commit is contained in:
Adrian Greeve
2025-12-04 20:57:32 +07:00
committed by Huong Nguyen
3 changed files with 39 additions and 16 deletions
+8 -8
View File
@@ -502,7 +502,7 @@ final class dml_test extends \database_driver_testcase {
$out = $fixture->four($sql);
$expected = <<<EOD
SELECT \* FROM {users}
-- line \d+ of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to ReflectionMethod->invoke\(\)
-- line \d+ of /public/lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to ReflectionMethod->invoke\(\)
EOD;
$this->assertMatchesRegularExpression('@' . $this->unix_to_os_dirsep($expected) . '@', $out);
@@ -510,8 +510,8 @@ EOD;
$out = $fixture->four($sql);
$expected = <<<EOD
SELECT \* FROM {users}
-- line \d+ of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to ReflectionMethod->invoke\(\)
-- line \d+ of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to test_dml_sql_debugging_fixture->one\(\)
-- line \d+ of /public/lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to ReflectionMethod->invoke\(\)
-- line \d+ of /public/lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to test_dml_sql_debugging_fixture->one\(\)
EOD;
$this->assertMatchesRegularExpression('@' . $this->unix_to_os_dirsep($expected) . '@', $out);
@@ -519,11 +519,11 @@ EOD;
$out = $fixture->four($sql);
$expected = <<<EOD
SELECT \* FROM {users}
-- line \d+ of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to ReflectionMethod->invoke\(\)
-- line \d+ of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to test_dml_sql_debugging_fixture->one\(\)
-- line \d+ of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to test_dml_sql_debugging_fixture->two\(\)
-- line \d+ of /lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to test_dml_sql_debugging_fixture->three\(\)
-- line \d+ of /lib/dml/tests/dml_test.php: call to test_dml_sql_debugging_fixture->four\(\)
-- line \d+ of /public/lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to ReflectionMethod->invoke\(\)
-- line \d+ of /public/lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to test_dml_sql_debugging_fixture->one\(\)
-- line \d+ of /public/lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to test_dml_sql_debugging_fixture->two\(\)
-- line \d+ of /public/lib/dml/tests/fixtures/test_dml_sql_debugging_fixture.php: call to test_dml_sql_debugging_fixture->three\(\)
-- line \d+ of /public/lib/dml/tests/dml_test.php: call to test_dml_sql_debugging_fixture->four\(\)
EOD;
$this->assertMatchesRegularExpression('@' . $this->unix_to_os_dirsep($expected) . '@', $out);
+11 -7
View File
@@ -471,8 +471,8 @@ function get_docs_url($path = null) {
* @return string formatted backtrace, ready for output.
*/
function format_backtrace($callers, $plaintext = false) {
// do not use $CFG->dirroot because it might not be available in destructors
$dirroot = dirname(__DIR__);
// Do not use $CFG->dirroot because it might not be available in destructors.
$dirroot = dirname(__DIR__, 2);
if (empty($callers)) {
return '';
@@ -481,21 +481,25 @@ function format_backtrace($callers, $plaintext = false) {
$from = $plaintext ? '' : '<ul style="text-align: left" data-rel="backtrace">';
foreach ($callers as $caller) {
if (!isset($caller['line'])) {
$caller['line'] = '?'; // probably call_user_func()
$caller['line'] = '?'; // Probably call_user_func().
}
if (!isset($caller['file'])) {
$caller['file'] = 'unknownfile'; // probably call_user_func()
$caller['file'] = 'unknownfile'; // Probably call_user_func().
}
$line = $plaintext ? '* ' : '<li>';
$line .= 'line ' . $caller['line'] . ' of ' . str_replace($dirroot, '', $caller['file']);
$line .= sprintf(
'line %d of %s',
$caller['line'],
str_replace($dirroot, '', $caller['file']),
);
if (isset($caller['function'])) {
$line .= ': call to ';
if (isset($caller['class'])) {
$line .= $caller['class'] . $caller['type'];
}
$line .= $caller['function'] . '()';
$line .= "{$caller['function']}()";
} else if (isset($caller['exception'])) {
$line .= ': '.$caller['exception'].' thrown';
$line .= ": {$caller['exception']} thrown";
}
// Remove any non printable chars.
+20 -1
View File
@@ -24,8 +24,8 @@ namespace core;
* @copyright 2012 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
#[\PHPUnit\Framework\Attributes\CoversFunction('format_backtrace')]
final class setuplib_test extends \advanced_testcase {
/**
* Test get_docs_url_standard in the normal case when we should link to Moodle docs.
*/
@@ -540,4 +540,23 @@ final class setuplib_test extends \advanced_testcase {
$this->expectNotToPerformAssertions();
require_phpunit_isolation();
}
/**
* Ensure that formatted backtraces do not contain a full path but do contain the public dir.
*/
public function test_format_backtrace(): void {
// Note: Stack traces do not include the path for the location that they are generated.
// Use an IIFE to add an extra frame to the trace.
$backtrace = (fn (): array => debug_backtrace())();
$output = format_backtrace($backtrace, true);
// It should not contain the full dir.
$this->assertStringNotContainsString(__DIR__, $output);
// But it should contain the local variant.
$this->assertStringContainsString(' of /public/lib/tests/setuplib_test.php', $output);
// And a vendor path.
$this->assertStringContainsString(' of /vendor/', $output);
}
}