MediaWiki  1.29.1
ParserMethodsTest.php
Go to the documentation of this file.
1 <?php
2 
8 
9  public static function providePreSaveTransform() {
10  return [
11  [ 'hello this is ~~~',
12  "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
13  ],
14  [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
15  'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
16  ],
17  ];
18  }
19 
23  public function testPreSaveTransform( $text, $expected ) {
25 
26  $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
27  $user = new User();
28  $user->setName( "127.0.0.1" );
30  $text = $wgParser->preSaveTransform( $text, $title, $user, $popts );
31 
32  $this->assertEquals( $expected, $text );
33  }
34 
35  public static function provideStripOuterParagraph() {
36  // This mimics the most common use case (stripping paragraphs generated by the parser).
37  $message = new RawMessage( "Message text." );
38 
39  return [
40  [
41  "<p>Text.</p>",
42  "Text.",
43  ],
44  [
45  "<p class='foo'>Text.</p>",
46  "<p class='foo'>Text.</p>",
47  ],
48  [
49  "<p>Text.\n</p>\n",
50  "Text.",
51  ],
52  [
53  "<p>Text.</p><p>More text.</p>",
54  "<p>Text.</p><p>More text.</p>",
55  ],
56  [
57  $message->parse(),
58  "Message text.",
59  ],
60  ];
61  }
62 
66  public function testStripOuterParagraph( $text, $expected ) {
67  $this->assertEquals( $expected, Parser::stripOuterParagraph( $text ) );
68  }
69 
75  public function testRecursiveParse() {
77  $title = Title::newFromText( 'foo' );
78  $po = new ParserOptions;
79  $wgParser->setHook( 'recursivecallparser', [ $this, 'helperParserFunc' ] );
80  $wgParser->parse( '<recursivecallparser>baz</recursivecallparser>', $title, $po );
81  }
82 
83  public function helperParserFunc( $input, $args, $parser ) {
84  $title = Title::newFromText( 'foo' );
85  $po = new ParserOptions;
86  $parser->parse( $input, $title, $po );
87  return 'bar';
88  }
89 
90  public function testCallParserFunction() {
92 
93  // Normal parses test passing PPNodes. Test passing an array.
94  $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
95  $wgParser->startExternalParse( $title, new ParserOptions(), Parser::OT_HTML );
96  $frame = $wgParser->getPreprocessor()->newFrame();
97  $ret = $wgParser->callParserFunction( $frame, '#tag',
98  [ 'pre', 'foo', 'style' => 'margin-left: 1.6em' ]
99  );
100  $ret['text'] = $wgParser->mStripState->unstripBoth( $ret['text'] );
101  $this->assertSame( [
102  'found' => true,
103  'text' => '<pre style="margin-left: 1.6em">foo</pre>',
104  ], $ret, 'callParserFunction works for {{#tag:pre|foo|style=margin-left: 1.6em}}' );
105  }
106 
111  public function testGetSections() {
113 
114  $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
115  $out = $wgParser->parse( "==foo==\n<h2>bar</h2>\n==baz==\n", $title, new ParserOptions() );
116  $this->assertSame( [
117  [
118  'toclevel' => 1,
119  'level' => '2',
120  'line' => 'foo',
121  'number' => '1',
122  'index' => '1',
123  'fromtitle' => $title->getPrefixedDBkey(),
124  'byteoffset' => 0,
125  'anchor' => 'foo',
126  ],
127  [
128  'toclevel' => 1,
129  'level' => '2',
130  'line' => 'bar',
131  'number' => '2',
132  'index' => '',
133  'fromtitle' => false,
134  'byteoffset' => null,
135  'anchor' => 'bar',
136  ],
137  [
138  'toclevel' => 1,
139  'level' => '2',
140  'line' => 'baz',
141  'number' => '3',
142  'index' => '2',
143  'fromtitle' => $title->getPrefixedDBkey(),
144  'byteoffset' => 21,
145  'anchor' => 'baz',
146  ],
147  ], $out->getSections(), 'getSections() with proper value when <h2> is used' );
148  }
149 
153  public function testNormalizeLinkUrl( $explanation, $url, $expected ) {
154  $this->assertEquals( $expected, Parser::normalizeLinkUrl( $url ), $explanation );
155  }
156 
157  public static function provideNormalizeLinkUrl() {
158  return [
159  [
160  'Escaping of unsafe characters',
161  'http://example.org/foo bar?param[]="value"&param[]=valüe',
162  'http://example.org/foo%20bar?param%5B%5D=%22value%22&param%5B%5D=val%C3%BCe',
163  ],
164  [
165  'Case normalization of percent-encoded characters',
166  'http://example.org/%ab%cD%Ef%FF',
167  'http://example.org/%AB%CD%EF%FF',
168  ],
169  [
170  'Unescaping of safe characters',
171  'http://example.org/%3C%66%6f%6F%3E?%3C%66%6f%6F%3E#%3C%66%6f%6F%3E',
172  'http://example.org/%3Cfoo%3E?%3Cfoo%3E#%3Cfoo%3E',
173  ],
174  [
175  'Context-sensitive replacement of sometimes-safe characters',
176  'http://example.org/%23%2F%3F%26%3D%2B%3B?%23%2F%3F%26%3D%2B%3B#%23%2F%3F%26%3D%2B%3B',
177  'http://example.org/%23%2F%3F&=+;?%23/?%26%3D%2B%3B#%23/?&=+;',
178  ],
179  ];
180  }
181 
182  // @todo Add tests for cleanSig() / cleanSigInSig(), getSection(),
183  // replaceSection(), getPreloadText()
184 }
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:33
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:265
$wgParser
$wgParser
Definition: Setup.php:796
ParserMethodsTest\helperParserFunc
helperParserFunc( $input, $args, $parser)
Definition: ParserMethodsTest.php:83
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:246
User
User
Definition: All_system_messages.txt:425
ParserMethodsTest\testNormalizeLinkUrl
testNormalizeLinkUrl( $explanation, $url, $expected)
provideNormalizeLinkUrl
Definition: ParserMethodsTest.php:153
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ParserMethodsTest\providePreSaveTransform
static providePreSaveTransform()
Definition: ParserMethodsTest.php:9
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
ParserMethodsTest
Database Parser.
Definition: ParserMethodsTest.php:7
$input
if(is_array( $mode)) switch( $mode) $input
Definition: postprocess-phan.php:141
ParserMethodsTest\testRecursiveParse
testRecursiveParse()
MWException Parser state cleared while parsing.
Definition: ParserMethodsTest.php:75
ParserMethodsTest\testPreSaveTransform
testPreSaveTransform( $text, $expected)
providePreSaveTransform
Definition: ParserMethodsTest.php:23
$parser
do that in ParserLimitReportFormat instead $parser
Definition: hooks.txt:2536
ParserMethodsTest\testCallParserFunction
testCallParserFunction()
Definition: ParserMethodsTest.php:90
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:6
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1956
$args
if( $line===false) $args
Definition: cdb.php:63
OT_HTML
const OT_HTML
Definition: Defines.php:182
ParserMethodsTest\testStripOuterParagraph
testStripOuterParagraph( $text, $expected)
provideStripOuterParagraph
Definition: ParserMethodsTest.php:66
ParserMethodsTest\provideStripOuterParagraph
static provideStripOuterParagraph()
Definition: ParserMethodsTest.php:35
ParserMethodsTest\provideNormalizeLinkUrl
static provideNormalizeLinkUrl()
Definition: ParserMethodsTest.php:157
ParserMethodsTest\testGetSections
testGetSections()
Parser ParserOutput::getSections.
Definition: ParserMethodsTest.php:111
ParserOptions\newFromUser
static newFromUser( $user)
Get a ParserOptions object from a given user.
Definition: ParserOptions.php:705
$out
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:783