Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / content / WikitextStructureTest.php
blob49907c8532d5a82c6c3a9588831442f04e78e132
1 <?php
3 class WikitextStructureTest extends MediaWikiLangTestCase {
5 private function getMockTitle() {
6 return Title::newFromText( "TestTitle" );
9 /**
10 * Get parser output for Wiki text
11 * @param $text
12 * @return ParserOutput
14 private function getParserOutput( $text ) {
15 $content = new WikitextContent( $text );
16 return $content->getParserOutput( $this->getMockTitle() );
19 /**
20 * Get WikitextStructure for given text
21 * @param $text
22 * @return WikiTextStructure
24 private function getStructure( $text ) {
25 return new WikiTextStructure( $this->getParserOutput( $text ) );
28 public function testHeadings() {
29 $text = <<<END
30 Some text here
31 == Heading one ==
32 Some text
33 ==== heading two ====
34 More text
35 === Applicability of the strict mass-energy equivalence formula, ''E'' = ''mc''<sup>2</sup> ===
36 and more text
37 == Wikitext '''in''' [[Heading]] and also <b>html</b> ==
38 more text
39 ==== See also ====
40 * Also things to see!
41 END;
42 $struct = $this->getStructure( $text );
43 $headings = $struct->headings();
44 $this->assertCount( 4, $headings );
45 $this->assertContains( "Heading one", $headings );
46 $this->assertContains( "heading two", $headings );
47 $this->assertContains( "Applicability of the strict mass-energy equivalence formula, E = mc2",
48 $headings );
49 $this->assertContains( "Wikitext in Heading and also html", $headings );
52 public function testDefaultSort() {
53 $text = <<<END
54 Louise Michel
55 == Heading one ==
56 Some text
57 ==== See also ====
58 * Also things to see!
59 {{DEFAULTSORT:Michel, Louise}}
60 END;
61 $struct = $this->getStructure( $text );
62 $this->assertEquals( "Michel, Louise", $struct->getDefaultSort() );
65 public function testHeadingsFirst() {
66 $text = <<<END
67 == Heading one ==
68 Some text
69 ==== heading two ====
70 END;
71 $struct = $this->getStructure( $text );
72 $headings = $struct->headings();
73 $this->assertCount( 2, $headings );
74 $this->assertContains( "Heading one", $headings );
75 $this->assertContains( "heading two", $headings );
78 public function testHeadingsNone() {
79 $text = "This text is completely devoid of headings.";
80 $struct = $this->getStructure( $text );
81 $headings = $struct->headings();
82 $this->assertArrayEquals( [], $headings );
85 public function testTexts() {
86 $text = <<<END
87 Opening text is opening.
88 == Then comes header ==
89 Then we got more<br>text
90 === And more headers ===
91 {| class="wikitable"
93 ! Header table
95 | row in table
97 | another row in table
99 END;
100 $struct = $this->getStructure( $text );
101 $this->assertEquals( "Opening text is opening.", $struct->getOpeningText() );
102 $this->assertEquals( "Opening text is opening. Then we got more text",
103 $struct->getMainText() );
104 $this->assertEquals( [ "Header table row in table another row in table" ],
105 $struct->getAuxiliaryText() );