MediaWiki REL1_31
ExtraParserTest.php
Go to the documentation of this file.
1<?php
2
9
11 protected $options;
13 protected $parser;
14
15 protected function setUp() {
16 parent::setUp();
17
18 $contLang = Language::factory( 'en' );
19 $this->setMwGlobals( [
20 'wgShowDBErrorBacktrace' => true,
21 'wgCleanSignatures' => true,
22 ] );
23 $this->setUserLang( 'en' );
24 $this->setContentLang( $contLang );
25
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;
30
32 }
33
39 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
40
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( [ 'unwrap' => true ] ) );
45 }
46
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( [ 'unwrap' => true ] )
57 );
58 }
59
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
70 );
71
72 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
73 }
74
78 public function testPreprocess() {
79 $title = Title::newFromText( __FUNCTION__ );
80 $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
81
82 $this->assertEquals(
83 "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''",
84 $outputText
85 );
86 }
87
92 public function testCleanSig() {
93 $title = Title::newFromText( __FUNCTION__ );
94 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
95
96 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
97 }
98
103 public function testCleanSigDisabled() {
104 $this->setMwGlobals( 'wgCleanSignatures', false );
105
106 $title = Title::newFromText( __FUNCTION__ );
107 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
108
109 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
110 }
111
117 public function testCleanSigInSig( $in, $out ) {
118 $this->assertEquals( Parser::cleanSigInSig( $in ), $out );
119 }
120
121 public static function provideStringsForCleanSigInSig() {
122 return [
123 [ "{{Foo}} ~~~~", "{{Foo}} " ],
124 [ "~~~", "" ],
125 [ "~~~~~", "" ],
126 ];
127 }
128
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",
136 2
137 );
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",
141 1
142 );
143
144 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
145 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
146 }
147
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",
155 1,
156 "New section 1"
157 );
158
159 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
160 }
161
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
172 );
173
174 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
175 }
176
183 static function statelessFetchTemplate( $title, $parser = false ) {
184 $text = "Content of ''" . $title->getFullText() . "''";
185 $deps = [];
186
187 return [
188 'text' => $text,
189 'finalTitle' => $title,
190 'deps' => $deps ];
191 }
192
196 public function testTrackingCategory() {
197 $title = Title::newFromText( __FUNCTION__ );
198 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
199 $cat = Title::makeTitleSafe( NS_CATEGORY, $catName );
200 $expected = [ $cat->getDBkey() ];
201 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
202 $result = $parserOutput->getCategoryLinks();
203 $this->assertEquals( $expected, $result );
204 }
205
209 public function testTrackingCategorySpecial() {
210 // Special pages shouldn't have tracking cats.
211 $title = SpecialPage::getTitleFor( 'Contributions' );
212 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
213 $result = $parserOutput->getCategoryLinks();
214 $this->assertEmpty( $result );
215 }
216}
Parser-related tests that don't suit for parserTests.txt.
testGetSection()
Parser::getSection.
testCleanSigDisabled()
cleanSig() should do nothing if disabled Parser::cleanSig
testParse()
Test the parser entry points Parser::parse.
testGetPreloadText()
Templates and comments are not affected, but noinclude/onlyinclude is.
testCleanSigInSig( $in, $out)
cleanSigInSig() just removes tildes provideStringsForCleanSigInSig Parser::cleanSigInSig
testLongNumericLinesDontKillTheParser()
testReplaceSection()
Parser::replaceSection.
static statelessFetchTemplate( $title, $parser=false)
testPreprocess()
Parser::preprocess.
ParserOptions $options
testTrackingCategorySpecial()
Parser::parse.
static provideStringsForCleanSigInSig()
testCleanSig()
cleanSig() makes all templates substs and removes tildes Parser::cleanSig
testTrackingCategory()
Parser::parse.
testPreSaveTransform()
Parser::preSaveTransform.
static clearCache()
Clear the self::$mObjects variable For use in parser tests.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Set options of the Parser.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:70
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:53
We ve cleaned up the code here by removing clumps of infrequently used code and moving them off somewhere else It s much easier for someone working with this code to see what s _really_ going and make changes or fix bugs In we can take all the code that deals with the little used title reversing options(say) and put it in one place. Instead of having little title-reversing if-blocks spread all over the codebase in showAnArticle
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:964
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt;div ...>$1&lt;/div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:864
const NS_CATEGORY
Definition Defines.php:88