| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Api\Format; |
| 4 |
|
| 5 |
use MediaWiki\Api\ApiBase; |
| 6 |
use MediaWiki\Api\ApiFormatBase; |
| 7 |
use MediaWiki\Api\ApiMain; |
| 8 |
use MediaWiki\Context\RequestContext; |
| 9 |
use MediaWiki\MainConfigNames; |
| 10 |
use MediaWiki\Request\FauxRequest; |
| 11 |
use PHPUnit\Framework\MockObject\MockObject; |
| 12 |
use Wikimedia\ParamValidator\ParamValidator; |
| 13 |
use Wikimedia\TestingAccessWrapper; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class ApiFormatBaseTest extends ApiFormatTestBase { |
| 21 |
|
| 22 |
|
| 23 |
protected $printerName = 'mockbase'; |
| 24 |
|
| 25 |
protected function setUp(): void { |
| 26 |
parent::setUp(); |
| 27 |
$this->overrideConfigValue( MainConfigNames::Server, 'http://example.org' ); |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public function getMockFormatter( ?ApiMain $main, $format, $methods = [] ) { |
| 37 |
if ( $main === null ) { |
| 38 |
$context = new RequestContext; |
| 39 |
$context->setRequest( new FauxRequest( [], true ) ); |
| 40 |
$main = new ApiMain( $context ); |
| 41 |
} |
| 42 |
|
| 43 |
$mock = $this->getMockBuilder( ApiFormatBase::class ) |
| 44 |
->setConstructorArgs( [ $main, $format ] ) |
| 45 |
->onlyMethods( array_unique( array_merge( $methods, [ 'getMimeType', 'execute' ] ) ) ) |
| 46 |
->getMock(); |
| 47 |
if ( !in_array( 'getMimeType', $methods, true ) ) { |
| 48 |
$mock->method( 'getMimeType' )->willReturn( 'text/x-mock' ); |
| 49 |
} |
| 50 |
return $mock; |
| 51 |
} |
| 52 |
|
| 53 |
protected function encodeData( array $params, array $data, $options = [] ) { |
| 54 |
$options += [ |
| 55 |
'name' => 'mock', |
| 56 |
'class' => ApiFormatBase::class, |
| 57 |
'factory' => function ( ApiMain $main, $format ) use ( $options ) { |
| 58 |
$mock = $this->getMockFormatter( $main, $format ); |
| 59 |
$mock->expects( $this->once() )->method( 'execute' ) |
| 60 |
->willReturnCallback( static function () use ( $mock ) { |
| 61 |
$mock->printText( "Format {$mock->getFormat()}: " ); |
| 62 |
$mock->printText( "<b>ok</b>" ); |
| 63 |
} ); |
| 64 |
|
| 65 |
if ( isset( $options['status'] ) ) { |
| 66 |
$mock->setHttpStatus( $options['status'] ); |
| 67 |
} |
| 68 |
|
| 69 |
return $mock; |
| 70 |
}, |
| 71 |
'returnPrinter' => true, |
| 72 |
]; |
| 73 |
|
| 74 |
$this->overrideConfigValue( MainConfigNames::ApiFrameOptions, 'DENY' ); |
| 75 |
|
| 76 |
$ret = parent::encodeData( $params, $data, $options ); |
| 77 |
|
| 78 |
$printer = $ret['printer']; |
| 79 |
$text = $ret['text']; |
| 80 |
|
| 81 |
if ( $options['name'] !== 'mockfm' ) { |
| 82 |
$ct = 'text/x-mock'; |
| 83 |
$file = 'api-result.mock'; |
| 84 |
$status = $options['status'] ?? null; |
| 85 |
} elseif ( isset( $params['wrappedhtml'] ) ) { |
| 86 |
$ct = 'text/mediawiki-api-prettyprint-wrapped'; |
| 87 |
$file = 'api-result-wrapped.json'; |
| 88 |
$status = null; |
| 89 |
|
| 90 |
|
| 91 |
$text = preg_replace( '/"time":\d+/', '"time":1234', $text ); |
| 92 |
} else { |
| 93 |
$ct = 'text/html'; |
| 94 |
$file = 'api-result.html'; |
| 95 |
$status = null; |
| 96 |
|
| 97 |
|
| 98 |
if ( preg_match( '!<pre class="api-pretty-content">.*</pre>!s', $text, $m ) ) { |
| 99 |
$text = $m[0]; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
$response = $printer->getMain()->getRequest()->response(); |
| 104 |
$this->assertSame( "$ct; charset=utf-8", strtolower( $response->getHeader( 'Content-Type' ) ) ); |
| 105 |
$this->assertSame( 'DENY', $response->getHeader( 'X-Frame-Options' ) ); |
| 106 |
$this->assertSame( $file, $printer->getFilename() ); |
| 107 |
$this->assertSame( "inline; filename=$file", $response->getHeader( 'Content-Disposition' ) ); |
| 108 |
$this->assertSame( $status, $response->getStatusCode() ); |
| 109 |
|
| 110 |
return $text; |
| 111 |
} |
| 112 |
|
| 113 |
public static function provideGeneralEncoding() { |
| 114 |
return [ |
| 115 |
'normal' => [ |
| 116 |
[], |
| 117 |
"Format MOCK: <b>ok</b>", |
| 118 |
[], |
| 119 |
[ 'name' => 'mock' ] |
| 120 |
], |
| 121 |
'normal ignores wrappedhtml' => [ |
| 122 |
[], |
| 123 |
"Format MOCK: <b>ok</b>", |
| 124 |
[ 'wrappedhtml' => 1 ], |
| 125 |
[ 'name' => 'mock' ] |
| 126 |
], |
| 127 |
'HTML format' => [ |
| 128 |
[], |
| 129 |
'<pre class="api-pretty-content">Format MOCK: <b>ok</b></pre>', |
| 130 |
[], |
| 131 |
[ 'name' => 'mockfm' ] |
| 132 |
], |
| 133 |
'wrapped HTML format' => [ |
| 134 |
[], |
| 135 |
'{"status":200,"statustext":"OK","html":"<pre class=\"api-pretty-content\">Format MOCK: <b>ok</b></pre>","modules":["mediawiki.apipretty"],"continue":null,"time":1234}', |
| 136 |
[ 'wrappedhtml' => 1 ], |
| 137 |
[ 'name' => 'mockfm' ] |
| 138 |
], |
| 139 |
'normal, with set status' => [ |
| 140 |
[], |
| 141 |
"Format MOCK: <b>ok</b>", |
| 142 |
[], |
| 143 |
[ 'name' => 'mock', 'status' => 400 ] |
| 144 |
], |
| 145 |
'HTML format, with set status' => [ |
| 146 |
[], |
| 147 |
'<pre class="api-pretty-content">Format MOCK: <b>ok</b></pre>', |
| 148 |
[], |
| 149 |
[ 'name' => 'mockfm', 'status' => 400 ] |
| 150 |
], |
| 151 |
'wrapped HTML format, with set status' => [ |
| 152 |
[], |
| 153 |
'{"status":400,"statustext":"Bad Request","html":"<pre class=\"api-pretty-content\">Format MOCK: <b>ok</b></pre>","modules":["mediawiki.apipretty"],"continue":null,"time":1234}', |
| 154 |
[ 'wrappedhtml' => 1 ], |
| 155 |
[ 'name' => 'mockfm', 'status' => 400 ] |
| 156 |
], |
| 157 |
]; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function testFilenameEncoding( $filename, $expect ) { |
| 164 |
$ret = parent::encodeData( [], [], [ |
| 165 |
'name' => 'mock', |
| 166 |
'class' => ApiFormatBase::class, |
| 167 |
'factory' => function ( ApiMain $main, $format ) use ( $filename ) { |
| 168 |
$mock = $this->getMockFormatter( $main, $format, [ 'getFilename' ] ); |
| 169 |
$mock->method( 'getFilename' )->willReturn( $filename ); |
| 170 |
return $mock; |
| 171 |
}, |
| 172 |
'returnPrinter' => true, |
| 173 |
] ); |
| 174 |
$response = $ret['printer']->getMain()->getRequest()->response(); |
| 175 |
|
| 176 |
$this->assertSame( "inline; $expect", $response->getHeader( 'Content-Disposition' ) ); |
| 177 |
} |
| 178 |
|
| 179 |
public static function provideFilenameEncoding() { |
| 180 |
return [ |
| 181 |
'something simple' => [ |
| 182 |
'foo.xyz', 'filename=foo.xyz' |
| 183 |
], |
| 184 |
'more complicated, but still simple' => [ |
| 185 |
'foo.!#$%&\'*+-^_`|~', 'filename=foo.!#$%&\'*+-^_`|~' |
| 186 |
], |
| 187 |
'Needs quoting' => [ |
| 188 |
'foo\\bar.xyz', 'filename="foo\\\\bar.xyz"' |
| 189 |
], |
| 190 |
'Needs quoting (2)' => [ |
| 191 |
'foo (bar).xyz', 'filename="foo (bar).xyz"' |
| 192 |
], |
| 193 |
'Needs quoting (3)' => [ |
| 194 |
"foo\t\"b\x5car\"\0.xyz", "filename=\"foo\x5c\t\x5c\"b\x5c\x5car\x5c\"\x5c\0.xyz\"" |
| 195 |
], |
| 196 |
'Non-ASCII characters' => [ |
| 197 |
'fóo bár.🙌!', |
| 198 |
"filename=\"f\xF3o b\xE1r.?!\"; filename*=UTF-8''f%C3%B3o%20b%C3%A1r.%F0%9F%99%8C!" |
| 199 |
] |
| 200 |
]; |
| 201 |
} |
| 202 |
|
| 203 |
public function testBasics() { |
| 204 |
$printer = $this->getMockFormatter( null, 'mock' ); |
| 205 |
$this->assertTrue( $printer->canPrintErrors() ); |
| 206 |
$this->assertSame( |
| 207 |
'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Data_formats', |
| 208 |
$printer->getHelpUrls() |
| 209 |
); |
| 210 |
} |
| 211 |
|
| 212 |
public function testDisable() { |
| 213 |
$this->overrideConfigValue( MainConfigNames::ApiFrameOptions, 'DENY' ); |
| 214 |
|
| 215 |
$printer = $this->getMockFormatter( null, 'mock' ); |
| 216 |
$printer->method( 'execute' )->willReturnCallback( static function () use ( $printer ) { |
| 217 |
$printer->printText( 'Foo' ); |
| 218 |
} ); |
| 219 |
$this->assertFalse( $printer->isDisabled() ); |
| 220 |
$printer->disable(); |
| 221 |
$this->assertTrue( $printer->isDisabled() ); |
| 222 |
|
| 223 |
$printer->setHttpStatus( 400 ); |
| 224 |
$printer->initPrinter(); |
| 225 |
$printer->execute(); |
| 226 |
ob_start(); |
| 227 |
$printer->closePrinter(); |
| 228 |
$this->assertSame( '', ob_get_clean() ); |
| 229 |
$response = $printer->getMain()->getRequest()->response(); |
| 230 |
$this->assertNull( $response->getHeader( 'Content-Type' ) ); |
| 231 |
$this->assertNull( $response->getHeader( 'X-Frame-Options' ) ); |
| 232 |
$this->assertNull( $response->getHeader( 'Content-Disposition' ) ); |
| 233 |
$this->assertNull( $response->getStatusCode() ); |
| 234 |
} |
| 235 |
|
| 236 |
public function testNullMimeType() { |
| 237 |
$this->overrideConfigValue( MainConfigNames::ApiFrameOptions, 'DENY' ); |
| 238 |
|
| 239 |
$printer = $this->getMockFormatter( null, 'mock', [ 'getMimeType' ] ); |
| 240 |
$printer->method( 'execute' )->willReturnCallback( static function () use ( $printer ) { |
| 241 |
$printer->printText( 'Foo' ); |
| 242 |
} ); |
| 243 |
$printer->method( 'getMimeType' )->willReturn( null ); |
| 244 |
$this->assertNull( $printer->getMimeType() ); |
| 245 |
|
| 246 |
$printer->initPrinter(); |
| 247 |
$printer->execute(); |
| 248 |
ob_start(); |
| 249 |
$printer->closePrinter(); |
| 250 |
$this->assertSame( 'Foo', ob_get_clean() ); |
| 251 |
$response = $printer->getMain()->getRequest()->response(); |
| 252 |
$this->assertNull( $response->getHeader( 'Content-Type' ) ); |
| 253 |
$this->assertNull( $response->getHeader( 'X-Frame-Options' ) ); |
| 254 |
$this->assertNull( $response->getHeader( 'Content-Disposition' ) ); |
| 255 |
|
| 256 |
$printer = $this->getMockFormatter( null, 'mockfm', [ 'getMimeType' ] ); |
| 257 |
$printer->method( 'execute' )->willReturnCallback( static function () use ( $printer ) { |
| 258 |
$printer->printText( 'Foo' ); |
| 259 |
} ); |
| 260 |
$printer->method( 'getMimeType' )->willReturn( null ); |
| 261 |
$this->assertNull( $printer->getMimeType() ); |
| 262 |
$this->assertTrue( $printer->getIsHtml() ); |
| 263 |
|
| 264 |
$printer->initPrinter(); |
| 265 |
$printer->execute(); |
| 266 |
ob_start(); |
| 267 |
$printer->closePrinter(); |
| 268 |
$this->assertSame( 'Foo', ob_get_clean() ); |
| 269 |
$response = $printer->getMain()->getRequest()->response(); |
| 270 |
$this->assertSame( |
| 271 |
'text/html; charset=utf-8', strtolower( $response->getHeader( 'Content-Type' ) ) |
| 272 |
); |
| 273 |
$this->assertSame( 'DENY', $response->getHeader( 'X-Frame-Options' ) ); |
| 274 |
$this->assertSame( |
| 275 |
'inline; filename=api-result.html', $response->getHeader( 'Content-Disposition' ) |
| 276 |
); |
| 277 |
} |
| 278 |
|
| 279 |
public static function provideApiFrameOptions() { |
| 280 |
yield 'Override ApiFrameOptions to DENY' => [ 'DENY', 'DENY' ]; |
| 281 |
yield 'Override ApiFrameOptions to SAMEORIGIN' => [ 'SAMEORIGIN', 'SAMEORIGIN' ]; |
| 282 |
yield 'Override ApiFrameOptions to false' => [ false, null ]; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
public function testApiFrameOptions( $customConfig, $expectedHeader ) { |
| 289 |
$this->overrideConfigValue( MainConfigNames::ApiFrameOptions, $customConfig ); |
| 290 |
$printer = $this->getMockFormatter( null, 'mock' ); |
| 291 |
$printer->initPrinter(); |
| 292 |
$this->assertSame( |
| 293 |
$expectedHeader, |
| 294 |
$printer->getMain()->getRequest()->response()->getHeader( 'X-Frame-Options' ) |
| 295 |
); |
| 296 |
} |
| 297 |
|
| 298 |
public function testForceDefaultParams() { |
| 299 |
$context = new RequestContext; |
| 300 |
$context->setRequest( new FauxRequest( [ 'foo' => '1', 'bar' => '2', 'baz' => '3' ], true ) ); |
| 301 |
$main = new ApiMain( $context ); |
| 302 |
$allowedParams = [ |
| 303 |
'foo' => [], |
| 304 |
'bar' => [ ParamValidator::PARAM_DEFAULT => 'bar?' ], |
| 305 |
'baz' => 'baz!', |
| 306 |
]; |
| 307 |
|
| 308 |
$printer = $this->getMockFormatter( $main, 'mock', [ 'getAllowedParams' ] ); |
| 309 |
$printer->method( 'getAllowedParams' )->willReturn( $allowedParams ); |
| 310 |
$this->assertEquals( |
| 311 |
[ 'foo' => '1', 'bar' => '2', 'baz' => '3' ], |
| 312 |
$printer->extractRequestParams() |
| 313 |
); |
| 314 |
|
| 315 |
$printer = $this->getMockFormatter( $main, 'mock', [ 'getAllowedParams' ] ); |
| 316 |
$printer->method( 'getAllowedParams' )->willReturn( $allowedParams ); |
| 317 |
$printer->forceDefaultParams(); |
| 318 |
$this->assertEquals( |
| 319 |
[ 'foo' => null, 'bar' => 'bar?', 'baz' => 'baz!' ], |
| 320 |
$printer->extractRequestParams() |
| 321 |
); |
| 322 |
} |
| 323 |
|
| 324 |
public function testGetAllowedParams() { |
| 325 |
$printer = $this->getMockFormatter( null, 'mock' ); |
| 326 |
$this->assertSame( [], $printer->getAllowedParams() ); |
| 327 |
|
| 328 |
$printer = $this->getMockFormatter( null, 'mockfm' ); |
| 329 |
$this->assertSame( [ |
| 330 |
'wrappedhtml' => [ |
| 331 |
ParamValidator::PARAM_DEFAULT => false, |
| 332 |
ApiBase::PARAM_HELP_MSG => 'apihelp-format-param-wrappedhtml', |
| 333 |
] |
| 334 |
], $printer->getAllowedParams() ); |
| 335 |
} |
| 336 |
|
| 337 |
public function testGetExamplesMessages() { |
| 338 |
|
| 339 |
$printer = TestingAccessWrapper::newFromObject( $this->getMockFormatter( null, 'mock' ) ); |
| 340 |
$this->assertSame( [ |
| 341 |
'action=query&meta=siteinfo&siprop=namespaces&format=mock' |
| 342 |
=> [ 'apihelp-format-example-generic', 'MOCK' ] |
| 343 |
], $printer->getExamplesMessages() ); |
| 344 |
|
| 345 |
$printer = TestingAccessWrapper::newFromObject( $this->getMockFormatter( null, 'mockfm' ) ); |
| 346 |
$this->assertSame( [ |
| 347 |
'action=query&meta=siteinfo&siprop=namespaces&format=mockfm' |
| 348 |
=> [ 'apihelp-format-example-generic', 'MOCK' ] |
| 349 |
], $printer->getExamplesMessages() ); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public function testHtmlHeader( $post, $registerNonHtml, $expect ) { |
| 356 |
$context = new RequestContext; |
| 357 |
$request = new FauxRequest( [ 'a' => 1, 'b' => 2 ], $post ); |
| 358 |
$request->setRequestURL( '/wx/api.php' ); |
| 359 |
$context->setRequest( $request ); |
| 360 |
$context->setLanguage( 'qqx' ); |
| 361 |
$main = new ApiMain( $context ); |
| 362 |
$printer = $this->getMockFormatter( $main, 'mockfm' ); |
| 363 |
$mm = $printer->getMain()->getModuleManager(); |
| 364 |
$mm->addModule( 'mockfm', 'format', [ |
| 365 |
'class' => ApiFormatBase::class, |
| 366 |
'factory' => static function () { |
| 367 |
return $mock; |
| 368 |
} |
| 369 |
] ); |
| 370 |
if ( $registerNonHtml ) { |
| 371 |
$mm->addModule( 'mock', 'format', [ |
| 372 |
'class' => ApiFormatBase::class, |
| 373 |
'factory' => static function () { |
| 374 |
return $mock; |
| 375 |
} |
| 376 |
] ); |
| 377 |
} |
| 378 |
|
| 379 |
$printer->initPrinter(); |
| 380 |
$printer->execute(); |
| 381 |
ob_start(); |
| 382 |
$printer->closePrinter(); |
| 383 |
$text = ob_get_clean(); |
| 384 |
$this->assertStringContainsString( $expect, $text ); |
| 385 |
$this->assertSame( 'private, must-revalidate, max-age=0', $main->getContext()->getRequest()->response()->getHeader( 'Cache-Control' ) ); |
| 386 |
} |
| 387 |
|
| 388 |
public static function provideHtmlIsPrivate() { |
| 389 |
yield [ 'private', 'private' ]; |
| 390 |
yield [ 'public', 'anon-public-user-private' ]; |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
public function testHtmlIsPrivate( $moduleCacheMode, $expectedCacheMode ) { |
| 398 |
$context = new RequestContext; |
| 399 |
$request = new FauxRequest( [ 'uselang' => 'qqx' ] ); |
| 400 |
$request->setRequestURL( '/wx/api.php' ); |
| 401 |
$context->setRequest( $request ); |
| 402 |
$context->setLanguage( 'qqx' ); |
| 403 |
$main = new ApiMain( $context ); |
| 404 |
|
| 405 |
$printer = $this->getMockFormatter( $main, 'mockfm' ); |
| 406 |
$mm = $printer->getMain()->getModuleManager(); |
| 407 |
$mm->addModule( 'mockfm', 'format', [ |
| 408 |
'class' => ApiFormatBase::class, |
| 409 |
'factory' => static function () { |
| 410 |
return $mock; |
| 411 |
} |
| 412 |
] ); |
| 413 |
|
| 414 |
|
| 415 |
$main->setCacheMode( $moduleCacheMode ); |
| 416 |
$printer->initPrinter(); |
| 417 |
|
| 418 |
$mainAccess = TestingAccessWrapper::newFromObject( $main ); |
| 419 |
$this->assertSame( $expectedCacheMode, $main->getCacheMode() ); |
| 420 |
|
| 421 |
$mainAccess->sendCacheHeaders( false ); |
| 422 |
$this->assertSame( |
| 423 |
'private, must-revalidate, max-age=0', |
| 424 |
$request->response()->getHeader( 'cache-control' ) |
| 425 |
); |
| 426 |
} |
| 427 |
|
| 428 |
public static function provideHtmlHeader() { |
| 429 |
return [ |
| 430 |
[ false, false, '(api-format-prettyprint-header-only-html: MOCK)' ], |
| 431 |
[ true, false, '(api-format-prettyprint-header-only-html: MOCK)' ], |
| 432 |
[ false, true, '(api-format-prettyprint-header-hyperlinked: MOCK, mock, <a rel="nofollow" class="external free" href="http://example.org/wx/api.php?a=1&b=2&format=mock">http://example.org/wx/api.php?a=1&b=2&format=mock</a>)' ], |
| 433 |
[ true, true, '(api-format-prettyprint-header: MOCK, mock)' ], |
| 434 |
]; |
| 435 |
} |
| 436 |
|
| 437 |
} |