summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/ExtraParserTest.php
blob: 5b0aa98ba82972dbbb76669d9397de5917fc59c3 (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
111
112
113
<?php

/**
 * Parser-related tests that don't suit for parserTests.txt
 */
class ExtraParserTest extends MediaWikiTestCase {

	function setUp() {
		global $wgMemc;
		global $wgContLang;
		global $wgShowDBErrorBacktrace;
		global $wgLanguageCode;

		$wgShowDBErrorBacktrace = true;
		$wgLanguageCode = 'en';
		$wgContLang = new Language( 'en' );
		$wgMemc = new EmptyBagOStuff;
		
		$this->options = new ParserOptions;
		$this->options->setTemplateCallback( array( __CLASS__, 'statelessFetchTemplate' ) );
		$this->parser = new Parser;
	}

	// Bug 8689 - Long numeric lines kill the parser
	function testBug8689() {
		global $wgLang;
		global $wgUser;
		$longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";

		if ( $wgLang === null ) $wgLang = new Language;
		
		$t = Title::newFromText( 'Unit test' );
		$options = ParserOptions::newFromUser( $wgUser );
		$this->assertEquals( "<p>$longLine</p>",
			$this->parser->parse( $longLine, $t, $options )->getText() );
	}
	
	/* Test the parser entry points */
	function testParse() {
		$title = Title::newFromText( __FUNCTION__ );
		$parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}" , $title, $this->options );
		$this->assertEquals( "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>", $parserOutput->getText() );
	}
	
	function testPreSaveTransform() {
		global $wgUser;
		$title = Title::newFromText( __FUNCTION__ );
		$outputText = $this->parser->preSaveTransform( "Test\r\n{{subst:Foo}}\n{{Bar}}", $title, $wgUser, $this->options );

		$this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
	}
	
	function testPreprocess() {
		$title = Title::newFromText( __FUNCTION__ );
		$outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}" , $title, $this->options );
		
		$this->assertEquals( "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''", $outputText );
	}
	
	/**
	 * cleanSig() makes all templates substs and removes tildes
	 */
	function testCleanSig() {
		$title = Title::newFromText( __FUNCTION__ );
		$outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
		
		$this->assertEquals( "{{SUBST:Foo}} ", $outputText );
	}
	
	/**
	 * cleanSigInSig() just removes tildes
	 */
	function testCleanSigInSig() {
		$title = Title::newFromText( __FUNCTION__ );
		$outputText = $this->parser->cleanSigInSig( "{{Foo}} ~~~~" );
		
		$this->assertEquals( "{{Foo}} ", $outputText );
	}
	
	function testGetSection() {
		$outputText2 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 2 );
		$outputText1 = $this->parser->getSection( "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2\n== Heading 3 ==\nSection 3\n", 1 );
		
		$this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
		$this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
	}
	
	function testReplaceSection() {
		$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" );
		
		$this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
	}
	
	/**
	 * Templates and comments are not affected, but noinclude/onlyinclude is.
	 */
	function testGetPreloadText() {
		$title = Title::newFromText( __FUNCTION__ );
		$outputText = $this->parser->getPreloadText( "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->", $title, $this->options );
		
		$this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
	}
	
	static function statelessFetchTemplate( $title, $parser=false ) {
		$text = "Content of ''" . $title->getFullText() . "''";
		$deps = array();
		
		return array(
			'text' => $text,
			'finalTitle' => $title,
			'deps' => $deps );
	}
 }