rdbms: Rename "memCache" to "memStash" in LBFactory
[mediawiki.git] / tests / phpunit / includes / ExtraParserTest.php
bloba4e3bb94935c4840d6629decb16d1f202228bcfa
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->options->setWrapOutputClass( false );
30 $this->parser = new Parser;
32 MagicWord::clearCache();
35 /**
36 * @see T10689
37 * @covers Parser::parse
39 public function testLongNumericLinesDontKillTheParser() {
40 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
42 $title = Title::newFromText( 'Unit test' );
43 $options = ParserOptions::newFromUser( new User() );
44 $options->setWrapOutputClass( false );
45 $this->assertEquals( "<p>$longLine</p>",
46 $this->parser->parse( $longLine, $title, $options )->getText() );
49 /**
50 * Test the parser entry points
51 * @covers Parser::parse
53 public function testParse() {
54 $title = Title::newFromText( __FUNCTION__ );
55 $parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
56 $this->assertEquals(
57 "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>",
58 $parserOutput->getText()
62 /**
63 * @covers Parser::preSaveTransform
65 public function testPreSaveTransform() {
66 $title = Title::newFromText( __FUNCTION__ );
67 $outputText = $this->parser->preSaveTransform(
68 "Test\r\n{{subst:Foo}}\n{{Bar}}",
69 $title,
70 new User(),
71 $this->options
74 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
77 /**
78 * @covers Parser::preprocess
80 public function testPreprocess() {
81 $title = Title::newFromText( __FUNCTION__ );
82 $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
84 $this->assertEquals(
85 "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''",
86 $outputText
90 /**
91 * cleanSig() makes all templates substs and removes tildes
92 * @covers Parser::cleanSig
94 public function testCleanSig() {
95 $title = Title::newFromText( __FUNCTION__ );
96 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
98 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
102 * cleanSig() should do nothing if disabled
103 * @covers Parser::cleanSig
105 public function testCleanSigDisabled() {
106 $this->setMwGlobals( 'wgCleanSignatures', false );
108 $title = Title::newFromText( __FUNCTION__ );
109 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
111 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
115 * cleanSigInSig() just removes tildes
116 * @dataProvider provideStringsForCleanSigInSig
117 * @covers Parser::cleanSigInSig
119 public function testCleanSigInSig( $in, $out ) {
120 $this->assertEquals( Parser::cleanSigInSig( $in ), $out );
123 public static function provideStringsForCleanSigInSig() {
124 return [
125 [ "{{Foo}} ~~~~", "{{Foo}} " ],
126 [ "~~~", "" ],
127 [ "~~~~~", "" ],
132 * @covers Parser::getSection
134 public function testGetSection() {
135 $outputText2 = $this->parser->getSection(
136 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
137 . "Section 2\n== Heading 3 ==\nSection 3\n",
140 $outputText1 = $this->parser->getSection(
141 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
142 . "Section 2\n== Heading 3 ==\nSection 3\n",
146 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
147 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
151 * @covers Parser::replaceSection
153 public function testReplaceSection() {
154 $outputText = $this->parser->replaceSection(
155 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
156 . "Section 2\n== Heading 3 ==\nSection 3\n",
158 "New section 1"
161 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
165 * Templates and comments are not affected, but noinclude/onlyinclude is.
166 * @covers Parser::getPreloadText
168 public function testGetPreloadText() {
169 $title = Title::newFromText( __FUNCTION__ );
170 $outputText = $this->parser->getPreloadText(
171 "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->",
172 $title,
173 $this->options
176 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
180 * @param Title $title
181 * @param bool $parser
183 * @return array
185 static function statelessFetchTemplate( $title, $parser = false ) {
186 $text = "Content of ''" . $title->getFullText() . "''";
187 $deps = [];
189 return [
190 'text' => $text,
191 'finalTitle' => $title,
192 'deps' => $deps ];
196 * @group Database
197 * @covers Parser::parse
199 public function testTrackingCategory() {
200 $title = Title::newFromText( __FUNCTION__ );
201 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
202 $cat = Title::makeTitleSafe( NS_CATEGORY, $catName );
203 $expected = [ $cat->getDBkey() ];
204 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
205 $result = $parserOutput->getCategoryLinks();
206 $this->assertEquals( $expected, $result );
210 * @group Database
211 * @covers Parser::parse
213 public function testTrackingCategorySpecial() {
214 // Special pages shouldn't have tracking cats.
215 $title = SpecialPage::getTitleFor( 'Contributions' );
216 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
217 $result = $parserOutput->getCategoryLinks();
218 $this->assertEmpty( $result );