3 use Wikimedia\TestingAccessWrapper
;
7 * @covers ApiFormatBase
9 class ApiFormatBaseTest
extends ApiFormatTestBase
{
11 protected $printerName = 'mockbase';
13 protected function setUp() {
15 $this->setMwGlobals( [
16 'wgServer' => 'http://example.org'
20 public function getMockFormatter( ApiMain
$main = null, $format, $methods = [] ) {
21 if ( $main === null ) {
22 $context = new RequestContext
;
23 $context->setRequest( new FauxRequest( [], true ) );
24 $main = new ApiMain( $context );
27 $mock = $this->getMockBuilder( ApiFormatBase
::class )
28 ->setConstructorArgs( [ $main, $format ] )
29 ->setMethods( array_unique( array_merge( $methods, [ 'getMimeType', 'execute' ] ) ) )
31 if ( !in_array( 'getMimeType', $methods, true ) ) {
32 $mock->method( 'getMimeType' )->willReturn( 'text/x-mock' );
37 protected function encodeData( array $params, array $data, $options = [] ) {
40 'class' => ApiFormatBase
::class,
41 'factory' => function ( ApiMain
$main, $format ) use ( $options ) {
42 $mock = $this->getMockFormatter( $main, $format );
43 $mock->expects( $this->once() )->method( 'execute' )
44 ->willReturnCallback( function () use ( $mock ) {
45 $mock->printText( "Format {$mock->getFormat()}: " );
46 $mock->printText( "<b>ok</b>" );
49 if ( isset( $options['status'] ) ) {
50 $mock->setHttpStatus( $options['status'] );
55 'returnPrinter' => true,
58 $this->setMwGlobals( [
59 'wgApiFrameOptions' => 'DENY',
62 $ret = parent
::encodeData( $params, $data, $options );
63 $printer = TestingAccessWrapper
::newFromObject( $ret['printer'] );
66 if ( $options['name'] !== 'mockfm' ) {
68 $file = 'api-result.mock';
69 $status = $options['status'] ??
null;
70 } elseif ( isset( $params['wrappedhtml'] ) ) {
71 $ct = 'text/mediawiki-api-prettyprint-wrapped';
72 $file = 'api-result-wrapped.json';
75 // Replace varying field
76 $text = preg_replace( '/"time":\d+/', '"time":1234', $text );
79 $file = 'api-result.html';
82 // Strip OutputPage-generated HTML
83 if ( preg_match( '!<pre class="api-pretty-content">.*</pre>!s', $text, $m ) ) {
88 $response = $printer->getMain()->getRequest()->response();
89 $this->assertSame( "$ct; charset=utf-8", strtolower( $response->getHeader( 'Content-Type' ) ) );
90 $this->assertSame( 'DENY', $response->getHeader( 'X-Frame-Options' ) );
91 $this->assertSame( $file, $printer->getFilename() );
92 $this->assertSame( "inline; filename=$file", $response->getHeader( 'Content-Disposition' ) );
93 $this->assertSame( $status, $response->getStatusCode() );
98 public static function provideGeneralEncoding() {
102 "Format MOCK: <b>ok</b>",
106 'normal ignores wrappedhtml' => [
108 "Format MOCK: <b>ok</b>",
109 [ 'wrappedhtml' => 1 ],
114 '<pre class="api-pretty-content">Format MOCK: <b>ok</b></pre>',
116 [ 'name' => 'mockfm' ]
118 'wrapped HTML format' => [
120 // phpcs:ignore Generic.Files.LineLength.TooLong
121 '{"status":200,"statustext":"OK","html":"<pre class=\"api-pretty-content\">Format MOCK: <b>ok</b></pre>","modules":["mediawiki.apipretty"],"continue":null,"time":1234}',
122 [ 'wrappedhtml' => 1 ],
123 [ 'name' => 'mockfm' ]
125 'normal, with set status' => [
127 "Format MOCK: <b>ok</b>",
129 [ 'name' => 'mock', 'status' => 400 ]
131 'HTML format, with set status' => [
133 '<pre class="api-pretty-content">Format MOCK: <b>ok</b></pre>',
135 [ 'name' => 'mockfm', 'status' => 400 ]
137 'wrapped HTML format, with set status' => [
139 // phpcs:ignore Generic.Files.LineLength.TooLong
140 '{"status":400,"statustext":"Bad Request","html":"<pre class=\"api-pretty-content\">Format MOCK: <b>ok</b></pre>","modules":["mediawiki.apipretty"],"continue":null,"time":1234}',
141 [ 'wrappedhtml' => 1 ],
142 [ 'name' => 'mockfm', 'status' => 400 ]
144 'wrapped HTML format, cross-domain-policy' => [
145 [ 'continue' => '< CrOsS-DoMaIn-PoLiCy >' ],
146 // phpcs:ignore Generic.Files.LineLength.TooLong
147 '{"status":200,"statustext":"OK","html":"<pre class=\"api-pretty-content\">Format MOCK: <b>ok</b></pre>","modules":["mediawiki.apipretty"],"continue":"\u003C CrOsS-DoMaIn-PoLiCy \u003E","time":1234}',
148 [ 'wrappedhtml' => 1 ],
149 [ 'name' => 'mockfm' ]
155 * @dataProvider provideFilenameEncoding
157 public function testFilenameEncoding( $filename, $expect ) {
158 $ret = parent
::encodeData( [], [], [
160 'class' => ApiFormatBase
::class,
161 'factory' => function ( ApiMain
$main, $format ) use ( $filename ) {
162 $mock = $this->getMockFormatter( $main, $format, [ 'getFilename' ] );
163 $mock->method( 'getFilename' )->willReturn( $filename );
166 'returnPrinter' => true,
168 $response = $ret['printer']->getMain()->getRequest()->response();
170 $this->assertSame( "inline; $expect", $response->getHeader( 'Content-Disposition' ) );
173 public static function provideFilenameEncoding() {
175 'something simple' => [
176 'foo.xyz', 'filename=foo.xyz'
178 'more complicated, but still simple' => [
179 'foo.!#$%&\'*+-^_`|~', 'filename=foo.!#$%&\'*+-^_`|~'
182 'foo\\bar.xyz', 'filename="foo\\\\bar.xyz"'
184 'Needs quoting (2)' => [
185 'foo (bar).xyz', 'filename="foo (bar).xyz"'
187 'Needs quoting (3)' => [
188 "foo\t\"b\x5car\"\0.xyz", "filename=\"foo\x5c\t\x5c\"b\x5c\x5car\x5c\"\x5c\0.xyz\""
190 'Non-ASCII characters' => [
192 "filename=\"f\xF3o b\xE1r.?!\"; filename*=UTF-8''f%C3%B3o%20b%C3%A1r.%F0%9F%99%8C!"
197 public function testBasics() {
198 $printer = $this->getMockFormatter( null, 'mock' );
199 $this->assertTrue( $printer->canPrintErrors() );
201 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Data_formats',
202 $printer->getHelpUrls()
206 public function testDisable() {
207 $this->setMwGlobals( [
208 'wgApiFrameOptions' => 'DENY',
211 $printer = $this->getMockFormatter( null, 'mock' );
212 $printer->method( 'execute' )->willReturnCallback( function () use ( $printer ) {
213 $printer->printText( 'Foo' );
215 $this->assertFalse( $printer->isDisabled() );
217 $this->assertTrue( $printer->isDisabled() );
219 $printer->setHttpStatus( 400 );
220 $printer->initPrinter();
223 $printer->closePrinter();
224 $this->assertSame( '', ob_get_clean() );
225 $response = $printer->getMain()->getRequest()->response();
226 $this->assertNull( $response->getHeader( 'Content-Type' ) );
227 $this->assertNull( $response->getHeader( 'X-Frame-Options' ) );
228 $this->assertNull( $response->getHeader( 'Content-Disposition' ) );
229 $this->assertNull( $response->getStatusCode() );
232 public function testNullMimeType() {
233 $this->setMwGlobals( [
234 'wgApiFrameOptions' => 'DENY',
237 $printer = $this->getMockFormatter( null, 'mock', [ 'getMimeType' ] );
238 $printer->method( 'execute' )->willReturnCallback( function () use ( $printer ) {
239 $printer->printText( 'Foo' );
241 $printer->method( 'getMimeType' )->willReturn( null );
242 $this->assertNull( $printer->getMimeType(), 'sanity check' );
244 $printer->initPrinter();
247 $printer->closePrinter();
248 $this->assertSame( 'Foo', ob_get_clean() );
249 $response = $printer->getMain()->getRequest()->response();
250 $this->assertNull( $response->getHeader( 'Content-Type' ) );
251 $this->assertNull( $response->getHeader( 'X-Frame-Options' ) );
252 $this->assertNull( $response->getHeader( 'Content-Disposition' ) );
254 $printer = $this->getMockFormatter( null, 'mockfm', [ 'getMimeType' ] );
255 $printer->method( 'execute' )->willReturnCallback( function () use ( $printer ) {
256 $printer->printText( 'Foo' );
258 $printer->method( 'getMimeType' )->willReturn( null );
259 $this->assertNull( $printer->getMimeType(), 'sanity check' );
260 $this->assertTrue( $printer->getIsHtml(), 'sanity check' );
262 $printer->initPrinter();
265 $printer->closePrinter();
266 $this->assertSame( 'Foo', ob_get_clean() );
267 $response = $printer->getMain()->getRequest()->response();
269 'text/html; charset=utf-8', strtolower( $response->getHeader( 'Content-Type' ) )
271 $this->assertSame( 'DENY', $response->getHeader( 'X-Frame-Options' ) );
273 'inline; filename=api-result.html', $response->getHeader( 'Content-Disposition' )
277 public function testApiFrameOptions() {
278 $this->setMwGlobals( [ 'wgApiFrameOptions' => 'DENY' ] );
279 $printer = $this->getMockFormatter( null, 'mock' );
280 $printer->initPrinter();
283 $printer->getMain()->getRequest()->response()->getHeader( 'X-Frame-Options' )
286 $this->setMwGlobals( [ 'wgApiFrameOptions' => 'SAMEORIGIN' ] );
287 $printer = $this->getMockFormatter( null, 'mock' );
288 $printer->initPrinter();
291 $printer->getMain()->getRequest()->response()->getHeader( 'X-Frame-Options' )
294 $this->setMwGlobals( [ 'wgApiFrameOptions' => false ] );
295 $printer = $this->getMockFormatter( null, 'mock' );
296 $printer->initPrinter();
298 $printer->getMain()->getRequest()->response()->getHeader( 'X-Frame-Options' )
302 public function testForceDefaultParams() {
303 $context = new RequestContext
;
304 $context->setRequest( new FauxRequest( [ 'foo' => '1', 'bar' => '2', 'baz' => '3' ], true ) );
305 $main = new ApiMain( $context );
308 'bar' => [ ApiBase
::PARAM_DFLT
=> 'bar?' ],
312 $printer = $this->getMockFormatter( $main, 'mock', [ 'getAllowedParams' ] );
313 $printer->method( 'getAllowedParams' )->willReturn( $allowedParams );
315 [ 'foo' => '1', 'bar' => '2', 'baz' => '3' ],
316 $printer->extractRequestParams(),
320 $printer = $this->getMockFormatter( $main, 'mock', [ 'getAllowedParams' ] );
321 $printer->method( 'getAllowedParams' )->willReturn( $allowedParams );
322 $printer->forceDefaultParams();
324 [ 'foo' => null, 'bar' => 'bar?', 'baz' => 'baz!' ],
325 $printer->extractRequestParams()
329 public function testGetAllowedParams() {
330 $printer = $this->getMockFormatter( null, 'mock' );
331 $this->assertSame( [], $printer->getAllowedParams() );
333 $printer = $this->getMockFormatter( null, 'mockfm' );
336 ApiBase
::PARAM_DFLT
=> false,
337 ApiBase
::PARAM_HELP_MSG
=> 'apihelp-format-param-wrappedhtml',
339 ], $printer->getAllowedParams() );
342 public function testGetExamplesMessages() {
343 $printer = TestingAccessWrapper
::newFromObject( $this->getMockFormatter( null, 'mock' ) );
345 'action=query&meta=siteinfo&siprop=namespaces&format=mock'
346 => [ 'apihelp-format-example-generic', 'MOCK' ]
347 ], $printer->getExamplesMessages() );
349 $printer = TestingAccessWrapper
::newFromObject( $this->getMockFormatter( null, 'mockfm' ) );
351 'action=query&meta=siteinfo&siprop=namespaces&format=mockfm'
352 => [ 'apihelp-format-example-generic', 'MOCK' ]
353 ], $printer->getExamplesMessages() );
357 * @dataProvider provideHtmlHeader
359 public function testHtmlHeader( $post, $registerNonHtml, $expect ) {
360 $context = new RequestContext
;
361 $request = new FauxRequest( [ 'a' => 1, 'b' => 2 ], $post );
362 $request->setRequestURL( '/wx/api.php' );
363 $context->setRequest( $request );
364 $context->setLanguage( 'qqx' );
365 $main = new ApiMain( $context );
366 $printer = $this->getMockFormatter( $main, 'mockfm' );
367 $mm = $printer->getMain()->getModuleManager();
368 $mm->addModule( 'mockfm', 'format', ApiFormatBase
::class, function () {
371 if ( $registerNonHtml ) {
372 $mm->addModule( 'mock', 'format', ApiFormatBase
::class, function () {
377 $printer->initPrinter();
380 $printer->closePrinter();
381 $text = ob_get_clean();
382 $this->assertContains( $expect, $text );
385 public static function provideHtmlHeader() {
387 [ false, false, '(api-format-prettyprint-header-only-html: MOCK)' ],
388 [ true, false, '(api-format-prettyprint-header-only-html: MOCK)' ],
389 // phpcs:ignore Generic.Files.LineLength.TooLong
390 [ 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>)' ],
391 [ true, true, '(api-format-prettyprint-header: MOCK, mock)' ],