1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
<?php
namespace MediaWiki\Tests\Rest\Handler;
use BagOStuff;
use DeferredUpdates;
use EmptyBagOStuff;
use Exception;
use ExtensionRegistry;
use HashBagOStuff;
use HashConfig;
use MediaWiki\Json\JsonCodec;
use MediaWiki\Parser\ParserCacheFactory;
use MediaWiki\Rest\Handler\PageHTMLHandler;
use MediaWiki\Rest\LocalizedHttpException;
use MediaWiki\Rest\RequestData;
use MediaWikiIntegrationTestCase;
use MWTimestamp;
use NullStatsdDataFactory;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\NullLogger;
use Wikimedia\Message\MessageValue;
use Wikimedia\Parsoid\Core\ClientError;
use Wikimedia\Parsoid\Core\PageBundle;
use Wikimedia\Parsoid\Core\ResourceLimitExceededException;
use Wikimedia\Parsoid\Parsoid;
use Wikimedia\TestingAccessWrapper;
use WikiPage;
/**
* @covers \MediaWiki\Rest\Handler\PageHTMLHandler
* @group Database
*/
class PageHTMLHandlerTest extends MediaWikiIntegrationTestCase {
use HandlerTestTrait;
private const WIKITEXT = 'Hello \'\'\'World\'\'\'';
private const HTML = '<p>Hello <b>World</b></p>';
protected function setUp(): void {
parent::setUp();
// Clean up these tables after each test
$this->tablesUsed = [
'page',
'revision',
'comment',
'text',
'content'
];
}
/**
* Checks whether Parsoid extension is installed and skips the test if it's not.
*/
private function checkParsoidInstalled() {
if ( !ExtensionRegistry::getInstance()->isLoaded( 'Parsoid' ) ) {
$this->markTestSkipped( 'Skip test, since parsoid is not configured' );
}
}
/**
* @param BagOStuff|null $cache
* @param Parsoid|MockObject|null $parsoid
* @return PageHTMLHandler
* @throws Exception
*/
private function newHandler( BagOStuff $cache = null, Parsoid $parsoid = null ): PageHTMLHandler {
$handler = new PageHTMLHandler(
new HashConfig( [
'RightsUrl' => 'https://example.com/rights',
'RightsText' => 'some rights',
] ),
$this->getServiceContainer()->getPermissionManager(),
$this->getServiceContainer()->getRevisionLookup(),
$this->getServiceContainer()->getTitleFormatter(),
$this->getServiceContainer()->getTitleFactory(),
new ParserCacheFactory(
$cache ?: new EmptyBagOStuff(),
"1234567",
$this->createHookContainer(),
new JsonCodec(),
new NullStatsdDataFactory(),
new NullLogger()
),
$this->getServiceContainer()->getWikiPageFactory()
);
if ( $parsoid !== null ) {
$handlerWrapper = TestingAccessWrapper::newFromObject( $handler );
$helperWrapper = TestingAccessWrapper::newFromObject( $handlerWrapper->htmlHelper );
$helperWrapper->parsoid = $parsoid;
}
return $handler;
}
public function testExecuteWithHtml() {
$this->checkParsoidInstalled();
$page = $this->getExistingTestPage( 'HtmlEndpointTestPage/with/slashes' );
$this->assertTrue(
$this->editPage( $page, self::WIKITEXT )->isGood(),
'Sanity: edited a page'
);
$request = new RequestData(
[ 'pathParams' => [ 'title' => $page->getTitle()->getPrefixedText() ] ]
);
$handler = $this->newHandler();
$data = $this->executeHandlerAndGetBodyData( $handler, $request, [
'format' => 'with_html'
] );
$this->assertResponseData( $page, $data );
$this->assertStringContainsString( '<!DOCTYPE html>', $data['html'] );
$this->assertStringContainsString( '<html', $data['html'] );
$this->assertStringContainsString( self::HTML, $data['html'] );
}
public function testExecuteHtmlOnly() {
$this->checkParsoidInstalled();
$page = $this->getExistingTestPage( 'HtmlEndpointTestPage/with/slashes' );
$this->assertTrue(
$this->editPage( $page, self::WIKITEXT )->isGood(),
'Sanity: edited a page'
);
$request = new RequestData(
[ 'pathParams' => [ 'title' => $page->getTitle()->getPrefixedText() ] ]
);
$handler = $this->newHandler();
$response = $this->executeHandler( $handler, $request, [
'format' => 'html'
] );
$htmlResponse = (string)$response->getBody();
$this->assertStringContainsString( '<!DOCTYPE html>', $htmlResponse );
$this->assertStringContainsString( '<html', $htmlResponse );
$this->assertStringContainsString( self::HTML, $htmlResponse );
}
public function testHtmlIsCached() {
$this->checkParsoidInstalled();
$page = $this->getExistingTestPage( 'HtmlEndpointTestPage/with/slashes' );
$request = new RequestData(
[ 'pathParams' => [ 'title' => $page->getTitle()->getPrefixedText() ] ]
);
$cache = new HashBagOStuff();
$parsoid = $this->createNoOpMock( Parsoid::class, [ 'wikitext2html' ] );
$parsoid->expects( $this->once() )
->method( 'wikitext2html' )
->willReturn( new PageBundle( 'mocked HTML', null, null, '1.0' ) );
$handler = $this->newHandler( $cache, $parsoid );
$response = $this->executeHandler( $handler, $request, [
'format' => 'html'
] );
$htmlResponse = (string)$response->getBody();
$this->assertStringContainsString( 'mocked HTML', $htmlResponse );
// check that we can run the test again and ensure that the parse is only run once
$handler = $this->newHandler( $cache, $parsoid );
$response = $this->executeHandler( $handler, $request, [
'format' => 'html'
] );
$htmlResponse = (string)$response->getBody();
$this->assertStringContainsString( 'mocked HTML', $htmlResponse );
}
public function testEtagLastModified() {
$this->checkParsoidInstalled();
$time = time();
MWTimestamp::setFakeTime( $time );
$page = $this->getExistingTestPage( 'HtmlEndpointTestPage/with/slashes' );
$request = new RequestData(
[ 'pathParams' => [ 'title' => $page->getTitle()->getPrefixedText() ] ]
);
$cache = new HashBagOStuff();
// First, test it works if nothing was cached yet.
// Make some time pass since page was created:
MWTimestamp::setFakeTime( $time + 10 );
$handler = $this->newHandler( $cache );
$response = $this->executeHandler( $handler, $request, [
'format' => 'html'
] );
$this->assertArrayHasKey( 'ETag', $response->getHeaders() );
$etag = $response->getHeaderLine( 'ETag' );
$this->assertArrayHasKey( 'Last-Modified', $response->getHeaders() );
$this->assertSame( MWTimestamp::convert( TS_RFC2822, $time ),
$response->getHeaderLine( 'Last-Modified' ) );
// Now, test that headers work when getting from cache too.
$handler = $this->newHandler( $cache );
$response = $this->executeHandler( $handler, $request, [
'format' => 'html'
] );
$this->assertArrayHasKey( 'ETag', $response->getHeaders() );
$this->assertNotSame( $etag, $response->getHeaderLine( 'ETag' ) );
$etag = $response->getHeaderLine( 'ETag' );
$this->assertArrayHasKey( 'Last-Modified', $response->getHeaders() );
$this->assertSame( MWTimestamp::convert( TS_RFC2822, $time + 10 ),
$response->getHeaderLine( 'Last-Modified' ) );
// Now, expire the cache
$time += 1000;
MWTimestamp::setFakeTime( $time );
$this->assertTrue(
$page->getTitle()->invalidateCache( MWTimestamp::convert( TS_MW, $time ) ),
'Sanity: can invalidate cache'
);
DeferredUpdates::doUpdates();
$handler = $this->newHandler( $cache );
$response = $this->executeHandler( $handler, $request, [
'format' => 'html'
] );
$this->assertArrayHasKey( 'ETag', $response->getHeaders() );
$this->assertNotSame( $etag, $response->getHeaderLine( 'ETag' ) );
$this->assertArrayHasKey( 'Last-Modified', $response->getHeaders() );
$this->assertSame( MWTimestamp::convert( TS_RFC2822, $time ),
$response->getHeaderLine( 'Last-Modified' ) );
}
public function provideHandlesParsoidError() {
yield 'ClientError' => [
new ClientError( 'TEST_TEST' ),
new LocalizedHttpException(
new MessageValue( 'rest-html-backend-error' ),
400,
[
'reason' => 'TEST_TEST'
]
)
];
yield 'ResourceLimitExceededException' => [
new ResourceLimitExceededException( 'TEST_TEST' ),
new LocalizedHttpException(
new MessageValue( 'rest-resource-limit-exceeded' ),
413,
[
'reason' => 'TEST_TEST'
]
)
];
}
/**
* @dataProvider provideHandlesParsoidError
* @param Exception $parsoidException
* @param Exception $expectedException
*/
public function testHandlesParsoidError(
Exception $parsoidException,
Exception $expectedException
) {
$this->checkParsoidInstalled();
$page = $this->getExistingTestPage( 'HtmlEndpointTestPage/with/slashes' );
$request = new RequestData(
[ 'pathParams' => [ 'title' => $page->getTitle()->getPrefixedText() ] ]
);
$parsoid = $this->createNoOpMock( Parsoid::class, [ 'wikitext2html' ] );
$parsoid->expects( $this->once() )
->method( 'wikitext2html' )
->willThrowException( $parsoidException );
$handler = $this->newHandler( null, $parsoid );
$this->expectExceptionObject( $expectedException );
$this->executeHandler( $handler, $request, [
'format' => 'html'
] );
}
public function testExecute_missingparam() {
$request = new RequestData();
$this->expectExceptionObject(
new LocalizedHttpException(
new MessageValue( "paramvalidator-missingparam", [ 'title' ] ),
400
)
);
$handler = $this->newHandler();
$this->executeHandler( $handler, $request );
}
public function testExecute_error() {
$request = new RequestData( [ 'pathParams' => [ 'title' => 'DoesNotExist8237456assda1234' ] ] );
$this->expectExceptionObject(
new LocalizedHttpException(
new MessageValue( "rest-nonexistent-title", [ 'testing' ] ),
404
)
);
$handler = $this->newHandler();
$this->executeHandler( $handler, $request );
}
/**
* @param WikiPage $page
* @param array $data
*/
private function assertResponseData( WikiPage $page, array $data ): void {
$this->assertSame( $page->getId(), $data['id'] );
$this->assertSame( $page->getTitle()->getPrefixedDBkey(), $data['key'] );
$this->assertSame( $page->getTitle()->getPrefixedText(), $data['title'] );
$this->assertSame( $page->getLatest(), $data['latest']['id'] );
$this->assertSame(
wfTimestampOrNull( TS_ISO_8601, $page->getTimestamp() ),
$data['latest']['timestamp']
);
$this->assertSame( CONTENT_MODEL_WIKITEXT, $data['content_model'] );
$this->assertSame( 'https://example.com/rights', $data['license']['url'] );
$this->assertSame( 'some rights', $data['license']['title'] );
}
}
|