summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/MWExceptionHandlerTest.php
blob: 987dfa8347b3beef550f9718a2acb4dbe31e75dc (plain)
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
<?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() {
		try {
			$array = array( 'a', 'b' );
			$object = new StdClass();
			self::helperThrowAnException( $array, $object );
		} 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);
			}
		}
	}

	/**
	 * Helper function for testExpandArgumentsInCall
	 *
	 * Pass it an object and an array :-)
	 *
	 * @throws Exception
	 */
	protected static function helperThrowAnException( $a, $b ) {
		throw new Exception();
	}
}