| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Parser; |
| 4 |
|
| 5 |
use LogicException; |
| 6 |
use MediaWiki\Context\RequestContext; |
| 7 |
use MediaWiki\Debug\MWDebug; |
| 8 |
use MediaWiki\MainConfigNames; |
| 9 |
use MediaWiki\MediaWikiServices; |
| 10 |
use MediaWiki\Parser\ParserOptions; |
| 11 |
use MediaWiki\Parser\ParserOutput; |
| 12 |
use MediaWiki\Parser\ParserOutputFlags; |
| 13 |
use MediaWiki\Parser\ParserOutputLinkTypes; |
| 14 |
use MediaWiki\Parser\ParserOutputStringSets; |
| 15 |
use MediaWiki\Title\Title; |
| 16 |
use MediaWiki\Title\TitleValue; |
| 17 |
use MediaWiki\Utils\MWTimestamp; |
| 18 |
use MediaWikiLangTestCase; |
| 19 |
use Wikimedia\Bcp47Code\Bcp47CodeValue; |
| 20 |
use Wikimedia\Parsoid\Core\SectionMetadata; |
| 21 |
use Wikimedia\Parsoid\Core\TOCData; |
| 22 |
use Wikimedia\TestingAccessWrapper; |
| 23 |
use Wikimedia\Tests\SerializationTestTrait; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class ParserOutputTest extends MediaWikiLangTestCase { |
| 32 |
use SerializationTestTrait; |
| 33 |
|
| 34 |
protected function setUp(): void { |
| 35 |
parent::setUp(); |
| 36 |
|
| 37 |
MWTimestamp::setFakeTime( ParserCacheSerializationTestCases::FAKE_TIME ); |
| 38 |
$this->overrideConfigValue( |
| 39 |
MainConfigNames::ParserCacheExpireTime, |
| 40 |
ParserCacheSerializationTestCases::FAKE_CACHE_EXPIRY |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public static function getClassToTest(): string { |
| 49 |
return ParserOutput::class; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public static function getSerializedDataPath(): string { |
| 57 |
return __DIR__ . '/../../data/ParserCache'; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public static function getTestInstancesAndAssertions(): array { |
| 65 |
return ParserCacheSerializationTestCases::getParserOutputTestCases(); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public static function getSupportedSerializationFormats(): array { |
| 73 |
return ParserCacheSerializationTestCases::getSupportedSerializationFormats( |
| 74 |
self::getClassToTest() ); |
| 75 |
} |
| 76 |
|
| 77 |
public static function provideIsLinkInternal() { |
| 78 |
return [ |
| 79 |
|
| 80 |
[ false, 'http://example.org', 'http://mediawiki.org' ], |
| 81 |
|
| 82 |
[ true, 'http://example.org', 'http://example.org' ], |
| 83 |
[ true, 'https://example.org', 'https://example.org' ], |
| 84 |
[ true, '//example.org', '//example.org' ], |
| 85 |
|
| 86 |
[ true, 'http://example.org', 'http://EXAMPLE.ORG' ], |
| 87 |
|
| 88 |
[ true, 'http://example.org', 'http://example.org/wiki/Main_Page' ], |
| 89 |
[ true, 'http://example.org', 'http://example.org?my=query' ], |
| 90 |
[ true, 'http://example.org', 'http://example.org#its-a-fragment' ], |
| 91 |
|
| 92 |
[ false, 'http://example.org', 'https://example.org' ], |
| 93 |
[ false, 'https://example.org', 'http://example.org' ], |
| 94 |
|
| 95 |
[ true, '//example.org', 'http://example.org' ], |
| 96 |
[ true, '//example.org', 'https://example.org' ], |
| 97 |
|
| 98 |
[ false, '//example.org', 'irc://example.org' ], |
| 99 |
]; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public function testIsLinkInternal( $shouldMatch, $server, $url ) { |
| 108 |
$this->assertEquals( $shouldMatch, ParserOutput::isLinkInternal( $server, $url ) ); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public function testJsConfigVars() { |
| 117 |
$po = new ParserOutput(); |
| 118 |
|
| 119 |
$po->setJsConfigVar( 'a', '1' ); |
| 120 |
$po->appendJsConfigVar( 'b', 'a' ); |
| 121 |
$po->appendJsConfigVar( 'b', '0' ); |
| 122 |
|
| 123 |
$this->assertEqualsCanonicalizing( [ |
| 124 |
'a' => 1, |
| 125 |
'b' => [ 'a' => true, '0' => true ], |
| 126 |
], $po->getJsConfigVars() ); |
| 127 |
|
| 128 |
$po->setJsConfigVar( 'c', '2' ); |
| 129 |
$po->appendJsConfigVar( 'b', 'b' ); |
| 130 |
$po->appendJsConfigVar( 'b', '1' ); |
| 131 |
|
| 132 |
$this->assertEqualsCanonicalizing( [ |
| 133 |
'a' => 1, |
| 134 |
'b' => [ 'a' => true, 'b' => true, '0' => true, '1' => true ], |
| 135 |
'c' => 2, |
| 136 |
], $po->getJsConfigVars() ); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public function testExtensionData() { |
| 145 |
$po = new ParserOutput(); |
| 146 |
|
| 147 |
$po->setExtensionData( "one", "Foo" ); |
| 148 |
$po->appendExtensionData( "three", "abc" ); |
| 149 |
|
| 150 |
$this->assertEquals( "Foo", $po->getExtensionData( "one" ) ); |
| 151 |
$this->assertNull( $po->getExtensionData( "spam" ) ); |
| 152 |
|
| 153 |
$po->setExtensionData( "two", "Bar" ); |
| 154 |
$this->assertEquals( "Foo", $po->getExtensionData( "one" ) ); |
| 155 |
$this->assertEquals( "Bar", $po->getExtensionData( "two" ) ); |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
$po->setExtensionData( "one", null ); |
| 162 |
$this->assertNull( $po->getExtensionData( "one" ) ); |
| 163 |
$this->assertEquals( "Bar", $po->getExtensionData( "two" ) ); |
| 164 |
|
| 165 |
$this->assertEqualsCanonicalizing( [ |
| 166 |
'abc' => true, |
| 167 |
], $po->getExtensionData( "three" ) ); |
| 168 |
|
| 169 |
$po->appendExtensionData( "three", "xyz" ); |
| 170 |
$this->assertEqualsCanonicalizing( [ |
| 171 |
'abc' => true, |
| 172 |
'xyz' => true, |
| 173 |
], $po->getExtensionData( "three" ) ); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public function testPageProperties( string $setPageProperty, $value1, $value2, bool $expectDeprecation = false ) { |
| 186 |
$po = new ParserOutput(); |
| 187 |
if ( $expectDeprecation ) { |
| 188 |
MWDebug::filterDeprecationForTest( '/::setPageProperty with non-string value/' ); |
| 189 |
} |
| 190 |
|
| 191 |
$po->$setPageProperty( 'foo', $value1 ); |
| 192 |
|
| 193 |
$properties = $po->getPageProperties(); |
| 194 |
$this->assertSame( $value1, $po->getPageProperty( 'foo' ) ); |
| 195 |
$this->assertSame( $value1, $properties['foo'] ); |
| 196 |
|
| 197 |
$po->$setPageProperty( 'foo', $value2 ); |
| 198 |
|
| 199 |
$properties = $po->getPageProperties(); |
| 200 |
$this->assertSame( $value2, $po->getPageProperty( 'foo' ) ); |
| 201 |
$this->assertSame( $value2, $properties['foo'] ); |
| 202 |
|
| 203 |
$po->unsetPageProperty( 'foo' ); |
| 204 |
|
| 205 |
$properties = $po->getPageProperties(); |
| 206 |
$this->assertSame( null, $po->getPageProperty( 'foo' ) ); |
| 207 |
$this->assertArrayNotHasKey( 'foo', $properties ); |
| 208 |
} |
| 209 |
|
| 210 |
public static function providePageProperties() { |
| 211 |
yield 'Unsorted' => [ 'setUnsortedPageProperty', 'val', 'second val' ]; |
| 212 |
yield 'Numeric' => [ 'setNumericPageProperty', 42, 3.14 ]; |
| 213 |
yield 'Unsorted (old style)' => [ 'setPageProperty', 'val', 'second val' ]; |
| 214 |
yield 'Numeric (old style)' => [ 'setPageProperty', 123, 456, true ]; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
public function testNumericPageProperties() { |
| 221 |
$po = new ParserOutput(); |
| 222 |
|
| 223 |
$po->setNumericPageProperty( 'foo', '123' ); |
| 224 |
|
| 225 |
$properties = $po->getPageProperties(); |
| 226 |
$this->assertSame( 123, $po->getPageProperty( 'foo' ) ); |
| 227 |
$this->assertSame( 123, $properties['foo'] ); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public function testUnsortedPageProperties() { |
| 234 |
$po = new ParserOutput(); |
| 235 |
|
| 236 |
$po->setUnsortedPageProperty( 'foo', 123 ); |
| 237 |
|
| 238 |
$properties = $po->getPageProperties(); |
| 239 |
$this->assertSame( '123', $po->getPageProperty( 'foo' ) ); |
| 240 |
$this->assertSame( '123', $properties['foo'] ); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public function testLanguage() { |
| 248 |
$po = new ParserOutput(); |
| 249 |
|
| 250 |
$langFr = new Bcp47CodeValue( 'fr' ); |
| 251 |
$langCrhCyrl = new Bcp47CodeValue( 'crh-cyrl' ); |
| 252 |
|
| 253 |
|
| 254 |
$this->assertSame( null, $po->getLanguage() ); |
| 255 |
|
| 256 |
|
| 257 |
$po->setLanguage( $langFr ); |
| 258 |
$this->assertSame( $langFr->toBcp47Code(), $po->getLanguage()->toBcp47Code() ); |
| 259 |
|
| 260 |
|
| 261 |
$po->setLanguage( $langCrhCyrl ); |
| 262 |
$this->assertSame( $langCrhCyrl->toBcp47Code(), $po->getLanguage()->toBcp47Code() ); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public function testWrapperDivClass() { |
| 271 |
$po = new ParserOutput(); |
| 272 |
$opts = ParserOptions::newFromAnon(); |
| 273 |
$pipeline = MediaWikiServices::getInstance()->getDefaultOutputPipeline(); |
| 274 |
|
| 275 |
$po->setRawText( 'Kittens' ); |
| 276 |
$text = $pipeline->run( $po, $opts, [] )->getContentHolderText(); |
| 277 |
$this->assertStringContainsString( 'Kittens', $text ); |
| 278 |
$this->assertStringNotContainsString( '<div', $text ); |
| 279 |
$this->assertSame( 'Kittens', $po->getRawText() ); |
| 280 |
|
| 281 |
$po->addWrapperDivClass( 'foo' ); |
| 282 |
$text = $pipeline->run( $po, $opts, [] )->getContentHolderText(); |
| 283 |
$this->assertStringContainsString( 'Kittens', $text ); |
| 284 |
$this->assertStringContainsString( '<div', $text ); |
| 285 |
$this->assertStringContainsString( 'class="mw-content-ltr foo"', $text ); |
| 286 |
|
| 287 |
$po->addWrapperDivClass( 'bar' ); |
| 288 |
$text = $pipeline->run( $po, $opts, [] )->getContentHolderText(); |
| 289 |
$this->assertStringContainsString( 'Kittens', $text ); |
| 290 |
$this->assertStringContainsString( '<div', $text ); |
| 291 |
$this->assertStringContainsString( 'class="mw-content-ltr foo bar"', $text ); |
| 292 |
|
| 293 |
$po->addWrapperDivClass( 'bar' ); |
| 294 |
$text = $pipeline->run( $po, $opts, [ 'unwrap' => true ] )->getContentHolderText(); |
| 295 |
$this->assertStringContainsString( 'Kittens', $text ); |
| 296 |
$this->assertStringNotContainsString( '<div', $text ); |
| 297 |
$this->assertStringNotContainsString( 'class="', $text ); |
| 298 |
|
| 299 |
$text = $pipeline->run( $po, $opts, [ 'wrapperDivClass' => '' ] )->getContentHolderText(); |
| 300 |
$this->assertStringContainsString( 'Kittens', $text ); |
| 301 |
$this->assertStringNotContainsString( '<div', $text ); |
| 302 |
$this->assertStringNotContainsString( 'class="', $text ); |
| 303 |
|
| 304 |
$text = $pipeline->run( $po, $opts, [ 'wrapperDivClass' => 'xyzzy' ] )->getContentHolderText(); |
| 305 |
$this->assertStringContainsString( 'Kittens', $text ); |
| 306 |
$this->assertStringContainsString( '<div', $text ); |
| 307 |
$this->assertStringContainsString( 'class="mw-content-ltr xyzzy"', $text ); |
| 308 |
$this->assertStringNotContainsString( 'foo bar', $text ); |
| 309 |
|
| 310 |
$text = $po->getRawText(); |
| 311 |
$this->assertSame( 'Kittens', $text ); |
| 312 |
|
| 313 |
$po->clearWrapperDivClass(); |
| 314 |
$text = $pipeline->run( $po, $opts, [] )->getContentHolderText(); |
| 315 |
$this->assertStringContainsString( 'Kittens', $text ); |
| 316 |
$this->assertStringNotContainsString( '<div', $text ); |
| 317 |
$this->assertStringNotContainsString( 'class="', $text ); |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
public function testGetText( $options, $text, $expect ) { |
| 330 |
|
| 331 |
$this->overrideConfigValue( MainConfigNames::DefaultSkin, 'fallback' ); |
| 332 |
RequestContext::resetMain(); |
| 333 |
|
| 334 |
$this->overrideConfigValues( [ |
| 335 |
MainConfigNames::ScriptPath => '/w', |
| 336 |
MainConfigNames::Script => '/w/index.php', |
| 337 |
] ); |
| 338 |
|
| 339 |
$po = new ParserOutput( $text ); |
| 340 |
self::initSections( $po ); |
| 341 |
$actual = $po->getText( $options ); |
| 342 |
$this->assertSame( $expect, $actual ); |
| 343 |
} |
| 344 |
|
| 345 |
private static function initSections( ParserOutput $po ): void { |
| 346 |
$po->setTOCData( new TOCData( |
| 347 |
SectionMetadata::fromLegacy( [ |
| 348 |
'index' => "1", |
| 349 |
'level' => 1, |
| 350 |
'toclevel' => 1, |
| 351 |
'number' => "1", |
| 352 |
'line' => "Section 1", |
| 353 |
'anchor' => "Section_1" |
| 354 |
] ), |
| 355 |
SectionMetadata::fromLegacy( [ |
| 356 |
'index' => "2", |
| 357 |
'level' => 1, |
| 358 |
'toclevel' => 1, |
| 359 |
'number' => "2", |
| 360 |
'line' => "Section 2", |
| 361 |
'anchor' => "Section_2" |
| 362 |
] ), |
| 363 |
SectionMetadata::fromLegacy( [ |
| 364 |
'index' => "3", |
| 365 |
'level' => 2, |
| 366 |
'toclevel' => 2, |
| 367 |
'number' => "2.1", |
| 368 |
'line' => "Section 2.1", |
| 369 |
'anchor' => "Section_2.1" |
| 370 |
] ), |
| 371 |
SectionMetadata::fromLegacy( [ |
| 372 |
'index' => "4", |
| 373 |
'level' => 1, |
| 374 |
'toclevel' => 1, |
| 375 |
'number' => "3", |
| 376 |
'line' => "Section 3", |
| 377 |
'anchor' => "Section_3" |
| 378 |
] ), |
| 379 |
) ); |
| 380 |
} |
| 381 |
|
| 382 |
public static function provideGetText() { |
| 383 |
$text = <<<EOF |
| 384 |
<p>Test document. |
| 385 |
</p> |
| 386 |
<meta property="mw:PageProp/toc" /> |
| 387 |
<div class="mw-heading mw-heading2"><h2 id="Section_1">Section 1</h2><mw:editsection page="Test Page" section="1">Section 1</mw:editsection></div> |
| 388 |
<p>One |
| 389 |
</p> |
| 390 |
<div class="mw-heading mw-heading2"><h2 id="Section_2">Section 2</h2><mw:editsection page="Test Page" section="2">Section 2</mw:editsection></div> |
| 391 |
<p>Two |
| 392 |
</p> |
| 393 |
<div class="mw-heading mw-heading3"><h3 id="Section_2.1">Section 2.1</h3></div> |
| 394 |
<p>Two point one |
| 395 |
</p> |
| 396 |
<div class="mw-heading mw-heading2"><h2 id="Section_3">Section 3</h2><mw:editsection page="Test Page" section="4">Section 3</mw:editsection></div> |
| 397 |
<p>Three |
| 398 |
</p> |
| 399 |
EOF; |
| 400 |
|
| 401 |
$dedupText = <<<EOF |
| 402 |
<p>This is a test document.</p> |
| 403 |
<style data-mw-deduplicate="duplicate1">.Duplicate1 {}</style> |
| 404 |
<style data-mw-deduplicate="duplicate1">.Duplicate1 {}</style> |
| 405 |
<style data-mw-deduplicate="duplicate2">.Duplicate2 {}</style> |
| 406 |
<style data-mw-deduplicate="duplicate1">.Duplicate1 {}</style> |
| 407 |
<style data-mw-deduplicate="duplicate2">.Duplicate2 {}</style> |
| 408 |
<style data-mw-not-deduplicate="duplicate1">.Duplicate1 {}</style> |
| 409 |
<style data-mw-deduplicate="duplicate1">.Same-attribute-different-content {}</style> |
| 410 |
<style data-mw-deduplicate="duplicate3">.Duplicate1 {}</style> |
| 411 |
<style>.Duplicate1 {}</style> |
| 412 |
EOF; |
| 413 |
|
| 414 |
return [ |
| 415 |
'No options' => [ |
| 416 |
[], $text, <<<EOF |
| 417 |
<p>Test document. |
| 418 |
</p> |
| 419 |
<div id="toc" class="toc" role="navigation" aria-labelledby="mw-toc-heading"><input type="checkbox" role="button" id="toctogglecheckbox" class="toctogglecheckbox" style="display:none" /><div class="toctitle" lang="en" dir="ltr"><h2 id="mw-toc-heading">Contents</h2><span class="toctogglespan"><label class="toctogglelabel" for="toctogglecheckbox"></label></span></div> |
| 420 |
<ul> |
| 421 |
<li class="toclevel-1 tocsection-1"><a href="#Section_1"><span class="tocnumber">1</span> <span class="toctext">Section 1</span></a></li> |
| 422 |
<li class="toclevel-1 tocsection-2"><a href="#Section_2"><span class="tocnumber">2</span> <span class="toctext">Section 2</span></a> |
| 423 |
<ul> |
| 424 |
<li class="toclevel-2 tocsection-3"><a href="#Section_2.1"><span class="tocnumber">2.1</span> <span class="toctext">Section 2.1</span></a></li> |
| 425 |
</ul> |
| 426 |
</li> |
| 427 |
<li class="toclevel-1 tocsection-4"><a href="#Section_3"><span class="tocnumber">3</span> <span class="toctext">Section 3</span></a></li> |
| 428 |
</ul> |
| 429 |
</div> |
| 430 |
|
| 431 |
<div class="mw-heading mw-heading2"><h2 id="Section_1">Section 1</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=1" title="Edit section: Section 1">edit</a><span class="mw-editsection-bracket">]</span></span></div> |
| 432 |
<p>One |
| 433 |
</p> |
| 434 |
<div class="mw-heading mw-heading2"><h2 id="Section_2">Section 2</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=2" title="Edit section: Section 2">edit</a><span class="mw-editsection-bracket">]</span></span></div> |
| 435 |
<p>Two |
| 436 |
</p> |
| 437 |
<div class="mw-heading mw-heading3"><h3 id="Section_2.1">Section 2.1</h3></div> |
| 438 |
<p>Two point one |
| 439 |
</p> |
| 440 |
<div class="mw-heading mw-heading2"><h2 id="Section_3">Section 3</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=4" title="Edit section: Section 3">edit</a><span class="mw-editsection-bracket">]</span></span></div> |
| 441 |
<p>Three |
| 442 |
</p> |
| 443 |
EOF |
| 444 |
], |
| 445 |
'Disable section edit links' => [ |
| 446 |
[ 'enableSectionEditLinks' => false ], $text, <<<EOF |
| 447 |
<p>Test document. |
| 448 |
</p> |
| 449 |
<div id="toc" class="toc" role="navigation" aria-labelledby="mw-toc-heading"><input type="checkbox" role="button" id="toctogglecheckbox" class="toctogglecheckbox" style="display:none" /><div class="toctitle" lang="en" dir="ltr"><h2 id="mw-toc-heading">Contents</h2><span class="toctogglespan"><label class="toctogglelabel" for="toctogglecheckbox"></label></span></div> |
| 450 |
<ul> |
| 451 |
<li class="toclevel-1 tocsection-1"><a href="#Section_1"><span class="tocnumber">1</span> <span class="toctext">Section 1</span></a></li> |
| 452 |
<li class="toclevel-1 tocsection-2"><a href="#Section_2"><span class="tocnumber">2</span> <span class="toctext">Section 2</span></a> |
| 453 |
<ul> |
| 454 |
<li class="toclevel-2 tocsection-3"><a href="#Section_2.1"><span class="tocnumber">2.1</span> <span class="toctext">Section 2.1</span></a></li> |
| 455 |
</ul> |
| 456 |
</li> |
| 457 |
<li class="toclevel-1 tocsection-4"><a href="#Section_3"><span class="tocnumber">3</span> <span class="toctext">Section 3</span></a></li> |
| 458 |
</ul> |
| 459 |
</div> |
| 460 |
|
| 461 |
<div class="mw-heading mw-heading2"><h2 id="Section_1">Section 1</h2></div> |
| 462 |
<p>One |
| 463 |
</p> |
| 464 |
<div class="mw-heading mw-heading2"><h2 id="Section_2">Section 2</h2></div> |
| 465 |
<p>Two |
| 466 |
</p> |
| 467 |
<div class="mw-heading mw-heading3"><h3 id="Section_2.1">Section 2.1</h3></div> |
| 468 |
<p>Two point one |
| 469 |
</p> |
| 470 |
<div class="mw-heading mw-heading2"><h2 id="Section_3">Section 3</h2></div> |
| 471 |
<p>Three |
| 472 |
</p> |
| 473 |
EOF |
| 474 |
], |
| 475 |
'Disable TOC, but wrap' => [ |
| 476 |
[ 'allowTOC' => false, 'wrapperDivClass' => 'mw-parser-output' ], $text, <<<EOF |
| 477 |
<div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr"><p>Test document. |
| 478 |
</p> |
| 479 |
|
| 480 |
<div class="mw-heading mw-heading2"><h2 id="Section_1">Section 1</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=1" title="Edit section: Section 1">edit</a><span class="mw-editsection-bracket">]</span></span></div> |
| 481 |
<p>One |
| 482 |
</p> |
| 483 |
<div class="mw-heading mw-heading2"><h2 id="Section_2">Section 2</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=2" title="Edit section: Section 2">edit</a><span class="mw-editsection-bracket">]</span></span></div> |
| 484 |
<p>Two |
| 485 |
</p> |
| 486 |
<div class="mw-heading mw-heading3"><h3 id="Section_2.1">Section 2.1</h3></div> |
| 487 |
<p>Two point one |
| 488 |
</p> |
| 489 |
<div class="mw-heading mw-heading2"><h2 id="Section_3">Section 3</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=4" title="Edit section: Section 3">edit</a><span class="mw-editsection-bracket">]</span></span></div> |
| 490 |
<p>Three |
| 491 |
</p></div> |
| 492 |
EOF |
| 493 |
], |
| 494 |
'Style deduplication' => [ |
| 495 |
[], $dedupText, <<<EOF |
| 496 |
<p>This is a test document.</p> |
| 497 |
<style data-mw-deduplicate="duplicate1">.Duplicate1 {}</style> |
| 498 |
<link rel="mw-deduplicated-inline-style" href="mw-data:duplicate1" /> |
| 499 |
<style data-mw-deduplicate="duplicate2">.Duplicate2 {}</style> |
| 500 |
<link rel="mw-deduplicated-inline-style" href="mw-data:duplicate1" /> |
| 501 |
<link rel="mw-deduplicated-inline-style" href="mw-data:duplicate2" /> |
| 502 |
<style data-mw-not-deduplicate="duplicate1">.Duplicate1 {}</style> |
| 503 |
<link rel="mw-deduplicated-inline-style" href="mw-data:duplicate1" /> |
| 504 |
<style data-mw-deduplicate="duplicate3">.Duplicate1 {}</style> |
| 505 |
<style>.Duplicate1 {}</style> |
| 506 |
EOF |
| 507 |
], |
| 508 |
'Style deduplication disabled' => [ |
| 509 |
[ 'deduplicateStyles' => false ], $dedupText, $dedupText |
| 510 |
], |
| 511 |
]; |
| 512 |
|
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
public function testHasText() { |
| 519 |
$po = new ParserOutput( '' ); |
| 520 |
$this->assertTrue( $po->hasText() ); |
| 521 |
|
| 522 |
$po = new ParserOutput( null ); |
| 523 |
$this->assertFalse( $po->hasText() ); |
| 524 |
|
| 525 |
$po = new ParserOutput(); |
| 526 |
$this->assertFalse( $po->hasText() ); |
| 527 |
|
| 528 |
$po = new ParserOutput( '' ); |
| 529 |
$this->assertTrue( $po->hasText() ); |
| 530 |
|
| 531 |
$po = new ParserOutput( null ); |
| 532 |
$po->setRawText( '' ); |
| 533 |
$this->assertTrue( $po->hasText() ); |
| 534 |
|
| 535 |
$po = new ParserOutput( 'foo' ); |
| 536 |
$po->setRawText( null ); |
| 537 |
$this->assertFalse( $po->hasText() ); |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
public function testGetText_failsIfNoText() { |
| 546 |
$po = new ParserOutput( null ); |
| 547 |
|
| 548 |
$this->expectException( LogicException::class ); |
| 549 |
$po->getText(); |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
public function testGetRawText_failsIfNoText() { |
| 556 |
$po = new ParserOutput( null ); |
| 557 |
|
| 558 |
$this->expectException( LogicException::class ); |
| 559 |
$po->getRawText(); |
| 560 |
} |
| 561 |
|
| 562 |
public static function provideMergeHtmlMetaDataFrom() { |
| 563 |
|
| 564 |
$a = new ParserOutput(); |
| 565 |
$a->setTitleText( 'X' ); |
| 566 |
$b = new ParserOutput(); |
| 567 |
yield 'only left title text' => [ $a, $b, [ 'getTitleText' => 'X' ] ]; |
| 568 |
|
| 569 |
$a = new ParserOutput(); |
| 570 |
$b = new ParserOutput(); |
| 571 |
$b->setTitleText( 'Y' ); |
| 572 |
yield 'only right title text' => [ $a, $b, [ 'getTitleText' => 'Y' ] ]; |
| 573 |
|
| 574 |
$a = new ParserOutput(); |
| 575 |
$a->setTitleText( 'X' ); |
| 576 |
$b = new ParserOutput(); |
| 577 |
$b->setTitleText( 'Y' ); |
| 578 |
yield 'left title text wins' => [ $a, $b, [ 'getTitleText' => 'X' ] ]; |
| 579 |
|
| 580 |
|
| 581 |
$a = new ParserOutput(); |
| 582 |
$a->setIndexPolicy( 'index' ); |
| 583 |
$b = new ParserOutput(); |
| 584 |
yield 'only left index policy' => [ $a, $b, [ 'getIndexPolicy' => 'index' ] ]; |
| 585 |
|
| 586 |
$a = new ParserOutput(); |
| 587 |
$b = new ParserOutput(); |
| 588 |
$b->setIndexPolicy( 'index' ); |
| 589 |
yield 'only right index policy' => [ $a, $b, [ 'getIndexPolicy' => 'index' ] ]; |
| 590 |
|
| 591 |
$a = new ParserOutput(); |
| 592 |
$a->setIndexPolicy( 'noindex' ); |
| 593 |
$b = new ParserOutput(); |
| 594 |
$b->setIndexPolicy( 'index' ); |
| 595 |
yield 'left noindex wins' => [ $a, $b, [ 'getIndexPolicy' => 'noindex' ] ]; |
| 596 |
|
| 597 |
$a = new ParserOutput(); |
| 598 |
$a->setIndexPolicy( 'index' ); |
| 599 |
$b = new ParserOutput(); |
| 600 |
$b->setIndexPolicy( 'noindex' ); |
| 601 |
yield 'right noindex wins' => [ $a, $b, [ 'getIndexPolicy' => 'noindex' ] ]; |
| 602 |
|
| 603 |
$crhCyrl = new Bcp47CodeValue( 'crh-cyrl' ); |
| 604 |
|
| 605 |
$a = new ParserOutput(); |
| 606 |
$a->setLanguage( $crhCyrl ); |
| 607 |
$b = new ParserOutput(); |
| 608 |
yield 'only left language' => [ $a, $b, [ 'getLanguage' => $crhCyrl ] ]; |
| 609 |
|
| 610 |
$a = new ParserOutput(); |
| 611 |
$b = new ParserOutput(); |
| 612 |
$b->setLanguage( $crhCyrl ); |
| 613 |
yield 'only right language' => [ $a, $b, [ 'getLanguage' => $crhCyrl ] ]; |
| 614 |
|
| 615 |
|
| 616 |
$a = new ParserOutput(); |
| 617 |
$a->addHeadItem( '<foo1>' ); |
| 618 |
$a->addHeadItem( '<bar1>', 'bar' ); |
| 619 |
$a->addModules( [ 'test-module-a' ] ); |
| 620 |
$a->addModuleStyles( [ 'test-module-styles-a' ] ); |
| 621 |
$a->setJsConfigVar( 'test-config-var-a', 'a' ); |
| 622 |
$a->appendJsConfigVar( 'test-config-var-c', 'abc' ); |
| 623 |
$a->appendJsConfigVar( 'test-config-var-c', 'def' ); |
| 624 |
$a->addExtraCSPStyleSrc( 'css.com' ); |
| 625 |
$a->addExtraCSPStyleSrc( 'css2.com' ); |
| 626 |
$a->addExtraCSPScriptSrc( 'js.com' ); |
| 627 |
$a->addExtraCSPDefaultSrc( 'img.com' ); |
| 628 |
|
| 629 |
$b = new ParserOutput(); |
| 630 |
$b->setIndexPolicy( 'noindex' ); |
| 631 |
$b->addHeadItem( '<foo2>' ); |
| 632 |
$b->addHeadItem( '<bar2>', 'bar' ); |
| 633 |
$b->addModules( [ 'test-module-b' ] ); |
| 634 |
$b->addModuleStyles( [ 'test-module-styles-b' ] ); |
| 635 |
$b->setJsConfigVar( 'test-config-var-b', 'b' ); |
| 636 |
$b->setJsConfigVar( 'test-config-var-a', 'X' ); |
| 637 |
$a->appendJsConfigVar( 'test-config-var-c', 'xyz' ); |
| 638 |
$a->appendJsConfigVar( 'test-config-var-c', 'def' ); |
| 639 |
$b->addExtraCSPStyleSrc( 'https://css.ca' ); |
| 640 |
$b->addExtraCSPScriptSrc( 'jscript.com' ); |
| 641 |
$b->addExtraCSPScriptSrc( 'vbscript.com' ); |
| 642 |
$b->addExtraCSPDefaultSrc( 'img.com/foo.jpg' ); |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
yield 'head items and friends' => [ $a, $b, [ |
| 649 |
'getHeadItems' => [ |
| 650 |
'<foo1>', |
| 651 |
'<foo2>', |
| 652 |
'bar' => '<bar2>', |
| 653 |
], |
| 654 |
'getModules' => [ |
| 655 |
'test-module-a', |
| 656 |
'test-module-b', |
| 657 |
], |
| 658 |
'getModuleStyles' => [ |
| 659 |
'test-module-styles-a', |
| 660 |
'test-module-styles-b', |
| 661 |
], |
| 662 |
'getJsConfigVars' => [ |
| 663 |
'test-config-var-a' => 'X', |
| 664 |
'test-config-var-b' => 'b', |
| 665 |
'test-config-var-c' => [ |
| 666 |
'abc' => true, 'def' => true, 'xyz' => true, |
| 667 |
], |
| 668 |
], |
| 669 |
'getExtraCSPStyleSrcs' => [ |
| 670 |
'css.com', |
| 671 |
'css2.com', |
| 672 |
'https://css.ca' |
| 673 |
], |
| 674 |
'getExtraCSPScriptSrcs' => [ |
| 675 |
'js.com', |
| 676 |
'jscript.com', |
| 677 |
'vbscript.com' |
| 678 |
], |
| 679 |
'getExtraCSPDefaultSrcs' => [ |
| 680 |
'img.com', |
| 681 |
'img.com/foo.jpg' |
| 682 |
] |
| 683 |
] ]; |
| 684 |
|
| 685 |
|
| 686 |
$a = new ParserOutput( '' ); |
| 687 |
$a->setSections( [ [ 'fromtitle' => 'A1' ], [ 'fromtitle' => 'A2' ] ] ); |
| 688 |
|
| 689 |
$b = new ParserOutput( '' ); |
| 690 |
$b->setSections( [ [ 'fromtitle' => 'B1' ], [ 'fromtitle' => 'B2' ] ] ); |
| 691 |
|
| 692 |
yield 'concat TOC' => [ $a, $b, [ |
| 693 |
'getSections' => [ |
| 694 |
SectionMetadata::fromLegacy( [ 'fromtitle' => 'A1' ] )->toLegacy(), |
| 695 |
SectionMetadata::fromLegacy( [ 'fromtitle' => 'A2' ] )->toLegacy(), |
| 696 |
SectionMetadata::fromLegacy( [ 'fromtitle' => 'B1' ] )->toLegacy(), |
| 697 |
SectionMetadata::fromLegacy( [ 'fromtitle' => 'B2' ] )->toLegacy() |
| 698 |
], |
| 699 |
] ]; |
| 700 |
|
| 701 |
|
| 702 |
$a = new ParserOutput(); |
| 703 |
$a->setNewSection( true ); |
| 704 |
$a->setHideNewSection( true ); |
| 705 |
$a->setNoGallery( true ); |
| 706 |
$a->addWrapperDivClass( 'foo' ); |
| 707 |
|
| 708 |
$a->setIndicator( 'foo', 'Foo!' ); |
| 709 |
$a->setIndicator( 'bar', 'Bar!' ); |
| 710 |
|
| 711 |
$a->setExtensionData( 'foo', 'Foo!' ); |
| 712 |
$a->setExtensionData( 'bar', 'Bar!' ); |
| 713 |
$a->appendExtensionData( 'bat', 'abc' ); |
| 714 |
|
| 715 |
$b = new ParserOutput(); |
| 716 |
$b->setNoGallery( true ); |
| 717 |
$b->setEnableOOUI( true ); |
| 718 |
$b->setPreventClickjacking( true ); |
| 719 |
$a->addWrapperDivClass( 'bar' ); |
| 720 |
|
| 721 |
$b->setIndicator( 'zoo', 'Zoo!' ); |
| 722 |
$b->setIndicator( 'bar', 'Barrr!' ); |
| 723 |
|
| 724 |
$b->setExtensionData( 'zoo', 'Zoo!' ); |
| 725 |
$b->setExtensionData( 'bar', 'Barrr!' ); |
| 726 |
$b->appendExtensionData( 'bat', 'xyz' ); |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
yield 'skin control flags' => [ $a, $b, [ |
| 733 |
'getNewSection' => true, |
| 734 |
'getHideNewSection' => true, |
| 735 |
'getNoGallery' => true, |
| 736 |
'getEnableOOUI' => true, |
| 737 |
'getPreventClickjacking' => true, |
| 738 |
'getIndicators' => [ |
| 739 |
'foo' => 'Foo!', |
| 740 |
'bar' => 'Barrr!', |
| 741 |
'zoo' => 'Zoo!', |
| 742 |
], |
| 743 |
'getWrapperDivClass' => 'foo bar', |
| 744 |
'$mExtensionData' => [ |
| 745 |
'foo' => 'Foo!', |
| 746 |
'bar' => 'Barrr!', |
| 747 |
'zoo' => 'Zoo!', |
| 748 |
|
| 749 |
|
| 750 |
'bat' => [ 'abc' => true, 'xyz' => true, '_mw-strategy' => 'union' ], |
| 751 |
], |
| 752 |
] ]; |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
public function testMergeHtmlMetaDataFrom( ParserOutput $a, ParserOutput $b, $expected ) { |
| 764 |
$a->mergeHtmlMetaDataFrom( $b ); |
| 765 |
|
| 766 |
$this->assertFieldValues( $a, $expected ); |
| 767 |
|
| 768 |
|
| 769 |
$a->mergeHtmlMetaDataFrom( $b ); |
| 770 |
|
| 771 |
|
| 772 |
unset( $expected['getSections'] ); |
| 773 |
|
| 774 |
$this->assertFieldValues( $a, $expected ); |
| 775 |
} |
| 776 |
|
| 777 |
private function assertFieldValues( ParserOutput $po, $expected ) { |
| 778 |
$po = TestingAccessWrapper::newFromObject( $po ); |
| 779 |
|
| 780 |
foreach ( $expected as $method => $value ) { |
| 781 |
$canonicalize = false; |
| 782 |
if ( $method[0] === '$' ) { |
| 783 |
$field = substr( $method, 1 ); |
| 784 |
$actual = $po->__get( $field ); |
| 785 |
} elseif ( str_contains( $method, '!' ) ) { |
| 786 |
[ $trimmedMethod, $ignore ] = explode( '!', $method, 2 ); |
| 787 |
$args = $value['_args_'] ?? []; |
| 788 |
unset( $value['_args_'] ); |
| 789 |
$actual = $po->__call( $trimmedMethod, $args ); |
| 790 |
} else { |
| 791 |
$actual = $po->__call( $method, [] ); |
| 792 |
} |
| 793 |
if ( $method === 'getJsConfigVars' ) { |
| 794 |
$canonicalize = true; |
| 795 |
} |
| 796 |
|
| 797 |
if ( $canonicalize ) { |
| 798 |
|
| 799 |
$this->assertEqualsCanonicalizing( $value, $actual, $method ); |
| 800 |
} else { |
| 801 |
$this->assertEquals( $value, $actual, $method ); |
| 802 |
} |
| 803 |
} |
| 804 |
} |
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
public function testAddLink() { |
| 812 |
$a = new ParserOutput(); |
| 813 |
$a->addLink( Title::makeTitle( NS_MAIN, 'Kittens' ), 6 ); |
| 814 |
$a->addLink( new TitleValue( NS_TALK, 'Kittens' ), 16 ); |
| 815 |
$a->addLink( new TitleValue( NS_MAIN, 'Goats_786827346' ) ); |
| 816 |
|
| 817 |
$a->addLink( new TitleValue( NS_TALK, 'Puppies', 'Topic' ), 17 ); |
| 818 |
|
| 819 |
$expected = [ |
| 820 |
NS_MAIN => [ 'Kittens' => 6, 'Goats_786827346' => 0 ], |
| 821 |
NS_TALK => [ 'Kittens' => 16, 'Puppies' => 17 ] |
| 822 |
]; |
| 823 |
$this->assertSame( $expected, $a->getLinks() ); |
| 824 |
$expected = [ |
| 825 |
[ |
| 826 |
'link' => new TitleValue( NS_MAIN, 'Kittens' ), |
| 827 |
'pageid' => 6, |
| 828 |
], |
| 829 |
[ |
| 830 |
'link' => new TitleValue( NS_MAIN, 'Goats_786827346' ), |
| 831 |
'pageid' => 0, |
| 832 |
], |
| 833 |
[ |
| 834 |
'link' => new TitleValue( NS_TALK, 'Kittens' ), |
| 835 |
'pageid' => 16, |
| 836 |
], |
| 837 |
[ |
| 838 |
'link' => new TitleValue( NS_TALK, 'Puppies' ), |
| 839 |
'pageid' => 17, |
| 840 |
], |
| 841 |
]; |
| 842 |
$this->assertEquals( $expected, $a->getLinkList( ParserOutputLinkTypes::LOCAL ) ); |
| 843 |
} |
| 844 |
|
| 845 |
public static function provideMergeTrackingMetaDataFrom() { |
| 846 |
|
| 847 |
$a = new ParserOutput(); |
| 848 |
$a->addLink( Title::makeTitle( NS_MAIN, 'Kittens' ), 6 ); |
| 849 |
$a->addLink( new TitleValue( NS_TALK, 'Kittens' ), 16 ); |
| 850 |
|
| 851 |
$a->addLink( new TitleValue( NS_MAIN, 'Goats', 'Kids' ), 7 ); |
| 852 |
|
| 853 |
$a->addTemplate( Title::makeTitle( NS_TEMPLATE, 'Goats' ), 107, 1107 ); |
| 854 |
|
| 855 |
$a->addLanguageLink( new TitleValue( NS_MAIN, 'de', '', 'de' ) ); |
| 856 |
|
| 857 |
$a->addLanguageLink( new TitleValue( NS_MAIN, 'ru', 'ru', 'ru' ) ); |
| 858 |
$a->addInterwikiLink( Title::makeTitle( NS_MAIN, 'Kittens DE', '', 'de' ) ); |
| 859 |
|
| 860 |
$a->addInterwikiLink( new TitleValue( NS_MAIN, 'Kittens RU', 'ru', 'ru' ) ); |
| 861 |
$a->addExternalLink( 'https://kittens.wikimedia.test' ); |
| 862 |
|
| 863 |
$a->addExternalLink( 'https://goats.wikimedia.test#kids' ); |
| 864 |
|
| 865 |
|
| 866 |
$a->addCategory( new TitleValue( NS_CATEGORY, 'Foo', 'bar' ), 'X' ); |
| 867 |
|
| 868 |
$a->addImage( new TitleValue( NS_FILE, 'Billy.jpg', 'fragment' ), '20180101000013', 'DEAD' ); |
| 869 |
|
| 870 |
$a->addLink( new TitleValue( NS_SPECIAL, 'Version', 'section' ) ); |
| 871 |
|
| 872 |
$b = new ParserOutput(); |
| 873 |
$b->addLink( Title::makeTitle( NS_MAIN, 'Goats' ), 7 ); |
| 874 |
$b->addLink( Title::makeTitle( NS_TALK, 'Goats' ), 17 ); |
| 875 |
$b->addLink( new TitleValue( NS_MAIN, 'Dragons' ), 8 ); |
| 876 |
$b->addLink( new TitleValue( NS_FILE, 'Dragons.jpg' ), 28 ); |
| 877 |
|
| 878 |
|
| 879 |
$b->addTemplate( Title::makeTitle( NS_TEMPLATE, 'Dragons', 'red' ), 108, 1108 ); |
| 880 |
$a->addTemplate( new TitleValue( NS_MAIN, 'Dragons', 'platinum' ), 118, 1118 ); |
| 881 |
|
| 882 |
$b->addLanguageLink( new TitleValue( NS_MAIN, 'fr', '', 'fr' ) ); |
| 883 |
$b->addLanguageLink( new TitleValue( NS_MAIN, 'ru', 'ru', 'ru' ) ); |
| 884 |
$b->addInterwikiLink( Title::makeTitle( NS_MAIN, 'Kittens FR', '', 'fr' ) ); |
| 885 |
$b->addInterwikiLink( new TitleValue( NS_MAIN, 'Dragons RU', '', 'ru' ) ); |
| 886 |
$b->addExternalLink( 'https://dragons.wikimedia.test' ); |
| 887 |
$b->addExternalLink( 'https://goats.wikimedia.test#kids' ); |
| 888 |
|
| 889 |
$b->addCategory( 'Bar', 'Y' ); |
| 890 |
$b->addImage( new TitleValue( NS_FILE, 'Puff.jpg' ), '20180101000017', 'BEEF' ); |
| 891 |
|
| 892 |
yield 'all kinds of links' => [ $a, $b, [ |
| 893 |
'getLinks' => [ |
| 894 |
NS_MAIN => [ |
| 895 |
'Kittens' => 6, |
| 896 |
'Goats' => 7, |
| 897 |
'Dragons' => 8, |
| 898 |
], |
| 899 |
NS_TALK => [ |
| 900 |
'Kittens' => 16, |
| 901 |
'Goats' => 17, |
| 902 |
], |
| 903 |
NS_FILE => [ |
| 904 |
'Dragons.jpg' => 28, |
| 905 |
], |
| 906 |
], |
| 907 |
'getLinkList!LOCAL' => [ |
| 908 |
'_args_' => [ ParserOutputLinkTypes::LOCAL ], |
| 909 |
[ |
| 910 |
'link' => new TitleValue( NS_MAIN, 'Kittens' ), |
| 911 |
'pageid' => 6, |
| 912 |
], |
| 913 |
[ |
| 914 |
'link' => new TitleValue( NS_MAIN, 'Goats' ), |
| 915 |
'pageid' => 7, |
| 916 |
], |
| 917 |
[ |
| 918 |
'link' => new TitleValue( NS_MAIN, 'Dragons' ), |
| 919 |
'pageid' => 8, |
| 920 |
], |
| 921 |
[ |
| 922 |
'link' => new TitleValue( NS_TALK, 'Kittens' ), |
| 923 |
'pageid' => 16, |
| 924 |
], |
| 925 |
[ |
| 926 |
'link' => new TitleValue( NS_TALK, 'Goats' ), |
| 927 |
'pageid' => 17, |
| 928 |
], |
| 929 |
[ |
| 930 |
'link' => new TitleValue( NS_FILE, 'Dragons.jpg' ), |
| 931 |
'pageid' => 28, |
| 932 |
], |
| 933 |
], |
| 934 |
'getTemplates' => [ |
| 935 |
NS_MAIN => [ |
| 936 |
'Dragons' => 118, |
| 937 |
], |
| 938 |
NS_TEMPLATE => [ |
| 939 |
'Dragons' => 108, |
| 940 |
'Goats' => 107, |
| 941 |
], |
| 942 |
], |
| 943 |
'getTemplateIds' => [ |
| 944 |
NS_MAIN => [ |
| 945 |
'Dragons' => 1118, |
| 946 |
], |
| 947 |
NS_TEMPLATE => [ |
| 948 |
'Dragons' => 1108, |
| 949 |
'Goats' => 1107, |
| 950 |
], |
| 951 |
], |
| 952 |
'getLinkList!TEMPLATE' => [ |
| 953 |
'_args_' => [ ParserOutputLinkTypes::TEMPLATE ], |
| 954 |
[ |
| 955 |
'link' => new TitleValue( NS_TEMPLATE, 'Goats' ), |
| 956 |
'pageid' => 107, |
| 957 |
'revid' => 1107, |
| 958 |
], |
| 959 |
[ |
| 960 |
'link' => new TitleValue( NS_TEMPLATE, 'Dragons' ), |
| 961 |
'pageid' => 108, |
| 962 |
'revid' => 1108, |
| 963 |
], |
| 964 |
[ |
| 965 |
'link' => new TitleValue( NS_MAIN, 'Dragons' ), |
| 966 |
'pageid' => 118, |
| 967 |
'revid' => 1118, |
| 968 |
], |
| 969 |
], |
| 970 |
'getLanguageLinks' => [ 'de:de', 'ru:ru#ru', 'fr:fr' ], |
| 971 |
'getLinkList!LANGUAGE' => [ |
| 972 |
'_args_' => [ ParserOutputLinkTypes::LANGUAGE ], |
| 973 |
[ |
| 974 |
'link' => new TitleValue( NS_MAIN, 'de', '', 'de' ), |
| 975 |
], |
| 976 |
[ |
| 977 |
'link' => new TitleValue( NS_MAIN, 'ru', 'ru', 'ru' ), |
| 978 |
], |
| 979 |
[ |
| 980 |
'link' => new TitleValue( NS_MAIN, 'fr', '', 'fr' ), |
| 981 |
], |
| 982 |
], |
| 983 |
'getInterwikiLinks' => [ |
| 984 |
'de' => [ 'Kittens_DE' => 1 ], |
| 985 |
'ru' => [ 'Kittens_RU' => 1, 'Dragons_RU' => 1, ], |
| 986 |
'fr' => [ 'Kittens_FR' => 1 ], |
| 987 |
], |
| 988 |
'getLinkList!INTERWIKI' => [ |
| 989 |
'_args_' => [ ParserOutputLinkTypes::INTERWIKI ], |
| 990 |
[ |
| 991 |
'link' => new TitleValue( NS_MAIN, 'Kittens_DE', '', 'de' ), |
| 992 |
], |
| 993 |
[ |
| 994 |
'link' => new TitleValue( NS_MAIN, 'Kittens_RU', '', 'ru' ), |
| 995 |
], |
| 996 |
[ |
| 997 |
'link' => new TitleValue( NS_MAIN, 'Dragons_RU', '', 'ru' ), |
| 998 |
], |
| 999 |
[ |
| 1000 |
'link' => new TitleValue( NS_MAIN, 'Kittens_FR', '', 'fr' ), |
| 1001 |
], |
| 1002 |
], |
| 1003 |
'getCategoryMap' => [ 'Foo' => 'X', 'Bar' => 'Y' ], |
| 1004 |
'getLinkList!CATEGORY' => [ |
| 1005 |
'_args_' => [ ParserOutputLinkTypes::CATEGORY ], |
| 1006 |
[ |
| 1007 |
'link' => new TitleValue( NS_CATEGORY, 'Foo' ), |
| 1008 |
'sort' => 'X', |
| 1009 |
], |
| 1010 |
[ |
| 1011 |
'link' => new TitleValue( NS_CATEGORY, 'Bar' ), |
| 1012 |
'sort' => 'Y', |
| 1013 |
], |
| 1014 |
], |
| 1015 |
'getImages' => [ 'Billy.jpg' => 1, 'Puff.jpg' => 1 ], |
| 1016 |
'getFileSearchOptions' => [ |
| 1017 |
'Billy.jpg' => [ 'time' => '20180101000013', 'sha1' => 'DEAD' ], |
| 1018 |
'Puff.jpg' => [ 'time' => '20180101000017', 'sha1' => 'BEEF' ], |
| 1019 |
], |
| 1020 |
'getLinkList!MEDIA' => [ |
| 1021 |
'_args_' => [ ParserOutputLinkTypes::MEDIA ], |
| 1022 |
[ |
| 1023 |
'link' => new TitleValue( NS_FILE, 'Billy.jpg' ), |
| 1024 |
'time' => '20180101000013', |
| 1025 |
'sha1' => 'DEAD', |
| 1026 |
], |
| 1027 |
[ |
| 1028 |
'link' => new TitleValue( NS_FILE, 'Puff.jpg' ), |
| 1029 |
'time' => '20180101000017', |
| 1030 |
'sha1' => 'BEEF', |
| 1031 |
], |
| 1032 |
], |
| 1033 |
'getExternalLinks' => [ |
| 1034 |
'https://dragons.wikimedia.test' => 1, |
| 1035 |
'https://kittens.wikimedia.test' => 1, |
| 1036 |
'https://goats.wikimedia.test#kids' => 1, |
| 1037 |
], |
| 1038 |
'getLinkList!SPECIAL' => [ |
| 1039 |
'_args_' => [ ParserOutputLinkTypes::SPECIAL ], |
| 1040 |
[ |
| 1041 |
'link' => new TitleValue( NS_SPECIAL, 'Version' ), |
| 1042 |
], |
| 1043 |
], |
| 1044 |
] ]; |
| 1045 |
|
| 1046 |
|
| 1047 |
$a = new ParserOutput(); |
| 1048 |
|
| 1049 |
$a->setPageProperty( 'foo', 'Foo!' ); |
| 1050 |
$a->setPageProperty( 'bar', 'Bar!' ); |
| 1051 |
|
| 1052 |
$a->setExtensionData( 'foo', 'Foo!' ); |
| 1053 |
$a->setExtensionData( 'bar', 'Bar!' ); |
| 1054 |
$a->appendExtensionData( 'bat', 'abc' ); |
| 1055 |
|
| 1056 |
$b = new ParserOutput(); |
| 1057 |
|
| 1058 |
$b->setPageProperty( 'zoo', 'Zoo!' ); |
| 1059 |
$b->setPageProperty( 'bar', 'Barrr!' ); |
| 1060 |
|
| 1061 |
$b->setExtensionData( 'zoo', 'Zoo!' ); |
| 1062 |
$b->setExtensionData( 'bar', 'Barrr!' ); |
| 1063 |
$b->appendExtensionData( 'bat', 'xyz' ); |
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
yield 'properties' => [ $a, $b, [ |
| 1070 |
'getPageProperties' => [ |
| 1071 |
'foo' => 'Foo!', |
| 1072 |
'bar' => 'Barrr!', |
| 1073 |
'zoo' => 'Zoo!', |
| 1074 |
], |
| 1075 |
'$mExtensionData' => [ |
| 1076 |
'foo' => 'Foo!', |
| 1077 |
'bar' => 'Barrr!', |
| 1078 |
'zoo' => 'Zoo!', |
| 1079 |
|
| 1080 |
|
| 1081 |
'bat' => [ 'abc' => true, 'xyz' => true, '_mw-strategy' => 'union' ], |
| 1082 |
], |
| 1083 |
] ]; |
| 1084 |
} |
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
public function testMergeTrackingMetaDataFrom( ParserOutput $a, ParserOutput $b, $expected ) { |
| 1095 |
$a->mergeTrackingMetaDataFrom( $b ); |
| 1096 |
|
| 1097 |
$this->assertFieldValues( $a, $expected ); |
| 1098 |
|
| 1099 |
|
| 1100 |
$a->mergeTrackingMetaDataFrom( $b ); |
| 1101 |
|
| 1102 |
$this->assertFieldValues( $a, $expected ); |
| 1103 |
} |
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
public function testCollectMetaData( ParserOutput $a, ParserOutput $b, $expected ) { |
| 1114 |
$b->collectMetadata( $a ); |
| 1115 |
|
| 1116 |
$this->assertFieldValues( $a, $expected ); |
| 1117 |
} |
| 1118 |
|
| 1119 |
public function provideMergeInternalMetaDataFrom() { |
| 1120 |
$this->filterDeprecated( '/^.*CacheTime::setCacheTime called with -1 as an argument/' ); |
| 1121 |
|
| 1122 |
|
| 1123 |
$a = new ParserOutput(); |
| 1124 |
|
| 1125 |
$a->addWarningMsg( 'duplicate-args-warning', 'A', 'B', 'C' ); |
| 1126 |
$a->addWarningMsg( 'template-loop-warning', 'D' ); |
| 1127 |
|
| 1128 |
$a->setOutputFlag( 'foo' ); |
| 1129 |
$a->setOutputFlag( 'bar' ); |
| 1130 |
|
| 1131 |
$a->recordOption( 'Foo' ); |
| 1132 |
$a->recordOption( 'Bar' ); |
| 1133 |
|
| 1134 |
$b = new ParserOutput(); |
| 1135 |
|
| 1136 |
$b->addWarningMsg( 'template-equals-warning' ); |
| 1137 |
$b->addWarningMsg( 'template-loop-warning', 'D' ); |
| 1138 |
|
| 1139 |
$b->setOutputFlag( 'zoo' ); |
| 1140 |
$b->setOutputFlag( 'bar' ); |
| 1141 |
|
| 1142 |
$b->recordOption( 'Zoo' ); |
| 1143 |
$b->recordOption( 'Bar' ); |
| 1144 |
|
| 1145 |
yield 'flags' => [ $a, $b, [ |
| 1146 |
'getWarnings' => [ |
| 1147 |
wfMessage( 'duplicate-args-warning', 'A', 'B', 'C' )->text(), |
| 1148 |
wfMessage( 'template-loop-warning', 'D' )->text(), |
| 1149 |
wfMessage( 'template-equals-warning' )->text(), |
| 1150 |
], |
| 1151 |
'$mFlags' => [ 'foo' => true, 'bar' => true, 'zoo' => true ], |
| 1152 |
'getUsedOptions' => [ 'Foo', 'Bar', 'Zoo' ], |
| 1153 |
] ]; |
| 1154 |
|
| 1155 |
|
| 1156 |
$someTime = "20240207202040"; |
| 1157 |
$someLaterTime = "20240207202112"; |
| 1158 |
$a = new ParserOutput(); |
| 1159 |
$a->setCacheTime( $someTime ); |
| 1160 |
$b = new ParserOutput(); |
| 1161 |
yield 'only left cache time' => [ $a, $b, [ 'getCacheTime' => $someTime ] ]; |
| 1162 |
|
| 1163 |
$a = new ParserOutput(); |
| 1164 |
$b = new ParserOutput(); |
| 1165 |
$b->setCacheTime( $someTime ); |
| 1166 |
yield 'only right cache time' => [ $a, $b, [ 'getCacheTime' => $someTime ] ]; |
| 1167 |
|
| 1168 |
$a = new ParserOutput(); |
| 1169 |
$b = new ParserOutput(); |
| 1170 |
$a->setCacheTime( $someLaterTime ); |
| 1171 |
$b->setCacheTime( $someTime ); |
| 1172 |
yield 'left has later cache time' => [ $a, $b, [ 'getCacheTime' => $someLaterTime ] ]; |
| 1173 |
|
| 1174 |
$a = new ParserOutput(); |
| 1175 |
$b = new ParserOutput(); |
| 1176 |
$a->setCacheTime( $someTime ); |
| 1177 |
$b->setCacheTime( $someLaterTime ); |
| 1178 |
yield 'right has later cache time' => [ $a, $b, [ 'getCacheTime' => $someLaterTime ] ]; |
| 1179 |
|
| 1180 |
$a = new ParserOutput(); |
| 1181 |
$b = new ParserOutput(); |
| 1182 |
$a->setCacheTime( -1 ); |
| 1183 |
$b->setCacheTime( $someTime ); |
| 1184 |
yield 'left is uncacheable' => [ $a, $b, [ 'getCacheTime' => "-1" ] ]; |
| 1185 |
|
| 1186 |
$a = new ParserOutput(); |
| 1187 |
$b = new ParserOutput(); |
| 1188 |
$a->setCacheTime( $someTime ); |
| 1189 |
$b->setCacheTime( -1 ); |
| 1190 |
yield 'right is uncacheable' => [ $a, $b, [ 'getCacheTime' => "-1" ] ]; |
| 1191 |
|
| 1192 |
|
| 1193 |
$a = new ParserOutput(); |
| 1194 |
$a->setRevisionTimestamp( '20180101000011' ); |
| 1195 |
$b = new ParserOutput(); |
| 1196 |
yield 'only left timestamp' => [ $a, $b, [ 'getTimestamp' => '20180101000011' ] ]; |
| 1197 |
|
| 1198 |
$a = new ParserOutput(); |
| 1199 |
$b = new ParserOutput(); |
| 1200 |
$b->setRevisionTimestamp( '20180101000011' ); |
| 1201 |
yield 'only right timestamp' => [ $a, $b, [ 'getTimestamp' => '20180101000011' ] ]; |
| 1202 |
|
| 1203 |
$a = new ParserOutput(); |
| 1204 |
$a->setRevisionTimestamp( '20180101000011' ); |
| 1205 |
$b = new ParserOutput(); |
| 1206 |
$b->setRevisionTimestamp( '20180101000001' ); |
| 1207 |
yield 'left timestamp wins' => [ $a, $b, [ 'getTimestamp' => '20180101000011' ] ]; |
| 1208 |
|
| 1209 |
$a = new ParserOutput(); |
| 1210 |
$a->setRevisionTimestamp( '20180101000001' ); |
| 1211 |
$b = new ParserOutput(); |
| 1212 |
$b->setRevisionTimestamp( '20180101000011' ); |
| 1213 |
yield 'right timestamp wins' => [ $a, $b, [ 'getTimestamp' => '20180101000011' ] ]; |
| 1214 |
|
| 1215 |
|
| 1216 |
$a = new ParserOutput(); |
| 1217 |
$a->setSpeculativeRevIdUsed( 9 ); |
| 1218 |
$b = new ParserOutput(); |
| 1219 |
yield 'only left speculative rev id' => [ $a, $b, [ 'getSpeculativeRevIdUsed' => 9 ] ]; |
| 1220 |
|
| 1221 |
$a = new ParserOutput(); |
| 1222 |
$b = new ParserOutput(); |
| 1223 |
$b->setSpeculativeRevIdUsed( 9 ); |
| 1224 |
yield 'only right speculative rev id' => [ $a, $b, [ 'getSpeculativeRevIdUsed' => 9 ] ]; |
| 1225 |
|
| 1226 |
$a = new ParserOutput(); |
| 1227 |
$a->setSpeculativeRevIdUsed( 9 ); |
| 1228 |
$b = new ParserOutput(); |
| 1229 |
$b->setSpeculativeRevIdUsed( 9 ); |
| 1230 |
yield 'same speculative rev id' => [ $a, $b, [ 'getSpeculativeRevIdUsed' => 9 ] ]; |
| 1231 |
|
| 1232 |
|
| 1233 |
$a = new ParserOutput(); |
| 1234 |
|
| 1235 |
$a->setLimitReportData( 'naive1', 7 ); |
| 1236 |
$a->setLimitReportData( 'naive2', 27 ); |
| 1237 |
|
| 1238 |
$a->setLimitReportData( 'limitreport-simple1', 7 ); |
| 1239 |
$a->setLimitReportData( 'limitreport-simple2', 27 ); |
| 1240 |
|
| 1241 |
$a->setLimitReportData( 'limitreport-pair1', [ 7, 9 ] ); |
| 1242 |
$a->setLimitReportData( 'limitreport-pair2', [ 27, 29 ] ); |
| 1243 |
|
| 1244 |
$a->setLimitReportData( 'limitreport-more1', [ 7, 9, 1 ] ); |
| 1245 |
$a->setLimitReportData( 'limitreport-more2', [ 27, 29, 21 ] ); |
| 1246 |
|
| 1247 |
$a->setLimitReportData( 'limitreport-only-a', 13 ); |
| 1248 |
|
| 1249 |
$b = new ParserOutput(); |
| 1250 |
|
| 1251 |
$b->setLimitReportData( 'naive1', 17 ); |
| 1252 |
$b->setLimitReportData( 'naive2', 17 ); |
| 1253 |
|
| 1254 |
$b->setLimitReportData( 'limitreport-simple1', 17 ); |
| 1255 |
$b->setLimitReportData( 'limitreport-simple2', 17 ); |
| 1256 |
|
| 1257 |
$b->setLimitReportData( 'limitreport-pair1', [ 17, 19 ] ); |
| 1258 |
$b->setLimitReportData( 'limitreport-pair2', [ 17, 19 ] ); |
| 1259 |
|
| 1260 |
$b->setLimitReportData( 'limitreport-more1', [ 17, 19, 11 ] ); |
| 1261 |
$b->setLimitReportData( 'limitreport-more2', [ 17, 19, 11 ] ); |
| 1262 |
|
| 1263 |
$b->setLimitReportData( 'limitreport-only-b', 23 ); |
| 1264 |
|
| 1265 |
|
| 1266 |
yield 'limit report' => [ $a, $b, [ |
| 1267 |
'getLimitReportData' => [ |
| 1268 |
'naive1' => 7, |
| 1269 |
'naive2' => 27, |
| 1270 |
'limitreport-simple1' => 7, |
| 1271 |
'limitreport-simple2' => 27, |
| 1272 |
'limitreport-pair1' => [ 7, 9 ], |
| 1273 |
'limitreport-pair2' => [ 27, 29 ], |
| 1274 |
'limitreport-more1' => [ 7, 9, 1 ], |
| 1275 |
'limitreport-more2' => [ 27, 29, 21 ], |
| 1276 |
'limitreport-only-a' => 13, |
| 1277 |
], |
| 1278 |
'getLimitReportJSData' => [ |
| 1279 |
'naive1' => 7, |
| 1280 |
'naive2' => 27, |
| 1281 |
'limitreport' => [ |
| 1282 |
'simple1' => 7, |
| 1283 |
'simple2' => 27, |
| 1284 |
'pair1' => [ 'value' => 7, 'limit' => 9 ], |
| 1285 |
'pair2' => [ 'value' => 27, 'limit' => 29 ], |
| 1286 |
'more1' => [ 7, 9, 1 ], |
| 1287 |
'more2' => [ 27, 29, 21 ], |
| 1288 |
'only-a' => 13, |
| 1289 |
], |
| 1290 |
], |
| 1291 |
] ]; |
| 1292 |
|
| 1293 |
MWDebug::clearDeprecationFilters(); |
| 1294 |
} |
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
public function testMergeInternalMetaDataFrom( ParserOutput $a, ParserOutput $b, $expected ) { |
| 1305 |
$this->filterDeprecated( '/^.*CacheTime::setCacheTime called with -1 as an argument/' ); |
| 1306 |
$a->mergeInternalMetaDataFrom( $b ); |
| 1307 |
|
| 1308 |
$this->assertFieldValues( $a, $expected ); |
| 1309 |
|
| 1310 |
|
| 1311 |
$a->mergeInternalMetaDataFrom( $b ); |
| 1312 |
|
| 1313 |
$this->assertFieldValues( $a, $expected ); |
| 1314 |
} |
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
public function testMergeInternalMetaDataFrom_parseStartTime() { |
| 1322 |
|
| 1323 |
$a = new ParserOutput(); |
| 1324 |
$a = TestingAccessWrapper::newFromObject( $a ); |
| 1325 |
|
| 1326 |
$a->resetParseStartTime(); |
| 1327 |
$aClocks = $a->mParseStartTime; |
| 1328 |
|
| 1329 |
$b = new ParserOutput(); |
| 1330 |
|
| 1331 |
$a->mergeInternalMetaDataFrom( $b ); |
| 1332 |
$mergedClocks = $a->mParseStartTime; |
| 1333 |
|
| 1334 |
foreach ( $mergedClocks as $clock => $timestamp ) { |
| 1335 |
$this->assertSame( $aClocks[$clock], $timestamp, $clock ); |
| 1336 |
} |
| 1337 |
|
| 1338 |
|
| 1339 |
usleep( 1234 ); |
| 1340 |
|
| 1341 |
|
| 1342 |
$b = new ParserOutput(); |
| 1343 |
$b = TestingAccessWrapper::newFromObject( $b ); |
| 1344 |
|
| 1345 |
$b->resetParseStartTime(); |
| 1346 |
|
| 1347 |
$bClocks = $b->mParseStartTime; |
| 1348 |
|
| 1349 |
$a->mergeInternalMetaDataFrom( $b->object ); |
| 1350 |
$mergedClocks = $a->mParseStartTime; |
| 1351 |
|
| 1352 |
foreach ( $mergedClocks as $clock => $timestamp ) { |
| 1353 |
$this->assertSame( $aClocks[$clock], $timestamp, $clock ); |
| 1354 |
$this->assertLessThanOrEqual( $bClocks[$clock], $timestamp, $clock ); |
| 1355 |
} |
| 1356 |
|
| 1357 |
|
| 1358 |
usleep( 1234 ); |
| 1359 |
$a->resetParseStartTime(); |
| 1360 |
$aClocks = $a->mParseStartTime; |
| 1361 |
|
| 1362 |
$a->mergeInternalMetaDataFrom( $b->object ); |
| 1363 |
$mergedClocks = $a->mParseStartTime; |
| 1364 |
|
| 1365 |
foreach ( $mergedClocks as $clock => $timestamp ) { |
| 1366 |
$this->assertSame( $bClocks[$clock], $timestamp, $clock ); |
| 1367 |
$this->assertLessThanOrEqual( $aClocks[$clock], $timestamp, $clock ); |
| 1368 |
} |
| 1369 |
|
| 1370 |
|
| 1371 |
$a = new ParserOutput(); |
| 1372 |
$a = TestingAccessWrapper::newFromObject( $a ); |
| 1373 |
|
| 1374 |
$a->mergeInternalMetaDataFrom( $b->object ); |
| 1375 |
$mergedClocks = $a->mParseStartTime; |
| 1376 |
|
| 1377 |
foreach ( $mergedClocks as $clock => $timestamp ) { |
| 1378 |
$this->assertSame( $bClocks[$clock], $timestamp, $clock ); |
| 1379 |
} |
| 1380 |
} |
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
public function testMergeInternalMetaDataFrom_timeProfile() { |
| 1390 |
|
| 1391 |
$a = new ParserOutput(); |
| 1392 |
$a = TestingAccessWrapper::newFromObject( $a ); |
| 1393 |
|
| 1394 |
$a->resetParseStartTime(); |
| 1395 |
usleep( 1234 ); |
| 1396 |
$a->recordTimeProfile(); |
| 1397 |
|
| 1398 |
$aClocks = $a->mTimeProfile; |
| 1399 |
|
| 1400 |
|
| 1401 |
usleep( 1234 ); |
| 1402 |
$a->recordTimeProfile(); |
| 1403 |
|
| 1404 |
foreach ( $aClocks as $clock => $duration ) { |
| 1405 |
$this->assertNotNull( $duration ); |
| 1406 |
$this->assertGreaterThan( 0, $duration ); |
| 1407 |
$this->assertSame( $aClocks[$clock], $a->getTimeProfile( $clock ) ); |
| 1408 |
} |
| 1409 |
|
| 1410 |
$b = new ParserOutput(); |
| 1411 |
|
| 1412 |
$a->mergeInternalMetaDataFrom( $b ); |
| 1413 |
$mergedClocks = $a->mTimeProfile; |
| 1414 |
|
| 1415 |
foreach ( $mergedClocks as $clock => $duration ) { |
| 1416 |
$this->assertSame( $aClocks[$clock], $duration, $clock ); |
| 1417 |
} |
| 1418 |
|
| 1419 |
|
| 1420 |
$b->resetParseStartTime(); |
| 1421 |
usleep( 1234 ); |
| 1422 |
$b->recordTimeProfile(); |
| 1423 |
|
| 1424 |
$b = TestingAccessWrapper::newFromObject( $b ); |
| 1425 |
$bClocks = $b->mTimeProfile; |
| 1426 |
|
| 1427 |
$a->mergeInternalMetaDataFrom( $b->object ); |
| 1428 |
$mergedClocks = $a->mTimeProfile; |
| 1429 |
|
| 1430 |
foreach ( $mergedClocks as $clock => $duration ) { |
| 1431 |
$this->assertGreaterThanOrEqual( $aClocks[$clock], $duration, $clock ); |
| 1432 |
$this->assertGreaterThanOrEqual( $bClocks[$clock], $duration, $clock ); |
| 1433 |
} |
| 1434 |
} |
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
public function testGetCacheTime() { |
| 1441 |
$clock = MWTimestamp::convert( TS_UNIX, '20100101000000' ); |
| 1442 |
MWTimestamp::setFakeTime( static function () use ( &$clock ) { |
| 1443 |
return $clock++; |
| 1444 |
} ); |
| 1445 |
|
| 1446 |
$po = new ParserOutput(); |
| 1447 |
$time = $po->getCacheTime(); |
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
$this->assertStringStartsWith( '2010010100000', $time ); |
| 1452 |
$this->assertSame( $time, $po->getCacheTime() ); |
| 1453 |
|
| 1454 |
|
| 1455 |
$time = '20110606112233'; |
| 1456 |
$po->setCacheTime( $time ); |
| 1457 |
$this->assertSame( $time, $po->getCacheTime() ); |
| 1458 |
} |
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
public function testCSPSources() { |
| 1469 |
$po = new ParserOutput; |
| 1470 |
|
| 1471 |
$this->assertEquals( [], $po->getExtraCSPScriptSrcs(), 'empty Script' ); |
| 1472 |
$this->assertEquals( [], $po->getExtraCSPStyleSrcs(), 'empty Style' ); |
| 1473 |
$this->assertEquals( [], $po->getExtraCSPDefaultSrcs(), 'empty Default' ); |
| 1474 |
|
| 1475 |
$po->addExtraCSPScriptSrc( 'foo.com' ); |
| 1476 |
$po->addExtraCSPScriptSrc( 'bar.com' ); |
| 1477 |
$po->addExtraCSPDefaultSrc( 'baz.com' ); |
| 1478 |
$po->addExtraCSPStyleSrc( 'fred.com' ); |
| 1479 |
$po->addExtraCSPStyleSrc( 'xyzzy.com' ); |
| 1480 |
|
| 1481 |
$this->assertEquals( [ 'foo.com', 'bar.com' ], $po->getExtraCSPScriptSrcs(), 'Script' ); |
| 1482 |
$this->assertEquals( [ 'baz.com' ], $po->getExtraCSPDefaultSrcs(), 'Default' ); |
| 1483 |
$this->assertEquals( [ 'fred.com', 'xyzzy.com' ], $po->getExtraCSPStyleSrcs(), 'Style' ); |
| 1484 |
} |
| 1485 |
|
| 1486 |
public function testOutputStrings() { |
| 1487 |
$po = new ParserOutput; |
| 1488 |
|
| 1489 |
$this->assertEquals( [], $po->getOutputStrings( ParserOutputStringSets::MODULE ) ); |
| 1490 |
$this->assertEquals( [], $po->getOutputStrings( ParserOutputStringSets::MODULE_STYLE ) ); |
| 1491 |
$this->assertEquals( [], $po->getOutputStrings( ParserOutputStringSets::EXTRA_CSP_SCRIPT_SRC ) ); |
| 1492 |
$this->assertEquals( [], $po->getOutputStrings( ParserOutputStringSets::EXTRA_CSP_STYLE_SRC ) ); |
| 1493 |
$this->assertEquals( [], $po->getOutputStrings( ParserOutputStringSets::EXTRA_CSP_DEFAULT_SRC ) ); |
| 1494 |
|
| 1495 |
$this->assertEquals( [], $po->getModules() ); |
| 1496 |
$this->assertEquals( [], $po->getModuleStyles() ); |
| 1497 |
$this->assertEquals( [], $po->getExtraCSPScriptSrcs() ); |
| 1498 |
$this->assertEquals( [], $po->getExtraCSPStyleSrcs() ); |
| 1499 |
$this->assertEquals( [], $po->getExtraCSPDefaultSrcs() ); |
| 1500 |
|
| 1501 |
$po->appendOutputStrings( ParserOutputStringSets::MODULE, [ 'a' ] ); |
| 1502 |
$po->appendOutputStrings( ParserOutputStringSets::MODULE_STYLE, [ 'b' ] ); |
| 1503 |
$po->appendOutputStrings( ParserOutputStringSets::EXTRA_CSP_SCRIPT_SRC, [ 'foo.com', 'bar.com' ] ); |
| 1504 |
$po->appendOutputStrings( ParserOutputStringSets::EXTRA_CSP_DEFAULT_SRC, [ 'baz.com' ] ); |
| 1505 |
$po->appendOutputStrings( ParserOutputStringSets::EXTRA_CSP_STYLE_SRC, [ 'fred.com' ] ); |
| 1506 |
$po->appendOutputStrings( ParserOutputStringSets::EXTRA_CSP_STYLE_SRC, [ 'xyzzy.com' ] ); |
| 1507 |
|
| 1508 |
$this->assertEquals( [ 'a' ], $po->getOutputStrings( ParserOutputStringSets::MODULE ) ); |
| 1509 |
$this->assertEquals( [ 'b' ], $po->getOutputStrings( ParserOutputStringSets::MODULE_STYLE ) ); |
| 1510 |
$this->assertEquals( [ 'foo.com', 'bar.com' ], |
| 1511 |
$po->getOutputStrings( ParserOutputStringSets::EXTRA_CSP_SCRIPT_SRC ) ); |
| 1512 |
$this->assertEquals( [ 'baz.com' ], |
| 1513 |
$po->getOutputStrings( ParserOutputStringSets::EXTRA_CSP_DEFAULT_SRC ) ); |
| 1514 |
$this->assertEquals( [ 'fred.com', 'xyzzy.com' ], |
| 1515 |
$po->getOutputStrings( ParserOutputStringSets::EXTRA_CSP_STYLE_SRC ) ); |
| 1516 |
|
| 1517 |
$this->assertEquals( [ 'a' ], $po->getModules() ); |
| 1518 |
$this->assertEquals( [ 'b' ], $po->getModuleStyles() ); |
| 1519 |
$this->assertEquals( [ 'foo.com', 'bar.com' ], $po->getExtraCSPScriptSrcs() ); |
| 1520 |
$this->assertEquals( [ 'baz.com' ], $po->getExtraCSPDefaultSrcs() ); |
| 1521 |
$this->assertEquals( [ 'fred.com', 'xyzzy.com' ], $po->getExtraCSPStyleSrcs() ); |
| 1522 |
} |
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
public function testCacheTime() { |
| 1529 |
$po = new ParserOutput(); |
| 1530 |
|
| 1531 |
|
| 1532 |
$this->assertFalse( $po->hasCacheTime() ); |
| 1533 |
|
| 1534 |
$po->getCacheTime(); |
| 1535 |
$this->assertTrue( $po->hasCacheTime() ); |
| 1536 |
|
| 1537 |
$po->setCacheTime( "20240207202040" ); |
| 1538 |
$this->assertSame( "20240207202040", $po->getCacheTime() ); |
| 1539 |
} |
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
public function testRenderId() { |
| 1546 |
$po = new ParserOutput(); |
| 1547 |
|
| 1548 |
|
| 1549 |
$this->assertNull( $po->getRenderId() ); |
| 1550 |
|
| 1551 |
|
| 1552 |
$po->setRenderId( "TestRenderId" ); |
| 1553 |
$this->assertEquals( "TestRenderId", $po->getRenderId() ); |
| 1554 |
} |
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
public function testRenderIdBackCompat() { |
| 1560 |
$po = new ParserOutput(); |
| 1561 |
|
| 1562 |
|
| 1563 |
$po->setExtensionData( 'parsoid-render-id', "1234/LegacyRenderId" ); |
| 1564 |
$this->assertEquals( "LegacyRenderId", $po->getRenderId() ); |
| 1565 |
} |
| 1566 |
|
| 1567 |
public function testSetFromParserOptions() { |
| 1568 |
|
| 1569 |
$pOptions = ParserOptions::newFromAnon(); |
| 1570 |
$pOutput = new ParserOutput; |
| 1571 |
$pOutput->setFromParserOptions( $pOptions ); |
| 1572 |
$this->assertSame( 'mw-parser-output', $pOutput->getWrapperDivClass() ); |
| 1573 |
$this->assertFalse( $pOutput->getOutputFlag( ParserOutputFlags::IS_PREVIEW ) ); |
| 1574 |
$this->assertTrue( $pOutput->isCacheable() ); |
| 1575 |
$this->assertFalse( $pOutput->getOutputFlag( ParserOutputFlags::NO_SECTION_EDIT_LINKS ) ); |
| 1576 |
$this->assertFalse( $pOutput->getOutputFlag( ParserOutputFlags::COLLAPSIBLE_SECTIONS ) ); |
| 1577 |
|
| 1578 |
|
| 1579 |
$pOptions->setWrapOutputClass( 'test-wrapper' ); |
| 1580 |
$pOptions->setIsPreview( true ); |
| 1581 |
$pOptions->setSuppressSectionEditLinks(); |
| 1582 |
$pOptions->setCollapsibleSections(); |
| 1583 |
$pOutput = new ParserOutput; |
| 1584 |
$pOutput->setFromParserOptions( $pOptions ); |
| 1585 |
$this->assertEquals( 'test-wrapper', $pOutput->getWrapperDivClass() ); |
| 1586 |
$this->assertTrue( $pOutput->getOutputFlag( ParserOutputFlags::IS_PREVIEW ) ); |
| 1587 |
$this->assertFalse( $pOutput->isCacheable() ); |
| 1588 |
$this->assertTrue( $pOutput->getOutputFlag( ParserOutputFlags::NO_SECTION_EDIT_LINKS ) ); |
| 1589 |
$this->assertTrue( $pOutput->getOutputFlag( ParserOutputFlags::COLLAPSIBLE_SECTIONS ) ); |
| 1590 |
} |
| 1591 |
} |