test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / includes / ExtraParserTest.php
blob07215c121e8f27aa3f066663c7ab59ea9a6fb37b
1 <?php
3 /**
4 * Parser-related tests that don't suit for parserTests.txt
5 */
6 class ExtraParserTest extends MediaWikiTestCase {
8 protected function setUp() {
9 parent::setUp();
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 ) );
22 $this->options = ParserOptions::newFromUserAndLang( new User, $contLang );
23 $this->options->setTemplateCallback( array( __CLASS__, 'statelessFetchTemplate' ) );
24 $this->parser = new Parser;
26 MagicWord::clearCache();
29 // Bug 8689 - Long numeric lines kill the parser
30 function testBug8689() {
31 global $wgUser;
32 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
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() );
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() );
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 );
52 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
55 function testPreprocess() {
56 $title = Title::newFromText( __FUNCTION__ );
57 $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
59 $this->assertEquals( "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''", $outputText );
62 /**
63 * cleanSig() makes all templates substs and removes tildes
65 function testCleanSig() {
66 $title = Title::newFromText( __FUNCTION__ );
67 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
69 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
72 /**
73 * cleanSig() should do nothing if disabled
75 function testCleanSigDisabled() {
76 $this->setMwGlobals( 'wgCleanSignatures', false );
78 $title = Title::newFromText( __FUNCTION__ );
79 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
81 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
84 /**
85 * cleanSigInSig() just removes tildes
86 * @dataProvider provideStringsForCleanSigInSig
88 function testCleanSigInSig( $in, $out ) {
89 $this->assertEquals( Parser::cleanSigInSig( $in ), $out );
92 public static function provideStringsForCleanSigInSig() {
93 return array(
94 array( "{{Foo}} ~~~~", "{{Foo}} " ),
95 array( "~~~", "" ),
96 array( "~~~~~", "" ),
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 );
104 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
105 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
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" );
111 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
115 * Templates and comments are not affected, but noinclude/onlyinclude is.
117 function testGetPreloadText() {
118 $title = Title::newFromText( __FUNCTION__ );
119 $outputText = $this->parser->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options );
121 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
124 static function statelessFetchTemplate( $title, $parser = false ) {
125 $text = "Content of ''" . $title->getFullText() . "''";
126 $deps = array();
128 return array(
129 'text' => $text,
130 'finalTitle' => $title,
131 'deps' => $deps );
135 * @group Database
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 );
148 * @group Database
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 );