Merge "Allow API results to wrap long lines"
[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 // Bug 8689 - Long numeric lines kill the parser
30 function testBug8689() {
31 global $wgUser;
32 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
33
34 $t = Title::newFromText( 'Unit test' );
35 $options = ParserOptions::newFromUser( $wgUser );
36 $this->assertEquals( "<p>$longLine</p>",
37 $this->parser->parse( $longLine, $t, $options )->getText() );
38 }
39
40 /* Test the parser entry points */
41 function testParse() {
42 $title = Title::newFromText( __FUNCTION__ );
43 $parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
44 $this->assertEquals( "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>", $parserOutput->getText() );
45 }
46
47 function testPreSaveTransform() {
48 global $wgUser;
49 $title = Title::newFromText( __FUNCTION__ );
50 $outputText = $this->parser->preSaveTransform( "Test\r\n{{subst:Foo}}\n{{Bar}}", $title, $wgUser, $this->options );
51
52 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
53 }
54
55 function testPreprocess() {
56 $title = Title::newFromText( __FUNCTION__ );
57 $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
58
59 $this->assertEquals( "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''", $outputText );
60 }
61
62 /**
63 * cleanSig() makes all templates substs and removes tildes
64 */
65 function testCleanSig() {
66 $title = Title::newFromText( __FUNCTION__ );
67 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
68
69 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
70 }
71
72 /**
73 * cleanSig() should do nothing if disabled
74 */
75 function testCleanSigDisabled() {
76 global $wgCleanSignatures;
77 $wgCleanSignatures = false;
78
79 $title = Title::newFromText( __FUNCTION__ );
80 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
81
82 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
83 }
84
85 /**
86 * cleanSigInSig() just removes tildes
87 * @dataProvider provideStringsForCleanSigInSig
88 */
89 function testCleanSigInSig( $in, $out ) {
90 $this->assertEquals( Parser::cleanSigInSig( $in ), $out );
91 }
92
93 public static function provideStringsForCleanSigInSig() {
94 return array(
95 array( "{{Foo}} ~~~~", "{{Foo}} " ),
96 array( "~~~", "" ),
97 array( "~~~~~", "" ),
98 );
99 }
100
101 function testGetSection() {
102 $outputText2 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
103 $outputText1 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 );
104
105 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
106 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
107 }
108
109 function testReplaceSection() {
110 $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" );
111
112 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
113 }
114
115 /**
116 * Templates and comments are not affected, but noinclude/onlyinclude is.
117 */
118 function testGetPreloadText() {
119 $title = Title::newFromText( __FUNCTION__ );
120 $outputText = $this->parser->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options );
121
122 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
123 }
124
125 static function statelessFetchTemplate( $title, $parser = false ) {
126 $text = "Content of ''" . $title->getFullText() . "''";
127 $deps = array();
128
129 return array(
130 'text' => $text,
131 'finalTitle' => $title,
132 'deps' => $deps );
133 }
134
135 /**
136 * @group Database
137 */
138 function testTrackingCategory() {
139 $title = Title::newFromText( __FUNCTION__ );
140 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
141 $cat = Title::makeTitleSafe( NS_CATEGORY, $catName );
142 $expected = array( $cat->getDBkey() );
143 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
144 $result = $parserOutput->getCategoryLinks();
145 $this->assertEquals( $expected, $result );
146 }
147
148 /**
149 * @group Database
150 */
151 function testTrackingCategorySpecial() {
152 // Special pages shouldn't have tracking cats.
153 $title = SpecialPage::getTitleFor( 'Contributions' );
154 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
155 $result = $parserOutput->getCategoryLinks();
156 $this->assertEmpty( $result );
157 }
158 }