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 TagHooksTest
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 );
53 * @dataProvider provideValidNames
55 public function testTagHooks( $tag ) {
57 $parser = new Parser( $wgParserConf );
59 $parser->setHook( $tag, [ $this, 'tagCallback' ] );
60 $parserOutput = $parser->parse(
61 "Foo<$tag>Bar</$tag>Baz",
62 Title
::newFromText( 'Test' ),
63 $this->getParserOptions()
65 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText( [ 'unwrap' => true ] ) );
67 $parser->mPreprocessor
= null; # Break the Parser <-> Preprocessor cycle
71 * @dataProvider provideBadNames
72 * @expectedException MWException
74 public function testBadTagHooks( $tag ) {
76 $parser = new Parser( $wgParserConf );
78 $parser->setHook( $tag, [ $this, 'tagCallback' ] );
80 "Foo<$tag>Bar</$tag>Baz",
81 Title
::newFromText( 'Test' ),
82 $this->getParserOptions()
84 $this->fail( 'Exception not thrown.' );
88 * @dataProvider provideValidNames
90 public function testFunctionTagHooks( $tag ) {
92 $parser = new Parser( $wgParserConf );
94 $parser->setFunctionTagHook( $tag, [ $this, 'functionTagCallback' ], 0 );
95 $parserOutput = $parser->parse(
96 "Foo<$tag>Bar</$tag>Baz",
97 Title
::newFromText( 'Test' ),
98 $this->getParserOptions()
100 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText( [ 'unwrap' => true ] ) );
102 $parser->mPreprocessor
= null; # Break the Parser <-> Preprocessor cycle
106 * @dataProvider provideBadNames
107 * @expectedException MWException
109 public function testBadFunctionTagHooks( $tag ) {
110 global $wgParserConf;
111 $parser = new Parser( $wgParserConf );
113 $parser->setFunctionTagHook(
115 [ $this, 'functionTagCallback' ],
116 Parser
::SFH_OBJECT_ARGS
119 "Foo<$tag>Bar</$tag>Baz",
120 Title
::newFromText( 'Test' ),
121 $this->getParserOptions()
123 $this->fail( 'Exception not thrown.' );
126 function tagCallback( $text, $params, $parser ) {
127 return str_rot13( $text );
130 function functionTagCallback( &$parser, $frame, $code, $attribs ) {
131 return str_rot13( $code );