Merge "Split the 'mediawiki.htmlform' module code into multiple files"
[lhc/web/wiklou.git] / tests / phpunit / includes / content / WikitextStructureTest.php
1 <?php
2
3 class WikitextStructureTest extends MediaWikiLangTestCase {
4
5 private function getMockTitle() {
6 return Title::newFromText( "TestTitle" );
7 }
8
9 /**
10 * Get parser output for Wiki text
11 * @param $text
12 * @return ParserOutput
13 */
14 private function getParserOutput( $text ) {
15 $content = new WikitextContent( $text );
16 return $content->getParserOutput( $this->getMockTitle() );
17 }
18
19 /**
20 * Get WikitextStructure for given text
21 * @param $text
22 * @return WikiTextStructure
23 */
24 private function getStructure( $text ) {
25 return new WikiTextStructure( $this->getParserOutput( $text ) );
26 }
27
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 );
50 }
51
52 public function testHeadingsFirst() {
53 $text = <<<END
54 == Heading one ==
55 Some text
56 ==== heading two ====
57 END;
58 $struct = $this->getStructure( $text );
59 $headings = $struct->headings();
60 $this->assertCount( 2, $headings );
61 $this->assertContains( "Heading one", $headings );
62 $this->assertContains( "heading two", $headings );
63 }
64
65 public function testHeadingsNone() {
66 $text = "This text is completely devoid of headings.";
67 $struct = $this->getStructure( $text );
68 $headings = $struct->headings();
69 $this->assertArrayEquals( [], $headings );
70 }
71
72 public function testTexts() {
73 $text = <<<END
74 Opening text is opening.
75 == Then comes header ==
76 Then we got more<br>text
77 === And more headers ===
78 {| class="wikitable"
79 |-
80 ! Header table
81 |-
82 | row in table
83 |-
84 | another row in table
85 |}
86 END;
87 $struct = $this->getStructure( $text );
88 $this->assertEquals( "Opening text is opening.", $struct->getOpeningText() );
89 $this->assertEquals( "Opening text is opening. Then we got more text",
90 $struct->getMainText() );
91 $this->assertEquals( [ "Header table row in table another row in table" ],
92 $struct->getAuxiliaryText() );
93 }
94 }