3 use MediaWiki\LinkedData\PageDataRequestHandler
;
4 use MediaWiki\Output\OutputPage
;
5 use MediaWiki\Request\FauxRequest
;
6 use MediaWiki\Request\FauxResponse
;
7 use MediaWiki\Specials\SpecialPageData
;
10 * @covers \MediaWiki\Specials\SpecialPageData
14 * @author Daniel Kinzler
16 class SpecialPageDataTest
extends SpecialPageTestBase
{
18 protected function setUp(): void
{
21 $this->setContentLang( 'qqx' );
24 protected function newSpecialPage() {
25 $page = new SpecialPageData();
27 // why is this needed?
28 $page->getContext()->setOutput( new OutputPage( $page->getContext() ) );
30 $page->setRequestHandler( new PageDataRequestHandler() );
35 public static function provideExecute() {
38 $cases['Empty request'] = [ '', [], [], '!!', 200 ];
40 $cases['Only title specified'] = [
42 [ 'target' => 'Helsinki' ],
46 [ 'Location' => '!.+!' ]
49 $cases['Accept only HTML'] = [
51 [ 'target' => 'Helsinki' ],
52 [ 'Accept' => 'text/HTML' ],
55 [ 'Location' => '!Helsinki$!' ]
58 $cases['Accept only HTML with revid'] = [
61 'target' => 'Helsinki',
64 [ 'Accept' => 'text/HTML' ],
67 [ 'Location' => '!Helsinki(\?|&)oldid=4242!' ]
70 $cases['Nothing specified'] = [
76 [ 'Location' => '!Helsinki&action=raw!' ]
79 $cases['Nothing specified 2'] = [
85 [ 'Location' => '!Helsinki&action=raw!' ]
88 $cases['Invalid Accept header'] = [
91 [ 'Accept' => 'text/foobar' ],
101 * @dataProvider provideExecute
103 * @param string $subpage The subpage to request (or '')
104 * @param array $params Request parameters
105 * @param array $headers Request headers
106 * @param string $expRegExp Regex to match the output against.
107 * @param int $expCode Expected HTTP status code
108 * @param array $expHeaders Expected HTTP response headers
110 public function testExecute(
116 array $expHeaders = []
118 $request = new FauxRequest( $params );
119 $request->response()->header( 'Status: 200 OK', true, 200 ); // init/reset
121 foreach ( $headers as $name => $value ) {
122 $request->setHeader( strtoupper( $name ), $value );
126 /** @var FauxResponse $response */
127 [ $output, $response ] = $this->executeSpecialPage( $subpage, $request );
129 $this->assertEquals( $expCode, $response->getStatusCode(), "status code" );
130 $this->assertMatchesRegularExpression( $expRegExp, $output, "output" );
132 foreach ( $expHeaders as $name => $exp ) {
133 $value = $response->getHeader( $name );
134 $this->assertNotNull( $value, "header: $name" );
135 $this->assertIsString( $value, "header: $name" );
136 $this->assertMatchesRegularExpression( $exp, $value, "header: $name" );
138 } catch ( HttpError
$e ) {
139 $this->assertEquals( $expCode, $e->getStatusCode(), "status code" );
140 $this->assertMatchesRegularExpression( $expRegExp, $e->getHTML(), "error output" );
144 public function testSpecialPageWithoutParameters() {
145 $request = new FauxRequest();
146 $request->response()->header( 'Status: 200 OK', true, 200 ); // init/reset
148 [ $output, ] = $this->executeSpecialPage( '', $request );
150 $this->assertStringContainsString( '(pagedata-text)', $output );