wiki.techinc.nl/tests/phpunit/includes/MWExceptionHandlerTest.php
Brad Jorsch 405acaeba6 Add test for getRedactedTrace and reference args
This tests the issue PleaseStand noticed when reviewing I3d570a63, where
if some function in the call stack took arguments by reference then
passing the exception to MWExceptionHandler::getRedactedTrace would
clobber those arguments.

Change-Id: Iaaba3ef2fb5eb6a338ab229201105ed4308b0692
2013-11-01 23:36:13 +00:00

76 lines
2 KiB
PHP

<?php
/**
* Tests for includes/Exception.php.
*
* @author Antoine Musso
* @copyright Copyright © 2013, Antoine Musso
* @copyright Copyright © 2013, Wikimedia Foundation Inc.
* @file
*/
class MWExceptionHandlerTest extends MediaWikiTestCase {
/**
* @covers MWExceptionHandler::getRedactedTrace
*/
function testGetRedactedTrace() {
$refvar = 'value';
try {
$array = array( 'a', 'b' );
$object = new StdClass();
self::helperThrowAnException( $array, $object, $refvar );
} catch (Exception $e) {
}
# 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 );
}
if( $hasObject && $hasArray ) {
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 ) {
$this->assertNotInternalType( 'array', $arg);
$this->assertNotInternalType( 'object', $arg);
}
}
$this->assertEquals( 'value', $refvar, 'Ensuring reference variable wasn\'t changed' );
}
/**
* Helper function for testExpandArgumentsInCall
*
* Pass it an object and an array, and something by reference :-)
*
* @throws Exception
*/
protected static function helperThrowAnException( $a, $b, &$c ) {
throw new Exception();
}
}