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