Merge "Add dotall modifier to EDITSECTION_REGEX"
[lhc/web/wiklou.git] / tests / phpunit / includes / exception / MWExceptionTest.php
1 <?php
2 /**
3 * @author Antoine Musso
4 * @copyright Copyright © 2013, Antoine Musso
5 * @copyright Copyright © 2013, Wikimedia Foundation Inc.
6 * @file
7 */
8
9 class MWExceptionTest extends MediaWikiTestCase {
10
11 /**
12 * @expectedException MWException
13 */
14 public function testMwexceptionThrowing() {
15 throw new MWException();
16 }
17
18 /**
19 * @dataProvider provideTextUseOutputPage
20 * @covers MWException::useOutputPage
21 */
22 public function testUseOutputPage( $expected, $langObj, $wgFullyInitialised, $wgOut ) {
23 $this->setMwGlobals( [
24 'wgLang' => $langObj,
25 'wgFullyInitialised' => $wgFullyInitialised,
26 'wgOut' => $wgOut,
27 ] );
28
29 $e = new MWException();
30 $this->assertEquals( $expected, $e->useOutputPage() );
31 }
32
33 public function provideTextUseOutputPage() {
34 return [
35 // expected, langObj, wgFullyInitialised, wgOut
36 [ false, null, null, null ],
37 [ false, $this->getMockLanguage(), null, null ],
38 [ false, $this->getMockLanguage(), true, null ],
39 [ false, null, true, null ],
40 [ false, null, null, true ],
41 [ true, $this->getMockLanguage(), true, true ],
42 ];
43 }
44
45 private function getMockLanguage() {
46 return $this->getMockBuilder( 'Language' )
47 ->disableOriginalConstructor()
48 ->getMock();
49 }
50
51 /**
52 * @dataProvider provideUseMessageCache
53 * @covers MWException::useMessageCache
54 */
55 public function testUseMessageCache( $expected, $langObj ) {
56 $this->setMwGlobals( [
57 'wgLang' => $langObj,
58 ] );
59 $e = new MWException();
60 $this->assertEquals( $expected, $e->useMessageCache() );
61 }
62
63 public function provideUseMessageCache() {
64 return [
65 [ false, null ],
66 [ true, $this->getMockLanguage() ],
67 ];
68 }
69
70 /**
71 * @covers MWException::isLoggable
72 */
73 public function testIsLogable() {
74 $e = new MWException();
75 $this->assertTrue( $e->isLoggable() );
76 }
77
78 /**
79 * @dataProvider provideRunHooks
80 * @covers MWException::runHooks
81 */
82 public function testRunHooks( $wgExceptionHooks, $name, $args, $expectedReturn ) {
83 $this->setMwGlobals( [
84 'wgExceptionHooks' => $wgExceptionHooks,
85 ] );
86 $e = new MWException();
87 $this->assertEquals( $expectedReturn, $e->runHooks( $name, $args ) );
88 }
89
90 public static function provideRunHooks() {
91 return [
92 [ null, null, null, null ],
93 [ [], 'name', [], null ],
94 [ [ 'name' => false ], 'name', [], null ],
95 [
96 [ 'mockHook' => [ 'MWExceptionTest::mockHook' ] ],
97 'mockHook', [], 'YAY.[]'
98 ],
99 [
100 [ 'mockHook' => [ 'MWExceptionTest::mockHook' ] ],
101 'mockHook', [ 'a' ], 'YAY.{"1":"a"}'
102 ],
103 [
104 [ 'mockHook' => [ 'MWExceptionTest::mockHook' ] ],
105 'mockHook', [ null ], null
106 ],
107 ];
108 }
109
110 /**
111 * Used in conjunction with provideRunHooks and testRunHooks as a mock callback for a hook
112 */
113 public static function mockHook() {
114 $args = func_get_args();
115 if ( !$args[0] instanceof MWException ) {
116 return '$caller not instance of MWException';
117 }
118 unset( $args[0] );
119 if ( array_key_exists( 1, $args ) && $args[1] === null ) {
120 return null;
121 }
122 return 'YAY.' . json_encode( $args );
123 }
124
125 /**
126 * @dataProvider provideIsCommandLine
127 * @covers MWException::isCommandLine
128 */
129 public function testisCommandLine( $expected, $wgCommandLineMode ) {
130 $this->setMwGlobals( [
131 'wgCommandLineMode' => $wgCommandLineMode,
132 ] );
133 $e = new MWException();
134 $this->assertEquals( $expected, $e->isCommandLine() );
135 }
136
137 public static function provideIsCommandLine() {
138 return [
139 [ false, null ],
140 [ true, true ],
141 ];
142 }
143
144 /**
145 * Verify the exception classes are JSON serializabe.
146 *
147 * @covers MWExceptionHandler::jsonSerializeException
148 * @dataProvider provideExceptionClasses
149 */
150 public function testJsonSerializeExceptions( $exception_class ) {
151 $json = MWExceptionHandler::jsonSerializeException(
152 new $exception_class()
153 );
154 $this->assertNotEquals( false, $json,
155 "The $exception_class exception should be JSON serializable, got false." );
156 }
157
158 public static function provideExceptionClasses() {
159 return [
160 [ 'Exception' ],
161 [ 'MWException' ],
162 ];
163 }
164
165 /**
166 * Lame JSON schema validation.
167 *
168 * @covers MWExceptionHandler::jsonSerializeException
169 *
170 * @param string $expectedKeyType Type expected as returned by gettype()
171 * @param string $exClass An exception class (ie: Exception, MWException)
172 * @param string $key Name of the key to validate in the serialized JSON
173 * @dataProvider provideJsonSerializedKeys
174 */
175 public function testJsonserializeexceptionKeys( $expectedKeyType, $exClass, $key ) {
176 # Make sure we log a backtrace:
177 $this->setMwGlobals( [ 'wgLogExceptionBacktrace' => true ] );
178
179 $json = json_decode(
180 MWExceptionHandler::jsonSerializeException( new $exClass() )
181 );
182 $this->assertObjectHasAttribute( $key, $json,
183 "JSON serialized exception is missing key '$key'"
184 );
185 $this->assertInternalType( $expectedKeyType, $json->$key,
186 "JSON serialized key '$key' has type " . gettype( $json->$key )
187 . " (expected: $expectedKeyType)."
188 );
189 }
190
191 /**
192 * Returns test cases: exception class, key name, gettype()
193 */
194 public static function provideJsonSerializedKeys() {
195 $testCases = [];
196 foreach ( [ 'Exception', 'MWException' ] as $exClass ) {
197 $exTests = [
198 [ 'string', $exClass, 'id' ],
199 [ 'string', $exClass, 'file' ],
200 [ 'integer', $exClass, 'line' ],
201 [ 'string', $exClass, 'message' ],
202 [ 'null', $exClass, 'url' ],
203 # Backtrace only enabled with wgLogExceptionBacktrace = true
204 [ 'array', $exClass, 'backtrace' ],
205 ];
206 $testCases = array_merge( $testCases, $exTests );
207 }
208 return $testCases;
209 }
210
211 /**
212 * Given wgLogExceptionBacktrace is true
213 * then serialized exception SHOULD have a backtrace
214 *
215 * @covers MWExceptionHandler::jsonSerializeException
216 */
217 public function testJsonserializeexceptionBacktracingEnabled() {
218 $this->setMwGlobals( [ 'wgLogExceptionBacktrace' => true ] );
219 $json = json_decode(
220 MWExceptionHandler::jsonSerializeException( new Exception() )
221 );
222 $this->assertObjectHasAttribute( 'backtrace', $json );
223 }
224
225 /**
226 * Given wgLogExceptionBacktrace is false
227 * then serialized exception SHOULD NOT have a backtrace
228 *
229 * @covers MWExceptionHandler::jsonSerializeException
230 */
231 public function testJsonserializeexceptionBacktracingDisabled() {
232 $this->setMwGlobals( [ 'wgLogExceptionBacktrace' => false ] );
233 $json = json_decode(
234 MWExceptionHandler::jsonSerializeException( new Exception() )
235 );
236 $this->assertObjectNotHasAttribute( 'backtrace', $json );
237 }
238
239 }