2013-10-28 16:56:37 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* @author Antoine Musso
|
|
|
|
|
* @copyright Copyright © 2013, Antoine Musso
|
|
|
|
|
* @copyright Copyright © 2013, Wikimedia Foundation Inc.
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
2019-06-30 13:23:53 +00:00
|
|
|
class MWExceptionHandlerTest extends \MediaWikiUnitTestCase {
|
2013-10-28 16:56:37 +00:00
|
|
|
|
2019-12-03 01:16:13 +00:00
|
|
|
private $oldSettingValue;
|
|
|
|
|
|
2020-06-14 10:51:39 +00:00
|
|
|
protected function setUp() : void {
|
2019-12-03 01:16:13 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
// We need to make sure the traces have function arguments as we're testing
|
|
|
|
|
// their handling.
|
|
|
|
|
$this->oldSettingValue = ini_set( 'zend.exception_ignore_args', 0 );
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-14 10:51:39 +00:00
|
|
|
protected function tearDown() : void {
|
2019-12-03 01:16:13 +00:00
|
|
|
ini_set( 'zend.exception_ignore_args', $this->oldSettingValue );
|
|
|
|
|
parent::tearDown();
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-28 16:56:37 +00:00
|
|
|
/**
|
|
|
|
|
* @covers MWExceptionHandler::getRedactedTrace
|
|
|
|
|
*/
|
2014-02-24 20:21:09 +00:00
|
|
|
public function testGetRedactedTrace() {
|
2013-11-01 18:33:21 +00:00
|
|
|
$refvar = 'value';
|
2013-10-28 16:56:37 +00:00
|
|
|
try {
|
2016-02-17 09:09:32 +00:00
|
|
|
$array = [ 'a', 'b' ];
|
2020-02-28 15:13:53 +00:00
|
|
|
$object = (object)[];
|
2013-11-01 18:33:21 +00:00
|
|
|
self::helperThrowAnException( $array, $object, $refvar );
|
2013-11-19 18:03:54 +00:00
|
|
|
} catch ( Exception $e ) {
|
2013-10-28 16:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
2014-02-25 21:37:40 +00:00
|
|
|
# Make sure our stack trace contains an array and an object passed to
|
2013-10-28 16:56:37 +00:00
|
|
|
# some function in the stacktrace. Else, we can not assert the trace
|
|
|
|
|
# redaction achieved its job.
|
|
|
|
|
$trace = $e->getTrace();
|
|
|
|
|
$hasObject = false;
|
|
|
|
|
$hasArray = false;
|
|
|
|
|
foreach ( $trace as $frame ) {
|
2014-07-21 12:47:42 +00:00
|
|
|
if ( !isset( $frame['args'] ) ) {
|
2013-10-28 16:56:37 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
foreach ( $frame['args'] as $arg ) {
|
|
|
|
|
$hasObject = $hasObject || is_object( $arg );
|
|
|
|
|
$hasArray = $hasArray || is_array( $arg );
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 18:03:54 +00:00
|
|
|
if ( $hasObject && $hasArray ) {
|
2013-10-28 16:56:37 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->assertTrue( $hasObject,
|
2019-12-03 01:16:13 +00:00
|
|
|
"The stacktrace must contain a function having an object as a parameter" );
|
2013-10-28 16:56:37 +00:00
|
|
|
$this->assertTrue( $hasArray,
|
2019-12-03 01:16:13 +00:00
|
|
|
"The stacktrace must contain a function having an array as a parameter" );
|
2013-10-28 16:56:37 +00:00
|
|
|
|
|
|
|
|
# Now we redact the trace.. and make sure no function arguments are
|
|
|
|
|
# arrays or objects.
|
|
|
|
|
$redacted = MWExceptionHandler::getRedactedTrace( $e );
|
|
|
|
|
|
|
|
|
|
foreach ( $redacted as $frame ) {
|
2014-07-21 12:47:42 +00:00
|
|
|
if ( !isset( $frame['args'] ) ) {
|
2013-10-28 16:56:37 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
foreach ( $frame['args'] as $arg ) {
|
2019-12-13 14:29:10 +00:00
|
|
|
$this->assertIsNotArray( $arg );
|
|
|
|
|
$this->assertIsNotObject( $arg );
|
2013-10-28 16:56:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-11-01 18:33:21 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals( 'value', $refvar, 'Ensuring reference variable wasn\'t changed' );
|
2013-10-28 16:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper function for testExpandArgumentsInCall
|
|
|
|
|
*
|
2013-11-01 18:33:21 +00:00
|
|
|
* Pass it an object and an array, and something by reference :-)
|
2013-10-28 16:56:37 +00:00
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2013-11-01 18:33:21 +00:00
|
|
|
protected static function helperThrowAnException( $a, $b, &$c ) {
|
2013-10-28 16:56:37 +00:00
|
|
|
throw new Exception();
|
|
|
|
|
}
|
|
|
|
|
}
|