jquery.textSelection: Implement 'encapsulateSelection' in terms of the other commands
[lhc/web/wiklou.git] / tests / phpunit / includes / api / format / ApiFormatBaseTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4
5 /**
6 * @group API
7 * @covers ApiFormatBase
8 */
9 class ApiFormatBaseTest extends ApiFormatTestBase {
10
11 protected $printerName = 'mockbase';
12
13 public function getMockFormatter( ApiMain $main = null, $format, $methods = [] ) {
14 if ( $main === null ) {
15 $context = new RequestContext;
16 $context->setRequest( new FauxRequest( [], true ) );
17 $main = new ApiMain( $context );
18 }
19
20 $mock = $this->getMockBuilder( ApiFormatBase::class )
21 ->setConstructorArgs( [ $main, $format ] )
22 ->setMethods( array_unique( array_merge( $methods, [ 'getMimeType', 'execute' ] ) ) )
23 ->getMock();
24 if ( !in_array( 'getMimeType', $methods, true ) ) {
25 $mock->method( 'getMimeType' )->willReturn( 'text/x-mock' );
26 }
27 return $mock;
28 }
29
30 protected function encodeData( array $params, array $data, $options = [] ) {
31 $options += [
32 'name' => 'mock',
33 'class' => ApiFormatBase::class,
34 'factory' => function ( ApiMain $main, $format ) use ( $options ) {
35 $mock = $this->getMockFormatter( $main, $format );
36 $mock->expects( $this->once() )->method( 'execute' )
37 ->willReturnCallback( function () use ( $mock ) {
38 $mock->printText( "Format {$mock->getFormat()}: " );
39 $mock->printText( "<b>ok</b>" );
40 } );
41
42 if ( isset( $options['status'] ) ) {
43 $mock->setHttpStatus( $options['status'] );
44 }
45
46 return $mock;
47 },
48 'returnPrinter' => true,
49 ];
50
51 $this->setMwGlobals( [
52 'wgApiFrameOptions' => 'DENY',
53 ] );
54
55 $ret = parent::encodeData( $params, $data, $options );
56 $printer = TestingAccessWrapper::newFromObject( $ret['printer'] );
57 $text = $ret['text'];
58
59 if ( $options['name'] !== 'mockfm' ) {
60 $ct = 'text/x-mock';
61 $file = 'api-result.mock';
62 $status = isset( $options['status'] ) ? $options['status'] : null;
63 } elseif ( isset( $params['wrappedhtml'] ) ) {
64 $ct = 'text/mediawiki-api-prettyprint-wrapped';
65 $file = 'api-result-wrapped.json';
66 $status = null;
67
68 // Replace varying field
69 $text = preg_replace( '/"time":\d+/', '"time":1234', $text );
70 } else {
71 $ct = 'text/html';
72 $file = 'api-result.html';
73 $status = null;
74
75 // Strip OutputPage-generated HTML
76 if ( preg_match( '!<pre class="api-pretty-content">.*</pre>!s', $text, $m ) ) {
77 $text = $m[0];
78 }
79 }
80
81 $response = $printer->getMain()->getRequest()->response();
82 $this->assertSame( "$ct; charset=utf-8", strtolower( $response->getHeader( 'Content-Type' ) ) );
83 $this->assertSame( 'DENY', $response->getHeader( 'X-Frame-Options' ) );
84 $this->assertSame( $file, $printer->getFilename() );
85 $this->assertSame( "inline; filename=\"$file\"", $response->getHeader( 'Content-Disposition' ) );
86 $this->assertSame( $status, $response->getStatusCode() );
87
88 return $text;
89 }
90
91 public static function provideGeneralEncoding() {
92 return [
93 'normal' => [
94 [],
95 "Format MOCK: <b>ok</b>",
96 [],
97 [ 'name' => 'mock' ]
98 ],
99 'normal ignores wrappedhtml' => [
100 [],
101 "Format MOCK: <b>ok</b>",
102 [ 'wrappedhtml' => 1 ],
103 [ 'name' => 'mock' ]
104 ],
105 'HTML format' => [
106 [],
107 '<pre class="api-pretty-content">Format MOCK: &lt;b>ok&lt;/b></pre>',
108 [],
109 [ 'name' => 'mockfm' ]
110 ],
111 'wrapped HTML format' => [
112 [],
113 // phpcs:ignore Generic.Files.LineLength.TooLong
114 '{"status":200,"statustext":"OK","html":"<pre class=\"api-pretty-content\">Format MOCK: &lt;b>ok&lt;/b></pre>","modules":["mediawiki.apipretty"],"continue":null,"time":1234}',
115 [ 'wrappedhtml' => 1 ],
116 [ 'name' => 'mockfm' ]
117 ],
118 'normal, with set status' => [
119 [],
120 "Format MOCK: <b>ok</b>",
121 [],
122 [ 'name' => 'mock', 'status' => 400 ]
123 ],
124 'HTML format, with set status' => [
125 [],
126 '<pre class="api-pretty-content">Format MOCK: &lt;b>ok&lt;/b></pre>',
127 [],
128 [ 'name' => 'mockfm', 'status' => 400 ]
129 ],
130 'wrapped HTML format, with set status' => [
131 [],
132 // phpcs:ignore Generic.Files.LineLength.TooLong
133 '{"status":400,"statustext":"Bad Request","html":"<pre class=\"api-pretty-content\">Format MOCK: &lt;b>ok&lt;/b></pre>","modules":["mediawiki.apipretty"],"continue":null,"time":1234}',
134 [ 'wrappedhtml' => 1 ],
135 [ 'name' => 'mockfm', 'status' => 400 ]
136 ],
137 'wrapped HTML format, cross-domain-policy' => [
138 [ 'continue' => '< CrOsS-DoMaIn-PoLiCy >' ],
139 // phpcs:ignore Generic.Files.LineLength.TooLong
140 '{"status":200,"statustext":"OK","html":"<pre class=\"api-pretty-content\">Format MOCK: &lt;b>ok&lt;/b></pre>","modules":["mediawiki.apipretty"],"continue":"\u003C CrOsS-DoMaIn-PoLiCy \u003E","time":1234}',
141 [ 'wrappedhtml' => 1 ],
142 [ 'name' => 'mockfm' ]
143 ],
144 ];
145 }
146
147 public function testBasics() {
148 $printer = $this->getMockFormatter( null, 'mock' );
149 $this->assertTrue( $printer->canPrintErrors() );
150 $this->assertSame(
151 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Data_formats',
152 $printer->getHelpUrls()
153 );
154 }
155
156 public function testDisable() {
157 $this->setMwGlobals( [
158 'wgApiFrameOptions' => 'DENY',
159 ] );
160
161 $printer = $this->getMockFormatter( null, 'mock' );
162 $printer->method( 'execute' )->willReturnCallback( function () use ( $printer ) {
163 $printer->printText( 'Foo' );
164 } );
165 $this->assertFalse( $printer->isDisabled() );
166 $printer->disable();
167 $this->assertTrue( $printer->isDisabled() );
168
169 $printer->setHttpStatus( 400 );
170 $printer->initPrinter();
171 $printer->execute();
172 ob_start();
173 $printer->closePrinter();
174 $this->assertSame( '', ob_get_clean() );
175 $response = $printer->getMain()->getRequest()->response();
176 $this->assertNull( $response->getHeader( 'Content-Type' ) );
177 $this->assertNull( $response->getHeader( 'X-Frame-Options' ) );
178 $this->assertNull( $response->getHeader( 'Content-Disposition' ) );
179 $this->assertNull( $response->getStatusCode() );
180 }
181
182 public function testNullMimeType() {
183 $this->setMwGlobals( [
184 'wgApiFrameOptions' => 'DENY',
185 ] );
186
187 $printer = $this->getMockFormatter( null, 'mock', [ 'getMimeType' ] );
188 $printer->method( 'execute' )->willReturnCallback( function () use ( $printer ) {
189 $printer->printText( 'Foo' );
190 } );
191 $printer->method( 'getMimeType' )->willReturn( null );
192 $this->assertNull( $printer->getMimeType(), 'sanity check' );
193
194 $printer->initPrinter();
195 $printer->execute();
196 ob_start();
197 $printer->closePrinter();
198 $this->assertSame( 'Foo', ob_get_clean() );
199 $response = $printer->getMain()->getRequest()->response();
200 $this->assertNull( $response->getHeader( 'Content-Type' ) );
201 $this->assertNull( $response->getHeader( 'X-Frame-Options' ) );
202 $this->assertNull( $response->getHeader( 'Content-Disposition' ) );
203
204 $printer = $this->getMockFormatter( null, 'mockfm', [ 'getMimeType' ] );
205 $printer->method( 'execute' )->willReturnCallback( function () use ( $printer ) {
206 $printer->printText( 'Foo' );
207 } );
208 $printer->method( 'getMimeType' )->willReturn( null );
209 $this->assertNull( $printer->getMimeType(), 'sanity check' );
210 $this->assertTrue( $printer->getIsHtml(), 'sanity check' );
211
212 $printer->initPrinter();
213 $printer->execute();
214 ob_start();
215 $printer->closePrinter();
216 $this->assertSame( 'Foo', ob_get_clean() );
217 $response = $printer->getMain()->getRequest()->response();
218 $this->assertSame(
219 'text/html; charset=utf-8', strtolower( $response->getHeader( 'Content-Type' ) )
220 );
221 $this->assertSame( 'DENY', $response->getHeader( 'X-Frame-Options' ) );
222 $this->assertSame(
223 'inline; filename="api-result.html"', $response->getHeader( 'Content-Disposition' )
224 );
225 }
226
227 public function testApiFrameOptions() {
228 $this->setMwGlobals( [ 'wgApiFrameOptions' => 'DENY' ] );
229 $printer = $this->getMockFormatter( null, 'mock' );
230 $printer->initPrinter();
231 $this->assertSame(
232 'DENY',
233 $printer->getMain()->getRequest()->response()->getHeader( 'X-Frame-Options' )
234 );
235
236 $this->setMwGlobals( [ 'wgApiFrameOptions' => 'SAMEORIGIN' ] );
237 $printer = $this->getMockFormatter( null, 'mock' );
238 $printer->initPrinter();
239 $this->assertSame(
240 'SAMEORIGIN',
241 $printer->getMain()->getRequest()->response()->getHeader( 'X-Frame-Options' )
242 );
243
244 $this->setMwGlobals( [ 'wgApiFrameOptions' => false ] );
245 $printer = $this->getMockFormatter( null, 'mock' );
246 $printer->initPrinter();
247 $this->assertNull(
248 $printer->getMain()->getRequest()->response()->getHeader( 'X-Frame-Options' )
249 );
250 }
251
252 public function testForceDefaultParams() {
253 $context = new RequestContext;
254 $context->setRequest( new FauxRequest( [ 'foo' => '1', 'bar' => '2', 'baz' => '3' ], true ) );
255 $main = new ApiMain( $context );
256 $allowedParams = [
257 'foo' => [],
258 'bar' => [ ApiBase::PARAM_DFLT => 'bar?' ],
259 'baz' => 'baz!',
260 ];
261
262 $printer = $this->getMockFormatter( $main, 'mock', [ 'getAllowedParams' ] );
263 $printer->method( 'getAllowedParams' )->willReturn( $allowedParams );
264 $this->assertEquals(
265 [ 'foo' => '1', 'bar' => '2', 'baz' => '3' ],
266 $printer->extractRequestParams(),
267 'sanity check'
268 );
269
270 $printer = $this->getMockFormatter( $main, 'mock', [ 'getAllowedParams' ] );
271 $printer->method( 'getAllowedParams' )->willReturn( $allowedParams );
272 $printer->forceDefaultParams();
273 $this->assertEquals(
274 [ 'foo' => null, 'bar' => 'bar?', 'baz' => 'baz!' ],
275 $printer->extractRequestParams()
276 );
277 }
278
279 public function testGetAllowedParams() {
280 $printer = $this->getMockFormatter( null, 'mock' );
281 $this->assertSame( [], $printer->getAllowedParams() );
282
283 $printer = $this->getMockFormatter( null, 'mockfm' );
284 $this->assertSame( [
285 'wrappedhtml' => [
286 ApiBase::PARAM_DFLT => false,
287 ApiBase::PARAM_HELP_MSG => 'apihelp-format-param-wrappedhtml',
288 ]
289 ], $printer->getAllowedParams() );
290 }
291
292 public function testGetExamplesMessages() {
293 $printer = TestingAccessWrapper::newFromObject( $this->getMockFormatter( null, 'mock' ) );
294 $this->assertSame( [
295 'action=query&meta=siteinfo&siprop=namespaces&format=mock'
296 => [ 'apihelp-format-example-generic', 'MOCK' ]
297 ], $printer->getExamplesMessages() );
298
299 $printer = TestingAccessWrapper::newFromObject( $this->getMockFormatter( null, 'mockfm' ) );
300 $this->assertSame( [
301 'action=query&meta=siteinfo&siprop=namespaces&format=mockfm'
302 => [ 'apihelp-format-example-generic', 'MOCK' ]
303 ], $printer->getExamplesMessages() );
304 }
305
306 /**
307 * @dataProvider provideHtmlHeader
308 */
309 public function testHtmlHeader( $post, $registerNonHtml, $expect ) {
310 $context = new RequestContext;
311 $request = new FauxRequest( [ 'a' => 1, 'b' => 2 ], $post );
312 $request->setRequestURL( 'http://example.org/wx/api.php' );
313 $context->setRequest( $request );
314 $context->setLanguage( 'qqx' );
315 $main = new ApiMain( $context );
316 $printer = $this->getMockFormatter( $main, 'mockfm' );
317 $mm = $printer->getMain()->getModuleManager();
318 $mm->addModule( 'mockfm', 'format', ApiFormatBase::class, function () {
319 return $mock;
320 } );
321 if ( $registerNonHtml ) {
322 $mm->addModule( 'mock', 'format', ApiFormatBase::class, function () {
323 return $mock;
324 } );
325 }
326
327 $printer->initPrinter();
328 $printer->execute();
329 ob_start();
330 $printer->closePrinter();
331 $text = ob_get_clean();
332 $this->assertContains( $expect, $text );
333 }
334
335 public static function provideHtmlHeader() {
336 return [
337 [ false, false, '(api-format-prettyprint-header-only-html: MOCK)' ],
338 [ true, false, '(api-format-prettyprint-header-only-html: MOCK)' ],
339 // phpcs:ignore Generic.Files.LineLength.TooLong
340 [ false, true, '(api-format-prettyprint-header-hyperlinked: MOCK, mock, <a rel="nofollow" class="external free" href="http://example.org/wx/api.php?a=1&amp;b=2&amp;format=mock">http://example.org/wx/api.php?a=1&amp;b=2&amp;format=mock</a>)' ],
341 [ true, true, '(api-format-prettyprint-header: MOCK, mock)' ],
342 ];
343 }
344
345 }