Make content handlers assemble content for search
[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 END;
95 // FIXME: add test for ==== See also ==== after cirrussearch-ignored-headings is renamed
96 $struct = $this->getStructure( $text );
97 $headings = $struct->headings();
98 $this->assertCount( 4, $headings );
99 $this->assertContains( "Heading one", $headings );
100 $this->assertContains( "heading two", $headings );
101 $this->assertContains( "Applicability of the strict mass-energy equivalence formula, E = mc2",
102 $headings );
103 $this->assertContains( "Wikitext in Heading and also html", $headings );
104 }
105
106 public function testHeadingsFirst() {
107 $text = <<<END
108 == Heading one ==
109 Some text
110 ==== heading two ====
111 END;
112 $struct = $this->getStructure( $text );
113 $headings = $struct->headings();
114 $this->assertCount( 2, $headings );
115 $this->assertContains( "Heading one", $headings );
116 $this->assertContains( "heading two", $headings );
117 }
118
119 public function testHeadingsNone() {
120 $text = "This text is completely devoid of headings.";
121 $struct = $this->getStructure( $text );
122 $headings = $struct->headings();
123 $this->assertArrayEquals( [], $headings );
124 }
125
126 public function testTexts() {
127 $text = <<<END
128 Opening text is opening.
129 == Then comes header ==
130 Then we got more<br>text
131 === And more headers ===
132 {| class="wikitable"
133 |-
134 ! Header table
135 |-
136 | row in table
137 |-
138 | another row in table
139 |}
140 END;
141 $struct = $this->getStructure( $text );
142 $this->assertEquals( "Opening text is opening.", $struct->getOpeningText() );
143 $this->assertEquals( "Opening text is opening. Then we got more text",
144 $struct->getMainText() );
145 $this->assertEquals( [ "Header table row in table another row in table" ],
146 $struct->getAuxiliaryText() );
147 }
148 }