6 class TagHookTest
extends MediaWikiTestCase
{
7 public static function provideValidNames() {
8 return array( array( 'foo' ), array( 'foo-bar' ), array( 'foo_bar' ), array( 'FOO-BAR' ), array( 'foo bar' ) );
11 public static function provideBadNames() {
12 return array( array( "foo<bar" ), array( "foo>bar" ), array( "foo\nbar" ), array( "foo\rbar" ) );
15 protected function setUp() {
18 $this->setMwGlobals( 'wgAlwaysUseTidy', false );
22 * @dataProvider provideValidNames
24 function testTagHooks( $tag ) {
25 global $wgParserConf, $wgContLang;
26 $parser = new Parser( $wgParserConf );
28 $parser->setHook( $tag, array( $this, 'tagCallback' ) );
29 $parserOutput = $parser->parse( "Foo<$tag>Bar</$tag>Baz", Title
::newFromText( 'Test' ), ParserOptions
::newFromUserAndLang( new User
, $wgContLang ) );
30 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
32 $parser->mPreprocessor
= null; # Break the Parser <-> Preprocessor cycle
36 * @dataProvider provideBadNames
37 * @expectedException MWException
39 function testBadTagHooks( $tag ) {
40 global $wgParserConf, $wgContLang;
41 $parser = new Parser( $wgParserConf );
43 $parser->setHook( $tag, array( $this, 'tagCallback' ) );
44 $parser->parse( "Foo<$tag>Bar</$tag>Baz", Title
::newFromText( 'Test' ), ParserOptions
::newFromUserAndLang( new User
, $wgContLang ) );
45 $this->fail( 'Exception not thrown.' );
49 * @dataProvider provideValidNames
51 function testFunctionTagHooks( $tag ) {
52 global $wgParserConf, $wgContLang;
53 $parser = new Parser( $wgParserConf );
55 $parser->setFunctionTagHook( $tag, array( $this, 'functionTagCallback' ), 0 );
56 $parserOutput = $parser->parse( "Foo<$tag>Bar</$tag>Baz", Title
::newFromText( 'Test' ), ParserOptions
::newFromUserAndLang( new User
, $wgContLang ) );
57 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
59 $parser->mPreprocessor
= null; # Break the Parser <-> Preprocessor cycle
63 * @dataProvider provideBadNames
64 * @expectedException MWException
66 function testBadFunctionTagHooks( $tag ) {
67 global $wgParserConf, $wgContLang;
68 $parser = new Parser( $wgParserConf );
70 $parser->setFunctionTagHook( $tag, array( $this, 'functionTagCallback' ), SFH_OBJECT_ARGS
);
71 $parser->parse( "Foo<$tag>Bar</$tag>Baz", Title
::newFromText( 'Test' ), ParserOptions
::newFromUserAndLang( new User
, $wgContLang ) );
72 $this->fail( 'Exception not thrown.' );
75 function tagCallback( $text, $params, $parser ) {
76 return str_rot13( $text );
79 function functionTagCallback( &$parser, $frame, $code, $attribs ) {
80 return str_rot13( $code );