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