Don't look for pipes in the root node.
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / TagHooks.php
1 <?php
2
3 /**
4 * @group Parser
5 */
6 class TagHookTest extends MediaWikiTestCase {
7
8 public static function provideValidNames() {
9 return array( array( 'foo' ), array( 'foo-bar' ), array( 'foo_bar' ), array( 'FOO-BAR' ), array( 'foo bar' ) );
10 }
11
12 public static function provideBadNames() {
13 return array( array( "foo<bar" ), array( "foo>bar" ), array( "foo\nbar" ), array( "foo\rbar" ) );
14 }
15
16 /**
17 * @dataProvider provideValidNames
18 */
19 function testTagHooks( $tag ) {
20 global $wgParserConf;
21 $parser = new Parser( $wgParserConf );
22
23 $parser->setHook( $tag, array( $this, 'tagCallback' ) );
24 $parserOutput = $parser->parse( "Foo<$tag>Bar</$tag>Baz", new Title( 'Test' ), new ParserOptions );
25 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
26
27 $parser->mPreprocessor = null; # Break the Parser <-> Preprocessor cycle
28 }
29
30 /**
31 * @dataProvider provideBadNames
32 * @expectedException MWException
33 */
34 function testBadTagHooks( $tag ) {
35 global $wgParserConf;
36 $parser = new Parser( $wgParserConf );
37
38 $parser->setHook( $tag, array( $this, 'tagCallback' ) );
39 $parser->parse( "Foo<$tag>Bar</$tag>Baz", new Title( 'Test' ), new ParserOptions );
40 $this->fail('Exception not thrown.');
41 }
42
43 /**
44 * @dataProvider provideValidNames
45 */
46 function testFunctionTagHooks( $tag ) {
47 global $wgParserConf;
48 $parser = new Parser( $wgParserConf );
49
50 $parser->setFunctionTagHook( $tag, array( $this, 'functionTagCallback' ), 0 );
51 $parserOutput = $parser->parse( "Foo<$tag>Bar</$tag>Baz", new Title( 'Test' ), new ParserOptions );
52 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
53
54 $parser->mPreprocessor = null; # Break the Parser <-> Preprocessor cycle
55 }
56
57 /**
58 * @dataProvider provideBadNames
59 * @expectedException MWException
60 */
61 function testBadFunctionTagHooks( $tag ) {
62 global $wgParserConf;
63 $parser = new Parser( $wgParserConf );
64
65 $parser->setFunctionTagHook( $tag, array( $this, 'functionTagCallback' ), SFH_OBJECT_ARGS );
66 $parser->parse( "Foo<$tag>Bar</$tag>Baz", new Title( 'Test' ), new ParserOptions );
67 $this->fail('Exception not thrown.');
68 }
69
70 function tagCallback( $text, $params, $parser ) {
71 return str_rot13( $text );
72 }
73
74 function functionTagCallback( &$parser, $frame, $code, $attribs ) {
75 return str_rot13( $code );
76 }
77 }