Merge "Make addedwatchtext less verbose"
[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 protected function setUp() {
23 parent::setUp();
24
25 $this->setMwGlobals( 'wgAlwaysUseTidy', false );
26 }
27
28 /**
29 * @dataProvider provideValidNames
30 * @covers Parser::setHook
31 */
32 public function testTagHooks( $tag ) {
33 global $wgParserConf, $wgContLang;
34 $parser = new Parser( $wgParserConf );
35
36 $parser->setHook( $tag, array( $this, 'tagCallback' ) );
37 $parserOutput = $parser->parse(
38 "Foo<$tag>Bar</$tag>Baz",
39 Title::newFromText( 'Test' ),
40 ParserOptions::newFromUserAndLang( new User, $wgContLang )
41 );
42 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
43
44 $parser->mPreprocessor = null; # Break the Parser <-> Preprocessor cycle
45 }
46
47 /**
48 * @dataProvider provideBadNames
49 * @expectedException MWException
50 * @covers Parser::setHook
51 */
52 public function testBadTagHooks( $tag ) {
53 global $wgParserConf, $wgContLang;
54 $parser = new Parser( $wgParserConf );
55
56 $parser->setHook( $tag, array( $this, 'tagCallback' ) );
57 $parser->parse(
58 "Foo<$tag>Bar</$tag>Baz",
59 Title::newFromText( 'Test' ),
60 ParserOptions::newFromUserAndLang( new User, $wgContLang )
61 );
62 $this->fail( 'Exception not thrown.' );
63 }
64
65 /**
66 * @dataProvider provideValidNames
67 * @covers Parser::setFunctionTagHook
68 */
69 public function testFunctionTagHooks( $tag ) {
70 global $wgParserConf, $wgContLang;
71 $parser = new Parser( $wgParserConf );
72
73 $parser->setFunctionTagHook( $tag, array( $this, 'functionTagCallback' ), 0 );
74 $parserOutput = $parser->parse(
75 "Foo<$tag>Bar</$tag>Baz",
76 Title::newFromText( 'Test' ),
77 ParserOptions::newFromUserAndLang( new User, $wgContLang )
78 );
79 $this->assertEquals( "<p>FooOneBaz\n</p>", $parserOutput->getText() );
80
81 $parser->mPreprocessor = null; # Break the Parser <-> Preprocessor cycle
82 }
83
84 /**
85 * @dataProvider provideBadNames
86 * @expectedException MWException
87 * @covers Parser::setFunctionTagHook
88 */
89 public function testBadFunctionTagHooks( $tag ) {
90 global $wgParserConf, $wgContLang;
91 $parser = new Parser( $wgParserConf );
92
93 $parser->setFunctionTagHook( $tag, array( $this, 'functionTagCallback' ), Parser::SFH_OBJECT_ARGS );
94 $parser->parse(
95 "Foo<$tag>Bar</$tag>Baz",
96 Title::newFromText( 'Test' ),
97 ParserOptions::newFromUserAndLang( new User, $wgContLang )
98 );
99 $this->fail( 'Exception not thrown.' );
100 }
101
102 function tagCallback( $text, $params, $parser ) {
103 return str_rot13( $text );
104 }
105
106 function functionTagCallback( &$parser, $frame, $code, $attribs ) {
107 return str_rot13( $code );
108 }
109 }