7 class TagHookTest
extends MediaWikiTestCase
{
8 public static function provideValidNames() {
18 public static function provideBadNames() {
19 return [ [ "foo<bar" ], [ "foo>bar" ], [ "foo\nbar" ], [ "foo\rbar" ] ];
23 * @dataProvider provideValidNames
24 * @covers Parser::setHook
26 public function testTagHooks( $tag ) {
27 global $wgParserConf, $wgContLang;
28 $parser = new Parser( $wgParserConf );
30 $parser->setHook( $tag, [ $this, 'tagCallback' ] );
31 $parserOutput = $parser->parse(
32 "Foo<$tag>Bar</$tag>Baz",
33 Title
::newFromText( 'Test' ),
34 ParserOptions
::newFromUserAndLang( new User
, $wgContLang )
36 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
38 $parser->mPreprocessor
= null; # Break the Parser <-> Preprocessor cycle
42 * @dataProvider provideBadNames
43 * @expectedException MWException
44 * @covers Parser::setHook
46 public function testBadTagHooks( $tag ) {
47 global $wgParserConf, $wgContLang;
48 $parser = new Parser( $wgParserConf );
50 $parser->setHook( $tag, [ $this, 'tagCallback' ] );
52 "Foo<$tag>Bar</$tag>Baz",
53 Title
::newFromText( 'Test' ),
54 ParserOptions
::newFromUserAndLang( new User
, $wgContLang )
56 $this->fail( 'Exception not thrown.' );
60 * @dataProvider provideValidNames
61 * @covers Parser::setFunctionTagHook
63 public function testFunctionTagHooks( $tag ) {
64 global $wgParserConf, $wgContLang;
65 $parser = new Parser( $wgParserConf );
67 $parser->setFunctionTagHook( $tag, [ $this, 'functionTagCallback' ], 0 );
68 $parserOutput = $parser->parse(
69 "Foo<$tag>Bar</$tag>Baz",
70 Title
::newFromText( 'Test' ),
71 ParserOptions
::newFromUserAndLang( new User
, $wgContLang )
73 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
75 $parser->mPreprocessor
= null; # Break the Parser <-> Preprocessor cycle
79 * @dataProvider provideBadNames
80 * @expectedException MWException
81 * @covers Parser::setFunctionTagHook
83 public function testBadFunctionTagHooks( $tag ) {
84 global $wgParserConf, $wgContLang;
85 $parser = new Parser( $wgParserConf );
87 $parser->setFunctionTagHook(
89 [ $this, 'functionTagCallback' ],
90 Parser
::SFH_OBJECT_ARGS
93 "Foo<$tag>Bar</$tag>Baz",
94 Title
::newFromText( 'Test' ),
95 ParserOptions
::newFromUserAndLang( new User
, $wgContLang )
97 $this->fail( 'Exception not thrown.' );
100 function tagCallback( $text, $params, $parser ) {
101 return str_rot13( $text );
104 function functionTagCallback( &$parser, $frame, $code, $attribs ) {
105 return str_rot13( $code );