summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/includes/content/WikitextStructureTest.php
blob: 88f4d8f74c300cb345c8e1751c0e23a4d2818976 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php

/**
 * @covers WikiTextStructure
 */
class WikitextStructureTest extends MediaWikiLangTestCase {

	private function getMockTitle() {
		return Title::newFromText( "TestTitle" );
	}

	/**
	 * Get parser output for Wiki text
	 * @param string $text
	 * @return ParserOutput
	 */
	private function getParserOutput( $text ) {
		$content = new WikitextContent( $text );
		return $content->getParserOutput( $this->getMockTitle() );
	}

	/**
	 * Get WikitextStructure for given text
	 * @param string $text
	 * @return WikiTextStructure
	 */
	private function getStructure( $text ) {
		return new WikiTextStructure( $this->getParserOutput( $text ) );
	}

	public function testHeadings() {
		$text = <<<END
Some text here
== Heading one ==
Some text
==== heading two ====
More text
=== Applicability of the strict mass-energy equivalence formula, ''E'' = ''mc''<sup>2</sup> ===
and more text
== Wikitext '''in''' [[Heading]] and also <b>html</b> ==
more text
==== See also ====
* Also things to see!
END;
		$struct = $this->getStructure( $text );
		$headings = $struct->headings();
		$this->assertCount( 4, $headings );
		$this->assertContains( "Heading one", $headings );
		$this->assertContains( "heading two", $headings );
		$this->assertContains( "Applicability of the strict mass-energy equivalence formula, E = mc2",
			$headings );
		$this->assertContains( "Wikitext in Heading and also html", $headings );
	}

	public function testDefaultSort() {
		$text = <<<END
Louise Michel
== Heading one ==
Some text
==== See also ====
* Also things to see!
{{DEFAULTSORT:Michel, Louise}}
END;
		$struct = $this->getStructure( $text );
		$this->assertEquals( "Michel, Louise", $struct->getDefaultSort() );
	}

	public function testHeadingsFirst() {
		$text = <<<END
== Heading one ==
Some text
==== heading two ====
END;
		$struct = $this->getStructure( $text );
		$headings = $struct->headings();
		$this->assertCount( 2, $headings );
		$this->assertContains( "Heading one", $headings );
		$this->assertContains( "heading two", $headings );
	}

	public function testHeadingsNone() {
		$text = "This text is completely devoid of headings.";
		$struct = $this->getStructure( $text );
		$headings = $struct->headings();
		$this->assertArrayEquals( [], $headings );
	}

	public function testTexts() {
		$text = <<<END
Opening text is opening.
== Then comes header ==
Then we got more<br>text
=== And more headers ===
{| class="wikitable"
|-
! Header table
|-
| row in table
|-
| another row in table
|}
END;
		$struct = $this->getStructure( $text );
		$this->assertEquals( "Opening text is opening.", $struct->getOpeningText() );
		$this->assertEquals( "Opening text is opening.   Then we got more text",
			$struct->getMainText() );
		$this->assertEquals( [ "Header table row in table another row in table" ],
			$struct->getAuxiliaryText() );
	}
}