MediaWiki  master
TagHooksTest.php
Go to the documentation of this file.
00001 <?php
00002 
00006 class TagHookTest extends MediaWikiTestCase {
00007         
00008         public static function provideValidNames() {
00009                 return array( array( 'foo' ), array( 'foo-bar' ), array( 'foo_bar' ), array( 'FOO-BAR' ), array( 'foo bar' ) );
00010         }
00011 
00012         public static function provideBadNames() {
00013                 return array( array( "foo<bar" ), array( "foo>bar" ), array( "foo\nbar" ),  array( "foo\rbar" ) );
00014         }
00015 
00016         protected function setUp() {
00017                 parent::setUp();
00018 
00019                 $this->setMwGlobals( 'wgAlwaysUseTidy', false );
00020         }
00021 
00025         function testTagHooks( $tag ) {
00026                 global $wgParserConf, $wgContLang;
00027                 $parser = new Parser( $wgParserConf );
00028                 
00029                 $parser->setHook( $tag, array( $this, 'tagCallback' ) );
00030                 $parserOutput = $parser->parse( "Foo<$tag>Bar</$tag>Baz", Title::newFromText( 'Test' ), ParserOptions::newFromUserAndLang( new User, $wgContLang ) );
00031                 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
00032                 
00033                 $parser->mPreprocessor = null; # Break the Parser <-> Preprocessor cycle
00034         }
00035         
00040         function testBadTagHooks( $tag ) {
00041                 global $wgParserConf, $wgContLang;
00042                 $parser = new Parser( $wgParserConf );
00043                 
00044                 $parser->setHook( $tag, array( $this, 'tagCallback' ) );
00045                 $parser->parse( "Foo<$tag>Bar</$tag>Baz", Title::newFromText( 'Test' ), ParserOptions::newFromUserAndLang( new User, $wgContLang ) );
00046                 $this->fail('Exception not thrown.');
00047         }
00048         
00052         function testFunctionTagHooks( $tag ) {
00053                 global $wgParserConf, $wgContLang;
00054                 $parser = new Parser( $wgParserConf );
00055                 
00056                 $parser->setFunctionTagHook( $tag, array( $this, 'functionTagCallback' ), 0 );
00057                 $parserOutput = $parser->parse( "Foo<$tag>Bar</$tag>Baz", Title::newFromText( 'Test' ), ParserOptions::newFromUserAndLang( new User, $wgContLang ) );
00058                 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
00059                 
00060                 $parser->mPreprocessor = null; # Break the Parser <-> Preprocessor cycle
00061         }
00062         
00067         function testBadFunctionTagHooks( $tag ) {
00068                 global $wgParserConf, $wgContLang;
00069                 $parser = new Parser( $wgParserConf );
00070                 
00071                 $parser->setFunctionTagHook( $tag, array( $this, 'functionTagCallback' ), SFH_OBJECT_ARGS );
00072                 $parser->parse( "Foo<$tag>Bar</$tag>Baz", Title::newFromText( 'Test' ), ParserOptions::newFromUserAndLang( new User, $wgContLang ) );
00073                 $this->fail('Exception not thrown.');
00074         }
00075         
00076         function tagCallback( $text, $params, $parser ) {
00077                 return str_rot13( $text );
00078         }
00079         
00080         function functionTagCallback( &$parser, $frame, $code, $attribs ) {
00081                 return str_rot13( $code );
00082         }
00083 }