MediaWiki REL1_33
ExtraParserTest.php
Go to the documentation of this file.
1<?php
2
4
11
13 protected $options;
15 protected $parser;
16
17 protected function setUp() {
18 parent::setUp();
19
20 $contLang = Language::factory( 'en' );
21 $this->setMwGlobals( [
22 'wgShowExceptionDetails' => true,
23 'wgCleanSignatures' => true,
24 ] );
25 $this->setUserLang( 'en' );
26 $this->setContentLang( $contLang );
27
28 // FIXME: This test should pass without setting global content language
29 $this->options = ParserOptions::newFromUserAndLang( new User, $contLang );
30 $this->options->setTemplateCallback( [ __CLASS__, 'statelessFetchTemplate' ] );
31 $this->parser = new Parser;
32
33 MediaWikiServices::getInstance()->resetServiceForTesting( 'MagicWordFactory' );
34 }
35
41 $longLine = '1.' . str_repeat( '1234567890', 100000 ) . "\n";
42
43 $title = Title::newFromText( 'Unit test' );
44 $options = ParserOptions::newFromUser( new User() );
45 $this->assertEquals( "<p>$longLine</p>",
46 $this->parser->parse( $longLine, $title, $options )->getText( [ 'unwrap' => true ] ) );
47 }
48
54 $text = "{{Special:ApiHelp/help}}";
55 $title = Title::newFromText( 'testSpecialPageTransclusionRestoresGlobalState' );
56 $options = ParserOptions::newFromUser( new User() );
57
58 RequestContext::getMain()->setTitle( $title );
59 RequestContext::getMain()->getWikiPage()->CustomTestProp = true;
60
61 $parsed = $this->parser->parse( $text, $title, $options )->getText();
62 $this->assertContains( 'apihelp-header', $parsed );
63
64 // Verify that this property wasn't wiped out by the parse
65 $this->assertTrue( RequestContext::getMain()->getWikiPage()->CustomTestProp );
66 }
67
72 public function testParse() {
73 $title = Title::newFromText( __FUNCTION__ );
74 $parserOutput = $this->parser->parse( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
75 $this->assertEquals(
76 "<p>Test\nContent of <i>Template:Foo</i>\nContent of <i>Template:Bar</i>\n</p>",
77 $parserOutput->getText( [ 'unwrap' => true ] )
78 );
79 }
80
84 public function testPreSaveTransform() {
85 $title = Title::newFromText( __FUNCTION__ );
86 $outputText = $this->parser->preSaveTransform(
87 "Test\r\n{{subst:Foo}}\n{{Bar}}",
88 $title,
89 new User(),
90 $this->options
91 );
92
93 $this->assertEquals( "Test\nContent of ''Template:Foo''\n{{Bar}}", $outputText );
94 }
95
99 public function testPreprocess() {
100 $title = Title::newFromText( __FUNCTION__ );
101 $outputText = $this->parser->preprocess( "Test\n{{Foo}}\n{{Bar}}", $title, $this->options );
102
103 $this->assertEquals(
104 "Test\nContent of ''Template:Foo''\nContent of ''Template:Bar''",
105 $outputText
106 );
107 }
108
113 public function testCleanSig() {
114 $title = Title::newFromText( __FUNCTION__ );
115 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
116
117 $this->assertEquals( "{{SUBST:Foo}} ", $outputText );
118 }
119
124 public function testCleanSigDisabled() {
125 $this->setMwGlobals( 'wgCleanSignatures', false );
126
127 $title = Title::newFromText( __FUNCTION__ );
128 $outputText = $this->parser->cleanSig( "{{Foo}} ~~~~" );
129
130 $this->assertEquals( "{{Foo}} ~~~~", $outputText );
131 }
132
138 public function testCleanSigInSig( $in, $out ) {
139 $this->assertEquals( Parser::cleanSigInSig( $in ), $out );
140 }
141
142 public static function provideStringsForCleanSigInSig() {
143 return [
144 [ "{{Foo}} ~~~~", "{{Foo}} " ],
145 [ "~~~", "" ],
146 [ "~~~~~", "" ],
147 ];
148 }
149
153 public function testGetSection() {
154 $outputText2 = $this->parser->getSection(
155 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
156 . "Section 2\n== Heading 3 ==\nSection 3\n",
157 2
158 );
159 $outputText1 = $this->parser->getSection(
160 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
161 . "Section 2\n== Heading 3 ==\nSection 3\n",
162 1
163 );
164
165 $this->assertEquals( "=== Heading 2 ===\nSection 2", $outputText2 );
166 $this->assertEquals( "== Heading 1 ==\nSection 1\n=== Heading 2 ===\nSection 2", $outputText1 );
167 }
168
172 public function testReplaceSection() {
173 $outputText = $this->parser->replaceSection(
174 "Section 0\n== Heading 1 ==\nSection 1\n=== Heading 2 ===\n"
175 . "Section 2\n== Heading 3 ==\nSection 3\n",
176 1,
177 "New section 1"
178 );
179
180 $this->assertEquals( "Section 0\nNew section 1\n\n== Heading 3 ==\nSection 3", $outputText );
181 }
182
187 public function testGetPreloadText() {
188 $title = Title::newFromText( __FUNCTION__ );
189 $outputText = $this->parser->getPreloadText(
190 "{{Foo}}<noinclude> censored</noinclude> information <!-- is very secret -->",
191 $title,
192 $this->options
193 );
194
195 $this->assertEquals( "{{Foo}} information <!-- is very secret -->", $outputText );
196 }
197
204 static function statelessFetchTemplate( $title, $parser = false ) {
205 $text = "Content of ''" . $title->getFullText() . "''";
206 $deps = [];
207
208 return [
209 'text' => $text,
210 'finalTitle' => $title,
211 'deps' => $deps ];
212 }
213
217 public function testTrackingCategory() {
218 $title = Title::newFromText( __FUNCTION__ );
219 $catName = wfMessage( 'broken-file-category' )->inContentLanguage()->text();
220 $cat = Title::makeTitleSafe( NS_CATEGORY, $catName );
221 $expected = [ $cat->getDBkey() ];
222 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
223 $result = $parserOutput->getCategoryLinks();
224 $this->assertEquals( $expected, $result );
225 }
226
230 public function testTrackingCategorySpecial() {
231 // Special pages shouldn't have tracking cats.
232 $title = SpecialPage::getTitleFor( 'Contributions' );
233 $parserOutput = $this->parser->parse( "[[file:nonexistent]]", $title, $this->options );
234 $result = $parserOutput->getCategoryLinks();
235 $this->assertEmpty( $result );
236 }
237
242 public function testParseLinkParameter( $input, $expected, $expectedLinks, $desc ) {
243 $this->parser->startExternalParse( Title::newFromText( __FUNCTION__ ),
244 $this->options, Parser::OT_HTML );
245 $output = $this->parser->parseLinkParameter( $input );
246
247 $this->assertEquals( $expected[0], $output[0], "$desc (type)" );
248
249 if ( $expected[0] === 'link-title' ) {
250 $this->assertTrue( $expected[1]->equals( $output[1] ), "$desc (target)" );
251 } else {
252 $this->assertEquals( $expected[1], $output[1], "$desc (target)" );
253 }
254
255 foreach ( $expectedLinks as $func => $expected ) {
256 $output = $this->parser->getOutput()->$func();
257 $this->assertEquals( $expected, $output, "$desc ($func)" );
258 }
259 }
260
261 public static function provideParseLinkParameter() {
262 return [
263 [
264 '',
265 [ 'no-link', false ],
266 [],
267 'Return no link when requested',
268 ],
269 [
270 'https://example.com/',
271 [ 'link-url', 'https://example.com/' ],
272 [ 'getExternalLinks' => [ 'https://example.com/' => 1 ] ],
273 'External link',
274 ],
275 [
276 '//example.com/',
277 [ 'link-url', '//example.com/' ],
278 [ 'getExternalLinks' => [ '//example.com/' => 1 ] ],
279 'External link',
280 ],
281 [
282 'Test',
283 [ 'link-title', Title::newFromText( 'Test' ) ],
284 [ 'getLinks' => [ 0 => [ 'Test' => 0 ] ] ],
285 'Internal link',
286 ],
287 [
288 'mw:Test',
289 [ 'link-title', Title::newFromText( 'mw:Test' ) ],
290 [ 'getInterwikiLinks' => [ 'mw' => [ 'Test' => 1 ] ] ],
291 'Internal link (interwiki)',
292 ],
293 [
294 'https://',
295 [ null, false ],
296 [],
297 'Invalid link target',
298 ],
299 [
300 '<>',
301 [ null, false ],
302 [],
303 'Invalid link target',
304 ],
305 [
306 ' ',
307 [ null, false ],
308 [],
309 'Invalid link target',
310 ],
311 ];
312 }
313}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
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()
static provideParseLinkParameter()
testReplaceSection()
Parser::replaceSection.
static statelessFetchTemplate( $title, $parser=false)
testPreprocess()
Parser::preprocess.
ParserOptions $options
testSpecialPageTransclusionRestoresGlobalState()
Parser::braceSubstitution SpecialPageFactory::capturePath.
testParseLinkParameter( $input, $expected, $expectedLinks, $desc)
Parser::parseLinkParameter provideParseLinkParameter.
testTrackingCategorySpecial()
Parser::parse.
static provideStringsForCleanSigInSig()
testCleanSig()
cleanSig() makes all templates substs and removes tildes Parser::cleanSig
testTrackingCategory()
Parser::parse.
testPreSaveTransform()
Parser::preSaveTransform.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
MediaWikiServices is the service locator for the application scope of MediaWiki.
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:69
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:48
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password 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:855
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:955
Using a hook running we can avoid having all this option specific stuff in our mainline code Using the function 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
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 use $formDescriptor instead 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
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place $output
Definition hooks.txt:2272
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
const NS_CATEGORY
Definition Defines.php:87
if(is_array($mode)) switch( $mode) $input