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" ] ];
46 private function getParserOptions() {
48 $popt = ParserOptions
::newFromUserAndLang( new User
, $wgContLang );
49 $popt->setWrapOutputClass( false );
54 * @dataProvider provideValidNames
56 public function testTagHooks( $tag ) {
58 $parser = new Parser( $wgParserConf );
60 $parser->setHook( $tag, [ $this, 'tagCallback' ] );
61 $parserOutput = $parser->parse(
62 "Foo<$tag>Bar</$tag>Baz",
63 Title
::newFromText( 'Test' ),
64 $this->getParserOptions()
66 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
68 $parser->mPreprocessor
= null; # Break the Parser <-> Preprocessor cycle
72 * @dataProvider provideBadNames
73 * @expectedException MWException
75 public function testBadTagHooks( $tag ) {
77 $parser = new Parser( $wgParserConf );
79 $parser->setHook( $tag, [ $this, 'tagCallback' ] );
81 "Foo<$tag>Bar</$tag>Baz",
82 Title
::newFromText( 'Test' ),
83 $this->getParserOptions()
85 $this->fail( 'Exception not thrown.' );
89 * @dataProvider provideValidNames
91 public function testFunctionTagHooks( $tag ) {
93 $parser = new Parser( $wgParserConf );
95 $parser->setFunctionTagHook( $tag, [ $this, 'functionTagCallback' ], 0 );
96 $parserOutput = $parser->parse(
97 "Foo<$tag>Bar</$tag>Baz",
98 Title
::newFromText( 'Test' ),
99 $this->getParserOptions()
101 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
103 $parser->mPreprocessor
= null; # Break the Parser <-> Preprocessor cycle
107 * @dataProvider provideBadNames
108 * @expectedException MWException
110 public function testBadFunctionTagHooks( $tag ) {
111 global $wgParserConf;
112 $parser = new Parser( $wgParserConf );
114 $parser->setFunctionTagHook(
116 [ $this, 'functionTagCallback' ],
117 Parser
::SFH_OBJECT_ARGS
120 "Foo<$tag>Bar</$tag>Baz",
121 Title
::newFromText( 'Test' ),
122 $this->getParserOptions()
124 $this->fail( 'Exception not thrown.' );
127 function tagCallback( $text, $params, $parser ) {
128 return str_rot13( $text );
131 function functionTagCallback( &$parser, $frame, $code, $attribs ) {
132 return str_rot13( $code );