Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / ExtraParserTest.php
blob5dd4f7a07c019e30a49c38130fb24c720b30b11c
1 <?php
3 /**
4 * Parser-related tests that don't suit for parserTests.txt
6 * @group Database
7 */
8 class ExtraParserTest extends MediaWikiTestCase {
10 /** @var ParserOptions */
11 protected $options;
12 /** @var Parser */
13 protected $parser;
15 protected function setUp() {
16 parent::setUp();
18 $contLang = Language::factory( 'en' );
19 $this->setMwGlobals( [
20 'wgShowDBErrorBacktrace' => true,
21 'wgCleanSignatures' => true,
22 ] );
23 $this->setUserLang( 'en' );
24 $this->setContentLang( $contLang );
26 // FIXME: This test should pass without setting global content language
27 $this->options = ParserOptions::newFromUserAndLang( new User, $contLang );
28 $this->options->setTemplateCallback( [ __CLASS__, 'statelessFetchTemplate' ] );
29 $this->parser = new Parser;
31 MagicWord::clearCache();
34 /**
35 * @see Bug 8689
36 * @covers Parser::parse
38 public function testLongNumericLinesDontKillTheParser() {
39 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
41 $title = Title::newFromText( 'Unit test' );
42 $options = ParserOptions::newFromUser( new User() );
43 $this->assertEquals( "<p>$longLine</p>",
44 $this->parser->parse( $longLine, $title, $options )->getText() );
47 /**
48 * Test the parser entry points
49 * @covers Parser::parse
51 public function testParse() {
52 $title = Title::newFromText( __FUNCTION__ );
53 $parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
54 $this->assertEquals(
55 "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>",
56 $parserOutput->getText()
60 /**
61 * @covers Parser::preSaveTransform
63 public function testPreSaveTransform() {
64 $title = Title::newFromText( __FUNCTION__ );
65 $outputText = $this->parser->preSaveTransform(
66 "Test\r\n{{subst:Foo}}\n{{Bar}}",
67 $title,
68 new User(),
69 $this->options
72 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
75 /**
76 * @covers Parser::preprocess
78 public function testPreprocess() {
79 $title = Title::newFromText( __FUNCTION__ );
80 $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
82 $this->assertEquals(
83 "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''",
84 $outputText
88 /**
89 * cleanSig() makes all templates substs and removes tildes
90 * @covers Parser::cleanSig
92 public function testCleanSig() {
93 $title = Title::newFromText( __FUNCTION__ );
94 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
96 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
99 /**
100 * cleanSig() should do nothing if disabled
101 * @covers Parser::cleanSig
103 public function testCleanSigDisabled() {
104 $this->setMwGlobals( 'wgCleanSignatures', false );
106 $title = Title::newFromText( __FUNCTION__ );
107 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
109 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
113 * cleanSigInSig() just removes tildes
114 * @dataProvider provideStringsForCleanSigInSig
115 * @covers Parser::cleanSigInSig
117 public function testCleanSigInSig( $in, $out ) {
118 $this->assertEquals( Parser::cleanSigInSig( $in ), $out );
121 public static function provideStringsForCleanSigInSig() {
122 return [
123 [ "{{Foo}} ~~~~", "{{Foo}} " ],
124 [ "~~~", "" ],
125 [ "~~~~~", "" ],
130 * @covers Parser::getSection
132 public function testGetSection() {
133 $outputText2 = $this->parser->getSection(
134 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
135 . "Section 2\n== Heading 3 ==\nSection 3\n",
138 $outputText1 = $this->parser->getSection(
139 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
140 . "Section 2\n== Heading 3 ==\nSection 3\n",
144 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
145 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
149 * @covers Parser::replaceSection
151 public function testReplaceSection() {
152 $outputText = $this->parser->replaceSection(
153 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
154 . "Section 2\n== Heading 3 ==\nSection 3\n",
156 "New section 1"
159 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
163 * Templates and comments are not affected, but noinclude/onlyinclude is.
164 * @covers Parser::getPreloadText
166 public function testGetPreloadText() {
167 $title = Title::newFromText( __FUNCTION__ );
168 $outputText = $this->parser->getPreloadText(
169 "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->",
170 $title,
171 $this->options
174 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
178 * @param Title $title
179 * @param bool $parser
181 * @return array
183 static function statelessFetchTemplate( $title, $parser = false ) {
184 $text = "Content of ''" . $title->getFullText() . "''";
185 $deps = [];
187 return [
188 'text' => $text,
189 'finalTitle' => $title,
190 'deps' => $deps ];
194 * @group Database
195 * @covers Parser::parse
197 public function testTrackingCategory() {
198 $title = Title::newFromText( __FUNCTION__ );
199 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
200 $cat = Title::makeTitleSafe( NS_CATEGORY, $catName );
201 $expected = [ $cat->getDBkey() ];
202 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
203 $result = $parserOutput->getCategoryLinks();
204 $this->assertEquals( $expected, $result );
208 * @group Database
209 * @covers Parser::parse
211 public function testTrackingCategorySpecial() {
212 // Special pages shouldn't have tracking cats.
213 $title = SpecialPage::getTitleFor( 'Contributions' );
214 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
215 $result = $parserOutput->getCategoryLinks();
216 $this->assertEmpty( $result );