2 use Wikimedia\TestingAccessWrapper
;
6 * ^--- trigger DB shadowing because we are using Title magic
8 class ParserOutputTest
extends MediaWikiLangTestCase
{
10 public static function provideIsLinkInternal() {
13 [ false, 'http://example.org', 'http://mediawiki.org' ],
15 [ true, 'http://example.org', 'http://example.org' ],
16 [ true, 'https://example.org', 'https://example.org' ],
17 [ true, '//example.org', '//example.org' ],
18 // Same domain different cases
19 [ true, 'http://example.org', 'http://EXAMPLE.ORG' ],
20 // Paths, queries, and fragments are not relevant
21 [ true, 'http://example.org', 'http://example.org/wiki/Main_Page' ],
22 [ true, 'http://example.org', 'http://example.org?my=query' ],
23 [ true, 'http://example.org', 'http://example.org#its-a-fragment' ],
24 // Different protocols
25 [ false, 'http://example.org', 'https://example.org' ],
26 [ false, 'https://example.org', 'http://example.org' ],
27 // Protocol relative servers always match http and https links
28 [ true, '//example.org', 'http://example.org' ],
29 [ true, '//example.org', 'https://example.org' ],
30 // But they don't match strange things like this
31 [ false, '//example.org', 'irc://example.org' ],
36 * Test to make sure ParserOutput::isLinkInternal behaves properly
37 * @dataProvider provideIsLinkInternal
38 * @covers ParserOutput::isLinkInternal
40 public function testIsLinkInternal( $shouldMatch, $server, $url ) {
41 $this->assertEquals( $shouldMatch, ParserOutput
::isLinkInternal( $server, $url ) );
45 * @covers ParserOutput::setExtensionData
46 * @covers ParserOutput::getExtensionData
48 public function testExtensionData() {
49 $po = new ParserOutput();
51 $po->setExtensionData( "one", "Foo" );
53 $this->assertEquals( "Foo", $po->getExtensionData( "one" ) );
54 $this->assertNull( $po->getExtensionData( "spam" ) );
56 $po->setExtensionData( "two", "Bar" );
57 $this->assertEquals( "Foo", $po->getExtensionData( "one" ) );
58 $this->assertEquals( "Bar", $po->getExtensionData( "two" ) );
60 $po->setExtensionData( "one", null );
61 $this->assertNull( $po->getExtensionData( "one" ) );
62 $this->assertEquals( "Bar", $po->getExtensionData( "two" ) );
66 * @covers ParserOutput::setProperty
67 * @covers ParserOutput::getProperty
68 * @covers ParserOutput::unsetProperty
69 * @covers ParserOutput::getProperties
71 public function testProperties() {
72 $po = new ParserOutput();
74 $po->setProperty( 'foo', 'val' );
76 $properties = $po->getProperties();
77 $this->assertEquals( $po->getProperty( 'foo' ), 'val' );
78 $this->assertEquals( $properties['foo'], 'val' );
80 $po->setProperty( 'foo', 'second val' );
82 $properties = $po->getProperties();
83 $this->assertEquals( $po->getProperty( 'foo' ), 'second val' );
84 $this->assertEquals( $properties['foo'], 'second val' );
86 $po->unsetProperty( 'foo' );
88 $properties = $po->getProperties();
89 $this->assertEquals( $po->getProperty( 'foo' ), false );
90 $this->assertArrayNotHasKey( 'foo', $properties );
94 * @covers ParserOutput::getWrapperDivClass
95 * @covers ParserOutput::addWrapperDivClass
96 * @covers ParserOutput::clearWrapperDivClass
97 * @covers ParserOutput::getText
99 public function testWrapperDivClass() {
100 $po = new ParserOutput();
102 $po->setText( 'Kittens' );
103 $this->assertContains( 'Kittens', $po->getText() );
104 $this->assertNotContains( '<div', $po->getText() );
105 $this->assertSame( 'Kittens', $po->getRawText() );
107 $po->addWrapperDivClass( 'foo' );
108 $text = $po->getText();
109 $this->assertContains( 'Kittens', $text );
110 $this->assertContains( '<div', $text );
111 $this->assertContains( 'class="foo"', $text );
113 $po->addWrapperDivClass( 'bar' );
114 $text = $po->getText();
115 $this->assertContains( 'Kittens', $text );
116 $this->assertContains( '<div', $text );
117 $this->assertContains( 'class="foo bar"', $text );
119 $po->addWrapperDivClass( 'bar' ); // second time does nothing, no "foo bar bar".
120 $text = $po->getText( [ 'unwrap' => true ] );
121 $this->assertContains( 'Kittens', $text );
122 $this->assertNotContains( '<div', $text );
123 $this->assertNotContains( 'class="foo bar"', $text );
125 $text = $po->getText( [ 'wrapperDivClass' => '' ] );
126 $this->assertContains( 'Kittens', $text );
127 $this->assertNotContains( '<div', $text );
128 $this->assertNotContains( 'class="foo bar"', $text );
130 $text = $po->getText( [ 'wrapperDivClass' => 'xyzzy' ] );
131 $this->assertContains( 'Kittens', $text );
132 $this->assertContains( '<div', $text );
133 $this->assertContains( 'class="xyzzy"', $text );
134 $this->assertNotContains( 'class="foo bar"', $text );
136 $text = $po->getRawText();
137 $this->assertSame( 'Kittens', $text );
139 $po->clearWrapperDivClass();
140 $text = $po->getText();
141 $this->assertContains( 'Kittens', $text );
142 $this->assertNotContains( '<div', $text );
143 $this->assertNotContains( 'class="foo bar"', $text );
146 public function testT203716() {
147 // simulate extra wrapping from old parser cache
148 $out = new ParserOutput( '<div class="mw-parser-output">Foo</div>' );
149 $out = unserialize( serialize( $out ) );
151 $plainText = $out->getText( [ 'unwrap' => true ] );
152 $wrappedText = $out->getText( [ 'unwrap' => false ] );
153 $wrappedText2 = $out->getText( [ 'wrapperDivClass' => 'mw-parser-output' ] );
155 $this->assertNotContains( '<div', $plainText );
156 $this->assertContains( '<div', $wrappedText );
157 $this->assertStringNotMatchesFormat( '<div%s<div%s', $wrappedText );
158 $this->assertContains( '<div', $wrappedText2 );
159 $this->assertStringNotMatchesFormat( '<div%s<div%s', $wrappedText2 );
161 // simulate ParserOuput creation by new parser code
162 $out = new ParserOutput( 'Foo' );
163 $out->addWrapperDivClass( 'mw-parser-outout' );
164 $out = unserialize( serialize( $out ) );
166 $plainText = $out->getText( [ 'unwrap' => true ] );
167 $wrappedText = $out->getText( [ 'unwrap' => false ] );
168 $wrappedText2 = $out->getText( [ 'wrapperDivClass' => 'mw-parser-output' ] );
170 $this->assertNotContains( '<div', $plainText );
171 $this->assertContains( '<div', $wrappedText );
172 $this->assertStringNotMatchesFormat( '<div%s<div%s', $wrappedText );
173 $this->assertContains( '<div', $wrappedText2 );
174 $this->assertStringNotMatchesFormat( '<div%s<div%s', $wrappedText2 );
178 * @covers ParserOutput::getText
179 * @dataProvider provideGetText
180 * @param array $options Options to getText()
181 * @param string $text Parser text
182 * @param string $expect Expected output
184 public function testGetText( $options, $text, $expect ) {
185 $this->setMwGlobals( [
186 'wgArticlePath' => '/wiki/$1',
187 'wgScriptPath' => '/w',
188 'wgScript' => '/w/index.php',
191 $po = new ParserOutput( $text );
192 $actual = $po->getText( $options );
193 $this->assertSame( $expect, $actual );
196 public static function provideGetText() {
197 // phpcs:disable Generic.Files.LineLength
201 <mw:toc><div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
203 <li class="toclevel-1 tocsection-1"><a href="#Section_1"><span class="tocnumber">1</span> <span class="toctext">Section 1</span></a></li>
204 <li class="toclevel-1 tocsection-2"><a href="#Section_2"><span class="tocnumber">2</span> <span class="toctext">Section 2</span></a>
206 <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>
209 <li class="toclevel-1 tocsection-4"><a href="#Section_3"><span class="tocnumber">3</span> <span class="toctext">Section 3</span></a></li>
213 <h2><span class="mw-headline" id="Section_1">Section 1</span><mw:editsection page="Test Page" section="1">Section 1</mw:editsection></h2>
216 <h2><span class="mw-headline" id="Section_2">Section 2</span><mw:editsection page="Test Page" section="2">Section 2</mw:editsection></h2>
219 <h3><span class="mw-headline" id="Section_2.1">Section 2.1</span><mw:editsection page="Test Page" section="3">Section 2.1</mw:editsection></h3>
222 <h2><span class="mw-headline" id="Section_3">Section 3</span><mw:editsection page="Test Page" section="4">Section 3</mw:editsection></h2>
228 <p>This is a test document.</p>
229 <style data-mw-deduplicate="duplicate1">.Duplicate1 {}</style>
230 <style data-mw-deduplicate="duplicate1">.Duplicate1 {}</style>
231 <style data-mw-deduplicate="duplicate2">.Duplicate2 {}</style>
232 <style data-mw-deduplicate="duplicate1">.Duplicate1 {}</style>
233 <style data-mw-deduplicate="duplicate2">.Duplicate2 {}</style>
234 <style data-mw-not-deduplicate="duplicate1">.Duplicate1 {}</style>
235 <style data-mw-deduplicate="duplicate1">.Same-attribute-different-content {}</style>
236 <style data-mw-deduplicate="duplicate3">.Duplicate1 {}</style>
237 <style>.Duplicate1 {}</style>
245 <div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
247 <li class="toclevel-1 tocsection-1"><a href="#Section_1"><span class="tocnumber">1</span> <span class="toctext">Section 1</span></a></li>
248 <li class="toclevel-1 tocsection-2"><a href="#Section_2"><span class="tocnumber">2</span> <span class="toctext">Section 2</span></a>
250 <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>
253 <li class="toclevel-1 tocsection-4"><a href="#Section_3"><span class="tocnumber">3</span> <span class="toctext">Section 3</span></a></li>
257 <h2><span class="mw-headline" id="Section_1">Section 1</span><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></h2>
260 <h2><span class="mw-headline" id="Section_2">Section 2</span><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></h2>
263 <h3><span class="mw-headline" id="Section_2.1">Section 2.1</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=3" title="Edit section: Section 2.1">edit</a><span class="mw-editsection-bracket">]</span></span></h3>
266 <h2><span class="mw-headline" id="Section_3">Section 3</span><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></h2>
271 'Disable section edit links' => [
272 [ 'enableSectionEditLinks' => false ], $text, <<<EOF
275 <div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
277 <li class="toclevel-1 tocsection-1"><a href="#Section_1"><span class="tocnumber">1</span> <span class="toctext">Section 1</span></a></li>
278 <li class="toclevel-1 tocsection-2"><a href="#Section_2"><span class="tocnumber">2</span> <span class="toctext">Section 2</span></a>
280 <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>
283 <li class="toclevel-1 tocsection-4"><a href="#Section_3"><span class="tocnumber">3</span> <span class="toctext">Section 3</span></a></li>
287 <h2><span class="mw-headline" id="Section_1">Section 1</span></h2>
290 <h2><span class="mw-headline" id="Section_2">Section 2</span></h2>
293 <h3><span class="mw-headline" id="Section_2.1">Section 2.1</span></h3>
296 <h2><span class="mw-headline" id="Section_3">Section 3</span></h2>
301 'Disable TOC, but wrap' => [
302 [ 'allowTOC' => false, 'wrapperDivClass' => 'mw-parser-output' ], $text, <<<EOF
303 <div class="mw-parser-output"><p>Test document.
306 <h2><span class="mw-headline" id="Section_1">Section 1</span><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></h2>
309 <h2><span class="mw-headline" id="Section_2">Section 2</span><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></h2>
312 <h3><span class="mw-headline" id="Section_2.1">Section 2.1</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&action=edit&section=3" title="Edit section: Section 2.1">edit</a><span class="mw-editsection-bracket">]</span></span></h3>
315 <h2><span class="mw-headline" id="Section_3">Section 3</span><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></h2>
320 'Style deduplication' => [
321 [], $dedupText, <<<EOF
322 <p>This is a test document.</p>
323 <style data-mw-deduplicate="duplicate1">.Duplicate1 {}</style>
324 <link rel="mw-deduplicated-inline-style" href="mw-data:duplicate1"/>
325 <style data-mw-deduplicate="duplicate2">.Duplicate2 {}</style>
326 <link rel="mw-deduplicated-inline-style" href="mw-data:duplicate1"/>
327 <link rel="mw-deduplicated-inline-style" href="mw-data:duplicate2"/>
328 <style data-mw-not-deduplicate="duplicate1">.Duplicate1 {}</style>
329 <link rel="mw-deduplicated-inline-style" href="mw-data:duplicate1"/>
330 <style data-mw-deduplicate="duplicate3">.Duplicate1 {}</style>
331 <style>.Duplicate1 {}</style>
334 'Style deduplication disabled' => [
335 [ 'deduplicateStyles' => false ], $dedupText, $dedupText
342 * @covers ParserOutput::hasText
344 public function testHasText() {
345 $po = new ParserOutput();
346 $this->assertTrue( $po->hasText() );
348 $po = new ParserOutput( null );
349 $this->assertFalse( $po->hasText() );
351 $po = new ParserOutput( '' );
352 $this->assertTrue( $po->hasText() );
354 $po = new ParserOutput( null );
356 $this->assertTrue( $po->hasText() );
360 * @covers ParserOutput::getText
362 public function testGetText_failsIfNoText() {
363 $po = new ParserOutput( null );
365 $this->setExpectedException( LogicException
::class );
370 * @covers ParserOutput::getRawText
372 public function testGetRawText_failsIfNoText() {
373 $po = new ParserOutput( null );
375 $this->setExpectedException( LogicException
::class );
379 public function provideMergeHtmlMetaDataFrom() {
380 // title text ------------
381 $a = new ParserOutput();
382 $a->setTitleText( 'X' );
383 $b = new ParserOutput();
384 yield
'only left title text' => [ $a, $b, [ 'getTitleText' => 'X' ] ];
386 $a = new ParserOutput();
387 $b = new ParserOutput();
388 $b->setTitleText( 'Y' );
389 yield
'only right title text' => [ $a, $b, [ 'getTitleText' => 'Y' ] ];
391 $a = new ParserOutput();
392 $a->setTitleText( 'X' );
393 $b = new ParserOutput();
394 $b->setTitleText( 'Y' );
395 yield
'left title text wins' => [ $a, $b, [ 'getTitleText' => 'X' ] ];
397 // index policy ------------
398 $a = new ParserOutput();
399 $a->setIndexPolicy( 'index' );
400 $b = new ParserOutput();
401 yield
'only left index policy' => [ $a, $b, [ 'getIndexPolicy' => 'index' ] ];
403 $a = new ParserOutput();
404 $b = new ParserOutput();
405 $b->setIndexPolicy( 'index' );
406 yield
'only right index policy' => [ $a, $b, [ 'getIndexPolicy' => 'index' ] ];
408 $a = new ParserOutput();
409 $a->setIndexPolicy( 'noindex' );
410 $b = new ParserOutput();
411 $b->setIndexPolicy( 'index' );
412 yield
'left noindex wins' => [ $a, $b, [ 'getIndexPolicy' => 'noindex' ] ];
414 $a = new ParserOutput();
415 $a->setIndexPolicy( 'index' );
416 $b = new ParserOutput();
417 $b->setIndexPolicy( 'noindex' );
418 yield
'right noindex wins' => [ $a, $b, [ 'getIndexPolicy' => 'noindex' ] ];
420 // head items and friends ------------
421 $a = new ParserOutput();
422 $a->addHeadItem( '<foo1>' );
423 $a->addHeadItem( '<bar1>', 'bar' );
424 $a->addModules( 'test-module-a' );
425 $a->addModuleScripts( 'test-module-script-a' );
426 $a->addModuleStyles( 'test-module-styles-a' );
427 $b->addJsConfigVars( 'test-config-var-a', 'a' );
429 $b = new ParserOutput();
430 $b->setIndexPolicy( 'noindex' );
431 $b->addHeadItem( '<foo2>' );
432 $b->addHeadItem( '<bar2>', 'bar' );
433 $b->addModules( 'test-module-b' );
434 $b->addModuleScripts( 'test-module-script-b' );
435 $b->addModuleStyles( 'test-module-styles-b' );
436 $b->addJsConfigVars( 'test-config-var-b', 'b' );
437 $b->addJsConfigVars( 'test-config-var-a', 'X' );
439 yield
'head items and friends' => [ $a, $b, [
443 'bar' => '<bar2>', // overwritten
449 'getModuleScripts' => [
450 'test-module-script-a',
451 'test-module-script-b',
453 'getModuleStyles' => [
454 'test-module-styles-a',
455 'test-module-styles-b',
457 'getJsConfigVars' => [
458 'test-config-var-a' => 'X', // overwritten
459 'test-config-var-b' => 'b',
464 $a = new ParserOutput();
465 $a->setTOCHTML( '<p>TOC A</p>' );
466 $a->setSections( [ [ 'fromtitle' => 'A1' ], [ 'fromtitle' => 'A2' ] ] );
468 $b = new ParserOutput();
469 $b->setTOCHTML( '<p>TOC B</p>' );
470 $b->setSections( [ [ 'fromtitle' => 'B1' ], [ 'fromtitle' => 'B2' ] ] );
472 yield
'concat TOC' => [ $a, $b, [
473 'getTOCHTML' => '<p>TOC A</p><p>TOC B</p>',
475 [ 'fromtitle' => 'A1' ],
476 [ 'fromtitle' => 'A2' ],
477 [ 'fromtitle' => 'B1' ],
478 [ 'fromtitle' => 'B2' ]
482 // Skin Control ------------
483 $a = new ParserOutput();
484 $a->setNewSection( true );
485 $a->hideNewSection( true );
486 $a->setNoGallery( true );
487 $a->addWrapperDivClass( 'foo' );
489 $a->setIndicator( 'foo', 'Foo!' );
490 $a->setIndicator( 'bar', 'Bar!' );
492 $a->setExtensionData( 'foo', 'Foo!' );
493 $a->setExtensionData( 'bar', 'Bar!' );
495 $b = new ParserOutput();
496 $b->setNoGallery( true );
497 $b->setEnableOOUI( true );
498 $b->preventClickjacking( true );
499 $a->addWrapperDivClass( 'bar' );
501 $b->setIndicator( 'zoo', 'Zoo!' );
502 $b->setIndicator( 'bar', 'Barrr!' );
504 $b->setExtensionData( 'zoo', 'Zoo!' );
505 $b->setExtensionData( 'bar', 'Barrr!' );
507 yield
'skin control flags' => [ $a, $b, [
508 'getNewSection' => true,
509 'getHideNewSection' => true,
510 'getNoGallery' => true,
511 'getEnableOOUI' => true,
512 'preventClickjacking' => true,
518 'getWrapperDivClass' => 'foo bar',
519 '$mExtensionData' => [
528 * @dataProvider provideMergeHtmlMetaDataFrom
529 * @covers ParserOutput::mergeHtmlMetaDataFrom
531 * @param ParserOutput $a
532 * @param ParserOutput $b
533 * @param array $expected
535 public function testMergeHtmlMetaDataFrom( ParserOutput
$a, ParserOutput
$b, $expected ) {
536 $a->mergeHtmlMetaDataFrom( $b );
538 $this->assertFieldValues( $a, $expected );
540 // test twice, to make sure the operation is idempotent (except for the TOC, see below)
541 $a->mergeHtmlMetaDataFrom( $b );
543 // XXX: TOC joining should get smarter. Can we make it idempotent as well?
544 unset( $expected['getTOCHTML'] );
545 unset( $expected['getSections'] );
547 $this->assertFieldValues( $a, $expected );
550 private function assertFieldValues( ParserOutput
$po, $expected ) {
551 $po = TestingAccessWrapper
::newFromObject( $po );
553 foreach ( $expected as $method => $value ) {
554 if ( $method[0] === '$' ) {
555 $field = substr( $method, 1 );
556 $actual = $po->__get( $field );
558 $actual = $po->__call( $method, [] );
561 $this->assertEquals( $value, $actual, $method );
565 public function provideMergeTrackingMetaDataFrom() {
566 // links ------------
567 $a = new ParserOutput();
568 $a->addLink( Title
::makeTitle( NS_MAIN
, 'Kittens' ), 6 );
569 $a->addLink( Title
::makeTitle( NS_TALK
, 'Kittens' ), 16 );
570 $a->addLink( Title
::makeTitle( NS_MAIN
, 'Goats' ), 7 );
572 $a->addTemplate( Title
::makeTitle( NS_TEMPLATE
, 'Goats' ), 107, 1107 );
574 $a->addLanguageLink( 'de' );
575 $a->addLanguageLink( 'ru' );
576 $a->addInterwikiLink( Title
::makeTitle( NS_MAIN
, 'Kittens DE', '', 'de' ) );
577 $a->addInterwikiLink( Title
::makeTitle( NS_MAIN
, 'Kittens RU', '', 'ru' ) );
578 $a->addExternalLink( 'https://kittens.wikimedia.test' );
579 $a->addExternalLink( 'https://goats.wikimedia.test' );
581 $a->addCategory( 'Foo', 'X' );
582 $a->addImage( 'Billy.jpg', '20180101000013', 'DEAD' );
584 $b = new ParserOutput();
585 $b->addLink( Title
::makeTitle( NS_MAIN
, 'Goats' ), 7 );
586 $b->addLink( Title
::makeTitle( NS_TALK
, 'Goats' ), 17 );
587 $b->addLink( Title
::makeTitle( NS_MAIN
, 'Dragons' ), 8 );
588 $b->addLink( Title
::makeTitle( NS_FILE
, 'Dragons.jpg' ), 28 );
590 $b->addTemplate( Title
::makeTitle( NS_TEMPLATE
, 'Dragons' ), 108, 1108 );
591 $a->addTemplate( Title
::makeTitle( NS_MAIN
, 'Dragons' ), 118, 1118 );
593 $b->addLanguageLink( 'fr' );
594 $b->addLanguageLink( 'ru' );
595 $b->addInterwikiLink( Title
::makeTitle( NS_MAIN
, 'Kittens FR', '', 'fr' ) );
596 $b->addInterwikiLink( Title
::makeTitle( NS_MAIN
, 'Dragons RU', '', 'ru' ) );
597 $b->addExternalLink( 'https://dragons.wikimedia.test' );
598 $b->addExternalLink( 'https://goats.wikimedia.test' );
600 $b->addCategory( 'Bar', 'Y' );
601 $b->addImage( 'Puff.jpg', '20180101000017', 'BEEF' );
603 yield
'all kinds of links' => [ $a, $b, [
627 'getTemplateIds' => [
636 'getLanguageLinks' => [ 'de', 'ru', 'fr' ],
637 'getInterwikiLinks' => [
638 'de' => [ 'Kittens_DE' => 1 ],
639 'ru' => [ 'Kittens_RU' => 1, 'Dragons_RU' => 1, ],
640 'fr' => [ 'Kittens_FR' => 1 ],
642 'getCategories' => [ 'Foo' => 'X', 'Bar' => 'Y' ],
643 'getImages' => [ 'Billy.jpg' => 1, 'Puff.jpg' => 1 ],
644 'getFileSearchOptions' => [
645 'Billy.jpg' => [ 'time' => '20180101000013', 'sha1' => 'DEAD' ],
646 'Puff.jpg' => [ 'time' => '20180101000017', 'sha1' => 'BEEF' ],
648 'getExternalLinks' => [
649 'https://dragons.wikimedia.test' => 1,
650 'https://kittens.wikimedia.test' => 1,
651 'https://goats.wikimedia.test' => 1,
655 // properties ------------
656 $a = new ParserOutput();
658 $a->setProperty( 'foo', 'Foo!' );
659 $a->setProperty( 'bar', 'Bar!' );
661 $a->setExtensionData( 'foo', 'Foo!' );
662 $a->setExtensionData( 'bar', 'Bar!' );
664 $b = new ParserOutput();
666 $b->setProperty( 'zoo', 'Zoo!' );
667 $b->setProperty( 'bar', 'Barrr!' );
669 $b->setExtensionData( 'zoo', 'Zoo!' );
670 $b->setExtensionData( 'bar', 'Barrr!' );
672 yield
'properties' => [ $a, $b, [
678 '$mExtensionData' => [
687 * @dataProvider provideMergeTrackingMetaDataFrom
688 * @covers ParserOutput::mergeTrackingMetaDataFrom
690 * @param ParserOutput $a
691 * @param ParserOutput $b
692 * @param array $expected
694 public function testMergeTrackingMetaDataFrom( ParserOutput
$a, ParserOutput
$b, $expected ) {
695 $a->mergeTrackingMetaDataFrom( $b );
697 $this->assertFieldValues( $a, $expected );
699 // test twice, to make sure the operation is idempotent
700 $a->mergeTrackingMetaDataFrom( $b );
702 $this->assertFieldValues( $a, $expected );
705 public function provideMergeInternalMetaDataFrom() {
707 $a = new ParserOutput();
709 $a->addOutputHook( 'foo', 'X' );
710 $a->addOutputHook( 'bar' );
712 $b = new ParserOutput();
714 $b->addOutputHook( 'foo', 'Y' );
715 $b->addOutputHook( 'bar' );
716 $b->addOutputHook( 'zoo' );
718 yield
'hooks' => [ $a, $b, [
719 'getOutputHooks' => [
728 $a = new ParserOutput();
730 $a->addWarning( 'Oops' );
731 $a->addWarning( 'Whoops' );
733 $a->setFlag( 'foo' );
734 $a->setFlag( 'bar' );
736 $a->recordOption( 'Foo' );
737 $a->recordOption( 'Bar' );
739 $b = new ParserOutput();
741 $b->addWarning( 'Yikes' );
742 $b->addWarning( 'Whoops' );
744 $b->setFlag( 'zoo' );
745 $b->setFlag( 'bar' );
747 $b->recordOption( 'Zoo' );
748 $b->recordOption( 'Bar' );
750 yield
'flags' => [ $a, $b, [
751 'getWarnings' => [ 'Oops', 'Whoops', 'Yikes' ],
752 '$mFlags' => [ 'foo' => true, 'bar' => true, 'zoo' => true ],
753 'getUsedOptions' => [ 'Foo', 'Bar', 'Zoo' ],
756 // timestamp ------------
757 $a = new ParserOutput();
758 $a->setTimestamp( '20180101000011' );
759 $b = new ParserOutput();
760 yield
'only left timestamp' => [ $a, $b, [ 'getTimestamp' => '20180101000011' ] ];
762 $a = new ParserOutput();
763 $b = new ParserOutput();
764 $b->setTimestamp( '20180101000011' );
765 yield
'only right timestamp' => [ $a, $b, [ 'getTimestamp' => '20180101000011' ] ];
767 $a = new ParserOutput();
768 $a->setTimestamp( '20180101000011' );
769 $b = new ParserOutput();
770 $b->setTimestamp( '20180101000001' );
771 yield
'left timestamp wins' => [ $a, $b, [ 'getTimestamp' => '20180101000011' ] ];
773 $a = new ParserOutput();
774 $a->setTimestamp( '20180101000001' );
775 $b = new ParserOutput();
776 $b->setTimestamp( '20180101000011' );
777 yield
'right timestamp wins' => [ $a, $b, [ 'getTimestamp' => '20180101000011' ] ];
779 // speculative rev id ------------
780 $a = new ParserOutput();
781 $a->setSpeculativeRevIdUsed( 9 );
782 $b = new ParserOutput();
783 yield
'only left speculative rev id' => [ $a, $b, [ 'getSpeculativeRevIdUsed' => 9 ] ];
785 $a = new ParserOutput();
786 $b = new ParserOutput();
787 $b->setSpeculativeRevIdUsed( 9 );
788 yield
'only right speculative rev id' => [ $a, $b, [ 'getSpeculativeRevIdUsed' => 9 ] ];
790 $a = new ParserOutput();
791 $a->setSpeculativeRevIdUsed( 9 );
792 $b = new ParserOutput();
793 $b->setSpeculativeRevIdUsed( 9 );
794 yield
'same speculative rev id' => [ $a, $b, [ 'getSpeculativeRevIdUsed' => 9 ] ];
796 // limit report (recursive max) ------------
797 $a = new ParserOutput();
799 $a->setLimitReportData( 'naive1', 7 );
800 $a->setLimitReportData( 'naive2', 27 );
802 $a->setLimitReportData( 'limitreport-simple1', 7 );
803 $a->setLimitReportData( 'limitreport-simple2', 27 );
805 $a->setLimitReportData( 'limitreport-pair1', [ 7, 9 ] );
806 $a->setLimitReportData( 'limitreport-pair2', [ 27, 29 ] );
808 $a->setLimitReportData( 'limitreport-more1', [ 7, 9, 1 ] );
809 $a->setLimitReportData( 'limitreport-more2', [ 27, 29, 21 ] );
811 $a->setLimitReportData( 'limitreport-only-a', 13 );
813 $b = new ParserOutput();
815 $b->setLimitReportData( 'naive1', 17 );
816 $b->setLimitReportData( 'naive2', 17 );
818 $b->setLimitReportData( 'limitreport-simple1', 17 );
819 $b->setLimitReportData( 'limitreport-simple2', 17 );
821 $b->setLimitReportData( 'limitreport-pair1', [ 17, 19 ] );
822 $b->setLimitReportData( 'limitreport-pair2', [ 17, 19 ] );
824 $b->setLimitReportData( 'limitreport-more1', [ 17, 19, 11 ] );
825 $b->setLimitReportData( 'limitreport-more2', [ 17, 19, 11 ] );
827 $b->setLimitReportData( 'limitreport-only-b', 23 );
830 yield
'limit report' => [ $a, $b, [
831 'getLimitReportData' => [
834 'limitreport-simple1' => 7,
835 'limitreport-simple2' => 27,
836 'limitreport-pair1' => [ 7, 9 ],
837 'limitreport-pair2' => [ 27, 29 ],
838 'limitreport-more1' => [ 7, 9, 1 ],
839 'limitreport-more2' => [ 27, 29, 21 ],
840 'limitreport-only-a' => 13,
842 'getLimitReportJSData' => [
848 'pair1' => [ 'value' => 7, 'limit' => 9 ],
849 'pair2' => [ 'value' => 27, 'limit' => 29 ],
850 'more1' => [ 7, 9, 1 ],
851 'more2' => [ 27, 29, 21 ],
859 * @dataProvider provideMergeInternalMetaDataFrom
860 * @covers ParserOutput::mergeInternalMetaDataFrom
862 * @param ParserOutput $a
863 * @param ParserOutput $b
864 * @param array $expected
866 public function testMergeInternalMetaDataFrom( ParserOutput
$a, ParserOutput
$b, $expected ) {
867 $a->mergeInternalMetaDataFrom( $b );
869 $this->assertFieldValues( $a, $expected );
871 // test twice, to make sure the operation is idempotent
872 $a->mergeInternalMetaDataFrom( $b );
874 $this->assertFieldValues( $a, $expected );
877 public function testMergeInternalMetaDataFrom_parseStartTime() {
878 /** @var object $a */
879 $a = new ParserOutput();
880 $a = TestingAccessWrapper
::newFromObject( $a );
882 $a->resetParseStartTime();
883 $aClocks = $a->mParseStartTime
;
885 $b = new ParserOutput();
887 $a->mergeInternalMetaDataFrom( $b );
888 $mergedClocks = $a->mParseStartTime
;
890 foreach ( $mergedClocks as $clock => $timestamp ) {
891 $this->assertSame( $aClocks[$clock], $timestamp, $clock );
894 // try again, with times in $b also set, and later than $a's
897 /** @var object $b */
898 $b = new ParserOutput();
899 $b = TestingAccessWrapper
::newFromObject( $b );
901 $b->resetParseStartTime();
903 $bClocks = $b->mParseStartTime
;
905 $a->mergeInternalMetaDataFrom( $b->object, 'b' );
906 $mergedClocks = $a->mParseStartTime
;
908 foreach ( $mergedClocks as $clock => $timestamp ) {
909 $this->assertSame( $aClocks[$clock], $timestamp, $clock );
910 $this->assertLessThanOrEqual( $bClocks[$clock], $timestamp, $clock );
913 // try again, with $a's times being later
915 $a->resetParseStartTime();
916 $aClocks = $a->mParseStartTime
;
918 $a->mergeInternalMetaDataFrom( $b->object, 'b' );
919 $mergedClocks = $a->mParseStartTime
;
921 foreach ( $mergedClocks as $clock => $timestamp ) {
922 $this->assertSame( $bClocks[$clock], $timestamp, $clock );
923 $this->assertLessThanOrEqual( $aClocks[$clock], $timestamp, $clock );
926 // try again, with no times in $a set
927 $a = new ParserOutput();
928 $a = TestingAccessWrapper
::newFromObject( $a );
930 $a->mergeInternalMetaDataFrom( $b->object, 'b' );
931 $mergedClocks = $a->mParseStartTime
;
933 foreach ( $mergedClocks as $clock => $timestamp ) {
934 $this->assertSame( $bClocks[$clock], $timestamp, $clock );