7 class TagHookTest
extends MediaWikiTestCase
{
8 public static function provideValidNames() {
18 public static function provideBadNames() {
19 return array( array( "foo<bar" ), array( "foo>bar" ), array( "foo\nbar" ), array( "foo\rbar" ) );
22 protected function setUp() {
25 $this->setMwGlobals( 'wgAlwaysUseTidy', false );
29 * @dataProvider provideValidNames
30 * @covers Parser::setHook
32 public function testTagHooks( $tag ) {
33 global $wgParserConf, $wgContLang;
34 $parser = new Parser( $wgParserConf );
36 $parser->setHook( $tag, array( $this, 'tagCallback' ) );
37 $parserOutput = $parser->parse(
38 "Foo<$tag>Bar</$tag>Baz",
39 Title
::newFromText( 'Test' ),
40 ParserOptions
::newFromUserAndLang( new User
, $wgContLang )
42 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
44 $parser->mPreprocessor
= null; # Break the Parser <-> Preprocessor cycle
48 * @dataProvider provideBadNames
49 * @expectedException MWException
50 * @covers Parser::setHook
52 public function testBadTagHooks( $tag ) {
53 global $wgParserConf, $wgContLang;
54 $parser = new Parser( $wgParserConf );
56 $parser->setHook( $tag, array( $this, 'tagCallback' ) );
58 "Foo<$tag>Bar</$tag>Baz",
59 Title
::newFromText( 'Test' ),
60 ParserOptions
::newFromUserAndLang( new User
, $wgContLang )
62 $this->fail( 'Exception not thrown.' );
66 * @dataProvider provideValidNames
67 * @covers Parser::setFunctionTagHook
69 public function testFunctionTagHooks( $tag ) {
70 global $wgParserConf, $wgContLang;
71 $parser = new Parser( $wgParserConf );
73 $parser->setFunctionTagHook( $tag, array( $this, 'functionTagCallback' ), 0 );
74 $parserOutput = $parser->parse(
75 "Foo<$tag>Bar</$tag>Baz",
76 Title
::newFromText( 'Test' ),
77 ParserOptions
::newFromUserAndLang( new User
, $wgContLang )
79 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
81 $parser->mPreprocessor
= null; # Break the Parser <-> Preprocessor cycle
85 * @dataProvider provideBadNames
86 * @expectedException MWException
87 * @covers Parser::setFunctionTagHook
89 public function testBadFunctionTagHooks( $tag ) {
90 global $wgParserConf, $wgContLang;
91 $parser = new Parser( $wgParserConf );
93 $parser->setFunctionTagHook( $tag, array( $this, 'functionTagCallback' ), Parser
::SFH_OBJECT_ARGS
);
95 "Foo<$tag>Bar</$tag>Baz",
96 Title
::newFromText( 'Test' ),
97 ParserOptions
::newFromUserAndLang( new User
, $wgContLang )
99 $this->fail( 'Exception not thrown.' );
102 function tagCallback( $text, $params, $parser ) {
103 return str_rot13( $text );
106 function functionTagCallback( &$parser, $frame, $code, $attribs ) {
107 return str_rot13( $code );