2013-10-28 16:24:50 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* @author Antoine Musso
|
|
|
|
|
* @copyright Copyright © 2013, Antoine Musso
|
|
|
|
|
* @copyright Copyright © 2013, Wikimedia Foundation Inc.
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
2020-06-30 15:09:24 +00:00
|
|
|
class MWExceptionTest extends MediaWikiIntegrationTestCase {
|
2013-10-28 16:24:50 +00:00
|
|
|
|
|
|
|
|
/**
|
2017-12-28 08:22:35 +00:00
|
|
|
* @covers MWException
|
2013-10-28 16:24:50 +00:00
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function testMwexceptionThrowing() {
|
2019-10-11 22:22:26 +00:00
|
|
|
$this->expectException( MWException::class );
|
2013-10-28 16:24:50 +00:00
|
|
|
throw new MWException();
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-26 13:01:47 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideTextUseOutputPage
|
|
|
|
|
* @covers MWException::useOutputPage
|
|
|
|
|
*/
|
2016-03-09 16:47:58 +00:00
|
|
|
public function testUseOutputPage( $expected, $langObj, $wgFullyInitialised, $wgOut ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
2016-03-09 16:47:58 +00:00
|
|
|
'wgLang' => $langObj,
|
2014-02-26 13:01:47 +00:00
|
|
|
'wgFullyInitialised' => $wgFullyInitialised,
|
|
|
|
|
'wgOut' => $wgOut,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2014-02-26 13:01:47 +00:00
|
|
|
|
|
|
|
|
$e = new MWException();
|
|
|
|
|
$this->assertEquals( $expected, $e->useOutputPage() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function provideTextUseOutputPage() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2016-03-09 16:47:58 +00:00
|
|
|
// expected, langObj, wgFullyInitialised, wgOut
|
2016-02-17 09:09:32 +00:00
|
|
|
[ false, null, null, null ],
|
|
|
|
|
[ false, $this->getMockLanguage(), null, null ],
|
|
|
|
|
[ false, $this->getMockLanguage(), true, null ],
|
|
|
|
|
[ false, null, true, null ],
|
|
|
|
|
[ false, null, null, true ],
|
|
|
|
|
[ true, $this->getMockLanguage(), true, true ],
|
|
|
|
|
];
|
2014-02-26 13:01:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getMockLanguage() {
|
2018-01-13 00:02:09 +00:00
|
|
|
return $this->getMockBuilder( Language::class )
|
2014-02-26 13:01:47 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers MWException::useMessageCache
|
|
|
|
|
*/
|
2020-03-02 09:35:57 +00:00
|
|
|
public function testUseMessageCache() {
|
2014-02-26 13:01:47 +00:00
|
|
|
$e = new MWException();
|
2020-03-02 09:35:57 +00:00
|
|
|
$this->assertTrue( $e->useMessageCache() );
|
2014-02-26 13:01:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers MWException::isLoggable
|
|
|
|
|
*/
|
|
|
|
|
public function testIsLogable() {
|
|
|
|
|
$e = new MWException();
|
|
|
|
|
$this->assertTrue( $e->isLoggable() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideIsCommandLine
|
|
|
|
|
* @covers MWException::isCommandLine
|
|
|
|
|
*/
|
|
|
|
|
public function testisCommandLine( $expected, $wgCommandLineMode ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
2014-02-26 13:01:47 +00:00
|
|
|
'wgCommandLineMode' => $wgCommandLineMode,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2014-02-26 13:01:47 +00:00
|
|
|
$e = new MWException();
|
|
|
|
|
$this->assertEquals( $expected, $e->isCommandLine() );
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-18 01:28:26 +00:00
|
|
|
public static function provideIsCommandLine() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ false, null ],
|
|
|
|
|
[ true, true ],
|
|
|
|
|
];
|
2014-02-26 13:01:47 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-28 16:24:50 +00:00
|
|
|
/**
|
|
|
|
|
* Verify the exception classes are JSON serializabe.
|
|
|
|
|
*
|
|
|
|
|
* @covers MWExceptionHandler::jsonSerializeException
|
|
|
|
|
* @dataProvider provideExceptionClasses
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function testJsonSerializeExceptions( $exception_class ) {
|
2013-10-28 16:24:50 +00:00
|
|
|
$json = MWExceptionHandler::jsonSerializeException(
|
|
|
|
|
new $exception_class()
|
|
|
|
|
);
|
|
|
|
|
$this->assertNotEquals( false, $json,
|
|
|
|
|
"The $exception_class exception should be JSON serializable, got false." );
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-18 01:28:26 +00:00
|
|
|
public static function provideExceptionClasses() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2018-01-13 00:02:09 +00:00
|
|
|
[ Exception::class ],
|
|
|
|
|
[ MWException::class ],
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-10-28 16:24:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Lame JSON schema validation.
|
|
|
|
|
*
|
|
|
|
|
* @covers MWExceptionHandler::jsonSerializeException
|
|
|
|
|
*
|
2014-04-17 18:43:42 +00:00
|
|
|
* @param string $expectedKeyType Type expected as returned by gettype()
|
|
|
|
|
* @param string $exClass An exception class (ie: Exception, MWException)
|
|
|
|
|
* @param string $key Name of the key to validate in the serialized JSON
|
2013-10-28 16:24:50 +00:00
|
|
|
* @dataProvider provideJsonSerializedKeys
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function testJsonserializeexceptionKeys( $expectedKeyType, $exClass, $key ) {
|
2013-10-28 16:24:50 +00:00
|
|
|
# Make sure we log a backtrace:
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [ 'wgLogExceptionBacktrace' => true ] );
|
2013-10-28 16:24:50 +00:00
|
|
|
|
|
|
|
|
$json = json_decode(
|
2015-06-16 19:06:19 +00:00
|
|
|
MWExceptionHandler::jsonSerializeException( new $exClass() )
|
2013-10-28 16:24:50 +00:00
|
|
|
);
|
|
|
|
|
$this->assertObjectHasAttribute( $key, $json,
|
|
|
|
|
"JSON serialized exception is missing key '$key'"
|
|
|
|
|
);
|
2019-12-13 18:21:35 +00:00
|
|
|
$this->assertSame( $expectedKeyType, gettype( $json->$key ),
|
2013-11-19 18:03:54 +00:00
|
|
|
"JSON serialized key '$key' has type " . gettype( $json->$key )
|
2013-10-28 16:24:50 +00:00
|
|
|
. " (expected: $expectedKeyType)."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns test cases: exception class, key name, gettype()
|
|
|
|
|
*/
|
2014-09-18 01:28:26 +00:00
|
|
|
public static function provideJsonSerializedKeys() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$testCases = [];
|
2018-01-13 00:02:09 +00:00
|
|
|
foreach ( [ Exception::class, MWException::class ] as $exClass ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$exTests = [
|
|
|
|
|
[ 'string', $exClass, 'id' ],
|
|
|
|
|
[ 'string', $exClass, 'file' ],
|
|
|
|
|
[ 'integer', $exClass, 'line' ],
|
|
|
|
|
[ 'string', $exClass, 'message' ],
|
2019-12-13 18:21:35 +00:00
|
|
|
[ 'NULL', $exClass, 'url' ],
|
2013-10-28 16:24:50 +00:00
|
|
|
# Backtrace only enabled with wgLogExceptionBacktrace = true
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'array', $exClass, 'backtrace' ],
|
|
|
|
|
];
|
2013-11-19 18:03:54 +00:00
|
|
|
$testCases = array_merge( $testCases, $exTests );
|
2013-10-28 16:24:50 +00:00
|
|
|
}
|
|
|
|
|
return $testCases;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Given wgLogExceptionBacktrace is true
|
|
|
|
|
* then serialized exception SHOULD have a backtrace
|
|
|
|
|
*
|
|
|
|
|
* @covers MWExceptionHandler::jsonSerializeException
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function testJsonserializeexceptionBacktracingEnabled() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [ 'wgLogExceptionBacktrace' => true ] );
|
2013-10-28 16:24:50 +00:00
|
|
|
$json = json_decode(
|
|
|
|
|
MWExceptionHandler::jsonSerializeException( new Exception() )
|
|
|
|
|
);
|
|
|
|
|
$this->assertObjectHasAttribute( 'backtrace', $json );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Given wgLogExceptionBacktrace is false
|
|
|
|
|
* then serialized exception SHOULD NOT have a backtrace
|
|
|
|
|
*
|
|
|
|
|
* @covers MWExceptionHandler::jsonSerializeException
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function testJsonserializeexceptionBacktracingDisabled() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [ 'wgLogExceptionBacktrace' => false ] );
|
2013-10-28 16:24:50 +00:00
|
|
|
$json = json_decode(
|
|
|
|
|
MWExceptionHandler::jsonSerializeException( new Exception() )
|
|
|
|
|
);
|
|
|
|
|
$this->assertObjectNotHasAttribute( 'backtrace', $json );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|