3 use MediaWiki\Content\WikitextContent
;
4 use MediaWiki\Content\WikiTextStructure
;
5 use MediaWiki\Title\Title
;
9 * @covers \MediaWiki\Content\WikiTextStructure
11 class WikitextStructureTest
extends MediaWikiLangTestCase
{
14 * Get WikitextStructure for given text
16 * @return WikiTextStructure
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 );
25 public function testHeadings() {
32 === Applicability of the strict mass-energy equivalence formula, ''E'' = ''mc''<sup>2</sup> ===
34 == Wikitext '''in''' [[Heading]] and also <b>html</b> ==
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",
46 $this->assertContains( "Wikitext in Heading and also html", $headings );
49 public function testDefaultSort() {
56 {{DEFAULTSORT:Michel, Louise}}
58 $struct = $this->getStructure( $text );
59 $this->assertEquals( "Michel, Louise", $struct->getDefaultSort() );
62 public function testHeadingsFirst() {
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 );
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 );
82 public function testTexts() {
84 Opening text is opening.
85 <h2 class="hello">Then comes header</h2>
86 Then we got more<br>text
87 === And more headers ===
94 | another row in table
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() );
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() );