10 * @covers Preprocessor_DOM
12 * @covers PPDStackElement
15 * @covers PPTemplateFrame_DOM
16 * @covers PPCustomFrame_DOM
19 * @covers Preprocessor_Hash
20 * @covers PPDStack_Hash
21 * @covers PPDStackElement_Hash
22 * @covers PPDPart_Hash
23 * @covers PPFrame_Hash
24 * @covers PPTemplateFrame_Hash
25 * @covers PPCustomFrame_Hash
26 * @covers PPNode_Hash_Tree
27 * @covers PPNode_Hash_Text
28 * @covers PPNode_Hash_Array
29 * @covers PPNode_Hash_Attr
31 class TagHookTest
extends MediaWikiTestCase
{
32 public static function provideValidNames() {
42 public static function provideBadNames() {
43 return [ [ "foo<bar" ], [ "foo>bar" ], [ "foo\nbar" ], [ "foo\rbar" ] ];
47 * @dataProvider provideValidNames
49 public function testTagHooks( $tag ) {
50 global $wgParserConf, $wgContLang;
51 $parser = new Parser( $wgParserConf );
53 $parser->setHook( $tag, [ $this, 'tagCallback' ] );
54 $parserOutput = $parser->parse(
55 "Foo<$tag>Bar</$tag>Baz",
56 Title
::newFromText( 'Test' ),
57 ParserOptions
::newFromUserAndLang( new User
, $wgContLang )
59 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
61 $parser->mPreprocessor
= null; # Break the Parser <-> Preprocessor cycle
65 * @dataProvider provideBadNames
66 * @expectedException MWException
68 public function testBadTagHooks( $tag ) {
69 global $wgParserConf, $wgContLang;
70 $parser = new Parser( $wgParserConf );
72 $parser->setHook( $tag, [ $this, 'tagCallback' ] );
74 "Foo<$tag>Bar</$tag>Baz",
75 Title
::newFromText( 'Test' ),
76 ParserOptions
::newFromUserAndLang( new User
, $wgContLang )
78 $this->fail( 'Exception not thrown.' );
82 * @dataProvider provideValidNames
84 public function testFunctionTagHooks( $tag ) {
85 global $wgParserConf, $wgContLang;
86 $parser = new Parser( $wgParserConf );
88 $parser->setFunctionTagHook( $tag, [ $this, 'functionTagCallback' ], 0 );
89 $parserOutput = $parser->parse(
90 "Foo<$tag>Bar</$tag>Baz",
91 Title
::newFromText( 'Test' ),
92 ParserOptions
::newFromUserAndLang( new User
, $wgContLang )
94 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
96 $parser->mPreprocessor
= null; # Break the Parser <-> Preprocessor cycle
100 * @dataProvider provideBadNames
101 * @expectedException MWException
103 public function testBadFunctionTagHooks( $tag ) {
104 global $wgParserConf, $wgContLang;
105 $parser = new Parser( $wgParserConf );
107 $parser->setFunctionTagHook(
109 [ $this, 'functionTagCallback' ],
110 Parser
::SFH_OBJECT_ARGS
113 "Foo<$tag>Bar</$tag>Baz",
114 Title
::newFromText( 'Test' ),
115 ParserOptions
::newFromUserAndLang( new User
, $wgContLang )
117 $this->fail( 'Exception not thrown.' );
120 function tagCallback( $text, $params, $parser ) {
121 return str_rot13( $text );
124 function functionTagCallback( &$parser, $frame, $code, $attribs ) {
125 return str_rot13( $code );