Merge "Some additional test cases for Parsoid."
[lhc/web/wiklou.git] / tests / phpunit / includes / ExtraParserTest.php
1 <?php
2
3 /**
4 * Parser-related tests that don't suit for parserTests.txt
5 */
6 class ExtraParserTest extends MediaWikiTestCase {
7
8 protected function setUp() {
9 parent::setUp();
10
11 $contLang = Language::factory( 'en' );
12 $this->setMwGlobals( array(
13 'wgShowDBErrorBacktrace' => true,
14 'wgLanguageCode' => 'en',
15 'wgContLang' => $contLang,
16 'wgLang' => Language::factory( 'en' ),
17 'wgMemc' => new EmptyBagOStuff,
18 'wgAlwaysUseTidy' => false,
19 'wgCleanSignatures' => true,
20 ) );
21
22 $this->options = ParserOptions::newFromUserAndLang( new User, $contLang );
23 $this->options->setTemplateCallback( array( __CLASS__, 'statelessFetchTemplate' ) );
24 $this->parser = new Parser;
25
26 MagicWord::clearCache();
27 }
28
29 /**
30 * Bug 8689 - Long numeric lines kill the parser
31 *
32 * @group Database
33 */
34 function testBug8689() {
35 global $wgUser;
36 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
37
38 $t = Title::newFromText( 'Unit test' );
39 $options = ParserOptions::newFromUser( $wgUser );
40 $this->assertEquals( "<p>$longLine</p>",
41 $this->parser->parse( $longLine, $t, $options )->getText() );
42 }
43
44 /**
45 * Test the parser entry points
46 *
47 * @group Database
48 */
49 function testParse() {
50 $title = Title::newFromText( __FUNCTION__ );
51 $parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
52 $this->assertEquals( "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>", $parserOutput->getText() );
53 }
54
55 /**
56 * @group Database
57 */
58 function testPreSaveTransform() {
59 global $wgUser;
60 $title = Title::newFromText( __FUNCTION__ );
61 $outputText = $this->parser->preSaveTransform( "Test\r\n{{subst:Foo}}\n{{Bar}}", $title, $wgUser, $this->options );
62
63 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
64 }
65
66 /**
67 * @group Database
68 */
69 function testPreprocess() {
70 $title = Title::newFromText( __FUNCTION__ );
71 $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
72
73 $this->assertEquals( "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''", $outputText );
74 }
75
76 /**
77 * cleanSig() makes all templates substs and removes tildes
78 */
79 function testCleanSig() {
80 $title = Title::newFromText( __FUNCTION__ );
81 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
82
83 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
84 }
85
86 /**
87 * cleanSig() should do nothing if disabled
88 */
89 function testCleanSigDisabled() {
90 global $wgCleanSignatures;
91 $wgCleanSignatures = false;
92
93 $title = Title::newFromText( __FUNCTION__ );
94 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
95
96 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
97 }
98
99 /**
100 * cleanSigInSig() just removes tildes
101 * @dataProvider provideStringsForCleanSigInSig
102 */
103 function testCleanSigInSig( $in, $out ) {
104 $this->assertEquals( Parser::cleanSigInSig( $in ), $out );
105 }
106
107 public static function provideStringsForCleanSigInSig() {
108 return array(
109 array( "{{Foo}} ~~~~", "{{Foo}} " ),
110 array( "~~~", "" ),
111 array( "~~~~~", "" ),
112 );
113 }
114
115 function testGetSection() {
116 $outputText2 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
117 $outputText1 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 );
118
119 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
120 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
121 }
122
123 function testReplaceSection() {
124 $outputText = $this->parser->replaceSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1, "New section 1" );
125
126 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
127 }
128
129 /**
130 * Templates and comments are not affected, but noinclude/onlyinclude is.
131 */
132 function testGetPreloadText() {
133 $title = Title::newFromText( __FUNCTION__ );
134 $outputText = $this->parser->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options );
135
136 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
137 }
138
139 static function statelessFetchTemplate( $title, $parser = false ) {
140 $text = "Content of ''" . $title->getFullText() . "''";
141 $deps = array();
142
143 return array(
144 'text' => $text,
145 'finalTitle' => $title,
146 'deps' => $deps );
147 }
148
149 /**
150 * @group Database
151 */
152 function testTrackingCategory() {
153 $title = Title::newFromText( __FUNCTION__ );
154 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
155 $cat = Title::makeTitleSafe( NS_CATEGORY, $catName );
156 $expected = array( $cat->getDBkey() );
157 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
158 $result = $parserOutput->getCategoryLinks();
159 $this->assertEquals( $expected, $result );
160 }
161
162 /**
163 * @group Database
164 */
165 function testTrackingCategorySpecial() {
166 // Special pages shouldn't have tracking cats.
167 $title = SpecialPage::getTitleFor( 'Contributions' );
168 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
169 $result = $parserOutput->getCategoryLinks();
170 $this->assertEmpty( $result );
171 }
172 }