Merge "Drop zh-tw message "saveprefs""
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / TagHooksTest.php
1 <?php
2
3 /**
4 * @group Database
5 * @group Parser
6 */
7 class TagHookTest extends MediaWikiTestCase {
8 public static function provideValidNames() {
9 return array(
10 array( 'foo' ),
11 array( 'foo-bar' ),
12 array( 'foo_bar' ),
13 array( 'FOO-BAR' ),
14 array( 'foo bar' )
15 );
16 }
17
18 public static function provideBadNames() {
19 return array( array( "foo<bar" ), array( "foo>bar" ), array( "foo\nbar" ), array( "foo\rbar" ) );
20 }
21
22 /**
23 * @dataProvider provideValidNames
24 * @covers Parser::setHook
25 */
26 public function testTagHooks( $tag ) {
27 global $wgParserConf, $wgContLang;
28 $parser = new Parser( $wgParserConf );
29
30 $parser->setHook( $tag, array( $this, 'tagCallback' ) );
31 $parserOutput = $parser->parse(
32 "Foo<$tag>Bar</$tag>Baz",
33 Title::newFromText( 'Test' ),
34 ParserOptions::newFromUserAndLang( new User, $wgContLang )
35 );
36 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
37
38 $parser->mPreprocessor = null; # Break the Parser <-> Preprocessor cycle
39 }
40
41 /**
42 * @dataProvider provideBadNames
43 * @expectedException MWException
44 * @covers Parser::setHook
45 */
46 public function testBadTagHooks( $tag ) {
47 global $wgParserConf, $wgContLang;
48 $parser = new Parser( $wgParserConf );
49
50 $parser->setHook( $tag, array( $this, 'tagCallback' ) );
51 $parser->parse(
52 "Foo<$tag>Bar</$tag>Baz",
53 Title::newFromText( 'Test' ),
54 ParserOptions::newFromUserAndLang( new User, $wgContLang )
55 );
56 $this->fail( 'Exception not thrown.' );
57 }
58
59 /**
60 * @dataProvider provideValidNames
61 * @covers Parser::setFunctionTagHook
62 */
63 public function testFunctionTagHooks( $tag ) {
64 global $wgParserConf, $wgContLang;
65 $parser = new Parser( $wgParserConf );
66
67 $parser->setFunctionTagHook( $tag, array( $this, 'functionTagCallback' ), 0 );
68 $parserOutput = $parser->parse(
69 "Foo<$tag>Bar</$tag>Baz",
70 Title::newFromText( 'Test' ),
71 ParserOptions::newFromUserAndLang( new User, $wgContLang )
72 );
73 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
74
75 $parser->mPreprocessor = null; # Break the Parser <-> Preprocessor cycle
76 }
77
78 /**
79 * @dataProvider provideBadNames
80 * @expectedException MWException
81 * @covers Parser::setFunctionTagHook
82 */
83 public function testBadFunctionTagHooks( $tag ) {
84 global $wgParserConf, $wgContLang;
85 $parser = new Parser( $wgParserConf );
86
87 $parser->setFunctionTagHook( $tag, array( $this, 'functionTagCallback' ), Parser::SFH_OBJECT_ARGS );
88 $parser->parse(
89 "Foo<$tag>Bar</$tag>Baz",
90 Title::newFromText( 'Test' ),
91 ParserOptions::newFromUserAndLang( new User, $wgContLang )
92 );
93 $this->fail( 'Exception not thrown.' );
94 }
95
96 function tagCallback( $text, $params, $parser ) {
97 return str_rot13( $text );
98 }
99
100 function functionTagCallback( &$parser, $frame, $code, $attribs ) {
101 return str_rot13( $code );
102 }
103 }