2013-10-28 16:56:37 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* @author Antoine Musso
|
|
|
|
|
* @copyright Copyright © 2013, Antoine Musso
|
|
|
|
|
* @copyright Copyright © 2013, Wikimedia Foundation Inc.
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
class MWExceptionHandlerTest extends MediaWikiTestCase {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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 {
|
|
|
|
|
$array = array( 'a', 'b' );
|
|
|
|
|
$object = new StdClass();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Make sure our strack trace contains an array and an object passed to
|
|
|
|
|
# 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 ) {
|
|
|
|
|
if ( ! isset( $frame['args'] ) ) {
|
|
|
|
|
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,
|
|
|
|
|
"The stacktrace must have a function having an object has parameter" );
|
|
|
|
|
$this->assertTrue( $hasArray,
|
|
|
|
|
"The stacktrace must have a function having an array has parameter" );
|
|
|
|
|
|
|
|
|
|
# Now we redact the trace.. and make sure no function arguments are
|
|
|
|
|
# arrays or objects.
|
|
|
|
|
$redacted = MWExceptionHandler::getRedactedTrace( $e );
|
|
|
|
|
|
|
|
|
|
foreach ( $redacted as $frame ) {
|
|
|
|
|
if ( ! isset( $frame['args'] ) ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
foreach ( $frame['args'] as $arg ) {
|
2013-11-19 18:03:54 +00:00
|
|
|
$this->assertNotInternalType( 'array', $arg );
|
|
|
|
|
$this->assertNotInternalType( 'object', $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();
|
|
|
|
|
}
|
|
|
|
|
}
|