MediaWiki  1.28.0
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 
24  public function testPreSaveTransform( $text, $expected ) {
26 
27  $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
28  $user = new User();
29  $user->setName( "127.0.0.1" );
31  $text = $wgParser->preSaveTransform( $text, $title, $user, $popts );
32 
33  $this->assertEquals( $expected, $text );
34  }
35 
36  public static function provideStripOuterParagraph() {
37  // This mimics the most common use case (stripping paragraphs generated by the parser).
38  $message = new RawMessage( "Message text." );
39 
40  return [
41  [
42  "<p>Text.</p>",
43  "Text.",
44  ],
45  [
46  "<p class='foo'>Text.</p>",
47  "<p class='foo'>Text.</p>",
48  ],
49  [
50  "<p>Text.\n</p>\n",
51  "Text.",
52  ],
53  [
54  "<p>Text.</p><p>More text.</p>",
55  "<p>Text.</p><p>More text.</p>",
56  ],
57  [
58  $message->parse(),
59  "Message text.",
60  ],
61  ];
62  }
63 
68  public function testStripOuterParagraph( $text, $expected ) {
69  $this->assertEquals( $expected, Parser::stripOuterParagraph( $text ) );
70  }
71 
78  public function testRecursiveParse() {
80  $title = Title::newFromText( 'foo' );
81  $po = new ParserOptions;
82  $wgParser->setHook( 'recursivecallparser', [ $this, 'helperParserFunc' ] );
83  $wgParser->parse( '<recursivecallparser>baz</recursivecallparser>', $title, $po );
84  }
85 
86  public function helperParserFunc( $input, $args, $parser ) {
87  $title = Title::newFromText( 'foo' );
88  $po = new ParserOptions;
89  $parser->parse( $input, $title, $po );
90  return 'bar';
91  }
92 
96  public function testCallParserFunction() {
98 
99  // Normal parses test passing PPNodes. Test passing an array.
100  $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
101  $wgParser->startExternalParse( $title, new ParserOptions(), Parser::OT_HTML );
102  $frame = $wgParser->getPreprocessor()->newFrame();
103  $ret = $wgParser->callParserFunction( $frame, '#tag',
104  [ 'pre', 'foo', 'style' => 'margin-left: 1.6em' ]
105  );
106  $ret['text'] = $wgParser->mStripState->unstripBoth( $ret['text'] );
107  $this->assertSame( [
108  'found' => true,
109  'text' => '<pre style="margin-left: 1.6em">foo</pre>',
110  ], $ret, 'callParserFunction works for {{#tag:pre|foo|style=margin-left: 1.6em}}' );
111  }
112 
117  public function testGetSections() {
119 
120  $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
121  $out = $wgParser->parse( "==foo==\n<h2>bar</h2>\n==baz==\n", $title, new ParserOptions() );
122  $this->assertSame( [
123  [
124  'toclevel' => 1,
125  'level' => '2',
126  'line' => 'foo',
127  'number' => '1',
128  'index' => '1',
129  'fromtitle' => $title->getPrefixedDBkey(),
130  'byteoffset' => 0,
131  'anchor' => 'foo',
132  ],
133  [
134  'toclevel' => 1,
135  'level' => '2',
136  'line' => 'bar',
137  'number' => '2',
138  'index' => '',
139  'fromtitle' => false,
140  'byteoffset' => null,
141  'anchor' => 'bar',
142  ],
143  [
144  'toclevel' => 1,
145  'level' => '2',
146  'line' => 'baz',
147  'number' => '3',
148  'index' => '2',
149  'fromtitle' => $title->getPrefixedDBkey(),
150  'byteoffset' => 21,
151  'anchor' => 'baz',
152  ],
153  ], $out->getSections(), 'getSections() with proper value when <h2> is used' );
154  }
155 
161  public function testNormalizeLinkUrl( $explanation, $url, $expected ) {
162  $this->assertEquals( $expected, Parser::normalizeLinkUrl( $url ), $explanation );
163  }
164 
165  public static function provideNormalizeLinkUrl() {
166  return [
167  [
168  'Escaping of unsafe characters',
169  'http://example.org/foo bar?param[]="value"&param[]=valüe',
170  'http://example.org/foo%20bar?param%5B%5D=%22value%22&param%5B%5D=val%C3%BCe',
171  ],
172  [
173  'Case normalization of percent-encoded characters',
174  'http://example.org/%ab%cD%Ef%FF',
175  'http://example.org/%AB%CD%EF%FF',
176  ],
177  [
178  'Unescaping of safe characters',
179  'http://example.org/%3C%66%6f%6F%3E?%3C%66%6f%6F%3E#%3C%66%6f%6F%3E',
180  'http://example.org/%3Cfoo%3E?%3Cfoo%3E#%3Cfoo%3E',
181  ],
182  [
183  'Context-sensitive replacement of sometimes-safe characters',
184  'http://example.org/%23%2F%3F%26%3D%2B%3B?%23%2F%3F%26%3D%2B%3B#%23%2F%3F%26%3D%2B%3B',
185  'http://example.org/%23%2F%3F&=+;?%23/?%26%3D%2B%3B#%23/?&=+;',
186  ],
187  ];
188  }
189 
190  // @todo Add tests for cleanSig() / cleanSigInSig(), getSection(),
191  // replaceSection(), getPreloadText()
192 }
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:802
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:1936
Set options of the Parser.
$wgParser
Definition: Setup.php:821
static providePreSaveTransform()
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:262
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
static newFromUser($user)
Get a ParserOptions object from a given user.
magic word & $parser
Definition: hooks.txt:2487
if($line===false) $args
Definition: cdb.php:64
static stripOuterParagraph($html)
Strip outer.
Definition: Parser.php:6034
testCallParserFunction()
Parser::callParserFunction.
testRecursiveParse()
MWException Parser state cleared while parsing.
Base class that store and restore the Language objects.
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:953
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 local account $user
Definition: hooks.txt:242
testNormalizeLinkUrl($explanation, $url, $expected)
provideNormalizeLinkUrl Parser::normalizeLinkUrl Parser::normalizeUrlComponent
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
Variant of the Message class.
Definition: Message.php:1242
helperParserFunc($input, $args, $parser)
testGetSections()
Parser::parse ParserOutput::getSections.
static normalizeLinkUrl($url)
Replace unusual escape codes in a URL with their equivalent characters.
Definition: Parser.php:1956
testPreSaveTransform($text, $expected)
providePreSaveTransform Parser::preSaveTransform
testStripOuterParagraph($text, $expected)
provideStripOuterParagraph Parser::stripOuterParagraph
static provideStripOuterParagraph()
const OT_HTML
Definition: Parser.php:110
static provideNormalizeLinkUrl()