| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Content\WikitextContent; |
| 4 |
use MediaWiki\Content\WikiTextStructure; |
| 5 |
use MediaWiki\Title\Title; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class WikitextStructureTest extends MediaWikiLangTestCase { |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
private function getStructure( $text ) { |
| 19 |
$content = new WikitextContent( $text ); |
| 20 |
$contentRenderer = $this->getServiceContainer()->getContentRenderer(); |
| 21 |
$parserOutput = $contentRenderer->getParserOutput( $content, Title::makeTitle( NS_MAIN, 'TestTitle' ) ); |
| 22 |
return new WikiTextStructure( $parserOutput ); |
| 23 |
} |
| 24 |
|
| 25 |
public function testHeadings() { |
| 26 |
$text = <<<END |
| 27 |
Some text here |
| 28 |
== Heading one == |
| 29 |
Some text |
| 30 |
==== heading two ==== |
| 31 |
More text |
| 32 |
=== Applicability of the strict mass-energy equivalence formula, ''E'' = ''mc''<sup>2</sup> === |
| 33 |
and more text |
| 34 |
== Wikitext '''in''' [[Heading]] and also <b>html</b> == |
| 35 |
more text |
| 36 |
==== See also ==== |
| 37 |
* Also things to see! |
| 38 |
END; |
| 39 |
$struct = $this->getStructure( $text ); |
| 40 |
$headings = $struct->headings(); |
| 41 |
$this->assertCount( 4, $headings ); |
| 42 |
$this->assertContains( "Heading one", $headings ); |
| 43 |
$this->assertContains( "heading two", $headings ); |
| 44 |
$this->assertContains( "Applicability of the strict mass-energy equivalence formula, E = mc2", |
| 45 |
$headings ); |
| 46 |
$this->assertContains( "Wikitext in Heading and also html", $headings ); |
| 47 |
} |
| 48 |
|
| 49 |
public function testDefaultSort() { |
| 50 |
$text = <<<END |
| 51 |
Louise Michel |
| 52 |
== Heading one == |
| 53 |
Some text |
| 54 |
==== See also ==== |
| 55 |
* Also things to see! |
| 56 |
{{DEFAULTSORT:Michel, Louise}} |
| 57 |
END; |
| 58 |
$struct = $this->getStructure( $text ); |
| 59 |
$this->assertEquals( "Michel, Louise", $struct->getDefaultSort() ); |
| 60 |
} |
| 61 |
|
| 62 |
public function testHeadingsFirst() { |
| 63 |
$text = <<<END |
| 64 |
== Heading one == |
| 65 |
Some text |
| 66 |
==== heading two ==== |
| 67 |
END; |
| 68 |
$struct = $this->getStructure( $text ); |
| 69 |
$headings = $struct->headings(); |
| 70 |
$this->assertCount( 2, $headings ); |
| 71 |
$this->assertContains( "Heading one", $headings ); |
| 72 |
$this->assertContains( "heading two", $headings ); |
| 73 |
} |
| 74 |
|
| 75 |
public function testHeadingsNone() { |
| 76 |
$text = "This text is completely devoid of headings."; |
| 77 |
$struct = $this->getStructure( $text ); |
| 78 |
$headings = $struct->headings(); |
| 79 |
$this->assertArrayEquals( [], $headings ); |
| 80 |
} |
| 81 |
|
| 82 |
public function testTexts() { |
| 83 |
$text = <<<END |
| 84 |
Opening text is opening. |
| 85 |
<h2 class="hello">Then comes header</h2> |
| 86 |
Then we got more<br>text |
| 87 |
=== And more headers === |
| 88 |
{| class="wikitable" |
| 89 |
|- |
| 90 |
! Header table |
| 91 |
|- |
| 92 |
| row in table |
| 93 |
|- |
| 94 |
| another row in table |
| 95 |
|} |
| 96 |
END; |
| 97 |
$struct = $this->getStructure( $text ); |
| 98 |
$this->assertEquals( "Opening text is opening.", $struct->getOpeningText() ); |
| 99 |
$this->assertEquals( "Opening text is opening. Then we got more text", |
| 100 |
$struct->getMainText() ); |
| 101 |
$this->assertEquals( [ "Header table row in table another row in table" ], |
| 102 |
$struct->getAuxiliaryText() ); |
| 103 |
} |
| 104 |
|
| 105 |
public function testPreservesWordSpacing() { |
| 106 |
$text = "<dd><dl>foo</dl><dl>bar</dl></dd><p>baz</p>"; |
| 107 |
$struct = $this->getStructure( $text ); |
| 108 |
$this->assertEquals( "foo bar baz", $struct->getMainText() ); |
| 109 |
} |
| 110 |
} |