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