Debian Codegraph @

tests/phpunit/includes/parser/ParserCacheSerializationTestCases.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Parser;
4
5 use JsonSerializable;
6 use MediaWiki\Debug\MWDebug;
7 use MediaWiki\Json\JsonCodec;
8 use MediaWiki\Parser\CacheTime;
9 use MediaWiki\Parser\ParserOutput;
10 use MediaWiki\Parser\ParserOutputFlags;
11 use MediaWiki\Tests\Json\JsonDeserializableSubClass;
12 use MediaWiki\Title\Title;
13 use MediaWiki\Title\TitleValue;
14 use MediaWiki\Utils\MWTimestamp;
15 use MediaWikiIntegrationTestCase;
16 use stdClass;
17 use Wikimedia\JsonCodec\JsonCodecable;
18 use Wikimedia\TestingAccessWrapper;
19 use Wikimedia\Tests\SerializationTestUtils;
20
21 /**
22 * A collection of serialization test cases for parser cache.
23 *
24 * Contains a set of acceptance tests for CacheTime and ParserOutput objects.
25 * The acceptance tests will be run on instances created by the current code,
26 * as well as instances deserialized from stored serializations for various MW
27 * versions.
28 *
29 * Since backwards compatibility for objects stored in ParserCache is necessary,
30 * failure of a deserialization test most likely indicates an error in the code.
31 * However, since the serialization format may change, thus if proper compatibility
32 * logic was added but a serialization test is still failing, you might want to
33 * update stored serialized data using validateParserCacheSerializationTestData.php
34 * script. The same script should be run when more acceptance tests are added
35 * to generate and save serialized object, which would be used for acceptance
36 * deserialization tests.
37 *
38 * See:
39 * https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility
40 *
41 * @see SerializationTestTrait
42 * @see SerializationTestUtils
43 * @see ValidateParserCacheSerializationTestData
44 */
45 abstract class ParserCacheSerializationTestCases {
46
47 public const FAKE_TIME = 123456789;
48
49 public const FAKE_CACHE_EXPIRY = 42;
50
51 private const MOCK_EXT_DATA = [
52 'boolean' => true,
53 'null' => null,
54 'number' => 42,
55 'string' => 'string',
56 'array' => [ 1, 2, 3 ],
57 'map' => [ 'key' => 'value' ]
58 ];
59
60 private const MOCK_FALSY_PROPERTIES = [
61 'boolean' => false,
62 'null' => null,
63 'number' => 0,
64 'string' => '',
65 'numstring' => '0',
66 'array' => [],
67 ];
68
69 private const MOCK_BINARY_PROPERTIES = [
70 'empty' => '',
71 '\x00' => "\x00",
72 'gzip' => "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\xcb\x48\xcd\xc9\xc9\x57\x28\xcf\x2f'
73 . '\xca\x49\x01\x00\x85\x11\x4a\x0d\x0b\x00\x00\x00",
74 ];
75
76 private const SECTIONS = [
77 [
78 'toclevel' => 0,
79 'line' => 'heading_1',
80 'level' => 1,
81 'number' => '1.0',
82 'index' => 'T-1',
83 'fromtitle' => '',
84 'byteoffset' => null,
85 'anchor' => 'heading_1',
86 'linkAnchor' => '#heading_1',
87 ],
88 [
89 'toclevel' => 1,
90 'line' => 'heading_2',
91 'level' => 2,
92 'number' => '2.0',
93 'index' => 'T-2',
94 'fromtitle' => '',
95 'byteoffset' => null,
96 'anchor' => 'heading_2',
97 'linkAnchor' => '#heading_2'
98 ],
99 ];
100
101 private const CACHE_TIME = '20010419042521';
102
103 /**
104 * Get acceptance test cases for CacheTime class.
105 * @see SerializationTestTrait::getTestInstancesAndAssertions()
106 * @return array[]
107 */
108 public static function getCacheTimeTestCases(): array {
109 $cacheTimeWithUsedOptions = new CacheTime();
110 $cacheTimeWithUsedOptions->recordOptions( [ 'optA', 'optX' ] );
111
112 $cacheTimeWithTime = new CacheTime();
113 $cacheTimeWithTime->setCacheTime( self::CACHE_TIME );
114
115 $cacheExpiry = 10;
116 $cacheTimeWithExpiry = new CacheTime();
117 $cacheTimeWithExpiry->updateCacheExpiry( $cacheExpiry );
118
119 $cacheRevisionId = 1234;
120 $cacheTimeWithRevId = new CacheTime();
121 $cacheTimeWithRevId->setCacheRevisionId( $cacheRevisionId );
122
123 return [
124 'empty' => [
125 'instance' => new CacheTime(),
126 'assertions' => static function ( MediaWikiIntegrationTestCase $testCase, CacheTime $object ) {
127 $testCase->assertSame( self::FAKE_CACHE_EXPIRY, $object->getCacheExpiry() );
128 $testCase->assertNull( $object->getCacheRevisionId() );
129 $testCase->assertSame(
130 MWTimestamp::convert( TS_MW, self::FAKE_TIME ),
131 $object->getCacheTime()
132 );
133 // When the cacheRevisionId is not set, this method always returns true.
134 $testCase->assertFalse( $object->isDifferentRevision( 29 ) );
135 $testCase->assertFalse( $object->isDifferentRevision( 31 ) );
136 $testCase->assertTrue( $object->isCacheable() );
137 $testCase->assertSame(
138 $object->getCacheTime(),
139 MWTimestamp::convert( TS_MW, self::FAKE_TIME )
140 );
141 }
142 ],
143 'usedOptions' => [
144 'instance' => $cacheTimeWithUsedOptions,
145 'assertions' => static function ( MediaWikiIntegrationTestCase $testCase, CacheTime $object ) {
146 $testCase->assertArrayEquals( [ 'optA', 'optX' ], $object->getUsedOptions() );
147 }
148 ],
149 'cacheTime' => [
150 'instance' => $cacheTimeWithTime,
151 'assertions' => static function (
152 MediaWikiIntegrationTestCase $testCase, CacheTime $object
153 ) {
154 $testCase->assertSame( self::CACHE_TIME, $object->getCacheTime() );
155 }
156 ],
157 'cacheExpiry' => [
158 'instance' => $cacheTimeWithExpiry,
159 'assertions' => static function (
160 MediaWikiIntegrationTestCase $testCase, CacheTime $object
161 ) use ( $cacheExpiry ) {
162 $testCase->assertSame( $cacheExpiry, $object->getCacheExpiry() );
163 }
164 ],
165 'cacheRevisionId' => [
166 'instance' => $cacheTimeWithRevId,
167 'assertions' => static function (
168 MediaWikiIntegrationTestCase $testCase, CacheTime $object
169 ) use ( $cacheRevisionId ) {
170 $testCase->assertSame( $cacheRevisionId, $object->getCacheRevisionId() );
171 }
172 ]
173 ];
174 }
175
176 /**
177 * This matches PagePropsTable::encodeValue() and represents how
178 * page properties are actually stored in the database.
179 * @param mixed $v
180 * @return int|float|string
181 */
182 public static function pagePropEncode( $v ) {
183 if ( is_int( $v ) || is_float( $v ) || is_string( $v ) ) {
184 return $v;
185 }
186 if ( is_bool( $v ) ) {
187 return (int)$v;
188 }
189 if ( $v === null ) {
190 return '';
191 }
192 if ( is_array( $v ) ) {
193 // strval on an array yield 'Array' plus a warning.
194 return 'Array';
195 }
196 return strval( $v );
197 }
198
199 /**
200 * Assert equality in terms of semantically equivalent page property
201 * values.
202 * @param MediaWikiIntegrationTestCase $testCase
203 * @param mixed $expected
204 * @param mixed $actual
205 */
206 public static function assertPagePropSame( MediaWikiIntegrationTestCase $testCase, $expected, $actual ): void {
207 $testCase->assertSame(
208 self::pagePropEncode( $expected ),
209 $actual
210 );
211 }
212
213 /**
214 * Get acceptance test cases for ParserOutput class.
215 * @see SerializationTestTrait::getTestInstancesAndAssertions()
216 * @return array[]
217 */
218 public static function getParserOutputTestCases() {
219 MWDebug::filterDeprecationForTest( '/::setPageProperty with (non-scalar|non-string|null) value/' );
220 MWDebug::filterDeprecationForTest( '/::addLanguageLink without prefix/' );
221 $parserOutputWithCacheTimeProps = new ParserOutput( 'CacheTime' );
222 $parserOutputWithCacheTimeProps->setCacheTime( self::CACHE_TIME );
223 $parserOutputWithCacheTimeProps->updateCacheExpiry( 10 );
224 $parserOutputWithCacheTimeProps->setCacheRevisionId( 42 );
225
226 $parserOutputWithUsedOptions = new ParserOutput( 'Dummy' );
227 $parserOutputWithUsedOptions->recordOption( 'optA' );
228 $parserOutputWithUsedOptions->recordOption( 'optX' );
229
230 $parserOutputWithExtensionData = new ParserOutput( '' );
231 foreach ( self::MOCK_EXT_DATA as $key => $value ) {
232 $parserOutputWithExtensionData->setExtensionData( $key, $value );
233 }
234
235 $parserOutputWithCodecableExtensionData = new ParserOutput( '' );
236 $parserOutputWithCodecableExtensionData->setExtensionData(
237 'map',
238 [
239 'a' => new JsonDeserializableSubClass( 'super', 'sub' ),
240 'b' => (object)[ 'r' => 2, 'd' => '2' ],
241 ]
242 );
243
244 $parserOutputWithProperties = new ParserOutput( '' );
245 foreach ( self::MOCK_EXT_DATA as $key => $value ) {
246 $parserOutputWithProperties->setPageProperty( $key, $value );
247 }
248
249 $parserOutputWithProperties1_45 = new ParserOutput( '' );
250 foreach ( self::MOCK_EXT_DATA as $key => $value ) {
251 $value = self::pagePropEncode( $value );
252 $parserOutputWithProperties1_45->setPageProperty( $key, $value );
253 }
254
255 $parserOutputWithFalsyProperties = new ParserOutput( '' );
256 foreach ( self::MOCK_FALSY_PROPERTIES as $key => $value ) {
257 $parserOutputWithFalsyProperties->setPageProperty( $key, $value );
258 }
259
260 $parserOutputWithFalsyProperties1_45 = new ParserOutput( '' );
261 foreach ( self::MOCK_FALSY_PROPERTIES as $key => $value ) {
262 $value = self::pagePropEncode( $value );
263 $parserOutputWithFalsyProperties1_45->setPageProperty( $key, $value );
264 }
265
266 $parserOutputWithBinaryProperties = new ParserOutput( '' );
267 foreach ( self::MOCK_BINARY_PROPERTIES as $key => $value ) {
268 $parserOutputWithBinaryProperties->setPageProperty( $key, $value );
269 }
270
271 $parserOutputWithMetadata = new ParserOutput( '' );
272 $parserOutputWithMetadata->setSpeculativeRevIdUsed( 42 );
273 $parserOutputWithMetadata->addLanguageLink( Title::makeTitle( NS_MAIN, 'link1' ) );
274 $parserOutputWithMetadata->addLanguageLink( Title::makeTItle( NS_MAIN, 'link2' ) );
275 $parserOutputWithMetadata->addInterwikiLink( Title::makeTitle( NS_MAIN, 'interwiki1', '', 'enwiki' ) );
276 $parserOutputWithMetadata->addInterwikiLink( Title::makeTitle( NS_MAIN, 'interwiki2', '', 'enwiki' ) );
277 $parserOutputWithMetadata->addCategory( Title::makeTitle( NS_CATEGORY, 'category2' ), '1' );
278 $parserOutputWithMetadata->addCategory( Title::makeTitle( NS_CATEGORY, 'category1' ), '2' );
279 $parserOutputWithMetadata->setIndicator( 'indicator1', 'indicator1_value' );
280 $parserOutputWithMetadata->setTitleText( 'title_text1' );
281 $parserOutputWithMetadata->setSections( self::SECTIONS );
282 $parserOutputWithMetadata->addLink( Title::makeTitle( NS_MAIN, 'Link1' ), 42 );
283 $parserOutputWithMetadata->addLink( Title::makeTitle( NS_USER, 'Link2' ), 43 );
284 $parserOutputWithMetadata->addTemplate(
285 Title::makeTitle( NS_TEMPLATE, 'Template1' ),
286 42,
287 4242
288 );
289 $parserOutputWithMetadata->addImage(
290 new TitleValue( NS_FILE, 'Image1' ),
291 MWTimestamp::convert( TS_MW, 123456789 ),
292 'test_sha1'
293 );
294 $parserOutputWithMetadata->addExternalLink( 'https://test.org' );
295 $parserOutputWithMetadata->addHeadItem( 'head_item1', 'tag1' );
296 $parserOutputWithMetadata->addModules( [ 'module1' ] );
297 $parserOutputWithMetadata->addModuleStyles( [ 'module_style1' ] );
298 $parserOutputWithMetadata->setJsConfigVar( 'key1', 'value1' );
299 $parserOutputWithMetadata->addWarningMsg( 'rawmessage', 'warning1' );
300 $parserOutputWithMetadata->setIndexPolicy( 'noindex' );
301 $parserOutputWithMetadata->setRevisionTimestamp( MWTimestamp::convert( TS_MW, 987654321 ) );
302 $parserOutputWithMetadata->setLimitReportData( 'limit_report_key1', 'value1' );
303 $parserOutputWithMetadata->setEnableOOUI( true );
304 $parserOutputWithMetadata->setHideNewSection( true );
305 $parserOutputWithMetadata->setNewSection( true );
306 $parserOutputWithMetadata->setOutputFlag( 'test' );
307
308 // For compatibility with older serialized objects, clear out the
309 // $mWarningMsgs array, which is not currently stored.
310 // See T343050 for the steps required to remove this workaround in
311 // the future.
312 TestingAccessWrapper::newFromObject(
313 $parserOutputWithMetadata
314 )->mWarningMsgs = [];
315
316 $parserOutputWithMetadataPost1_44 = clone $parserOutputWithMetadata;
317 $parserOutputWithMetadataPost1_44->setLanguageLinks( [ 'm:link1', 'mw:link2' ] );
318
319 $parserOutputWithMetadataPost1_45 = clone $parserOutputWithMetadataPost1_44;
320 // Non-standard output flags were deprecated in MW 1.45
321 $parserOutputWithMetadataPost1_45->setOutputFlag( 'test', false );
322 $parserOutputWithMetadataPost1_45->setOutputFlag( ParserOutputFlags::VARY_REVISION_ID );
323
324 $parserOutputWithSections = new ParserOutput( '' );
325 $parserOutputWithSections->setSections( self::SECTIONS );
326
327 $parserOutputWithMetadataPost1_31 = new ParserOutput( '' );
328 $parserOutputWithMetadataPost1_31->addWrapperDivClass( 'test_wrapper' );
329 $parserOutputWithMetadataPost1_31->setSpeculativePageIdUsed( 4242 );
330 $parserOutputWithMetadataPost1_31->setRevisionTimestampUsed(
331 MWTimestamp::convert( TS_MW, 123456789 )
332 );
333 $parserOutputWithMetadataPost1_31->setRevisionUsedSha1Base36( 'test_hash' );
334 $parserOutputWithMetadataPost1_31->setNoGallery( true );
335
336 $parserOutputWithMetadataPost1_34 = new ParserOutput( '' );
337 $parserOutputWithMetadataPost1_34->addExtraCSPStyleSrc( 'style1' );
338 $parserOutputWithMetadataPost1_34->addExtraCSPDefaultSrc( 'default1' );
339 $parserOutputWithMetadataPost1_34->addExtraCSPScriptSrc( 'script1' );
340 $parserOutputWithMetadataPost1_34->addLink( Title::makeTitle( NS_SPECIAL, 'Link3' ) );
341
342 $parserOutputWithEmptyToC = new ParserOutput( '' );
343 $parserOutputWithEmptyToC->setSections( [] );
344
345 MWDebug::clearDeprecationFilters();
346
347 $testCases = [
348 'empty' => [
349 'instance' => new ParserOutput( '' ),
350 'assertions' => static function ( MediaWikiIntegrationTestCase $testCase, ParserOutput $object ) {
351 // Empty CacheTime assertions
352 self::getCacheTimeTestCases()['empty']['assertions']( $testCase, $object );
353 // Empty string text is counted as having text.
354 $testCase->assertTrue( $object->hasText() );
355
356 $testCase->assertSame( '', $object->getRawText() );
357 $testCase->assertSame( '', $object->getWrapperDivClass() );
358 $testCase->assertNull( $object->getSpeculativeRevIdUsed() );
359 $testCase->assertNull( $object->getSpeculativePageIdUsed() );
360 $testCase->assertNull( $object->getRevisionTimestampUsed() );
361 $testCase->assertNull( $object->getRevisionUsedSha1Base36() );
362 $testCase->assertArrayEquals( [], $object->getLanguageLinks() );
363 $testCase->assertArrayEquals( [], $object->getInterwikiLinks() );
364 $testCase->assertArrayEquals( [], $object->getCategoryNames() );
365 $testCase->assertArrayEquals( [], $object->getCategoryMap() );
366 $testCase->assertArrayEquals( [], $object->getIndicators() );
367 $testCase->assertSame( '', $object->getTitleText() );
368 $testCase->assertArrayEquals( [], $object->getSections() );
369 $testCase->assertNull( $object->getTOCData() );
370 $testCase->assertArrayEquals( [], $object->getLinks() );
371 $testCase->assertArrayEquals( [], $object->getLinksSpecial() );
372 $testCase->assertArrayEquals( [], $object->getTemplates() );
373 $testCase->assertArrayEquals( [], $object->getTemplateIds() );
374 $testCase->assertArrayEquals( [], $object->getImages() );
375 $testCase->assertArrayEquals( [], $object->getFileSearchOptions() );
376 $testCase->assertArrayEquals( [], $object->getExternalLinks() );
377 $testCase->assertFalse( $object->getNoGallery() );
378 $testCase->assertArrayEquals( [], $object->getHeadItems() );
379 $testCase->assertArrayEquals( [], $object->getModules() );
380 $testCase->assertArrayEquals( [], $object->getModuleStyles() );
381 $testCase->assertArrayEquals( [], $object->getJsConfigVars() );
382 $testCase->assertArrayEquals( [], $object->getWarnings() );
383 $testCase->assertSame( '', $object->getIndexPolicy() );
384 $testCase->assertNull( $object->getRevisionTimestamp() );
385 $testCase->assertArrayEquals( [], $object->getLimitReportData() );
386 $testCase->assertArrayEquals( [], $object->getLimitReportJSData() );
387 $testCase->assertFalse( $object->getEnableOOUI() );
388 $testCase->assertArrayEquals( [], $object->getExtraCSPDefaultSrcs() );
389 $testCase->assertArrayEquals( [], $object->getExtraCSPScriptSrcs() );
390 $testCase->assertArrayEquals( [], $object->getExtraCSPStyleSrcs() );
391 $testCase->assertFalse( $object->getHideNewSection() );
392 $testCase->assertFalse( $object->getNewSection() );
393 $testCase->assertFalse( $object->getDisplayTitle() );
394 $testCase->assertFalse( $object->getOutputFlag( 'test' ) );
395 $testCase->assertArrayEquals( [], $object->getAllFlags() );
396 $testCase->assertNull( $object->getPageProperty( 'test_prop' ) );
397 $testCase->assertArrayEquals( [], $object->getPageProperties() );
398 $testCase->assertArrayEquals( [], $object->getUsedOptions() );
399 $testCase->assertNull( $object->getExtensionData( 'test_ext_data' ) );
400 $testCase->assertNull( $object->getTimeProfile( 'wall' ) );
401 }
402 ],
403 'cacheTime' => [
404 'instance' => $parserOutputWithCacheTimeProps,
405 'assertions' => static function ( MediaWikiIntegrationTestCase $testCase, ParserOutput $object ) {
406 $testCase->assertSame( self::CACHE_TIME, $object->getCacheTime() );
407 $testCase->assertSame( 10, $object->getCacheExpiry() );
408 $testCase->assertSame( 42, $object->getCacheRevisionId() );
409 }
410 ],
411 'text' => [
412 'instance' => new ParserOutput( 'Lorem Ipsum' ),
413 'assertions' => static function ( MediaWikiIntegrationTestCase $testCase, ParserOutput $object ) {
414 $testCase->assertTrue( $object->hasText() );
415 $testCase->assertSame( 'Lorem Ipsum', $object->getRawText() );
416 }
417 ],
418 'usedOptions' => [
419 'instance' => $parserOutputWithUsedOptions,
420 'assertions' => static function ( MediaWikiIntegrationTestCase $testCase, ParserOutput $object ) {
421 $testCase->assertArrayEquals( [ 'optA', 'optX' ], $object->getUsedOptions() );
422 }
423 ],
424 'extensionData' => [
425 'instance' => $parserOutputWithExtensionData,
426 'assertions' => static function ( MediaWikiIntegrationTestCase $testCase, ParserOutput $object ) {
427 $testCase->assertSame( self::MOCK_EXT_DATA['boolean'], $object->getExtensionData( 'boolean' ) );
428 $testCase->assertSame( self::MOCK_EXT_DATA['null'], $object->getExtensionData( 'null' ) );
429 $testCase->assertSame( self::MOCK_EXT_DATA['number'], $object->getExtensionData( 'number' ) );
430 $testCase->assertSame( self::MOCK_EXT_DATA['string'], $object->getExtensionData( 'string' ) );
431 $testCase->assertArrayEquals( self::MOCK_EXT_DATA['array'], $object->getExtensionData( 'array' ) );
432 $testCase->assertSame( self::MOCK_EXT_DATA['map'], $object->getExtensionData( 'map' ) );
433 }
434 ],
435 'codecableExtensionData' => [
436 'instance' => $parserOutputWithCodecableExtensionData,
437 'assertions' => static function ( MediaWikiIntegrationTestCase $testCase, ParserOutput $object ) {
438 $actual = $object->getExtensionData( 'map' );
439 $testCase->assertIsArray( $actual );
440 $testCase->assertArrayHasKey( 'a', $actual );
441 $testCase->assertInstanceOf(
442 JsonDeserializableSubClass::class, $actual['a']
443 );
444 $testCase->assertSame( 'super', $actual['a']->getSuperClassField() );
445 $testCase->assertSame( 'sub', $actual['a']->getSubClassField() );
446 $testCase->assertArrayHasKey( 'b', $actual );
447 $testCase->assertInstanceOf(
448 stdClass::class, $actual['b']
449 );
450 $testCase->assertSame(
451 2, $actual['b']->r
452 );
453 $testCase->assertSame(
454 '2', $actual['b']->d
455 );
456 $testCase->assertCount( 2, (array)$actual['b'] );
457 $testCase->assertCount( 2, $actual );
458 }
459 ],
460 'pageProperties' => [
461 'instance' => $parserOutputWithProperties,
462 'assertions' => static function ( MediaWikiIntegrationTestCase $testCase, ParserOutput $object ) {
463 $testCase->assertSame( self::MOCK_EXT_DATA['boolean'], $object->getPageProperty( 'boolean' ) );
464 $testCase->assertSame( self::MOCK_EXT_DATA['null'], $object->getPageProperty( 'null' ) );
465 $testCase->assertSame( self::MOCK_EXT_DATA['number'], $object->getPageProperty( 'number' ) );
466 $testCase->assertSame( self::MOCK_EXT_DATA['string'], $object->getPageProperty( 'string' ) );
467 $testCase->assertArrayEquals( self::MOCK_EXT_DATA['array'], $object->getPageProperty( 'array' ) );
468 $testCase->assertSame( self::MOCK_EXT_DATA['map'], $object->getPageProperty( 'map' ) );
469 $testCase->assertArrayEquals( self::MOCK_EXT_DATA, $object->getPageProperties() );
470 }
471 ],
472 'pageProperties1_45' => [
473 'instance' => $parserOutputWithProperties1_45,
474 'assertions' => static function ( MediaWikiIntegrationTestCase $testCase, ParserOutput $object ) {
475 self::assertPagePropSame( $testCase, self::MOCK_EXT_DATA['boolean'], $object->getPageProperty( 'boolean' ) );
476 self::assertPagePropSame( $testCase, self::MOCK_EXT_DATA['null'], $object->getPageProperty( 'null' ) );
477 self::assertPagePropSame( $testCase, self::MOCK_EXT_DATA['number'], $object->getPageProperty( 'number' ) );
478 self::assertPagePropSame( $testCase, self::MOCK_EXT_DATA['string'], $object->getPageProperty( 'string' ) );
479 self::assertPagePropSame( $testCase, self::MOCK_EXT_DATA['array'], $object->getPageProperty( 'array' ) );
480 self::assertPagePropSame( $testCase, self::MOCK_EXT_DATA['map'], $object->getPageProperty( 'map' ) );
481 $testCase->assertArrayEquals(
482 array_map( fn ( $v )=>self::pagePropEncode( $v ), self::MOCK_EXT_DATA ),
483 array_map( fn ( $v )=>self::pagePropEncode( $v ), $object->getPageProperties() )
484 );
485 }
486 ],
487 'binaryPageProperties' => [
488 'instance' => $parserOutputWithBinaryProperties,
489 'assertions' => static function ( MediaWikiIntegrationTestCase $testCase, ParserOutput $object ) {
490 $testCase->assertSame( self::MOCK_BINARY_PROPERTIES['empty'], $object->getPageProperty( 'empty' ) );
491 $testCase->assertSame( self::MOCK_BINARY_PROPERTIES['\x00'], $object->getPageProperty( '\x00' ) );
492 $testCase->assertSame( self::MOCK_BINARY_PROPERTIES['gzip'], $object->getPageProperty( 'gzip' ) );
493 $testCase->assertArrayEquals( self::MOCK_BINARY_PROPERTIES, $object->getPageProperties() );
494 }
495 ],
496 'withMetadata' => [
497 'instance' => $parserOutputWithMetadata,
498 'assertions' => static function ( MediaWikiIntegrationTestCase $testCase, ParserOutput $object ) {
499 $testCase->assertSame( 42, $object->getSpeculativeRevIdUsed() );
500 $testCase->assertArrayEquals( [ 'link1', 'link2' ], $object->getLanguageLinks() );
501 $testCase->assertArrayEquals( [ 'enwiki' => [
502 'interwiki1' => 1,
503 'interwiki2' => 1
504 ] ], $object->getInterwikiLinks() );
505 $testCase->assertArrayEquals( [ 'category1', 'category2' ], $object->getCategoryNames() );
506 $testCase->assertArrayEquals( [
507 'category1' => '2',
508 'category2' => '1'
509 ], $object->getCategoryMap() );
510 $testCase->assertArrayEquals( [ 'indicator1' => 'indicator1_value' ], $object->getIndicators() );
511 $testCase->assertSame( 'title_text1', $object->getTitleText() );
512 $testCase->assertArrayEquals( self::SECTIONS, $object->getSections() );
513 $testCase->assertNotNull( $object->getTOCData() );
514 $testCase->assertArrayEquals( [
515 NS_MAIN => [ 'Link1' => 42 ],
516 NS_USER => [ 'Link2' => 43 ]
517 ], $object->getLinks() );
518 $testCase->assertArrayEquals( [
519 NS_SPECIAL => [ 'Template1' => 42 ]
520 ], $object->getTemplates() );
521 $testCase->assertArrayEquals( [
522 NS_SPECIAL => [ 'Template1' => 4242 ]
523 ], $object->getTemplateIds() );
524 $testCase->assertArrayEquals( [ 'Image1' => 1 ], $object->getImages() );
525 $testCase->assertArrayEquals( [ 'Image1' => [
526 'time' => MWTimestamp::convert( TS_MW, 123456789 ), 'sha1' => 'test_sha1'
527 ] ], $object->getFileSearchOptions() );
528 $testCase->assertArrayEquals( [ 'https://test.com' => 1 ], $object->getExternalLinks() );
529 $testCase->assertArrayEquals( [ 'tag1' => 'head_item1' ], $object->getHeadItems() );
530 $testCase->assertArrayEquals( [ 'module1' ], $object->getModules() );
531 $testCase->assertArrayEquals( [ 'module_style1' ], $object->getModuleStyles() );
532 $testCase->assertArrayEquals( [ 'key1' => 'value1' ], $object->getJsConfigVars() );
533 $testCase->assertArrayEquals( [ 'warning1' ], $object->getWarnings() );
534 $testCase->assertSame( 'noindex', $object->getIndexPolicy() );
535 $testCase->assertSame( MWTimestamp::convert( TS_MW, 987654321 ), $object->getRevisionTimestamp() );
536 $testCase->assertArrayEquals(
537 [ 'limit_report_key1' => 'value1' ],
538 $object->getLimitReportData()
539 );
540 $testCase->assertArrayEquals(
541 [ 'limit_report_key1' => 'value1' ],
542 $object->getLimitReportJSData()
543 );
544 $testCase->assertTrue( $object->getEnableOOUI() );
545 $testCase->assertTrue( $object->getHideNewSection() );
546 $testCase->assertTrue( $object->getNewSection() );
547 $testCase->assertTrue( $object->getOutputFlag( 'test' ) );
548 $testCase->assertArrayEquals( [ 'test' ], $object->getAllFlags() );
549 }
550 ],
551 'withMetadataPost1_44' => [
552 'instance' => $parserOutputWithMetadataPost1_44,
553 'assertions' => static function ( MediaWikiIntegrationTestCase $testCase, ParserOutput $object ) {
554 $testCase->assertSame( 42, $object->getSpeculativeRevIdUsed() );
555 $testCase->assertArrayEquals( [ 'm:link1', 'mw:link2' ], $object->getLanguageLinks() );
556 $testCase->assertArrayEquals( [ 'enwiki' => [
557 'interwiki1' => 1,
558 'interwiki2' => 1
559 ] ], $object->getInterwikiLinks() );
560 $testCase->assertArrayEquals( [ 'category1', 'category2' ], $object->getCategoryNames() );
561 $testCase->assertArrayEquals( [
562 'category1' => '2',
563 'category2' => '1'
564 ], $object->getCategoryMap() );
565 $testCase->assertArrayEquals( [ 'indicator1' => 'indicator1_value' ], $object->getIndicators() );
566 $testCase->assertSame( 'title_text1', $object->getTitleText() );
567 $testCase->assertArrayEquals( self::SECTIONS, $object->getSections() );
568 $testCase->assertNotNull( $object->getTOCData() );
569 $testCase->assertArrayEquals( [
570 NS_MAIN => [ 'Link1' => 42 ],
571 NS_USER => [ 'Link2' => 43 ]
572 ], $object->getLinks() );
573 $testCase->assertArrayEquals( [
574 NS_SPECIAL => [ 'Template1' => 42 ]
575 ], $object->getTemplates() );
576 $testCase->assertArrayEquals( [
577 NS_SPECIAL => [ 'Template1' => 4242 ]
578 ], $object->getTemplateIds() );
579 $testCase->assertArrayEquals( [ 'Image1' => 1 ], $object->getImages() );
580 $testCase->assertArrayEquals( [ 'Image1' => [
581 'time' => MWTimestamp::convert( TS_MW, 123456789 ), 'sha1' => 'test_sha1'
582 ] ], $object->getFileSearchOptions() );
583 $testCase->assertArrayEquals( [ 'https://test.com' => 1 ], $object->getExternalLinks() );
584 $testCase->assertArrayEquals( [ 'tag1' => 'head_item1' ], $object->getHeadItems() );
585 $testCase->assertArrayEquals( [ 'module1' ], $object->getModules() );
586 $testCase->assertArrayEquals( [ 'module_style1' ], $object->getModuleStyles() );
587 $testCase->assertArrayEquals( [ 'key1' => 'value1' ], $object->getJsConfigVars() );
588 $testCase->assertArrayEquals( [ 'warning1' ], $object->getWarnings() );
589 $testCase->assertSame( 'noindex', $object->getIndexPolicy() );
590 $testCase->assertSame( MWTimestamp::convert( TS_MW, 987654321 ), $object->getRevisionTimestamp() );
591 $testCase->assertArrayEquals(
592 [ 'limit_report_key1' => 'value1' ],
593 $object->getLimitReportData()
594 );
595 $testCase->assertArrayEquals(
596 [ 'limit_report_key1' => 'value1' ],
597 $object->getLimitReportJSData()
598 );
599 $testCase->assertTrue( $object->getEnableOOUI() );
600 $testCase->assertTrue( $object->getHideNewSection() );
601 $testCase->assertTrue( $object->getNewSection() );
602 $testCase->assertTrue( $object->getOutputFlag( 'test' ) );
603 $testCase->assertArrayEquals( [ 'test' ], $object->getAllFlags() );
604 }
605 ],
606 'withMetadataPost1_45' => [
607 'instance' => $parserOutputWithMetadataPost1_45,
608 'assertions' => static function ( MediaWikiIntegrationTestCase $testCase, ParserOutput $object ) {
609 $testCase->assertSame( 42, $object->getSpeculativeRevIdUsed() );
610 $testCase->assertArrayEquals( [ 'm:link1', 'mw:link2' ], $object->getLanguageLinks() );
611 $testCase->assertArrayEquals( [ 'enwiki' => [
612 'interwiki1' => 1,
613 'interwiki2' => 1
614 ] ], $object->getInterwikiLinks() );
615 $testCase->assertArrayEquals( [ 'category1', 'category2' ], $object->getCategoryNames() );
616 $testCase->assertArrayEquals( [
617 'category1' => '2',
618 'category2' => '1'
619 ], $object->getCategoryMap() );
620 $testCase->assertArrayEquals( [ 'indicator1' => 'indicator1_value' ], $object->getIndicators() );
621 $testCase->assertSame( 'title_text1', $object->getTitleText() );
622 $testCase->assertArrayEquals( self::SECTIONS, $object->getSections() );
623 $testCase->assertNotNull( $object->getTOCData() );
624 $testCase->assertArrayEquals( [
625 NS_MAIN => [ 'Link1' => 42 ],
626 NS_USER => [ 'Link2' => 43 ]
627 ], $object->getLinks() );
628 $testCase->assertArrayEquals( [
629 NS_SPECIAL => [ 'Template1' => 42 ]
630 ], $object->getTemplates() );
631 $testCase->assertArrayEquals( [
632 NS_SPECIAL => [ 'Template1' => 4242 ]
633 ], $object->getTemplateIds() );
634 $testCase->assertArrayEquals( [ 'Image1' => 1 ], $object->getImages() );
635 $testCase->assertArrayEquals( [ 'Image1' => [
636 'time' => MWTimestamp::convert( TS_MW, 123456789 ), 'sha1' => 'test_sha1'
637 ] ], $object->getFileSearchOptions() );
638 $testCase->assertArrayEquals( [ 'https://test.com' => 1 ], $object->getExternalLinks() );
639 $testCase->assertArrayEquals( [ 'tag1' => 'head_item1' ], $object->getHeadItems() );
640 $testCase->assertArrayEquals( [ 'module1' ], $object->getModules() );
641 $testCase->assertArrayEquals( [ 'module_style1' ], $object->getModuleStyles() );
642 $testCase->assertArrayEquals( [ 'key1' => 'value1' ], $object->getJsConfigVars() );
643 $testCase->assertArrayEquals( [ 'warning1' ], $object->getWarnings() );
644 $testCase->assertSame( 'noindex', $object->getIndexPolicy() );
645 $testCase->assertSame( MWTimestamp::convert( TS_MW, 987654321 ), $object->getRevisionTimestamp() );
646 $testCase->assertArrayEquals(
647 [ 'limit_report_key1' => 'value1' ],
648 $object->getLimitReportData()
649 );
650 $testCase->assertArrayEquals(
651 [ 'limit_report_key1' => 'value1' ],
652 $object->getLimitReportJSData()
653 );
654 $testCase->assertTrue( $object->getEnableOOUI() );
655 $testCase->assertTrue( $object->getHideNewSection() );
656 $testCase->assertTrue( $object->getNewSection() );
657 $testCase->assertTrue( $object->getOutputFlag( ParserOutputFlags::VARY_REVISION_ID ) );
658 $testCase->assertArrayEquals( [ 'vary-revision-id' ], $object->getAllFlags() );
659 }
660 ],
661 'withSections' => [
662 'instance' => $parserOutputWithSections,
663 'assertions' => static function ( MediaWikiIntegrationTestCase $testCase, ParserOutput $object ) {
664 $testCase->assertArrayEquals( self::SECTIONS, $object->getSections() );
665 $testCase->assertNotNull( $object->getTOCData() );
666 }
667 ],
668 'withMetadataPost1_31' => [
669 'instance' => $parserOutputWithMetadataPost1_31,
670 'assertions' => static function ( MediaWikiIntegrationTestCase $testCase, ParserOutput $object ) {
671 $testCase->assertSame( 'test_wrapper', $object->getWrapperDivClass() );
672 $testCase->assertSame( 4242, $object->getSpeculativePageIdUsed() );
673 $testCase->assertSame(
674 MWTimestamp::convert( TS_MW, 123456789 ),
675 $object->getRevisionTimestampUsed()
676 );
677 $testCase->assertSame( 'test_hash', $object->getRevisionUsedSha1Base36() );
678 $testCase->assertTrue( $object->getNoGallery() );
679 }
680 ],
681 'withMetadataPost1_34' => [
682 'instance' => $parserOutputWithMetadataPost1_34,
683 'assertions' => static function ( MediaWikiIntegrationTestCase $testCase, ParserOutput $object ) {
684 $testCase->assertArrayEquals( [ 'default1' ], $object->getExtraCSPDefaultSrcs() );
685 $testCase->assertArrayEquals( [ 'script1' ], $object->getExtraCSPScriptSrcs() );
686 $testCase->assertArrayEquals( [ 'style1' ], $object->getExtraCSPStyleSrcs() );
687 $testCase->assertArrayEquals( [ 'Link3' => 1 ], $object->getLinksSpecial() );
688 }
689 ],
690 'withFalsyProperties' => [
691 'instance' => $parserOutputWithFalsyProperties,
692 'assertions' => static function ( MediaWikiIntegrationTestCase $testCase, ParserOutput $object ) {
693 $testCase->assertSame(
694 self::MOCK_FALSY_PROPERTIES['boolean'],
695 $object->getPageProperty( 'boolean' )
696 );
697 $testCase->assertSame(
698 self::MOCK_FALSY_PROPERTIES['null'],
699 $object->getPageProperty( 'null' )
700 );
701 $testCase->assertSame(
702 self::MOCK_FALSY_PROPERTIES['number'],
703 $object->getPageProperty( 'number' )
704 );
705 $testCase->assertSame(
706 self::MOCK_FALSY_PROPERTIES['string'],
707 $object->getPageProperty( 'string' )
708 );
709 $testCase->assertSame(
710 self::MOCK_FALSY_PROPERTIES['numstring'],
711 $object->getPageProperty( 'numstring' )
712 );
713 $testCase->assertArrayEquals(
714 self::MOCK_FALSY_PROPERTIES['array'],
715 $object->getPageProperty( 'array' )
716 );
717 }
718 ],
719 'withFalsyProperties1_45' => [
720 'instance' => $parserOutputWithFalsyProperties1_45,
721 'assertions' => static function ( MediaWikiIntegrationTestCase $testCase, ParserOutput $object ) {
722 self::assertPagePropSame( $testCase,
723 self::MOCK_FALSY_PROPERTIES['boolean'],
724 $object->getPageProperty( 'boolean' )
725 );
726 self::assertPagePropSame( $testCase,
727 self::MOCK_FALSY_PROPERTIES['null'],
728 $object->getPageProperty( 'null' )
729 );
730 self::assertPagePropSame( $testCase,
731 self::MOCK_FALSY_PROPERTIES['number'],
732 $object->getPageProperty( 'number' )
733 );
734 self::assertPagePropSame( $testCase,
735 self::MOCK_FALSY_PROPERTIES['string'],
736 $object->getPageProperty( 'string' )
737 );
738 self::assertPagePropSame( $testCase,
739 self::MOCK_FALSY_PROPERTIES['numstring'],
740 $object->getPageProperty( 'numstring' )
741 );
742 self::assertPagePropSame( $testCase,
743 self::MOCK_FALSY_PROPERTIES['array'],
744 $object->getPageProperty( 'array' )
745 );
746 }
747 ],
748 'withEmptyToC' => [
749 'instance' => $parserOutputWithEmptyToC,
750 'assertions' => static function ( MediaWikiIntegrationTestCase $testCase, ParserOutput $object ) {
751 $testCase->assertArrayEquals( [], $object->getSections() );
752 $testCase->assertNotNull( $object->getTOCData() );
753 },
754 ],
755 ];
756 // We don't serialize or restore parseStartTime any more, so
757 // ensure that it is cleared in the instances we are going to
758 // compare against.
759 foreach ( $testCases as $name => $case ) {
760 $case['instance']->clearParseStartTime();
761 }
762 return $testCases;
763 }
764
765 /**
766 * @param string $class the class name
767 * @return string[][] a list of supported serialization formats info
768 * in the following format:
769 * 'ext' => string file extension for stored serializations
770 * 'serializer' => callable to serialize objects
771 * 'deserializer' => callable to deserialize objects
772 */
773 public static function getSupportedSerializationFormats( string $class ): array {
774 $serializationFormats = [ [
775 'ext' => 'serialized',
776 'serializer' => 'serialize',
777 'deserializer' => 'unserialize'
778 ] ];
779 if (
780 is_subclass_of( $class, JsonSerializable::class ) ||
781 is_subclass_of( $class, JsonCodecable::class )
782 ) {
783 $jsonCodec = new JsonCodec();
784 $serializationFormats[] = [
785 'ext' => 'json',
786 'serializer' => static function ( $obj ) use ( $jsonCodec ) {
787 return $jsonCodec->serialize( $obj );
788 },
789 'deserializer' => static function ( $data ) use ( $jsonCodec ) {
790 return $jsonCodec->deserialize( $data );
791 }
792 ];
793 }
794 // T374736: hack for old test cases
795 foreach ( $serializationFormats as [ 'deserializer' => &$d ] ) {
796 $oldDeserializer = $d;
797 $d = static function ( $data ) use ( $oldDeserializer ) {
798 MWDebug::filterDeprecationForTest( '/::addLanguageLink without prefix/' );
799 MWDebug::filterDeprecationForTest( '/::setOutputFlag with non-standard flag/' );
800 return $oldDeserializer( $data );
801 };
802 }
803 return $serializationFormats;
804 }
805 }