summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/exception/MWExceptionTest.php
blob: ef0f2a9e2c49716a53fdb952015ad18a6490b188 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
 * @author Antoine Musso
 * @copyright Copyright © 2013, Antoine Musso
 * @copyright Copyright © 2013, Wikimedia Foundation Inc.
 * @file
 */

class MWExceptionTest extends MediaWikiTestCase {

	/**
	 * @expectedException MWException
	 */
	public function testMwexceptionThrowing() {
		throw new MWException();
	}

	/**
	 * @dataProvider provideTextUseOutputPage
	 * @covers MWException::useOutputPage
	 */
	public function testUseOutputPage( $expected, $wgLang, $wgFullyInitialised, $wgOut ) {
		$this->setMwGlobals( array(
			'wgLang' => $wgLang,
			'wgFullyInitialised' => $wgFullyInitialised,
			'wgOut' => $wgOut,
		) );

		$e = new MWException();
		$this->assertEquals( $expected, $e->useOutputPage() );
	}

	public function provideTextUseOutputPage() {
		return array(
			// expected, wgLang, wgFullyInitialised, wgOut
			array( false, null, null, null ),
			array( false, $this->getMockLanguage(), null, null ),
			array( false, $this->getMockLanguage(), true, null ),
			array( false, null, true, null ),
			array( false, null, null, true ),
			array( true, $this->getMockLanguage(), true, true ),
		);
	}

	private function getMockLanguage() {
		return $this->getMockBuilder( 'Language' )
			->disableOriginalConstructor()
			->getMock();
	}

	/**
	 * @dataProvider provideUseMessageCache
	 * @covers MWException::useMessageCache
	 */
	public function testUseMessageCache( $expected, $wgLang ) {
		$this->setMwGlobals( array(
			'wgLang' => $wgLang,
		) );
		$e = new MWException();
		$this->assertEquals( $expected, $e->useMessageCache() );
	}

	public function provideUseMessageCache() {
		return array(
			array( false, null ),
			array( true, $this->getMockLanguage() ),
		);
	}

	/**
	 * @covers MWException::isLoggable
	 */
	public function testIsLogable() {
		$e = new MWException();
		$this->assertTrue( $e->isLoggable() );
	}

	/**
	 * @dataProvider provideRunHooks
	 * @covers MWException::runHooks
	 */
	public function testRunHooks( $wgExceptionHooks, $name, $args, $expectedReturn ) {
		$this->setMwGlobals( array(
			'wgExceptionHooks' => $wgExceptionHooks,
		) );
		$e = new MWException();
		$this->assertEquals( $expectedReturn, $e->runHooks( $name, $args ) );
	}

	public static function provideRunHooks() {
		return array(
			array( null, null, null, null ),
			array( array(), 'name', array(), null ),
			array( array( 'name' => false ), 'name', array(), null ),
			array(
				array( 'mockHook' => array( 'MWExceptionTest::mockHook' ) ),
				'mockHook', array(), 'YAY.[]'
			),
			array(
				array( 'mockHook' => array( 'MWExceptionTest::mockHook' ) ),
				'mockHook', array( 'a' ), 'YAY.{"1":"a"}'
			),
			array(
				array( 'mockHook' => array( 'MWExceptionTest::mockHook' ) ),
				'mockHook', array( null ), null
			),
		);
	}

	/**
	 * Used in conjunction with provideRunHooks and testRunHooks as a mock callback for a hook
	 */
	public static function mockHook() {
		$args = func_get_args();
		if ( !$args[0] instanceof MWException ) {
			return '$caller not instance of MWException';
		}
		unset( $args[0] );
		if ( array_key_exists( 1, $args ) && $args[1] === null ) {
			return null;
		}
		return 'YAY.' . json_encode( $args );
	}

	/**
	 * @dataProvider provideIsCommandLine
	 * @covers MWException::isCommandLine
	 */
	public function testisCommandLine( $expected, $wgCommandLineMode ) {
		$this->setMwGlobals( array(
			'wgCommandLineMode' => $wgCommandLineMode,
		) );
		$e = new MWException();
		$this->assertEquals( $expected, $e->isCommandLine() );
	}

	public static function provideIsCommandLine() {
		return array(
			array( false, null ),
			array( true, true ),
		);
	}

	/**
	 * Verify the exception classes are JSON serializabe.
	 *
	 * @covers MWExceptionHandler::jsonSerializeException
	 * @dataProvider provideExceptionClasses
	 */
	public function testJsonSerializeExceptions( $exception_class ) {
		$json = MWExceptionHandler::jsonSerializeException(
			new $exception_class()
		);
		$this->assertNotEquals( false, $json,
			"The $exception_class exception should be JSON serializable, got false." );
	}

	public static function provideExceptionClasses() {
		return array(
			array( 'Exception' ),
			array( 'MWException' ),
		);
	}

	/**
	 * Lame JSON schema validation.
	 *
	 * @covers MWExceptionHandler::jsonSerializeException
	 *
	 * @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
	 * @dataProvider provideJsonSerializedKeys
	 */
	public function testJsonserializeexceptionKeys( $expectedKeyType, $exClass, $key ) {

		# Make sure we log a backtrace:
		$this->setMwGlobals( array( 'wgLogExceptionBacktrace' => true ) );

		$json = json_decode(
			MWExceptionHandler::jsonSerializeException( new $exClass())
		);
		$this->assertObjectHasAttribute( $key, $json,
			"JSON serialized exception is missing key '$key'"
		);
		$this->assertInternalType( $expectedKeyType, $json->$key,
			"JSON serialized key '$key' has type " . gettype( $json->$key )
			. " (expected: $expectedKeyType)."
		);
	}

	/**
	 * Returns test cases: exception class, key name, gettype()
	 */
	public static function provideJsonSerializedKeys() {
		$testCases = array();
		foreach ( array( 'Exception', 'MWException' ) as $exClass ) {
			$exTests = array(
				array( 'string', $exClass, 'id' ),
				array( 'string', $exClass, 'file' ),
				array( 'integer', $exClass, 'line' ),
				array( 'string', $exClass, 'message' ),
				array( 'null', $exClass, 'url' ),
				# Backtrace only enabled with wgLogExceptionBacktrace = true
				array( 'array', $exClass, 'backtrace' ),
			);
			$testCases = array_merge( $testCases, $exTests );
		}
		return $testCases;
	}

	/**
	 * Given wgLogExceptionBacktrace is true
	 * then serialized exception SHOULD have a backtrace
	 *
	 * @covers MWExceptionHandler::jsonSerializeException
	 */
	public function testJsonserializeexceptionBacktracingEnabled() {
		$this->setMwGlobals( array( 'wgLogExceptionBacktrace' => true ) );
		$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
	 */
	public function testJsonserializeexceptionBacktracingDisabled() {
		$this->setMwGlobals( array( 'wgLogExceptionBacktrace' => false ) );
		$json = json_decode(
			MWExceptionHandler::jsonSerializeException( new Exception() )
		);
		$this->assertObjectNotHasAttribute( 'backtrace', $json );

	}

}