Merge "Http::getProxy() method to get proxy configuration"
[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 [
10 [ 'foo' ],
11 [ 'foo-bar' ],
12 [ 'foo_bar' ],
13 [ 'FOO-BAR' ],
14 [ 'foo bar' ]
15 ];
16 }
17
18 public static function provideBadNames() {
19 return [ [ "foo<bar" ], [ "foo>bar" ], [ "foo\nbar" ], [ "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, [ $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, [ $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, [ $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(
88 $tag,
89 [ $this, 'functionTagCallback' ],
90 Parser::SFH_OBJECT_ARGS
91 );
92 $parser->parse(
93 "Foo<$tag>Bar</$tag>Baz",
94 Title::newFromText( 'Test' ),
95 ParserOptions::newFromUserAndLang( new User, $wgContLang )
96 );
97 $this->fail( 'Exception not thrown.' );
98 }
99
100 function tagCallback( $text, $params, $parser ) {
101 return str_rot13( $text );
102 }
103
104 function functionTagCallback( &$parser, $frame, $code, $attribs ) {
105 return str_rot13( $code );
106 }
107 }