wiki.techinc.nl/tests/phpunit/includes/debug/MWDebugTest.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

211 lines
5.6 KiB
PHP
Raw Normal View History

<?php
use MediaWiki\Context\RequestContext;
use MediaWiki\Debug\MWDebug;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MainConfigNames;
use MediaWiki\Request\FauxRequest;
use MediaWiki\Title\TitleValue;
MWDebug: Add support for native PHP warnings to "Console" == Change == Remove integration from MWDebug methods in favour of capturing messages from the 'error' channel. This way we fix the following that we were all blind to: * PHP built-in errors for native notices and deprecation warnings. * trigger_error calls from our own code such as in standolone libs, where we can't use wfLogWarning or wfDeprecated, and instead use E_USER_WARNING and E_USER_DEPRECATED instead. * trigger_error calls from third-party Composer libraries, which similarly can't and don't know about these at all. I removed support for including a backtrace. XDebug does a better job of this nowadays, and the information is also available in mw-error.log already in a more readable manner. Cramming it into a 100px scrollable area made the "Console" tab much less readable in my opinion. To make it work, we'd need to duplicate the pretty string formatting code of wfBacktrace() and make it re-usable when given only a trace array (rather than capture a new backtrace). I considered re-using MWExceptionHandler::prettyPrintTrace but that is far more verbose than the string format in wfBacktrace. Creating yet another untested error pretty print function seemed not worthwhile as imho in the majority of cases the $caller suffices, and when not, the toolbar suffices as a nudge to take a look in mw-debug.log, or check XDebug display_errors, or engage an IDE. == Test plan == 1. Add the following to MediaWiki.php#main() as example: ``` wfLogWarning('Hello'); wfDeprecated('Something'); trigger_error('Goodbye'); $x = []; $a = $x['a']; ``` 2. Before this change, your native display_errors=1 or XDebug shows all four. With $wgDebugToolbar enabled, the "Console" tab showed only the wfLogWarning and wfDeprecated entry. 3. After this change, all four are included Change-Id: I62d864823ec8ab9b940aae0e0f47b47a728ba861
2023-10-03 04:30:44 +00:00
use Psr\Log\LoggerInterface;
/**
* @covers \MediaWiki\Debug\MWDebug
*/
class MWDebugTest extends MediaWikiIntegrationTestCase {
protected function setUp(): void {
$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, false );
MWDebug: Add support for native PHP warnings to "Console" == Change == Remove integration from MWDebug methods in favour of capturing messages from the 'error' channel. This way we fix the following that we were all blind to: * PHP built-in errors for native notices and deprecation warnings. * trigger_error calls from our own code such as in standolone libs, where we can't use wfLogWarning or wfDeprecated, and instead use E_USER_WARNING and E_USER_DEPRECATED instead. * trigger_error calls from third-party Composer libraries, which similarly can't and don't know about these at all. I removed support for including a backtrace. XDebug does a better job of this nowadays, and the information is also available in mw-error.log already in a more readable manner. Cramming it into a 100px scrollable area made the "Console" tab much less readable in my opinion. To make it work, we'd need to duplicate the pretty string formatting code of wfBacktrace() and make it re-usable when given only a trace array (rather than capture a new backtrace). I considered re-using MWExceptionHandler::prettyPrintTrace but that is far more verbose than the string format in wfBacktrace. Creating yet another untested error pretty print function seemed not worthwhile as imho in the majority of cases the $caller suffices, and when not, the toolbar suffices as a nudge to take a look in mw-debug.log, or check XDebug display_errors, or engage an IDE. == Test plan == 1. Add the following to MediaWiki.php#main() as example: ``` wfLogWarning('Hello'); wfDeprecated('Something'); trigger_error('Goodbye'); $x = []; $a = $x['a']; ``` 2. Before this change, your native display_errors=1 or XDebug shows all four. With $wgDebugToolbar enabled, the "Console" tab showed only the wfLogWarning and wfDeprecated entry. 3. After this change, all four are included Change-Id: I62d864823ec8ab9b940aae0e0f47b47a728ba861
2023-10-03 04:30:44 +00:00
parent::setUp();
/** Clear log before each test */
MWDebug::clearLog();
}
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
MWDebug::init();
}
public static function tearDownAfterClass(): void {
MWDebug::deinit();
parent::tearDownAfterClass();
}
MWDebug: Add support for native PHP warnings to "Console" == Change == Remove integration from MWDebug methods in favour of capturing messages from the 'error' channel. This way we fix the following that we were all blind to: * PHP built-in errors for native notices and deprecation warnings. * trigger_error calls from our own code such as in standolone libs, where we can't use wfLogWarning or wfDeprecated, and instead use E_USER_WARNING and E_USER_DEPRECATED instead. * trigger_error calls from third-party Composer libraries, which similarly can't and don't know about these at all. I removed support for including a backtrace. XDebug does a better job of this nowadays, and the information is also available in mw-error.log already in a more readable manner. Cramming it into a 100px scrollable area made the "Console" tab much less readable in my opinion. To make it work, we'd need to duplicate the pretty string formatting code of wfBacktrace() and make it re-usable when given only a trace array (rather than capture a new backtrace). I considered re-using MWExceptionHandler::prettyPrintTrace but that is far more verbose than the string format in wfBacktrace. Creating yet another untested error pretty print function seemed not worthwhile as imho in the majority of cases the $caller suffices, and when not, the toolbar suffices as a nudge to take a look in mw-debug.log, or check XDebug display_errors, or engage an IDE. == Test plan == 1. Add the following to MediaWiki.php#main() as example: ``` wfLogWarning('Hello'); wfDeprecated('Something'); trigger_error('Goodbye'); $x = []; $a = $x['a']; ``` 2. Before this change, your native display_errors=1 or XDebug shows all four. With $wgDebugToolbar enabled, the "Console" tab showed only the wfLogWarning and wfDeprecated entry. 3. After this change, all four are included Change-Id: I62d864823ec8ab9b940aae0e0f47b47a728ba861
2023-10-03 04:30:44 +00:00
public function testLog() {
@MWDebug::log( 'logging a string' );
$this->assertEquals(
[ [
'msg' => 'logging a string',
'type' => 'log',
MWDebug: Add support for native PHP warnings to "Console" == Change == Remove integration from MWDebug methods in favour of capturing messages from the 'error' channel. This way we fix the following that we were all blind to: * PHP built-in errors for native notices and deprecation warnings. * trigger_error calls from our own code such as in standolone libs, where we can't use wfLogWarning or wfDeprecated, and instead use E_USER_WARNING and E_USER_DEPRECATED instead. * trigger_error calls from third-party Composer libraries, which similarly can't and don't know about these at all. I removed support for including a backtrace. XDebug does a better job of this nowadays, and the information is also available in mw-error.log already in a more readable manner. Cramming it into a 100px scrollable area made the "Console" tab much less readable in my opinion. To make it work, we'd need to duplicate the pretty string formatting code of wfBacktrace() and make it re-usable when given only a trace array (rather than capture a new backtrace). I considered re-using MWExceptionHandler::prettyPrintTrace but that is far more verbose than the string format in wfBacktrace. Creating yet another untested error pretty print function seemed not worthwhile as imho in the majority of cases the $caller suffices, and when not, the toolbar suffices as a nudge to take a look in mw-debug.log, or check XDebug display_errors, or engage an IDE. == Test plan == 1. Add the following to MediaWiki.php#main() as example: ``` wfLogWarning('Hello'); wfDeprecated('Something'); trigger_error('Goodbye'); $x = []; $a = $x['a']; ``` 2. Before this change, your native display_errors=1 or XDebug shows all four. With $wgDebugToolbar enabled, the "Console" tab showed only the wfLogWarning and wfDeprecated entry. 3. After this change, all four are included Change-Id: I62d864823ec8ab9b940aae0e0f47b47a728ba861
2023-10-03 04:30:44 +00:00
'caller' => 'MWDebugTest->testLog',
] ],
MWDebug::getLog()
);
}
MWDebug: Add support for native PHP warnings to "Console" == Change == Remove integration from MWDebug methods in favour of capturing messages from the 'error' channel. This way we fix the following that we were all blind to: * PHP built-in errors for native notices and deprecation warnings. * trigger_error calls from our own code such as in standolone libs, where we can't use wfLogWarning or wfDeprecated, and instead use E_USER_WARNING and E_USER_DEPRECATED instead. * trigger_error calls from third-party Composer libraries, which similarly can't and don't know about these at all. I removed support for including a backtrace. XDebug does a better job of this nowadays, and the information is also available in mw-error.log already in a more readable manner. Cramming it into a 100px scrollable area made the "Console" tab much less readable in my opinion. To make it work, we'd need to duplicate the pretty string formatting code of wfBacktrace() and make it re-usable when given only a trace array (rather than capture a new backtrace). I considered re-using MWExceptionHandler::prettyPrintTrace but that is far more verbose than the string format in wfBacktrace. Creating yet another untested error pretty print function seemed not worthwhile as imho in the majority of cases the $caller suffices, and when not, the toolbar suffices as a nudge to take a look in mw-debug.log, or check XDebug display_errors, or engage an IDE. == Test plan == 1. Add the following to MediaWiki.php#main() as example: ``` wfLogWarning('Hello'); wfDeprecated('Something'); trigger_error('Goodbye'); $x = []; $a = $x['a']; ``` 2. Before this change, your native display_errors=1 or XDebug shows all four. With $wgDebugToolbar enabled, the "Console" tab showed only the wfLogWarning and wfDeprecated entry. 3. After this change, all four are included Change-Id: I62d864823ec8ab9b940aae0e0f47b47a728ba861
2023-10-03 04:30:44 +00:00
public function testWarningProduction() {
$logger = $this->createMock( LoggerInterface::class );
$logger->expects( $this->once() )->method( 'info' );
$this->setLogger( 'warning', $logger );
@MWDebug::warning( 'Ohnosecond!' );
}
public function testWarningDevelopment() {
$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, true );
MWDebug: Add support for native PHP warnings to "Console" == Change == Remove integration from MWDebug methods in favour of capturing messages from the 'error' channel. This way we fix the following that we were all blind to: * PHP built-in errors for native notices and deprecation warnings. * trigger_error calls from our own code such as in standolone libs, where we can't use wfLogWarning or wfDeprecated, and instead use E_USER_WARNING and E_USER_DEPRECATED instead. * trigger_error calls from third-party Composer libraries, which similarly can't and don't know about these at all. I removed support for including a backtrace. XDebug does a better job of this nowadays, and the information is also available in mw-error.log already in a more readable manner. Cramming it into a 100px scrollable area made the "Console" tab much less readable in my opinion. To make it work, we'd need to duplicate the pretty string formatting code of wfBacktrace() and make it re-usable when given only a trace array (rather than capture a new backtrace). I considered re-using MWExceptionHandler::prettyPrintTrace but that is far more verbose than the string format in wfBacktrace. Creating yet another untested error pretty print function seemed not worthwhile as imho in the majority of cases the $caller suffices, and when not, the toolbar suffices as a nudge to take a look in mw-debug.log, or check XDebug display_errors, or engage an IDE. == Test plan == 1. Add the following to MediaWiki.php#main() as example: ``` wfLogWarning('Hello'); wfDeprecated('Something'); trigger_error('Goodbye'); $x = []; $a = $x['a']; ``` 2. Before this change, your native display_errors=1 or XDebug shows all four. With $wgDebugToolbar enabled, the "Console" tab showed only the wfLogWarning and wfDeprecated entry. 3. After this change, all four are included Change-Id: I62d864823ec8ab9b940aae0e0f47b47a728ba861
2023-10-03 04:30:44 +00:00
$this->expectPHPError(
E_USER_NOTICE,
static function () {
MWDebug::warning( 'Ohnosecond!' );
},
'Ohnosecond!'
);
MWDebug: Add support for native PHP warnings to "Console" == Change == Remove integration from MWDebug methods in favour of capturing messages from the 'error' channel. This way we fix the following that we were all blind to: * PHP built-in errors for native notices and deprecation warnings. * trigger_error calls from our own code such as in standolone libs, where we can't use wfLogWarning or wfDeprecated, and instead use E_USER_WARNING and E_USER_DEPRECATED instead. * trigger_error calls from third-party Composer libraries, which similarly can't and don't know about these at all. I removed support for including a backtrace. XDebug does a better job of this nowadays, and the information is also available in mw-error.log already in a more readable manner. Cramming it into a 100px scrollable area made the "Console" tab much less readable in my opinion. To make it work, we'd need to duplicate the pretty string formatting code of wfBacktrace() and make it re-usable when given only a trace array (rather than capture a new backtrace). I considered re-using MWExceptionHandler::prettyPrintTrace but that is far more verbose than the string format in wfBacktrace. Creating yet another untested error pretty print function seemed not worthwhile as imho in the majority of cases the $caller suffices, and when not, the toolbar suffices as a nudge to take a look in mw-debug.log, or check XDebug display_errors, or engage an IDE. == Test plan == 1. Add the following to MediaWiki.php#main() as example: ``` wfLogWarning('Hello'); wfDeprecated('Something'); trigger_error('Goodbye'); $x = []; $a = $x['a']; ``` 2. Before this change, your native display_errors=1 or XDebug shows all four. With $wgDebugToolbar enabled, the "Console" tab showed only the wfLogWarning and wfDeprecated entry. 3. After this change, all four are included Change-Id: I62d864823ec8ab9b940aae0e0f47b47a728ba861
2023-10-03 04:30:44 +00:00
}
/**
* Message from the error channel are copied to the debug toolbar "Console" log.
*
* This normally happens via wfDeprecated -> MWDebug::deprecated -> trigger_error
* -> MWExceptionHandler -> LoggerFactory -> LegacyLogger -> MWDebug::debugMsg.
*
* The above test asserts up until trigger_error.
* This test asserts from LegacyLogger down.
*/
public function testMessagesFromErrorChannel() {
// Turn off to keep mw-error.log file empty in CI (and thus avoid build failure)
$this->overrideConfigValue( MainConfigNames::DebugLogGroups, [] );
MWDebug: Add support for native PHP warnings to "Console" == Change == Remove integration from MWDebug methods in favour of capturing messages from the 'error' channel. This way we fix the following that we were all blind to: * PHP built-in errors for native notices and deprecation warnings. * trigger_error calls from our own code such as in standolone libs, where we can't use wfLogWarning or wfDeprecated, and instead use E_USER_WARNING and E_USER_DEPRECATED instead. * trigger_error calls from third-party Composer libraries, which similarly can't and don't know about these at all. I removed support for including a backtrace. XDebug does a better job of this nowadays, and the information is also available in mw-error.log already in a more readable manner. Cramming it into a 100px scrollable area made the "Console" tab much less readable in my opinion. To make it work, we'd need to duplicate the pretty string formatting code of wfBacktrace() and make it re-usable when given only a trace array (rather than capture a new backtrace). I considered re-using MWExceptionHandler::prettyPrintTrace but that is far more verbose than the string format in wfBacktrace. Creating yet another untested error pretty print function seemed not worthwhile as imho in the majority of cases the $caller suffices, and when not, the toolbar suffices as a nudge to take a look in mw-debug.log, or check XDebug display_errors, or engage an IDE. == Test plan == 1. Add the following to MediaWiki.php#main() as example: ``` wfLogWarning('Hello'); wfDeprecated('Something'); trigger_error('Goodbye'); $x = []; $a = $x['a']; ``` 2. Before this change, your native display_errors=1 or XDebug shows all four. With $wgDebugToolbar enabled, the "Console" tab showed only the wfLogWarning and wfDeprecated entry. 3. After this change, all four are included Change-Id: I62d864823ec8ab9b940aae0e0f47b47a728ba861
2023-10-03 04:30:44 +00:00
MWExceptionHandler::handleError( E_USER_DEPRECATED, 'Warning message' );
$this->assertEquals(
[ [
MWDebug: Add support for native PHP warnings to "Console" == Change == Remove integration from MWDebug methods in favour of capturing messages from the 'error' channel. This way we fix the following that we were all blind to: * PHP built-in errors for native notices and deprecation warnings. * trigger_error calls from our own code such as in standolone libs, where we can't use wfLogWarning or wfDeprecated, and instead use E_USER_WARNING and E_USER_DEPRECATED instead. * trigger_error calls from third-party Composer libraries, which similarly can't and don't know about these at all. I removed support for including a backtrace. XDebug does a better job of this nowadays, and the information is also available in mw-error.log already in a more readable manner. Cramming it into a 100px scrollable area made the "Console" tab much less readable in my opinion. To make it work, we'd need to duplicate the pretty string formatting code of wfBacktrace() and make it re-usable when given only a trace array (rather than capture a new backtrace). I considered re-using MWExceptionHandler::prettyPrintTrace but that is far more verbose than the string format in wfBacktrace. Creating yet another untested error pretty print function seemed not worthwhile as imho in the majority of cases the $caller suffices, and when not, the toolbar suffices as a nudge to take a look in mw-debug.log, or check XDebug display_errors, or engage an IDE. == Test plan == 1. Add the following to MediaWiki.php#main() as example: ``` wfLogWarning('Hello'); wfDeprecated('Something'); trigger_error('Goodbye'); $x = []; $a = $x['a']; ``` 2. Before this change, your native display_errors=1 or XDebug shows all four. With $wgDebugToolbar enabled, the "Console" tab showed only the wfLogWarning and wfDeprecated entry. 3. After this change, all four are included Change-Id: I62d864823ec8ab9b940aae0e0f47b47a728ba861
2023-10-03 04:30:44 +00:00
'msg' => 'PHP Deprecated: Warning message',
'type' => 'warn',
MWDebug: Add support for native PHP warnings to "Console" == Change == Remove integration from MWDebug methods in favour of capturing messages from the 'error' channel. This way we fix the following that we were all blind to: * PHP built-in errors for native notices and deprecation warnings. * trigger_error calls from our own code such as in standolone libs, where we can't use wfLogWarning or wfDeprecated, and instead use E_USER_WARNING and E_USER_DEPRECATED instead. * trigger_error calls from third-party Composer libraries, which similarly can't and don't know about these at all. I removed support for including a backtrace. XDebug does a better job of this nowadays, and the information is also available in mw-error.log already in a more readable manner. Cramming it into a 100px scrollable area made the "Console" tab much less readable in my opinion. To make it work, we'd need to duplicate the pretty string formatting code of wfBacktrace() and make it re-usable when given only a trace array (rather than capture a new backtrace). I considered re-using MWExceptionHandler::prettyPrintTrace but that is far more verbose than the string format in wfBacktrace. Creating yet another untested error pretty print function seemed not worthwhile as imho in the majority of cases the $caller suffices, and when not, the toolbar suffices as a nudge to take a look in mw-debug.log, or check XDebug display_errors, or engage an IDE. == Test plan == 1. Add the following to MediaWiki.php#main() as example: ``` wfLogWarning('Hello'); wfDeprecated('Something'); trigger_error('Goodbye'); $x = []; $a = $x['a']; ``` 2. Before this change, your native display_errors=1 or XDebug shows all four. With $wgDebugToolbar enabled, the "Console" tab showed only the wfLogWarning and wfDeprecated entry. 3. After this change, all four are included Change-Id: I62d864823ec8ab9b940aae0e0f47b47a728ba861
2023-10-03 04:30:44 +00:00
'caller' => 'MWDebugTest::testMessagesFromErrorChannel',
] ],
MWDebug::getLog()
);
}
public function testDetectDeprecatedOverride() {
$baseclassInstance = new TitleValue( NS_MAIN, 'Test' );
$this->assertFalse(
MWDebug::detectDeprecatedOverride(
$baseclassInstance,
TitleValue::class,
'getNamespace',
MW_VERSION
)
);
// create a dummy subclass that overrides a method
$subclassInstance = new class ( NS_MAIN, 'Test' ) extends TitleValue {
public function getNamespace(): int {
// never called
return -100;
}
};
$this->expectPHPError(
E_USER_DEPRECATED,
static function () use ( $subclassInstance ) {
MWDebug::detectDeprecatedOverride(
$subclassInstance,
TitleValue::class,
'getNamespace',
MW_VERSION
);
},
'@anonymous'
);
MWDebug: Add support for native PHP warnings to "Console" == Change == Remove integration from MWDebug methods in favour of capturing messages from the 'error' channel. This way we fix the following that we were all blind to: * PHP built-in errors for native notices and deprecation warnings. * trigger_error calls from our own code such as in standolone libs, where we can't use wfLogWarning or wfDeprecated, and instead use E_USER_WARNING and E_USER_DEPRECATED instead. * trigger_error calls from third-party Composer libraries, which similarly can't and don't know about these at all. I removed support for including a backtrace. XDebug does a better job of this nowadays, and the information is also available in mw-error.log already in a more readable manner. Cramming it into a 100px scrollable area made the "Console" tab much less readable in my opinion. To make it work, we'd need to duplicate the pretty string formatting code of wfBacktrace() and make it re-usable when given only a trace array (rather than capture a new backtrace). I considered re-using MWExceptionHandler::prettyPrintTrace but that is far more verbose than the string format in wfBacktrace. Creating yet another untested error pretty print function seemed not worthwhile as imho in the majority of cases the $caller suffices, and when not, the toolbar suffices as a nudge to take a look in mw-debug.log, or check XDebug display_errors, or engage an IDE. == Test plan == 1. Add the following to MediaWiki.php#main() as example: ``` wfLogWarning('Hello'); wfDeprecated('Something'); trigger_error('Goodbye'); $x = []; $a = $x['a']; ``` 2. Before this change, your native display_errors=1 or XDebug shows all four. With $wgDebugToolbar enabled, the "Console" tab showed only the wfLogWarning and wfDeprecated entry. 3. After this change, all four are included Change-Id: I62d864823ec8ab9b940aae0e0f47b47a728ba861
2023-10-03 04:30:44 +00:00
}
public function testDeprecated() {
$this->expectPHPError(
E_USER_DEPRECATED,
static function () {
MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
},
'wfOldFunction'
);
}
MWDebug: Add support for native PHP warnings to "Console" == Change == Remove integration from MWDebug methods in favour of capturing messages from the 'error' channel. This way we fix the following that we were all blind to: * PHP built-in errors for native notices and deprecation warnings. * trigger_error calls from our own code such as in standolone libs, where we can't use wfLogWarning or wfDeprecated, and instead use E_USER_WARNING and E_USER_DEPRECATED instead. * trigger_error calls from third-party Composer libraries, which similarly can't and don't know about these at all. I removed support for including a backtrace. XDebug does a better job of this nowadays, and the information is also available in mw-error.log already in a more readable manner. Cramming it into a 100px scrollable area made the "Console" tab much less readable in my opinion. To make it work, we'd need to duplicate the pretty string formatting code of wfBacktrace() and make it re-usable when given only a trace array (rather than capture a new backtrace). I considered re-using MWExceptionHandler::prettyPrintTrace but that is far more verbose than the string format in wfBacktrace. Creating yet another untested error pretty print function seemed not worthwhile as imho in the majority of cases the $caller suffices, and when not, the toolbar suffices as a nudge to take a look in mw-debug.log, or check XDebug display_errors, or engage an IDE. == Test plan == 1. Add the following to MediaWiki.php#main() as example: ``` wfLogWarning('Hello'); wfDeprecated('Something'); trigger_error('Goodbye'); $x = []; $a = $x['a']; ``` 2. Before this change, your native display_errors=1 or XDebug shows all four. With $wgDebugToolbar enabled, the "Console" tab showed only the wfLogWarning and wfDeprecated entry. 3. After this change, all four are included Change-Id: I62d864823ec8ab9b940aae0e0f47b47a728ba861
2023-10-03 04:30:44 +00:00
/**
* @doesNotPerformAssertions
*/
public function testDeprecatedIgnoreDuplicate() {
@MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
MWDebug: Add support for native PHP warnings to "Console" == Change == Remove integration from MWDebug methods in favour of capturing messages from the 'error' channel. This way we fix the following that we were all blind to: * PHP built-in errors for native notices and deprecation warnings. * trigger_error calls from our own code such as in standolone libs, where we can't use wfLogWarning or wfDeprecated, and instead use E_USER_WARNING and E_USER_DEPRECATED instead. * trigger_error calls from third-party Composer libraries, which similarly can't and don't know about these at all. I removed support for including a backtrace. XDebug does a better job of this nowadays, and the information is also available in mw-error.log already in a more readable manner. Cramming it into a 100px scrollable area made the "Console" tab much less readable in my opinion. To make it work, we'd need to duplicate the pretty string formatting code of wfBacktrace() and make it re-usable when given only a trace array (rather than capture a new backtrace). I considered re-using MWExceptionHandler::prettyPrintTrace but that is far more verbose than the string format in wfBacktrace. Creating yet another untested error pretty print function seemed not worthwhile as imho in the majority of cases the $caller suffices, and when not, the toolbar suffices as a nudge to take a look in mw-debug.log, or check XDebug display_errors, or engage an IDE. == Test plan == 1. Add the following to MediaWiki.php#main() as example: ``` wfLogWarning('Hello'); wfDeprecated('Something'); trigger_error('Goodbye'); $x = []; $a = $x['a']; ``` 2. Before this change, your native display_errors=1 or XDebug shows all four. With $wgDebugToolbar enabled, the "Console" tab showed only the wfLogWarning and wfDeprecated entry. 3. After this change, all four are included Change-Id: I62d864823ec8ab9b940aae0e0f47b47a728ba861
2023-10-03 04:30:44 +00:00
MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
MWDebug: Add support for native PHP warnings to "Console" == Change == Remove integration from MWDebug methods in favour of capturing messages from the 'error' channel. This way we fix the following that we were all blind to: * PHP built-in errors for native notices and deprecation warnings. * trigger_error calls from our own code such as in standolone libs, where we can't use wfLogWarning or wfDeprecated, and instead use E_USER_WARNING and E_USER_DEPRECATED instead. * trigger_error calls from third-party Composer libraries, which similarly can't and don't know about these at all. I removed support for including a backtrace. XDebug does a better job of this nowadays, and the information is also available in mw-error.log already in a more readable manner. Cramming it into a 100px scrollable area made the "Console" tab much less readable in my opinion. To make it work, we'd need to duplicate the pretty string formatting code of wfBacktrace() and make it re-usable when given only a trace array (rather than capture a new backtrace). I considered re-using MWExceptionHandler::prettyPrintTrace but that is far more verbose than the string format in wfBacktrace. Creating yet another untested error pretty print function seemed not worthwhile as imho in the majority of cases the $caller suffices, and when not, the toolbar suffices as a nudge to take a look in mw-debug.log, or check XDebug display_errors, or engage an IDE. == Test plan == 1. Add the following to MediaWiki.php#main() as example: ``` wfLogWarning('Hello'); wfDeprecated('Something'); trigger_error('Goodbye'); $x = []; $a = $x['a']; ``` 2. Before this change, your native display_errors=1 or XDebug shows all four. With $wgDebugToolbar enabled, the "Console" tab showed only the wfLogWarning and wfDeprecated entry. 3. After this change, all four are included Change-Id: I62d864823ec8ab9b940aae0e0f47b47a728ba861
2023-10-03 04:30:44 +00:00
// If we reach here, than the second one did not throw any deprecation warning.
// The first one was silenced to seed the ignore logic.
}
MWDebug: Add support for native PHP warnings to "Console" == Change == Remove integration from MWDebug methods in favour of capturing messages from the 'error' channel. This way we fix the following that we were all blind to: * PHP built-in errors for native notices and deprecation warnings. * trigger_error calls from our own code such as in standolone libs, where we can't use wfLogWarning or wfDeprecated, and instead use E_USER_WARNING and E_USER_DEPRECATED instead. * trigger_error calls from third-party Composer libraries, which similarly can't and don't know about these at all. I removed support for including a backtrace. XDebug does a better job of this nowadays, and the information is also available in mw-error.log already in a more readable manner. Cramming it into a 100px scrollable area made the "Console" tab much less readable in my opinion. To make it work, we'd need to duplicate the pretty string formatting code of wfBacktrace() and make it re-usable when given only a trace array (rather than capture a new backtrace). I considered re-using MWExceptionHandler::prettyPrintTrace but that is far more verbose than the string format in wfBacktrace. Creating yet another untested error pretty print function seemed not worthwhile as imho in the majority of cases the $caller suffices, and when not, the toolbar suffices as a nudge to take a look in mw-debug.log, or check XDebug display_errors, or engage an IDE. == Test plan == 1. Add the following to MediaWiki.php#main() as example: ``` wfLogWarning('Hello'); wfDeprecated('Something'); trigger_error('Goodbye'); $x = []; $a = $x['a']; ``` 2. Before this change, your native display_errors=1 or XDebug shows all four. With $wgDebugToolbar enabled, the "Console" tab showed only the wfLogWarning and wfDeprecated entry. 3. After this change, all four are included Change-Id: I62d864823ec8ab9b940aae0e0f47b47a728ba861
2023-10-03 04:30:44 +00:00
/**
* @doesNotPerformAssertions
*/
public function testDeprecatedIgnoreNonConsecutivesDuplicate() {
@MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
@MWDebug::warning( 'some warning' );
@MWDebug::log( 'we could have logged something too' );
MWDebug: Add support for native PHP warnings to "Console" == Change == Remove integration from MWDebug methods in favour of capturing messages from the 'error' channel. This way we fix the following that we were all blind to: * PHP built-in errors for native notices and deprecation warnings. * trigger_error calls from our own code such as in standolone libs, where we can't use wfLogWarning or wfDeprecated, and instead use E_USER_WARNING and E_USER_DEPRECATED instead. * trigger_error calls from third-party Composer libraries, which similarly can't and don't know about these at all. I removed support for including a backtrace. XDebug does a better job of this nowadays, and the information is also available in mw-error.log already in a more readable manner. Cramming it into a 100px scrollable area made the "Console" tab much less readable in my opinion. To make it work, we'd need to duplicate the pretty string formatting code of wfBacktrace() and make it re-usable when given only a trace array (rather than capture a new backtrace). I considered re-using MWExceptionHandler::prettyPrintTrace but that is far more verbose than the string format in wfBacktrace. Creating yet another untested error pretty print function seemed not worthwhile as imho in the majority of cases the $caller suffices, and when not, the toolbar suffices as a nudge to take a look in mw-debug.log, or check XDebug display_errors, or engage an IDE. == Test plan == 1. Add the following to MediaWiki.php#main() as example: ``` wfLogWarning('Hello'); wfDeprecated('Something'); trigger_error('Goodbye'); $x = []; $a = $x['a']; ``` 2. Before this change, your native display_errors=1 or XDebug shows all four. With $wgDebugToolbar enabled, the "Console" tab showed only the wfLogWarning and wfDeprecated entry. 3. After this change, all four are included Change-Id: I62d864823ec8ab9b940aae0e0f47b47a728ba861
2023-10-03 04:30:44 +00:00
// Another deprecation (not silenced)
MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
}
public function testDebugMsg() {
$this->overrideConfigValue( MainConfigNames::ShowDebug, true );
// Generate a log to be sure there is at least one
$logger = LoggerFactory::getInstance( 'test-debug-channel' );
$logger->debug( 'My message', [] );
$debugLog = (string)MWDebug::getHTMLDebugLog();
$this->assertNotSame( '', $debugLog, 'MWDebug::getHTMLDebugLog() should not be an empty string' );
$this->assertStringNotContainsString( "<ul id=\"mw-debug-html\">\n</ul>", $debugLog,
'MWDebug::getHTMLDebugLog() should contain a non-empty debug log'
);
}
public function testAppendDebugInfoToApiResultXmlFormat() {
$request = $this->newApiRequest(
[ 'action' => 'help', 'format' => 'xml' ],
'/api.php?action=help&format=xml'
);
$context = new RequestContext();
$context->setRequest( $request );
$result = new ApiResult( false );
MWDebug::appendDebugInfoToApiResult( $context, $result );
$this->assertInstanceOf( ApiResult::class, $result );
API: Overhaul ApiResult, make format=xml not throw, and add json formatversion ApiResult was a mess: some methods could only be used with an array reference instead of manipulating the stored data, methods that had both array-ref and internal-data versions had names that didn't at all correspond, some methods that worked on an array reference were annoyingly non-static, and then the whole mess with setIndexedTagName. ApiFormatXml is also entirely annoying to deal with, as it liked to throw exceptions if certain metadata wasn't provided that no other formatter required. Its legacy also means we have this silly convention of using empty-string rather than boolean true, annoying restrictions on keys (leading to things that should be hashes being arrays of key-value object instead), '*' used as a key all over the place, and so on. So, changes here: * ApiResult is no longer an ApiBase or a ContextSource. * Wherever sensible, ApiResult provides a static method working on an arrayref and a non-static method working on internal data. * Metadata is now always added to ApiResult's internal data structure. Formatters are responsible for stripping it if necessary. "raw mode" is deprecated. * New metadata to replace the '*' key, solve the array() => '[]' vs '{}' question, and so on. * New class for formatting warnings and errors using i18n messages, and support for multiple errors and a more machine-readable format for warnings. For the moment, though, the actual output will not be changing yet (see T47843 for future plans). * New formatversion parameter for format=json and format=php, to select between BC mode and the modern output. * In BC mode, booleans will be converted to empty-string presence style; modules currently returning booleans will need to use ApiResult::META_BC_BOOLS to preserve their current output. Actual changes to the API modules' output (e.g. actually returning booleans for the new formatversion) beyond the use of ApiResult::setContentValue() are left for a future change. Bug: T76728 Bug: T57371 Bug: T33629 Change-Id: I7b37295e8862b188d1f3b0cd07f66ac34629678f
2014-12-03 22:14:22 +00:00
$data = $result->getResultData();
$expectedKeys = [ 'mwVersion', 'phpEngine', 'phpVersion', 'gitRevision', 'gitBranch',
'gitViewUrl', 'time', 'log', 'debugLog', 'queries', 'request', 'memory',
'memoryPeak', 'includes', '_element' ];
foreach ( $expectedKeys as $expectedKey ) {
$this->assertArrayHasKey( $expectedKey, $data['debuginfo'], "debuginfo has $expectedKey" );
}
$xml = ApiFormatXml::recXmlPrint( 'help', $data, null );
// exception not thrown
$this->assertIsString( $xml );
}
/**
* @param string[] $params
* @param string $requestUrl
* @return FauxRequest
*/
private function newApiRequest( array $params, $requestUrl ) {
$req = new FauxRequest( $params );
$req->setRequestURL( $requestUrl );
return $req;
}
}