5 * ^--- trigger DB shadowing because we are using Title magic
7 class ParserOutputTest
extends MediaWikiTestCase
{
9 public static function provideIsLinkInternal() {
12 [ false, 'http://example.org', 'http://mediawiki.org' ],
14 [ true, 'http://example.org', 'http://example.org' ],
15 [ true, 'https://example.org', 'https://example.org' ],
16 [ true, '//example.org', '//example.org' ],
17 // Same domain different cases
18 [ true, 'http://example.org', 'http://EXAMPLE.ORG' ],
19 // Paths, queries, and fragments are not relevant
20 [ true, 'http://example.org', 'http://example.org/wiki/Main_Page' ],
21 [ true, 'http://example.org', 'http://example.org?my=query' ],
22 [ true, 'http://example.org', 'http://example.org#its-a-fragment' ],
23 // Different protocols
24 [ false, 'http://example.org', 'https://example.org' ],
25 [ false, 'https://example.org', 'http://example.org' ],
26 // Protocol relative servers always match http and https links
27 [ true, '//example.org', 'http://example.org' ],
28 [ true, '//example.org', 'https://example.org' ],
29 // But they don't match strange things like this
30 [ false, '//example.org', 'irc://example.org' ],
35 * Test to make sure ParserOutput::isLinkInternal behaves properly
36 * @dataProvider provideIsLinkInternal
37 * @covers ParserOutput::isLinkInternal
39 public function testIsLinkInternal( $shouldMatch, $server, $url ) {
40 $this->assertEquals( $shouldMatch, ParserOutput
::isLinkInternal( $server, $url ) );
44 * @covers ParserOutput::setExtensionData
45 * @covers ParserOutput::getExtensionData
47 public function testExtensionData() {
48 $po = new ParserOutput();
50 $po->setExtensionData( "one", "Foo" );
52 $this->assertEquals( "Foo", $po->getExtensionData( "one" ) );
53 $this->assertNull( $po->getExtensionData( "spam" ) );
55 $po->setExtensionData( "two", "Bar" );
56 $this->assertEquals( "Foo", $po->getExtensionData( "one" ) );
57 $this->assertEquals( "Bar", $po->getExtensionData( "two" ) );
59 $po->setExtensionData( "one", null );
60 $this->assertNull( $po->getExtensionData( "one" ) );
61 $this->assertEquals( "Bar", $po->getExtensionData( "two" ) );
65 * @covers ParserOutput::setProperty
66 * @covers ParserOutput::getProperty
67 * @covers ParserOutput::unsetProperty
68 * @covers ParserOutput::getProperties
70 public function testProperties() {
71 $po = new ParserOutput();
73 $po->setProperty( 'foo', 'val' );
75 $properties = $po->getProperties();
76 $this->assertEquals( $po->getProperty( 'foo' ), 'val' );
77 $this->assertEquals( $properties['foo'], 'val' );
79 $po->setProperty( 'foo', 'second val' );
81 $properties = $po->getProperties();
82 $this->assertEquals( $po->getProperty( 'foo' ), 'second val' );
83 $this->assertEquals( $properties['foo'], 'second val' );
85 $po->unsetProperty( 'foo' );
87 $properties = $po->getProperties();
88 $this->assertEquals( $po->getProperty( 'foo' ), false );
89 $this->assertArrayNotHasKey( 'foo', $properties );
93 * @covers ParserOutput::getWrapperDivClass
94 * @covers ParserOutput::addWrapperDivClass
95 * @covers ParserOutput::clearWrapperDivClass
96 * @covers ParserOutput::getText
98 public function testWrapperDivClass() {
99 $po = new ParserOutput();
101 $po->setText( 'Kittens' );
102 $this->assertContains( 'Kittens', $po->getText() );
103 $this->assertNotContains( '<div', $po->getText() );
104 $this->assertSame( 'Kittens', $po->getRawText() );
106 $po->addWrapperDivClass( 'foo' );
107 $text = $po->getText();
108 $this->assertContains( 'Kittens', $text );
109 $this->assertContains( '<div', $text );
110 $this->assertContains( 'class="foo"', $text );
112 $po->addWrapperDivClass( 'bar' );
113 $text = $po->getText();
114 $this->assertContains( 'Kittens', $text );
115 $this->assertContains( '<div', $text );
116 $this->assertContains( 'class="foo bar"', $text );
118 $po->addWrapperDivClass( 'bar' ); // second time does nothing, no "foo bar bar".
119 $text = $po->getText( [ 'unwrap' => true ] );
120 $this->assertContains( 'Kittens', $text );
121 $this->assertNotContains( '<div', $text );
122 $this->assertNotContains( 'class="foo bar"', $text );
124 $text = $po->getText( [ 'wrapperDivClass' => '' ] );
125 $this->assertContains( 'Kittens', $text );
126 $this->assertNotContains( '<div', $text );
127 $this->assertNotContains( 'class="foo bar"', $text );
129 $text = $po->getText( [ 'wrapperDivClass' => 'xyzzy' ] );
130 $this->assertContains( 'Kittens', $text );
131 $this->assertContains( '<div', $text );
132 $this->assertContains( 'class="xyzzy"', $text );
133 $this->assertNotContains( 'class="foo bar"', $text );
135 $text = $po->getRawText();
136 $this->assertSame( 'Kittens', $text );
138 $po->clearWrapperDivClass();
139 $text = $po->getText();
140 $this->assertContains( 'Kittens', $text );
141 $this->assertNotContains( '<div', $text );
142 $this->assertNotContains( 'class="foo bar"', $text );
146 * @covers ParserOutput::getText
147 * @dataProvider provideGetText
148 * @param array $options Options to getText()
149 * @param string $text Parser text
150 * @param string $expect Expected output
152 public function testGetText( $options, $text, $expect ) {
153 $this->setMwGlobals( [
154 'wgArticlePath' => '/wiki/$1',
155 'wgScriptPath' => '/w',
156 'wgScript' => '/w/index.php',
159 $po = new ParserOutput( $text );
160 $actual = $po->getText( $options );
161 $this->assertSame( $expect, $actual );
164 public static function provideGetText() {
165 // phpcs:disable Generic.Files.LineLength
169 <mw:toc><div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
171 <li class="toclevel-1 tocsection-1"><a href="#Section_1"><span class="tocnumber">1</span> <span class="toctext">Section 1</span></a></li>
172 <li class="toclevel-1 tocsection-2"><a href="#Section_2"><span class="tocnumber">2</span> <span class="toctext">Section 2</span></a>
174 <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>
177 <li class="toclevel-1 tocsection-4"><a href="#Section_3"><span class="tocnumber">3</span> <span class="toctext">Section 3</span></a></li>
181 <h2><span class="mw-headline" id="Section_1">Section 1</span><mw:editsection page="Test Page" section="1">Section 1</mw:editsection></h2>
184 <h2><span class="mw-headline" id="Section_2">Section 2</span><mw:editsection page="Test Page" section="2">Section 2</mw:editsection></h2>
187 <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>
190 <h2><span class="mw-headline" id="Section_3">Section 3</span><mw:editsection page="Test Page" section="4">Section 3</mw:editsection></h2>
196 <p>This is a test document.</p>
197 <style data-mw-deduplicate="duplicate1">.Duplicate1 {}</style>
198 <style data-mw-deduplicate="duplicate1">.Duplicate1 {}</style>
199 <style data-mw-deduplicate="duplicate2">.Duplicate2 {}</style>
200 <style data-mw-deduplicate="duplicate1">.Duplicate1 {}</style>
201 <style data-mw-deduplicate="duplicate2">.Duplicate2 {}</style>
202 <style data-mw-not-deduplicate="duplicate1">.Duplicate1 {}</style>
203 <style data-mw-deduplicate="duplicate1">.Same-attribute-different-content {}</style>
204 <style data-mw-deduplicate="duplicate3">.Duplicate1 {}</style>
205 <style>.Duplicate1 {}</style>
213 <div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
215 <li class="toclevel-1 tocsection-1"><a href="#Section_1"><span class="tocnumber">1</span> <span class="toctext">Section 1</span></a></li>
216 <li class="toclevel-1 tocsection-2"><a href="#Section_2"><span class="tocnumber">2</span> <span class="toctext">Section 2</span></a>
218 <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>
221 <li class="toclevel-1 tocsection-4"><a href="#Section_3"><span class="tocnumber">3</span> <span class="toctext">Section 3</span></a></li>
225 <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>
228 <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>
231 <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>
234 <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>
239 'Disable section edit links' => [
240 [ 'enableSectionEditLinks' => false ], $text, <<<EOF
243 <div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
245 <li class="toclevel-1 tocsection-1"><a href="#Section_1"><span class="tocnumber">1</span> <span class="toctext">Section 1</span></a></li>
246 <li class="toclevel-1 tocsection-2"><a href="#Section_2"><span class="tocnumber">2</span> <span class="toctext">Section 2</span></a>
248 <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>
251 <li class="toclevel-1 tocsection-4"><a href="#Section_3"><span class="tocnumber">3</span> <span class="toctext">Section 3</span></a></li>
255 <h2><span class="mw-headline" id="Section_1">Section 1</span></h2>
258 <h2><span class="mw-headline" id="Section_2">Section 2</span></h2>
261 <h3><span class="mw-headline" id="Section_2.1">Section 2.1</span></h3>
264 <h2><span class="mw-headline" id="Section_3">Section 3</span></h2>
269 'Disable TOC, but wrap' => [
270 [ 'allowTOC' => false, 'wrapperDivClass' => 'mw-parser-output' ], $text, <<<EOF
271 <div class="mw-parser-output"><p>Test document.
274 <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>
277 <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>
280 <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>
283 <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>
288 'Style deduplication' => [
289 [], $dedupText, <<<EOF
290 <p>This is a test document.</p>
291 <style data-mw-deduplicate="duplicate1">.Duplicate1 {}</style>
292 <link rel="mw-deduplicated-inline-style" href="mw-data:duplicate1"/>
293 <style data-mw-deduplicate="duplicate2">.Duplicate2 {}</style>
294 <link rel="mw-deduplicated-inline-style" href="mw-data:duplicate1"/>
295 <link rel="mw-deduplicated-inline-style" href="mw-data:duplicate2"/>
296 <style data-mw-not-deduplicate="duplicate1">.Duplicate1 {}</style>
297 <link rel="mw-deduplicated-inline-style" href="mw-data:duplicate1"/>
298 <style data-mw-deduplicate="duplicate3">.Duplicate1 {}</style>
299 <style>.Duplicate1 {}</style>
302 'Style deduplication disabled' => [
303 [ 'deduplicateStyles' => false ], $dedupText, $dedupText