Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / specials / SpecialPageDataTest.php
blobe2f6371a3783e28af6705e5482c8eee24ada779d
1 <?php
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;
9 /**
10 * @covers \MediaWiki\Specials\SpecialPageData
11 * @group Database
12 * @group SpecialPage
14 * @author Daniel Kinzler
16 class SpecialPageDataTest extends SpecialPageTestBase {
18 protected function setUp(): void {
19 parent::setUp();
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() );
32 return $page;
35 public static function provideExecute() {
36 $cases = [];
38 $cases['Empty request'] = [ '', [], [], '!!', 200 ];
40 $cases['Only title specified'] = [
41 '',
42 [ 'target' => 'Helsinki' ],
43 [],
44 '!!',
45 303,
46 [ 'Location' => '!.+!' ]
49 $cases['Accept only HTML'] = [
50 '',
51 [ 'target' => 'Helsinki' ],
52 [ 'Accept' => 'text/HTML' ],
53 '!!',
54 303,
55 [ 'Location' => '!Helsinki$!' ]
58 $cases['Accept only HTML with revid'] = [
59 '',
61 'target' => 'Helsinki',
62 'revision' => '4242',
64 [ 'Accept' => 'text/HTML' ],
65 '!!',
66 303,
67 [ 'Location' => '!Helsinki(\?|&)oldid=4242!' ]
70 $cases['Nothing specified'] = [
71 'main/Helsinki',
72 [],
73 [],
74 '!!',
75 303,
76 [ 'Location' => '!Helsinki&action=raw!' ]
79 $cases['Nothing specified 2'] = [
80 '/Helsinki',
81 [],
82 [],
83 '!!',
84 303,
85 [ 'Location' => '!Helsinki&action=raw!' ]
88 $cases['Invalid Accept header'] = [
89 'main/Helsinki',
90 [],
91 [ 'Accept' => 'text/foobar' ],
92 '!!',
93 406,
94 [],
97 return $cases;
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(
111 $subpage,
112 array $params,
113 array $headers,
114 $expRegExp,
115 $expCode = 200,
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 );
125 try {
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 );