1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
<?php
use MediaWiki\Api\ApiFormatXml;
use MediaWiki\Api\ApiResult;
use MediaWiki\Context\RequestContext;
use MediaWiki\Debug\MWDebug;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MainConfigNames;
use MediaWiki\Request\FauxRequest;
use MediaWiki\Title\TitleValue;
use Psr\Log\LoggerInterface;
/**
* @covers \MediaWiki\Debug\MWDebug
*/
class MWDebugTest extends MediaWikiIntegrationTestCase {
protected function setUp(): void {
$this->overrideConfigValue( MainConfigNames::DevelopmentWarnings, false );
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();
}
public function testLog() {
@MWDebug::log( 'logging a string' );
$this->assertEquals(
[ [
'msg' => 'logging a string',
'type' => 'log',
'caller' => 'MWDebugTest->testLog',
] ],
MWDebug::getLog()
);
}
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 );
$this->expectPHPError(
E_USER_NOTICE,
static function () {
MWDebug::warning( 'Ohnosecond!' );
},
'Ohnosecond!'
);
}
/**
* 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, [] );
MWExceptionHandler::handleError( E_USER_DEPRECATED, 'Warning message' );
$this->assertEquals(
[ [
'msg' => 'PHP Deprecated: Warning message',
'type' => 'warn',
'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'
);
}
public function testDeprecated() {
$this->expectPHPError(
E_USER_DEPRECATED,
static function () {
MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
},
'wfOldFunction'
);
}
/**
* @doesNotPerformAssertions
*/
public function testDeprecatedIgnoreDuplicate() {
@MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
// If we reach here, than the second one did not throw any deprecation warning.
// The first one was silenced to seed the ignore logic.
}
/**
* @doesNotPerformAssertions
*/
public function testDeprecatedIgnoreNonConsecutivesDuplicate() {
@MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
@MWDebug::warning( 'some warning' );
@MWDebug::log( 'we could have logged something too' );
// 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 );
$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;
}
}
|