Merge "Balancer: remove redundant assignment"
[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 testCategories() {
29 $text = <<<END
30 We also have a {{Template}} and an {{Another template}} in addition.
31 This text also has [[Category:Some Category| ]] and then [[Category:Yet another category]].
32 And [[Category:Some Category| this category]] is repeated.
33 END;
34 $struct = $this->getStructure( $text );
35 $cats = $struct->categories();
36 $this->assertCount( 2, $cats );
37 $this->assertContains( "Some Category", $cats );
38 $this->assertContains( "Yet another category", $cats );
39 }
40
41 public function testOutgoingLinks() {
42 $text = <<<END
43 Here I add link to [[Some Page]]. And [[Some Page|This same page]] gets linked twice.
44 We also have [[File:Image.jpg|image]].
45 We also have a {{Template}} and an {{Another template}} in addition.
46 Some templates are {{lowercase}}.
47 And [[Some_Page]] is linked again.
48 It also has [[Category:Some Category| ]] and then [[Category:Yet another category]].
49 Also link to a [[Talk:TestTitle|talk page]] is here.
50 END;
51 $struct = $this->getStructure( $text );
52 $links = $struct->outgoingLinks();
53 $this->assertContains( "Some_Page", $links );
54 $this->assertContains( "Template:Template", $links );
55 $this->assertContains( "Template:Another_template", $links );
56 $this->assertContains( "Template:Lowercase", $links );
57 $this->assertContains( "Talk:TestTitle", $links );
58 $this->assertCount( 5, $links );
59 }
60
61 public function testTemplates() {
62 $text = <<<END
63 We have a {{Template}} and an {{Another template}} in addition.
64 Some templates are {{lowercase}}. And this {{Template}} is repeated.
65 Here is {{another_template|with=argument}}.
66 This is a template that {{Xdoes not exist}}.
67 END;
68 $this->setTemporaryHook( 'TitleExists', function ( Title $title, &$exists ) {
69 $txt = $title->getBaseText();
70 if ( $txt[0] != 'X' ) {
71 $exists = true;
72 }
73 return true;
74 } );
75 $struct = $this->getStructure( $text );
76 $templates = $struct->templates();
77 $this->assertCount( 3, $templates );
78 $this->assertContains( "Template:Template", $templates );
79 $this->assertContains( "Template:Another template", $templates );
80 $this->assertContains( "Template:Lowercase", $templates );
81 }
82
83 public function testHeadings() {
84 $text = <<<END
85 Some text here
86 == Heading one ==
87 Some text
88 ==== heading two ====
89 More text
90 === Applicability of the strict mass-energy equivalence formula, ''E'' = ''mc''<sup>2</sup> ===
91 and more text
92 == Wikitext '''in''' [[Heading]] and also <b>html</b> ==
93 more text
94 ==== See also ====
95 * Also things to see!
96 END;
97 $struct = $this->getStructure( $text );
98 $headings = $struct->headings();
99 $this->assertCount( 4, $headings );
100 $this->assertContains( "Heading one", $headings );
101 $this->assertContains( "heading two", $headings );
102 $this->assertContains( "Applicability of the strict mass-energy equivalence formula, E = mc2",
103 $headings );
104 $this->assertContains( "Wikitext in Heading and also html", $headings );
105 }
106
107 public function testHeadingsFirst() {
108 $text = <<<END
109 == Heading one ==
110 Some text
111 ==== heading two ====
112 END;
113 $struct = $this->getStructure( $text );
114 $headings = $struct->headings();
115 $this->assertCount( 2, $headings );
116 $this->assertContains( "Heading one", $headings );
117 $this->assertContains( "heading two", $headings );
118 }
119
120 public function testHeadingsNone() {
121 $text = "This text is completely devoid of headings.";
122 $struct = $this->getStructure( $text );
123 $headings = $struct->headings();
124 $this->assertArrayEquals( [], $headings );
125 }
126
127 public function testTexts() {
128 $text = <<<END
129 Opening text is opening.
130 == Then comes header ==
131 Then we got more<br>text
132 === And more headers ===
133 {| class="wikitable"
134 |-
135 ! Header table
136 |-
137 | row in table
138 |-
139 | another row in table
140 |}
141 END;
142 $struct = $this->getStructure( $text );
143 $this->assertEquals( "Opening text is opening.", $struct->getOpeningText() );
144 $this->assertEquals( "Opening text is opening. Then we got more text",
145 $struct->getMainText() );
146 $this->assertEquals( [ "Header table row in table another row in table" ],
147 $struct->getAuxiliaryText() );
148 }
149 }